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(), toggle_window(), find_passable_pos(),
27 #include "rrand.h" /* for rrand(), rrand_seed() */
28 #include "rexit.h" /* for exit_game() */
29 #include "control.h" /* for meta_control() */
30 #include "command_db.h" /* for init_command_db() */
34 int main(int argc, char *argv[])
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");
108 /* For interactive mode, try to load world state from savefile. */
109 char * err_o = "Trouble loading game (fopen() in main()) / "
110 "opening 'savefile' for reading.";
111 char * err_r = "Trouble loading game (in main()) / "
112 "reading from opened 'savefile'.";
113 char * err_c = "Trouble loading game (fclose() in main()) / "
114 "closing opened 'savefile'.";
116 if (1 == world.interactive && 0 == access(savefile, F_OK))
118 file = fopen(savefile, "r");
119 exit_err(0 == file, &world, err_o);
120 if ( read_uint32_bigendian(file, &world.seed)
121 || read_uint32_bigendian(file, &world.turn)
122 || read_uint16_bigendian(file, &world.score)
123 || read_uint16_bigendian(file, &player.pos.y)
124 || read_uint16_bigendian(file, &player.pos.x)
125 || read_uint8(file, &player.hitpoints)
126 || read_map_objects(&world, &world.monster, file)
127 || read_map_objects(&world, &world.item, file))
129 exit_err(1, &world, err_r);
131 exit_err(fclose(file), &world, err_c);
136 /* For non-interactive mode, try to load world state from record file. */
139 err_o = "Trouble loading record file (fopen() in main()) / "
140 "opening file 'record' for reading.";
141 err_r = "Trouble loading record file (read_uint32_bigendian() in "
142 "main()) / reading from opened file 'record'.";
144 if (0 == world.interactive)
146 file = fopen(recordfile, "r");
147 exit_err(NULL == file, &world, err_o);
148 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
151 /* For interactive-mode in newly started world, generate a start seed
152 * from the current time.
156 world.seed = time(NULL);
158 err_o = "Trouble recording new seed (fopen() in main()) / "
159 "opening 'record_tmp' file for writing.";
160 char * err_w = "Trouble recording new seed "
161 "(write_uint32_bigendian() in main()) / writing to "
162 "opened file 'record_tmp'.";
163 err_c = "Trouble recording new seed (fclose() in main()) / "
164 "closing opened file 'record_tmp'.";
165 char * err_m = "Trouble recording new seed (rename() in main()) : "
166 "renaming file 'record_tmp' to 'record'.";
167 file = fopen(recordfile_tmp, "w");
168 exit_err(0 == file, &world, err_o);
169 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
170 exit_err(fclose(file), &world, err_c);
171 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
176 /* Generate map from seed and, if newly generated world, start positions of
179 rrand_seed(world.seed);
180 struct Map map = init_map();
182 set_cleanup_flag(CLEANUP_MAP);
185 player.pos = find_passable_pos(world.map);
187 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
188 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
189 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
190 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
191 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
194 /* Initialize window system and windows. */
195 WINDOW * screen = initscr();
196 set_cleanup_flag(CLEANUP_NCURSES);
199 keypad(screen, TRUE);
201 init_keybindings(&world);
202 set_cleanup_flag(CLEANUP_KEYBINDINGS);
203 struct WinMeta win_meta;
204 char * err_winmem = "Trouble with init_win:meta() or draw_all_wins() in "
206 exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
207 world.wins.meta = &win_meta;
208 struct Win win_keys = init_win(&win_meta, "Keys",
209 0, 29, &world, draw_keys_win);
210 world.wins.keys = &win_keys;
211 struct Win win_info = init_win(&win_meta, "Info",
212 3, 20, &world, draw_info_win);
213 world.wins.info = &win_info;
214 uint16_t height_logwin = win_meta.padframe.size.y
215 - (2 + win_info.frame.size.y);
216 struct Win win_log = init_win(&win_meta, "Log",
217 height_logwin, 20, &world, draw_log_win);
218 world.wins.log = &win_log;
219 uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
220 - win_log.frame.size.x - 2;
221 struct Win win_map = init_win(&win_meta, "Map",
222 0, width_mapwin, &world, draw_map_win);
223 world.wins.map = &win_map;
224 toggle_window(&win_meta, world.wins.keys);
225 toggle_window(&win_meta, world.wins.map);
226 toggle_window(&win_meta, world.wins.info);
227 toggle_window(&win_meta, world.wins.log);
231 if (0 == world.interactive)
236 while (world.turn != start_turn)
243 record_control(action, &world);
248 draw_all_wins(&win_meta);
251 && key == get_action_key(world.keybindings, "wait"))
256 record_control(action, &world);
259 else if (meta_control(key, &world))
261 err_c = "Trouble closing 'record' file (fclose() in main()).";
262 exit_err(fclose(file), &world, err_c);
268 /* Interactive mode. */
274 draw_all_wins(&win_meta);
276 if (0 != player.hitpoints && 0 == player_control(key, &world))
280 if (meta_control(key, &world))