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 if (1 == world->interactive)
119 FILE * file = fopen("record", "a");
120 exit_err(write_uint8(action, file), world, "Record writing failure.");
124 rrand_seed(world->seed * world->turn);
125 struct Monster * monster;
126 for (monster = world->monster;
128 monster = monster->map_obj.next)
130 move_monster(world, monster);
136 extern void save_game(struct World * world)
138 char * savefile_tmp = "savefile_tmp";
139 char * savefile = "savefile";
140 FILE * file = fopen(savefile_tmp, "w");
141 exit_err(0 == file, world,
142 "Error saving game: Unable to open new savefile for writing.");
143 if ( write_uint32_bigendian(world->seed, file)
144 || write_uint32_bigendian(world->turn, file)
145 || write_uint16_bigendian(world->player->pos.y + 1, file)
146 || write_uint16_bigendian(world->player->pos.x + 1, file)
147 || write_uint8(world->player->hitpoints, file)
148 || write_map_objects(world, world->monster, file)
149 || write_map_objects(world, world->item, file))
152 "Error saving game: Trouble writing to opened new savefile.");
154 exit_err(fclose(file), world,
155 "Error saving game: Unable to close opened new savefile.");
156 if (!access(savefile, F_OK))
158 exit_err(unlink(savefile), world,
159 "Error saving game: Unable to unlink old savefile.");
161 exit_err(rename(savefile_tmp, savefile), world,
162 "Error saving game: Unable to rename 'savefile_tmp' to "
168 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
170 if (0 != win->frame.curses_win)
172 suspend_win(win_meta, win);
176 append_win(win_meta, win);
182 extern void scroll_pad(struct WinMeta * win_meta, char dir)
186 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
190 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
196 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
198 if (0 != win_meta->active)
200 struct yx_uint16 size = win_meta->active->frame.size;
205 else if (change == '+')
209 else if (change == '_')
213 else if (change == '*')
217 resize_active_win (win_meta, size);
223 extern struct yx_uint16 find_passable_pos(struct Map * map)
225 struct yx_uint16 pos;
226 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
228 pos.y = rrand() % map->size.y;
229 pos.x = rrand() % map->size.x;
236 extern uint8_t meta_keys(int key, struct World * world,
237 struct WinMeta * win_meta, struct Win * win_keys,
238 struct Win * win_map, struct Win * win_info,
239 struct Win * win_log)
241 if (key == get_action_key(world->keybindings, "quit"))
245 else if (key == get_action_key(world->keybindings, "scroll pad right"))
247 scroll_pad (win_meta, '+');
249 else if (key == get_action_key(world->keybindings, "scroll pad left"))
251 scroll_pad (win_meta, '-');
253 else if (key == get_action_key(world->keybindings, "toggle keys window"))
255 toggle_window(win_meta, win_keys);
257 else if (key == get_action_key(world->keybindings, "toggle map window"))
259 toggle_window(win_meta, win_map);
261 else if (key == get_action_key(world->keybindings, "toggle info window"))
263 toggle_window(win_meta, win_info);
265 else if (key == get_action_key(world->keybindings, "toggle log window"))
267 toggle_window(win_meta, win_log);
269 else if (key == get_action_key(world->keybindings, "cycle forwards"))
271 cycle_active_win(win_meta, 'n');
273 else if (key == get_action_key(world->keybindings, "cycle backwards"))
275 cycle_active_win(win_meta, 'p');
277 else if (key == get_action_key(world->keybindings, "shift forwards"))
279 shift_active_win(win_meta, 'f');
281 else if (key == get_action_key(world->keybindings, "shift backwards"))
283 shift_active_win(win_meta, 'b');
285 else if (key == get_action_key(world->keybindings, "grow horizontally"))
287 growshrink_active_window(win_meta, '*');
289 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
291 growshrink_active_window(win_meta, '_');
293 else if (key == get_action_key(world->keybindings, "grow vertically"))
295 growshrink_active_window(win_meta, '+');
297 else if (key == get_action_key(world->keybindings, "shrink vertically"))
299 growshrink_active_window(win_meta, '-');
301 else if (key == get_action_key(world->keybindings, "save keys"))
303 save_keybindings(world);
305 else if (key == get_action_key(world->keybindings, "keys nav up"))
307 keyswin_move_selection (world, 'u');
309 else if (key == get_action_key(world->keybindings, "keys nav down"))
311 keyswin_move_selection (world, 'd');
313 else if (key == get_action_key(world->keybindings, "keys mod"))
315 keyswin_mod_key (world, win_meta);
317 else if (key == get_action_key(world->keybindings, "map up"))
319 map_scroll (world->map, NORTH, win_map->frame.size);
321 else if (key == get_action_key(world->keybindings, "map down"))
323 map_scroll (world->map, SOUTH, win_map->frame.size);
325 else if (key == get_action_key(world->keybindings, "map right"))
327 map_scroll (world->map, EAST, win_map->frame.size);
329 else if (key == get_action_key(world->keybindings, "map left"))
331 map_scroll (world->map, WEST, win_map->frame.size);
333 else if (key == get_action_key(world->keybindings, "map center player"))
335 map_center_player (world->map, world->player, win_map->frame.size);