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(), 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(), textfile_width(), try_fputc()
17 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
18 #include "../common/try_malloc.h" /* try_malloc() */
19 #include "ai.h" /* ai() */
20 #include "cleanup.h" /* set_cleanup_flag(), unset_cleanup_flag() */
21 #include "field_of_view.h" /* build_fov_map() */
22 #include "hardcoded_strings.h" /* s */
23 #include "init.h" /* remake_world() */
24 #include "io.h" /* io_round(), save_world() */
25 #include "map.h" /* remake_map() */
26 #include "thing_actions.h" /* ThingAction */
27 #include "things.h" /* Thing, get_thing(), own_thing(), add_thing(),
28 * get_thing_action_id_by_name(), get_player()
30 #include "world.h" /* world */
34 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
35 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
37 /* Parse/apply god commansd in "tok0"/"tok1" on positioning a thing "t". */
38 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
40 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
41 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
43 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
44 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
46 /* Parse/apply god command on enabling/disabling generation of fields of view on
47 * god commands that may affect them, via static global "do_fov". On enabling,
48 * (re-)generate all animate things' fields of view.
50 static uint8_t parse_do_fov(char * tok0, char * tok1);
52 /* Parse/apply god command in "tok0"/"tok1" manipulating a thing's state. */
53 static uint8_t parse_thing_manipulation(char * tok0, char * tok1);
55 /* Parse player command "tok0" with one arguments "tok1" to player action. */
56 static uint8_t parse_player_command_0arg(char * tok0, char * tok1);
58 /* Parse player command "tok0" with no argument to player action, comment on
59 * invalidity of non-zero "tok1" (but do not abort in that case).
61 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
63 /* Parse/apply commadn "tok0" with argument "tok1" and test the line for further
64 * tokens, commenting on their invalidity (but don't abort on findingthem).
66 static uint8_t parse_command_1arg(char * tok0, char * tok1);
69 /* Compares first line of server out file to world.server_test, aborts if they
70 * don't match, but not before unsetting the flags deleting files in the server
71 * directory, for in that case those must be assumed to belong to another server
74 static void server_test();
76 /* Run the game world and its inhabitants (and their actions) until the player
77 * avatar is free to receive new commands (or is dead).
79 static void turn_over();
83 /* Do god commands to create / position things generate their fields of view? */
84 static uint8_t do_fov = 0;
88 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
91 if (parse_val(tok0, tok1, s[S_CMD_CARRIES], '8', (char *) &id))
93 if (!err_line(id == t->id, "Thing cannot carry itself."))
95 struct Thing * o = get_thing(world.things, id, 0);
96 if (!err_line(!o, "Thing cannot carry thing that does not exist."))
98 own_thing(&(t->owns), &world.things, id);
109 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
112 if (!strcmp(tok0, s[S_CMD_POS_Y]))
116 else if (!strcmp(tok0, s[S_CMD_POS_X]))
120 if (axis && !parsetest_int(tok1, '8'))
122 uint8_t length = atoi(tok1);
123 char * err = "Position is outside of map.";
124 if (!err_line(length >= world.map.length, err))
130 else if ('x' == axis)
135 t->fov_map= do_fov && t->lifepoints ? build_fov_map(t) : t->fov_map;
144 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
147 if (parse_val(tok0, tok1, s[S_CMD_TYPE], '8', (char *) &type))
149 struct ThingType * tt = world.thing_types;
150 for (; NULL != tt && type != tt->id; tt = tt->next);
151 if (!err_line(!tt, "Thing type does not exist."))
162 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
165 if (parse_val(tok0, tok1, s[S_CMD_COMMAND], '8', (char *) &command))
169 t->command = command;
172 struct ThingAction * ta = world.thing_actions;
173 for (; NULL != ta && command != ta->id; ta = ta->next);
174 if (!err_line(!ta, "Thing action does not exist."))
176 t->command = command;
185 static uint8_t parse_do_fov(char * tok0, char * tok1)
187 if (parse_val(tok0, tok1, s[S_CMD_DO_FOV], '8', (char *) &do_fov))
192 for (ti = world.things; ti; ti = ti->next)
194 ti->fov_map = ti->lifepoints ? build_fov_map(ti) : ti->fov_map;
204 static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
207 static struct Thing * t = NULL;
208 if (t && ( parse_thing_type(tok0, tok1, t)
209 || parse_thing_command(tok0, tok1, t)
210 || parse_val(tok0, tok1, s[S_CMD_ARGUMENT], '8', (char *)&t->arg)
211 || parse_val(tok0,tok1,s[S_CMD_PROGRESS],'8',(char *)&t->progress)
212 || parse_val(tok0, tok1, s[S_CMD_LIFEPOINTS], '8',
213 (char *) &t->lifepoints)
214 || parse_position(tok0, tok1, t)
215 || parse_carry(tok0, tok1, t)));
216 else if (parse_val(tok0, tok1, s[S_CMD_THING], '8', (char *) &id))
218 t = get_thing(world.things, id, 1);
221 t = add_thing(id, 0, 0);
222 set_cleanup_flag(CLEANUP_THINGS);
223 t->fov_map= do_fov && t->lifepoints ? build_fov_map(t) : t->fov_map;
235 static uint8_t parse_player_command_0arg(char * tok0, char * tok1)
237 struct Thing * player = get_player();
238 if (!strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP]))
240 player->command = get_thing_action_id_by_name(tok0);
243 err_line (NULL != tok1, "No arguments expected, ignoring arguments.");
251 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
253 struct Thing * player = get_player();
254 if ( parse_val(tok0, tok1, s[S_CMD_MOVE], '8', (char *) &player->arg)
255 || parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
256 || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
258 player->command = get_thing_action_id_by_name(tok0);
270 static uint8_t parse_command_1arg(char * tok0, char * tok1)
272 char * tok2 = token_from_line(NULL);
273 if ( parse_thing_manipulation(tok0, tok1)
274 || parse_player_command_1arg(tok0, tok1)
275 || parse_val(tok0, tok1, s[S_CMD_SEED_RAND], 'U', (char *) &world.seed)
276 || parse_val(tok0, tok1, s[S_CMD_TURN], 'u', (char *) &world.turn)
277 || parse_do_fov(tok0, tok1));
278 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
283 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
291 char * err = "But one argument expected, ignoring further arguments.";
292 err_line (NULL != tok2, err);
298 static void server_test()
300 char * f_name = "server_test()";
301 char test[10 + 1 + 10 + 1 + 1];
302 FILE * file = try_fopen(s[S_PATH_OUT], "r", f_name);
303 try_fgets(test, 10 + 10 + 1 + 1, file, f_name);
304 try_fclose(file, f_name);
305 if (strcmp(test, world.server_test))
307 unset_cleanup_flag(CLEANUP_WORLDSTATE);
308 unset_cleanup_flag(CLEANUP_OUT);
309 unset_cleanup_flag(CLEANUP_IN);
310 char * msg = "Server test string in server output file does not match. "
311 "This indicates that the current server process has been "
312 "superseded by another one.";
319 static void turn_over()
321 struct Thing * player = get_player();
322 struct Thing * thing = player;
323 uint16_t start_turn = world.turn;
324 while ( 0 < player->lifepoints
325 || (0 == player->lifepoints && start_turn == world.turn))
330 thing = world.things;
332 if (0 < thing->lifepoints)
334 if (0 == thing->command)
343 struct ThingAction * ta = world.thing_actions;
344 while (ta->id != thing->command)
348 if (thing->progress == ta->effort)
361 static void record_msg(char * msg)
363 char * f_name = "record_msg()";
365 FILE * file_tmp = atomic_write_start(s[S_PATH_RECORD], &path_tmp);
366 if (!access(s[S_PATH_RECORD], F_OK))
368 FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", f_name);
369 uint32_t linemax = textfile_width(file_read);
370 char * line = try_malloc(linemax + 1, f_name);
371 while (try_fgets(line, linemax + 1, file_read, f_name))
373 try_fwrite(line, strlen(line), 1, file_tmp, f_name);
376 try_fclose(file_read, f_name);
378 try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
379 try_fputc('\n', file_tmp, f_name);
380 atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
385 extern void obey_msg(char * msg, uint8_t do_record)
387 set_err_line_options("Trouble with message: ", msg, 0, 0);
388 char * msg_copy = strdup(msg);
389 char * tok0 = token_from_line(msg_copy);
392 char * tok1 = token_from_line(NULL);
393 if ( parse_player_command_0arg(tok0, tok1)
394 || (tok1 && parse_command_1arg(tok0, tok1)))
406 err_line(1, "Unknown command or bad number of tokens.");
412 extern uint8_t io_loop()
414 char * f_name = "io_loop()";
418 char * msg = io_round();
423 if (world.is_verbose)
425 exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
427 if (!strcmp("QUIT", msg))
432 if (!strcmp("PING", msg))
435 char * pong = "PONG\n";
436 try_fwrite(pong, strlen(pong), 1, world.file_out, f_name);
437 fflush(world.file_out);