home · contact · privacy
Define server messages and server message arguments in confclient/commands, instead...
[plomrogue] / src / client / control.c
index 8ab72db11151da6fd2932d5fe6d36bbd50a036b0..56c41fd4b132a34e37cf9c880065ec5af0bb035d 100644 (file)
@@ -4,7 +4,7 @@
 #include <stdint.h> /* uint8_t, uint16_t */
 #include <stdio.h> /* sprintf() */
 #include <string.h> /* strlen() */
-#include "command_db.h" /* get_command_id(), is_command_id_shortdsc() */
+#include "command_db.h" /* get_command_data() */
 #include "io.h" /* try_send() */
 #include "keybindings.h" /* struct KeyBindingDB, get_command_to_keycode(),
                           * get_keycode_to_command(), mod_selected_keyb(),
 
 
 
-/* If "cmd" matches "match" in get_available_keycode_to_command(), execute "f"
- * with provided char arguments and return 1; else only return 0.
+/* If "keycode" matches "match" in get_available_keycode_to_command(), execute
+ * "f" with provided char arguments and return 1; else only return 0.
  */
-static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ());
-static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c);
-static uint8_t try_cmd_2args(int cmd, char * match,
+static uint8_t try_cmd_0args(int keycode, char * match, void (* f) ());
+static uint8_t try_cmd_1args(int keycode, char * match,
+                             void (* f) (char), char c);
+static uint8_t try_cmd_2args(int keycode, char * match,
                              void (* f) (char, char), char c1, char c2);
 
-/* If "command_id" is id of command named "match", send (via try_send()) a string
- * of "match" + " " + the string representation of "arg" to the server.
- */
-static uint8_t try_player_cmd(int command_id, char * match, char * command_name,
-                              uint8_t arg);
-
 /* Return keycode to "command" if it is available in current window config. */
 static uint16_t get_available_keycode_to_command(char * command);
 
@@ -51,9 +46,9 @@ static void wrap_mv_kb_mod(char c1, char c2);
 
 
 
-static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ())
+static uint8_t try_cmd_0args(int keycode, char * match, void (* f) ())
 {
-    if (cmd == get_available_keycode_to_command(match))
+    if (keycode == get_available_keycode_to_command(match))
     {
         f();
         return 1;
@@ -63,9 +58,10 @@ static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ())
 
 
 
-static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c)
+static uint8_t try_cmd_1args(int keycode, char * match,
+                             void (* f) (char), char c)
 {
-    if (cmd == get_available_keycode_to_command(match))
+    if (keycode == get_available_keycode_to_command(match))
     {
         f(c);
         return 1;
@@ -75,10 +71,10 @@ static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c)
 
 
 
-static uint8_t try_cmd_2args(int cmd, char * match,
+static uint8_t try_cmd_2args(int keycode, char * match,
                              void (* f) (char, char), char c1, char c2)
 {
-    if (cmd == get_available_keycode_to_command(match))
+    if (keycode == get_available_keycode_to_command(match))
     {
         f(c1, c2);
         return 1;
@@ -88,23 +84,6 @@ static uint8_t try_cmd_2args(int cmd, char * match,
 
 
 
-static uint8_t try_player_cmd(int command_id, char * match, char * command_name,
-                              uint8_t arg)
-{
-    if (is_command_id_shortdsc(command_id, match))
-    {
-        uint8_t command_size = strlen(command_name);
-        uint8_t arg_size = 3;
-        char msg[command_size + 1 + arg_size + 1];
-        sprintf(msg, "%s %d", command_name, arg);
-        try_send(msg);
-        return 1;
-    }
-    return 0;
-}
-
-
-
 static uint16_t get_available_keycode_to_command(char * command)
 {
     uint16_t keycode = get_keycode_to_command(world.kb_global.kbs, command);
@@ -113,7 +92,7 @@ static uint16_t get_available_keycode_to_command(char * command)
         return keycode;
     }
     struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
-    if (0 == wc->view)
+    if      (0 == wc->view)
     {
         keycode = get_keycode_to_command(wc->kb.kbs, command);
     }
@@ -168,26 +147,26 @@ static void wrap_mv_kb_mod(char c1, char c2)
 
 extern uint8_t player_control(int key)
 {
-    char * command = get_command_to_keycode(world.kb_global.kbs, key);
-    if (NULL == command && 0 != world.wmeta.active)
+    char * cmd = get_command_to_keycode(world.kb_global.kbs, key);
+    if (NULL == cmd && 0 != world.wmeta.active)
     {
         struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
-        command = get_command_to_keycode(wc->kb.kbs, key);
+        cmd = get_command_to_keycode(wc->kb.kbs, key);
     }
-    if (NULL != command)
+    if (NULL != cmd)
     {
-        uint8_t id = get_command_id(command);
-        if (   try_player_cmd(id, "wait", "wait", 0)
-            || try_player_cmd(id, "drop", "drop", world.player_inventory_select)
-            || try_player_cmd(id, "pick", "pick_up", 0)
-            || try_player_cmd(id, "use", "use", world.player_inventory_select)
-            || try_player_cmd(id, "player_u", "move", 'N')
-            || try_player_cmd(id, "player_d", "move", 'S')
-            || try_player_cmd(id, "player_r", "move", 'E')
-            || try_player_cmd(id, "player_l", "move", 'W'))
+        struct Command * command_data = get_command_data(cmd);
+        uint8_t arg = (uint8_t) command_data->arg;
+        if ('i' == arg)
         {
-            return 1;
+            arg = world.player_inventory_select;
         }
+        uint8_t command_size = strlen(command_data->server_msg);
+        uint8_t arg_size = 3;
+        char msg[command_size + 1 + arg_size + 1];
+        sprintf(msg, "%s %d", command_data->server_msg, arg);
+        try_send(msg);
+        return 1;
     }
     return 0;
 }