3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200112L /* snrpintf() */
10 #include <errno.h> /* global errno */
11 #include <limits.h> /* PIPE_BUF */
12 #include <stddef.h> /* NULL */
13 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int32_t, UINT8_MAX */
14 #include <stdio.h> /* defines FILE, sprintf(), fprintf() */
15 #include <stdlib.h> /* free() */
16 #include <string.h> /* strlen(), snprintf(), memcpy(), strchr() */
17 #include <sys/types.h> /* time_t */
18 #include <time.h> /* time(), nanosleep() */
19 #include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
20 * get_message_from_queue(), try_fwrite(),
21 * read_file_into_queue(), try_fputc()
23 #include "../common/rexit.h" /* exit_trouble() */
24 #include "../common/try_malloc.h" /* try_malloc() */
25 #include "cleanup.h" /* set_cleanup_flag() */
26 #include "hardcoded_strings.h" /* s */
27 #include "map.h" /* init_empty_map() */
28 #include "run.h" /* send_to_outfile() */
29 #include "things.h" /* Thing, ThingType, ThingInMemory, ThingAction,
30 * get_thing_type(), get_player()
32 #include "world.h" /* global world */
36 /* Helpers to write lines of god commands to recreate thing "t". */
37 static void write_key_space(FILE * file, char * key);
38 static void write_uvalue(FILE * file, uint32_t value);
39 static void write_string(FILE * file, char * string);
40 static void write_key_space_uvalue(FILE * file, char * key, uint32_t value);
41 static void write_key_space_svalue(FILE * file, char * key, int32_t value);
42 static void write_key_space_string(FILE * file, char * key, char * string);
44 /* Write to "file" game-map-sized "map" in "command"-prefixed numbered lines. */
45 static void write_mem_map(FILE * file, char * map, char * command);
47 /* Write to "file" \n-delimited line of "key" + space + "value" as string. */
48 static void write_thing(FILE * file, struct Thing * t);
50 /* Poll input file for world.queue input. Wait a few seconds until giving up;
51 * poll only every 0.03 seconds. Translate '\n' chars in input file into '\0'.
53 static void try_growing_queue();
55 /* Write world state as visible to clients to its file. Write single dot line to
56 * server output file to satisfy client ping mechanisms.
58 static void update_worldstate_file();
60 /* Write "value" to new \n-delimited line of "file". */
61 static void write_value_as_line(int32_t value, FILE * file);
63 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
64 static void write_inventory(struct Thing * player, FILE * file);
66 /* Return map cells sequence as visible to the "player", with invisible cells as
67 * whitespace. Super-impose over visible map cells things positioned there,
68 * with animate things overwriting inanimate things, and inanimate consumable
69 * things overwriting inanimate non-consumable things.
71 static char * build_visible_map(struct Thing * player);
73 /* Write to "file" game map as visible to "player" right now, as drawn by
74 * build_visible_map(), and thereafter game map as memorized by player in its
75 * .mem_map and .t_mem. Write one row per \n-delimited line.
77 static void write_map(struct Thing * player, FILE * file);
81 static void write_key_space(FILE * file, char * key)
83 try_fwrite(key, strlen(key), 1, file, __func__);
84 try_fputc(' ', file, __func__);
89 static void write_uvalue(FILE * file, uint32_t value)
91 char * line = try_malloc(11, __func__);
92 exit_trouble(-1 == sprintf(line, "%u", value), __func__, s[S_FCN_SPRINTF]);
93 try_fwrite(line, strlen(line), 1, file, __func__);
99 static void write_string(FILE * file, char * string)
101 try_fputc('\'', file, __func__);
102 try_fwrite(string, strlen(string), 1, file, __func__);
103 try_fputc('\'', file, __func__);
108 static void write_key_space_uvalue(FILE * file, char * key, uint32_t value)
110 write_key_space(file, key);
111 write_uvalue(file, value);
112 try_fputc('\n', file, __func__);
117 static void write_key_space_svalue(FILE * file, char * key, int32_t value)
119 write_key_space(file, key);
120 char * line = try_malloc(11, __func__);
121 exit_trouble(-1 == sprintf(line, "%d", value), __func__, s[S_FCN_SPRINTF]);
122 try_fwrite(line, strlen(line), 1, file, __func__);
124 try_fputc('\n', file, __func__);
129 static void write_key_space_string(FILE * file, char * key, char * string)
131 write_key_space(file, key);
132 write_string(file, string);
133 try_fputc('\n', file, __func__);
138 static void write_mem_map(FILE * file, char * map, char * command)
142 uint32_t map_size = world.map.length * world.map.length;/* snprintf() */
143 char * map_copy = try_malloc(map_size + 1, __func__); /* reads one */
144 memcpy(map_copy, map, map_size); /* byte beyond map_size */
145 map_copy[map_size] = '\0'; /* if string is not \0-terminated. */
147 char string[UINT8_MAX + 1 + 1];
148 for (y = 0; y < world.map.length; y++)
150 int test = snprintf(string, world.map.length + 1, "%s",
151 map_copy + (y * world.map.length));
152 exit_trouble(test < 0, __func__, "snprintf()");
153 write_key_space(file, command);
154 write_uvalue(file, y);
155 try_fputc(' ', file, __func__);
156 write_string(file, string);
157 try_fputc('\n', file, __func__);
165 static void write_thing(FILE * file, struct Thing * t)
168 for (o = t->owns; o; o = o->next)
170 write_thing(file, o);
172 write_key_space_uvalue(file, s[S_CMD_T_ID], t->id);
173 struct ThingInMemory * tm = t->t_mem;
174 for (; tm; tm = tm->next)
176 write_key_space(file, s[S_CMD_T_MEMTHING]);
177 write_uvalue(file, tm->type);
178 try_fputc(' ', file, __func__);
179 write_uvalue(file, tm->pos.y);
180 try_fputc(' ', file, __func__);
181 write_uvalue(file, tm->pos.x);
182 try_fputc('\n', file, __func__);
184 write_key_space_uvalue(file, s[S_CMD_T_COMMAND], t->command);
185 write_key_space_uvalue(file, s[S_CMD_T_HP], t->lifepoints);
186 write_key_space_uvalue(file, s[S_CMD_T_TYPE], t->type);
187 write_key_space_uvalue(file, s[S_CMD_T_ARGUMENT], t->arg);
188 write_key_space_uvalue(file, s[S_CMD_T_POSY], t->pos.y);
189 write_key_space_uvalue(file, s[S_CMD_T_POSX], t->pos.x);
190 write_key_space_uvalue(file, s[S_CMD_T_PROGRESS], t->progress);
191 write_mem_map(file, t->mem_depth_map, s[S_CMD_T_MEMDEPTHMAP]);
192 write_key_space_svalue(file, s[S_CMD_T_SATIATION], t->satiation);
193 write_mem_map(file, t->mem_map, s[S_CMD_T_MEMMAP]);
198 static void write_thing_carrying(FILE * file, struct Thing * t)
202 write_key_space_uvalue(file, s[S_CMD_T_ID], t->id);
204 for (o = t->owns; o; o = o->next)
206 write_key_space_uvalue(file, s[S_CMD_T_CARRIES], o->id);
213 static void try_growing_queue()
215 uint8_t wait_seconds = 5;
216 time_t now = time(0);
219 dur.tv_nsec = 33333333;
222 if (read_file_into_queue(world.file_in, &world.queue))
226 nanosleep(&dur, NULL);
227 if (time(0) > now + wait_seconds)
236 static void update_worldstate_file()
239 FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
240 struct Thing * player = get_player();
241 write_value_as_line(world.turn, file);
242 write_value_as_line(player->lifepoints, file);
243 write_value_as_line(player->satiation, file);
244 write_inventory(player, file);
245 write_value_as_line(player->pos.y, file);
246 write_value_as_line(player->pos.x, file);
247 write_value_as_line(world.map.length, file);
248 write_map(player, file);
249 atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
250 set_cleanup_flag(CLEANUP_WORLDSTATE);
252 try_fwrite(dot, strlen(dot), 1, world.file_out, __func__);
253 fflush(world.file_out);
258 static void write_value_as_line(int32_t value, FILE * file)
260 char write_buf[13]; /* Hold "+"/"-" + 10 digits of int32_t max + \n + \0. */
261 exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]);
262 try_fwrite(write_buf, strlen(write_buf), 1, file, __func__);
267 static void write_inventory(struct Thing * player, FILE * file)
269 struct Thing * owned = player->owns;
272 char * empty = "(none)\n";
273 try_fwrite(empty, strlen(empty), 1, file, __func__);
278 for (q = 0; owned; q++)
280 struct ThingType * tt = get_thing_type(owned->type);
281 try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
282 try_fputc('\n', file, __func__);
286 try_fputc('%', file, __func__);
287 try_fputc('\n', file, __func__);
292 static char * build_visible_map(struct Thing * player)
295 init_empty_map(&visible_map);
296 if (player->fov_map) /* May fail if player thing was created / positioned */
297 { /* by god command after turning off FOV building. */
299 for (; pos_i < (uint32_t) world.map.length * world.map.length; pos_i++)
301 if (player->fov_map[pos_i] == 'v')
303 visible_map[pos_i] = world.map.cells[pos_i];
309 for (i = 0; i < 3; i++)
311 for (t = world.things; t != 0; t = t->next)
313 if ('v' == player->fov_map[t->pos.y*world.map.length+t->pos.x])
315 struct ThingType * tt = get_thing_type(t->type);
316 if ( (0 == i && !t->lifepoints && !tt->consumable)
317 || (1 == i && !t->lifepoints && tt->consumable)
318 || (2 == i && t->lifepoints))
321 visible_map[t->pos.y * world.map.length + t->pos.x] = c;
332 static void write_map(struct Thing * player, FILE * file)
334 char * visible_map = build_visible_map(player);
336 for (y = 0; y < world.map.length; y++)
338 for (x = 0; x < world.map.length; x++)
340 try_fputc(visible_map[y * world.map.length + x], file, __func__);
342 try_fputc('\n', file, __func__);
345 uint32_t map_size = world.map.length * world.map.length;
346 char * mem_map = try_malloc(map_size, __func__);
347 memcpy(mem_map, player->mem_map, map_size);
349 struct ThingInMemory * tm;
350 for (i = 0; i < 2; i++)
352 for (tm = player->t_mem; tm; tm = tm->next)
354 if (' ' != player->mem_map[tm->pos.y*world.map.length+tm->pos.x])
356 struct ThingType * tt = get_thing_type(tm->type);
357 if ( (0 == i && !tt->consumable)
358 || (1 == i && tt->consumable))
360 char c = tt->char_on_map;
361 mem_map[tm->pos.y * world.map.length + tm->pos.x] = c;
366 for (y = 0; y < world.map.length; y++)
368 for (x = 0; x < world.map.length; x++)
370 try_fputc(mem_map[y * world.map.length + x], file, __func__);
372 try_fputc('\n', file, __func__);
379 extern char * io_round()
381 if (world.queue && strlen(world.queue))
383 return get_message_from_queue(&world.queue);
387 update_worldstate_file();
388 send_to_outfile("WORLD_UPDATED\n", 1);
392 return get_message_from_queue(&world.queue);
397 extern void save_world()
400 FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
401 write_key_space_uvalue(file, s[S_CMD_TURN], world.turn);
402 write_key_space_uvalue(file, s[S_CMD_PLAYTYPE], world.player_type);
403 write_key_space_uvalue(file, s[S_CMD_MAPLENGTH], world.map.length);
404 write_key_space_uvalue(file, s[S_CMD_SEED_MAP], world.seed_map);
405 struct ThingAction * ta;
406 for (ta = world.thing_actions; ta; ta = ta->next)
408 write_key_space_uvalue(file, s[S_CMD_TA_ID], ta->id);
409 write_key_space_uvalue(file, s[S_CMD_TA_EFFORT], ta->effort);
410 write_key_space_string(file, s[S_CMD_TA_NAME], ta->name);
412 struct ThingType * tt;
413 for (tt = world.thing_types; tt; tt = tt->next)
415 write_key_space_uvalue(file, s[S_CMD_TT_ID], tt->id);
416 write_key_space_uvalue(file, s[S_CMD_TT_STARTN], tt->start_n);
417 int test = fprintf(file, "%s '%c'\n", s[S_CMD_TT_SYMB], tt->char_on_map);
418 exit_trouble(test < 0, __func__, "fprintf");
419 write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
420 write_key_space_uvalue(file, s[S_CMD_TT_PROL], tt->proliferate);
421 write_key_space_uvalue(file, s[S_CMD_TT_HP], tt->lifepoints);
422 write_key_space_uvalue(file, s[S_CMD_TT_CONSUM], tt->consumable);
424 for (tt = world.thing_types; tt; tt = tt->next)
426 write_key_space_uvalue(file, s[S_CMD_TT_ID], tt->id);
427 write_key_space_uvalue(file, s[S_CMD_TT_CORPS], tt->corpse_id);
430 for (t = world.things; t; t = t->next)
432 write_thing(file, t);
434 for (t = world.things; t; t = t->next)
436 write_thing_carrying(file, t);
438 write_key_space_uvalue(file, s[S_CMD_SEED_RAND], world.seed);
439 write_key_space_uvalue(file, s[S_CMD_WORLD_ACTIVE], 1);
440 atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);