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 unlink(), getopt(), optarg */
9 #include <stdint.h> /* for uint8_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 initkeybindings(), 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_object_actions.h" /* for player_wait(), move_player() */
24 #include "map.h" /* for struct Map, init_map() */
25 #include "misc.h" /* for update_log(), toggle_window(), find_passable_pos(),
28 #include "yx_uint16.h" /* for dir enum */
29 #include "rrand.h" /* for rrand(), rrand_seed() */
30 #include "rexit.h" /* for exit_game() */
31 #include "control.h" /* for meta_control() */
34 int main(int argc, char *argv[])
38 /* Check for corrupted savefile / recordfile savings. */
39 char * recordfile = "record";
40 char * savefile = "savefile";
41 char * recordfile_tmp = "record_tmp";
42 char * savefile_tmp = "savefile_tmp";
43 char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
44 "was in order, both or none would exist. I won't start "
45 "until this is corrected.";
46 if (!access(recordfile, F_OK) && access(savefile, F_OK))
49 exit_err(1, &world, err_x);
51 err_x = "A 'savefile' exists, but no file 'record'. If everything "
52 "was in order, both or none would exist. I won't start "
53 "until this is corrected.";
54 if (!access(savefile, F_OK) && access(recordfile, F_OK))
57 exit_err(1, &world, err_x);
59 err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
60 "previous record saving process. To avoid game record "
61 "corruption, I won't start until it is removed or renamed.";
62 exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
63 err_x = "A file 'savefile_tmp' exists, probably from a corrupted "
64 "previous game saving process. To avoid savegame "
65 "corruption, I won't start until it is removed or renamed.";
66 exit_err(!access(savefile_tmp, F_OK), &world, err_x);
68 /* Read in startup options (i.e. replay option and replay start turn). */
71 world.interactive = 1;
72 while ((opt = getopt(argc, argv, "s::")) != -1)
78 world.interactive = 0;
82 start_turn = atoi(optarg);
93 /* Initialize log, player, monster/item definitions and monsters/items. */
95 world.log = calloc(1, sizeof(char));
96 set_cleanup_flag(CLEANUP_LOG);
97 update_log (&world, " ");
100 world.player = &player;
103 init_map_object_defs(&world, "defs");
105 /* For interactive mode, try to load world state from savefile. */
106 char * err_o = "Trouble loading game (fopen() in main()) / "
107 "opening 'savefile' for reading.";
108 char * err_r = "Trouble loading game (in main()) / "
109 "reading from opened 'savefile'.";
110 char * err_c = "Trouble loading game (fclose() in main()) / "
111 "closing opened 'savefile'.";
113 if (1 == world.interactive && 0 == access(savefile, F_OK))
115 file = fopen(savefile, "r");
116 exit_err(0 == file, &world, err_o);
117 if ( read_uint32_bigendian(file, &world.seed)
118 || read_uint32_bigendian(file, &world.turn)
119 || read_uint16_bigendian(file, &world.score)
120 || read_uint16_bigendian(file, &player.pos.y)
121 || read_uint16_bigendian(file, &player.pos.x)
122 || read_uint8(file, &player.hitpoints)
123 || read_map_objects(&world, &world.monster, file)
124 || read_map_objects(&world, &world.item, file))
126 exit_err(1, &world, err_r);
128 exit_err(fclose(file), &world, err_c);
133 /* For non-interactive mode, try to load world state from record file. */
136 err_o = "Trouble loading record file (fopen() in main()) / "
137 "opening file 'record' for reading.";
138 err_r = "Trouble loading record file (read_uint32_bigendian() in "
139 "main()) / reading from opened file 'record'.";
141 if (0 == world.interactive)
143 file = fopen(recordfile, "r");
144 exit_err(NULL == file, &world, err_o);
145 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
148 /* For interactive-mode in newly started world, generate a start seed
149 * from the current time.
153 world.seed = time(NULL);
155 err_o = "Trouble recording new seed (fopen() in main()) / "
156 "opening 'record_tmp' file for writing.";
157 char * err_w = "Trouble recording new seed "
158 "(write_uint32_bigendian() in main()) / writing to "
159 "opened file 'record_tmp'.";
160 err_c = "Trouble recording new seed (fclose() in main()) / "
161 "closing opened file 'record_tmp'.";
162 char * err_m = "Trouble recording new seed (rename() in main()) : "
163 "renaming file 'record_tmp' to 'record'.";
164 file = fopen(recordfile_tmp, "w");
165 exit_err(0 == file, &world, err_o);
166 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
167 exit_err(fclose(file), &world, err_c);
168 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
173 /* Generate map from seed and, if newly generated world, start positions of
176 rrand_seed(world.seed);
177 struct Map map = init_map();
179 set_cleanup_flag(CLEANUP_MAP);
182 player.pos = find_passable_pos(world.map);
184 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
185 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
186 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
187 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
188 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
191 /* Initialize window system and windows. */
192 WINDOW * screen = initscr();
193 set_cleanup_flag(CLEANUP_NCURSES);
196 keypad(screen, TRUE);
198 init_keybindings(&world);
199 set_cleanup_flag(CLEANUP_KEYBINDINGS);
200 struct WinMeta win_meta;
201 char * err_winmem = "Trouble with init_win:meta() or draw_all_wins() in "
203 exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
204 world.wins.meta = &win_meta;
205 struct Win win_keys = init_win(&win_meta, "Keys",
206 0, 29, &world, draw_keys_win);
207 world.wins.keys = &win_keys;
208 struct Win win_info = init_win(&win_meta, "Info",
209 3, 20, &world, draw_info_win);
210 world.wins.info = &win_info;
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 world.wins.log = &win_log;
216 uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
217 - win_log.frame.size.x - 2;
218 struct Win win_map = init_win(&win_meta, "Map",
219 0, width_mapwin, &world, draw_map_win);
220 world.wins.map = &win_map;
221 toggle_window(&win_meta, world.wins.keys);
222 toggle_window(&win_meta, world.wins.map);
223 toggle_window(&win_meta, world.wins.info);
224 toggle_window(&win_meta, world.wins.log);
228 if (0 == world.interactive)
233 while (world.turn != start_turn)
240 record_control(action, &world);
245 draw_all_wins(&win_meta);
248 && key == get_action_key(world.keybindings, "wait / next turn"))
253 record_control(action, &world);
256 else if (meta_control(key, &world))
258 err_c = "Trouble closing 'record' file (fclose() in main()).";
259 exit_err(fclose(file), &world, err_c);
265 /* Interactive mode. */
271 draw_all_wins(&win_meta);
273 if (0 != player.hitpoints && 0 == player_control(key, &world))
277 if (meta_control(key, &world))