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> /* 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()
20 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
21 * try_fgets(), textfile_width(), try_fputc(),
22 * atomic_write_finish(), build_temp_path()
24 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
25 #include "../common/try_malloc.h" /* try_malloc() */
26 #include "ai.h" /* ai() */
27 #include "cleanup.h" /* unset_cleanup_flag() */
28 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
29 #include "hardcoded_strings.h" /* s */
30 #include "io.h" /* io_round(), save_world() */
31 #include "things.h" /* Thing, ThingType, get_thing_action_id_by_name(),
32 * get_player(), try_thing_proliferation()
34 #include "world.h" /* world */
38 /* If "string" and "comparand" match in string, set "c_to_set" to value." */
39 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
40 char * c_to_set, char value);
42 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
43 static uint8_t player_commands_allowed();
45 /* Parse player commands "tok0" with optional argument "tok1" to player action.
46 * Return 1 on success, 0 on failure.
48 static uint8_t parse_player_command_0arg(char * tok0);
49 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
51 /* Parse/apply (non-)meta command "tok0" (read further tokens as necessary).
52 * Return 0 on failure, 1 on success, 2 on QUIT meta command.
54 static uint8_t parse_command_nonmeta(char * tok0);
55 static uint8_t parse_command_meta(char * tok0);
57 /* Compare 1st line of server out file to world.server_test, abort if they don't
58 * match, but not before unsetting flags deleting files in the server directory,
59 * for in that case those must be assumed to belong to another server process.
61 static void server_test();
63 /* Return array of IDs of non-owned things in game world, ended by non-ID -1. */
64 static int16_t * build_whitelist();
66 /* Return 1 if value of "id" appears in "whitelist", else 0. */
67 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist);
69 /* Run the game world and its inhabitants (and their actions) until the player
70 * avatar is free to receive new commands (or is dead). Only actions and
71 * proliferations for non-owned things are performed that exist at the start of
72 * the turn jumped into, or started anew by the cycle.
74 static void turn_over();
78 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
79 char * c_to_set, char value)
81 if (!strcmp(string, comparand))
91 static uint8_t player_commands_allowed()
95 return !err_line(1, "No world exists in which to run player commands.");
102 static uint8_t parse_player_command_0arg(char * tok0)
104 struct Thing * player = get_player();
105 if ( !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
106 || !strcmp(tok0, s[S_CMD_AI]))
108 if (player_commands_allowed())
110 if (!strcmp(tok0, s[S_CMD_AI]))
116 player->command = get_thing_action_id_by_name(tok0);
128 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
130 struct Thing * player = get_player();
131 if ( ( parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
132 || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
133 && player_commands_allowed())
135 player->command = get_thing_action_id_by_name(tok0);
138 else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
141 if (!( set_char_by_string_comparison(tok1, "east", &dir, 'd')
142 || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
143 || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
144 || set_char_by_string_comparison(tok1, "west", &dir, 's')
145 || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
146 || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
151 player->command = get_thing_action_id_by_name(tok0);
163 static uint8_t parse_command_nonmeta(char * tok0)
165 if (parse_player_command_0arg(tok0))
171 char * tok1 = token_from_line(NULL);
172 if (tok1 && ( parse_player_command_1arg(tok0, tok1)
173 || parse_god_command_1arg(tok0, tok1)))
179 char * tok2 = token_from_line(NULL);
180 if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
186 char * tok3 = token_from_line(NULL);
187 if (tok2 && parse_god_command_3arg(tok0, tok1, tok2, tok3))
199 static uint8_t parse_command_meta(char * tok0)
201 if (!strcmp("QUIT", tok0))
205 if (!strcmp("PING", tok0))
207 send_to_outfile("PONG\n", 1);
210 if (!strcmp("STACK", tok0))
212 send_to_outfile("THINGS_BELOW_PLAYER START\n", 1);
213 struct Thing * player = get_player();
215 for (t = world.things; t; t = t->next)
217 if ( t->pos.y == player->pos.y && t->pos.x == player->pos.x
220 struct ThingType * tt = get_thing_type(t->type);
221 send_to_outfile(tt->name, 0);
222 send_to_outfile("\n", 1);
225 send_to_outfile("THINGS_BELOW_PLAYER END\n", 1);
233 static void server_test()
235 char test[10 + 1 + 10 + 1 + 1];
236 FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
237 try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
238 try_fclose(file, __func__);
239 if (strcmp(test, world.server_test))
241 unset_cleanup_flag(CLEANUP_WORLDSTATE);
242 unset_cleanup_flag(CLEANUP_OUT);
243 unset_cleanup_flag(CLEANUP_IN);
244 char * msg = "Server test string in server output file does not match. "
245 "This indicates that the current server process has been "
246 "superseded by another one.";
253 static int16_t * build_whitelist()
255 uint16_t i_things = NULL != world.things;
256 struct Thing * t = world.things;
257 for (; t; t = t->next, i_things++);
258 int16_t * whitelist = try_malloc(i_things * sizeof(int16_t), __func__);
259 for (i_things = 0, t = world.things; t;
260 whitelist[i_things] = t->id, t = t->next, i_things++);
261 whitelist[i_things] = -1;
267 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist)
270 for (i = 0; -1 < whitelist[i]; i++)
272 if ((int16_t) id == whitelist[i])
282 static void turn_over()
284 struct Thing * player = get_player();
285 struct Thing * thing = player;
286 int16_t * whitelist = build_whitelist();
287 while (0 < player->lifepoints)
292 thing = world.things;
294 whitelist = build_whitelist();/* The whitelist excludes things */
295 } /* that appear only during the turn.*/
296 if (thing_in_whitelist(thing->id, whitelist))
298 if (0 < thing->lifepoints)
300 if (0 == thing->command)
309 struct ThingAction * ta = get_thing_action(thing->command);
310 if (thing->progress == ta->effort)
317 try_thing_proliferation(thing);
326 extern void send_to_outfile(char * answer, uint8_t flush)
328 try_fwrite(answer, strlen(answer), 1, world.file_out, __func__);
331 fflush(world.file_out);
337 extern void record(char * msg, uint8_t force)
339 static FILE * file_tmp = NULL;
340 static time_t save_wait = 0;
341 static char * path_tmp;
344 path_tmp = build_temp_path(s[S_PATH_RECORD]);
345 file_tmp = try_fopen(path_tmp, "w", __func__);
346 if (!access(s[S_PATH_RECORD], F_OK))
348 FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
349 uint32_t linemax = textfile_width(file_read);
350 char * line = try_malloc(linemax + 1, __func__);
351 while (try_fgets(line, linemax + 1, file_read, __func__))
353 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
356 try_fclose(file_read, __func__);
361 try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
362 try_fputc('\n', file_tmp, __func__);
364 if (force || time(NULL) > save_wait + 15)
366 save_wait = time(NULL);
368 atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
375 extern uint8_t obey_msg(char * msg, uint8_t obey_state)
377 if (world.is_verbose)
379 exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
381 set_err_line_options("Trouble with message: ", msg, 0);
382 char * msg_copy = strdup(msg);
383 char * tok0 = token_from_line(msg_copy);
387 ret = parse_command_meta(tok0);
388 if (ret || (obey_state < 2 && parse_command_nonmeta(tok0)))
401 char * tokplus = token_from_line(NULL);
402 err_line(!(!tokplus), "Too many arguments, ignoring overflow.");
404 else if (world.replay)
408 else if (!world.replay)
410 err_line(1, "Unknown command/argument or bad number of tokens.");
419 extern uint8_t io_loop(uint8_t obey_state)
423 char * msg = io_round();
427 uint8_t ret = obey_msg(msg, obey_state);
429 if ( (!world.replay && 2 == ret)
430 || ( world.replay && 2 <= ret))