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()
12 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
13 #include "map_object_actions.h" /* for is_passable(), move_monster() */
14 #include "map.h" /* for Map struct */
15 #include "main.h" /* for World struct */
16 #include "yx_uint16.h" /* for yx_uint16 struct */
17 #include "rrand.h" /* for rrand(), rrand_seed() */
18 #include "rexit.h" /* for exit_err() */
19 #include "wincontrol.h" /* for init_winconfs(), init_wins(), free_winconfs(),
20 * sorted_wintoggle_and_activate()
22 #include "windows.h" /* for suspend_win() */
25 extern char * trouble_msg(struct World * w, char * parent, char * child)
27 char * p1 = "Trouble in ";
30 uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
32 char * msg = malloc(size);
33 exit_err(NULL == msg, w, "malloc() in trouble_msg() failed.");
34 sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
40 extern void * try_malloc(size_t size, struct World * w, char * f)
42 char * msg = trouble_msg(w, f, "malloc()");
43 void * p = malloc(size);
44 exit_err(NULL == p, w, msg);
51 extern void * try_calloc(size_t size1, size_t size2, struct World * w, char * f)
53 char * msg = trouble_msg(w, f, "calloc()");
54 void * p = calloc(size1, size2);
55 exit_err(NULL == p, w, msg);
62 extern void check_files_xor(char * p1, char * p2, struct World * w)
64 char * msg1 = "A file '";
65 char * msg2 = "' exists, but no file '";
66 char * msg3 = "'. If everything was in order, both or noe would exist. "
67 "The game won't start until this is corrected.";
68 uint16_t size = strlen(msg1) + strlen(p1) + strlen(msg2) + strlen(p2)
71 if (!access(p1, F_OK) && access(p2, F_OK))
73 sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
77 else if (access(p1, F_OK) && !access(p2, F_OK))
79 sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
87 extern void check_tempfile(char * path, struct World * w)
89 char * msg1 = "A file '";
90 char * msg2 = "' exists, probably from a corrupted previous file saving "
91 "process. To avoid corruption of game files, the game won't "
92 "start until it is removed or renamed.";
93 uint16_t size = strlen(msg1) + strlen(path) + strlen(msg2);
95 sprintf(msg, "%s%s%s", msg1, path, msg2);
96 exit_err(!access(path, F_OK), w, msg);
101 extern void save_interface_conf(struct World * world)
103 save_keybindings(world, "config/keybindings_global", &world->kb_global);
104 save_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
105 save_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
106 save_win_configs(world);
111 extern void load_interface_conf(struct World * world)
113 init_keybindings(world, "config/keybindings_global", &world->kb_global);
114 init_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
115 init_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
116 init_winconfs(world);
118 sorted_wintoggle_and_activate(world);
123 extern void unload_interface_conf(struct World * world)
125 free_keybindings(world->kb_global.kbs);
126 free_keybindings(world->kb_wingeom.kbs);
127 free_keybindings(world->kb_winkeys.kbs);
128 while (0 != world->wmeta->active)
130 suspend_win(world->wmeta, world->wmeta->active);
132 free_winconfs(world);
137 extern void update_log(struct World * world, char * text)
139 char * f_name = "update_log()";
140 static char * last_msg; /* TODO: valgrind is dissatisfied */
141 if (0 == last_msg) /* with this calloc'd pointer not */
142 { /* being freed. Rectify this? */
143 last_msg = try_calloc(1, sizeof(char), world, f_name);
146 uint16_t len_old = strlen(world->log);
147 if (0 == strcmp(last_msg, text))
149 uint16_t len_whole = len_old + 1;
150 new_text = try_calloc(len_whole + 1, sizeof(char), world, f_name);
151 memcpy(new_text, world->log, len_old);
152 memcpy(new_text + len_old, ".", 1);
156 uint16_t len_new = strlen(text);
157 uint16_t len_whole = len_old + len_new + 1;
158 new_text = try_calloc(len_whole, sizeof(char), world, f_name);
159 memcpy(new_text, world->log, len_old);
160 memcpy(new_text + len_old, text, len_new);
161 last_msg = try_calloc(len_new + 1, sizeof(char), world, f_name);
162 memcpy(last_msg, text, len_new);
165 world->log = new_text;
170 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
174 if (mapsize > framesize)
176 if (pos > framesize / 2)
178 if (pos < mapsize - (framesize / 2))
180 offset = pos - (framesize / 2);
184 offset = mapsize - framesize;
193 extern void turn_over(struct World * world, char action)
195 char * f_name = "turn_over()";
196 char * err_write = "Trouble in turn_over() with write_uint8() "
197 "writing to opened file 'record_tmp'.";
199 char * recordfile_tmp = "record_tmp";
200 char * recordfile = "record";
201 if (1 == world->interactive)
203 FILE * file_old = try_fopen(recordfile, "r", world, f_name);
204 FILE * file_new = try_fopen(recordfile_tmp, "w", world, f_name);
205 char c = fgetc(file_old);
208 exit_err(write_uint8(c, file_new), world, err_write);
211 try_fclose(file_old, world, f_name);
212 exit_err(write_uint8(action, file_new), world, err_write);
213 try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile,
217 rrand_seed(world->seed * world->turn);
218 struct Monster * monster;
219 for (monster = world->monster;
221 monster = monster->map_obj.next)
223 move_monster(world, monster);
229 extern void save_game(struct World * world)
231 char * f_name = "save_game()";
232 char * err_write = "Trouble in save_game() "
233 "writing to opened file 'savefile_tmp'.";
235 char * savefile_tmp = "savefile_tmp";
236 char * savefile = "savefile";
237 FILE * file = try_fopen(savefile_tmp, "w", world, f_name);
238 if ( write_uint32_bigendian(world->seed, file)
239 || write_uint32_bigendian(world->turn, file)
240 || write_uint16_bigendian(world->score, file)
241 || write_uint16_bigendian(world->player->pos.y + 1, file)
242 || write_uint16_bigendian(world->player->pos.x + 1, file)
243 || write_uint8(world->player->hitpoints, file)
244 || write_map_objects(world, world->monster, file)
245 || write_map_objects(world, world->item, file))
247 exit_err(1, world, err_write);
249 try_fclose_unlink_rename(file, savefile_tmp, savefile, world, f_name);
254 extern struct yx_uint16 find_passable_pos(struct Map * map)
256 struct yx_uint16 pos;
257 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
259 pos.y = rrand() % map->size.y;
260 pos.x = rrand() % map->size.x;