3 #include "main.h" /* for world global */
4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE */
5 #include <stdio.h> /* for FILE typedef, F_OK */
6 #include <ncurses.h> /* for initscr(), noecho(), curs_set(), keypad(), raw() */
7 #include <time.h> /* for time() */
8 #include <unistd.h> /* for getopt(), optarg */
9 #include <stdint.h> /* for uint32_t */
10 #include "windows.h" /* for structs WinMeta, Win, init_win_meta(),
13 #include "readwrite.h" /* for read_uint32_bigendian](),
14 * write_uint32_bigendian(), try_fopen(), try_fclose(),
15 * try_fclose_unlink_rename()
17 #include "map_objects.h" /* for structs MapObj, init_map_object_defs(),
18 * build_map_objects(), get_player()
20 #include "map.h" /* for struct Map, init_map() */
21 #include "misc.h" /* for update_log(), find_passable_pos(), save_game(),
22 * try_calloc(), check_tempfile(), check_xor_files(),
23 * load_interface_conf(), load_game()
25 #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
26 #include "rrand.h" /* for rrand(), rrand_seed() */
27 #include "rexit.h" /* for exit_game(), exit_err() */
28 #include "command_db.h" /* for init_command_db(), is_command_id_shortdsc() */
29 #include "control.h" /* for *_control(), get_available_keycode_to_action() */
33 int main(int argc, char *argv[])
35 char * f_name = "main()";
36 world.turn = 0; /* Turns to 1 when map and objects are initalized. */
39 set_cleanup_flag(CLEANUP_COMMAND_DB);
41 /* Check for corrupted savefile / recordfile savings. */
42 char * recordfile = "record";
43 char * savefile = "savefile";
44 char * recordfile_tmp = "record_tmp";
45 char * savefile_tmp = "savefile_tmp";
46 check_files_xor(savefile, recordfile);
47 check_tempfile(recordfile_tmp);
48 check_tempfile(savefile_tmp);
49 check_tempfile("config/windows/Win_tmp_k");
50 check_tempfile("config/windows/Win_tmp_m");
51 check_tempfile("config/windows/Win_tmp_i");
52 check_tempfile("config/windows/Win_tmp_l");
53 check_tempfile("config/windows/toggle_order_tmp");
55 /* Read in startup options (i.e. replay option and replay start turn). */
58 world.interactive = 1;
59 while ((opt = getopt(argc, argv, "s::")) != -1)
65 world.interactive = 0;
69 start_turn = atoi(optarg);
80 /* Initialize log and map object definitions. */
82 world.log = try_calloc(1, sizeof(char), f_name);
83 set_cleanup_flag(CLEANUP_LOG);
85 init_map_object_defs("config/defs");
86 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
87 world.map_obj_count = 0;
89 /* For interactive mode, try to load world state from savefile. */
90 char * err_r = "Trouble loading game (in main()) / "
91 "reading from opened 'savefile'.";
93 if (1 == world.interactive && 0 == access(savefile, F_OK))
96 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
99 /* For non-interactive mode, try to load world state from record file. */
102 err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
104 if (0 == world.interactive)
106 file = try_fopen(recordfile, "r", f_name);
107 exit_err(read_uint32_bigendian(file, &world.seed), err_r);
110 /* For interactive-mode in newly started world, generate a start seed
111 * from the current time.
115 world.seed = time(NULL);
117 char * err_w = "Trouble recording new seed "
118 "(write_uint32_bigendian() in main()) / writing to "
119 "file 'record_tmp'.";
120 file = try_fopen(recordfile_tmp, "w", f_name);
121 exit_err(write_uint32_bigendian(world.seed, file), err_w);
122 try_fclose_unlink_rename(file, recordfile_tmp, recordfile, f_name);
126 /* Generate map from seed and, if newly generated world, start positions of
129 rrand_seed(world.seed);
130 struct Map map = init_map();
132 set_cleanup_flag(CLEANUP_MAP);
135 world.map_objs = NULL;
136 add_map_objects(0, 1);
137 add_map_objects(1, 1 + rrand() % 27);
138 add_map_objects(2, 1 + rrand() % 9);
139 add_map_objects(3, 1 + rrand() % 3);
140 add_map_objects(4, 1 + rrand() % 3);
141 add_map_objects(5, 1 + rrand() % 3);
142 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
146 /* Initialize window system and windows. */
147 WINDOW * screen = initscr();
148 set_cleanup_flag(CLEANUP_NCURSES);
151 keypad(screen, TRUE);
153 char * err_winmem = "Trouble with init_win_meta() in main ().";
154 exit_err(init_win_meta(screen, &world.wmeta), err_winmem);
155 set_cleanup_flag(CLEANUP_WIN_META);
156 load_interface_conf(/*&world*/);
157 set_cleanup_flag(CLEANUP_INTERFACE_CONF);
158 err_winmem = "Trouble with draw_all_wins() in main().";
160 /* Focus map on player. */
161 struct MapObj * player = get_player();
162 struct Win * win_map = get_win_by_id('m');
163 win_map->center = player->pos;
165 /* Initialize player's inventory selection index to start position. */
166 world.inventory_select = 0;
171 if (0 == world.interactive)
176 while (world.turn != start_turn)
183 if (is_command_id_shortdsc(action, "drop"))
185 world.inventory_select = getc(file);
187 record_control(action);
192 draw_all_wins(world.wmeta);
194 wc = get_winconf_by_win(world.wmeta->active);
195 if ( (1 == wc->view && wingeom_control(key))
196 || (2 == wc->view && winkeyb_control(key)))
201 && key == get_available_keycode_to_action("wait"))
206 if (is_command_id_shortdsc(action, "drop"))
208 world.inventory_select = getc(file);
210 record_control(action);
213 else if (meta_control(key))
215 try_fclose(file, f_name);
221 /* Interactive mode. */
227 draw_all_wins(world.wmeta);
229 wc = get_winconf_by_win(world.wmeta->active);
230 if (1 == wc->view && wingeom_control(key))
234 else if (2 == wc->view && winkeyb_control(key))
239 if ( (1 == wc->view && wingeom_control(key))
240 || (2 == wc->view && winkeyb_control(key))
241 || (0 != player->lifepoints && player_control(key)))
246 if (meta_control(key))