home · contact · privacy
Add auto-mapping / map memory.
[plomrogue] / src / server / io.c
1 /* src/server/io.c */
2
3 #define _POSIX_C_SOURCE 199309L
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 "field_of_view.h" /* VISIBLE */
21 #include "hardcoded_strings.h" /* s */
22 #include "things.h" /* Thing, ThingType, ThingAction, get_thing_type(),
23                      * get_player()
24                      */
25 #include "world.h" /* global world  */
26
27
28
29 /* Helpers to write lines of god commands to recreate thing "t". */
30 static void write_key_space(FILE * file, char * key);
31 static void write_value(FILE * file, uint32_t value);
32 static void write_string(FILE * file, char * string);
33 static void write_key_space_value(FILE * file, char * key, uint32_t value);
34 static void write_key_space_string(FILE * file, char * key, char * string);
35
36 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
37 static void write_thing(FILE * file, struct Thing * t);
38
39 /* Cut out and return first \0-terminated string from world.queue and
40  * appropriately reduce world.queue_size. Return NULL if queue is empty.
41  * Superfluous \0 bytes after the string are also cut out. Should the queue
42  * start with \0 bytes, those are cut out, but NULL is returned instead of "".
43 */
44 static char * get_message_from_queue();
45
46 /* Poll input file for world.queue input. Wait a few seconds until giving up;
47  * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
48  */
49 static void read_file_into_queue();
50
51 /* Write world state as visible to clients to its file. Write single dot line to
52  * server output file to satisfy client ping mechanisms.
53  */
54 static void update_worldstate_file();
55
56 /* Write "value" to new \n-delimited line of "file". */
57 static void write_value_as_line(uint32_t value, FILE * file);
58
59 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
60 static void write_inventory(struct Thing * player, FILE * file);
61
62 /* Return map cells sequence as visible to the "player", with invisible cells as
63  * whitespace. Super-impose over visible map cells things positioned there.
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. 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     }
168     try_fputc('\n', file, __func__);
169 }
170
171
172
173 static char * get_message_from_queue()
174 {
175     char * message = NULL;
176     if (world.queue_size)
177     {
178         size_t cutout_len = strlen(world.queue);
179         if (0 < cutout_len)
180         {
181             cutout_len++;
182             message = try_malloc(cutout_len, __func__);
183             memcpy(message, world.queue, cutout_len);
184         }
185         for (;
186              cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
187              cutout_len++);
188         world.queue_size = world.queue_size - cutout_len;
189         if (0 == world.queue_size)
190         {
191             free(world.queue);   /* NULL so read_file_into_queue() may free() */
192             world.queue = NULL;  /* this every time, even when it's           */
193         }                        /* un-allocated first. */
194         else
195         {
196             char * new_queue = try_malloc(world.queue_size, __func__);
197             memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
198             free(world.queue);
199             world.queue = new_queue;
200         }
201     }
202     return message;
203 }
204
205
206
207 static void read_file_into_queue()
208 {
209     uint8_t wait_seconds = 5;
210     time_t now = time(0);
211     struct timespec dur;
212     dur.tv_sec = 0;
213     dur.tv_nsec = 33333333;
214     int test;
215     while (EOF == (test = try_fgetc(world.file_in, __func__)))
216     {
217         nanosleep(&dur, NULL);
218         if (time(0) > now + wait_seconds)
219         {
220             return;
221         }
222     }
223     do
224     {
225         char c = (char) test;
226         if ('\n' == c)
227         {
228             c = '\0';
229         }
230         char * new_queue = try_malloc(world.queue_size + 1, __func__);
231         memcpy(new_queue, world.queue, world.queue_size);
232         char * new_pos = new_queue + world.queue_size;
233         * new_pos = c;
234         world.queue_size++;
235         free(world.queue);
236         world.queue = new_queue;
237     }
238     while (EOF != (test = try_fgetc(world.file_in, __func__)));
239 }
240
241
242
243 static void update_worldstate_file()
244 {
245     char * path_tmp;
246     FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
247     struct Thing * player = get_player();
248     write_value_as_line(world.turn, file);
249     write_value_as_line(player->lifepoints, file);
250     write_inventory(player, file);
251     write_value_as_line(player->pos.y, file);
252     write_value_as_line(player->pos.x, file);
253     write_value_as_line(world.map.length, file);
254     write_map(player, file);
255     if (world.log)
256     {
257         try_fwrite(world.log, strlen(world.log), 1, file, __func__);
258     }
259     atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
260     set_cleanup_flag(CLEANUP_WORLDSTATE);
261     char * dot = ".\n";
262     try_fwrite(dot, strlen(dot), 1, world.file_out, __func__);
263     fflush(world.file_out);
264 }
265
266
267
268 static void write_value_as_line(uint32_t value, FILE * file)
269 {
270     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
271     exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]);
272     try_fwrite(write_buf, strlen(write_buf), 1, file, __func__);
273 }
274
275
276
277 static void write_inventory(struct Thing * player, FILE * file)
278 {
279     struct Thing * owned = player->owns;
280     if (NULL == owned)
281     {
282         char * empty = "(none)\n";
283         try_fwrite(empty, strlen(empty), 1, file, __func__);
284     }
285     else
286     {
287         uint8_t q;
288         for (q = 0; NULL != owned; q++)
289         {
290             struct ThingType * tt = get_thing_type(owned->type);
291             try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
292             try_fputc('\n', file, __func__);
293             owned = owned->next;
294         }
295     }
296     try_fputc('%', file, __func__);
297     try_fputc('\n', file, __func__);
298 }
299
300
301
302 static char * build_visible_map(struct Thing * player)
303 {
304     uint32_t map_size = world.map.length * world.map.length;
305     char * visible_map = try_malloc(map_size, __func__);
306     memset(visible_map, ' ', map_size);
307     if (player->fov_map) /* May fail if player thing was created / positioned */
308     {                    /* by god command after turning off FOV building.    */
309         uint32_t pos_i;
310         for (pos_i = 0; pos_i < map_size; pos_i++)
311         {
312             if (player->fov_map[pos_i] & VISIBLE)
313             {
314                 visible_map[pos_i] = world.map.cells[pos_i];
315             }
316         }
317         struct Thing * t;
318         struct ThingType * tt;
319         char c;
320         uint8_t i;
321         for (i = 0; i < 2; i++)
322         {
323             for (t = world.things; t != 0; t = t->next)
324             {
325                 if (   (  player->fov_map[t->pos.y * world.map.length +t->pos.x]
326                         & VISIBLE)
327                     && (   (0 == i && 0 == t->lifepoints)
328                         || (1 == i && 0 < t->lifepoints)))
329                 {
330                     tt = get_thing_type(t->type);
331                     c = tt->char_on_map;
332                     visible_map[t->pos.y * world.map.length + t->pos.x] = c;
333                 }
334             }
335         }
336     }
337     return visible_map;
338 }
339
340
341
342 static void write_map(struct Thing * player, FILE * file)
343 {
344     char * visible_map = build_visible_map(player);
345     uint16_t x, y;
346     for (y = 0; y < world.map.length; y++)
347     {
348         for (x = 0; x < world.map.length; x++)
349         {
350             try_fputc(visible_map[y * world.map.length + x], file, __func__);
351         }
352         try_fputc('\n', file, __func__);
353     }
354     free(visible_map);
355     for (y = 0; y < world.map.length; y++)
356     {
357         for (x = 0; x < world.map.length; x++)
358         {
359             try_fputc(player->mem_map[y * world.map.length + x], file, __func__);
360         }
361         try_fputc('\n', file, __func__);
362     }
363 }
364
365
366
367 extern char * io_round()
368 {
369     if (0 < world.queue_size)
370     {
371         return get_message_from_queue();
372     }
373     if (world.do_update)
374     {
375         update_worldstate_file();
376         world.do_update = 0;
377     }
378     read_file_into_queue();
379     if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
380     {
381         char * new_queue = try_malloc(world.queue_size + 1, __func__);
382         memcpy(new_queue, world.queue, world.queue_size);
383         new_queue[world.queue_size] = '\0';
384         world.queue_size++;
385         free(world.queue);
386         world.queue = new_queue;
387     }
388     return get_message_from_queue();
389 }
390
391
392
393 extern void save_world()
394 {
395     char * path_tmp;
396     FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
397     write_key_space_value(file, s[S_CMD_MAPLENGTH], world.map.length);
398     write_key_space_value(file, s[S_CMD_PLAYTYPE], world.player_type);
399     try_fputc('\n', file, __func__);
400     struct ThingAction * ta;
401     for (ta = world.thing_actions; ta; ta = ta->next)
402     {
403         write_key_space_value(file, s[S_CMD_TA_ID], ta->id);
404         write_key_space_value(file, s[S_CMD_TA_EFFORT], ta->effort);
405         write_key_space_string(file, s[S_CMD_TA_NAME], ta->name);
406         try_fputc('\n', file, __func__);
407     }
408     struct ThingType * tt;
409     for (tt = world.thing_types; tt; tt = tt->next)
410     {
411         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
412         write_key_space_value(file, s[S_CMD_TT_STARTN], tt->start_n);
413         write_key_space_value(file, s[S_CMD_TT_HP], tt->lifepoints);
414         int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map);
415         exit_trouble(test < 0, __func__, "fprintf");
416         write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
417         write_key_space_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
418         try_fputc('\n', file, __func__);
419     }
420     for (tt = world.thing_types; tt; tt = tt->next)
421     {
422         write_key_space_value(file, s[S_CMD_TT_ID], tt->id);
423         write_key_space_value(file, s[S_CMD_TT_CORPS], tt->corpse_id);
424     }
425     try_fputc('\n', file, __func__);
426     write_key_space_value(file, s[S_CMD_SEED_MAP], world.seed_map);
427     write_key_space_value(file, s[S_CMD_SEED_RAND], world.seed);
428     write_key_space_value(file, s[S_CMD_TURN], world.turn);
429     try_fputc('\n', file, __func__);
430     struct Thing * t;
431     for (t = world.things; t; t = t->next)
432     {
433         write_thing(file, t);
434     }
435     write_key_space_value(file, s[S_CMD_WORLD_ACTIVE], 1);
436     atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);
437 }