4 #include <stdlib.h> /* for exit(), EXIT_SUCCESS define, calloc(), free() */
5 #include <string.h> /* for strlen(), strcmp(), memcpy() */
6 #include <ncurses.h> /* for endwin() */
7 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
8 * resize_active_win(), cycle_active_win(),
9 * shift_active_win(), struct Win, struct WinMeta
11 #include "keybindings.h" /* for get_action_key(), save_keybindings(),
12 * keyswin_move_selection(), keyswin_mod_key()
14 #include "readwrite.h" /* for write_uint16_bigendian(), write_uint32_bigendian()
16 #include "map_objects.h" /* for struct Monster, write_map_objects(),
17 * write_map_objects_monsterdata()
19 #include "map_object_actions.h" /* for is_passable(), move_monster() */
20 #include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */
21 #include "main.h" /* for World struct */
22 #include "yx_uint16.h" /* for yx_uint16 */
23 #include "rrand.h" /* for rrand(), rrand_seed() */
27 extern void exit_game(struct World * world, struct Map * map)
32 for (key = 0; key <= world->keyswindata->max; key++)
34 free(world->keybindings[key].name);
36 free(world->keybindings);
37 free(world->keyswindata);
44 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
57 if (c_count > linemax)
59 linemax = c_count + 1;
68 fseek(file, 0, SEEK_SET);
69 * linemax_p = linemax;
72 * n_lines_p = n_lines;
78 extern void update_log(struct World * world, char * text)
80 static char * last_msg;
83 last_msg = calloc(1, sizeof(char));
86 uint16_t len_old = strlen(world->log);
87 if (0 == strcmp(last_msg, text))
89 uint16_t len_whole = len_old + 1;
90 new_text = calloc(len_whole + 1, sizeof(char));
91 memcpy(new_text, world->log, len_old);
92 memcpy(new_text + len_old, ".", 1);
96 uint16_t len_new = strlen(text);
97 uint16_t len_whole = len_old + len_new + 1;
98 new_text = calloc(len_whole, sizeof(char));
99 memcpy(new_text, world->log, len_old);
100 memcpy(new_text + len_old, text, len_new);
101 last_msg = calloc(len_new + 1, sizeof(char));
102 memcpy(last_msg, text, len_new);
105 world->log = new_text;
110 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
114 if (mapsize > framesize)
116 if (pos > framesize / 2)
118 if (pos < mapsize - (framesize / 2))
120 offset = pos - (framesize / 2);
124 offset = mapsize - framesize;
133 extern void turn_over(struct World * world, char action)
135 if (1 == world->interactive)
137 FILE * file = fopen("record", "a");
142 rrand_seed(world->seed * world->turn);
143 struct Monster * monster;
144 for (monster = world->monster;
146 monster = monster->map_obj.next)
148 move_monster(world, monster);
154 extern void save_game(struct World * world)
156 FILE * file = fopen("savefile", "w");
157 write_uint32_bigendian(world->seed, file);
158 write_uint32_bigendian(world->turn, file);
159 write_uint16_bigendian(world->player->pos.y + 1, file);
160 write_uint16_bigendian(world->player->pos.x + 1, file);
161 fputc(world->player->hitpoints, file);
162 write_map_objects (world->monster, file, write_map_objects_monsterdata);
163 write_map_objects (world->item, file, NULL);
169 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
171 if (0 != win->frame.curses_win)
173 suspend_win(win_meta, win);
177 append_win(win_meta, win);
183 extern void scroll_pad(struct WinMeta * win_meta, char dir)
187 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
191 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
197 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
199 if (0 != win_meta->active)
201 struct yx_uint16 size = win_meta->active->frame.size;
206 else if (change == '+')
210 else if (change == '_')
214 else if (change == '*')
218 resize_active_win (win_meta, size);
224 extern struct yx_uint16 find_passable_pos(struct Map * map)
226 struct yx_uint16 pos;
227 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
229 pos.y = rrand() % map->size.y;
230 pos.x = rrand() % map->size.x;
237 extern unsigned char meta_keys(int key, struct World * world,
238 struct WinMeta * win_meta,
239 struct Win * win_keys,
240 struct Win * win_map,
241 struct Win * win_info,
242 struct Win * win_log)
244 if (key == get_action_key(world->keybindings, "quit"))
248 else if (key == get_action_key(world->keybindings, "scroll pad right"))
250 scroll_pad (win_meta, '+');
252 else if (key == get_action_key(world->keybindings, "scroll pad left"))
254 scroll_pad (win_meta, '-');
256 else if (key == get_action_key(world->keybindings, "toggle keys window"))
258 toggle_window(win_meta, win_keys);
260 else if (key == get_action_key(world->keybindings, "toggle map window"))
262 toggle_window(win_meta, win_map);
264 else if (key == get_action_key(world->keybindings, "toggle info window"))
266 toggle_window(win_meta, win_info);
268 else if (key == get_action_key(world->keybindings, "toggle log window"))
270 toggle_window(win_meta, win_log);
272 else if (key == get_action_key(world->keybindings, "cycle forwards"))
274 cycle_active_win(win_meta, 'n');
276 else if (key == get_action_key(world->keybindings, "cycle backwards"))
278 cycle_active_win(win_meta, 'p');
280 else if (key == get_action_key(world->keybindings, "shift forwards"))
282 shift_active_win(win_meta, 'f');
284 else if (key == get_action_key(world->keybindings, "shift backwards"))
286 shift_active_win(win_meta, 'b');
288 else if (key == get_action_key(world->keybindings, "grow horizontally"))
290 growshrink_active_window(win_meta, '*');
292 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
294 growshrink_active_window(win_meta, '_');
296 else if (key == get_action_key(world->keybindings, "grow vertically"))
298 growshrink_active_window(win_meta, '+');
300 else if (key == get_action_key(world->keybindings, "shrink vertically"))
302 growshrink_active_window(win_meta, '-');
304 else if (key == get_action_key(world->keybindings, "save keys"))
306 save_keybindings(world);
308 else if (key == get_action_key(world->keybindings, "keys nav up"))
310 keyswin_move_selection (world, 'u');
312 else if (key == get_action_key(world->keybindings, "keys nav down"))
314 keyswin_move_selection (world, 'd');
316 else if (key == get_action_key(world->keybindings, "keys mod"))
318 keyswin_mod_key (world, win_meta);
320 else if (key == get_action_key(world->keybindings, "map up"))
322 map_scroll (world->map, NORTH, win_map->frame.size);
324 else if (key == get_action_key(world->keybindings, "map down"))
326 map_scroll (world->map, SOUTH, win_map->frame.size);
328 else if (key == get_action_key(world->keybindings, "map right"))
330 map_scroll (world->map, EAST, win_map->frame.size);
332 else if (key == get_action_key(world->keybindings, "map left"))
334 map_scroll (world->map, WEST, win_map->frame.size);
336 else if (key == get_action_key(world->keybindings, "map center player"))
338 map_center_player (world->map, world->player, win_map->frame.size);