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 "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
13 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
16 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
17 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
18 #include "map_objects.h" /* for structs Monster, Item, Player,
19 * init_map_object_defs(), read_map_objects(),
22 #include "map_object_actions.h" /* for player_wait(), move_player() */
23 #include "map.h" /* for struct Map, init_map() */
24 #include "misc.h" /* for update_log(), toggle_window(), find_passable_pos(),
25 * meta_keys(), save_game()
27 #include "yx_uint16.h" /* for dir enum */
28 #include "rrand.h" /* for rrand(), rrand_seed() */
29 #include "rexit.h" /* for exit_game() */
31 int main(int argc, char *argv[])
34 char * recordfile_tmp = "record_tmp";
35 char * savefile_tmp = "savefile_tmp";
36 char * err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
37 "previous record saving process. To avoid game record "
38 "corruption, I won't start until it is removed or renamed.";
39 exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
40 err_x = "A file 'savefile_tmp' exists, probably from a corrupted "
41 "previous game saving process. To avoid savegame "
42 "corruption, I won't start until it is removed or renamed.";
43 exit_err(!access(savefile_tmp, F_OK), &world, err_x);
45 /* Read in startup options (i.e. replay option and replay start turn). */
48 world.interactive = 1;
49 while ((opt = getopt(argc, argv, "s::")) != -1)
54 world.interactive = 0;
57 start_turn = atoi(optarg);
64 /* Initialize log, player, monster/item definitions and monsters/items. */
65 world.log = calloc(1, sizeof(char));
66 set_cleanup_flag(CLEANUP_LOG);
67 update_log (&world, " ");
70 world.player = &player;
73 init_map_object_defs(&world, "defs");
75 /* For interactive mode, try to load world state from savefile. */
76 char * err_o = "Error loading game: "
77 "Unable to open 'savefile' for reading.";
78 char * err_r = "Error loading game: "
79 "Trouble reading from opened 'savefile'.";
80 char * err_c = "Error loading game: "
81 "Unable to close opened 'savefile'.";
82 char * savefile = "savefile";
84 if (1 == world.interactive && 0 == access(savefile, F_OK))
86 file = fopen(savefile, "r");
87 exit_err(0 == file, &world, err_o);
88 if ( read_uint32_bigendian(file, &world.seed)
89 || read_uint32_bigendian(file, &world.turn)
90 || read_uint16_bigendian(file, &player.pos.y)
91 || read_uint16_bigendian(file, &player.pos.x)
92 || read_uint8(file, &player.hitpoints)
93 || read_map_objects(&world, &world.monster, file)
94 || read_map_objects(&world, &world.item, file))
96 exit_err(1, &world, err_r);
98 exit_err(fclose(file), &world, err_c);
103 /* For non-interactive mode, try to load world state from record file. */
106 err_o = "Error loading record file: "
107 "Unable to open file 'record' for reading.";
108 err_r = "Error loading record file: "
109 "Trouble reading from opened file 'record'.";
110 char * recordfile = "record";
112 if (0 == world.interactive)
114 file = fopen(recordfile, "r");
115 exit_err(0 == file, &world, err_o);
116 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
119 /* For interactive-mode in newly started world, generate a start seed
120 * from the current time.
124 world.seed = time(NULL);
126 err_x = "Error recording new seed: "
127 "A file 'record' already exists, when it shouldn't.";
128 err_o = "Error recording new seed: "
129 "Unable to open 'record_tmp' file for writing.";
130 char * err_w = "Error recording new seed: "
131 "Trouble writing to opened file 'record_tmp'.";
132 err_c = "Error recording new seed: "
133 "Unable to close opened file 'record_tmp'.";
134 char * err_m = "Error recording new seed: "
135 "Unable to rename file 'record_tmp' to 'record'.";
136 exit_err(!access(recordfile, F_OK), &world, err_x);
137 file = fopen(recordfile_tmp, "w");
138 exit_err(0 == file, &world, err_o);
139 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
140 exit_err(fclose(file), &world, err_c);
141 exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
146 /* Generate map from seed and, if newly generated world, start positions of
149 rrand_seed(world.seed);
150 struct Map map = init_map();
152 set_cleanup_flag(CLEANUP_MAP);
155 player.pos = find_passable_pos(world.map);
157 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
158 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
159 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
160 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
161 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
164 /* Initialize window system and windows. */
165 WINDOW * screen = initscr();
166 set_cleanup_flag(CLEANUP_NCURSES);
169 keypad(screen, TRUE);
171 init_keybindings(&world);
172 set_cleanup_flag(CLEANUP_KEYBINDINGS);
173 struct WinMeta win_meta;
174 char * err_winmem = "Error: Window drawing memory allocation failed.";
175 exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
176 struct Win win_keys = init_win(&win_meta, "Keys",
177 0, 29, &world, draw_keys_win);
178 struct Win win_info = init_win(&win_meta, "Info",
179 2, 20, &world, draw_info_win);
180 uint16_t height_logwin = win_meta.padframe.size.y
181 - (2 + win_info.frame.size.y);
182 struct Win win_log = init_win(&win_meta, "Log",
183 height_logwin, 20, &world, draw_log_win);
184 uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
185 - win_log.frame.size.x - 2;
186 struct Win win_map = init_win(&win_meta, "Map",
187 0, width_mapwin, &world, draw_map_win);
188 toggle_window(&win_meta, &win_keys);
189 toggle_window(&win_meta, &win_map);
190 toggle_window(&win_meta, &win_info);
191 toggle_window(&win_meta, &win_log);
195 uint8_t quit_called = 0;
196 uint8_t await_actions = 1;
197 if (0 == world.interactive)
202 if (start_turn == world.turn)
208 exit_err(draw_all_wins(&win_meta), &world, err_winmem);
211 if (1 == await_actions
212 && (world.turn < start_turn
213 || key == get_action_key(world.keybindings,
214 "wait / next turn")) )
222 else if (0 == action)
224 player_wait (&world);
226 else if (NORTH == action)
228 move_player(&world, NORTH);
230 else if (EAST == action)
232 move_player(&world, EAST);
234 else if (SOUTH == action)
236 move_player(&world, SOUTH);
238 else if (WEST == action)
240 move_player(&world, WEST);
245 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
246 &win_map, &win_info, &win_log);
247 if (1 == quit_called)
249 err_c = "Error closing read 'record' file.";
250 exit_err(fclose(file), &world, err_c);
257 /* Interactive mode. */
260 uint32_t last_turn = 0;
263 if (last_turn != world.turn)
266 last_turn = world.turn;
268 if (1 == await_actions && 0 == player.hitpoints)
272 draw_all_wins (&win_meta);
274 if (1 == await_actions
275 && key == get_action_key(world.keybindings,
278 move_player(&world, NORTH);
280 else if (1 == await_actions
281 && key == get_action_key(world.keybindings,
284 move_player(&world, EAST);
286 else if (1 == await_actions
287 && key == get_action_key(world.keybindings,
290 move_player(&world, SOUTH);
292 else if (1 == await_actions
293 && key == get_action_key(world.keybindings,
296 move_player(&world, WEST);
298 else if (1 == await_actions
299 && key == get_action_key(world.keybindings,
302 player_wait (&world);
306 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
307 &win_map, &win_info, &win_log);
308 if (1 == quit_called)