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 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 void textfile_sizes(FILE * file, uint16_t * linemax_p,
33 if (c_count > linemax)
35 linemax = c_count + 1;
44 fseek(file, 0, SEEK_SET);
45 * linemax_p = linemax;
48 * n_lines_p = n_lines;
54 extern void update_log(struct World * world, char * text)
56 static char * last_msg;
59 last_msg = calloc(1, sizeof(char));
62 uint16_t len_old = strlen(world->log);
63 if (0 == strcmp(last_msg, text))
65 uint16_t len_whole = len_old + 1;
66 new_text = calloc(len_whole + 1, sizeof(char));
67 memcpy(new_text, world->log, len_old);
68 memcpy(new_text + len_old, ".", 1);
72 uint16_t len_new = strlen(text);
73 uint16_t len_whole = len_old + len_new + 1;
74 new_text = calloc(len_whole, sizeof(char));
75 memcpy(new_text, world->log, len_old);
76 memcpy(new_text + len_old, text, len_new);
77 last_msg = calloc(len_new + 1, sizeof(char));
78 memcpy(last_msg, text, len_new);
81 world->log = new_text;
86 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
90 if (mapsize > framesize)
92 if (pos > framesize / 2)
94 if (pos < mapsize - (framesize / 2))
96 offset = pos - (framesize / 2);
100 offset = mapsize - framesize;
109 extern void turn_over(struct World * world, char action)
111 char * err_open = "Trouble in turn_over() with fopen() "
112 "opening file 'record_tmp' for appending.";
113 char * err_write = "Trouble in turn_over() with write_uint8() "
114 "writing to opened file 'record_tmp'.";
115 char * err_close = "Trouble in turn_over() with fclose() "
116 "closing opened file 'record'.";
117 char * err_unl = "Trouble in turn_over() with unlink() "
118 "unlinking old file 'record'.";
119 char * err_move = "Trouble in turn_over() with rename() "
120 "renaming file 'record_tmp' to 'record'.";
121 char * recordfile_tmp = "record_tmp";
122 char * recordfile = "record";
123 if (1 == world->interactive)
125 FILE * file_old = fopen(recordfile, "r");
126 FILE * file_new = fopen(recordfile_tmp, "w");
127 exit_err(0 == file_old, world, err_open);
128 char c = fgetc(file_old);
131 exit_err(write_uint8(c, file_new), world, err_write);
134 exit_err(fclose(file_old), world, err_close);
135 exit_err(write_uint8(action, file_new), world, err_write);
136 err_close = "Trouble in turn_over() with fclose() "
137 "closing opened file 'record_tmp'.";
138 exit_err(fclose(file_new), world, err_close);
139 exit_err(unlink(recordfile), world, err_unl);
140 exit_err(rename(recordfile_tmp, recordfile), world, err_move);
143 rrand_seed(world->seed * world->turn);
144 struct Monster * monster;
145 for (monster = world->monster;
147 monster = monster->map_obj.next)
149 move_monster(world, monster);
155 extern void save_game(struct World * world)
157 char * err_open = "Trouble in save_game() with fopen() "
158 "opening file 'savefile_tmp' for writing.";
159 char * err_write = "Trouble in save_game() "
160 "writing to opened file 'savefile_tmp'.";
161 char * err_close = "Trouble in save_game() with fclose() "
162 "closing opened file 'savefile_tmp'.";
163 char * err_unl = "Trouble in save_game() with unlink() "
164 "unlinking old 'savefile' file.";
165 char * err_move = "Trouble in save_game() with rename() "
166 "renaming 'file savefile_tmp' to 'savefile'.";
167 char * savefile_tmp = "savefile_tmp";
168 char * savefile = "savefile";
169 FILE * file = fopen(savefile_tmp, "w");
170 exit_err(0 == file, world, err_open);
171 if ( write_uint32_bigendian(world->seed, file)
172 || write_uint32_bigendian(world->turn, file)
173 || write_uint16_bigendian(world->score, file)
174 || write_uint16_bigendian(world->player->pos.y + 1, file)
175 || write_uint16_bigendian(world->player->pos.x + 1, file)
176 || write_uint8(world->player->hitpoints, file)
177 || write_map_objects(world, world->monster, file)
178 || write_map_objects(world, world->item, file))
180 exit_err(1, world, err_write);
182 exit_err(fclose(file), world, err_close);
183 if (!access(savefile, F_OK))
185 exit_err(unlink(savefile), world, err_unl);
187 exit_err(rename(savefile_tmp, savefile), world, err_move);
192 extern struct yx_uint16 find_passable_pos(struct Map * map)
194 struct yx_uint16 pos;
195 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
197 pos.y = rrand() % map->size.y;
198 pos.x = rrand() % map->size.x;