*/
static uint8_t try_client_commands(struct Command * command);
+/* If c == c_to_match, set "string" to "string_to_set". */
+static uint8_t set_string_if_char_match(char c, char c_to_match,
+ char ** string, char * string_to_set);
+
+/* Transform "command" to server command + argument string (free externally). */
+static char * build_server_message_with_argument(struct Command * command);
+
/* Try out "command" as one for server messaging; sending is .server_msg,
* followed by either a string representing "command"'s .arg, or, if .arg is
* 'i', world.player_inventory_select, or, if .arg is '0', nothing. Return 1 on
+static uint8_t set_string_if_char_match(char c, char c_to_match,
+ char ** string, char * string_to_set)
+{
+ if (c == c_to_match)
+ {
+ *string = string_to_set;
+ return 1;
+ }
+ return 0;
+}
+
+
+
+static char * build_server_message_with_argument(struct Command * cmd)
+{
+ char * f_name = "build_server_message_with_argument()";
+ uint8_t command_size = strlen(cmd->server_msg);
+ char * arg_str;
+ uint8_t arg_size;
+ if ('i' == cmd->arg)
+ {
+ arg_size = 3;
+ arg_str = try_malloc(arg_size + 1, f_name);
+ int test = sprintf(arg_str, "%d",world.player_inventory_select);
+ exit_trouble(test < 0, f_name, "sprintf()");
+ }
+ else if ( set_string_if_char_match(cmd->arg, 'd', &arg_str, "east")
+ || set_string_if_char_match(cmd->arg, 'c', &arg_str, "south-east")
+ || set_string_if_char_match(cmd->arg, 'x', &arg_str, "south-west")
+ || set_string_if_char_match(cmd->arg, 's', &arg_str, "west")
+ || set_string_if_char_match(cmd->arg, 'w', &arg_str, "north-west")
+ || set_string_if_char_match(cmd->arg, 'e', &arg_str, "north-east"))
+ {
+ arg_size = strlen(arg_str);
+ }
+ else
+ {
+ exit_err(1, "Illegal server command argument.");
+ }
+ char * msg = try_malloc(command_size + 1 + arg_size + 1, f_name);
+ int test = sprintf(msg, "%s %s", cmd->server_msg, arg_str);
+ exit_trouble(test < 0, f_name, "sprintf()");
+ if ('i' == cmd->arg)
+ {
+ free(arg_str);
+ }
+ return msg;
+}
+
+
+
static uint8_t try_server_commands(struct Command * command)
{
- char * f_name = "try_server_commands()";
if (command->server_msg)
{
- uint8_t arg = (uint8_t) command->arg;
- if ('0' == arg)
+ if ('0' == command->arg)
{
send(command->server_msg);
}
else
{
- if ('i' == arg)
- {
- arg = world.player_inventory_select;
- }
- uint8_t command_size = strlen(command->server_msg);
- uint8_t arg_size = 3;
- char * msg = try_malloc(command_size + 1 + arg_size + 1, f_name);
- int test = sprintf(msg, "%s %d", command->server_msg, arg);
- exit_trouble(test < 0, f_name, "sprintf()");
+ char * msg = build_server_message_with_argument(command);
send(msg);
free(msg);
}
/* Parse/apply god command in "tok0"/"tok1" manipulating a thing's state. */
static uint8_t parse_thing_manipulation(char * tok0, char * tok1);
-/* Parse player command "tok0" with one arguments "tok1" to player action. */
-static uint8_t parse_player_command_0arg(char * tok0, char * tok1);
-
/* Parse player command "tok0" with no argument to player action, comment on
* invalidity of non-zero "tok1" (but do not abort in that case).
*/
+static uint8_t parse_player_command_0arg(char * tok0, char * tok1);
+
+/* If "string" and "comparand" match in string, set "c_to_set" to value." */
+static uint8_t set_char_by_string_comparison(char * string, char * comparand,
+ char * c_to_set, char value);
+
+/* Parse player command "tok0" with one argument "tok1" to player action. */
static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
/* Parse/apply commadn "tok0" with argument "tok1" and test the line for further
+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 parse_player_command_1arg(char * tok0, char * tok1)
{
struct Thing * player = get_player();
- if ( parse_val(tok0, tok1, s[S_CMD_MOVE], '8', (char *) &player->arg)
- || parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
+ 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->command = get_thing_action_id_by_name(tok0);
turn_over();
}
+ else if (!strcmp(tok0, s[S_CMD_MOVE]))
+ {
+ 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;