From: Christian Heller Date: Fri, 11 Jul 2014 23:25:06 +0000 (+0200) Subject: Use direction names instead of numbers in player move communication. X-Git-Tag: tce~709 X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Byoutube_prefix%7D%7D%7B%7Bresult.id%7D%7D?a=commitdiff_plain;h=aaa8274f5566c6f1a16bb3c8971f422ff06d0e37;p=plomrogue Use direction names instead of numbers in player move communication. --- diff --git a/TODO b/TODO index 5123c14..a892e09 100644 --- a/TODO +++ b/TODO @@ -9,8 +9,6 @@ BOTH SERVER/CLIENT: - make server and client communicate by specific world state info requests in server/out, replacing server/worldstate -- beautify form of command arguments (strings instead of numbers for directions) - SERVER: - consider @@ -26,3 +24,5 @@ CLIENT: - enable toggling of window borders - make log scrollable + +- re-work unnecessary complex command / keybinding / server message mapping diff --git a/src/client/control.c b/src/client/control.c index 7ff99f3..04f0c94 100644 --- a/src/client/control.c +++ b/src/client/control.c @@ -40,6 +40,13 @@ static uint8_t try_2args(struct Command * command, char * match, */ 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 @@ -157,27 +164,68 @@ static uint8_t try_client_commands(struct Command * command) +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); } diff --git a/src/server/run.c b/src/server/run.c index bd05402..394081c 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -52,12 +52,16 @@ static uint8_t parse_do_fov(char * tok0, char * tok1); /* 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 @@ -248,16 +252,45 @@ static uint8_t parse_player_command_0arg(char * tok0, char * tok1) +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;