home · contact · privacy
Server: Send "WORLD_UPDATED" message in parallel to worldstate updating.
[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(), memset(), 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 "run.h" /* send_to_outfile() */
28 #include "things.h" /* Thing, ThingType, ThingInMemory, ThingAction,
29                      * get_thing_type(), get_player()
30                      */
31 #include "world.h" /* global world  */
32
33
34
35 /* Helpers to write lines of god commands to recreate thing "t". */
36 static void write_key_space(FILE * file, char * key);
37 static void write_value(FILE * file, uint32_t value);
38 static void write_string(FILE * file, char * string);
39 static void write_key_space_value(FILE * file, char * key, uint32_t value);
40 static void write_key_space_string(FILE * file, char * key, char * string);
41
42 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
43 static void write_thing(FILE * file, struct Thing * t);
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 try_growing_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 and .t_mem. 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         struct ThingInMemory * tm = t->t_mem;
169         for (; tm; tm = tm->next)
170         {
171             write_key_space(file, s[S_CMD_T_MEMTHING]);
172             write_value(file, tm->type);
173             try_fputc(' ', file, __func__);
174             write_value(file, tm->pos.y);
175             try_fputc(' ', file, __func__);
176             write_value(file, tm->pos.x);
177             try_fputc('\n', file, __func__);
178         }
179     }
180     try_fputc('\n', file, __func__);
181 }
182
183
184
185 static void try_growing_queue()
186 {
187     uint8_t wait_seconds = 5;
188     time_t now = time(0);
189     struct timespec dur;
190     dur.tv_sec = 0;
191     dur.tv_nsec = 33333333;
192     while (1)
193     {
194         if (read_file_into_queue(world.file_in, &world.queue))
195         {
196             return;
197         }
198         nanosleep(&dur, NULL);
199         if (time(0) > now + wait_seconds)
200         {
201             return;
202         }
203     }
204 }
205
206
207
208 static void update_worldstate_file()
209 {
210     char * path_tmp;
211     FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
212     struct Thing * player = get_player();
213     write_value_as_line(world.turn, file);
214     write_value_as_line(player->lifepoints, file);
215     write_inventory(player, file);
216     write_value_as_line(player->pos.y, file);
217     write_value_as_line(player->pos.x, file);
218     write_value_as_line(world.map.length, file);
219     write_map(player, file);
220     atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
221     set_cleanup_flag(CLEANUP_WORLDSTATE);
222     char * dot = ".\n";
223     try_fwrite(dot, strlen(dot), 1, world.file_out, __func__);
224     fflush(world.file_out);
225 }
226
227
228
229 static void write_value_as_line(uint32_t value, FILE * file)
230 {
231     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
232     exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]);
233     try_fwrite(write_buf, strlen(write_buf), 1, file, __func__);
234 }
235
236
237
238 static void write_inventory(struct Thing * player, FILE * file)
239 {
240     struct Thing * owned = player->owns;
241     if (!owned)
242     {
243         char * empty = "(none)\n";
244         try_fwrite(empty, strlen(empty), 1, file, __func__);
245     }
246     else
247     {
248         uint8_t q;
249         for (q = 0; owned; q++)
250         {
251             struct ThingType * tt = get_thing_type(owned->type);
252             try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
253             try_fputc('\n', file, __func__);
254             owned = owned->next;
255         }
256     }
257     try_fputc('%', file, __func__);
258     try_fputc('\n', file, __func__);
259 }
260
261
262
263 static char * build_visible_map(struct Thing * player)
264 {
265     uint32_t map_size = world.map.length * world.map.length;
266     char * visible_map = try_malloc(map_size, __func__);
267     memset(visible_map, ' ', map_size);
268     if (player->fov_map) /* May fail if player thing was created / positioned */
269     {                    /* by god command after turning off FOV building.    */
270         uint32_t pos_i;
271         for (pos_i = 0; pos_i < map_size; pos_i++)
272         {
273             if (player->fov_map[pos_i] == 'v')
274             {
275                 visible_map[pos_i] = world.map.cells[pos_i];
276             }
277         }
278         struct Thing * t;
279         char c;
280         uint8_t i;
281         for (i = 0; i < 3; i++)
282         {
283             for (t = world.things; t != 0; t = t->next)
284             {
285                 if ('v' == player->fov_map[t->pos.y*world.map.length+t->pos.x])
286                 {
287                     struct ThingType * tt = get_thing_type(t->type);
288                     if (   (0 == i && !t->lifepoints && !tt->consumable)
289                         || (1 == i && !t->lifepoints &&  tt->consumable)
290                         || (2 == i &&  t->lifepoints))
291                     {
292                         c = tt->char_on_map;
293                         visible_map[t->pos.y * world.map.length + t->pos.x] = c;
294                     }
295                 }
296             }
297         }
298     }
299     return visible_map;
300 }
301
302
303
304 static void write_map(struct Thing * player, FILE * file)
305 {
306     char * visible_map = build_visible_map(player);
307     uint16_t x, y;
308     for (y = 0; y < world.map.length; y++)
309     {
310         for (x = 0; x < world.map.length; x++)
311         {
312             try_fputc(visible_map[y * world.map.length + x], file, __func__);
313         }
314         try_fputc('\n', file, __func__);
315     }
316     free(visible_map);
317     uint32_t map_size = world.map.length * world.map.length;
318     char * mem_map = try_malloc(map_size, __func__);
319     memcpy(mem_map, player->mem_map, map_size);
320     uint8_t i;
321     struct ThingInMemory * tm;
322     for (i = 0; i < 2; i++)
323     {
324         for (tm = player->t_mem; tm; tm = tm->next)
325         {
326             if (' ' != player->mem_map[tm->pos.y*world.map.length+tm->pos.x])
327             {
328                 struct ThingType * tt = get_thing_type(tm->type);
329                 if (   (0 == i && !tt->consumable)
330                     || (1 == i &&  tt->consumable))
331                 {
332                     char c = tt->char_on_map;
333                     mem_map[tm->pos.y * world.map.length + tm->pos.x] = c;
334                 }
335             }
336         }
337     }
338     for (y = 0; y < world.map.length; y++)
339     {
340         for (x = 0; x < world.map.length; x++)
341         {
342             try_fputc(mem_map[y * world.map.length + x], file, __func__);
343         }
344         try_fputc('\n', file, __func__);
345     }
346     free(mem_map);
347 }
348
349
350
351 extern char * io_round()
352 {
353     if (world.queue && strlen(world.queue))
354     {
355         return get_message_from_queue(&world.queue);
356     }
357     if (world.do_update)
358     {
359         update_worldstate_file();
360         send_to_outfile("WORLD_UDPATED\n", 1);
361         world.do_update = 0;
362     }
363     try_growing_queue();
364     return get_message_from_queue(&world.queue);
365 }
366
367
368
369 extern void save_world()
370 {
371     char * path_tmp;
372     FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
373     write_key_space_value(file, s[S_CMD_MAPLENGTH], world.map.length);
374     write_key_space_value(file, s[S_CMD_PLAYTYPE], world.player_type);
375     try_fputc('\n', file, __func__);
376     struct ThingAction * ta;
377     for (ta = world.thing_actions; ta; ta = ta->next)
378     {
379         write_key_space_value(file, s[S_CMD_TA_ID], ta->id);
380         write_key_space_value(file, s[S_CMD_TA_EFFORT], ta->effort);
381         write_key_space_string(file, s[S_CMD_TA_NAME], ta->name);
382         try_fputc('\n', file, __func__);
383     }
384     struct ThingType * tt;
385     for (tt = world.thing_types; tt; tt = tt->next)
386     {
387         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
388         write_key_space_value(file, s[S_CMD_TT_STARTN], tt->start_n);
389         write_key_space_value(file, s[S_CMD_TT_HP], tt->lifepoints);
390         int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map);
391         exit_trouble(test < 0, __func__, "fprintf");
392         write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
393         write_key_space_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
394         write_key_space_value(file, s[S_CMD_TT_PROL], tt->proliferate);
395         try_fputc('\n', file, __func__);
396     }
397     for (tt = world.thing_types; tt; tt = tt->next)
398     {
399         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
400         write_key_space_value(file, s[S_CMD_TT_CORPS], tt->corpse_id);
401     }
402     try_fputc('\n', file, __func__);
403     write_key_space_value(file, s[S_CMD_SEED_MAP], world.seed_map);
404     write_key_space_value(file, s[S_CMD_SEED_RAND], world.seed);
405     write_key_space_value(file, s[S_CMD_TURN], world.turn);
406     try_fputc('\n', file, __func__);
407     struct Thing * t;
408     for (t = world.things; t; t = t->next)
409     {
410         write_thing(file, t);
411     }
412     write_key_space_value(file, s[S_CMD_WORLD_ACTIVE], 1);
413     atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);
414 }