4 #include <stdio.h> /* for rename() */
5 #include <unistd.h> /* for unlink(), acess() */
6 #include <stdlib.h> /* for calloc(), free() */
7 #include <string.h> /* for strlen(), strcmp(), memcpy() */
8 #include <stdint.h> /* for uint8_t */
9 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
10 * resize_active_win(), cycle_active_win(),
11 * shift_active_win(), struct Win, struct WinMeta
13 #include "keybindings.h" /* for get_action_key(), save_keybindings(),
14 * keyswin_move_selection(), keyswin_mod_key()
16 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
17 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
18 #include "map_object_actions.h" /* for is_passable(), move_monster() */
19 #include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */
20 #include "main.h" /* for World struct */
21 #include "yx_uint16.h" /* for yx_uint16 */
22 #include "rrand.h" /* for rrand(), rrand_seed() */
23 #include "rexit.h" /* for exit_err() */
26 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
39 if (c_count > linemax)
41 linemax = c_count + 1;
50 fseek(file, 0, SEEK_SET);
51 * linemax_p = linemax;
54 * n_lines_p = n_lines;
60 extern void update_log(struct World * world, char * text)
62 static char * last_msg;
65 last_msg = calloc(1, sizeof(char));
68 uint16_t len_old = strlen(world->log);
69 if (0 == strcmp(last_msg, text))
71 uint16_t len_whole = len_old + 1;
72 new_text = calloc(len_whole + 1, sizeof(char));
73 memcpy(new_text, world->log, len_old);
74 memcpy(new_text + len_old, ".", 1);
78 uint16_t len_new = strlen(text);
79 uint16_t len_whole = len_old + len_new + 1;
80 new_text = calloc(len_whole, sizeof(char));
81 memcpy(new_text, world->log, len_old);
82 memcpy(new_text + len_old, text, len_new);
83 last_msg = calloc(len_new + 1, sizeof(char));
84 memcpy(last_msg, text, len_new);
87 world->log = new_text;
92 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
96 if (mapsize > framesize)
98 if (pos > framesize / 2)
100 if (pos < mapsize - (framesize / 2))
102 offset = pos - (framesize / 2);
106 offset = mapsize - framesize;
115 extern void turn_over(struct World * world, char action)
117 char * err_open = "Trouble in turn_over() with fopen() "
118 "opening file 'record_tmp' for appending.";
119 char * err_write = "Trouble in turn_over() with write_uint8() "
120 "writing to opened file 'record_tmp'.";
121 char * err_close = "Trouble in turn_over() with fclose() "
122 "closing opened file 'record_tmp'.";
123 char * err_unl = "Trouble in turn_over() with unlink() "
124 "unlinking old file 'record'.";
125 char * err_move = "Trouble in turn_over() with rename() "
126 "renaming file 'record_tmp' to 'record'.";
127 char * recordfile_tmp = "record_tmp";
128 char * recordfile = "record";
129 if (1 == world->interactive)
131 FILE * file = fopen(recordfile_tmp, "a");
132 exit_err(0 == file, world, err_open);
133 exit_err(write_uint8(action, file), world, err_write);
134 exit_err(fclose(file), world, err_close);
135 exit_err(unlink(recordfile), world, err_unl);
136 exit_err(rename(recordfile_tmp, recordfile), world, err_move);
140 rrand_seed(world->seed * world->turn);
141 struct Monster * monster;
142 for (monster = world->monster;
144 monster = monster->map_obj.next)
146 move_monster(world, monster);
152 extern void save_game(struct World * world)
154 char * err_open = "Trouble in save_game() with fopen() "
155 "opening file 'savefile_tmp' for writing.";
156 char * err_write = "Trouble in save_game() "
157 "writing to opened file 'savefile_tmp'.";
158 char * err_close = "Trouble in save_game() with fclose() "
159 "closing opened file 'savefile_tmp'.";
160 char * err_unl = "Trouble in save_game() with unlink() "
161 "unlinking old 'savefile' file.";
162 char * err_move = "Trouble in save_game() with rename() "
163 "renaming 'file savefile_tmp' to 'savefile'.";
164 char * savefile_tmp = "savefile_tmp";
165 char * savefile = "savefile";
166 FILE * file = fopen(savefile_tmp, "w");
167 exit_err(0 == file, world, err_open);
168 if ( write_uint32_bigendian(world->seed, file)
169 || write_uint32_bigendian(world->turn, file)
170 || write_uint16_bigendian(world->player->pos.y + 1, file)
171 || write_uint16_bigendian(world->player->pos.x + 1, file)
172 || write_uint8(world->player->hitpoints, file)
173 || write_map_objects(world, world->monster, file)
174 || write_map_objects(world, world->item, file))
176 exit_err(1, world, err_write);
178 exit_err(fclose(file), world, err_close);
179 if (!access(savefile, F_OK))
181 exit_err(unlink(savefile), world, err_unl);
183 exit_err(rename(savefile_tmp, savefile), world, err_move);
188 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
190 if (0 != win->frame.curses_win)
192 return suspend_win(win_meta, win);
196 return append_win(win_meta, win);
202 extern void scroll_pad(struct WinMeta * win_meta, char dir)
206 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
210 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
216 extern uint8_t growshrink_active_window(struct WinMeta * win_meta, char change)
218 if (0 != win_meta->active)
220 struct yx_uint16 size = win_meta->active->frame.size;
225 else if (change == '+')
229 else if (change == '_')
233 else if (change == '*')
237 return resize_active_win (win_meta, size);
244 extern struct yx_uint16 find_passable_pos(struct Map * map)
246 struct yx_uint16 pos;
247 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
249 pos.y = rrand() % map->size.y;
250 pos.x = rrand() % map->size.x;
257 extern uint8_t meta_keys(int key, struct World * world,
258 struct WinMeta * win_meta, struct Win * win_keys,
259 struct Win * win_map, struct Win * win_info,
260 struct Win * win_log)
262 char * err_toggle = "Trouble with toggle_window() in meta_keys().";
263 char * err_shift = "Trouble with shift_active_win() in meta_keys().";
264 char * err_resize = "Trouble with growshrink_active_window() in "
266 if (key == get_action_key(world->keybindings, "quit"))
270 else if (key == get_action_key(world->keybindings, "scroll pad right"))
272 scroll_pad (win_meta, '+');
274 else if (key == get_action_key(world->keybindings, "scroll pad left"))
276 scroll_pad (win_meta, '-');
278 else if (key == get_action_key(world->keybindings, "toggle keys window"))
280 exit_err(toggle_window(win_meta, win_keys), world, err_toggle);
282 else if (key == get_action_key(world->keybindings, "toggle map window"))
284 exit_err(toggle_window(win_meta, win_map), world, err_toggle);
286 else if (key == get_action_key(world->keybindings, "toggle info window"))
288 exit_err(toggle_window(win_meta, win_info), world, err_toggle);
290 else if (key == get_action_key(world->keybindings, "toggle log window"))
292 exit_err(toggle_window(win_meta, win_log), world, err_toggle);
294 else if (key == get_action_key(world->keybindings, "cycle forwards"))
296 cycle_active_win(win_meta, 'n');
298 else if (key == get_action_key(world->keybindings, "cycle backwards"))
300 cycle_active_win(win_meta, 'p');
302 else if (key == get_action_key(world->keybindings, "shift forwards"))
304 exit_err(shift_active_win(win_meta, 'f'), world, err_shift);
306 else if (key == get_action_key(world->keybindings, "shift backwards"))
308 exit_err(shift_active_win(win_meta, 'b'), world, err_shift);
310 else if (key == get_action_key(world->keybindings, "grow horizontally"))
312 exit_err(growshrink_active_window(win_meta, '*'), world, err_resize);
314 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
316 exit_err(growshrink_active_window(win_meta, '_'), world, err_resize);
318 else if (key == get_action_key(world->keybindings, "grow vertically"))
320 exit_err(growshrink_active_window(win_meta, '+'), world, err_resize);
322 else if (key == get_action_key(world->keybindings, "shrink vertically"))
324 exit_err(growshrink_active_window(win_meta, '-'), world, err_resize);
326 else if (key == get_action_key(world->keybindings, "save keys"))
328 save_keybindings(world);
330 else if (key == get_action_key(world->keybindings, "keys nav up"))
332 keyswin_move_selection (world, 'u');
334 else if (key == get_action_key(world->keybindings, "keys nav down"))
336 keyswin_move_selection (world, 'd');
338 else if (key == get_action_key(world->keybindings, "keys mod"))
340 keyswin_mod_key (world, win_meta);
342 else if (key == get_action_key(world->keybindings, "map up"))
344 map_scroll (world->map, NORTH, win_map->frame.size);
346 else if (key == get_action_key(world->keybindings, "map down"))
348 map_scroll (world->map, SOUTH, win_map->frame.size);
350 else if (key == get_action_key(world->keybindings, "map right"))
352 map_scroll (world->map, EAST, win_map->frame.size);
354 else if (key == get_action_key(world->keybindings, "map left"))
356 map_scroll (world->map, WEST, win_map->frame.size);
358 else if (key == get_action_key(world->keybindings, "map center player"))
360 map_center_player (world->map, world->player, win_map->frame.size);