4 #include <stdlib.h> /* for calloc(), free() */
5 #include <string.h> /* for strlen(), strcmp(), memcpy() */
6 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
7 * resize_active_win(), cycle_active_win(),
8 * shift_active_win(), struct Win, struct WinMeta
10 #include "keybindings.h" /* for get_action_key(), save_keybindings(),
11 * keyswin_move_selection(), keyswin_mod_key()
13 #include "readwrite.h" /* for write_uint16_bigendian(), write_uint32_bigendian()
15 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
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 */
20 #include "rrand.h" /* for rrand(), rrand_seed() */
24 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
37 if (c_count > linemax)
39 linemax = c_count + 1;
48 fseek(file, 0, SEEK_SET);
49 * linemax_p = linemax;
52 * n_lines_p = n_lines;
58 extern void update_log(struct World * world, char * text)
60 static char * last_msg;
63 last_msg = calloc(1, sizeof(char));
66 uint16_t len_old = strlen(world->log);
67 if (0 == strcmp(last_msg, text))
69 uint16_t len_whole = len_old + 1;
70 new_text = calloc(len_whole + 1, sizeof(char));
71 memcpy(new_text, world->log, len_old);
72 memcpy(new_text + len_old, ".", 1);
76 uint16_t len_new = strlen(text);
77 uint16_t len_whole = len_old + len_new + 1;
78 new_text = calloc(len_whole, sizeof(char));
79 memcpy(new_text, world->log, len_old);
80 memcpy(new_text + len_old, text, len_new);
81 last_msg = calloc(len_new + 1, sizeof(char));
82 memcpy(last_msg, text, len_new);
85 world->log = new_text;
90 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
94 if (mapsize > framesize)
96 if (pos > framesize / 2)
98 if (pos < mapsize - (framesize / 2))
100 offset = pos - (framesize / 2);
104 offset = mapsize - framesize;
113 extern void turn_over(struct World * world, char action)
115 if (1 == world->interactive)
117 FILE * file = fopen("record", "a");
122 rrand_seed(world->seed * world->turn);
123 struct Monster * monster;
124 for (monster = world->monster;
126 monster = monster->map_obj.next)
128 move_monster(world, monster);
134 extern void save_game(struct World * world)
136 FILE * file = fopen("savefile", "w");
137 write_uint32_bigendian(world->seed, file);
138 write_uint32_bigendian(world->turn, file);
139 write_uint16_bigendian(world->player->pos.y + 1, file);
140 write_uint16_bigendian(world->player->pos.x + 1, file);
141 fputc(world->player->hitpoints, file);
142 write_map_objects(world, world->monster, file);
143 write_map_objects(world, world->item, file);
149 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
151 if (0 != win->frame.curses_win)
153 suspend_win(win_meta, win);
157 append_win(win_meta, win);
163 extern void scroll_pad(struct WinMeta * win_meta, char dir)
167 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
171 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
177 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
179 if (0 != win_meta->active)
181 struct yx_uint16 size = win_meta->active->frame.size;
186 else if (change == '+')
190 else if (change == '_')
194 else if (change == '*')
198 resize_active_win (win_meta, size);
204 extern struct yx_uint16 find_passable_pos(struct Map * map)
206 struct yx_uint16 pos;
207 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
209 pos.y = rrand() % map->size.y;
210 pos.x = rrand() % map->size.x;
217 extern unsigned char meta_keys(int key, struct World * world,
218 struct WinMeta * win_meta,
219 struct Win * win_keys,
220 struct Win * win_map,
221 struct Win * win_info,
222 struct Win * win_log)
224 if (key == get_action_key(world->keybindings, "quit"))
228 else if (key == get_action_key(world->keybindings, "scroll pad right"))
230 scroll_pad (win_meta, '+');
232 else if (key == get_action_key(world->keybindings, "scroll pad left"))
234 scroll_pad (win_meta, '-');
236 else if (key == get_action_key(world->keybindings, "toggle keys window"))
238 toggle_window(win_meta, win_keys);
240 else if (key == get_action_key(world->keybindings, "toggle map window"))
242 toggle_window(win_meta, win_map);
244 else if (key == get_action_key(world->keybindings, "toggle info window"))
246 toggle_window(win_meta, win_info);
248 else if (key == get_action_key(world->keybindings, "toggle log window"))
250 toggle_window(win_meta, win_log);
252 else if (key == get_action_key(world->keybindings, "cycle forwards"))
254 cycle_active_win(win_meta, 'n');
256 else if (key == get_action_key(world->keybindings, "cycle backwards"))
258 cycle_active_win(win_meta, 'p');
260 else if (key == get_action_key(world->keybindings, "shift forwards"))
262 shift_active_win(win_meta, 'f');
264 else if (key == get_action_key(world->keybindings, "shift backwards"))
266 shift_active_win(win_meta, 'b');
268 else if (key == get_action_key(world->keybindings, "grow horizontally"))
270 growshrink_active_window(win_meta, '*');
272 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
274 growshrink_active_window(win_meta, '_');
276 else if (key == get_action_key(world->keybindings, "grow vertically"))
278 growshrink_active_window(win_meta, '+');
280 else if (key == get_action_key(world->keybindings, "shrink vertically"))
282 growshrink_active_window(win_meta, '-');
284 else if (key == get_action_key(world->keybindings, "save keys"))
286 save_keybindings(world);
288 else if (key == get_action_key(world->keybindings, "keys nav up"))
290 keyswin_move_selection (world, 'u');
292 else if (key == get_action_key(world->keybindings, "keys nav down"))
294 keyswin_move_selection (world, 'd');
296 else if (key == get_action_key(world->keybindings, "keys mod"))
298 keyswin_mod_key (world, win_meta);
300 else if (key == get_action_key(world->keybindings, "map up"))
302 map_scroll (world->map, NORTH, win_map->frame.size);
304 else if (key == get_action_key(world->keybindings, "map down"))
306 map_scroll (world->map, SOUTH, win_map->frame.size);
308 else if (key == get_action_key(world->keybindings, "map right"))
310 map_scroll (world->map, EAST, win_map->frame.size);
312 else if (key == get_action_key(world->keybindings, "map left"))
314 map_scroll (world->map, WEST, win_map->frame.size);
316 else if (key == get_action_key(world->keybindings, "map center player"))
318 map_center_player (world->map, world->player, win_map->frame.size);