home · contact · privacy
Use direction names instead of numbers in player move communication.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 11 Jul 2014 23:25:06 +0000 (01:25 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 11 Jul 2014 23:25:06 +0000 (01:25 +0200)
TODO
src/client/control.c
src/server/run.c

diff --git a/TODO b/TODO
index 5123c14d80878e6c522ad79d57dd1958da190bf1..a892e094e48ea54ddeb473ce665008d89ea89aef 100644 (file)
--- 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
index 7ff99f3e75d39f538c6ede99b63431a178e850a3..04f0c94825d234edefd67a2488acb41967d2b7c0 100644 (file)
@@ -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);
         }
index bd054022973d94c5f1d4e2a653414f2ef369be9f..394081c74d7190f3a3b3c4546384a8f92e231cb8 100644 (file)
@@ -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;