home · contact · privacy
Moved pseudo-random generator into its own library, simplified its interface and...
[plomrogue] / src / misc.c
1 /* misc.c */
2
3 #include "misc.h"
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                       */
11 #include "keybindings.h" /* for get_action_key(), save_keybindings(),
12                           * keyswin_move_selection(), keyswin_mod_key()
13                           */
14 #include "readwrite.h" /* for write_uint16_bigendian(), write_uint32_bigendian()
15                         */
16 #include "map_objects.h" /* for struct Monster, write_map_objects(),
17                           * write_map_objects_monsterdata()
18                           */
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 */
23 #include "rrand.h" /* for rrand(), rrand_seed() */
24
25
26
27 extern void exit_game(struct World * world, struct Map * map)
28 {
29     endwin();
30     free(map->cells);
31     uint16_t key;
32     for (key = 0; key <= world->keyswindata->max; key++)
33     {
34         free(world->keybindings[key].name);
35     }
36     free(world->keybindings);
37     free(world->keyswindata);
38     free(world->log);
39     exit (EXIT_SUCCESS);
40 }
41
42
43
44 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
45                            uint16_t * n_lines_p)
46 {
47     uint16_t n_lines = 0;
48     int c = 0;
49     uint16_t linemax = 0;
50     uint16_t c_count = 0;
51     while (EOF != c)
52     {
53         c_count++;
54         c = getc(file);
55         if ('\n' == c)
56         {
57             if (c_count > linemax)
58             {
59                 linemax = c_count + 1;
60             }
61             c_count = 0;
62             if (n_lines_p)
63             {
64                 n_lines++;
65             }
66         }
67     }
68     fseek(file, 0, SEEK_SET);
69     * linemax_p = linemax;
70     if (n_lines_p)
71     {
72         * n_lines_p = n_lines;
73     }
74 }
75
76
77
78 extern void update_log(struct World * world, char * text)
79 {
80     static char * last_msg;
81     if (0 == last_msg)
82     {
83         last_msg = calloc(1, sizeof(char));
84     }
85     char * new_text;
86     uint16_t len_old = strlen(world->log);
87     if (0 == strcmp(last_msg, text))
88     {
89         uint16_t len_whole = len_old + 1;
90         new_text = calloc(len_whole + 1, sizeof(char));
91         memcpy(new_text, world->log, len_old);
92         memcpy(new_text + len_old, ".", 1);
93     }
94     else
95     {
96         uint16_t len_new = strlen(text);
97         uint16_t len_whole = len_old + len_new + 1;
98         new_text = calloc(len_whole, sizeof(char));
99         memcpy(new_text, world->log, len_old);
100         memcpy(new_text + len_old, text, len_new);
101         last_msg = calloc(len_new + 1, sizeof(char));
102         memcpy(last_msg, text, len_new);
103     }
104     free(world->log);
105     world->log = new_text;
106 }
107
108
109
110 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
111                               uint16_t framesize)
112 {
113     uint16_t offset = 0;
114     if (mapsize > framesize)
115     {
116         if (pos > framesize / 2)
117         {
118             if (pos < mapsize - (framesize / 2))
119             {
120                 offset = pos - (framesize / 2);
121             }
122             else
123             {
124                 offset = mapsize - framesize;
125             }
126         }
127     }
128     return offset;
129 }
130
131
132
133 extern void turn_over(struct World * world, char action)
134 {
135     if (1 == world->interactive)
136     {
137         FILE * file = fopen("record", "a");
138         fputc(action, file);
139         fclose(file);
140     }
141     world->turn++;
142     rrand_seed(world->seed * world->turn);
143     struct Monster * monster;
144     for (monster = world->monster;
145          monster != 0;
146          monster = monster->map_obj.next)
147     {
148         move_monster(world, monster);
149     }
150 }
151
152
153
154 extern void save_game(struct World * world)
155 {
156     FILE * file = fopen("savefile", "w");
157     write_uint32_bigendian(world->seed, file);
158     write_uint32_bigendian(world->turn, file);
159     write_uint16_bigendian(world->player->pos.y + 1, file);
160     write_uint16_bigendian(world->player->pos.x + 1, file);
161     fputc(world->player->hitpoints, file);
162     write_map_objects (world->monster, file, write_map_objects_monsterdata);
163     write_map_objects (world->item, file, NULL);
164     fclose(file);
165 }
166
167
168
169 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
170 {
171     if (0 != win->frame.curses_win)
172     {
173         suspend_win(win_meta, win);
174     }
175     else
176     {
177         append_win(win_meta, win);
178     }
179 }
180
181
182
183 extern void scroll_pad(struct WinMeta * win_meta, char dir)
184 {
185     if      ('+' == dir)
186     {
187         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
188     }
189     else if ('-' == dir)
190     {
191         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
192     }
193 }
194
195
196
197 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
198 {
199     if (0 != win_meta->active)
200     {
201         struct yx_uint16 size = win_meta->active->frame.size;
202         if      (change == '-')
203         {
204             size.y--;
205         }
206         else if (change == '+')
207         {
208             size.y++;
209         }
210         else if (change == '_')
211         {
212             size.x--;
213         }
214         else if (change == '*')
215         {
216             size.x++;
217         }
218         resize_active_win (win_meta, size);
219     }
220 }
221
222
223
224 extern struct yx_uint16 find_passable_pos(struct Map * map)
225 {
226     struct yx_uint16 pos;
227     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
228     {
229         pos.y = rrand() % map->size.y;
230         pos.x = rrand() % map->size.x;
231     }
232     return pos;
233 }
234
235
236
237 extern unsigned char meta_keys(int key, struct World * world,
238                                struct WinMeta * win_meta,
239                                struct Win * win_keys,
240                                struct Win * win_map,
241                                struct Win * win_info,
242                                struct Win * win_log)
243 {
244     if (key == get_action_key(world->keybindings, "quit"))
245     {
246         return 1;
247     }
248     else if (key == get_action_key(world->keybindings, "scroll pad right"))
249     {
250         scroll_pad (win_meta, '+');
251     }
252     else if (key == get_action_key(world->keybindings, "scroll pad left"))
253     {
254         scroll_pad (win_meta, '-');
255     }
256     else if (key == get_action_key(world->keybindings, "toggle keys window"))
257     {
258         toggle_window(win_meta, win_keys);
259     }
260     else if (key == get_action_key(world->keybindings, "toggle map window"))
261     {
262         toggle_window(win_meta, win_map);
263     }
264     else if (key == get_action_key(world->keybindings, "toggle info window"))
265     {
266         toggle_window(win_meta, win_info);
267     }
268     else if (key == get_action_key(world->keybindings, "toggle log window"))
269     {
270         toggle_window(win_meta, win_log);
271     }
272     else if (key == get_action_key(world->keybindings, "cycle forwards"))
273     {
274         cycle_active_win(win_meta, 'n');
275     }
276     else if (key == get_action_key(world->keybindings, "cycle backwards"))
277     {
278         cycle_active_win(win_meta, 'p');
279     }
280     else if (key == get_action_key(world->keybindings, "shift forwards"))
281     {
282         shift_active_win(win_meta, 'f');
283     }
284     else if (key == get_action_key(world->keybindings, "shift backwards"))
285     {
286         shift_active_win(win_meta, 'b');
287     }
288     else if (key == get_action_key(world->keybindings, "grow horizontally"))
289     {
290         growshrink_active_window(win_meta, '*');
291     }
292     else if (key == get_action_key(world->keybindings, "shrink horizontally"))
293     {
294         growshrink_active_window(win_meta, '_');
295     }
296     else if (key == get_action_key(world->keybindings, "grow vertically"))
297     {
298         growshrink_active_window(win_meta, '+');
299     }
300     else if (key == get_action_key(world->keybindings, "shrink vertically"))
301     {
302         growshrink_active_window(win_meta, '-');
303     }
304     else if (key == get_action_key(world->keybindings, "save keys"))
305     {
306         save_keybindings(world);
307     }
308     else if (key == get_action_key(world->keybindings, "keys nav up"))
309     {
310         keyswin_move_selection (world, 'u');
311     }
312     else if (key == get_action_key(world->keybindings, "keys nav down"))
313     {
314         keyswin_move_selection (world, 'd');
315     }
316     else if (key == get_action_key(world->keybindings, "keys mod"))
317     {
318         keyswin_mod_key (world, win_meta);
319     }
320     else if (key == get_action_key(world->keybindings, "map up"))
321     {
322         map_scroll (world->map, NORTH, win_map->frame.size);
323      }
324     else if (key == get_action_key(world->keybindings, "map down"))
325     {
326         map_scroll (world->map, SOUTH, win_map->frame.size);
327     }
328     else if (key == get_action_key(world->keybindings, "map right"))
329     {
330         map_scroll (world->map, EAST, win_map->frame.size);
331     }
332     else if (key == get_action_key(world->keybindings, "map left"))
333     {
334         map_scroll (world->map, WEST, win_map->frame.size);
335     }
336     else if (key == get_action_key(world->keybindings, "map center player"))
337     {
338         map_center_player (world->map, world->player, win_map->frame.size);
339     }
340     return 0;
341 }