home · contact · privacy
0f65cea8c5e36060fa094af9eccd2f03629412b8
[plomrogue] / src / server / io.c
1 /* src/server/io.c */
2
3 #define _POSIX_C_SOURCE 200112L /* snrpintf() */
4 #include "io.h"
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()
16                                   */
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(),
22                      * get_player()
23                      */
24 #include "world.h" /* global world  */
25
26
27
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);
34
35 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
36 static void write_thing(FILE * file, struct Thing * t);
37
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 "".
42 */
43 static char * get_message_from_queue();
44
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'.
47  */
48 static void read_file_into_queue();
49
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.
52  */
53 static void update_worldstate_file();
54
55 /* Write "value" to new \n-delimited line of "file". */
56 static void write_value_as_line(uint32_t value, FILE * file);
57
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);
60
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,
63  * with animate things overwriting inanimate things, and inanimate consumable
64  * things overwriting inanimate non-consumable things.
65  */
66 static char * build_visible_map(struct Thing * player);
67
68 /* Write to "file" game map as visible to "player" right now, as drawn by
69  * build_visible_map(), and thereafter game map as memorized by player in its
70  * .mem_map. Write one row per \n-delimited line.
71  */
72 static void write_map(struct Thing * player, FILE * file);
73
74
75
76 static void write_key_space(FILE * file, char * key)
77 {
78     try_fwrite(key, strlen(key), 1, file, __func__);
79     try_fputc(' ', file, __func__);
80 }
81
82
83
84 static void write_value(FILE * file, uint32_t value)
85 {
86     char * line = try_malloc(11, __func__);
87     exit_trouble(-1 == sprintf(line, "%u", value), __func__, s[S_FCN_SPRINTF]);
88     try_fwrite(line, strlen(line), 1, file, __func__);
89     free(line);
90 }
91
92
93
94 static void write_string(FILE * file, char * string)
95 {
96     uint8_t contains_space = NULL != strchr(string, ' ');
97     if (contains_space)
98     {
99         try_fputc('\'', file, __func__);
100     }
101     try_fwrite(string, strlen(string), 1, file, __func__);
102     if (contains_space)
103     {
104         try_fputc('\'', file, __func__);
105     }
106 }
107
108
109
110 static void write_key_space_value(FILE * file, char * key, uint32_t value)
111 {
112     write_key_space(file, key);
113     write_value(file, value);
114     try_fputc('\n', file, __func__);
115 }
116
117
118
119 static void write_key_space_string(FILE * file, char * key, char * string)
120 {
121     write_key_space(file, key);
122     write_string(file, string);
123     try_fputc('\n', file, __func__);
124 }
125
126
127
128 static void write_thing(FILE * file, struct Thing * t)
129 {
130     struct Thing * o;
131     for (o = t->owns; o; o = o->next)
132     {
133         write_thing(file, o);
134     }
135     write_key_space_value(file, s[S_CMD_T_ID], t->id);
136     write_key_space_value(file, s[S_CMD_T_TYPE], t->type);
137     write_key_space_value(file, s[S_CMD_T_POSY], t->pos.y);
138     write_key_space_value(file, s[S_CMD_T_POSX], t->pos.x);
139     write_key_space_value(file, s[S_CMD_T_COMMAND], t->command);
140     write_key_space_value(file, s[S_CMD_T_ARGUMENT], t->arg);
141     write_key_space_value(file, s[S_CMD_T_PROGRESS], t->progress);
142     write_key_space_value(file, s[S_CMD_T_HP], t->lifepoints);
143     for (o = t->owns; o; o = o->next)
144     {
145         write_key_space_value(file, s[S_CMD_T_CARRIES], o->id);
146     }
147     if (t->mem_map)
148     {
149         uint32_t map_size = world.map.length * world.map.length;/* snprintf() */
150         char * mem_map_copy = try_malloc(map_size + 1, __func__);/* reads one */
151         memcpy(mem_map_copy, t->mem_map, map_size);   /* byte beyond map_size */
152         mem_map_copy[map_size] = '\0';     /* if string is not \0-terminated. */
153         uint16_t y;
154         char string[UINT8_MAX + 1 + 1];
155         for (y = 0; y < world.map.length; y++)
156         {
157
158             int test = snprintf(string, world.map.length + 1, "%s",
159                                 mem_map_copy + (y * world.map.length));
160             exit_trouble(test < 0, __func__, "snprintf()");
161             write_key_space(file, s[S_CMD_T_MEMMAP]);
162             write_value(file, y);
163             try_fputc(' ', file, __func__);
164             write_string(file, string);
165             try_fputc('\n', file, __func__);
166         }
167         free(mem_map_copy);
168     }
169     try_fputc('\n', file, __func__);
170 }
171
172
173
174 static char * get_message_from_queue()
175 {
176     char * message = NULL;
177     if (world.queue_size)
178     {
179         size_t cutout_len = strlen(world.queue);
180         if (0 < cutout_len)
181         {
182             cutout_len++;
183             message = try_malloc(cutout_len, __func__);
184             memcpy(message, world.queue, cutout_len);
185         }
186         for (;
187              cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
188              cutout_len++);
189         world.queue_size = world.queue_size - cutout_len;
190         if (0 == world.queue_size)
191         {
192             free(world.queue);   /* NULL so read_file_into_queue() may free() */
193             world.queue = NULL;  /* this every time, even when it's           */
194         }                        /* un-allocated first. */
195         else
196         {
197             char * new_queue = try_malloc(world.queue_size, __func__);
198             memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
199             free(world.queue);
200             world.queue = new_queue;
201         }
202     }
203     return message;
204 }
205
206
207
208 static void read_file_into_queue()
209 {
210     uint8_t wait_seconds = 5;
211     time_t now = time(0);
212     struct timespec dur;
213     dur.tv_sec = 0;
214     dur.tv_nsec = 33333333;
215     int test;
216     while (EOF == (test = try_fgetc(world.file_in, __func__)))
217     {
218         nanosleep(&dur, NULL);
219         if (time(0) > now + wait_seconds)
220         {
221             return;
222         }
223     }
224     do
225     {
226         char c = (char) test;
227         if ('\n' == c)
228         {
229             c = '\0';
230         }
231         char * new_queue = try_malloc(world.queue_size + 1, __func__);
232         memcpy(new_queue, world.queue, world.queue_size);
233         char * new_pos = new_queue + world.queue_size;
234         * new_pos = c;
235         world.queue_size++;
236         free(world.queue);
237         world.queue = new_queue;
238     }
239     while (EOF != (test = try_fgetc(world.file_in, __func__)));
240 }
241
242
243
244 static void update_worldstate_file()
245 {
246     char * path_tmp;
247     FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
248     struct Thing * player = get_player();
249     write_value_as_line(world.turn, file);
250     write_value_as_line(player->lifepoints, file);
251     write_inventory(player, file);
252     write_value_as_line(player->pos.y, file);
253     write_value_as_line(player->pos.x, file);
254     write_value_as_line(world.map.length, file);
255     write_map(player, file);
256     if (world.log)
257     {
258         try_fwrite(world.log, strlen(world.log), 1, file, __func__);
259     }
260     atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
261     set_cleanup_flag(CLEANUP_WORLDSTATE);
262     char * dot = ".\n";
263     try_fwrite(dot, strlen(dot), 1, world.file_out, __func__);
264     fflush(world.file_out);
265 }
266
267
268
269 static void write_value_as_line(uint32_t value, FILE * file)
270 {
271     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
272     exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]);
273     try_fwrite(write_buf, strlen(write_buf), 1, file, __func__);
274 }
275
276
277
278 static void write_inventory(struct Thing * player, FILE * file)
279 {
280     struct Thing * owned = player->owns;
281     if (NULL == owned)
282     {
283         char * empty = "(none)\n";
284         try_fwrite(empty, strlen(empty), 1, file, __func__);
285     }
286     else
287     {
288         uint8_t q;
289         for (q = 0; NULL != owned; q++)
290         {
291             struct ThingType * tt = get_thing_type(owned->type);
292             try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
293             try_fputc('\n', file, __func__);
294             owned = owned->next;
295         }
296     }
297     try_fputc('%', file, __func__);
298     try_fputc('\n', file, __func__);
299 }
300
301
302
303 static char * build_visible_map(struct Thing * player)
304 {
305     uint32_t map_size = world.map.length * world.map.length;
306     char * visible_map = try_malloc(map_size, __func__);
307     memset(visible_map, ' ', map_size);
308     if (player->fov_map) /* May fail if player thing was created / positioned */
309     {                    /* by god command after turning off FOV building.    */
310         uint32_t pos_i;
311         for (pos_i = 0; pos_i < map_size; pos_i++)
312         {
313             if (player->fov_map[pos_i] == 'v')
314             {
315                 visible_map[pos_i] = world.map.cells[pos_i];
316             }
317         }
318         struct Thing * t;
319         char c;
320         uint8_t i;
321         for (i = 0; i < 3; i++)
322         {
323             for (t = world.things; t != 0; t = t->next)
324             {
325                 if ('v' == player->fov_map[t->pos.y*world.map.length+t->pos.x])
326                 {
327                     struct ThingType * tt = get_thing_type(t->type);
328                     if (   (0 == i && !t->lifepoints && !tt->consumable)
329                         || (1 == i && !t->lifepoints &&  tt->consumable)
330                         || (2 == i &&  t->lifepoints))
331                     {
332                         c = tt->char_on_map;
333                         visible_map[t->pos.y * world.map.length + t->pos.x] = c;
334                     }
335                 }
336             }
337         }
338     }
339     return visible_map;
340 }
341
342
343
344 static void write_map(struct Thing * player, FILE * file)
345 {
346     char * visible_map = build_visible_map(player);
347     uint16_t x, y;
348     for (y = 0; y < world.map.length; y++)
349     {
350         for (x = 0; x < world.map.length; x++)
351         {
352             try_fputc(visible_map[y * world.map.length + x], file, __func__);
353         }
354         try_fputc('\n', file, __func__);
355     }
356     free(visible_map);
357     for (y = 0; y < world.map.length; y++)
358     {
359         for (x = 0; x < world.map.length; x++)
360         {
361             try_fputc(player->mem_map[y*world.map.length+x], file, __func__);
362         }
363         try_fputc('\n', file, __func__);
364     }
365 }
366
367
368
369 extern char * io_round()
370 {
371     if (0 < world.queue_size)
372     {
373         return get_message_from_queue();
374     }
375     if (world.do_update)
376     {
377         update_worldstate_file();
378         world.do_update = 0;
379     }
380     read_file_into_queue();
381     if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
382     {
383         char * new_queue = try_malloc(world.queue_size + 1, __func__);
384         memcpy(new_queue, world.queue, world.queue_size);
385         new_queue[world.queue_size] = '\0';
386         world.queue_size++;
387         free(world.queue);
388         world.queue = new_queue;
389     }
390     return get_message_from_queue();
391 }
392
393
394
395 extern void save_world()
396 {
397     char * path_tmp;
398     FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
399     write_key_space_value(file, s[S_CMD_MAPLENGTH], world.map.length);
400     write_key_space_value(file, s[S_CMD_PLAYTYPE], world.player_type);
401     try_fputc('\n', file, __func__);
402     struct ThingAction * ta;
403     for (ta = world.thing_actions; ta; ta = ta->next)
404     {
405         write_key_space_value(file, s[S_CMD_TA_ID], ta->id);
406         write_key_space_value(file, s[S_CMD_TA_EFFORT], ta->effort);
407         write_key_space_string(file, s[S_CMD_TA_NAME], ta->name);
408         try_fputc('\n', file, __func__);
409     }
410     struct ThingType * tt;
411     for (tt = world.thing_types; tt; tt = tt->next)
412     {
413         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
414         write_key_space_value(file, s[S_CMD_TT_STARTN], tt->start_n);
415         write_key_space_value(file, s[S_CMD_TT_HP], tt->lifepoints);
416         int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map);
417         exit_trouble(test < 0, __func__, "fprintf");
418         write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
419         write_key_space_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
420         try_fputc('\n', file, __func__);
421     }
422     for (tt = world.thing_types; tt; tt = tt->next)
423     {
424         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
425         write_key_space_value(file, s[S_CMD_TT_CORPS], tt->corpse_id);
426     }
427     try_fputc('\n', file, __func__);
428     write_key_space_value(file, s[S_CMD_SEED_MAP], world.seed_map);
429     write_key_space_value(file, s[S_CMD_SEED_RAND], world.seed);
430     write_key_space_value(file, s[S_CMD_TURN], world.turn);
431     try_fputc('\n', file, __func__);
432     struct Thing * t;
433     for (t = world.things; t; t = t->next)
434     {
435         write_thing(file, t);
436     }
437     write_key_space_value(file, s[S_CMD_WORLD_ACTIVE], 1);
438     atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);
439 }