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 #include "map_object_actions.h" /* for is_passable(), move_monster() */
18 #include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */
19 #include "main.h" /* for World struct */
20 #include "yx_uint16.h" /* for yx_uint16 */
21 #include "rrand.h" /* for rrand(), rrand_seed() */
25 extern void exit_game(struct World * world, struct Map * map)
30 for (key = 0; key <= world->keyswindata->max; key++)
32 free(world->keybindings[key].name);
34 free(world->keybindings);
35 free(world->keyswindata);
42 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
55 if (c_count > linemax)
57 linemax = c_count + 1;
66 fseek(file, 0, SEEK_SET);
67 * linemax_p = linemax;
70 * n_lines_p = n_lines;
76 extern void update_log(struct World * world, char * text)
78 static char * last_msg;
81 last_msg = calloc(1, sizeof(char));
84 uint16_t len_old = strlen(world->log);
85 if (0 == strcmp(last_msg, text))
87 uint16_t len_whole = len_old + 1;
88 new_text = calloc(len_whole + 1, sizeof(char));
89 memcpy(new_text, world->log, len_old);
90 memcpy(new_text + len_old, ".", 1);
94 uint16_t len_new = strlen(text);
95 uint16_t len_whole = len_old + len_new + 1;
96 new_text = calloc(len_whole, sizeof(char));
97 memcpy(new_text, world->log, len_old);
98 memcpy(new_text + len_old, text, len_new);
99 last_msg = calloc(len_new + 1, sizeof(char));
100 memcpy(last_msg, text, len_new);
103 world->log = new_text;
108 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
112 if (mapsize > framesize)
114 if (pos > framesize / 2)
116 if (pos < mapsize - (framesize / 2))
118 offset = pos - (framesize / 2);
122 offset = mapsize - framesize;
131 extern void turn_over(struct World * world, char action)
133 if (1 == world->interactive)
135 FILE * file = fopen("record", "a");
140 rrand_seed(world->seed * world->turn);
141 struct Monster * monster;
142 for (monster = world->monster;
144 monster = monster->map_obj.next)
146 move_monster(world, monster);
152 extern void save_game(struct World * world)
154 FILE * file = fopen("savefile", "w");
155 write_uint32_bigendian(world->seed, file);
156 write_uint32_bigendian(world->turn, file);
157 write_uint16_bigendian(world->player->pos.y + 1, file);
158 write_uint16_bigendian(world->player->pos.x + 1, file);
159 fputc(world->player->hitpoints, file);
160 write_map_objects(world, world->monster, file);
161 write_map_objects(world, world->item, file);
167 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
169 if (0 != win->frame.curses_win)
171 suspend_win(win_meta, win);
175 append_win(win_meta, win);
181 extern void scroll_pad(struct WinMeta * win_meta, char dir)
185 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
189 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
195 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
197 if (0 != win_meta->active)
199 struct yx_uint16 size = win_meta->active->frame.size;
204 else if (change == '+')
208 else if (change == '_')
212 else if (change == '*')
216 resize_active_win (win_meta, size);
222 extern struct yx_uint16 find_passable_pos(struct Map * map)
224 struct yx_uint16 pos;
225 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
227 pos.y = rrand() % map->size.y;
228 pos.x = rrand() % map->size.x;
235 extern unsigned char meta_keys(int key, struct World * world,
236 struct WinMeta * win_meta,
237 struct Win * win_keys,
238 struct Win * win_map,
239 struct Win * win_info,
240 struct Win * win_log)
242 if (key == get_action_key(world->keybindings, "quit"))
246 else if (key == get_action_key(world->keybindings, "scroll pad right"))
248 scroll_pad (win_meta, '+');
250 else if (key == get_action_key(world->keybindings, "scroll pad left"))
252 scroll_pad (win_meta, '-');
254 else if (key == get_action_key(world->keybindings, "toggle keys window"))
256 toggle_window(win_meta, win_keys);
258 else if (key == get_action_key(world->keybindings, "toggle map window"))
260 toggle_window(win_meta, win_map);
262 else if (key == get_action_key(world->keybindings, "toggle info window"))
264 toggle_window(win_meta, win_info);
266 else if (key == get_action_key(world->keybindings, "toggle log window"))
268 toggle_window(win_meta, win_log);
270 else if (key == get_action_key(world->keybindings, "cycle forwards"))
272 cycle_active_win(win_meta, 'n');
274 else if (key == get_action_key(world->keybindings, "cycle backwards"))
276 cycle_active_win(win_meta, 'p');
278 else if (key == get_action_key(world->keybindings, "shift forwards"))
280 shift_active_win(win_meta, 'f');
282 else if (key == get_action_key(world->keybindings, "shift backwards"))
284 shift_active_win(win_meta, 'b');
286 else if (key == get_action_key(world->keybindings, "grow horizontally"))
288 growshrink_active_window(win_meta, '*');
290 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
292 growshrink_active_window(win_meta, '_');
294 else if (key == get_action_key(world->keybindings, "grow vertically"))
296 growshrink_active_window(win_meta, '+');
298 else if (key == get_action_key(world->keybindings, "shrink vertically"))
300 growshrink_active_window(win_meta, '-');
302 else if (key == get_action_key(world->keybindings, "save keys"))
304 save_keybindings(world);
306 else if (key == get_action_key(world->keybindings, "keys nav up"))
308 keyswin_move_selection (world, 'u');
310 else if (key == get_action_key(world->keybindings, "keys nav down"))
312 keyswin_move_selection (world, 'd');
314 else if (key == get_action_key(world->keybindings, "keys mod"))
316 keyswin_mod_key (world, win_meta);
318 else if (key == get_action_key(world->keybindings, "map up"))
320 map_scroll (world->map, NORTH, win_map->frame.size);
322 else if (key == get_action_key(world->keybindings, "map down"))
324 map_scroll (world->map, SOUTH, win_map->frame.size);
326 else if (key == get_action_key(world->keybindings, "map right"))
328 map_scroll (world->map, EAST, win_map->frame.size);
330 else if (key == get_action_key(world->keybindings, "map left"))
332 map_scroll (world->map, WEST, win_map->frame.size);
334 else if (key == get_action_key(world->keybindings, "map center player"))
336 map_center_player (world->map, world->player, win_map->frame.size);