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