3 #define _POSIX_C_SOURCE 200112L /* snrpintf() */
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, UINT8_MAX */
9 #include <stdio.h> /* defines EOF, FILE, sprintf(), fprintf() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), snprintf(), memcpy(), memset(), strchr() */
12 #include <sys/types.h> /* time_t */
13 #include <time.h> /* time(), nanosleep() */
14 #include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
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 "hardcoded_strings.h" /* s */
21 #include "things.h" /* Thing, ThingType, ThingAction, get_thing_type(),
24 #include "world.h" /* global world */
28 /* Helpers to write lines of god commands to recreate thing "t". */
29 static void write_key_space(FILE * file, char * key);
30 static void write_value(FILE * file, uint32_t value);
31 static void write_string(FILE * file, char * string);
32 static void write_key_space_value(FILE * file, char * key, uint32_t value);
33 static void write_key_space_string(FILE * file, char * key, char * string);
35 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
36 static void write_thing(FILE * file, struct Thing * t);
38 /* Cut out and return first \0-terminated string from world.queue and
39 * appropriately reduce world.queue_size. Return NULL if queue is empty.
40 * Superfluous \0 bytes after the string are also cut out. Should the queue
41 * start with \0 bytes, those are cut out, but NULL is returned instead of "".
43 static char * get_message_from_queue();
45 /* Poll input file for world.queue input. Wait a few seconds until giving up;
46 * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
48 static void read_file_into_queue();
50 /* Write world state as visible to clients to its file. Write single dot line to
51 * server output file to satisfy client ping mechanisms.
53 static void update_worldstate_file();
55 /* Write "value" to new \n-delimited line of "file". */
56 static void write_value_as_line(uint32_t value, FILE * file);
58 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
59 static void write_inventory(struct Thing * player, FILE * file);
61 /* Return map cells sequence as visible to the "player", with invisible cells as
62 * whitespace. Super-impose over visible map cells things positioned there.
64 static char * build_visible_map(struct Thing * player);
66 /* Write to "file" game map as visible to "player" right now, as drawn by
67 * build_visible_map(), and thereafter game map as memorized by player in its
68 * .mem_map. Write one row per \n-delimited line.
70 static void write_map(struct Thing * player, FILE * file);
74 static void write_key_space(FILE * file, char * key)
76 try_fwrite(key, strlen(key), 1, file, __func__);
77 try_fputc(' ', file, __func__);
82 static void write_value(FILE * file, uint32_t value)
84 char * line = try_malloc(11, __func__);
85 exit_trouble(-1 == sprintf(line, "%u", value), __func__, s[S_FCN_SPRINTF]);
86 try_fwrite(line, strlen(line), 1, file, __func__);
92 static void write_string(FILE * file, char * string)
94 uint8_t contains_space = NULL != strchr(string, ' ');
97 try_fputc('\'', file, __func__);
99 try_fwrite(string, strlen(string), 1, file, __func__);
102 try_fputc('\'', file, __func__);
108 static void write_key_space_value(FILE * file, char * key, uint32_t value)
110 write_key_space(file, key);
111 write_value(file, value);
112 try_fputc('\n', file, __func__);
117 static void write_key_space_string(FILE * file, char * key, char * string)
119 write_key_space(file, key);
120 write_string(file, string);
121 try_fputc('\n', file, __func__);
126 static void write_thing(FILE * file, struct Thing * t)
129 for (o = t->owns; o; o = o->next)
131 write_thing(file, o);
133 write_key_space_value(file, s[S_CMD_T_ID], t->id);
134 write_key_space_value(file, s[S_CMD_T_TYPE], t->type);
135 write_key_space_value(file, s[S_CMD_T_POSY], t->pos.y);
136 write_key_space_value(file, s[S_CMD_T_POSX], t->pos.x);
137 write_key_space_value(file, s[S_CMD_T_COMMAND], t->command);
138 write_key_space_value(file, s[S_CMD_T_ARGUMENT], t->arg);
139 write_key_space_value(file, s[S_CMD_T_PROGRESS], t->progress);
140 write_key_space_value(file, s[S_CMD_T_HP], t->lifepoints);
141 for (o = t->owns; o; o = o->next)
143 write_key_space_value(file, s[S_CMD_T_CARRIES], o->id);
147 uint32_t map_size = world.map.length * world.map.length;/* snprintf() */
148 char * mem_map_copy = try_malloc(map_size + 1, __func__);/* reads one */
149 memcpy(mem_map_copy, t->mem_map, map_size); /* byte beyond map_size */
150 mem_map_copy[map_size] = '\0'; /* if string is not \0-terminated. */
152 char string[UINT8_MAX + 1 + 1];
153 for (y = 0; y < world.map.length; y++)
156 int test = snprintf(string, world.map.length + 1, "%s",
157 mem_map_copy + (y * world.map.length));
158 exit_trouble(test < 0, __func__, "snprintf()");
159 write_key_space(file, s[S_CMD_T_MEMMAP]);
160 write_value(file, y);
161 try_fputc(' ', file, __func__);
162 write_string(file, string);
163 try_fputc('\n', file, __func__);
167 try_fputc('\n', file, __func__);
172 static char * get_message_from_queue()
174 char * message = NULL;
175 if (world.queue_size)
177 size_t cutout_len = strlen(world.queue);
181 message = try_malloc(cutout_len, __func__);
182 memcpy(message, world.queue, cutout_len);
185 cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
187 world.queue_size = world.queue_size - cutout_len;
188 if (0 == world.queue_size)
190 free(world.queue); /* NULL so read_file_into_queue() may free() */
191 world.queue = NULL; /* this every time, even when it's */
192 } /* un-allocated first. */
195 char * new_queue = try_malloc(world.queue_size, __func__);
196 memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
198 world.queue = new_queue;
206 static void read_file_into_queue()
208 uint8_t wait_seconds = 5;
209 time_t now = time(0);
212 dur.tv_nsec = 33333333;
214 while (EOF == (test = try_fgetc(world.file_in, __func__)))
216 nanosleep(&dur, NULL);
217 if (time(0) > now + wait_seconds)
224 char c = (char) test;
229 char * new_queue = try_malloc(world.queue_size + 1, __func__);
230 memcpy(new_queue, world.queue, world.queue_size);
231 char * new_pos = new_queue + world.queue_size;
235 world.queue = new_queue;
237 while (EOF != (test = try_fgetc(world.file_in, __func__)));
242 static void update_worldstate_file()
245 FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
246 struct Thing * player = get_player();
247 write_value_as_line(world.turn, file);
248 write_value_as_line(player->lifepoints, file);
249 write_inventory(player, file);
250 write_value_as_line(player->pos.y, file);
251 write_value_as_line(player->pos.x, file);
252 write_value_as_line(world.map.length, file);
253 write_map(player, file);
256 try_fwrite(world.log, strlen(world.log), 1, file, __func__);
258 atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
259 set_cleanup_flag(CLEANUP_WORLDSTATE);
261 try_fwrite(dot, strlen(dot), 1, world.file_out, __func__);
262 fflush(world.file_out);
267 static void write_value_as_line(uint32_t value, FILE * file)
269 char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
270 exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]);
271 try_fwrite(write_buf, strlen(write_buf), 1, file, __func__);
276 static void write_inventory(struct Thing * player, FILE * file)
278 struct Thing * owned = player->owns;
281 char * empty = "(none)\n";
282 try_fwrite(empty, strlen(empty), 1, file, __func__);
287 for (q = 0; NULL != owned; q++)
289 struct ThingType * tt = get_thing_type(owned->type);
290 try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
291 try_fputc('\n', file, __func__);
295 try_fputc('%', file, __func__);
296 try_fputc('\n', file, __func__);
301 static char * build_visible_map(struct Thing * player)
303 uint32_t map_size = world.map.length * world.map.length;
304 char * visible_map = try_malloc(map_size, __func__);
305 memset(visible_map, ' ', map_size);
306 if (player->fov_map) /* May fail if player thing was created / positioned */
307 { /* by god command after turning off FOV building. */
309 for (pos_i = 0; pos_i < map_size; pos_i++)
311 if (player->fov_map[pos_i] == 'v')
313 visible_map[pos_i] = world.map.cells[pos_i];
317 struct ThingType * tt;
320 for (i = 0; i < 2; i++)
322 for (t = world.things; t != 0; t = t->next)
324 if ( 'v'==player->fov_map[t->pos.y*world.map.length+t->pos.x]
325 && ( (0 == i && 0 == t->lifepoints)
326 || (1 == i && 0 < t->lifepoints)))
328 tt = get_thing_type(t->type);
330 visible_map[t->pos.y * world.map.length + t->pos.x] = c;
340 static void write_map(struct Thing * player, FILE * file)
342 char * visible_map = build_visible_map(player);
344 for (y = 0; y < world.map.length; y++)
346 for (x = 0; x < world.map.length; x++)
348 try_fputc(visible_map[y * world.map.length + x], file, __func__);
350 try_fputc('\n', file, __func__);
353 for (y = 0; y < world.map.length; y++)
355 for (x = 0; x < world.map.length; x++)
357 try_fputc(player->mem_map[y * world.map.length + x], file, __func__);
359 try_fputc('\n', file, __func__);
365 extern char * io_round()
367 if (0 < world.queue_size)
369 return get_message_from_queue();
373 update_worldstate_file();
376 read_file_into_queue();
377 if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
379 char * new_queue = try_malloc(world.queue_size + 1, __func__);
380 memcpy(new_queue, world.queue, world.queue_size);
381 new_queue[world.queue_size] = '\0';
384 world.queue = new_queue;
386 return get_message_from_queue();
391 extern void save_world()
394 FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
395 write_key_space_value(file, s[S_CMD_MAPLENGTH], world.map.length);
396 write_key_space_value(file, s[S_CMD_PLAYTYPE], world.player_type);
397 try_fputc('\n', file, __func__);
398 struct ThingAction * ta;
399 for (ta = world.thing_actions; ta; ta = ta->next)
401 write_key_space_value(file, s[S_CMD_TA_ID], ta->id);
402 write_key_space_value(file, s[S_CMD_TA_EFFORT], ta->effort);
403 write_key_space_string(file, s[S_CMD_TA_NAME], ta->name);
404 try_fputc('\n', file, __func__);
406 struct ThingType * tt;
407 for (tt = world.thing_types; tt; tt = tt->next)
409 write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
410 write_key_space_value(file, s[S_CMD_TT_STARTN], tt->start_n);
411 write_key_space_value(file, s[S_CMD_TT_HP], tt->lifepoints);
412 int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map);
413 exit_trouble(test < 0, __func__, "fprintf");
414 write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
415 write_key_space_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
416 try_fputc('\n', file, __func__);
418 for (tt = world.thing_types; tt; tt = tt->next)
420 write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
421 write_key_space_value(file, s[S_CMD_TT_CORPS], tt->corpse_id);
423 try_fputc('\n', file, __func__);
424 write_key_space_value(file, s[S_CMD_SEED_MAP], world.seed_map);
425 write_key_space_value(file, s[S_CMD_SEED_RAND], world.seed);
426 write_key_space_value(file, s[S_CMD_TURN], world.turn);
427 try_fputc('\n', file, __func__);
429 for (t = world.things; t; t = t->next)
431 write_thing(file, t);
433 write_key_space_value(file, s[S_CMD_WORLD_ACTIVE], 1);
434 atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);