4 #include <stdio.h> /* for rename() */
5 #include <unistd.h> /* for unlink(), acess() */
6 #include <stdlib.h> /* for 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 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
11 #include "map_object_actions.h" /* for is_passable(), move_monster() */
12 #include "map.h" /* for Map struct */
13 #include "main.h" /* for World struct */
14 #include "yx_uint16.h" /* for yx_uint16 struct */
15 #include "rrand.h" /* for rrand(), rrand_seed() */
16 #include "rexit.h" /* for exit_err() */
20 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
33 if (c_count > linemax)
35 linemax = c_count + 1;
44 if (-1 == fseek(file, 0, SEEK_SET))
48 * linemax_p = linemax;
51 * n_lines_p = n_lines;
58 extern void update_log(struct World * world, char * text)
60 static char * last_msg; /* TODO: valgrind is dissatisfied */
61 if (0 == last_msg) /* with this calloc'd pointer */
62 { /* never being freed. */
63 last_msg = calloc(1, sizeof(char)); /* Rectify this ? */
66 uint16_t len_old = strlen(world->log);
67 if (0 == strcmp(last_msg, text))
69 uint16_t len_whole = len_old + 1;
70 new_text = calloc(len_whole + 1, sizeof(char));
71 memcpy(new_text, world->log, len_old);
72 memcpy(new_text + len_old, ".", 1);
76 uint16_t len_new = strlen(text);
77 uint16_t len_whole = len_old + len_new + 1;
78 new_text = calloc(len_whole, sizeof(char));
79 memcpy(new_text, world->log, len_old);
80 memcpy(new_text + len_old, text, len_new);
81 last_msg = calloc(len_new + 1, sizeof(char));
82 memcpy(last_msg, text, len_new);
85 world->log = new_text;
90 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
94 if (mapsize > framesize)
96 if (pos > framesize / 2)
98 if (pos < mapsize - (framesize / 2))
100 offset = pos - (framesize / 2);
104 offset = mapsize - framesize;
113 extern void turn_over(struct World * world, char action)
115 char * err_open = "Trouble in turn_over() with fopen() "
116 "opening file 'record_tmp' for appending.";
117 char * err_write = "Trouble in turn_over() with write_uint8() "
118 "writing to opened file 'record_tmp'.";
119 char * err_close = "Trouble in turn_over() with fclose() "
120 "closing opened file 'record'.";
121 char * err_unl = "Trouble in turn_over() with unlink() "
122 "unlinking old file 'record'.";
123 char * err_move = "Trouble in turn_over() with rename() "
124 "renaming file 'record_tmp' to 'record'.";
125 char * recordfile_tmp = "record_tmp";
126 char * recordfile = "record";
127 if (1 == world->interactive)
129 FILE * file_old = fopen(recordfile, "r");
130 FILE * file_new = fopen(recordfile_tmp, "w");
131 exit_err(0 == file_old, world, err_open);
132 char c = fgetc(file_old);
135 exit_err(write_uint8(c, file_new), world, err_write);
138 exit_err(fclose(file_old), world, err_close);
139 exit_err(write_uint8(action, file_new), world, err_write);
140 err_close = "Trouble in turn_over() with fclose() "
141 "closing opened file 'record_tmp'.";
142 exit_err(fclose(file_new), world, err_close);
143 exit_err(unlink(recordfile), world, err_unl);
144 exit_err(rename(recordfile_tmp, recordfile), world, err_move);
147 rrand_seed(world->seed * world->turn);
148 struct Monster * monster;
149 for (monster = world->monster;
151 monster = monster->map_obj.next)
153 move_monster(world, monster);
159 extern void save_game(struct World * world)
161 char * err_open = "Trouble in save_game() with fopen() "
162 "opening file 'savefile_tmp' for writing.";
163 char * err_write = "Trouble in save_game() "
164 "writing to opened file 'savefile_tmp'.";
165 char * err_close = "Trouble in save_game() with fclose() "
166 "closing opened file 'savefile_tmp'.";
167 char * err_unl = "Trouble in save_game() with unlink() "
168 "unlinking old 'savefile' file.";
169 char * err_move = "Trouble in save_game() with rename() "
170 "renaming 'file savefile_tmp' to 'savefile'.";
171 char * savefile_tmp = "savefile_tmp";
172 char * savefile = "savefile";
173 FILE * file = fopen(savefile_tmp, "w");
174 exit_err(0 == file, world, err_open);
175 if ( write_uint32_bigendian(world->seed, file)
176 || write_uint32_bigendian(world->turn, file)
177 || write_uint16_bigendian(world->score, file)
178 || write_uint16_bigendian(world->player->pos.y + 1, file)
179 || write_uint16_bigendian(world->player->pos.x + 1, file)
180 || write_uint8(world->player->hitpoints, file)
181 || write_map_objects(world, world->monster, file)
182 || write_map_objects(world, world->item, file))
184 exit_err(1, world, err_write);
186 exit_err(fclose(file), world, err_close);
187 if (!access(savefile, F_OK))
189 exit_err(unlink(savefile), world, err_unl);
191 exit_err(rename(savefile_tmp, savefile), world, err_move);
196 extern struct yx_uint16 find_passable_pos(struct Map * map)
198 struct yx_uint16 pos;
199 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
201 pos.y = rrand() % map->size.y;
202 pos.x = rrand() % map->size.x;