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