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 200809L /* strdup() */
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t */
12 #include <stdio.h> /* FILE, printf(), fflush() */
13 #include <stdlib.h> /* atoi(), free() */
14 #include <string.h> /* strlen(), strcmp(), strncmp(), strdup() */
15 #include <time.h> /* time_t, time() */
16 #include <unistd.h> /* access() */
17 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
18 * err_line(), err_line_inc(), parse_val(),
21 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
22 * try_fgets(), textfile_width(), try_fputc(),
23 * atomic_write_finish(), build_temp_path()
25 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
26 #include "../common/try_malloc.h" /* try_malloc() */
27 #include "ai.h" /* ai() */
28 #include "cleanup.h" /* unset_cleanup_flag() */
29 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
30 #include "hardcoded_strings.h" /* s */
31 #include "io.h" /* io_round(), save_world() */
32 #include "things.h" /* Thing, ThingType, get_thing_action_id_by_name(),
33 * get_player(), try_thing_proliferation()
35 #include "world.h" /* world */
39 /* If "string" and "comparand" match in string, set "c_to_set" to value." */
40 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
41 char * c_to_set, char value);
43 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
44 static uint8_t player_commands_allowed();
46 /* Parse player commands "tok0" with optional argument "tok1" to player action.
47 * Return 1 on success, 0 on failure.
49 static uint8_t parse_player_command_0arg(char * tok0);
50 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
52 /* Parse/apply (non-)meta command "tok0" (read further tokens as necessary).
53 * Return 0 on failure, 1 on success, 2 on QUIT meta command.
55 static uint8_t parse_command_nonmeta(char * tok0);
56 static uint8_t parse_command_meta(char * tok0);
58 /* Compare 1st line of server out file to world.server_test, abort if they don't
59 * match, but not before unsetting flags deleting files in the server directory,
60 * for in that case those must be assumed to belong to another server process.
62 static void server_test();
64 /* Return array of IDs of non-owned things in game world, ended by non-ID -1. */
65 static int16_t * build_whitelist();
67 /* Return 1 if value of "id" appears in "whitelist", else 0. */
68 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist);
70 /* Run the game world and its inhabitants (and their actions) until the player
71 * avatar is free to receive new commands (or is dead). Only actions and
72 * proliferations for non-owned things are performed that exist at the start of
73 * the turn jumped into, or started anew by the cycle.
75 static void turn_over();
79 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
80 char * c_to_set, char value)
82 if (!strcmp(string, comparand))
92 static uint8_t player_commands_allowed()
96 return !err_line(1, "No world exists in which to run player commands.");
103 static uint8_t parse_player_command_0arg(char * tok0)
105 struct Thing * player = get_player();
106 if ( !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
107 || !strcmp(tok0, s[S_CMD_AI]))
109 if (player_commands_allowed())
111 if (!strcmp(tok0, s[S_CMD_AI]))
117 player->command = get_thing_action_id_by_name(tok0);
129 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
131 struct Thing * player = get_player();
132 if ( ( parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
133 || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
134 && player_commands_allowed())
136 player->command = get_thing_action_id_by_name(tok0);
139 else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
142 if (!( set_char_by_string_comparison(tok1, "east", &dir, 'd')
143 || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
144 || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
145 || set_char_by_string_comparison(tok1, "west", &dir, 's')
146 || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
147 || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
152 player->command = get_thing_action_id_by_name(tok0);
164 static uint8_t parse_command_nonmeta(char * tok0)
166 if (parse_player_command_0arg(tok0))
172 char * tok1 = token_from_line(NULL);
173 if (tok1 && ( parse_player_command_1arg(tok0, tok1)
174 || parse_god_command_1arg(tok0, tok1)))
180 char * tok2 = token_from_line(NULL);
181 if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
187 char * tok3 = token_from_line(NULL);
188 if (tok2 && parse_god_command_3arg(tok0, tok1, tok2, tok3))
200 static uint8_t parse_command_meta(char * tok0)
202 if (!strcmp("QUIT", tok0))
206 if (!strcmp("PING", tok0))
208 send_to_outfile("PONG\n", 1);
211 if (!strcmp("THINGS_HERE", tok0))
213 char * tok1 = token_from_line(NULL);
214 char * tok2 = token_from_line(NULL);
215 if (!parsetest_int(tok1, '8') && !parsetest_int(tok2, '8'))
217 send_to_outfile("THINGS_HERE START\n", 1);
219 for (t = world.things; t; t = t->next)
221 if (t->pos.y == atoi(tok1) && t->pos.x == atoi(tok2))
223 struct ThingType * tt = get_thing_type(t->type);
224 send_to_outfile(tt->name, 0);
225 send_to_outfile("\n", 1);
228 send_to_outfile("THINGS_HERE END\n", 1);
237 static void server_test()
239 char test[10 + 1 + 10 + 1 + 1];
240 FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
241 try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
242 try_fclose(file, __func__);
243 if (strcmp(test, world.server_test))
245 unset_cleanup_flag(CLEANUP_WORLDSTATE);
246 unset_cleanup_flag(CLEANUP_OUT);
247 unset_cleanup_flag(CLEANUP_IN);
248 char * msg = "Server test string in server output file does not match. "
249 "This indicates that the current server process has been "
250 "superseded by another one.";
257 static int16_t * build_whitelist()
259 uint16_t i_things = NULL != world.things;
260 struct Thing * t = world.things;
261 for (; t; t = t->next, i_things++);
262 int16_t * whitelist = try_malloc(i_things * sizeof(int16_t), __func__);
263 for (i_things = 0, t = world.things; t;
264 whitelist[i_things] = t->id, t = t->next, i_things++);
265 whitelist[i_things] = -1;
271 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist)
274 for (i = 0; -1 < whitelist[i]; i++)
276 if ((int16_t) id == whitelist[i])
286 static void turn_over()
288 struct Thing * player = get_player();
289 struct Thing * thing = player;
290 int16_t * whitelist = build_whitelist();
291 while (0 < player->lifepoints)
296 thing = world.things;
298 whitelist = build_whitelist();/* The whitelist excludes things */
299 } /* that appear only during the turn.*/
300 if (thing_in_whitelist(thing->id, whitelist))
302 if (0 < thing->lifepoints)
304 if (0 == thing->command)
313 struct ThingAction * ta = get_thing_action(thing->command);
314 if (thing->progress == ta->effort)
321 try_thing_proliferation(thing);
330 extern void send_to_outfile(char * answer, uint8_t flush)
332 try_fwrite(answer, strlen(answer), 1, world.file_out, __func__);
335 fflush(world.file_out);
341 extern void record(char * msg, uint8_t force)
343 static FILE * file_tmp = NULL;
344 static time_t save_wait = 0;
345 static char * path_tmp;
348 path_tmp = build_temp_path(s[S_PATH_RECORD]);
349 file_tmp = try_fopen(path_tmp, "w", __func__);
350 if (!access(s[S_PATH_RECORD], F_OK))
352 FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
353 uint32_t linemax = textfile_width(file_read);
354 char * line = try_malloc(linemax + 1, __func__);
355 while (try_fgets(line, linemax + 1, file_read, __func__))
357 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
360 try_fclose(file_read, __func__);
365 try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
366 try_fputc('\n', file_tmp, __func__);
368 if (force || time(NULL) > save_wait + 15)
370 save_wait = time(NULL);
372 atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
379 extern uint8_t obey_msg(char * msg, uint8_t obey_state)
381 if (world.is_verbose)
383 exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
385 set_err_line_options("Trouble with message: ", msg, 0);
386 char * msg_copy = strdup(msg);
387 char * tok0 = token_from_line(msg_copy);
391 ret = parse_command_meta(tok0);
392 if (ret || (obey_state < 2 && parse_command_nonmeta(tok0)))
405 char * tokplus = token_from_line(NULL);
406 err_line(!(!tokplus), "Too many arguments, ignoring overflow.");
408 else if (world.replay)
412 else if (!world.replay)
414 err_line(1, "Unknown command/argument or bad number of tokens.");
423 extern uint8_t io_loop(uint8_t obey_state)
427 char * msg = io_round();
431 uint8_t ret = obey_msg(msg, obey_state);
433 if ( (!world.replay && 2 == ret)
434 || ( world.replay && 2 <= ret))