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