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