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