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