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 void exit_trouble(uint8_t test, char * parent, char * child)
39 char * p1 = "Trouble in ";
42 uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
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 void * p = malloc(size);
55 exit_trouble(NULL == p, f, "malloc()");
61 extern void * try_calloc(size_t size1, size_t size2, char * f)
63 void * p = calloc(size1, size2);
64 exit_trouble(NULL == p, f, "calloc()");
70 extern void check_files_xor(char * p1, char * p2)
72 char * msg1 = "A file '";
73 char * msg2 = "' exists, but no file '";
74 char * msg3 = "'. If everything was in order, both or noe would exist. "
75 "The game won't start until this is corrected.";
76 uint16_t size = strlen(msg1) + strlen(p1) + strlen(msg2) + strlen(p2)
79 if (!access(p1, F_OK) && access(p2, F_OK))
81 sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
85 else if (access(p1, F_OK) && !access(p2, F_OK))
87 sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
95 extern void check_tempfile(char * path)
97 char * msg1 = "A file '";
98 char * msg2 = "' exists, probably from a corrupted previous file saving "
99 "process. To avoid corruption of game files, the game won't "
100 "start until it is removed or renamed.";
101 uint16_t size = strlen(msg1) + strlen(path) + strlen(msg2);
103 sprintf(msg, "%s%s%s", msg1, path, msg2);
104 exit_err(!access(path, F_OK), msg);
109 extern void save_interface_conf()
111 save_keybindings("config/keybindings_global", &world.kb_global);
112 save_keybindings("config/keybindings_wingeom", &world.kb_wingeom);
113 save_keybindings("config/keybindings_winkeys", &world.kb_winkeys);
119 extern void load_interface_conf()
121 init_keybindings("config/keybindings_global", &world.kb_global);
122 init_keybindings("config/keybindings_wingeom", &world.kb_wingeom);
123 init_keybindings("config/keybindings_winkeys", &world.kb_winkeys);
126 sorted_wintoggle_and_activate();
131 extern void unload_interface_conf()
133 free_keybindings(world.kb_global.kbs);
134 free_keybindings(world.kb_wingeom.kbs);
135 free_keybindings(world.kb_winkeys.kbs);
136 while (0 != world.wmeta->active)
138 suspend_win(world.wmeta->active);
145 extern void reload_interface_conf()
147 unload_interface_conf();
148 load_interface_conf();
153 extern void update_log(char * text)
155 char * f_name = "update_log()";
156 static char * last_msg; /* TODO: valgrind is dissatisfied */
157 if (0 == last_msg) /* with this calloc'd pointer not */
158 { /* being freed. Rectify this? */
159 last_msg = try_calloc(1, sizeof(char), f_name);
162 uint16_t len_old = strlen(world.log);
163 if (0 == strcmp(last_msg, text))
165 uint16_t len_whole = len_old + 1;
166 new_text = try_calloc(len_whole + 1, sizeof(char), f_name);
167 memcpy(new_text, world.log, len_old);
168 memcpy(new_text + len_old, ".", 1);
172 uint16_t len_new = strlen(text);
173 uint16_t len_whole = len_old + len_new + 1;
174 new_text = try_calloc(len_whole, sizeof(char), f_name);
175 memcpy(new_text, world.log, len_old);
176 memcpy(new_text + len_old, text, len_new);
177 last_msg = try_calloc(len_new + 1, sizeof(char), f_name);
178 memcpy(last_msg, text, len_new);
181 world.log = new_text;
186 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
190 if (mapsize > framesize)
192 if (pos > framesize / 2)
194 if (pos < mapsize - (framesize / 2))
196 offset = pos - (framesize / 2);
200 offset = mapsize - framesize;
209 extern void turn_over(char action)
211 char * f_name = "turn_over()";
212 char * err_write = "Trouble in turn_over() with write_uint8() "
213 "writing to opened file 'record_tmp'.";
215 char * recordfile_tmp = "record_tmp";
216 char * recordfile = "record";
217 if (1 == world.interactive)
219 FILE * file_old = try_fopen(recordfile, "r", f_name);
220 FILE * file_new = try_fopen(recordfile_tmp, "w", f_name);
221 char c = fgetc(file_old);
224 exit_err(write_uint8(c, file_new), err_write);
227 try_fclose(file_old, f_name);
228 exit_err(write_uint8(action, file_new), err_write);
229 if ( is_command_id_shortdsc(action, "drop")
230 || is_command_id_shortdsc(action, "use"))
232 exit_err(write_uint8(world.inventory_select, file_new), err_write);
234 try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile, f_name);
237 struct MapObj * player = get_player();
238 struct MapObj * map_object = player;
239 uint8_t first_round = 1;
240 while (0 < player->lifepoints)
242 if (NULL == map_object)
245 map_object = world.map_objs;
247 if (0 < map_object->lifepoints) /* map_object is animate. */
249 if (0 == first_round && 0 == map_object->progress)
251 if (map_object == player)
256 map_object->command = get_moa_id_by_name("move");
257 map_object->arg = sel[rrand() % 4];
260 map_object->progress++;
261 struct MapObjAct * moa = world.map_obj_acts;
262 while (moa->id != map_object->command)
266 if (map_object->progress == moa->effort)
268 moa->func(map_object);
269 map_object->progress = 0;
272 map_object = map_object->next;
278 extern void save_game()
280 char * f_name = "save_game()";
281 char * savefile_tmp = "savefile_tmp";
282 char * savefile = "savefile";
283 FILE * file = try_fopen(savefile_tmp, "w", f_name);
285 sprintf(line, "%u\n", world.mapseed);
286 try_fwrite(line, strlen(line), 1, file, f_name);
287 sprintf(line, "%u\n", world.seed);
288 try_fwrite(line, strlen(line), 1, file, f_name);
289 sprintf(line, "%u\n", world.turn);
290 try_fwrite(line, strlen(line), 1, file, f_name);
291 sprintf(line, "%u\n", world.score);
292 try_fwrite(line, strlen(line), 1, file, f_name);
293 write_map_objects(file);
294 try_fclose_unlink_rename(file, savefile_tmp, savefile, f_name);
299 extern void load_game()
301 char * f_name = "load_game2()";
302 char * filename = "savefile";
303 FILE * file = try_fopen(filename, "r", f_name);
304 uint16_t linemax = get_linemax(file, f_name);
305 char line[linemax + 1];
306 try_fgets(line, linemax + 1, file, f_name);
307 world.mapseed = atoi(line);
308 try_fgets(line, linemax + 1, file, f_name);
309 world.seed = atoi(line);
310 try_fgets(line, linemax + 1, file, f_name);
311 world.turn = atoi(line);
312 try_fgets(line, linemax + 1, file, f_name);
313 world.score = atoi(line);
314 read_map_objects(file, line, linemax);
315 try_fclose(file, f_name);
320 extern struct yx_uint16 find_passable_pos(struct Map * map)
322 struct yx_uint16 pos;
323 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
325 pos.y = rrand() % map->size.y;
326 pos.x = rrand() % map->size.x;
333 extern void nav_inventory(char dir)
337 if (world.inventory_select > 0)
339 world.inventory_select--;
343 struct MapObj * player = get_player();
344 struct MapObj * owned = player->owns;
350 for (; NULL != owned->next; owned = owned->next, n_owned++);
351 if (world.inventory_select < n_owned)
353 world.inventory_select++;