home · contact · privacy
697c182f7ae2b856c96e15a1727ddac84c8c46b0
[plomrogue] / src / misc.c
1 /* misc.c */
2
3 #include "misc.h"
4 #include <stdio.h> /* for rename() */
5 #include <unistd.h> /* for unlink(), acess() */
6 #include <stdlib.h> /* for calloc(), free() */
7 #include <string.h> /* for strlen(), strcmp(), memcpy() */
8 #include <stdint.h> /* for uint8_t */
9 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
10                       * resize_active_win(), cycle_active_win(),
11                       * shift_active_win(), struct Win, struct WinMeta
12                       */
13 #include "keybindings.h" /* for get_action_key(), save_keybindings(),
14                           * keyswin_move_selection(), keyswin_mod_key()
15                           */
16 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
17 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
18 #include "map_object_actions.h" /* for is_passable(), move_monster() */
19 #include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */
20 #include "main.h" /* for World struct */
21 #include "yx_uint16.h" /* for yx_uint16 */
22 #include "rrand.h" /* for rrand(), rrand_seed() */
23 #include "rexit.h" /* for exit_err() */
24
25
26 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
27                            uint16_t * n_lines_p)
28 {
29     uint16_t n_lines = 0;
30     int c = 0;
31     uint16_t linemax = 0;
32     uint16_t c_count = 0;
33     while (EOF != c)
34     {
35         c_count++;
36         c = getc(file);
37         if ('\n' == c)
38         {
39             if (c_count > linemax)
40             {
41                 linemax = c_count + 1;
42             }
43             c_count = 0;
44             if (n_lines_p)
45             {
46                 n_lines++;
47             }
48         }
49     }
50     fseek(file, 0, SEEK_SET);
51     * linemax_p = linemax;
52     if (n_lines_p)
53     {
54         * n_lines_p = n_lines;
55     }
56 }
57
58
59
60 extern void update_log(struct World * world, char * text)
61 {
62     static char * last_msg;
63     if (0 == last_msg)
64     {
65         last_msg = calloc(1, sizeof(char));
66     }
67     char * new_text;
68     uint16_t len_old = strlen(world->log);
69     if (0 == strcmp(last_msg, text))
70     {
71         uint16_t len_whole = len_old + 1;
72         new_text = calloc(len_whole + 1, sizeof(char));
73         memcpy(new_text, world->log, len_old);
74         memcpy(new_text + len_old, ".", 1);
75     }
76     else
77     {
78         uint16_t len_new = strlen(text);
79         uint16_t len_whole = len_old + len_new + 1;
80         new_text = calloc(len_whole, sizeof(char));
81         memcpy(new_text, world->log, len_old);
82         memcpy(new_text + len_old, text, len_new);
83         last_msg = calloc(len_new + 1, sizeof(char));
84         memcpy(last_msg, text, len_new);
85     }
86     free(world->log);
87     world->log = new_text;
88 }
89
90
91
92 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
93                               uint16_t framesize)
94 {
95     uint16_t offset = 0;
96     if (mapsize > framesize)
97     {
98         if (pos > framesize / 2)
99         {
100             if (pos < mapsize - (framesize / 2))
101             {
102                 offset = pos - (framesize / 2);
103             }
104             else
105             {
106                 offset = mapsize - framesize;
107             }
108         }
109     }
110     return offset;
111 }
112
113
114
115 extern void turn_over(struct World * world, char action)
116 {
117     if (1 == world->interactive)
118     {
119         FILE * file = fopen("record", "a");
120         exit_err(write_uint8(action, file), world, "Record writing failure.");
121         fclose(file);
122     }
123     world->turn++;
124     rrand_seed(world->seed * world->turn);
125     struct Monster * monster;
126     for (monster = world->monster;
127          monster != 0;
128          monster = monster->map_obj.next)
129     {
130         move_monster(world, monster);
131     }
132 }
133
134
135
136 extern void save_game(struct World * world)
137 {
138     char * savefile_tmp = "savefile_tmp";
139     char * savefile = "savefile";
140     FILE * file = fopen(savefile_tmp, "w");
141     exit_err(0 == file, world, "Error saving game: "
142              "Unable to open new savefile for writing.");
143     if (   write_uint32_bigendian(world->seed, file)
144         || write_uint32_bigendian(world->turn, file)
145         || write_uint16_bigendian(world->player->pos.y + 1, file)
146         || write_uint16_bigendian(world->player->pos.x + 1, file)
147         || write_uint8(world->player->hitpoints, file)
148         || write_map_objects(world, world->monster, file)
149         || write_map_objects(world, world->item, file))
150     {
151         exit_err(1, world, "Error saving game: "
152                  "Trouble writing to opened new savefile.");
153     }
154     exit_err(fclose(file), world, "Error saving game: "
155              "Unable to close opened new savefile.");
156     if (!access(savefile, F_OK))
157     {
158         exit_err(unlink(savefile), world, "Error saving game: "
159                  "Unable to unlink old savefile.");
160     }
161     exit_err(rename(savefile_tmp, "savefile"), world, "Error saving game: "
162              "Unable to rename 'savefile_tmp' to 'savefile'.");
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 uint8_t meta_keys(int key, struct World * world,
236                          struct WinMeta * win_meta, struct Win * win_keys,
237                          struct Win * win_map, struct Win * win_info,
238                          struct Win * win_log)
239 {
240     if (key == get_action_key(world->keybindings, "quit"))
241     {
242         return 1;
243     }
244     else if (key == get_action_key(world->keybindings, "scroll pad right"))
245     {
246         scroll_pad (win_meta, '+');
247     }
248     else if (key == get_action_key(world->keybindings, "scroll pad left"))
249     {
250         scroll_pad (win_meta, '-');
251     }
252     else if (key == get_action_key(world->keybindings, "toggle keys window"))
253     {
254         toggle_window(win_meta, win_keys);
255     }
256     else if (key == get_action_key(world->keybindings, "toggle map window"))
257     {
258         toggle_window(win_meta, win_map);
259     }
260     else if (key == get_action_key(world->keybindings, "toggle info window"))
261     {
262         toggle_window(win_meta, win_info);
263     }
264     else if (key == get_action_key(world->keybindings, "toggle log window"))
265     {
266         toggle_window(win_meta, win_log);
267     }
268     else if (key == get_action_key(world->keybindings, "cycle forwards"))
269     {
270         cycle_active_win(win_meta, 'n');
271     }
272     else if (key == get_action_key(world->keybindings, "cycle backwards"))
273     {
274         cycle_active_win(win_meta, 'p');
275     }
276     else if (key == get_action_key(world->keybindings, "shift forwards"))
277     {
278         shift_active_win(win_meta, 'f');
279     }
280     else if (key == get_action_key(world->keybindings, "shift backwards"))
281     {
282         shift_active_win(win_meta, 'b');
283     }
284     else if (key == get_action_key(world->keybindings, "grow horizontally"))
285     {
286         growshrink_active_window(win_meta, '*');
287     }
288     else if (key == get_action_key(world->keybindings, "shrink horizontally"))
289     {
290         growshrink_active_window(win_meta, '_');
291     }
292     else if (key == get_action_key(world->keybindings, "grow vertically"))
293     {
294         growshrink_active_window(win_meta, '+');
295     }
296     else if (key == get_action_key(world->keybindings, "shrink vertically"))
297     {
298         growshrink_active_window(win_meta, '-');
299     }
300     else if (key == get_action_key(world->keybindings, "save keys"))
301     {
302         save_keybindings(world);
303     }
304     else if (key == get_action_key(world->keybindings, "keys nav up"))
305     {
306         keyswin_move_selection (world, 'u');
307     }
308     else if (key == get_action_key(world->keybindings, "keys nav down"))
309     {
310         keyswin_move_selection (world, 'd');
311     }
312     else if (key == get_action_key(world->keybindings, "keys mod"))
313     {
314         keyswin_mod_key (world, win_meta);
315     }
316     else if (key == get_action_key(world->keybindings, "map up"))
317     {
318         map_scroll (world->map, NORTH, win_map->frame.size);
319      }
320     else if (key == get_action_key(world->keybindings, "map down"))
321     {
322         map_scroll (world->map, SOUTH, win_map->frame.size);
323     }
324     else if (key == get_action_key(world->keybindings, "map right"))
325     {
326         map_scroll (world->map, EAST, win_map->frame.size);
327     }
328     else if (key == get_action_key(world->keybindings, "map left"))
329     {
330         map_scroll (world->map, WEST, win_map->frame.size);
331     }
332     else if (key == get_action_key(world->keybindings, "map center player"))
333     {
334         map_center_player (world->map, world->player, win_map->frame.size);
335     }
336     return 0;
337 }