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() */
33 int main(int argc, char *argv[])
37 /* Check for corrupted savefile / recordfile savings. */
38 char * recordfile = "record";
39 char * savefile = "savefile";
40 char * recordfile_tmp = "record_tmp";
41 char * savefile_tmp = "savefile_tmp";
42 char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
43 "was in order, both or none would exist. I won't start "
44 "until this is corrected.";
45 if (!access(recordfile, F_OK) && access(savefile, F_OK))
48 exit_err(1, &world, err_x);
50 err_x = "A 'savefile' exists, but no file 'record'. If everything "
51 "was in order, both or none would exist. I won't start "
52 "until this is corrected.";
53 if (!access(savefile, F_OK) && access(recordfile, F_OK))
56 exit_err(1, &world, err_x);
58 err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
59 "previous record saving process. To avoid game record "
60 "corruption, I won't start until it is removed or renamed.";
61 exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
62 err_x = "A file 'savefile_tmp' exists, probably from a corrupted "
63 "previous game saving process. To avoid savegame "
64 "corruption, I won't start until it is removed or renamed.";
65 exit_err(!access(savefile_tmp, F_OK), &world, err_x);
67 /* Read in startup options (i.e. replay option and replay start turn). */
70 world.interactive = 1;
71 while ((opt = getopt(argc, argv, "s::")) != -1)
77 world.interactive = 0;
81 start_turn = atoi(optarg);
92 /* Initialize log, player, monster/item definitions and monsters/items. */
94 world.log = calloc(1, sizeof(char));
95 set_cleanup_flag(CLEANUP_LOG);
96 update_log (&world, " ");
99 world.player = &player;
102 init_map_object_defs(&world, "config/defs");
104 /* For interactive mode, try to load world state from savefile. */
105 char * err_o = "Trouble loading game (fopen() in main()) / "
106 "opening 'savefile' for reading.";
107 char * err_r = "Trouble loading game (in main()) / "
108 "reading from opened 'savefile'.";
109 char * err_c = "Trouble loading game (fclose() in main()) / "
110 "closing opened 'savefile'.";
112 if (1 == world.interactive && 0 == access(savefile, F_OK))
114 file = fopen(savefile, "r");
115 exit_err(0 == file, &world, err_o);
116 if ( read_uint32_bigendian(file, &world.seed)
117 || read_uint32_bigendian(file, &world.turn)
118 || read_uint16_bigendian(file, &world.score)
119 || read_uint16_bigendian(file, &player.pos.y)
120 || read_uint16_bigendian(file, &player.pos.x)
121 || read_uint8(file, &player.hitpoints)
122 || read_map_objects(&world, &world.monster, file)
123 || read_map_objects(&world, &world.item, file))
125 exit_err(1, &world, err_r);
127 exit_err(fclose(file), &world, err_c);
132 /* For non-interactive mode, try to load world state from record file. */
135 err_o = "Trouble loading record file (fopen() in main()) / "
136 "opening file 'record' for reading.";
137 err_r = "Trouble loading record file (read_uint32_bigendian() in "
138 "main()) / reading from opened file 'record'.";
140 if (0 == world.interactive)
142 file = fopen(recordfile, "r");
143 exit_err(NULL == file, &world, err_o);
144 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
147 /* For interactive-mode in newly started world, generate a start seed
148 * from the current time.
152 world.seed = time(NULL);
154 err_o = "Trouble recording new seed (fopen() in main()) / "
155 "opening 'record_tmp' file for writing.";
156 char * err_w = "Trouble recording new seed "
157 "(write_uint32_bigendian() in main()) / writing to "
158 "opened file 'record_tmp'.";
159 err_c = "Trouble recording new seed (fclose() in main()) / "
160 "closing opened file 'record_tmp'.";
161 char * err_m = "Trouble recording new seed (rename() in main()) : "
162 "renaming file 'record_tmp' to 'record'.";
163 file = fopen(recordfile_tmp, "w");
164 exit_err(0 == file, &world, err_o);
165 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
166 exit_err(fclose(file), &world, err_c);
167 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
172 /* Generate map from seed and, if newly generated world, start positions of
175 rrand_seed(world.seed);
176 struct Map map = init_map();
178 set_cleanup_flag(CLEANUP_MAP);
181 player.pos = find_passable_pos(world.map);
183 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
184 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
185 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
186 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
187 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
190 /* Initialize window system and windows. */
191 WINDOW * screen = initscr();
192 set_cleanup_flag(CLEANUP_NCURSES);
195 keypad(screen, TRUE);
197 init_keybindings(&world);
198 set_cleanup_flag(CLEANUP_KEYBINDINGS);
199 struct WinMeta win_meta;
200 char * err_winmem = "Trouble with init_win:meta() or draw_all_wins() in "
202 exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
203 world.wins.meta = &win_meta;
204 struct Win win_keys = init_win(&win_meta, "Keys",
205 0, 29, &world, draw_keys_win);
206 world.wins.keys = &win_keys;
207 struct Win win_info = init_win(&win_meta, "Info",
208 3, 20, &world, draw_info_win);
209 world.wins.info = &win_info;
210 uint16_t height_logwin = win_meta.padframe.size.y
211 - (2 + win_info.frame.size.y);
212 struct Win win_log = init_win(&win_meta, "Log",
213 height_logwin, 20, &world, draw_log_win);
214 world.wins.log = &win_log;
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.map = &win_map;
220 toggle_window(&win_meta, world.wins.keys);
221 toggle_window(&win_meta, world.wins.map);
222 toggle_window(&win_meta, world.wins.info);
223 toggle_window(&win_meta, world.wins.log);
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 / next turn"))
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))