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, 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() */
27 extern char * trouble_msg(struct World * w, char * parent, char * child)
29 char * p1 = "Trouble in ";
32 uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
34 char * msg = malloc(size);
35 exit_err(NULL == msg, w, "malloc() in trouble_msg() failed.");
36 sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
42 extern void * try_malloc(size_t size, struct World * w, char * f)
44 char * msg = trouble_msg(w, f, "malloc()");
45 void * p = malloc(size);
46 exit_err(NULL == p, w, msg);
53 extern void * try_calloc(size_t size1, size_t size2, struct World * w, char * f)
55 char * msg = trouble_msg(w, f, "calloc()");
56 void * p = calloc(size1, size2);
57 exit_err(NULL == p, w, msg);
64 extern void check_files_xor(char * p1, char * p2, struct World * w)
66 char * msg1 = "A file '";
67 char * msg2 = "' exists, but no file '";
68 char * msg3 = "'. If everything was in order, both or noe would exist. "
69 "The game won't start until this is corrected.";
70 uint16_t size = strlen(msg1) + strlen(p1) + strlen(msg2) + strlen(p2)
73 if (!access(p1, F_OK) && access(p2, F_OK))
75 sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
79 else if (access(p1, F_OK) && !access(p2, F_OK))
81 sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
89 extern void check_tempfile(char * path, struct World * w)
91 char * msg1 = "A file '";
92 char * msg2 = "' exists, probably from a corrupted previous file saving "
93 "process. To avoid corruption of game files, the game won't "
94 "start until it is removed or renamed.";
95 uint16_t size = strlen(msg1) + strlen(path) + strlen(msg2);
97 sprintf(msg, "%s%s%s", msg1, path, msg2);
98 exit_err(!access(path, F_OK), w, msg);
103 extern void save_interface_conf(struct World * world)
105 save_keybindings(world, "config/keybindings_global", &world->kb_global);
106 save_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
107 save_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
108 save_win_configs(world);
113 extern void load_interface_conf(struct World * world)
115 init_keybindings(world, "config/keybindings_global", &world->kb_global);
116 init_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
117 init_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
118 init_winconfs(world);
120 sorted_wintoggle_and_activate(world);
125 extern void unload_interface_conf(struct World * world)
127 free_keybindings(world->kb_global.kbs);
128 free_keybindings(world->kb_wingeom.kbs);
129 free_keybindings(world->kb_winkeys.kbs);
130 while (0 != world->wmeta->active)
132 suspend_win(world->wmeta, world->wmeta->active);
134 free_winconfs(world);
139 extern void update_log(struct World * world, char * text)
141 char * f_name = "update_log()";
142 static char * last_msg; /* TODO: valgrind is dissatisfied */
143 if (0 == last_msg) /* with this calloc'd pointer not */
144 { /* being freed. Rectify this? */
145 last_msg = try_calloc(1, sizeof(char), world, f_name);
148 uint16_t len_old = strlen(world->log);
149 if (0 == strcmp(last_msg, text))
151 uint16_t len_whole = len_old + 1;
152 new_text = try_calloc(len_whole + 1, sizeof(char), world, f_name);
153 memcpy(new_text, world->log, len_old);
154 memcpy(new_text + len_old, ".", 1);
158 uint16_t len_new = strlen(text);
159 uint16_t len_whole = len_old + len_new + 1;
160 new_text = try_calloc(len_whole, sizeof(char), world, f_name);
161 memcpy(new_text, world->log, len_old);
162 memcpy(new_text + len_old, text, len_new);
163 last_msg = try_calloc(len_new + 1, sizeof(char), world, f_name);
164 memcpy(last_msg, text, len_new);
167 world->log = new_text;
172 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
176 if (mapsize > framesize)
178 if (pos > framesize / 2)
180 if (pos < mapsize - (framesize / 2))
182 offset = pos - (framesize / 2);
186 offset = mapsize - framesize;
195 extern void turn_over(struct World * world, char action)
197 char * f_name = "turn_over()";
198 char * err_write = "Trouble in turn_over() with write_uint8() "
199 "writing to opened file 'record_tmp'.";
201 char * recordfile_tmp = "record_tmp";
202 char * recordfile = "record";
203 if (1 == world->interactive)
205 FILE * file_old = try_fopen(recordfile, "r", world, f_name);
206 FILE * file_new = try_fopen(recordfile_tmp, "w", world, f_name);
207 char c = fgetc(file_old);
210 exit_err(write_uint8(c, file_new), world, err_write);
213 try_fclose(file_old, world, f_name);
214 exit_err(write_uint8(action, file_new), world, err_write);
215 try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile,
219 rrand_seed(world->seed * world->turn);
220 struct MapObj * monster;
221 for (monster = world->map_objs;
223 monster = monster->next)
225 if (0 < monster->lifepoints && 0 != monster->id)
227 move_actor(world, monster, rrand() % 5);
234 extern void save_game(struct World * world)
236 char * f_name = "save_game()";
238 char * savefile_tmp = "savefile_tmp";
239 char * savefile = "savefile";
240 FILE * file = try_fopen(savefile_tmp, "w", world, f_name);
243 sprintf(line, "%d\n", world->seed);
244 try_fwrite(line, strlen(line), 1, file, world, f_name);
245 sprintf(line, "%d\n", world->turn);
246 try_fwrite(line, strlen(line), 1, file, world, f_name);
247 sprintf(line, "%d\n", world->score);
248 try_fwrite(line, strlen(line), 1, file, world, f_name);
249 write_map_objects(world, file);
251 try_fclose_unlink_rename(file, savefile_tmp, savefile, world, f_name);
256 extern void load_game(struct World * world)
258 char * f_name = "load_game2()";
259 char * filename = "savefile";
260 FILE * file = try_fopen(filename, "r", world, f_name);
261 uint16_t linemax = get_linemax(file, world, f_name);
262 char line[linemax + 1];
263 try_fgets(line, linemax + 1, file, world, f_name);
264 world->seed = atoi(line);
265 try_fgets(line, linemax + 1, file, world, f_name);
266 world->turn = atoi(line);
267 try_fgets(line, linemax + 1, file, world, f_name);
268 world->score = atoi(line);
269 read_map_objects(world, file, line, linemax);
270 try_fclose(file, world, f_name);
275 extern struct yx_uint16 find_passable_pos(struct Map * map)
277 struct yx_uint16 pos;
278 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
280 pos.y = rrand() % map->size.y;
281 pos.x = rrand() % map->size.x;