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