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 */
10 #include "keybindings.h" /* for get_action_key(), save_keybindings(), */
11 /* keyswin_move_selection(), keyswin_mod_key() */
12 #include "readwrite.h" /* for write_uint16_bigendian() */
13 /* write_uint32_bigendian() */
14 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
15 /* write_map_objects_monsterdata() */
16 #include "map_object_actions.h" /* for is_passable(), move_monster() */
17 #include "map.h" /* for map_scroll()/map_center_player(), Map struct, dir enum */
18 #include "main.h" /* for World struct */
19 #include "yx_uint16.h" /* for yx_uint16 */
23 extern void exit_game(struct World * world, struct Map * map)
28 for (key = 0; key <= world->keyswindata->max; key++)
30 free(world->keybindings[key].name);
32 free(world->keybindings);
33 free(world->keyswindata);
40 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
53 if (c_count > linemax)
55 linemax = c_count + 1;
64 fseek(file, 0, SEEK_SET);
65 * linemax_p = linemax;
68 * n_lines_p = n_lines;
74 extern uint16_t rrand(char use_seed, uint32_t new_seed)
82 /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
83 seed = ((seed * 1103515245) + 12345) % 2147483648;
85 return (seed / 65536); /* TODO: Use bit-shifting for ignoring the less */
86 } /* random least significant 16 bits. */
90 extern void update_log(struct World * world, char * text)
92 static char * last_msg;
95 last_msg = calloc(1, sizeof(char));
98 uint16_t len_old = strlen(world->log);
99 if (0 == strcmp(last_msg, text))
101 uint16_t len_whole = len_old + 1;
102 new_text = calloc(len_whole + 1, sizeof(char));
103 memcpy(new_text, world->log, len_old);
104 memcpy(new_text + len_old, ".", 1);
108 uint16_t len_new = strlen(text);
109 uint16_t len_whole = len_old + len_new + 1;
110 new_text = calloc(len_whole, sizeof(char));
111 memcpy(new_text, world->log, len_old);
112 memcpy(new_text + len_old, text, len_new);
113 last_msg = calloc(len_new + 1, sizeof(char));
114 memcpy(last_msg, text, len_new);
117 world->log = new_text;
122 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
126 if (mapsize > framesize)
128 if (pos > framesize / 2)
130 if (pos < mapsize - (framesize / 2))
132 offset = pos - (framesize / 2);
136 offset = mapsize - framesize;
145 extern void turn_over(struct World * world, char action)
147 if (1 == world->interactive)
149 FILE * file = fopen("record", "a");
154 rrand(1, world->seed * world->turn);
155 struct Monster * monster;
156 for (monster = world->monster;
158 monster = monster->map_obj.next)
160 move_monster(world, monster);
166 extern void save_game(struct World * world)
168 FILE * file = fopen("savefile", "w");
169 write_uint32_bigendian(world->seed, file);
170 write_uint32_bigendian(world->turn, file);
171 write_uint16_bigendian(world->player->pos.y + 1, file);
172 write_uint16_bigendian(world->player->pos.x + 1, file);
173 fputc(world->player->hitpoints, file);
174 write_map_objects (world->monster, file, write_map_objects_monsterdata);
175 write_map_objects (world->item, file, NULL);
181 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
183 if (0 != win->frame.curses_win)
185 suspend_win(win_meta, win);
189 append_win(win_meta, win);
195 extern void scroll_pad(struct WinMeta * win_meta, char dir)
199 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
203 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
209 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
211 if (0 != win_meta->active)
213 struct yx_uint16 size = win_meta->active->frame.size;
218 else if (change == '+')
222 else if (change == '_')
226 else if (change == '*')
230 resize_active_win (win_meta, size);
236 extern struct yx_uint16 find_passable_pos(struct Map * map)
238 struct yx_uint16 pos;
239 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
241 pos.y = rrand(0, 0) % map->size.y;
242 pos.x = rrand(0, 0) % map->size.x;
249 extern unsigned char meta_keys(int key, struct World * world,
250 struct WinMeta * win_meta,
251 struct Win * win_keys,
252 struct Win * win_map,
253 struct Win * win_info,
254 struct Win * win_log)
256 if (key == get_action_key(world->keybindings, "quit"))
260 else if (key == get_action_key(world->keybindings, "scroll pad right"))
262 scroll_pad (win_meta, '+');
264 else if (key == get_action_key(world->keybindings, "scroll pad left"))
266 scroll_pad (win_meta, '-');
268 else if (key == get_action_key(world->keybindings, "toggle keys window"))
270 toggle_window(win_meta, win_keys);
272 else if (key == get_action_key(world->keybindings, "toggle map window"))
274 toggle_window(win_meta, win_map);
276 else if (key == get_action_key(world->keybindings, "toggle info window"))
278 toggle_window(win_meta, win_info);
280 else if (key == get_action_key(world->keybindings, "toggle log window"))
282 toggle_window(win_meta, win_log);
284 else if (key == get_action_key(world->keybindings, "cycle forwards"))
286 cycle_active_win(win_meta, 'n');
288 else if (key == get_action_key(world->keybindings, "cycle backwards"))
290 cycle_active_win(win_meta, 'p');
292 else if (key == get_action_key(world->keybindings, "shift forwards"))
294 shift_active_win(win_meta, 'f');
296 else if (key == get_action_key(world->keybindings, "shift backwards"))
298 shift_active_win(win_meta, 'b');
300 else if (key == get_action_key(world->keybindings, "grow horizontally"))
302 growshrink_active_window(win_meta, '*');
304 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
306 growshrink_active_window(win_meta, '_');
308 else if (key == get_action_key(world->keybindings, "grow vertically"))
310 growshrink_active_window(win_meta, '+');
312 else if (key == get_action_key(world->keybindings, "shrink vertically"))
314 growshrink_active_window(win_meta, '-');
316 else if (key == get_action_key(world->keybindings, "save keys"))
318 save_keybindings(world);
320 else if (key == get_action_key(world->keybindings, "keys nav up"))
322 keyswin_move_selection (world, 'u');
324 else if (key == get_action_key(world->keybindings, "keys nav down"))
326 keyswin_move_selection (world, 'd');
328 else if (key == get_action_key(world->keybindings, "keys mod"))
330 keyswin_mod_key (world, win_meta);
332 else if (key == get_action_key(world->keybindings, "map up"))
334 map_scroll (world->map, NORTH, win_map->frame.size);
336 else if (key == get_action_key(world->keybindings, "map down"))
338 map_scroll (world->map, SOUTH, win_map->frame.size);
340 else if (key == get_action_key(world->keybindings, "map right"))
342 map_scroll (world->map, EAST, win_map->frame.size);
344 else if (key == get_action_key(world->keybindings, "map left"))
346 map_scroll (world->map, WEST, win_map->frame.size);
348 else if (key == get_action_key(world->keybindings, "map center player"))
350 map_center_player (world->map, world->player, win_map->frame.size);