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 create_winconfs(), init_winconfs(), init_wins(),
28 #include "rrand.h" /* for rrand(), rrand_seed() */
29 #include "rexit.h" /* for exit_game() */
30 #include "control.h" /* for meta_control() */
31 #include "command_db.h" /* for init_command_db() */
35 int main(int argc, char *argv[])
38 world.turn = 0; /* Turns to 1 when map and objects are initalized. */
40 init_command_db(&world);
41 set_cleanup_flag(CLEANUP_COMMAND_DB);
43 /* Check for corrupted savefile / recordfile savings. */
44 char * recordfile = "record";
45 char * savefile = "savefile";
46 char * recordfile_tmp = "record_tmp";
47 char * savefile_tmp = "savefile_tmp";
48 char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
49 "was in order, both or none would exist. I won't start "
50 "until this is corrected.";
51 if (!access(recordfile, F_OK) && access(savefile, F_OK))
54 exit_err(1, &world, err_x);
56 err_x = "A 'savefile' exists, but no file 'record'. If everything "
57 "was in order, both or none would exist. I won't start "
58 "until this is corrected.";
59 if (!access(savefile, F_OK) && access(recordfile, F_OK))
62 exit_err(1, &world, err_x);
64 err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
65 "previous record saving process. To avoid game record "
66 "corruption, I won't start until it is removed or renamed.";
67 exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
68 err_x = "A file 'savefile_tmp' exists, probably from a corrupted "
69 "previous game saving process. To avoid savegame "
70 "corruption, I won't start until it is removed or renamed.";
71 exit_err(!access(savefile_tmp, F_OK), &world, err_x);
73 /* Read in startup options (i.e. replay option and replay start turn). */
76 world.interactive = 1;
77 while ((opt = getopt(argc, argv, "s::")) != -1)
83 world.interactive = 0;
87 start_turn = atoi(optarg);
98 /* Initialize log, player, monster/item definitions and monsters/items. */
100 world.log = calloc(1, sizeof(char));
101 set_cleanup_flag(CLEANUP_LOG);
102 update_log(&world, " ");
103 struct Player player;
104 player.hitpoints = 5;
105 world.player = &player;
108 init_map_object_defs(&world, "config/defs");
109 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
111 /* For interactive mode, try to load world state from savefile. */
112 char * err_o = "Trouble loading game (fopen() in main()) / "
113 "opening 'savefile' for reading.";
114 char * err_r = "Trouble loading game (in main()) / "
115 "reading from opened 'savefile'.";
116 char * err_c = "Trouble loading game (fclose() in main()) / "
117 "closing opened 'savefile'.";
119 if (1 == world.interactive && 0 == access(savefile, F_OK))
121 file = fopen(savefile, "r");
122 exit_err(0 == file, &world, err_o);
123 if ( read_uint32_bigendian(file, &world.seed)
124 || read_uint32_bigendian(file, &world.turn)
125 || read_uint16_bigendian(file, &world.score)
126 || read_uint16_bigendian(file, &player.pos.y)
127 || read_uint16_bigendian(file, &player.pos.x)
128 || read_uint8(file, &player.hitpoints)
129 || read_map_objects(&world, &world.monster, file)
130 || read_map_objects(&world, &world.item, file))
132 exit_err(1, &world, err_r);
134 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
135 exit_err(fclose(file), &world, err_c);
140 /* For non-interactive mode, try to load world state from record file. */
143 err_o = "Trouble loading record file (fopen() in main()) / "
144 "opening file 'record' for reading.";
145 err_r = "Trouble loading record file (read_uint32_bigendian() in "
146 "main()) / reading from opened file 'record'.";
147 if (0 == world.interactive)
149 file = fopen(recordfile, "r");
150 exit_err(NULL == file, &world, err_o);
151 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
154 /* For interactive-mode in newly started world, generate a start seed
155 * from the current time.
159 world.seed = time(NULL);
161 err_o = "Trouble recording new seed (fopen() in main()) / "
162 "opening 'record_tmp' file for writing.";
163 char * err_w = "Trouble recording new seed "
164 "(write_uint32_bigendian() in main()) / writing to "
165 "opened file 'record_tmp'.";
166 err_c = "Trouble recording new seed (fclose() in main()) / "
167 "closing opened file 'record_tmp'.";
168 char * err_m = "Trouble recording new seed (rename() in main()) : "
169 "renaming file 'record_tmp' to 'record'.";
170 file = fopen(recordfile_tmp, "w");
171 exit_err(0 == file, &world, err_o);
172 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
173 exit_err(fclose(file), &world, err_c);
174 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
178 /* Generate map from seed and, if newly generated world, start positions of
181 rrand_seed(world.seed);
182 struct Map map = init_map();
184 set_cleanup_flag(CLEANUP_MAP);
187 player.pos = find_passable_pos(world.map);
189 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
190 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
191 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
192 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
193 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
194 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
198 /* Initialize window system and windows. */
199 WINDOW * screen = initscr();
200 set_cleanup_flag(CLEANUP_NCURSES);
203 keypad(screen, TRUE);
205 init_keybindings(&world);
206 set_cleanup_flag(CLEANUP_KEYBINDINGS);
207 char * err_winmem = "Trouble with init_win_meta() in main ().";
208 exit_err(init_win_meta(screen, &world.wmeta), &world, err_winmem);
209 set_cleanup_flag(CLEANUP_WIN_META);
210 // create_winconfs(&world);
211 init_winconfs(&world);
212 set_cleanup_flag(CLEANUP_WINCONFS);
214 set_cleanup_flag(CLEANUP_WINS);
215 sorted_wintoggle(&world);
216 err_winmem = "Trouble with draw_all_wins() in main().";
220 if (0 == world.interactive)
225 while (world.turn != start_turn)
232 record_control(action, &world);
237 draw_all_wins(world.wmeta);
240 && key == get_action_key(world.keybindings, "wait"))
245 record_control(action, &world);
248 else if (meta_control(key, &world))
250 err_c = "Trouble closing 'record' file (fclose() in main()).";
251 exit_err(fclose(file), &world, err_c);
257 /* Interactive mode. */
263 draw_all_wins(world.wmeta);
265 if (0 != player.hitpoints && 0 == player_control(key, &world))
269 if (meta_control(key, &world))