home · contact · privacy
Minor formatting improvements.
[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 / 65536);    /* TODO: Use bit-shifting for ignoring the less */
89 }                             /* random least significant 16 bits.            */
90
91
92
93 extern void update_log(struct World * world, char * text)
94 {
95     static char * last_msg;
96     if (0 == last_msg)
97     {
98         last_msg = calloc(1, sizeof(char));
99     }
100     char * new_text;
101     uint16_t len_old = strlen(world->log);
102     if (0 == strcmp(last_msg, text))
103     {
104         uint16_t len_whole = len_old + 1;
105         new_text = calloc(len_whole + 1, sizeof(char));
106         memcpy(new_text, world->log, len_old);
107         memcpy(new_text + len_old, ".", 1);
108     }
109     else
110     {
111         uint16_t len_new = strlen(text);
112         uint16_t len_whole = len_old + len_new + 1;
113         new_text = calloc(len_whole, sizeof(char));
114         memcpy(new_text, world->log, len_old);
115         memcpy(new_text + len_old, text, len_new);
116         last_msg = calloc(len_new + 1, sizeof(char));
117         memcpy(last_msg, text, len_new);
118     }
119     free(world->log);
120     world->log = new_text;
121 }
122
123
124
125 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
126                               uint16_t framesize)
127 {
128     uint16_t offset = 0;
129     if (mapsize > framesize)
130     {
131         if (pos > framesize / 2)
132         {
133             if (pos < mapsize - (framesize / 2))
134             {
135                 offset = pos - (framesize / 2);
136             }
137             else
138             {
139                 offset = mapsize - framesize;
140             }
141         }
142     }
143     return offset;
144 }
145
146
147
148 extern void turn_over(struct World * world, char action)
149 {
150     if (1 == world->interactive)
151     {
152         FILE * file = fopen("record", "a");
153         fputc(action, file);
154         fclose(file);
155     }
156     world->turn++;
157     rrand(1, world->seed * world->turn);
158     struct Monster * monster;
159     for (monster = world->monster;
160          monster != 0;
161          monster = monster->map_obj.next)
162     {
163         move_monster(world, monster);
164     }
165 }
166
167
168
169 extern void save_game(struct World * world)
170 {
171     FILE * file = fopen("savefile", "w");
172     write_uint32_bigendian(world->seed, file);
173     write_uint32_bigendian(world->turn, file);
174     write_uint16_bigendian(world->player->pos.y + 1, file);
175     write_uint16_bigendian(world->player->pos.x + 1, file);
176     fputc(world->player->hitpoints, file);
177     write_map_objects (world->monster, file, write_map_objects_monsterdata);
178     write_map_objects (world->item, file, NULL);
179     fclose(file);
180 }
181
182
183
184 extern void toggle_window(struct WinMeta * win_meta, struct Win * win)
185 {
186     if (0 != win->frame.curses_win)
187     {
188         suspend_win(win_meta, win);
189     }
190     else
191     {
192         append_win(win_meta, win);
193     }
194 }
195
196
197
198 extern void scroll_pad(struct WinMeta * win_meta, char dir)
199 {
200     if      ('+' == dir)
201     {
202         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
203     }
204     else if ('-' == dir)
205     {
206         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
207     }
208 }
209
210
211
212 extern void growshrink_active_window(struct WinMeta * win_meta, char change)
213 {
214     if (0 != win_meta->active)
215     {
216         struct yx_uint16 size = win_meta->active->frame.size;
217         if      (change == '-')
218         {
219             size.y--;
220         }
221         else if (change == '+')
222         {
223             size.y++;
224         }
225         else if (change == '_')
226         {
227             size.x--;
228         }
229         else if (change == '*')
230         {
231             size.x++;
232         }
233         resize_active_win (win_meta, size);
234     }
235 }
236
237
238
239 extern struct yx_uint16 find_passable_pos(struct Map * map)
240 {
241     struct yx_uint16 pos;
242     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
243     {
244         pos.y = rrand(0, 0) % map->size.y;
245         pos.x = rrand(0, 0) % map->size.x;
246     }
247     return pos;
248 }
249
250
251
252 extern unsigned char meta_keys(int key, struct World * world,
253                                struct WinMeta * win_meta,
254                                struct Win * win_keys,
255                                struct Win * win_map,
256                                struct Win * win_info,
257                                struct Win * win_log)
258 {
259     if (key == get_action_key(world->keybindings, "quit"))
260     {
261         return 1;
262     }
263     else if (key == get_action_key(world->keybindings, "scroll pad right"))
264     {
265         scroll_pad (win_meta, '+');
266     }
267     else if (key == get_action_key(world->keybindings, "scroll pad left"))
268     {
269         scroll_pad (win_meta, '-');
270     }
271     else if (key == get_action_key(world->keybindings, "toggle keys window"))
272     {
273         toggle_window(win_meta, win_keys);
274     }
275     else if (key == get_action_key(world->keybindings, "toggle map window"))
276     {
277         toggle_window(win_meta, win_map);
278     }
279     else if (key == get_action_key(world->keybindings, "toggle info window"))
280     {
281         toggle_window(win_meta, win_info);
282     }
283     else if (key == get_action_key(world->keybindings, "toggle log window"))
284     {
285         toggle_window(win_meta, win_log);
286     }
287     else if (key == get_action_key(world->keybindings, "cycle forwards"))
288     {
289         cycle_active_win(win_meta, 'n');
290     }
291     else if (key == get_action_key(world->keybindings, "cycle backwards"))
292     {
293         cycle_active_win(win_meta, 'p');
294     }
295     else if (key == get_action_key(world->keybindings, "shift forwards"))
296     {
297         shift_active_win(win_meta, 'f');
298     }
299     else if (key == get_action_key(world->keybindings, "shift backwards"))
300     {
301         shift_active_win(win_meta, 'b');
302     }
303     else if (key == get_action_key(world->keybindings, "grow horizontally"))
304     {
305         growshrink_active_window(win_meta, '*');
306     }
307     else if (key == get_action_key(world->keybindings, "shrink horizontally"))
308     {
309         growshrink_active_window(win_meta, '_');
310     }
311     else if (key == get_action_key(world->keybindings, "grow vertically"))
312     {
313         growshrink_active_window(win_meta, '+');
314     }
315     else if (key == get_action_key(world->keybindings, "shrink vertically"))
316     {
317         growshrink_active_window(win_meta, '-');
318     }
319     else if (key == get_action_key(world->keybindings, "save keys"))
320     {
321         save_keybindings(world);
322     }
323     else if (key == get_action_key(world->keybindings, "keys nav up"))
324     {
325         keyswin_move_selection (world, 'u');
326     }
327     else if (key == get_action_key(world->keybindings, "keys nav down"))
328     {
329         keyswin_move_selection (world, 'd');
330     }
331     else if (key == get_action_key(world->keybindings, "keys mod"))
332     {
333         keyswin_mod_key (world, win_meta);
334     }
335     else if (key == get_action_key(world->keybindings, "map up"))
336     {
337         map_scroll (world->map, NORTH, win_map->frame.size);
338      }
339     else if (key == get_action_key(world->keybindings, "map down"))
340     {
341         map_scroll (world->map, SOUTH, win_map->frame.size);
342     }
343     else if (key == get_action_key(world->keybindings, "map right"))
344     {
345         map_scroll (world->map, EAST, win_map->frame.size);
346     }
347     else if (key == get_action_key(world->keybindings, "map left"))
348     {
349         map_scroll (world->map, WEST, win_map->frame.size);
350     }
351     else if (key == get_action_key(world->keybindings, "map center player"))
352     {
353         map_center_player (world->map, world->player, win_map->frame.size);
354     }
355     return 0;
356 }