3 #define _POSIX_C_SOURCE 199309L
5 #include <errno.h> /* global errno */
6 #include <limits.h> /* PIPE_BUF */
7 #include <stddef.h> /* size_t, NULL */
8 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
9 #include <stdio.h> /* defines EOF, FILE, sprintf() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), memcpy(), memset() */
12 #include <sys/types.h> /* time_t */
13 #include <time.h> /* time(), nanosleep() */
14 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
15 * try_fwrite(), try_fputc(), try_fgetc()
17 #include "../common/rexit.h" /* exit_trouble() */
18 #include "../common/try_malloc.h" /* try_malloc() */
19 #include "cleanup.h" /* set_cleanup_flag() */
20 #include "field_of_view.h" /* VISIBLE */
21 #include "hardcoded_strings.h" /* s */
22 #include "map.h" /* yx_to_map_pos() */
23 #include "things.h" /* Thing, ThingType, get_thing_type(), get_player() */
24 #include "world.h" /* global world */
28 /* Write to "file" god commands (one per line) to recreate thing "t". */
29 static void write_key_value(FILE * file, char * key, uint32_t value);
31 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
32 static void write_thing(FILE * file, struct Thing * t);
34 /* Cut out and return first \0-terminated string from world.queue and
35 * appropriately reduce world.queue_size. Return NULL if queue is empty.
36 * Superfluous \0 bytes after the string are also cut out. Should the queue
37 * start with \0 bytes, those are cut out, but NULL is returned instead of "".
39 static char * get_message_from_queue();
41 /* Poll input file for world.queue input. Wait a few seconds until giving up;
42 * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
44 static void read_file_into_queue();
46 /* Write world state as visible to clients to its file. Write single dot line to
47 * server output file to satisfy client ping mechanisms.
49 static void update_worldstate_file();
51 /* Write "value" to new \n-delimited line of "file". */
52 static void write_value_as_line(uint32_t value, FILE * file);
54 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
55 static void write_inventory(struct Thing * player, FILE * file);
57 /* Return map cells sequence as visible to the "player", with invisible cells as
58 * whitespace. Super-impose over visible map cells things positioned there.
60 static char * build_visible_map(struct Thing * player);
62 /* Write to "file" game map as visible to "player", build_visible_map()-drawn.
63 * Write one row per \n-delimited line.
65 static void write_map(struct Thing * player, FILE * file);
69 static void write_key_value(FILE * file, char * key, uint32_t value)
71 char * f_name = "write_key_value()";
72 try_fwrite(key, strlen(key), 1, file, f_name);
73 try_fputc(' ', file, f_name);
74 char * line = try_malloc(11, f_name);
75 exit_trouble(-1 == sprintf(line, "%u", value), f_name, "sprintf()");
76 try_fwrite(line, strlen(line), 1, file, f_name);
78 try_fputc('\n', file, f_name);
83 static void write_thing(FILE * file, struct Thing * t)
85 char * f_name = "write_thing()";
87 for (o = t->owns; o; o = o->next)
91 write_key_value(file, s[S_CMD_THING], t->id);
92 write_key_value(file, s[S_CMD_TYPE], t->type);
93 write_key_value(file, s[S_CMD_POS_Y], t->pos.y);
94 write_key_value(file, s[S_CMD_POS_X], t->pos.x);
95 write_key_value(file, s[S_CMD_COMMAND], t->command);
96 write_key_value(file, s[S_CMD_ARGUMENT], t->arg);
97 write_key_value(file, s[S_CMD_PROGRESS], t->progress);
98 write_key_value(file, s[S_CMD_LIFEPOINTS], t->lifepoints);
99 for (o = t->owns; o; o = o->next)
101 write_key_value(file, s[S_CMD_CARRIES], o->id);
103 try_fputc('\n', file, f_name);
108 static char * get_message_from_queue()
110 char * f_name = "get_message_from_queue()";
111 char * message = NULL;
112 if (world.queue_size)
114 size_t cutout_len = strlen(world.queue);
118 message = try_malloc(cutout_len, f_name);
119 memcpy(message, world.queue, cutout_len);
122 cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
124 world.queue_size = world.queue_size - cutout_len;
125 if (0 == world.queue_size)
127 free(world.queue); /* NULL so read_file_into_queue() may free() */
128 world.queue = NULL; /* this every time, even when it's */
129 } /* un-allocated first. */
132 char * new_queue = try_malloc(world.queue_size, f_name);
133 memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
135 world.queue = new_queue;
143 static void read_file_into_queue()
145 char * f_name = "read_file_into_queue()";
146 uint8_t wait_seconds = 5;
147 time_t now = time(0);
150 dur.tv_nsec = 33333333;
152 while (EOF == (test = try_fgetc(world.file_in, f_name)))
154 nanosleep(&dur, NULL);
155 if (time(0) > now + wait_seconds)
162 char c = (char) test;
167 char * new_queue = try_malloc(world.queue_size + 1, f_name);
168 memcpy(new_queue, world.queue, world.queue_size);
169 char * new_pos = new_queue + world.queue_size;
173 world.queue = new_queue;
175 while (EOF != (test = try_fgetc(world.file_in, f_name)));
180 static void update_worldstate_file()
182 char * f_name = "update_worldstate_file()";
183 uint16_t size = strlen(s[S_PATH_WORLDSTATE])+strlen(s[S_PATH_SUFFIX_TMP])+1;
184 char * path_tmp = try_malloc(size, f_name);
185 int test=sprintf(path_tmp,"%s%s",s[S_PATH_WORLDSTATE],s[S_PATH_SUFFIX_TMP]);
186 exit_trouble(test < 0, f_name, "sprintf()");
187 FILE * file = try_fopen(path_tmp, "w", f_name);
188 struct Thing * player = get_player();
189 write_value_as_line(world.turn, file);
190 write_value_as_line(player->lifepoints, file);
191 write_inventory(player, file);
192 write_value_as_line(player->pos.y, file);
193 write_value_as_line(player->pos.x, file);
194 write_value_as_line(world.map.length, file);
195 write_map(player, file);
198 try_fwrite(world.log, strlen(world.log), 1, file, f_name);
200 try_fclose_unlink_rename(file, path_tmp, s[S_PATH_WORLDSTATE], f_name);
202 set_cleanup_flag(CLEANUP_WORLDSTATE);
204 try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
205 fflush(world.file_out);
210 static void write_value_as_line(uint32_t value, FILE * file)
212 char * f_name = "write_value_as_line()";
213 char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
214 exit_trouble(sprintf(write_buf, "%u\n", value) < 0, f_name, "sprintf()");
215 try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
220 static void write_inventory(struct Thing * player, FILE * file)
222 char * f_name = "write_inventory()";
223 struct Thing * owned = player->owns;
226 char * empty = "(none)\n";
227 try_fwrite(empty, strlen(empty), 1, file, f_name);
232 for (q = 0; NULL != owned; q++)
234 struct ThingType * tt = get_thing_type(owned->type);
235 try_fwrite(tt->name, strlen(tt->name), 1, file, f_name);
236 try_fputc('\n', file, f_name);
240 try_fputc('%', file, f_name);
241 try_fputc('\n', file, f_name);
246 static char * build_visible_map(struct Thing * player)
248 char * f_name = "build_visible_map()";
249 uint32_t map_size = world.map.length * world.map.length;
250 char * visible_map = try_malloc(map_size, f_name);
251 memset(visible_map, ' ', map_size);
252 if (player->fov_map) /* May fail if player thing was created / positioned */
253 { /* by god command after turning off FOV building. */
255 for (pos_i = 0; pos_i < map_size; pos_i++)
257 if (player->fov_map[pos_i] & VISIBLE)
259 visible_map[pos_i] = world.map.cells[pos_i];
263 struct ThingType * tt;
266 for (i = 0; i < 2; i++)
268 for (t = world.things; t != 0; t = t->next)
270 if ( player->fov_map[yx_to_map_pos(&t->pos)] & VISIBLE
271 && ( (0 == i && 0 == t->lifepoints)
272 || (1 == i && 0 < t->lifepoints)))
274 tt = get_thing_type(t->type);
276 visible_map[yx_to_map_pos(&t->pos)] = c;
286 static void write_map(struct Thing * player, FILE * file)
288 char * f_name = "write_map()";
289 char * visible_map = build_visible_map(player);
291 for (y = 0; y < world.map.length; y++)
293 for (x = 0; x < world.map.length; x++)
295 try_fputc(visible_map[(y * world.map.length) + x], file, f_name);
297 try_fputc('\n', file, f_name);
304 extern char * io_round()
306 char * f_name = "io_round()";
307 if (0 < world.queue_size)
309 return get_message_from_queue();
311 if (world.turn != world.last_update_turn)
313 update_worldstate_file();
314 world.last_update_turn = world.turn;
316 read_file_into_queue();
317 if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
319 char * new_queue = try_malloc(world.queue_size + 1, f_name);
320 memcpy(new_queue, world.queue, world.queue_size);
321 new_queue[world.queue_size] = '\0';
324 world.queue = new_queue;
326 return get_message_from_queue();
331 extern void save_world()
333 char * f_name = "save_world()";
334 char * path = s[S_PATH_SAVE];
335 FILE * file = try_fopen(path, "w", f_name);
336 write_key_value(file, s[S_CMD_DO_FOV], 0);
337 try_fputc('\n', file, f_name);
338 write_key_value(file, s[S_CMD_SEED_MAP], world.seed_map);
339 write_key_value(file, s[S_CMD_SEED_RAND], world.seed);
340 write_key_value(file, s[S_CMD_TURN], world.turn);
341 try_fputc('\n', file, f_name);
343 for (t = world.things; t; t = t->next)
345 write_thing(file, t);
347 write_key_value(file, s[S_CMD_DO_FOV], 1);
348 try_fclose(file, f_name);