+
+
+static uint8_t set_char_by_string_comparison(char * string, char * comparand,
+ char * c_to_set, char value)
+{
+ if (!strcmp(string, comparand))
+ {
+ * c_to_set = value;
+ return 1;
+ }
+ return 0;
+}
+
+
+
+static uint8_t player_commands_allowed()
+{
+ if (!world.exists)
+ {
+ err_line(1, "No world exists in which to run player commands.");
+ return 0;
+ }
+ return 1;
+}
+
+
+
+static uint8_t parse_player_command_0arg(char * tok0)
+{
+ struct Thing * player = get_player();
+ if ( !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
+ || !strcmp(tok0, s[S_CMD_AI]))
+ {
+ if (player_commands_allowed())
+ {
+ if (!strcmp(tok0, s[S_CMD_AI]))
+ {
+ ai(player);
+ }
+ else
+ {
+ player->command = get_thing_action_id_by_name(tok0);
+ player->arg = 0;
+ }
+ turn_over();
+ }
+ return 1;
+ }
+ return 0;
+}
+
+
+
+static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
+{
+ struct Thing * player = get_player();
+ if ( ( parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
+ || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
+ && player_commands_allowed())
+ {
+ player->command = get_thing_action_id_by_name(tok0);
+ turn_over();
+ }
+ else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
+ {
+ char dir = '\0';
+ if (!( set_char_by_string_comparison(tok1, "east", &dir, 'd')
+ || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
+ || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
+ || set_char_by_string_comparison(tok1, "west", &dir, 's')
+ || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
+ || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
+ {
+ return 0;
+ }
+ player->arg = dir;
+ player->command = get_thing_action_id_by_name(tok0);
+ turn_over();
+ }
+ else
+ {
+ return 0;
+ }
+ return 1;
+}
+
+
+
+static uint8_t parse_command(char * tok0)
+{
+ if (parse_player_command_0arg(tok0))
+ {
+ return 1;
+ }
+ else
+ {
+ char * tok1 = token_from_line(NULL);
+ if (tok1 && ( parse_player_command_1arg(tok0, tok1)
+ || parse_god_command_1arg(tok0, tok1)))
+ {
+ return 1;
+ }
+ else
+ {
+ char * tok2 = token_from_line(NULL);
+ if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
+ {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+
+
+static void server_test()
+{
+ char test[10 + 1 + 10 + 1 + 1];
+ FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
+ try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
+ try_fclose(file, __func__);
+ if (strcmp(test, world.server_test))
+ {
+ unset_cleanup_flag(CLEANUP_WORLDSTATE);
+ unset_cleanup_flag(CLEANUP_OUT);
+ unset_cleanup_flag(CLEANUP_IN);
+ char * msg = "Server test string in server output file does not match. "
+ "This indicates that the current server process has been "
+ "superseded by another one.";
+ exit_err(1, msg);
+ }
+}