home · contact · privacy
Not very elegant solution to bug of appropriate inventory selection not being saved...
[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()
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 struct */
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(struct World * w, 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, w, "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, struct World * w, char * f)
45 {
46     char * msg = trouble_msg(w, f, "malloc()");
47     void * p = malloc(size);
48     exit_err(NULL == p, w, msg);
49     free(msg);
50     return p;
51 }
52
53
54
55 extern void * try_calloc(size_t size1, size_t size2, struct World * w, char * f)
56 {
57     char * msg = trouble_msg(w, f, "calloc()");
58     void * p = calloc(size1, size2);
59     exit_err(NULL == p, w, msg);
60     free(msg);
61     return p;
62 }
63
64
65
66 extern void check_files_xor(char * p1, char * p2, struct World * w)
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, w, 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, w, msg);
86     }
87 }
88
89
90
91 extern void check_tempfile(char * path, struct World * w)
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), w, msg);
101 }
102
103
104
105 extern void save_interface_conf(struct World * world)
106 {
107     save_keybindings(world, "config/keybindings_global", &world->kb_global);
108     save_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
109     save_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
110     save_win_configs(world);
111 }
112
113
114
115 extern void load_interface_conf(struct World * world)
116 {
117     init_keybindings(world, "config/keybindings_global",  &world->kb_global);
118     init_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
119     init_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
120     init_winconfs(world);
121     init_wins(world);
122     sorted_wintoggle_and_activate(world);
123 }
124
125
126
127 extern void unload_interface_conf(struct World * world)
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, world->wmeta->active);
135     }
136     free_winconfs(world);
137 }
138
139
140
141 extern void update_log(struct World * world, char * text)
142 {
143     char * f_name = "update_log()";
144     static char * last_msg;                 /* TODO: valgrind is dissatisfied */
145     if (0 == last_msg)                      /* with this calloc'd pointer not */
146     {                                       /* being freed. Rectify this?     */
147         last_msg = try_calloc(1, sizeof(char), world, f_name);
148     }
149     char * new_text;
150     uint16_t len_old = strlen(world->log);
151     if (0 == strcmp(last_msg, text))
152     {
153         uint16_t len_whole = len_old + 1;
154         new_text = try_calloc(len_whole + 1, sizeof(char), world, f_name);
155         memcpy(new_text, world->log, len_old);
156         memcpy(new_text + len_old, ".", 1);
157     }
158     else
159     {
160         uint16_t len_new = strlen(text);
161         uint16_t len_whole = len_old + len_new + 1;
162         new_text = try_calloc(len_whole, sizeof(char), world, f_name);
163         memcpy(new_text, world->log, len_old);
164         memcpy(new_text + len_old, text, len_new);
165         last_msg = try_calloc(len_new + 1, sizeof(char), world, f_name);
166         memcpy(last_msg, text, len_new);
167     }
168     free(world->log);
169     world->log = new_text;
170 }
171
172
173
174 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
175                               uint16_t framesize)
176 {
177     uint16_t offset = 0;
178     if (mapsize > framesize)
179     {
180         if (pos > framesize / 2)
181         {
182             if (pos < mapsize - (framesize / 2))
183             {
184                 offset = pos - (framesize / 2);
185             }
186             else
187             {
188                 offset = mapsize - framesize;
189             }
190         }
191     }
192     return offset;
193 }
194
195
196
197 extern void turn_over(struct World * world, char action)
198 {
199     char * f_name = "turn_over()";
200     char * err_write = "Trouble in turn_over() with write_uint8() "
201                        "writing to opened file 'record_tmp'.";
202
203     char * recordfile_tmp = "record_tmp";
204     char * recordfile     = "record";
205     if (1 == world->interactive)
206     {
207         FILE * file_old = try_fopen(recordfile,     "r", world, f_name);
208         FILE * file_new = try_fopen(recordfile_tmp, "w", world, f_name);
209         char c = fgetc(file_old);
210         while (EOF != c)
211         {
212             exit_err(write_uint8(c, file_new), world, err_write);
213             c = fgetc(file_old);
214         }
215         try_fclose(file_old, world, f_name);
216         exit_err(write_uint8(action, file_new), world, err_write);
217         if (is_command_id_shortdsc(world, action, "drop"))
218         {
219             uint8_t inventory_select = world->old_inventory_select;
220             exit_err(write_uint8(inventory_select, file_new), world, err_write);
221         }
222         try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile,
223                                  world, f_name);
224     }
225     world->turn++;
226     rrand_seed(world->seed * world->turn);
227     struct MapObj * monster;
228     for (monster = world->map_objs;
229          monster != 0;
230          monster = monster->next)
231     {
232         if (0 < monster->lifepoints && 0 != monster->id)
233         {
234             move_actor(world, monster, rrand() % 5);
235         }
236     }
237 }
238
239
240
241 extern void save_game(struct World * world)
242 {
243     char * f_name = "save_game()";
244     char * savefile_tmp = "savefile_tmp";
245     char * savefile     = "savefile";
246     FILE * file = try_fopen(savefile_tmp, "w", world, f_name);
247     char line[12];
248     sprintf(line, "%d\n", world->seed);
249     try_fwrite(line, strlen(line), 1, file, world, f_name);
250     sprintf(line, "%d\n", world->turn);
251     try_fwrite(line, strlen(line), 1, file, world, f_name);
252     sprintf(line, "%d\n", world->score);
253     try_fwrite(line, strlen(line), 1, file, world, f_name);
254     write_map_objects(world, file);
255     try_fclose_unlink_rename(file, savefile_tmp, savefile, world, f_name);
256 }
257
258
259
260 extern void load_game(struct World * world)
261 {
262     char * f_name = "load_game2()";
263     char * filename = "savefile";
264     FILE * file = try_fopen(filename, "r", world, f_name);
265     uint16_t linemax = get_linemax(file, world, f_name);
266     char line[linemax + 1];
267     try_fgets(line, linemax + 1, file, world, f_name);
268     world->seed = atoi(line);
269     try_fgets(line, linemax + 1, file, world, f_name);
270     world->turn = atoi(line);
271     try_fgets(line, linemax + 1, file, world, f_name);
272     world->score = atoi(line);
273     read_map_objects(world, file, line, linemax);
274     try_fclose(file, world, f_name);
275 }
276
277
278
279 extern struct yx_uint16 find_passable_pos(struct Map * map)
280 {
281     struct yx_uint16 pos;
282     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
283     {
284         pos.y = rrand() % map->size.y;
285         pos.x = rrand() % map->size.x;
286     }
287     return pos;
288 }
289
290
291
292 extern void nav_inventory(struct World * world, char dir)
293 {
294     if ('u' == dir)
295     {
296         if (world->inventory_select > 0)
297         {
298             world->inventory_select--;
299         }
300         return;
301     }
302     struct MapObj * player = get_player(world);
303     struct MapObj * owned = player->owns;
304     if (NULL == owned)
305     {
306         return;
307     }
308     uint8_t n_owned = 0;
309     for (; NULL != owned->next; owned = owned->next, n_owned++);
310     if (world->inventory_select < n_owned)
311     {
312         world->inventory_select++;
313     }
314 }