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()";
37 world.turn = 0; /* Turns to 1 when map and objects are initalized. */
39 init_command_db(&world);
40 set_cleanup_flag(CLEANUP_COMMAND_DB);
42 /* Check for corrupted savefile / recordfile savings. */
43 char * recordfile = "record";
44 char * savefile = "savefile";
45 char * recordfile_tmp = "record_tmp";
46 char * savefile_tmp = "savefile_tmp";
47 check_files_xor(savefile, recordfile, &world);
48 check_tempfile(recordfile_tmp, &world);
49 check_tempfile(savefile_tmp, &world);
50 check_tempfile("config/windows/Win_tmp_k", &world);
51 check_tempfile("config/windows/Win_tmp_m", &world);
52 check_tempfile("config/windows/Win_tmp_i", &world);
53 check_tempfile("config/windows/Win_tmp_l", &world);
54 check_tempfile("config/windows/toggle_order_tmp", &world);
56 /* Read in startup options (i.e. replay option and replay start turn). */
59 world.interactive = 1;
60 while ((opt = getopt(argc, argv, "s::")) != -1)
66 world.interactive = 0;
70 start_turn = atoi(optarg);
81 /* Initialize log and map object definitions. */
83 world.log = try_calloc(1, sizeof(char), &world, f_name);
84 set_cleanup_flag(CLEANUP_LOG);
85 update_log(&world, " ");
86 init_map_object_defs(&world, "config/defs");
87 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
88 world.map_obj_count = 0;
90 /* For interactive mode, try to load world state from savefile. */
91 char * err_r = "Trouble loading game (in main()) / "
92 "reading from opened 'savefile'.";
94 if (1 == world.interactive && 0 == access(savefile, F_OK))
97 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
100 /* For non-interactive mode, try to load world state from record file. */
103 err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
105 if (0 == world.interactive)
107 file = try_fopen(recordfile, "r", &world, f_name);
108 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
111 /* For interactive-mode in newly started world, generate a start seed
112 * from the current time.
116 world.seed = time(NULL);
118 char * err_w = "Trouble recording new seed "
119 "(write_uint32_bigendian() in main()) / writing to "
120 "file 'record_tmp'.";
121 file = try_fopen(recordfile_tmp, "w", &world, f_name);
122 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
123 try_fclose_unlink_rename(file, recordfile_tmp, recordfile,
128 /* Generate map from seed and, if newly generated world, start positions of
131 rrand_seed(world.seed);
132 struct Map map = init_map();
134 set_cleanup_flag(CLEANUP_MAP);
137 world.map_objs = NULL;
138 add_map_objects(&world, 0, 1);
139 add_map_objects(&world, 1, 1 + rrand() % 27);
140 add_map_objects(&world, 2, 1 + rrand() % 9);
141 add_map_objects(&world, 3, 1 + rrand() % 3);
142 add_map_objects(&world, 4, 1 + rrand() % 3);
143 add_map_objects(&world, 5, 1 + rrand() % 3);
144 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
148 /* Initialize window system and windows. */
149 WINDOW * screen = initscr();
150 set_cleanup_flag(CLEANUP_NCURSES);
153 keypad(screen, TRUE);
155 char * err_winmem = "Trouble with init_win_meta() in main ().";
156 exit_err(init_win_meta(screen, &world.wmeta), &world, err_winmem);
157 set_cleanup_flag(CLEANUP_WIN_META);
158 load_interface_conf(&world);
159 set_cleanup_flag(CLEANUP_INTERFACE_CONF);
160 err_winmem = "Trouble with draw_all_wins() in main().";
162 /* Focus map on player. */
163 struct MapObj * player = get_player(&world);
164 struct Win * win_map = get_win_by_id(&world, 'm');
165 win_map->center = player->pos;
167 /* Initialize player's inventory selection index to start position. */
168 world.inventory_select = 0;
173 if (0 == world.interactive)
178 while (world.turn != start_turn)
185 if (is_command_id_shortdsc(&world, action, "drop"))
187 world.inventory_select = getc(file);
189 record_control(action, &world);
194 draw_all_wins(world.wmeta);
196 wc = get_winconf_by_win(&world, world.wmeta->active);
197 if ( (1 == wc->view && wingeom_control(key, &world))
198 || (2 == wc->view && winkeyb_control(key, &world)))
203 && key == get_available_keycode_to_action(&world, "wait"))
208 if (is_command_id_shortdsc(&world, action, "drop"))
210 world.inventory_select = getc(file);
212 record_control(action, &world);
215 else if (meta_control(key, &world))
217 try_fclose(file, &world, f_name);
223 /* Interactive mode. */
229 draw_all_wins(world.wmeta);
231 wc = get_winconf_by_win(&world, world.wmeta->active);
232 if (1 == wc->view && wingeom_control(key, &world))
236 else if (2 == wc->view && winkeyb_control(key, &world))
241 if ( (1 == wc->view && wingeom_control(key, &world))
242 || (2 == wc->view && winkeyb_control(key, &world))
243 || (0 != player->lifepoints && player_control(key, &world)))
248 if (meta_control(key, &world))