3 #define _POSIX_C_SOURCE 199309L
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, uint32_t */
9 #include <stdio.h> /* defines EOF, FILE, sprintf() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), memcpy() */
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()
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map_objects.h" /* structs MapObj, MapObjDef, get_map_obj_def() */
20 #include "world.h" /* global world */
24 /* Cut out and return first \0-terminated string from world.queue and
25 * appropriately reduce world.queue_size. Return NULL if queue is empty.
26 * Superfluous \0 bytes after the string are also cut out. Should the queue
27 * start with \0 bytes, those are cut out, but NULL is returned instead of "".
29 static char * get_message_from_queue();
31 /* Poll input file for world.queue input. Wait a few seconds until giving up;
32 * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
34 static void read_file_into_queue();
36 /* Write world state as visible to clients to its file. Write single dot line to
37 * server output file to satisfy client ping mechanisms.
39 static void update_worldstate_file();
41 /* Write "value" to new \n-delimited line of "file". */
42 static void write_value_as_line(uint32_t value, FILE * file);
44 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
45 static void write_inventory(struct MapObj * player, FILE * file);
47 /* Write to "file" game map, with map objects super-imposed. Write one row per
48 * \n-delimited line. Super-impose animated objects over inanimate objects.
50 static void write_map(FILE * file);
54 static char * get_message_from_queue()
56 char * f_name = "get_message_from_queue()";
57 char * message = NULL;
60 size_t cutout_len = strlen(world.queue);
64 message = try_malloc(cutout_len, f_name);
65 memcpy(message, world.queue, cutout_len);
68 cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
70 world.queue_size = world.queue_size - cutout_len;
71 if (0 == world.queue_size)
73 free(world.queue); /* NULL so read_file_into_queue() may free() */
74 world.queue = NULL; /* this every time, even when it's */
75 } /* un-allocated first. */
78 char * new_queue = try_malloc(world.queue_size, f_name);
79 memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
81 world.queue = new_queue;
89 static void read_file_into_queue()
91 char * f_name = "read_file_into_queue()";
92 uint8_t wait_seconds = 5;
96 dur.tv_nsec = 33333333;
98 while (EOF == (test = try_fgetc(world.file_in, f_name)))
100 nanosleep(&dur, NULL);
101 if (time(0) > now + wait_seconds)
108 char c = (char) test;
113 char * new_queue = try_malloc(world.queue_size + 1, f_name);
114 memcpy(new_queue, world.queue, world.queue_size);
115 char * new_pos = new_queue + world.queue_size;
119 world.queue = new_queue;
121 while (EOF != (test = try_fgetc(world.file_in, f_name)));
126 static void update_worldstate_file()
128 char * f_name = "update_worldstate_file()";
129 char path_tmp[strlen(world.path_worldstate) + strlen(world.tmp_suffix) + 1];
130 sprintf(path_tmp, "%s%s", world.path_worldstate, world.tmp_suffix);
131 FILE * file = try_fopen(path_tmp, "w", f_name);
132 struct MapObj * player = get_player();
133 write_value_as_line(world.turn, file);
134 write_value_as_line(player->lifepoints, file);
135 write_inventory(player, file);
136 write_value_as_line(player->pos.y, file);
137 write_value_as_line(player->pos.x, file);
138 write_value_as_line(world.map.size.y, file);
139 write_value_as_line(world.map.size.x, file);
143 try_fwrite(world.log, strlen(world.log), 1, file, f_name);
145 try_fclose_unlink_rename(file, path_tmp, world.path_worldstate, f_name);
146 set_cleanup_flag(CLEANUP_WORLDSTATE);
148 try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
149 fflush(world.file_out);
154 static void write_value_as_line(uint32_t value, FILE * file)
156 char * f_name = "write_value_as_line()";
157 char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
158 sprintf(write_buf, "%u\n", value);
159 try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
164 static void write_inventory(struct MapObj * player, FILE * file)
166 char * f_name = "write_inventory()";
167 struct MapObj * owned = player->owns;
170 char * empty = "(none)\n";
171 try_fwrite(empty, strlen(empty), 1, file, f_name);
176 for (q = 0; NULL != owned; q++)
178 struct MapObjDef * mod = get_map_object_def(owned->type);
179 try_fwrite(mod->name, strlen(mod->name), 1, file, f_name);
180 try_fputc('\n', file, f_name);
184 try_fputc('%', file, f_name);
185 try_fputc('\n', file, f_name);
190 static void write_map(FILE * file)
192 char * f_name = "write_map()";
193 uint32_t map_size = world.map.size.y * world.map.size.x;
194 char visible_map[map_size];
195 memcpy(visible_map, world.map.cells, map_size);
197 struct MapObjDef * d;
200 for (i = 0; i < 2; i++)
202 for (o = world.map_objs; o != 0; o = o->next)
204 if (( (0 == i && 0 == o->lifepoints)
205 || (1 == i && 0 < o->lifepoints)))
207 d = get_map_object_def(o->type);
209 visible_map[(o->pos.y * world.map.size.x) + o->pos.x] = c;
214 for (y = 0; y < world.map.size.y; y++)
216 for (x = 0; x < world.map.size.x; x++)
218 try_fputc(visible_map[(y * world.map.size.x) + x], file, f_name);
220 try_fputc('\n', file, f_name);
226 extern char * io_round()
228 char * f_name = "io_round()";
229 if (0 < world.queue_size)
231 return get_message_from_queue();
233 if (world.turn != world.last_update_turn)
235 update_worldstate_file();
236 world.last_update_turn = world.turn;
238 read_file_into_queue();
239 if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
241 char * new_queue = try_malloc(world.queue_size + 1, f_name);
242 memcpy(new_queue, world.queue, world.queue_size);
243 new_queue[world.queue_size] = '\0';
246 world.queue = new_queue;
248 return get_message_from_queue();