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 <unistd.h> /* access() */
11 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
12 * err_line(), err_line_inc(), parse_val()
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" /* unset_cleanup_flag() */
21 #include "god_commands.h" /* parse_god_command_1arg() */
22 #include "hardcoded_strings.h" /* s */
23 #include "io.h" /* io_round(), save_world() */
24 #include "things.h" /* Thing, get_thing_action_id_by_name(), get_player() */
25 #include "world.h" /* world */
29 /* If "string" and "comparand" match in string, set "c_to_set" to value." */
30 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
31 char * c_to_set, char value);
33 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
34 static uint8_t player_commands_allowed();
36 /* Parse player command "tok0" with no argument to player action, comment on
37 * invalidity of non-zero "tok1" (but do not abort in that case).
39 static uint8_t parse_player_command_0arg(char * tok0, char * tok1);
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" with argument "tok1" and test the line for further
45 * tokens, commenting on their invalidity (but don't abort on finding them).
47 static uint8_t parse_command_1arg(char * tok0, char * tok1);
50 /* Compares first line of server out file to world.server_test, aborts if they
51 * don't match, but not before unsetting the flags deleting files in the server
52 * directory, for in that case those must be assumed to belong to another server
55 static void server_test();
57 /* Run the game world and its inhabitants (and their actions) until the player
58 * avatar is free to receive new commands (or is dead).
60 static void turn_over();
65 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
66 char * c_to_set, char value)
68 if (!strcmp(string, comparand))
78 static uint8_t player_commands_allowed()
82 err_line(1, "No world exists in which to run player commands.");
90 static uint8_t parse_player_command_0arg(char * tok0, char * tok1)
92 struct Thing * player = get_player();
93 if (!strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP]))
95 if (player_commands_allowed())
97 player->command = get_thing_action_id_by_name(tok0);
100 err_line (NULL != tok1, "No arguments expected, ignoring them.");
109 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
111 struct Thing * player = get_player();
112 if ( ( parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
113 || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
114 && player_commands_allowed())
116 player->command = get_thing_action_id_by_name(tok0);
119 else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
122 if (!( set_char_by_string_comparison(tok1, "east", &dir, 'd')
123 || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
124 || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
125 || set_char_by_string_comparison(tok1, "west", &dir, 's')
126 || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
127 || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
132 player->command = get_thing_action_id_by_name(tok0);
144 static uint8_t parse_command_1arg(char * tok0, char * tok1)
146 char * tok2 = token_from_line(NULL);
147 if ( parse_player_command_1arg(tok0, tok1)
148 || parse_god_command_1arg(tok0, tok1));
153 char * err = "But one argument expected, ignoring further arguments.";
154 err_line (NULL != tok2, err);
160 static void server_test()
162 char test[10 + 1 + 10 + 1 + 1];
163 FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
164 try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
165 try_fclose(file, __func__);
166 if (strcmp(test, world.server_test))
168 unset_cleanup_flag(CLEANUP_WORLDSTATE);
169 unset_cleanup_flag(CLEANUP_OUT);
170 unset_cleanup_flag(CLEANUP_IN);
171 char * msg = "Server test string in server output file does not match. "
172 "This indicates that the current server process has been "
173 "superseded by another one.";
180 static void turn_over()
182 struct Thing * player = get_player();
183 struct Thing * thing = player;
184 uint16_t start_turn = world.turn;
185 while ( 0 < player->lifepoints
186 || (0 == player->lifepoints && start_turn == world.turn))
191 thing = world.things;
193 if (0 < thing->lifepoints)
195 if (0 == thing->command)
205 struct ThingAction * ta = get_thing_action(thing->command);
206 if (thing->progress == ta->effort)
219 static void record_msg(char * msg)
222 FILE * file_tmp = atomic_write_start(s[S_PATH_RECORD], &path_tmp);
223 if (!access(s[S_PATH_RECORD], F_OK))
225 FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
226 uint32_t linemax = textfile_width(file_read);
227 char * line = try_malloc(linemax + 1, __func__);
228 while (try_fgets(line, linemax + 1, file_read, __func__))
230 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
233 try_fclose(file_read, __func__);
235 try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
236 try_fputc('\n', file_tmp, __func__);
237 atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
242 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
244 if (world.is_verbose && do_verbose)
246 exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
248 set_err_line_options("Trouble with message: ", msg, 0);
249 char * msg_copy = strdup(msg);
255 char * tok0 = token_from_line(msg_copy);
258 char * tok1 = token_from_line(NULL);
259 if ( parse_player_command_0arg(tok0, tok1)
260 || (tok1 && parse_command_1arg(tok0, tok1)))
275 err_line(1, "Unknown command/argument or bad number of tokens.");
281 extern uint8_t io_loop()
286 char * msg = io_round();
291 if (world.is_verbose)
293 exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
295 if (!strcmp("QUIT", msg))
300 if (!strcmp("PING", msg))
303 char * pong = "PONG\n";
304 try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
305 fflush(world.file_out);