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[])
37 init_command_db(&world);
38 set_cleanup_flag(CLEANUP_COMMAND_DB);
40 /* Check for corrupted savefile / recordfile savings. */
41 char * recordfile = "record";
42 char * savefile = "savefile";
43 char * recordfile_tmp = "record_tmp";
44 char * savefile_tmp = "savefile_tmp";
45 char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
46 "was in order, both or none would exist. I won't start "
47 "until this is corrected.";
48 if (!access(recordfile, F_OK) && access(savefile, F_OK))
51 exit_err(1, &world, err_x);
53 err_x = "A 'savefile' exists, but no file 'record'. If everything "
54 "was in order, both or none would exist. I won't start "
55 "until this is corrected.";
56 if (!access(savefile, F_OK) && access(recordfile, F_OK))
59 exit_err(1, &world, err_x);
61 err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
62 "previous record saving process. To avoid game record "
63 "corruption, I won't start until it is removed or renamed.";
64 exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
65 err_x = "A file 'savefile_tmp' exists, probably from a corrupted "
66 "previous game saving process. To avoid savegame "
67 "corruption, I won't start until it is removed or renamed.";
68 exit_err(!access(savefile_tmp, F_OK), &world, err_x);
70 /* Read in startup options (i.e. replay option and replay start turn). */
73 world.interactive = 1;
74 while ((opt = getopt(argc, argv, "s::")) != -1)
80 world.interactive = 0;
84 start_turn = atoi(optarg);
95 /* Initialize log, player, monster/item definitions and monsters/items. */
97 world.log = calloc(1, sizeof(char));
98 set_cleanup_flag(CLEANUP_LOG);
99 update_log (&world, " ");
100 struct Player player;
101 player.hitpoints = 5;
102 world.player = &player;
105 init_map_object_defs(&world, "config/defs");
107 /* For interactive mode, try to load world state from savefile. */
108 char * err_o = "Trouble loading game (fopen() in main()) / "
109 "opening 'savefile' for reading.";
110 char * err_r = "Trouble loading game (in main()) / "
111 "reading from opened 'savefile'.";
112 char * err_c = "Trouble loading game (fclose() in main()) / "
113 "closing opened 'savefile'.";
115 if (1 == world.interactive && 0 == access(savefile, F_OK))
117 file = fopen(savefile, "r");
118 exit_err(0 == file, &world, err_o);
119 if ( read_uint32_bigendian(file, &world.seed)
120 || read_uint32_bigendian(file, &world.turn)
121 || read_uint16_bigendian(file, &world.score)
122 || read_uint16_bigendian(file, &player.pos.y)
123 || read_uint16_bigendian(file, &player.pos.x)
124 || read_uint8(file, &player.hitpoints)
125 || read_map_objects(&world, &world.monster, file)
126 || read_map_objects(&world, &world.item, file))
128 exit_err(1, &world, err_r);
130 exit_err(fclose(file), &world, err_c);
135 /* For non-interactive mode, try to load world state from record file. */
138 err_o = "Trouble loading record file (fopen() in main()) / "
139 "opening file 'record' for reading.";
140 err_r = "Trouble loading record file (read_uint32_bigendian() in "
141 "main()) / reading from opened file 'record'.";
143 if (0 == world.interactive)
145 file = fopen(recordfile, "r");
146 exit_err(NULL == file, &world, err_o);
147 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
150 /* For interactive-mode in newly started world, generate a start seed
151 * from the current time.
155 world.seed = time(NULL);
157 err_o = "Trouble recording new seed (fopen() in main()) / "
158 "opening 'record_tmp' file for writing.";
159 char * err_w = "Trouble recording new seed "
160 "(write_uint32_bigendian() in main()) / writing to "
161 "opened file 'record_tmp'.";
162 err_c = "Trouble recording new seed (fclose() in main()) / "
163 "closing opened file 'record_tmp'.";
164 char * err_m = "Trouble recording new seed (rename() in main()) : "
165 "renaming file 'record_tmp' to 'record'.";
166 file = fopen(recordfile_tmp, "w");
167 exit_err(0 == file, &world, err_o);
168 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
169 exit_err(fclose(file), &world, err_c);
170 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
175 /* Generate map from seed and, if newly generated world, start positions of
178 rrand_seed(world.seed);
179 struct Map map = init_map();
181 set_cleanup_flag(CLEANUP_MAP);
184 player.pos = find_passable_pos(world.map);
186 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
187 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
188 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
189 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
190 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
193 /* Initialize window system and windows. */
194 WINDOW * screen = initscr();
195 set_cleanup_flag(CLEANUP_NCURSES);
198 keypad(screen, TRUE);
200 init_keybindings(&world);
201 set_cleanup_flag(CLEANUP_KEYBINDINGS);
202 struct WinMeta win_meta;
203 char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in "
205 exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
206 world.wins.meta = &win_meta;
207 struct Win win_keys = init_win(&win_meta, "Keys",
208 0, 29, &world, draw_keys_win);
209 struct Win win_info = init_win(&win_meta, "Info",
210 3, 20, &world, draw_info_win);
211 uint16_t height_logwin = win_meta.padframe.size.y
212 - (2 + win_info.frame.size.y);
213 struct Win win_log = init_win(&win_meta, "Log",
214 height_logwin, 20, &world, draw_log_win);
215 uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
216 - win_log.frame.size.x - 2;
217 struct Win win_map = init_win(&win_meta, "Map",
218 0, width_mapwin, &world, draw_map_win);
219 world.wins.keys = &win_keys;
220 world.wins.log = &win_log;
221 world.wins.info = &win_info;
222 world.wins.map = &win_map;
223 sorted_wintoggle(&world);
227 if (0 == world.interactive)
232 while (world.turn != start_turn)
239 record_control(action, &world);
244 draw_all_wins(&win_meta);
247 && key == get_action_key(world.keybindings, "wait"))
252 record_control(action, &world);
255 else if (meta_control(key, &world))
257 err_c = "Trouble closing 'record' file (fclose() in main()).";
258 exit_err(fclose(file), &world, err_c);
264 /* Interactive mode. */
270 draw_all_wins(&win_meta);
272 if (0 != player.hitpoints && 0 == player_control(key, &world))
276 if (meta_control(key, &world))