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