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