home · contact · privacy
Unified (and heavily re-factored) (un-)loading/saving of keybindings and window confi...
[plomrogue] / src / misc.c
1 /* misc.c */
2
3 #include "misc.h"
4 #include <errno.h> /* for errno */
5 #include <unistd.h> /* for unlink(), acess() */
6 #include <stdlib.h> /* for size_t, calloc(), free() */
7 #include <string.h> /* for strlen(), strcmp(), memcpy() */
8 #include <stdint.h> /* for uint8_t, uint16_t */
9 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](),
10                         * try_fopen(), try_fclose()
11                         */
12 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
13 #include "map_object_actions.h" /* for is_passable(), move_monster() */
14 #include "map.h" /* for Map struct */
15 #include "main.h" /* for World struct */
16 #include "yx_uint16.h" /* for yx_uint16 struct */
17 #include "rrand.h" /* for rrand(), rrand_seed() */
18 #include "rexit.h" /* for exit_err() */
19 #include "wincontrol.h" /* for init_winconfs(), init_wins(), free_winconfs(),
20                          * sorted_wintoggle_and_activate()
21                          */
22 #include "windows.h" /* for suspend_win() */
23
24
25 extern char * trouble_msg(struct World * w, char * parent, char * child)
26 {
27     char * p1 = "Trouble in ";
28     char * p2 = " with ";
29     char * p3 = ".";
30     uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
31                     + strlen(p3) + 1;
32     char * msg = malloc(size);
33     exit_err(NULL == msg, w, "malloc() in trouble_msg() failed.");
34     sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
35     return msg;
36 }
37
38
39
40 extern void * try_malloc(size_t size, struct World * w, char * f)
41 {
42     char * msg = trouble_msg(w, f, "malloc()");
43     void * p = malloc(size);
44     exit_err(NULL == p, w, msg);
45     free(msg);
46     return p;
47 }
48
49
50
51 extern void * try_calloc(size_t size1, size_t size2, struct World * w, char * f)
52 {
53     char * msg = trouble_msg(w, f, "calloc()");
54     void * p = calloc(size1, size2);
55     exit_err(NULL == p, w, msg);
56     free(msg);
57     return p;
58 }
59
60
61
62 extern void check_files_xor(char * p1, char * p2, struct World * w)
63 {
64     char * msg1 = "A file '";
65     char * msg2 = "' exists, but no file '";
66     char * msg3 = "'. If everything was in order, both or noe would exist. "
67                   "The game won't start until this is corrected.";
68     uint16_t size = strlen(msg1) + strlen(p1) + strlen(msg2) + strlen(p2)
69                     + strlen(msg3);
70     char msg[size];
71     if      (!access(p1, F_OK) && access(p2, F_OK))
72     {
73         sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
74         errno = 0;
75         exit_err(1, w, msg);
76     }
77     else if (access(p1, F_OK) && !access(p2, F_OK))
78     {
79         sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
80         errno = 0;
81         exit_err(1, w, msg);
82     }
83 }
84
85
86
87 extern void check_tempfile(char * path, struct World * w)
88 {
89     char * msg1 = "A file '";
90     char * msg2 = "' exists, probably from a corrupted previous file saving "
91                 "process. To avoid corruption of game files, the game won't  "
92                 "start until it is removed or renamed.";
93     uint16_t size = strlen(msg1) + strlen(path) + strlen(msg2);
94     char msg[size];
95     sprintf(msg, "%s%s%s", msg1, path, msg2);
96     exit_err(!access(path, F_OK), w, msg);
97 }
98
99
100
101 extern void save_interface_conf(struct World * world)
102 {
103     save_keybindings(world, "config/keybindings_global", &world->kb_global);
104     save_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
105     save_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
106     save_win_configs(world);
107 }
108
109
110
111 extern void load_interface_conf(struct World * world)
112 {
113     init_keybindings(world, "config/keybindings_global",  &world->kb_global);
114     init_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
115     init_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
116     init_winconfs(world);
117     init_wins(world);
118     sorted_wintoggle_and_activate(world);
119 }
120
121
122
123 extern void unload_interface_conf(struct World * world)
124 {
125     free_keybindings(world->kb_global.kbs);
126     free_keybindings(world->kb_wingeom.kbs);
127     free_keybindings(world->kb_winkeys.kbs);
128     while (0 != world->wmeta->active)
129     {
130         suspend_win(world->wmeta, world->wmeta->active);
131     }
132     free_winconfs(world);
133 }
134
135
136
137 extern void update_log(struct World * world, char * text)
138 {
139     char * f_name = "update_log()";
140     static char * last_msg;                 /* TODO: valgrind is dissatisfied */
141     if (0 == last_msg)                      /* with this calloc'd pointer not */
142     {                                       /* being freed. Rectify this?     */
143         last_msg = try_calloc(1, sizeof(char), world, f_name);
144     }
145     char * new_text;
146     uint16_t len_old = strlen(world->log);
147     if (0 == strcmp(last_msg, text))
148     {
149         uint16_t len_whole = len_old + 1;
150         new_text = try_calloc(len_whole + 1, sizeof(char), world, f_name);
151         memcpy(new_text, world->log, len_old);
152         memcpy(new_text + len_old, ".", 1);
153     }
154     else
155     {
156         uint16_t len_new = strlen(text);
157         uint16_t len_whole = len_old + len_new + 1;
158         new_text = try_calloc(len_whole, sizeof(char), world, f_name);
159         memcpy(new_text, world->log, len_old);
160         memcpy(new_text + len_old, text, len_new);
161         last_msg = try_calloc(len_new + 1, sizeof(char), world, f_name);
162         memcpy(last_msg, text, len_new);
163     }
164     free(world->log);
165     world->log = new_text;
166 }
167
168
169
170 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
171                               uint16_t framesize)
172 {
173     uint16_t offset = 0;
174     if (mapsize > framesize)
175     {
176         if (pos > framesize / 2)
177         {
178             if (pos < mapsize - (framesize / 2))
179             {
180                 offset = pos - (framesize / 2);
181             }
182             else
183             {
184                 offset = mapsize - framesize;
185             }
186         }
187     }
188     return offset;
189 }
190
191
192
193 extern void turn_over(struct World * world, char action)
194 {
195     char * f_name = "turn_over()";
196     char * err_write = "Trouble in turn_over() with write_uint8() "
197                        "writing to opened file 'record_tmp'.";
198
199     char * recordfile_tmp = "record_tmp";
200     char * recordfile     = "record";
201     if (1 == world->interactive)
202     {
203         FILE * file_old = try_fopen(recordfile,     "r", world, f_name);
204         FILE * file_new = try_fopen(recordfile_tmp, "w", world, f_name);
205         char c = fgetc(file_old);
206         while (EOF != c)
207         {
208             exit_err(write_uint8(c, file_new), world, err_write);
209             c = fgetc(file_old);
210         }
211         try_fclose(file_old, world, f_name);
212         exit_err(write_uint8(action, file_new), world, err_write);
213         try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile,
214                                  world, f_name);
215     }
216     world->turn++;
217     rrand_seed(world->seed * world->turn);
218     struct Monster * monster;
219     for (monster = world->monster;
220          monster != 0;
221          monster = monster->map_obj.next)
222     {
223         move_monster(world, monster);
224     }
225 }
226
227
228
229 extern void save_game(struct World * world)
230 {
231     char * f_name = "save_game()";
232     char * err_write = "Trouble in save_game() "
233                        "writing to opened file 'savefile_tmp'.";
234
235     char * savefile_tmp = "savefile_tmp";
236     char * savefile     = "savefile";
237     FILE * file = try_fopen(savefile_tmp, "w", world, f_name);
238     if (   write_uint32_bigendian(world->seed, file)
239         || write_uint32_bigendian(world->turn, file)
240         || write_uint16_bigendian(world->score, file)
241         || write_uint16_bigendian(world->player->pos.y + 1, file)
242         || write_uint16_bigendian(world->player->pos.x + 1, file)
243         || write_uint8(world->player->hitpoints, file)
244         || write_map_objects(world, world->monster, file)
245         || write_map_objects(world, world->item, file))
246     {
247         exit_err(1, world, err_write);
248     }
249     try_fclose_unlink_rename(file, savefile_tmp, savefile, world, f_name);
250 }
251
252
253
254 extern struct yx_uint16 find_passable_pos(struct Map * map)
255 {
256     struct yx_uint16 pos;
257     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
258     {
259         pos.y = rrand() % map->size.y;
260         pos.x = rrand() % map->size.x;
261     }
262     return pos;
263 }