shithub: choc

Download patch

ref: dee1ed97a1c031a9c055088c53fd4189d09d81ab
parent: ef848275bcf6dd978499cfc6a28cc81256a82447
author: Simon Howard <fraggle@gmail.com>
date: Sat Oct 15 13:17:43 EDT 2011

Add weapon cycling keys for Heretic.

Subversion-branch: /branches/v2-branch
Subversion-revision: 2430

--- a/src/heretic/g_game.c
+++ b/src/heretic/g_game.c
@@ -165,6 +165,28 @@
     &key_weapon7
 };
 
+// Set to -1 or +1 to switch to the previous or next weapon.
+
+static int next_weapon = 0;
+
+// Used for prev/next weapon keys.
+
+static const struct
+{
+    weapontype_t weapon;
+    weapontype_t weapon_num;
+} weapon_order_table[] = {
+    { wp_staff,       wp_staff },
+    { wp_gauntlets,   wp_staff },
+    { wp_goldwand,    wp_goldwand },
+    { wp_crossbow,    wp_crossbow },
+    { wp_blaster,     wp_blaster },
+    { wp_skullrod,    wp_skullrod },
+    { wp_phoenixrod,  wp_phoenixrod },
+    { wp_mace,        wp_mace },
+    { wp_beak,        wp_beak },
+};
+
 #define SLOWTURNTICS    6
 
 #define NUMKEYS 256
@@ -210,6 +232,51 @@
 }
 */
 
+static boolean WeaponSelectable(weapontype_t weapon)
+{
+    if (weapon == wp_beak)
+    {
+        return false;
+    }
+
+    return players[consoleplayer].weaponowned[weapon];
+}
+
+static int G_NextWeapon(int direction)
+{
+    weapontype_t weapon;
+    int i;
+
+    // Find index in the table.
+
+    if (players[consoleplayer].pendingweapon == wp_nochange)
+    {
+        weapon = players[consoleplayer].readyweapon;
+    }
+    else
+    {
+        weapon = players[consoleplayer].pendingweapon;
+    }
+
+    for (i=0; i<arrlen(weapon_order_table); ++i)
+    {
+        if (weapon_order_table[i].weapon == weapon)
+        {
+            break;
+        }
+    }
+
+    // Switch weapon.
+
+    do
+    {
+        i += direction;
+        i = (i + arrlen(weapon_order_table)) % arrlen(weapon_order_table);
+    } while (!WeaponSelectable(weapon_order_table[i].weapon));
+
+    return weapon_order_table[i].weapon_num;
+}
+
 /*
 ====================
 =
@@ -411,19 +478,34 @@
         dclicks = 0;            // clear double clicks if hit use button
     }
 
-    for (i=0; i<arrlen(weapon_keys); ++i)
-    {
-        int key = *weapon_keys[i];
+    // If the previous or next weapon button is pressed, the
+    // next_weapon variable is set to change weapons when
+    // we generate a ticcmd.  Choose a new weapon.
+    // (Can't weapon cycle when the player is a chicken)
 
-        if (gamekeydown[key])
+    if (players[consoleplayer].chickenTics == 0 && next_weapon != 0)
+    {
+        i = G_NextWeapon(next_weapon);
+        cmd->buttons |= BT_CHANGE;
+        cmd->buttons |= i << BT_WEAPONSHIFT;
+    }
+    else
+    {
+        for (i=0; i<arrlen(weapon_keys); ++i)
         {
-	    cmd->buttons |= BT_CHANGE; 
-	    cmd->buttons |= i<<BT_WEAPONSHIFT; 
-	    break; 
+            int key = *weapon_keys[i];
+
+            if (gamekeydown[key])
+            {
+                cmd->buttons |= BT_CHANGE; 
+                cmd->buttons |= i<<BT_WEAPONSHIFT; 
+                break; 
+            }
         }
     }
-    
 
+    next_weapon = 0;
+
 //
 // mouse
 //
@@ -674,6 +756,15 @@
     if (ev->type == ev_mouse)
     {
         testcontrols_mousespeed = abs(ev->data2);
+    }
+
+    if (ev->type == ev_keydown && ev->data1 == key_prevweapon)
+    {
+        next_weapon = -1;
+    }
+    else if (ev->type == ev_keydown && ev->data1 == key_nextweapon)
+    {
+        next_weapon = 1;
     }
 
     switch (ev->type)