4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE, calloc() */
5 #include <stdio.h> /* for FILE typedef, F_OK, rename() */
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 uint16_t, uint32_t */
10 #include <errno.h> /* for errno */
11 #include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
14 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
17 #include "keybindings.h" /* for init_keybindings(), get_action_key() */
18 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
19 #include "map_objects.h" /* for structs Monster, Item, Player,
20 * init_map_object_defs(), read_map_objects(),
23 #include "map.h" /* for struct Map, init_map() */
24 #include "misc.h" /* for update_log(), find_passable_pos(), save_game() */
25 #include "wincontrol.h" /* for toggle_window() */
26 #include "rrand.h" /* for rrand(), rrand_seed() */
27 #include "rexit.h" /* for exit_game() */
28 #include "control.h" /* for meta_control() */
29 #include "command_db.h" /* for init_command_db() */
33 int main(int argc, char *argv[])
36 world.turn = 0; /* Turns to 1 when map and objects are initalized. */
38 init_command_db(&world);
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 char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
47 "was in order, both or none would exist. I won't start "
48 "until this is corrected.";
49 if (!access(recordfile, F_OK) && access(savefile, F_OK))
52 exit_err(1, &world, err_x);
54 err_x = "A 'savefile' exists, but no file 'record'. If everything "
55 "was in order, both or none would exist. I won't start "
56 "until this is corrected.";
57 if (!access(savefile, F_OK) && access(recordfile, F_OK))
60 exit_err(1, &world, err_x);
62 err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
63 "previous record saving process. To avoid game record "
64 "corruption, I won't start until it is removed or renamed.";
65 exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
66 err_x = "A file 'savefile_tmp' exists, probably from a corrupted "
67 "previous game saving process. To avoid savegame "
68 "corruption, I won't start until it is removed or renamed.";
69 exit_err(!access(savefile_tmp, F_OK), &world, err_x);
71 /* Read in startup options (i.e. replay option and replay start turn). */
74 world.interactive = 1;
75 while ((opt = getopt(argc, argv, "s::")) != -1)
81 world.interactive = 0;
85 start_turn = atoi(optarg);
96 /* Initialize log, player, monster/item definitions and monsters/items. */
98 world.log = calloc(1, sizeof(char));
99 set_cleanup_flag(CLEANUP_LOG);
100 update_log(&world, " ");
101 struct Player player;
102 player.hitpoints = 5;
103 world.player = &player;
106 init_map_object_defs(&world, "config/defs");
107 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
109 /* For interactive mode, try to load world state from savefile. */
110 char * err_o = "Trouble loading game (fopen() in main()) / "
111 "opening 'savefile' for reading.";
112 char * err_r = "Trouble loading game (in main()) / "
113 "reading from opened 'savefile'.";
114 char * err_c = "Trouble loading game (fclose() in main()) / "
115 "closing opened 'savefile'.";
117 if (1 == world.interactive && 0 == access(savefile, F_OK))
119 file = fopen(savefile, "r");
120 exit_err(0 == file, &world, err_o);
121 if ( read_uint32_bigendian(file, &world.seed)
122 || read_uint32_bigendian(file, &world.turn)
123 || read_uint16_bigendian(file, &world.score)
124 || read_uint16_bigendian(file, &player.pos.y)
125 || read_uint16_bigendian(file, &player.pos.x)
126 || read_uint8(file, &player.hitpoints)
127 || read_map_objects(&world, &world.monster, file)
128 || read_map_objects(&world, &world.item, file))
130 exit_err(1, &world, err_r);
132 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
133 exit_err(fclose(file), &world, err_c);
139 /* For non-interactive mode, try to load world state from record file. */
142 err_o = "Trouble loading record file (fopen() in main()) / "
143 "opening file 'record' for reading.";
144 err_r = "Trouble loading record file (read_uint32_bigendian() in "
145 "main()) / reading from opened file 'record'.";
146 if (0 == world.interactive)
148 file = fopen(recordfile, "r");
149 exit_err(NULL == file, &world, err_o);
150 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
153 /* For interactive-mode in newly started world, generate a start seed
154 * from the current time.
158 world.seed = time(NULL);
160 err_o = "Trouble recording new seed (fopen() in main()) / "
161 "opening 'record_tmp' file for writing.";
162 char * err_w = "Trouble recording new seed "
163 "(write_uint32_bigendian() in main()) / writing to "
164 "opened file 'record_tmp'.";
165 err_c = "Trouble recording new seed (fclose() in main()) / "
166 "closing opened file 'record_tmp'.";
167 char * err_m = "Trouble recording new seed (rename() in main()) : "
168 "renaming file 'record_tmp' to 'record'.";
169 file = fopen(recordfile_tmp, "w");
170 exit_err(0 == file, &world, err_o);
171 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
172 exit_err(fclose(file), &world, err_c);
173 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
177 /* Generate map from seed and, if newly generated world, start positions of
180 rrand_seed(world.seed);
181 struct Map map = init_map();
183 set_cleanup_flag(CLEANUP_MAP);
186 player.pos = find_passable_pos(world.map);
188 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
189 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
190 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
191 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
192 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
193 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
197 /* Initialize window system and windows. */
198 WINDOW * screen = initscr();
199 set_cleanup_flag(CLEANUP_NCURSES);
202 keypad(screen, TRUE);
204 init_keybindings(&world);
205 set_cleanup_flag(CLEANUP_KEYBINDINGS);
206 char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in "
208 exit_err(init_win_meta(screen, &world.wins.meta), &world, err_winmem);
209 set_cleanup_flag(CLEANUP_WIN_META);
210 world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win);
211 set_cleanup_flag(CLEANUP_WIN_KEYS);
212 world.wins.info = init_win_from_file(&world, "Info", draw_info_win);
213 set_cleanup_flag(CLEANUP_WIN_INFO);
214 world.wins.log = init_win_from_file(&world, "Log", draw_log_win);
215 set_cleanup_flag(CLEANUP_WIN_LOG);
216 world.wins.map = init_win_from_file(&world, "Map", draw_map_win);
217 set_cleanup_flag(CLEANUP_WIN_MAP);
219 sorted_wintoggle(&world);
223 if (0 == world.interactive)
228 while (world.turn != start_turn)
235 record_control(action, &world);
240 draw_all_wins(world.wins.meta);
243 && key == get_action_key(world.keybindings, "wait"))
248 record_control(action, &world);
251 else if (meta_control(key, &world))
253 err_c = "Trouble closing 'record' file (fclose() in main()).";
254 exit_err(fclose(file), &world, err_c);
260 /* Interactive mode. */
266 draw_all_wins(world.wins.meta);
268 if (0 != player.hitpoints && 0 == player_control(key, &world))
272 if (meta_control(key, &world))