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 */
26 extern void exit_game(struct World * world, struct Map * map)
31 for (key = 0; key <= world->keyswindata->max; key++)
33 free(world->keybindings[key].name);
35 free(world->keybindings);
36 free(world->keyswindata);
43 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
56 if (c_count > linemax)
58 linemax = c_count + 1;
67 fseek(file, 0, SEEK_SET);
68 * linemax_p = linemax;
71 * n_lines_p = n_lines;
77 extern uint16_t rrand(char use_seed, uint32_t new_seed)
85 /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
86 seed = ((seed * 1103515245) + 12345) % 2147483648;
88 return (seed / 65536); /* TODO: Use bit-shifting for ignoring the less */
89 } /* random least significant 16 bits. */
93 extern void update_log(struct World * world, char * text)
95 static char * last_msg;
98 last_msg = calloc(1, sizeof(char));
101 uint16_t len_old = strlen(world->log);
102 if (0 == strcmp(last_msg, text))
104 uint16_t len_whole = len_old + 1;
105 new_text = calloc(len_whole + 1, sizeof(char));
106 memcpy(new_text, world->log, len_old);
107 memcpy(new_text + len_old, ".", 1);
111 uint16_t len_new = strlen(text);
112 uint16_t len_whole = len_old + len_new + 1;
113 new_text = calloc(len_whole, sizeof(char));
114 memcpy(new_text, world->log, len_old);
115 memcpy(new_text + len_old, text, len_new);
116 last_msg = calloc(len_new + 1, sizeof(char));
117 memcpy(last_msg, text, len_new);
120 world->log = new_text;
125 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
129 if (mapsize > framesize)
131 if (pos > framesize / 2)
133 if (pos < mapsize - (framesize / 2))
135 offset = pos - (framesize / 2);
139 offset = mapsize - framesize;
148 extern void turn_over(struct World * world, char action)
150 if (1 == world->interactive)
152 FILE * file = fopen("record", "a");
157 rrand(1, world->seed * world->turn);
158 struct Monster * monster;
159 for (monster = world->monster;
161 monster = monster->map_obj.next)
163 move_monster(world, monster);
169 extern void save_game(struct World * world)
171 FILE * file = fopen("savefile", "w");
172 write_uint32_bigendian(world->seed, file);
173 write_uint32_bigendian(world->turn, file);
174 write_uint16_bigendian(world->player->pos.y + 1, file);
175 write_uint16_bigendian(world->player->pos.x + 1, file);
176 fputc(world->player->hitpoints, file);
177 write_map_objects (world->monster, file, write_map_objects_monsterdata);
178 write_map_objects (world->item, file, NULL);
184 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
186 if (0 != win->frame.curses_win)
188 suspend_win(win_meta, win);
192 append_win(win_meta, win);
198 extern void scroll_pad(struct WinMeta * win_meta, char dir)
202 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
206 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
212 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
214 if (0 != win_meta->active)
216 struct yx_uint16 size = win_meta->active->frame.size;
221 else if (change == '+')
225 else if (change == '_')
229 else if (change == '*')
233 resize_active_win (win_meta, size);
239 extern struct yx_uint16 find_passable_pos(struct Map * map)
241 struct yx_uint16 pos;
242 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
244 pos.y = rrand(0, 0) % map->size.y;
245 pos.x = rrand(0, 0) % map->size.x;
252 extern unsigned char meta_keys(int key, struct World * world,
253 struct WinMeta * win_meta,
254 struct Win * win_keys,
255 struct Win * win_map,
256 struct Win * win_info,
257 struct Win * win_log)
259 if (key == get_action_key(world->keybindings, "quit"))
263 else if (key == get_action_key(world->keybindings, "scroll pad right"))
265 scroll_pad (win_meta, '+');
267 else if (key == get_action_key(world->keybindings, "scroll pad left"))
269 scroll_pad (win_meta, '-');
271 else if (key == get_action_key(world->keybindings, "toggle keys window"))
273 toggle_window(win_meta, win_keys);
275 else if (key == get_action_key(world->keybindings, "toggle map window"))
277 toggle_window(win_meta, win_map);
279 else if (key == get_action_key(world->keybindings, "toggle info window"))
281 toggle_window(win_meta, win_info);
283 else if (key == get_action_key(world->keybindings, "toggle log window"))
285 toggle_window(win_meta, win_log);
287 else if (key == get_action_key(world->keybindings, "cycle forwards"))
289 cycle_active_win(win_meta, 'n');
291 else if (key == get_action_key(world->keybindings, "cycle backwards"))
293 cycle_active_win(win_meta, 'p');
295 else if (key == get_action_key(world->keybindings, "shift forwards"))
297 shift_active_win(win_meta, 'f');
299 else if (key == get_action_key(world->keybindings, "shift backwards"))
301 shift_active_win(win_meta, 'b');
303 else if (key == get_action_key(world->keybindings, "grow horizontally"))
305 growshrink_active_window(win_meta, '*');
307 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
309 growshrink_active_window(win_meta, '_');
311 else if (key == get_action_key(world->keybindings, "grow vertically"))
313 growshrink_active_window(win_meta, '+');
315 else if (key == get_action_key(world->keybindings, "shrink vertically"))
317 growshrink_active_window(win_meta, '-');
319 else if (key == get_action_key(world->keybindings, "save keys"))
321 save_keybindings(world);
323 else if (key == get_action_key(world->keybindings, "keys nav up"))
325 keyswin_move_selection (world, 'u');
327 else if (key == get_action_key(world->keybindings, "keys nav down"))
329 keyswin_move_selection (world, 'd');
331 else if (key == get_action_key(world->keybindings, "keys mod"))
333 keyswin_mod_key (world, win_meta);
335 else if (key == get_action_key(world->keybindings, "map up"))
337 map_scroll (world->map, NORTH, win_map->frame.size);
339 else if (key == get_action_key(world->keybindings, "map down"))
341 map_scroll (world->map, SOUTH, win_map->frame.size);
343 else if (key == get_action_key(world->keybindings, "map right"))
345 map_scroll (world->map, EAST, win_map->frame.size);
347 else if (key == get_action_key(world->keybindings, "map left"))
349 map_scroll (world->map, WEST, win_map->frame.size);
351 else if (key == get_action_key(world->keybindings, "map center player"))
353 map_center_player (world->map, world->player, win_map->frame.size);