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