3 #define _POSIX_C_SOURCE 200809L
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE, printf(), sprintf(), fflush() */
8 #include <stdlib.h> /* free(), atoi() */
9 #include <string.h> /* strlen(), strcmp() strncmp(), strdup() */
10 #include <unistd.h> /* access() */
11 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
14 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
15 * try_fgets(), try_fclose_unlink_rename(),
16 * textfile_width(), try_fputc()
18 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
19 #include "../common/try_malloc.h" /* try_malloc() */
20 #include "ai.h" /* ai() */
21 #include "cleanup.h" /* set_cleanup_flag(), unset_cleanup_flag() */
22 #include "field_of_view.h" /* build_fov_map() */
23 #include "hardcoded_strings.h" /* s */
24 #include "init.h" /* remake_world() */
25 #include "io.h" /* io_round(), save_world() */
26 #include "map.h" /* remake_map() */
27 #include "thing_actions.h" /* ThingAction */
28 #include "things.h" /* Thing, get_thing(), own_thing(), add_thing(),
29 * get_thing_action_id_by_name(), get_player()
31 #include "world.h" /* global world */
35 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
36 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
38 /* Parse/apply god commansd in "tok0"/"tok1" on positioning a thing "t". */
39 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
41 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
42 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
44 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
45 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
47 /* Parse/apply god command on enabling/disabling generation of fields of view on
48 * god commands that may affect them, via static global "do_fov". On enabling,
49 * (re-)generate all animate things' fields of view.
51 static uint8_t parse_do_fov(char * tok0, char * tok1);
53 /* Parse/apply god command in "tok0"/"tok1" manipulating a thing's state. */
54 static uint8_t parse_thing_manipulation(char * tok0, char * tok1);
56 /* Parse player command in "tok0"/"tok1" to action in player thing. */
57 static uint8_t parse_player_command(char * tok0, char * tok1);
59 /* Compares first line of server out file to world.server_test, aborts if they
60 * don't match, but not before unsetting the flags deleting files in the server
61 * directory, for in that case those must be assumed to belong to another server
64 static void server_test();
66 /* Run the game world and its inhabitants (and their actions) until the player
67 * avatar is free to receive new commands (or is dead).
69 static void turn_over();
73 /* Do god commands to create / position things generate their fields of view? */
74 static uint8_t do_fov = 0;
78 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
81 if (parse_val(tok0, tok1, s[S_CMD_CARRIES], '8', (char *) &id))
83 if (!err_line(id == t->id, "Thing cannot carry itself."))
85 struct Thing * o = get_thing(world.things, id, 0);
86 if (!err_line(!o, "Thing cannot carry thing that does not exist."))
88 own_thing(&(t->owns), &world.things, id);
99 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
102 if (!strcmp(tok0, s[S_CMD_POS_Y]))
106 else if (!strcmp(tok0, s[S_CMD_POS_X]))
110 if (axis && !parsetest_int(tok1, '8'))
112 uint8_t length = atoi(tok1);
113 char * err = "Position is outside of map.";
114 if (!err_line(length >= world.map.length, err))
120 else if ('x' == axis)
125 t->fov_map= do_fov && t->lifepoints ? build_fov_map(t) : t->fov_map;
134 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
137 if (parse_val(tok0, tok1, s[S_CMD_TYPE], '8', (char *) &type))
139 struct ThingType * tt = world.thing_types;
140 for (; NULL != tt && type != tt->id; tt = tt->next);
141 if (!err_line(!tt, "Thing type does not exist."))
152 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
155 if (parse_val(tok0, tok1, s[S_CMD_COMMAND], '8', (char *) &command))
159 t->command = command;
162 struct ThingAction * ta = world.thing_actions;
163 for (; NULL != ta && command != ta->id; ta = ta->next);
164 if (!err_line(!ta, "Thing action does not exist."))
166 t->command = command;
175 static uint8_t parse_do_fov(char * tok0, char * tok1)
177 if (parse_val(tok0, tok1, s[S_CMD_DO_FOV], '8', (char *) &do_fov))
182 for (ti = world.things; ti; ti = ti->next)
184 ti->fov_map = ti->lifepoints ? build_fov_map(ti) : ti->fov_map;
194 static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
197 static struct Thing * t = NULL;
198 if (t && ( parse_thing_type(tok0, tok1, t)
199 || parse_thing_command(tok0, tok1, t)
200 || parse_val(tok0, tok1, s[S_CMD_ARGUMENT], '8', (char *)&t->arg)
201 || parse_val(tok0,tok1,s[S_CMD_PROGRESS],'8',(char *)&t->progress)
202 || parse_val(tok0, tok1, s[S_CMD_LIFEPOINTS], '8',
203 (char *) &t->lifepoints)
204 || parse_position(tok0, tok1, t)
205 || parse_carry(tok0, tok1, t)));
206 else if (parse_val(tok0, tok1, s[S_CMD_THING], '8', (char *) &id))
208 t = get_thing(world.things, id, 1);
211 t = add_thing(id, 0, 0);
212 set_cleanup_flag(CLEANUP_THINGS);
213 t->fov_map= do_fov && t->lifepoints ? build_fov_map(t) : t->fov_map;
225 static uint8_t parse_player_command(char * tok0, char * tok1)
227 struct Thing * player = get_player();
228 if ( parse_val(tok0, tok1, s[S_CMD_WAIT], '8', (char *) &player->arg)
229 || parse_val(tok0, tok1, s[S_CMD_MOVE], '8', (char *) &player->arg)
230 || parse_val(tok0, tok1, s[S_CMD_PICKUP], '8', (char *) &player->arg)
231 || parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
232 || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
234 player->command = get_thing_action_id_by_name(tok0);
246 static void server_test()
248 char * f_name = "server_test()";
249 char test[10 + 1 + 10 + 1 + 1];
250 FILE * file = try_fopen(s[S_PATH_OUT], "r", f_name);
251 try_fgets(test, 10 + 10 + 1 + 1, file, f_name);
252 try_fclose(file, f_name);
253 if (strcmp(test, world.server_test))
255 unset_cleanup_flag(CLEANUP_WORLDSTATE);
256 unset_cleanup_flag(CLEANUP_OUT);
257 unset_cleanup_flag(CLEANUP_IN);
258 char * msg = "Server test string in server output file does not match. "
259 "This indicates that the current server process has been "
260 "superseded by another one.";
267 static void turn_over()
269 struct Thing * player = get_player();
270 struct Thing * thing = player;
271 uint16_t start_turn = world.turn;
272 while ( 0 < player->lifepoints
273 || (0 == player->lifepoints && start_turn == world.turn))
278 thing = world.things;
280 if (0 < thing->lifepoints)
282 if (0 == thing->command)
291 struct ThingAction * ta = world.thing_actions;
292 while (ta->id != thing->command)
296 if (thing->progress == ta->effort)
309 static void record_msg(char * msg)
311 char * f_name = "record_msg()";
312 uint16_t size = strlen(s[S_PATH_RECORD]) + strlen(s[S_PATH_SUFFIX_TMP]) + 1;
313 char * path_tmp = try_malloc(size, f_name);
314 int test = sprintf(path_tmp, "%s%s", s[S_PATH_RECORD],s[S_PATH_SUFFIX_TMP]);
315 exit_trouble(test < 0, f_name, "sprintf()");
316 FILE * file_tmp = try_fopen(path_tmp, "w", f_name);
317 if (!access(s[S_PATH_RECORD], F_OK))
319 FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", f_name);
320 uint32_t linemax = textfile_width(file_read);
321 char * line = try_malloc(linemax + 1, f_name);
322 while (try_fgets(line, linemax + 1, file_read, f_name))
324 try_fwrite(line, strlen(line), 1, file_tmp, f_name);
327 try_fclose(file_read, f_name);
329 try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
330 try_fputc('\n', file_tmp, f_name);
331 try_fclose_unlink_rename(file_tmp, path_tmp, s[S_PATH_RECORD], f_name);
337 extern void obey_msg(char * msg, uint8_t do_record)
339 set_err_line_options("Trouble with message: ", msg, 0, 0);
340 char * msg_copy = strdup(msg);
341 char * tok0 = token_from_line(msg_copy);
342 char * tok1 = token_from_line(NULL);
343 char * tok2 = token_from_line(NULL);
344 if (err_line(!(tok0 && tok1) || tok2, "Bad number of tokens."))
348 if ( parse_thing_manipulation(tok0, tok1)
349 || parse_player_command(tok0, tok1)
350 || parse_val(tok0, tok1, s[S_CMD_SEED_RAND], 'U', (char *) &world.seed)
351 || parse_val(tok0, tok1, s[S_CMD_TURN], 'u', (char *) &world.turn)
352 || parse_do_fov(tok0, tok1));
353 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
358 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
364 err_line(1, "Unknown command.");
368 world.last_update_turn = 0;
379 extern uint8_t io_loop()
381 char * f_name = "io_loop()";
385 char * msg = io_round();
390 if (world.is_verbose)
392 exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
394 if (!strcmp("QUIT", msg))
399 if (!strcmp("PING", msg))
402 char * pong = "PONG\n";
403 try_fwrite(pong, strlen(pong), 1, world.file_out, f_name);
404 fflush(world.file_out);