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() */
9 #include <string.h> /* strlen(), strcmp(), strncmp(), strdup() */
10 #include <time.h> /* time_t, time() */
11 #include <unistd.h> /* access() */
12 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
13 * err_line(), err_line_inc(), parse_val()
15 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
16 * try_fgets(), textfile_width(), try_fputc(),
17 * atomic_write_finish(), build_temp_path()
19 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
20 #include "../common/try_malloc.h" /* try_malloc() */
21 #include "ai.h" /* ai() */
22 #include "cleanup.h" /* unset_cleanup_flag() */
23 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
24 #include "hardcoded_strings.h" /* s */
25 #include "io.h" /* io_round(), save_world() */
26 #include "things.h" /* Thing, get_thing_action_id_by_name(), get_player() */
27 #include "world.h" /* world */
31 /* If "string" and "comparand" match in string, set "c_to_set" to value." */
32 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
33 char * c_to_set, char value);
35 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
36 static uint8_t player_commands_allowed();
38 /* Parse player command "tok0" with no argument to player action. */
39 static uint8_t parse_player_command_0arg(char * tok0);
41 /* Parse player command "tok0" with one argument "tok1" to player action. */
42 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
44 /* Parse/apply command "tok0" (read further tokens as necessary). */
45 static uint8_t parse_command(char * tok0);
47 /* Compares first line of server out file to world.server_test, aborts if they
48 * don't match, but not before unsetting the flags deleting files in the server
49 * directory, for in that case those must be assumed to belong to another server
52 static void server_test();
54 /* Run the game world and its inhabitants (and their actions) until the player
55 * avatar is free to receive new commands (or is dead).
57 static void turn_over();
61 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
62 char * c_to_set, char value)
64 if (!strcmp(string, comparand))
74 static uint8_t player_commands_allowed()
78 err_line(1, "No world exists in which to run player commands.");
86 static uint8_t parse_player_command_0arg(char * tok0)
88 struct Thing * player = get_player();
89 if ( !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
90 || !strcmp(tok0, s[S_CMD_AI]))
92 if (player_commands_allowed())
94 if (!strcmp(tok0, s[S_CMD_AI]))
100 player->command = get_thing_action_id_by_name(tok0);
112 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
114 struct Thing * player = get_player();
115 if ( ( parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
116 || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
117 && player_commands_allowed())
119 player->command = get_thing_action_id_by_name(tok0);
122 else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
125 if (!( set_char_by_string_comparison(tok1, "east", &dir, 'd')
126 || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
127 || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
128 || set_char_by_string_comparison(tok1, "west", &dir, 's')
129 || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
130 || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
135 player->command = get_thing_action_id_by_name(tok0);
147 static uint8_t parse_command(char * tok0)
149 if (parse_player_command_0arg(tok0))
155 char * tok1 = token_from_line(NULL);
156 if (tok1 && ( parse_player_command_1arg(tok0, tok1)
157 || parse_god_command_1arg(tok0, tok1)))
163 char * tok2 = token_from_line(NULL);
164 if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
170 char * tok3 = token_from_line(NULL);
171 if (tok2 && parse_god_command_3arg(tok0, tok1, tok2, tok3))
183 static void server_test()
185 char test[10 + 1 + 10 + 1 + 1];
186 FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
187 try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
188 try_fclose(file, __func__);
189 if (strcmp(test, world.server_test))
191 unset_cleanup_flag(CLEANUP_WORLDSTATE);
192 unset_cleanup_flag(CLEANUP_OUT);
193 unset_cleanup_flag(CLEANUP_IN);
194 char * msg = "Server test string in server output file does not match. "
195 "This indicates that the current server process has been "
196 "superseded by another one.";
203 static void turn_over()
205 struct Thing * player = get_player();
206 struct Thing * thing = player;
207 uint16_t start_turn = world.turn;
208 while ( 0 < player->lifepoints
209 || (0 == player->lifepoints && start_turn == world.turn))
214 thing = world.things;
216 if (0 < thing->lifepoints)
218 if (0 == thing->command)
227 struct ThingAction * ta = get_thing_action(thing->command);
228 if (thing->progress == ta->effort)
241 extern void record(char * msg, uint8_t force)
243 static FILE * file_tmp = NULL;
244 static time_t save_wait = 0;
245 static char * path_tmp;
248 path_tmp = build_temp_path(s[S_PATH_RECORD]);
249 file_tmp = try_fopen(path_tmp, "w", __func__);
250 if (!access(s[S_PATH_RECORD], F_OK))
252 FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
253 uint32_t linemax = textfile_width(file_read);
254 char * line = try_malloc(linemax + 1, __func__);
255 while (try_fgets(line, linemax + 1, file_read, __func__))
257 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
260 try_fclose(file_read, __func__);
265 try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
266 try_fputc('\n', file_tmp, __func__);
268 if (force || time(NULL) > save_wait + 15)
270 save_wait = time(NULL);
272 atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
279 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
281 if (world.is_verbose && do_verbose)
283 exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
285 set_err_line_options("Trouble with message: ", msg, 0);
286 char * msg_copy = strdup(msg);
287 char * tok0 = token_from_line(msg_copy);
291 if (parse_command(tok0))
301 char * tokplus = token_from_line(NULL);
302 err_line(!(!tokplus), "Too many arguments, ignoring overflow.");
307 err_line(1, "Unknown command/argument or bad number of tokens.");
313 extern uint8_t io_loop()
317 char * msg = io_round();
323 if (world.is_verbose)
325 exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
327 if (!strcmp("QUIT", msg))
332 if (!strcmp("PING", msg))
335 char * pong = "PONG\n";
336 try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
337 fflush(world.file_out);