home · contact · privacy
Removed redundancy between record_control() and player_control() by re-writing their...
authorChristian Heller <c.heller@plomlompom.de>
Tue, 5 Nov 2013 02:47:24 +0000 (03:47 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 5 Nov 2013 02:47:24 +0000 (03:47 +0100)
src/control.c
src/control.h
src/keybindings.c
src/keybindings.h
src/main.c

index c7c7b1b6fb4fff949e9e8a24ec7410c0a826a18a..287c099c875d89f2be010012726aed34c1d687b5 100644 (file)
@@ -52,7 +52,7 @@ extern uint16_t get_available_keycode_to_action(char * name)
 
 
 
-extern void record_control(int action)
+extern uint8_t player_control_by_id(int action)
 {
     if      (is_command_id_shortdsc(action, "wait"))
     {
@@ -82,45 +82,29 @@ extern void record_control(int action)
     {
         player_pick();
     }
+    else
+    {
+        return 0;
+    }
+    return 1;
 }
 
 
 
-extern uint8_t player_control(int key)
+extern uint8_t player_control_by_key(int key)
 {
-    if      (key == get_available_keycode_to_action("wait"))
-    {
-        player_wait();
-    }
-    else if (key == get_available_keycode_to_action("player_u"))
-    {
-        move_player(NORTH);
-    }
-    else if (key == get_available_keycode_to_action("player_r"))
-    {
-        move_player(EAST);
-    }
-    else if (key == get_available_keycode_to_action("player_d"))
+    char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
+    if (NULL == action_name && 0 != world.wmeta->active)
     {
-        move_player(SOUTH);
-    }
-    else if (key == get_available_keycode_to_action("player_l"))
-    {
-        move_player(WEST);
-    }
-    else if (key == get_available_keycode_to_action("drop"))
-    {
-        player_drop();
+        struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
+        action_name = get_func_to_keycode(wc->kb.kbs, key);
     }
-    else if (key == get_available_keycode_to_action("pick"))
+    if (NULL != action_name)
     {
-        player_pick();
+        uint8_t action_id = get_command_id(action_name);
+        return player_control_by_id(action_id);
     }
-    else
-    {
-        return 0;
-    }
-    return 1;
+    return 0;
 }
 
 
index 889bdbfd366394f1d7a44e29eed7853b8fde05c4..492ac88e3811831661774e9efb612b6951e29d1a 100644 (file)
@@ -15,10 +15,11 @@ extern uint16_t get_available_keycode_to_action(char * name);
 
 
 
-/* Control the player character, either via recorded "action" or pressed "key".
+/* Control the player character, either via action id "action" or pressed "key".
+ * Return 1 on success, 0 if no appropriate action to trigger was found.
  */
-extern void record_control(int action);
-extern uint8_t player_control(int key);
+extern uint8_t player_control_by_id(int action);
+extern uint8_t player_control_by_key(int key);
 
 
 
index 709813c89a98a6d98832a37085acaf013c4ff991..52c6c7077a8e8b0ed67b33ed0d293748bf42e0f5 100644 (file)
@@ -37,6 +37,21 @@ static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
 
 
 
+extern char * get_func_to_keycode(struct KeyBinding * kb_p, uint16_t key)
+{
+    while (0 != kb_p)
+    {
+        if (key == kb_p->key)
+        {
+            return kb_p->name;
+        }
+        kb_p = kb_p->next;
+    }
+    return NULL;
+}
+
+
+
 extern uint16_t get_keycode_to_action(struct KeyBinding * kb_p, char * name)
 {
     while (0 != kb_p)
index 3d1b75e73ae619ec055531682ab0205e6caed628..b2e74c07761c7de3f9b226e17cdba058ff1d6d2b 100644 (file)
@@ -30,6 +30,9 @@ struct KeyBiData
 
 
 
+/* Return name of action / functionality coupled to keycode; NULL on failure. */
+extern char * get_func_to_keycode(struct KeyBinding * kb_p, uint16_t key);
+
 /* Return keycode matched by keybinding to command of "name". */
 extern uint16_t get_keycode_to_action(struct KeyBinding * keybindings,
                                       char * name);
index 88264072da69f3673bda0b27e2e84b01a11e8ae3..adf093f0e44dfc64a8bc13dd102754cd4c907981 100644 (file)
@@ -26,7 +26,9 @@
 #include "rrand.h" /* for rrand(), rrand_seed() */
 #include "rexit.h" /* for exit_game(), exit_err() */
 #include "command_db.h" /* for init_command_db(), is_command_id_shortdsc() */
-#include "control.h" /* for *_control(), get_available_keycode_to_action() */
+#include "control.h" /* for control_by_id(), player_control(),
+                      * get_available_keycode_to_action()
+                      */
 
 
 
@@ -184,7 +186,7 @@ int main(int argc, char *argv[])
                 {
                     world.inventory_select = getc(file);
                 }
-                record_control(action);
+                player_control_by_id(action);
             }
         }
         while (1)
@@ -207,7 +209,7 @@ int main(int argc, char *argv[])
                     {
                         world.inventory_select = getc(file);
                     }
-                    record_control(action);
+                    player_control_by_id(action);
                 }
             }
             else if (meta_control(key))
@@ -227,22 +229,12 @@ int main(int argc, char *argv[])
             draw_all_wins(world.wmeta);
             key = getch();
             wc = get_winconf_by_win(world.wmeta->active);
-            if      (1 == wc->view && wingeom_control(key))
-            {
-                continue;
-            }
-            else if (2 == wc->view && winkeyb_control(key))
-            {
-                continue;
-            }
-
             if  (   (1 == wc->view && wingeom_control(key))
                  || (2 == wc->view && winkeyb_control(key))
-                 || (0 != player->lifepoints && player_control(key)))
+                 || (0 != player->lifepoints && player_control_by_key(key)))
             {
                 continue;
             }
-
             if (meta_control(key))
             {
                 exit_game();