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[CMD_THING], t->id);
92 write_key_value(file, s[CMD_TYPE], t->type);
93 write_key_value(file, s[CMD_POS_Y], t->pos.y);
94 write_key_value(file, s[CMD_POS_X], t->pos.x);
95 write_key_value(file, s[CMD_COMMAND], t->command);
96 write_key_value(file, s[CMD_ARGUMENT], t->arg);
97 write_key_value(file, s[CMD_PROGRESS], t->progress);
98 write_key_value(file, s[CMD_LIFEPOINTS], t->lifepoints);
99 for (o = t->owns; o; o = o->next)
101 write_key_value(file, 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 char path_tmp[strlen(s[PATH_WORLDSTATE]) + strlen(s[PATH_SUFFIX_TMP]) + 1];
184 sprintf(path_tmp, "%s%s", s[PATH_WORLDSTATE], s[PATH_SUFFIX_TMP]);
185 FILE * file = try_fopen(path_tmp, "w", f_name);
186 struct Thing * player = get_player();
187 write_value_as_line(world.turn, file);
188 write_value_as_line(player->lifepoints, file);
189 write_inventory(player, file);
190 write_value_as_line(player->pos.y, file);
191 write_value_as_line(player->pos.x, file);
192 write_value_as_line(world.map.length, file);
193 write_map(player, file);
196 try_fwrite(world.log, strlen(world.log), 1, file, f_name);
198 try_fclose_unlink_rename(file, path_tmp, s[PATH_WORLDSTATE], f_name);
199 set_cleanup_flag(CLEANUP_WORLDSTATE);
201 try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
202 fflush(world.file_out);
207 static void write_value_as_line(uint32_t value, FILE * file)
209 char * f_name = "write_value_as_line()";
210 char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
211 sprintf(write_buf, "%u\n", value);
212 try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
217 static void write_inventory(struct Thing * player, FILE * file)
219 char * f_name = "write_inventory()";
220 struct Thing * owned = player->owns;
223 char * empty = "(none)\n";
224 try_fwrite(empty, strlen(empty), 1, file, f_name);
229 for (q = 0; NULL != owned; q++)
231 struct ThingType * tt = get_thing_type(owned->type);
232 try_fwrite(tt->name, strlen(tt->name), 1, file, f_name);
233 try_fputc('\n', file, f_name);
237 try_fputc('%', file, f_name);
238 try_fputc('\n', file, f_name);
243 static char * build_visible_map(struct Thing * player)
245 char * f_name = "build_visible_map()";
246 uint32_t map_size = world.map.length * world.map.length;
247 char * visible_map = try_malloc(map_size, f_name);
248 memset(visible_map, ' ', map_size);
249 if (player->fov_map) /* May fail if player thing was created / positioned */
250 { /* by god command after turning off FOV building. */
252 for (pos_i = 0; pos_i < map_size; pos_i++)
254 if (player->fov_map[pos_i] & VISIBLE)
256 visible_map[pos_i] = world.map.cells[pos_i];
260 struct ThingType * tt;
263 for (i = 0; i < 2; i++)
265 for (t = world.things; t != 0; t = t->next)
267 if ( player->fov_map[yx_to_map_pos(&t->pos)] & VISIBLE
268 && ( (0 == i && 0 == t->lifepoints)
269 || (1 == i && 0 < t->lifepoints)))
271 tt = get_thing_type(t->type);
273 visible_map[yx_to_map_pos(&t->pos)] = c;
283 static void write_map(struct Thing * player, FILE * file)
285 char * f_name = "write_map()";
286 char * visible_map = build_visible_map(player);
288 for (y = 0; y < world.map.length; y++)
290 for (x = 0; x < world.map.length; x++)
292 try_fputc(visible_map[(y * world.map.length) + x], file, f_name);
294 try_fputc('\n', file, f_name);
301 extern char * io_round()
303 char * f_name = "io_round()";
304 if (0 < world.queue_size)
306 return get_message_from_queue();
308 if (world.turn != world.last_update_turn)
310 update_worldstate_file();
311 world.last_update_turn = world.turn;
313 read_file_into_queue();
314 if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
316 char * new_queue = try_malloc(world.queue_size + 1, f_name);
317 memcpy(new_queue, world.queue, world.queue_size);
318 new_queue[world.queue_size] = '\0';
321 world.queue = new_queue;
323 return get_message_from_queue();
328 extern void save_world()
330 char * f_name = "save_world()";
331 char * path = s[PATH_SAVE];
332 FILE * file = try_fopen(path, "w", f_name);
333 write_key_value(file, s[CMD_DO_FOV], 0);
334 try_fputc('\n', file, f_name);
335 write_key_value(file, s[CMD_SEED_MAP], world.seed_map);
336 write_key_value(file, s[CMD_SEED_RAND], world.seed);
337 write_key_value(file, s[CMD_TURN], world.turn);
338 try_fputc('\n', file, f_name);
340 for (t = world.things; t; t = t->next)
342 write_thing(file, t);
344 write_key_value(file, s[CMD_DO_FOV], 1);
345 try_fclose(file, f_name);