home · contact · privacy
3c75bd8842d15801fa3baff43186b46a477ed59e
[plomrogue] / src / server / io.c
1 /* src/server/io.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #define _POSIX_C_SOURCE 200112L /* snrpintf() */
9 #include "io.h"
10 #include <errno.h> /* global errno */
11 #include <limits.h> /* PIPE_BUF */
12 #include <stddef.h> /* NULL */
13 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT8_MAX */
14 #include <stdio.h> /* defines FILE, sprintf(), fprintf() */
15 #include <stdlib.h> /* free() */
16 #include <string.h> /* strlen(), snprintf(), memcpy(), strchr() */
17 #include <sys/types.h> /* time_t */
18 #include <time.h> /* time(), nanosleep() */
19 #include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
20                                   * get_message_from_queue(), try_fwrite(),
21                                   * read_file_into_queue(), try_fputc()
22                                   */
23 #include "../common/rexit.h" /* exit_trouble() */
24 #include "../common/try_malloc.h" /* try_malloc() */
25 #include "cleanup.h" /* set_cleanup_flag() */
26 #include "hardcoded_strings.h" /* s */
27 #include "map.h" /* init_empty_map() */
28 #include "run.h" /* send_to_outfile() */
29 #include "things.h" /* Thing, ThingType, ThingInMemory, ThingAction,
30                      * get_thing_type(), get_player()
31                      */
32 #include "world.h" /* global world  */
33
34
35
36 /* Helpers to write lines of god commands to recreate thing "t". */
37 static void write_key_space(FILE * file, char * key);
38 static void write_value(FILE * file, uint32_t value);
39 static void write_string(FILE * file, char * string);
40 static void write_key_space_value(FILE * file, char * key, uint32_t value);
41 static void write_key_space_string(FILE * file, char * key, char * string);
42
43 /* Write to "file" game-map-sized "map" in "command"-prefixed numbered lines. */
44 static void write_mem_map(FILE * file, char * map, char * command);
45
46 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
47 static void write_thing(FILE * file, struct Thing * t);
48
49 /* Poll input file for world.queue input. Wait a few seconds until giving up;
50  * poll only every 0.03 seconds. Translate '\n' chars in input file into '\0'.
51  */
52 static void try_growing_queue();
53
54 /* Write world state as visible to clients to its file. Write single dot line to
55  * server output file to satisfy client ping mechanisms.
56  */
57 static void update_worldstate_file();
58
59 /* Write "value" to new \n-delimited line of "file". */
60 static void write_value_as_line(uint32_t value, FILE * file);
61
62 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
63 static void write_inventory(struct Thing * player, FILE * file);
64
65 /* Return map cells sequence as visible to the "player", with invisible cells as
66  * whitespace. Super-impose over visible map cells things positioned there,
67  * with animate things overwriting inanimate things, and inanimate consumable
68  * things overwriting inanimate non-consumable things.
69  */
70 static char * build_visible_map(struct Thing * player);
71
72 /* Write to "file" game map as visible to "player" right now, as drawn by
73  * build_visible_map(), and thereafter game map as memorized by player in its
74  * .mem_map and .t_mem. Write one row per \n-delimited line.
75  */
76 static void write_map(struct Thing * player, FILE * file);
77
78
79
80 static void write_key_space(FILE * file, char * key)
81 {
82     try_fwrite(key, strlen(key), 1, file, __func__);
83     try_fputc(' ', file, __func__);
84 }
85
86
87
88 static void write_value(FILE * file, uint32_t value)
89 {
90     char * line = try_malloc(11, __func__);
91     exit_trouble(-1 == sprintf(line, "%u", value), __func__, s[S_FCN_SPRINTF]);
92     try_fwrite(line, strlen(line), 1, file, __func__);
93     free(line);
94 }
95
96
97
98 static void write_string(FILE * file, char * string)
99 {
100     uint8_t contains_space = NULL != strchr(string, ' ');
101     if (contains_space)
102     {
103         try_fputc('\'', file, __func__);
104     }
105     try_fwrite(string, strlen(string), 1, file, __func__);
106     if (contains_space)
107     {
108         try_fputc('\'', file, __func__);
109     }
110 }
111
112
113
114 static void write_key_space_value(FILE * file, char * key, uint32_t value)
115 {
116     write_key_space(file, key);
117     write_value(file, value);
118     try_fputc('\n', file, __func__);
119 }
120
121
122
123 static void write_key_space_string(FILE * file, char * key, char * string)
124 {
125     write_key_space(file, key);
126     write_string(file, string);
127     try_fputc('\n', file, __func__);
128 }
129
130
131
132 static void write_mem_map(FILE * file, char * map, char * command)
133 {
134     if (map)
135     {
136         uint32_t map_size = world.map.length * world.map.length;/* snprintf() */
137         char * map_copy = try_malloc(map_size + 1, __func__);    /* reads one */
138         memcpy(map_copy, map, map_size);              /* byte beyond map_size */
139         map_copy[map_size] = '\0';         /* if string is not \0-terminated. */
140         uint16_t y;
141         char string[UINT8_MAX + 1 + 1];
142         for (y = 0; y < world.map.length; y++)
143         {
144             int test = snprintf(string, world.map.length + 1, "%s",
145                                 map_copy + (y * world.map.length));
146             exit_trouble(test < 0, __func__, "snprintf()");
147             write_key_space(file, command);
148             write_value(file, y);
149             try_fputc(' ', file, __func__);
150             write_string(file, string);
151             try_fputc('\n', file, __func__);
152         }
153         free(map_copy);
154     }
155 }
156
157
158
159 static void write_thing(FILE * file, struct Thing * t)
160 {
161     struct Thing * o;
162     for (o = t->owns; o; o = o->next)
163     {
164         write_thing(file, o);
165     }
166     write_key_space_value(file, s[S_CMD_T_ID], t->id);
167     write_key_space_value(file, s[S_CMD_T_TYPE], t->type);
168     write_key_space_value(file, s[S_CMD_T_POSY], t->pos.y);
169     write_key_space_value(file, s[S_CMD_T_POSX], t->pos.x);
170     write_key_space_value(file, s[S_CMD_T_COMMAND], t->command);
171     write_key_space_value(file, s[S_CMD_T_ARGUMENT], t->arg);
172     write_key_space_value(file, s[S_CMD_T_PROGRESS], t->progress);
173     write_key_space_value(file, s[S_CMD_T_HP], t->lifepoints);
174     for (o = t->owns; o; o = o->next)
175     {
176         write_key_space_value(file, s[S_CMD_T_CARRIES], o->id);
177     }
178     write_mem_map(file, t->mem_depth_map, s[S_CMD_T_MEMDEPTHMAP]);
179     write_mem_map(file, t->mem_map, s[S_CMD_T_MEMMAP]);
180     struct ThingInMemory * tm = t->t_mem;
181     for (; tm; tm = tm->next)
182     {
183         write_key_space(file, s[S_CMD_T_MEMTHING]);
184         write_value(file, tm->type);
185         try_fputc(' ', file, __func__);
186         write_value(file, tm->pos.y);
187         try_fputc(' ', file, __func__);
188         write_value(file, tm->pos.x);
189         try_fputc('\n', file, __func__);
190     }
191     try_fputc('\n', file, __func__);
192 }
193
194
195
196 static void try_growing_queue()
197 {
198     uint8_t wait_seconds = 5;
199     time_t now = time(0);
200     struct timespec dur;
201     dur.tv_sec = 0;
202     dur.tv_nsec = 33333333;
203     while (1)
204     {
205         if (read_file_into_queue(world.file_in, &world.queue))
206         {
207             return;
208         }
209         nanosleep(&dur, NULL);
210         if (time(0) > now + wait_seconds)
211         {
212             return;
213         }
214     }
215 }
216
217
218
219 static void update_worldstate_file()
220 {
221     char * path_tmp;
222     FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
223     struct Thing * player = get_player();
224     write_value_as_line(world.turn, file);
225     write_value_as_line(player->lifepoints, file);
226     write_inventory(player, file);
227     write_value_as_line(player->pos.y, file);
228     write_value_as_line(player->pos.x, file);
229     write_value_as_line(world.map.length, file);
230     write_map(player, file);
231     atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
232     set_cleanup_flag(CLEANUP_WORLDSTATE);
233     char * dot = ".\n";
234     try_fwrite(dot, strlen(dot), 1, world.file_out, __func__);
235     fflush(world.file_out);
236 }
237
238
239
240 static void write_value_as_line(uint32_t value, FILE * file)
241 {
242     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
243     exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]);
244     try_fwrite(write_buf, strlen(write_buf), 1, file, __func__);
245 }
246
247
248
249 static void write_inventory(struct Thing * player, FILE * file)
250 {
251     struct Thing * owned = player->owns;
252     if (!owned)
253     {
254         char * empty = "(none)\n";
255         try_fwrite(empty, strlen(empty), 1, file, __func__);
256     }
257     else
258     {
259         uint8_t q;
260         for (q = 0; owned; q++)
261         {
262             struct ThingType * tt = get_thing_type(owned->type);
263             try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
264             try_fputc('\n', file, __func__);
265             owned = owned->next;
266         }
267     }
268     try_fputc('%', file, __func__);
269     try_fputc('\n', file, __func__);
270 }
271
272
273
274 static char * build_visible_map(struct Thing * player)
275 {
276     char * visible_map;
277     init_empty_map(&visible_map);
278     if (player->fov_map) /* May fail if player thing was created / positioned */
279     {                    /* by god command after turning off FOV building.    */
280         uint32_t pos_i = 0;
281         for (; pos_i < (uint32_t) world.map.length * world.map.length; pos_i++)
282         {
283             if (player->fov_map[pos_i] == 'v')
284             {
285                 visible_map[pos_i] = world.map.cells[pos_i];
286             }
287         }
288         struct Thing * t;
289         char c;
290         uint8_t i;
291         for (i = 0; i < 3; i++)
292         {
293             for (t = world.things; t != 0; t = t->next)
294             {
295                 if ('v' == player->fov_map[t->pos.y*world.map.length+t->pos.x])
296                 {
297                     struct ThingType * tt = get_thing_type(t->type);
298                     if (   (0 == i && !t->lifepoints && !tt->consumable)
299                         || (1 == i && !t->lifepoints &&  tt->consumable)
300                         || (2 == i &&  t->lifepoints))
301                     {
302                         c = tt->char_on_map;
303                         visible_map[t->pos.y * world.map.length + t->pos.x] = c;
304                     }
305                 }
306             }
307         }
308     }
309     return visible_map;
310 }
311
312
313
314 static void write_map(struct Thing * player, FILE * file)
315 {
316     char * visible_map = build_visible_map(player);
317     uint16_t x, y;
318     for (y = 0; y < world.map.length; y++)
319     {
320         for (x = 0; x < world.map.length; x++)
321         {
322             try_fputc(visible_map[y * world.map.length + x], file, __func__);
323         }
324         try_fputc('\n', file, __func__);
325     }
326     free(visible_map);
327     uint32_t map_size = world.map.length * world.map.length;
328     char * mem_map = try_malloc(map_size, __func__);
329     memcpy(mem_map, player->mem_map, map_size);
330     uint8_t i;
331     struct ThingInMemory * tm;
332     for (i = 0; i < 2; i++)
333     {
334         for (tm = player->t_mem; tm; tm = tm->next)
335         {
336             if (' ' != player->mem_map[tm->pos.y*world.map.length+tm->pos.x])
337             {
338                 struct ThingType * tt = get_thing_type(tm->type);
339                 if (   (0 == i && !tt->consumable)
340                     || (1 == i &&  tt->consumable))
341                 {
342                     char c = tt->char_on_map;
343                     mem_map[tm->pos.y * world.map.length + tm->pos.x] = c;
344                 }
345             }
346         }
347     }
348     for (y = 0; y < world.map.length; y++)
349     {
350         for (x = 0; x < world.map.length; x++)
351         {
352             try_fputc(mem_map[y * world.map.length + x], file, __func__);
353         }
354         try_fputc('\n', file, __func__);
355     }
356     free(mem_map);
357 }
358
359
360
361 extern char * io_round()
362 {
363     if (world.queue && strlen(world.queue))
364     {
365         return get_message_from_queue(&world.queue);
366     }
367     if (world.do_update)
368     {
369         update_worldstate_file();
370         send_to_outfile("WORLD_UPDATED\n", 1);
371         world.do_update = 0;
372     }
373     try_growing_queue();
374     return get_message_from_queue(&world.queue);
375 }
376
377
378
379 extern void save_world()
380 {
381     char * path_tmp;
382     FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
383     write_key_space_value(file, s[S_CMD_MAPLENGTH], world.map.length);
384     write_key_space_value(file, s[S_CMD_PLAYTYPE], world.player_type);
385     try_fputc('\n', file, __func__);
386     struct ThingAction * ta;
387     for (ta = world.thing_actions; ta; ta = ta->next)
388     {
389         write_key_space_value(file, s[S_CMD_TA_ID], ta->id);
390         write_key_space_value(file, s[S_CMD_TA_EFFORT], ta->effort);
391         write_key_space_string(file, s[S_CMD_TA_NAME], ta->name);
392         try_fputc('\n', file, __func__);
393     }
394     struct ThingType * tt;
395     for (tt = world.thing_types; tt; tt = tt->next)
396     {
397         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
398         write_key_space_value(file, s[S_CMD_TT_STARTN], tt->start_n);
399         write_key_space_value(file, s[S_CMD_TT_HP], tt->lifepoints);
400         int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map);
401         exit_trouble(test < 0, __func__, "fprintf");
402         write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
403         write_key_space_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
404         write_key_space_value(file, s[S_CMD_TT_PROL], tt->proliferate);
405         try_fputc('\n', file, __func__);
406     }
407     for (tt = world.thing_types; tt; tt = tt->next)
408     {
409         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
410         write_key_space_value(file, s[S_CMD_TT_CORPS], tt->corpse_id);
411     }
412     try_fputc('\n', file, __func__);
413     write_key_space_value(file, s[S_CMD_SEED_MAP], world.seed_map);
414     write_key_space_value(file, s[S_CMD_SEED_RAND], world.seed);
415     write_key_space_value(file, s[S_CMD_TURN], world.turn);
416     try_fputc('\n', file, __func__);
417     struct Thing * t;
418     for (t = world.things; t; t = t->next)
419     {
420         write_thing(file, t);
421     }
422     write_key_space_value(file, s[S_CMD_WORLD_ACTIVE], 1);
423     atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);
424 }