home · contact · privacy
Server: Internally, rename "map object" stuff to "thing" stuff.
[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/try_malloc.h" /* try_malloc() */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "field_of_view.h" /* VISIBLE */
20 #include "map.h" /* yx_to_map_pos() */
21 #include "things.h" /* Thing, ThingType, get_thing_type(), get_player() */
22 #include "world.h" /* global world  */
23
24
25
26 /* Cut out and return first \0-terminated string from world.queue and
27  * appropriately reduce world.queue_size. Return NULL if queue is empty.
28  * Superfluous \0 bytes after the string are also cut out. Should the queue
29  * start with \0 bytes, those are cut out, but NULL is returned instead of "".
30 */
31 static char * get_message_from_queue();
32
33 /* Poll input file for world.queue input. Wait a few seconds until giving up;
34  * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
35  */
36 static void read_file_into_queue();
37
38 /* Write world state as visible to clients to its file. Write single dot line to
39  * server output file to satisfy client ping mechanisms.
40  */
41 static void update_worldstate_file();
42
43 /* Write "value" to new \n-delimited line of "file". */
44 static void write_value_as_line(uint32_t value, FILE * file);
45
46 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
47 static void write_inventory(struct Thing * player, FILE * file);
48
49 /* Return map cells sequence as visible to the "player", with invisible cells as
50  * whitespace. Super-impose over visible map cells things positioned there.
51  */
52 static char * build_visible_map(struct Thing * player);
53
54 /* Write to "file" game map as visible to "player", build_visible_map()-drawn.
55  * Write one row per \n-delimited line.
56  */
57 static void write_map(struct Thing * player, FILE * file);
58
59
60
61 static char * get_message_from_queue()
62 {
63     char * f_name = "get_message_from_queue()";
64     char * message = NULL;
65     if (world.queue_size)
66     {
67         size_t cutout_len = strlen(world.queue);
68         if (0 < cutout_len)
69         {
70             cutout_len++;
71             message = try_malloc(cutout_len, f_name);
72             memcpy(message, world.queue, cutout_len);
73         }
74         for (;
75              cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
76              cutout_len++);
77         world.queue_size = world.queue_size - cutout_len;
78         if (0 == world.queue_size)
79         {
80             free(world.queue);   /* NULL so read_file_into_queue() may free() */
81             world.queue = NULL;  /* this every time, even when it's           */
82         }                        /* un-allocated first. */
83         else
84         {
85             char * new_queue = try_malloc(world.queue_size, f_name);
86             memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
87             free(world.queue);
88             world.queue = new_queue;
89         }
90     }
91     return message;
92 }
93
94
95
96 static void read_file_into_queue()
97 {
98     char * f_name = "read_file_into_queue()";
99     uint8_t wait_seconds = 5;
100     time_t now = time(0);
101     struct timespec dur;
102     dur.tv_sec = 0;
103     dur.tv_nsec = 33333333;
104     int test;
105     while (EOF == (test = try_fgetc(world.file_in, f_name)))
106     {
107         nanosleep(&dur, NULL);
108         if (time(0) > now + wait_seconds)
109         {
110             return;
111         }
112     }
113     do
114     {
115         char c = (char) test;
116         if ('\n' == c)
117         {
118             c = '\0';
119         }
120         char * new_queue = try_malloc(world.queue_size + 1, f_name);
121         memcpy(new_queue, world.queue, world.queue_size);
122         char * new_pos = new_queue + world.queue_size;
123         * new_pos = c;
124         world.queue_size++;
125         free(world.queue);
126         world.queue = new_queue;
127     }
128     while (EOF != (test = try_fgetc(world.file_in, f_name)));
129 }
130
131
132
133 static void update_worldstate_file()
134 {
135     char * f_name = "update_worldstate_file()";
136     char path_tmp[strlen(world.path_worldstate) + strlen(world.tmp_suffix) + 1];
137     sprintf(path_tmp, "%s%s", world.path_worldstate, world.tmp_suffix);
138     FILE * file = try_fopen(path_tmp, "w", f_name);
139     struct Thing * player = get_player();
140     write_value_as_line(world.turn, file);
141     write_value_as_line(player->lifepoints, file);
142     write_inventory(player, file);
143     write_value_as_line(player->pos.y, file);
144     write_value_as_line(player->pos.x, file);
145     write_value_as_line(world.map.length, file);
146     write_map(player, file);
147     if (world.log)
148     {
149         try_fwrite(world.log, strlen(world.log), 1, file, f_name);
150     }
151     try_fclose_unlink_rename(file, path_tmp, world.path_worldstate, f_name);
152     set_cleanup_flag(CLEANUP_WORLDSTATE);
153     char * dot = ".\n";;
154     try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
155     fflush(world.file_out);
156 }
157
158
159
160 static void write_value_as_line(uint32_t value, FILE * file)
161 {
162     char * f_name = "write_value_as_line()";
163     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
164     sprintf(write_buf, "%u\n", value);
165     try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
166 }
167
168
169
170 static void write_inventory(struct Thing * player, FILE * file)
171 {
172     char * f_name = "write_inventory()";
173     struct Thing * owned = player->owns;
174     if (NULL == owned)
175     {
176         char * empty = "(none)\n";
177         try_fwrite(empty, strlen(empty), 1, file, f_name);
178     }
179     else
180     {
181         uint8_t q;
182         for (q = 0; NULL != owned; q++)
183         {
184             struct ThingType * tt = get_thing_type(owned->type);
185             try_fwrite(tt->name, strlen(tt->name), 1, file, f_name);
186             try_fputc('\n', file, f_name);
187             owned = owned->next;
188         }
189     }
190     try_fputc('%', file, f_name);
191     try_fputc('\n', file, f_name);
192 }
193
194
195
196 static char * build_visible_map(struct Thing * player)
197 {
198     char * f_name = "build_visible_map()";
199     uint32_t map_size = world.map.length * world.map.length;
200     char * visible_map = try_malloc(map_size, f_name);
201     memset(visible_map, ' ', map_size);
202     uint16_t pos_i;
203     for (pos_i = 0; pos_i < map_size; pos_i++)
204     {
205         if (player->fov_map[pos_i] & VISIBLE)
206         {
207             visible_map[pos_i] = world.map.cells[pos_i];
208         }
209     }
210     struct Thing * t;
211     struct ThingType * tt;
212     char c;
213     uint8_t i;
214     for (i = 0; i < 2; i++)
215     {
216         for (t = world.things; t != 0; t = t->next)
217         {
218             if (   player->fov_map[yx_to_map_pos(&t->pos)] & VISIBLE
219                 && (   (0 == i && 0 == t->lifepoints)
220                     || (1 == i && 0 < t->lifepoints)))
221             {
222                 tt = get_thing_type(t->type);
223                 c = tt->char_on_map;
224                 visible_map[yx_to_map_pos(&t->pos)] = c;
225             }
226         }
227     }
228     return visible_map;
229 }
230
231
232
233 static void write_map(struct Thing * player, FILE * file)
234 {
235     char * f_name = "write_map()";
236     char * visible_map = build_visible_map(player);
237     uint16_t x, y;
238     for (y = 0; y < world.map.length; y++)
239     {
240         for (x = 0; x < world.map.length; x++)
241         {
242             try_fputc(visible_map[(y * world.map.length) + x], file, f_name);
243         }
244         try_fputc('\n', file, f_name);
245     }
246     free(visible_map);
247 }
248
249
250
251 extern char * io_round()
252 {
253     char * f_name = "io_round()";
254     if (0 < world.queue_size)
255     {
256         return get_message_from_queue();
257     }
258     if (world.turn != world.last_update_turn)
259     {
260         update_worldstate_file();
261         world.last_update_turn = world.turn;
262     }
263     read_file_into_queue();
264     if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
265     {
266         char * new_queue = try_malloc(world.queue_size + 1, f_name);
267         memcpy(new_queue, world.queue, world.queue_size);
268         new_queue[world.queue_size] = '\0';
269         world.queue_size++;
270         free(world.queue);
271         world.queue = new_queue;
272     }
273     return get_message_from_queue();
274 }