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 MapObj, get_player(), read_map_objects(),
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()
24 #include "windows.h" /* for suspend_win() */
25 #include "command_db.h" /* for is_command_id_shortdsc() */
29 extern char * trouble_msg(struct World * w, char * parent, char * child)
31 char * p1 = "Trouble in ";
34 uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
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);
44 extern void * try_malloc(size_t size, struct World * w, char * f)
46 char * msg = trouble_msg(w, f, "malloc()");
47 void * p = malloc(size);
48 exit_err(NULL == p, w, msg);
55 extern void * try_calloc(size_t size1, size_t size2, struct World * w, char * f)
57 char * msg = trouble_msg(w, f, "calloc()");
58 void * p = calloc(size1, size2);
59 exit_err(NULL == p, w, msg);
66 extern void check_files_xor(char * p1, char * p2, struct World * w)
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)
75 if (!access(p1, F_OK) && access(p2, F_OK))
77 sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
81 else if (access(p1, F_OK) && !access(p2, F_OK))
83 sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
91 extern void check_tempfile(char * path, struct World * w)
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);
99 sprintf(msg, "%s%s%s", msg1, path, msg2);
100 exit_err(!access(path, F_OK), w, msg);
105 extern void save_interface_conf(struct World * world)
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);
115 extern void load_interface_conf(struct World * world)
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);
122 sorted_wintoggle_and_activate(world);
127 extern void unload_interface_conf(struct World * world)
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)
134 suspend_win(world->wmeta, world->wmeta->active);
136 free_winconfs(world);
141 extern void update_log(struct World * world, char * text)
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);
150 uint16_t len_old = strlen(world->log);
151 if (0 == strcmp(last_msg, text))
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);
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);
169 world->log = new_text;
174 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
178 if (mapsize > framesize)
180 if (pos > framesize / 2)
182 if (pos < mapsize - (framesize / 2))
184 offset = pos - (framesize / 2);
188 offset = mapsize - framesize;
197 extern void turn_over(struct World * world, char action)
199 char * f_name = "turn_over()";
200 char * err_write = "Trouble in turn_over() with write_uint8() "
201 "writing to opened file 'record_tmp'.";
203 char * recordfile_tmp = "record_tmp";
204 char * recordfile = "record";
205 if (1 == world->interactive)
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);
212 exit_err(write_uint8(c, file_new), world, err_write);
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"))
219 uint8_t inventory_select = world->old_inventory_select;
220 exit_err(write_uint8(inventory_select, file_new), world, err_write);
222 try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile,
226 rrand_seed(world->seed * world->turn);
227 struct MapObj * monster;
228 for (monster = world->map_objs;
230 monster = monster->next)
232 if (0 < monster->lifepoints && 0 != monster->id)
234 move_actor(world, monster, rrand() % 5);
241 extern void save_game(struct World * world)
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);
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);
260 extern void load_game(struct World * world)
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);
279 extern struct yx_uint16 find_passable_pos(struct Map * map)
281 struct yx_uint16 pos;
282 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
284 pos.y = rrand() % map->size.y;
285 pos.x = rrand() % map->size.x;
292 extern void nav_inventory(struct World * world, char dir)
296 if (world->inventory_select > 0)
298 world->inventory_select--;
302 struct MapObj * player = get_player(world);
303 struct MapObj * owned = player->owns;
309 for (; NULL != owned->next; owned = owned->next, n_owned++);
310 if (world->inventory_select < n_owned)
312 world->inventory_select++;