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()
12 #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(),
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()
23 #include "windows.h" /* for suspend_win() */
24 #include "command_db.h" /* for is_command_id_shortdsc() */
28 extern uint16_t rrand()
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. */
37 extern char * trouble_msg(char * parent, char * child)
39 char * p1 = "Trouble in ";
42 uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
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);
52 extern void * try_malloc(size_t size, char * f)
54 char * msg = trouble_msg(f, "malloc()");
55 void * p = malloc(size);
56 exit_err(NULL == p, msg);
63 extern void * try_calloc(size_t size1, size_t size2, char * f)
65 char * msg = trouble_msg(f, "calloc()");
66 void * p = calloc(size1, size2);
67 exit_err(NULL == p, msg);
74 extern void check_files_xor(char * p1, char * p2)
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)
83 if (!access(p1, F_OK) && access(p2, F_OK))
85 sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
89 else if (access(p1, F_OK) && !access(p2, F_OK))
91 sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
99 extern void check_tempfile(char * path)
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);
107 sprintf(msg, "%s%s%s", msg1, path, msg2);
108 exit_err(!access(path, F_OK), msg);
113 extern void save_interface_conf()
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);
123 extern void load_interface_conf()
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);
130 sorted_wintoggle_and_activate();
135 extern void unload_interface_conf()
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)
142 suspend_win(world.wmeta->active);
149 extern void reload_interface_conf()
151 unload_interface_conf();
152 load_interface_conf();
157 extern void update_log(char * text)
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);
166 uint16_t len_old = strlen(world.log);
167 if (0 == strcmp(last_msg, text))
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);
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);
185 world.log = new_text;
190 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
194 if (mapsize > framesize)
196 if (pos > framesize / 2)
198 if (pos < mapsize - (framesize / 2))
200 offset = pos - (framesize / 2);
204 offset = mapsize - framesize;
213 extern void turn_over(char action)
215 char * f_name = "turn_over()";
216 char * err_write = "Trouble in turn_over() with write_uint8() "
217 "writing to opened file 'record_tmp'.";
219 char * recordfile_tmp = "record_tmp";
220 char * recordfile = "record";
221 if (1 == world.interactive)
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);
228 exit_err(write_uint8(c, file_new), err_write);
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"))
236 exit_err(write_uint8(world.inventory_select, file_new), err_write);
238 try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile, f_name);
241 struct MapObj * player = get_player();
242 struct MapObj * map_object = player;
243 uint8_t first_round = 1;
244 while (0 < player->lifepoints)
246 if (NULL == map_object)
249 map_object = world.map_objs;
251 if (0 < map_object->lifepoints) /* map_object is animate. */
253 if (0 == first_round && 0 == map_object->progress)
255 if (map_object == player)
260 map_object->command = get_moa_id_by_name("move");
261 map_object->arg = sel[rrand() % 4];
264 map_object->progress++;
265 struct MapObjAct * moa = world.map_obj_acts;
266 while (moa->id != map_object->command)
270 if (map_object->progress == moa->effort)
272 moa->func(map_object);
273 map_object->progress = 0;
276 map_object = map_object->next;
282 extern void save_game()
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);
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);
303 extern void load_game()
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);
324 extern struct yx_uint16 find_passable_pos(struct Map * map)
326 struct yx_uint16 pos;
327 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
329 pos.y = rrand() % map->size.y;
330 pos.x = rrand() % map->size.x;
337 extern void nav_inventory(char dir)
341 if (world.inventory_select > 0)
343 world.inventory_select--;
347 struct MapObj * player = get_player();
348 struct MapObj * owned = player->owns;
354 for (; NULL != owned->next; owned = owned->next, n_owned++);
355 if (world.inventory_select < n_owned)
357 world.inventory_select++;