home · contact · privacy
b2d8005a7a75b22788081e9cf9639ffeed8ceba1
[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() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), memcpy(), memset() */
12 #include <sys/types.h> /* time_t */
13 #include <time.h> /* time(), nanosleep() */
14 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
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, get_thing_type(), get_player() */
24 #include "world.h" /* global world  */
25
26
27
28 /* Write to "file" god commands (one per line) to recreate thing "t". */
29 static void write_key_value(FILE * file, char * key, uint32_t value);
30
31 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
32 static void write_thing(FILE * file, struct Thing * t);
33
34 /* Cut out and return first \0-terminated string from world.queue and
35  * appropriately reduce world.queue_size. Return NULL if queue is empty.
36  * Superfluous \0 bytes after the string are also cut out. Should the queue
37  * start with \0 bytes, those are cut out, but NULL is returned instead of "".
38 */
39 static char * get_message_from_queue();
40
41 /* Poll input file for world.queue input. Wait a few seconds until giving up;
42  * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
43  */
44 static void read_file_into_queue();
45
46 /* Write world state as visible to clients to its file. Write single dot line to
47  * server output file to satisfy client ping mechanisms.
48  */
49 static void update_worldstate_file();
50
51 /* Write "value" to new \n-delimited line of "file". */
52 static void write_value_as_line(uint32_t value, FILE * file);
53
54 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
55 static void write_inventory(struct Thing * player, FILE * file);
56
57 /* Return map cells sequence as visible to the "player", with invisible cells as
58  * whitespace. Super-impose over visible map cells things positioned there.
59  */
60 static char * build_visible_map(struct Thing * player);
61
62 /* Write to "file" game map as visible to "player", build_visible_map()-drawn.
63  * Write one row per \n-delimited line.
64  */
65 static void write_map(struct Thing * player, FILE * file);
66
67
68
69 static void write_key_value(FILE * file, char * key, uint32_t value)
70 {
71     char * f_name = "write_key_value()";
72     try_fwrite(key, strlen(key), 1, file, f_name);
73     try_fputc(' ', file, f_name);
74     char * line = try_malloc(11, f_name);
75     exit_trouble(-1 == sprintf(line, "%u", value), f_name, "sprintf()");
76     try_fwrite(line, strlen(line), 1, file, f_name);
77     free(line);
78     try_fputc('\n', file, f_name);
79 }
80
81
82
83 static void write_thing(FILE * file, struct Thing * t)
84 {
85     char * f_name = "write_thing()";
86     struct Thing * o;
87     for (o = t->owns; o; o = o->next)
88     {
89         write_thing(file, o);
90     }
91     write_key_value(file, s[CMD_THING], t->id);
92     write_key_value(file, s[CMD_TYPE], t->type);
93     write_key_value(file, s[CMD_POS_Y], t->pos.y);
94     write_key_value(file, s[CMD_POS_X], t->pos.x);
95     write_key_value(file, s[CMD_COMMAND], t->command);
96     write_key_value(file, s[CMD_ARGUMENT], t->arg);
97     write_key_value(file, s[CMD_PROGRESS], t->progress);
98     write_key_value(file, s[CMD_LIFEPOINTS], t->lifepoints);
99     for (o = t->owns; o; o = o->next)
100     {
101         write_key_value(file, s[CMD_CARRIES], o->id);
102     }
103     try_fputc('\n', file, f_name);
104 }
105
106
107
108 static char * get_message_from_queue()
109 {
110     char * f_name = "get_message_from_queue()";
111     char * message = NULL;
112     if (world.queue_size)
113     {
114         size_t cutout_len = strlen(world.queue);
115         if (0 < cutout_len)
116         {
117             cutout_len++;
118             message = try_malloc(cutout_len, f_name);
119             memcpy(message, world.queue, cutout_len);
120         }
121         for (;
122              cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
123              cutout_len++);
124         world.queue_size = world.queue_size - cutout_len;
125         if (0 == world.queue_size)
126         {
127             free(world.queue);   /* NULL so read_file_into_queue() may free() */
128             world.queue = NULL;  /* this every time, even when it's           */
129         }                        /* un-allocated first. */
130         else
131         {
132             char * new_queue = try_malloc(world.queue_size, f_name);
133             memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
134             free(world.queue);
135             world.queue = new_queue;
136         }
137     }
138     return message;
139 }
140
141
142
143 static void read_file_into_queue()
144 {
145     char * f_name = "read_file_into_queue()";
146     uint8_t wait_seconds = 5;
147     time_t now = time(0);
148     struct timespec dur;
149     dur.tv_sec = 0;
150     dur.tv_nsec = 33333333;
151     int test;
152     while (EOF == (test = try_fgetc(world.file_in, f_name)))
153     {
154         nanosleep(&dur, NULL);
155         if (time(0) > now + wait_seconds)
156         {
157             return;
158         }
159     }
160     do
161     {
162         char c = (char) test;
163         if ('\n' == c)
164         {
165             c = '\0';
166         }
167         char * new_queue = try_malloc(world.queue_size + 1, f_name);
168         memcpy(new_queue, world.queue, world.queue_size);
169         char * new_pos = new_queue + world.queue_size;
170         * new_pos = c;
171         world.queue_size++;
172         free(world.queue);
173         world.queue = new_queue;
174     }
175     while (EOF != (test = try_fgetc(world.file_in, f_name)));
176 }
177
178
179
180 static void update_worldstate_file()
181 {
182     char * f_name = "update_worldstate_file()";
183     char path_tmp[strlen(s[PATH_WORLDSTATE]) + strlen(s[PATH_SUFFIX_TMP]) + 1];
184     sprintf(path_tmp, "%s%s", s[PATH_WORLDSTATE], s[PATH_SUFFIX_TMP]);
185     FILE * file = try_fopen(path_tmp, "w", f_name);
186     struct Thing * player = get_player();
187     write_value_as_line(world.turn, file);
188     write_value_as_line(player->lifepoints, file);
189     write_inventory(player, file);
190     write_value_as_line(player->pos.y, file);
191     write_value_as_line(player->pos.x, file);
192     write_value_as_line(world.map.length, file);
193     write_map(player, file);
194     if (world.log)
195     {
196         try_fwrite(world.log, strlen(world.log), 1, file, f_name);
197     }
198     try_fclose_unlink_rename(file, path_tmp, s[PATH_WORLDSTATE], f_name);
199     set_cleanup_flag(CLEANUP_WORLDSTATE);
200     char * dot = ".\n";;
201     try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
202     fflush(world.file_out);
203 }
204
205
206
207 static void write_value_as_line(uint32_t value, FILE * file)
208 {
209     char * f_name = "write_value_as_line()";
210     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
211     sprintf(write_buf, "%u\n", value);
212     try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
213 }
214
215
216
217 static void write_inventory(struct Thing * player, FILE * file)
218 {
219     char * f_name = "write_inventory()";
220     struct Thing * owned = player->owns;
221     if (NULL == owned)
222     {
223         char * empty = "(none)\n";
224         try_fwrite(empty, strlen(empty), 1, file, f_name);
225     }
226     else
227     {
228         uint8_t q;
229         for (q = 0; NULL != owned; q++)
230         {
231             struct ThingType * tt = get_thing_type(owned->type);
232             try_fwrite(tt->name, strlen(tt->name), 1, file, f_name);
233             try_fputc('\n', file, f_name);
234             owned = owned->next;
235         }
236     }
237     try_fputc('%', file, f_name);
238     try_fputc('\n', file, f_name);
239 }
240
241
242
243 static char * build_visible_map(struct Thing * player)
244 {
245     char * f_name = "build_visible_map()";
246     uint32_t map_size = world.map.length * world.map.length;
247     char * visible_map = try_malloc(map_size, f_name);
248     memset(visible_map, ' ', map_size);
249     if (player->fov_map) /* May fail if player thing was created / positioned */
250     {                    /* by god command after turning off FOV building.    */
251         uint16_t pos_i;
252         for (pos_i = 0; pos_i < map_size; pos_i++)
253         {
254             if (player->fov_map[pos_i] & VISIBLE)
255             {
256                 visible_map[pos_i] = world.map.cells[pos_i];
257             }
258         }
259         struct Thing * t;
260         struct ThingType * tt;
261         char c;
262         uint8_t i;
263         for (i = 0; i < 2; i++)
264         {
265             for (t = world.things; t != 0; t = t->next)
266             {
267                 if (   player->fov_map[yx_to_map_pos(&t->pos)] & VISIBLE
268                     && (   (0 == i && 0 == t->lifepoints)
269                         || (1 == i && 0 < t->lifepoints)))
270                 {
271                     tt = get_thing_type(t->type);
272                     c = tt->char_on_map;
273                     visible_map[yx_to_map_pos(&t->pos)] = c;
274                 }
275             }
276         }
277     }
278     return visible_map;
279 }
280
281
282
283 static void write_map(struct Thing * player, FILE * file)
284 {
285     char * f_name = "write_map()";
286     char * visible_map = build_visible_map(player);
287     uint16_t x, y;
288     for (y = 0; y < world.map.length; y++)
289     {
290         for (x = 0; x < world.map.length; x++)
291         {
292             try_fputc(visible_map[(y * world.map.length) + x], file, f_name);
293         }
294         try_fputc('\n', file, f_name);
295     }
296     free(visible_map);
297 }
298
299
300
301 extern char * io_round()
302 {
303     char * f_name = "io_round()";
304     if (0 < world.queue_size)
305     {
306         return get_message_from_queue();
307     }
308     if (world.turn != world.last_update_turn)
309     {
310         update_worldstate_file();
311         world.last_update_turn = world.turn;
312     }
313     read_file_into_queue();
314     if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
315     {
316         char * new_queue = try_malloc(world.queue_size + 1, f_name);
317         memcpy(new_queue, world.queue, world.queue_size);
318         new_queue[world.queue_size] = '\0';
319         world.queue_size++;
320         free(world.queue);
321         world.queue = new_queue;
322     }
323     return get_message_from_queue();
324 }
325
326
327
328 extern void save_world()
329 {
330     char * f_name = "save_world()";
331     char * path = s[PATH_SAVE];
332     FILE * file = try_fopen(path, "w", f_name);
333     write_key_value(file, s[CMD_DO_FOV], 0);
334     try_fputc('\n', file, f_name);
335     write_key_value(file, s[CMD_SEED_MAP], world.seed_map);
336     write_key_value(file, s[CMD_SEED_RAND], world.seed);
337     write_key_value(file, s[CMD_TURN], world.turn);
338     try_fputc('\n', file, f_name);
339     struct Thing * t;
340     for (t = world.things; t; t = t->next)
341     {
342         write_thing(file, t);
343     }
344     write_key_value(file, s[CMD_DO_FOV], 1);
345     try_fclose(file, f_name);
346 }