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(), fprintf() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), 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 "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, ThingAction, get_thing_type(),
26 #include "world.h" /* global world */
30 /* Write to "file" god commands (one per line) to recreate thing "t". */
31 static void write_key_value(FILE * file, char * key, uint32_t value);
32 static void write_key_string(FILE * file, char * key, char * string);
34 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
35 static void write_thing(FILE * file, struct Thing * t);
37 /* Cut out and return first \0-terminated string from world.queue and
38 * appropriately reduce world.queue_size. Return NULL if queue is empty.
39 * Superfluous \0 bytes after the string are also cut out. Should the queue
40 * start with \0 bytes, those are cut out, but NULL is returned instead of "".
42 static char * get_message_from_queue();
44 /* Poll input file for world.queue input. Wait a few seconds until giving up;
45 * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
47 static void read_file_into_queue();
49 /* Write world state as visible to clients to its file. Write single dot line to
50 * server output file to satisfy client ping mechanisms.
52 static void update_worldstate_file();
54 /* Write "value" to new \n-delimited line of "file". */
55 static void write_value_as_line(uint32_t value, FILE * file);
57 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
58 static void write_inventory(struct Thing * player, FILE * file);
60 /* Return map cells sequence as visible to the "player", with invisible cells as
61 * whitespace. Super-impose over visible map cells things positioned there.
63 static char * build_visible_map(struct Thing * player);
65 /* Write to "file" game map as visible to "player", build_visible_map()-drawn.
66 * Write one row per \n-delimited line.
68 static void write_map(struct Thing * player, FILE * file);
72 static void write_key_value(FILE * file, char * key, uint32_t value)
74 char * f_name = "write_key_value()";
75 try_fwrite(key, strlen(key), 1, file, f_name);
76 try_fputc(' ', file, f_name);
77 char * line = try_malloc(11, f_name);
78 exit_trouble(-1 == sprintf(line, "%u", value), f_name, s[S_FCN_SPRINTF]);
79 try_fwrite(line, strlen(line), 1, file, f_name);
81 try_fputc('\n', file, f_name);
86 static void write_key_string(FILE * file, char * key, char * string)
88 char * f_name = "write_key_string()";
89 try_fwrite(key, strlen(key), 1, file, f_name);
90 try_fputc(' ', file, f_name);
91 uint8_t contains_space = NULL != strchr(string, ' ');
94 try_fputc('\'', file, f_name);
96 try_fwrite(string, strlen(string), 1, file, f_name);
99 try_fputc('\'', file, f_name);
101 try_fputc('\n', file, f_name);
106 static void write_thing(FILE * file, struct Thing * t)
108 char * f_name = "write_thing()";
110 for (o = t->owns; o; o = o->next)
112 write_thing(file, o);
114 write_key_value(file, s[S_CMD_THING], t->id);
115 write_key_value(file, s[S_CMD_T_TYPE], t->type);
116 write_key_value(file, s[S_CMD_T_POSY], t->pos.y);
117 write_key_value(file, s[S_CMD_T_POSX], t->pos.x);
118 write_key_value(file, s[S_CMD_T_COMMAND], t->command);
119 write_key_value(file, s[S_CMD_T_ARGUMENT], t->arg);
120 write_key_value(file, s[S_CMD_T_PROGRESS], t->progress);
121 write_key_value(file, s[S_CMD_T_HP], t->lifepoints);
122 for (o = t->owns; o; o = o->next)
124 write_key_value(file, s[S_CMD_T_CARRIES], o->id);
126 try_fputc('\n', file, f_name);
131 static char * get_message_from_queue()
133 char * f_name = "get_message_from_queue()";
134 char * message = NULL;
135 if (world.queue_size)
137 size_t cutout_len = strlen(world.queue);
141 message = try_malloc(cutout_len, f_name);
142 memcpy(message, world.queue, cutout_len);
145 cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
147 world.queue_size = world.queue_size - cutout_len;
148 if (0 == world.queue_size)
150 free(world.queue); /* NULL so read_file_into_queue() may free() */
151 world.queue = NULL; /* this every time, even when it's */
152 } /* un-allocated first. */
155 char * new_queue = try_malloc(world.queue_size, f_name);
156 memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
158 world.queue = new_queue;
166 static void read_file_into_queue()
168 char * f_name = "read_file_into_queue()";
169 uint8_t wait_seconds = 5;
170 time_t now = time(0);
173 dur.tv_nsec = 33333333;
175 while (EOF == (test = try_fgetc(world.file_in, f_name)))
177 nanosleep(&dur, NULL);
178 if (time(0) > now + wait_seconds)
185 char c = (char) test;
190 char * new_queue = try_malloc(world.queue_size + 1, f_name);
191 memcpy(new_queue, world.queue, world.queue_size);
192 char * new_pos = new_queue + world.queue_size;
196 world.queue = new_queue;
198 while (EOF != (test = try_fgetc(world.file_in, f_name)));
203 static void update_worldstate_file()
205 char * f_name = "update_worldstate_file()";
207 FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
208 struct Thing * player = get_player();
209 write_value_as_line(world.turn, file);
210 write_value_as_line(player->lifepoints, file);
211 write_inventory(player, file);
212 write_value_as_line(player->pos.y, file);
213 write_value_as_line(player->pos.x, file);
214 write_value_as_line(world.map.length, file);
215 write_map(player, file);
218 try_fwrite(world.log, strlen(world.log), 1, file, f_name);
220 atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
221 set_cleanup_flag(CLEANUP_WORLDSTATE);
223 try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
224 fflush(world.file_out);
229 static void write_value_as_line(uint32_t value, FILE * file)
231 char * f_name = "write_value_as_line()";
232 char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
233 exit_trouble(sprintf(write_buf, "%u\n",value) < 0, f_name,s[S_FCN_SPRINTF]);
234 try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
239 static void write_inventory(struct Thing * player, FILE * file)
241 char * f_name = "write_inventory()";
242 struct Thing * owned = player->owns;
245 char * empty = "(none)\n";
246 try_fwrite(empty, strlen(empty), 1, file, f_name);
251 for (q = 0; NULL != owned; q++)
253 struct ThingType * tt = get_thing_type(owned->type);
254 try_fwrite(tt->name, strlen(tt->name), 1, file, f_name);
255 try_fputc('\n', file, f_name);
259 try_fputc('%', file, f_name);
260 try_fputc('\n', file, f_name);
265 static char * build_visible_map(struct Thing * player)
267 char * f_name = "build_visible_map()";
268 uint32_t map_size = world.map.length * world.map.length;
269 char * visible_map = try_malloc(map_size, f_name);
270 memset(visible_map, ' ', map_size);
271 if (player->fov_map) /* May fail if player thing was created / positioned */
272 { /* by god command after turning off FOV building. */
274 for (pos_i = 0; pos_i < map_size; pos_i++)
276 if (player->fov_map[pos_i] & VISIBLE)
278 visible_map[pos_i] = world.map.cells[pos_i];
282 struct ThingType * tt;
285 for (i = 0; i < 2; i++)
287 for (t = world.things; t != 0; t = t->next)
289 if ( player->fov_map[yx_to_map_pos(&t->pos)] & VISIBLE
290 && ( (0 == i && 0 == t->lifepoints)
291 || (1 == i && 0 < t->lifepoints)))
293 tt = get_thing_type(t->type);
295 visible_map[yx_to_map_pos(&t->pos)] = c;
305 static void write_map(struct Thing * player, FILE * file)
307 char * f_name = "write_map()";
308 char * visible_map = build_visible_map(player);
310 for (y = 0; y < world.map.length; y++)
312 for (x = 0; x < world.map.length; x++)
314 try_fputc(visible_map[(y * world.map.length) + x], file, f_name);
316 try_fputc('\n', file, f_name);
323 extern char * io_round()
325 char * f_name = "io_round()";
326 if (0 < world.queue_size)
328 return get_message_from_queue();
332 update_worldstate_file();
335 read_file_into_queue();
336 if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
338 char * new_queue = try_malloc(world.queue_size + 1, f_name);
339 memcpy(new_queue, world.queue, world.queue_size);
340 new_queue[world.queue_size] = '\0';
343 world.queue = new_queue;
345 return get_message_from_queue();
350 extern void save_world()
352 char * f_name = "save_world()";
354 FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
355 write_key_value(file, s[S_CMD_MAPLENGTH], world.map.length);
356 write_key_value(file, s[S_CMD_PLAYTYPE], world.player_type);
357 try_fputc('\n', file, f_name);
358 struct ThingAction * ta;
359 for (ta = world.thing_actions; ta; ta = ta->next)
361 write_key_value(file, s[S_CMD_THINGACTION], ta->id);
362 write_key_value(file, s[S_CMD_TA_EFFORT], ta->effort);
363 write_key_string(file, s[S_CMD_TA_NAME], ta->name);
364 try_fputc('\n', file, f_name);
366 struct ThingType * tt;
367 for (tt = world.thing_types; tt; tt = tt->next)
369 write_key_value(file, s[S_CMD_THINGTYPE], tt->id);
370 write_key_value(file, s[S_CMD_TT_STARTN], tt->start_n);
371 write_key_value(file, s[S_CMD_TT_HP], tt->lifepoints);
372 int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map);
373 exit_trouble(test < 0, f_name, "fprintf()");
374 write_key_string(file, s[S_CMD_TT_NAME], tt->name);
375 write_key_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
376 try_fputc('\n', file, f_name);
378 for (tt = world.thing_types; tt; tt = tt->next)
380 write_key_value(file, s[S_CMD_THINGTYPE], tt->id);
381 write_key_value(file, s[S_CMD_TT_CORPS], tt->corpse_id);
383 try_fputc('\n', file, f_name);
384 write_key_value(file, s[S_CMD_SEED_MAP], world.seed_map);
385 write_key_value(file, s[S_CMD_SEED_RAND], world.seed);
386 write_key_value(file, s[S_CMD_TURN], world.turn);
387 try_fputc('\n', file, f_name);
389 for (t = world.things; t; t = t->next)
391 write_thing(file, t);
393 write_key_value(file, s[S_CMD_WORLD_ACTIVE], 1);
394 atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);