home · contact · privacy
Dead shoggoths corpse to "magic meat" that can now be "used" i.e. consumed for hitpoi...
[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(), get_linemax()
11                         */
12 #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(),
13                           * write_map_objects()
14                           */
15 #include "map_object_actions.h" /* for is_passable(), move_actor() */
16 #include "map.h" /* for Map struct */
17 #include "main.h" /* for world global */
18 #include "yx_uint16.h" /* for yx_uint16 struct */
19 #include "rrand.h" /* for rrand(), rrand_seed() */
20 #include "rexit.h" /* for exit_err() */
21 #include "wincontrol.h" /* for init_winconfs(), init_wins(), free_winconfs(),
22                          * sorted_wintoggle_and_activate()
23                          */
24 #include "windows.h" /* for suspend_win() */
25 #include "command_db.h" /* for is_command_id_shortdsc() */
26
27
28
29 extern char * trouble_msg(char * parent, char * child)
30 {
31     char * p1 = "Trouble in ";
32     char * p2 = " with ";
33     char * p3 = ".";
34     uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
35                     + strlen(p3) + 1;
36     char * msg = malloc(size);
37     exit_err(NULL == msg, "malloc() in trouble_msg() failed.");
38     sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
39     return msg;
40 }
41
42
43
44 extern void * try_malloc(size_t size, char * f)
45 {
46     char * msg = trouble_msg(f, "malloc()");
47     void * p = malloc(size);
48     exit_err(NULL == p, msg);
49     free(msg);
50     return p;
51 }
52
53
54
55 extern void * try_calloc(size_t size1, size_t size2, char * f)
56 {
57     char * msg = trouble_msg(f, "calloc()");
58     void * p = calloc(size1, size2);
59     exit_err(NULL == p, msg);
60     free(msg);
61     return p;
62 }
63
64
65
66 extern void check_files_xor(char * p1, char * p2)
67 {
68     char * msg1 = "A file '";
69     char * msg2 = "' exists, but no file '";
70     char * msg3 = "'. If everything was in order, both or noe would exist. "
71                   "The game won't start until this is corrected.";
72     uint16_t size = strlen(msg1) + strlen(p1) + strlen(msg2) + strlen(p2)
73                     + strlen(msg3);
74     char msg[size];
75     if      (!access(p1, F_OK) && access(p2, F_OK))
76     {
77         sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
78         errno = 0;
79         exit_err(1, msg);
80     }
81     else if (access(p1, F_OK) && !access(p2, F_OK))
82     {
83         sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
84         errno = 0;
85         exit_err(1, msg);
86     }
87 }
88
89
90
91 extern void check_tempfile(char * path)
92 {
93     char * msg1 = "A file '";
94     char * msg2 = "' exists, probably from a corrupted previous file saving "
95                   "process. To avoid corruption of game files, the game won't "
96                   "start until it is removed or renamed.";
97     uint16_t size = strlen(msg1) + strlen(path) + strlen(msg2);
98     char msg[size];
99     sprintf(msg, "%s%s%s", msg1, path, msg2);
100     exit_err(!access(path, F_OK), msg);
101 }
102
103
104
105 extern void save_interface_conf()
106 {
107     save_keybindings("config/keybindings_global", &world.kb_global);
108     save_keybindings("config/keybindings_wingeom", &world.kb_wingeom);
109     save_keybindings("config/keybindings_winkeys", &world.kb_winkeys);
110     save_win_configs();
111 }
112
113
114
115 extern void load_interface_conf()
116 {
117     init_keybindings("config/keybindings_global",  &world.kb_global);
118     init_keybindings("config/keybindings_wingeom", &world.kb_wingeom);
119     init_keybindings("config/keybindings_winkeys", &world.kb_winkeys);
120     init_winconfs();
121     init_wins();
122     sorted_wintoggle_and_activate();
123 }
124
125
126
127 extern void unload_interface_conf()
128 {
129     free_keybindings(world.kb_global.kbs);
130     free_keybindings(world.kb_wingeom.kbs);
131     free_keybindings(world.kb_winkeys.kbs);
132     while (0 != world.wmeta->active)
133     {
134         suspend_win(world.wmeta->active);
135     }
136     free_winconfs();
137 }
138
139
140
141 extern void reload_interface_conf()
142 {
143     unload_interface_conf();
144     load_interface_conf();
145 }
146
147
148
149 extern void update_log(char * text)
150 {
151     char * f_name = "update_log()";
152     static char * last_msg;                 /* TODO: valgrind is dissatisfied */
153     if (0 == last_msg)                      /* with this calloc'd pointer not */
154     {                                       /* being freed. Rectify this?     */
155         last_msg = try_calloc(1, sizeof(char), f_name);
156     }
157     char * new_text;
158     uint16_t len_old = strlen(world.log);
159     if (0 == strcmp(last_msg, text))
160     {
161         uint16_t len_whole = len_old + 1;
162         new_text = try_calloc(len_whole + 1, sizeof(char), f_name);
163         memcpy(new_text, world.log, len_old);
164         memcpy(new_text + len_old, ".", 1);
165     }
166     else
167     {
168         uint16_t len_new = strlen(text);
169         uint16_t len_whole = len_old + len_new + 1;
170         new_text = try_calloc(len_whole, sizeof(char), f_name);
171         memcpy(new_text, world.log, len_old);
172         memcpy(new_text + len_old, text, len_new);
173         last_msg = try_calloc(len_new + 1, sizeof(char), f_name);
174         memcpy(last_msg, text, len_new);
175     }
176     free(world.log);
177     world.log = new_text;
178 }
179
180
181
182 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
183                               uint16_t framesize)
184 {
185     uint16_t offset = 0;
186     if (mapsize > framesize)
187     {
188         if (pos > framesize / 2)
189         {
190             if (pos < mapsize - (framesize / 2))
191             {
192                 offset = pos - (framesize / 2);
193             }
194             else
195             {
196                 offset = mapsize - framesize;
197             }
198         }
199     }
200     return offset;
201 }
202
203
204
205 extern void turn_over(char action)
206 {
207     char * f_name = "turn_over()";
208     char * err_write = "Trouble in turn_over() with write_uint8() "
209                        "writing to opened file 'record_tmp'.";
210
211     char * recordfile_tmp = "record_tmp";
212     char * recordfile     = "record";
213     if (1 == world.interactive)
214     {
215         FILE * file_old = try_fopen(recordfile,     "r", f_name);
216         FILE * file_new = try_fopen(recordfile_tmp, "w", f_name);
217         char c = fgetc(file_old);
218         while (EOF != c)
219         {
220             exit_err(write_uint8(c, file_new), err_write);
221             c = fgetc(file_old);
222         }
223         try_fclose(file_old, f_name);
224         exit_err(write_uint8(action, file_new), err_write);
225         if (   is_command_id_shortdsc(action, "drop")
226             || is_command_id_shortdsc(action, "use"))
227         {
228             uint8_t inventory_select = world.old_inventory_select;
229             exit_err(write_uint8(inventory_select, file_new), err_write);
230         }
231         try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile, f_name);
232     }
233     world.turn++;
234     rrand_seed(world.seed * world.turn);
235     struct MapObj * monster;
236     for (monster = world.map_objs; monster != 0; monster = monster->next)
237     {
238         if (0 < monster->lifepoints && 0 != monster->id)
239         {
240             char * sel = "\0NSEW";
241             move_actor(monster, sel[rrand() % 5]);
242         }
243     }
244 }
245
246
247
248 extern void save_game()
249 {
250     char * f_name = "save_game()";
251     char * savefile_tmp = "savefile_tmp";
252     char * savefile     = "savefile";
253     FILE * file = try_fopen(savefile_tmp, "w", f_name);
254     char line[12];
255     sprintf(line, "%d\n", world.seed);
256     try_fwrite(line, strlen(line), 1, file, f_name);
257     sprintf(line, "%d\n", world.turn);
258     try_fwrite(line, strlen(line), 1, file, f_name);
259     sprintf(line, "%d\n", world.score);
260     try_fwrite(line, strlen(line), 1, file, f_name);
261     write_map_objects(file);
262     try_fclose_unlink_rename(file, savefile_tmp, savefile, f_name);
263 }
264
265
266
267 extern void load_game()
268 {
269     char * f_name = "load_game2()";
270     char * filename = "savefile";
271     FILE * file = try_fopen(filename, "r", f_name);
272     uint16_t linemax = get_linemax(file, f_name);
273     char line[linemax + 1];
274     try_fgets(line, linemax + 1, file, f_name);
275     world.seed = atoi(line);
276     try_fgets(line, linemax + 1, file, f_name);
277     world.turn = atoi(line);
278     try_fgets(line, linemax + 1, file, f_name);
279     world.score = atoi(line);
280     read_map_objects(file, line, linemax);
281     try_fclose(file, f_name);
282 }
283
284
285
286 extern struct yx_uint16 find_passable_pos(struct Map * map)
287 {
288     struct yx_uint16 pos;
289     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
290     {
291         pos.y = rrand() % map->size.y;
292         pos.x = rrand() % map->size.x;
293     }
294     return pos;
295 }
296
297
298
299 extern void nav_inventory(char dir)
300 {
301     if ('u' == dir)
302     {
303         if (world.inventory_select > 0)
304         {
305             world.inventory_select--;
306         }
307         return;
308     }
309     struct MapObj * player = get_player();
310     struct MapObj * owned = player->owns;
311     if (NULL == owned)
312     {
313         return;
314     }
315     uint8_t n_owned = 0;
316     for (; NULL != owned->next; owned = owned->next, n_owned++);
317     if (world.inventory_select < n_owned)
318     {
319         world.inventory_select++;
320     }
321 }