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 >> 16); /* Ignore less random least significant 16 bits. */
92 extern void update_log(struct World * world, char * text)
94 static char * last_msg;
97 last_msg = calloc(1, sizeof(char));
100 uint16_t len_old = strlen(world->log);
101 if (0 == strcmp(last_msg, text))
103 uint16_t len_whole = len_old + 1;
104 new_text = calloc(len_whole + 1, sizeof(char));
105 memcpy(new_text, world->log, len_old);
106 memcpy(new_text + len_old, ".", 1);
110 uint16_t len_new = strlen(text);
111 uint16_t len_whole = len_old + len_new + 1;
112 new_text = calloc(len_whole, sizeof(char));
113 memcpy(new_text, world->log, len_old);
114 memcpy(new_text + len_old, text, len_new);
115 last_msg = calloc(len_new + 1, sizeof(char));
116 memcpy(last_msg, text, len_new);
119 world->log = new_text;
124 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
128 if (mapsize > framesize)
130 if (pos > framesize / 2)
132 if (pos < mapsize - (framesize / 2))
134 offset = pos - (framesize / 2);
138 offset = mapsize - framesize;
147 extern void turn_over(struct World * world, char action)
149 if (1 == world->interactive)
151 FILE * file = fopen("record", "a");
156 rrand(1, world->seed * world->turn);
157 struct Monster * monster;
158 for (monster = world->monster;
160 monster = monster->map_obj.next)
162 move_monster(world, monster);
168 extern void save_game(struct World * world)
170 FILE * file = fopen("savefile", "w");
171 write_uint32_bigendian(world->seed, file);
172 write_uint32_bigendian(world->turn, file);
173 write_uint16_bigendian(world->player->pos.y + 1, file);
174 write_uint16_bigendian(world->player->pos.x + 1, file);
175 fputc(world->player->hitpoints, file);
176 write_map_objects (world->monster, file, write_map_objects_monsterdata);
177 write_map_objects (world->item, file, NULL);
183 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
185 if (0 != win->frame.curses_win)
187 suspend_win(win_meta, win);
191 append_win(win_meta, win);
197 extern void scroll_pad(struct WinMeta * win_meta, char dir)
201 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
205 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
211 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
213 if (0 != win_meta->active)
215 struct yx_uint16 size = win_meta->active->frame.size;
220 else if (change == '+')
224 else if (change == '_')
228 else if (change == '*')
232 resize_active_win (win_meta, size);
238 extern struct yx_uint16 find_passable_pos(struct Map * map)
240 struct yx_uint16 pos;
241 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
243 pos.y = rrand(0, 0) % map->size.y;
244 pos.x = rrand(0, 0) % map->size.x;
251 extern unsigned char meta_keys(int key, struct World * world,
252 struct WinMeta * win_meta,
253 struct Win * win_keys,
254 struct Win * win_map,
255 struct Win * win_info,
256 struct Win * win_log)
258 if (key == get_action_key(world->keybindings, "quit"))
262 else if (key == get_action_key(world->keybindings, "scroll pad right"))
264 scroll_pad (win_meta, '+');
266 else if (key == get_action_key(world->keybindings, "scroll pad left"))
268 scroll_pad (win_meta, '-');
270 else if (key == get_action_key(world->keybindings, "toggle keys window"))
272 toggle_window(win_meta, win_keys);
274 else if (key == get_action_key(world->keybindings, "toggle map window"))
276 toggle_window(win_meta, win_map);
278 else if (key == get_action_key(world->keybindings, "toggle info window"))
280 toggle_window(win_meta, win_info);
282 else if (key == get_action_key(world->keybindings, "toggle log window"))
284 toggle_window(win_meta, win_log);
286 else if (key == get_action_key(world->keybindings, "cycle forwards"))
288 cycle_active_win(win_meta, 'n');
290 else if (key == get_action_key(world->keybindings, "cycle backwards"))
292 cycle_active_win(win_meta, 'p');
294 else if (key == get_action_key(world->keybindings, "shift forwards"))
296 shift_active_win(win_meta, 'f');
298 else if (key == get_action_key(world->keybindings, "shift backwards"))
300 shift_active_win(win_meta, 'b');
302 else if (key == get_action_key(world->keybindings, "grow horizontally"))
304 growshrink_active_window(win_meta, '*');
306 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
308 growshrink_active_window(win_meta, '_');
310 else if (key == get_action_key(world->keybindings, "grow vertically"))
312 growshrink_active_window(win_meta, '+');
314 else if (key == get_action_key(world->keybindings, "shrink vertically"))
316 growshrink_active_window(win_meta, '-');
318 else if (key == get_action_key(world->keybindings, "save keys"))
320 save_keybindings(world);
322 else if (key == get_action_key(world->keybindings, "keys nav up"))
324 keyswin_move_selection (world, 'u');
326 else if (key == get_action_key(world->keybindings, "keys nav down"))
328 keyswin_move_selection (world, 'd');
330 else if (key == get_action_key(world->keybindings, "keys mod"))
332 keyswin_mod_key (world, win_meta);
334 else if (key == get_action_key(world->keybindings, "map up"))
336 map_scroll (world->map, NORTH, win_map->frame.size);
338 else if (key == get_action_key(world->keybindings, "map down"))
340 map_scroll (world->map, SOUTH, win_map->frame.size);
342 else if (key == get_action_key(world->keybindings, "map right"))
344 map_scroll (world->map, EAST, win_map->frame.size);
346 else if (key == get_action_key(world->keybindings, "map left"))
348 map_scroll (world->map, WEST, win_map->frame.size);
350 else if (key == get_action_key(world->keybindings, "map center player"))
352 map_center_player (world->map, world->player, win_map->frame.size);