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