5 #include "keybindings.h"
7 #include "map_objects.h"
8 #include "map_object_actions.h"
11 #include "yx_uint16.h"
13 extern void exit_game(struct World * world, struct Map * map) {
18 for (key = 0; key <= world->keyswindata->max; key++)
19 free(world->keybindings[key].name);
20 free(world->keybindings);
21 free(world->keyswindata);
23 exit (EXIT_SUCCESS); }
25 extern void textfile_sizes (FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p) {
26 // Learn largest line length (linemax_p) and (n_lines_p if not set to NULL) number of lines.
35 if (c_count > linemax)
36 linemax = c_count + 1;
40 fseek(file, 0, SEEK_SET);
41 * linemax_p = linemax;
43 * n_lines_p = n_lines; }
45 extern uint16_t rrand(char use_seed, uint32_t new_seed) {
46 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
50 seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)).
51 return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
53 extern void update_log (struct World * world, char * text) {
54 // Update log by appending text, or by appending a "." if text is the same as the last one.
55 static char * last_msg;
57 last_msg = calloc(1, sizeof(char));
59 uint16_t len_old = strlen(world->log);
60 if (0 == strcmp(last_msg, text)) {
61 uint16_t len_whole = len_old + 1;
62 new_text = calloc(len_whole + 1, sizeof(char));
63 memcpy(new_text, world->log, len_old);
64 memcpy(new_text + len_old, ".", 1); }
66 uint16_t len_new = strlen(text);
67 uint16_t len_whole = len_old + len_new + 1;
68 new_text = calloc(len_whole, sizeof(char));
69 memcpy(new_text, world->log, len_old);
70 memcpy(new_text + len_old, text, len_new);
71 last_msg = calloc(len_new + 1, sizeof(char));
72 memcpy(last_msg, text, len_new); }
74 world->log = new_text; }
76 extern uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t framesize) {
77 // Return the offset for display of a map inside a frame centered on pos.
79 if (mapsize > framesize) {
80 if (pos > framesize / 2) {
81 if (pos < mapsize - (framesize / 2))
82 offset = pos - (framesize / 2);
84 offset = mapsize - framesize; } }
87 extern void turn_over (struct World * world, char action) {
88 // Record action in game record file, increment turn and move enemy.
89 if (1 == world->interactive) {
90 FILE * file = fopen("record", "a");
94 rrand(1, world->seed * world->turn);
95 struct Monster * monster;
96 for (monster = world->monster; monster != 0; monster = monster->map_obj.next)
97 move_monster(world, monster); }
99 extern void save_game(struct World * world) {
100 // Save game data to game file.
101 FILE * file = fopen("savefile", "w");
102 write_uint32_bigendian(world->seed, file);
103 write_uint32_bigendian(world->turn, file);
104 write_uint16_bigendian(world->player->pos.y + 1, file);
105 write_uint16_bigendian(world->player->pos.x + 1, file);
106 fputc(world->player->hitpoints, file);
107 write_map_objects (world->monster, file, write_map_objects_monsterdata);
108 write_map_objects (world->item, file, NULL);
111 extern void toggle_window (struct WinMeta * win_meta, struct Win * win) {
112 // Toggle display of window win.
113 if (0 != win->frame.curses_win)
114 suspend_win(win_meta, win);
116 append_win(win_meta, win); }
118 extern void scroll_pad (struct WinMeta * win_meta, char dir) {
119 // Try to scroll pad left or right.
121 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
123 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
125 extern void growshrink_active_window (struct WinMeta * win_meta, char change) {
126 // Grow or shrink active window horizontally or vertically by one cell size.
127 if (0 != win_meta->active) {
128 struct yx_uint16 size = win_meta->active->frame.size;
129 if (change == '-') size.y--;
130 else if (change == '+') size.y++;
131 else if (change == '_') size.x--;
132 else if (change == '*') size.x++;
133 resize_active_win (win_meta, size); } }
135 extern struct yx_uint16 find_passable_pos (struct Map * map) {
136 // Return a random passable position on map.
137 struct yx_uint16 pos;
138 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) {
139 pos.y = rrand(0, 0) % map->size.y;
140 pos.x = rrand(0, 0) % map->size.x; }
143 extern unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta,
144 struct Win * win_keys, struct Win * win_map, struct Win * win_info,
145 struct Win * win_log) {
146 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
147 if (key == get_action_key(world->keybindings, "quit"))
149 else if (key == get_action_key(world->keybindings, "scroll pad right"))
150 scroll_pad (win_meta, '+');
151 else if (key == get_action_key(world->keybindings, "scroll pad left"))
152 scroll_pad (win_meta, '-');
153 else if (key == get_action_key(world->keybindings, "toggle keys window"))
154 toggle_window(win_meta, win_keys);
155 else if (key == get_action_key(world->keybindings, "toggle map window"))
156 toggle_window(win_meta, win_map);
157 else if (key == get_action_key(world->keybindings, "toggle info window"))
158 toggle_window(win_meta, win_info);
159 else if (key == get_action_key(world->keybindings, "toggle log window"))
160 toggle_window(win_meta, win_log);
161 else if (key == get_action_key(world->keybindings, "cycle forwards"))
162 cycle_active_win(win_meta, 'n');
163 else if (key == get_action_key(world->keybindings, "cycle backwards"))
164 cycle_active_win(win_meta, 'p');
165 else if (key == get_action_key(world->keybindings, "shift forwards"))
166 shift_active_win(win_meta, 'f');
167 else if (key == get_action_key(world->keybindings, "shift backwards"))
168 shift_active_win(win_meta, 'b');
169 else if (key == get_action_key(world->keybindings, "grow horizontally"))
170 growshrink_active_window(win_meta, '*');
171 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
172 growshrink_active_window(win_meta, '_');
173 else if (key == get_action_key(world->keybindings, "grow vertically"))
174 growshrink_active_window(win_meta, '+');
175 else if (key == get_action_key(world->keybindings, "shrink vertically"))
176 growshrink_active_window(win_meta, '-');
177 else if (key == get_action_key(world->keybindings, "save keys"))
178 save_keybindings(world);
179 else if (key == get_action_key(world->keybindings, "keys nav up"))
180 keyswin_move_selection (world, 'u');
181 else if (key == get_action_key(world->keybindings, "keys nav down"))
182 keyswin_move_selection (world, 'd');
183 else if (key == get_action_key(world->keybindings, "keys mod"))
184 keyswin_mod_key (world, win_meta);
185 else if (key == get_action_key(world->keybindings, "map up"))
186 map_scroll (world->map, NORTH, win_map->frame.size);
187 else if (key == get_action_key(world->keybindings, "map down"))
188 map_scroll (world->map, SOUTH, win_map->frame.size);
189 else if (key == get_action_key(world->keybindings, "map right"))
190 map_scroll (world->map, EAST, win_map->frame.size);
191 else if (key == get_action_key(world->keybindings, "map left"))
192 map_scroll (world->map, WEST, win_map->frame.size);
193 else if (key == get_action_key(world->keybindings, "map center player"))
194 map_center_player (world->map, world->player, win_map->frame.size);