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