]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/weapons/weapon.qh
#pragma once
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon.qh
index 75e00aa39716419201ef986ea313f54384c8c8aa..b2ede3c503596bc19d1a8e3c173a91ac36bc4859 100644 (file)
@@ -1,8 +1,48 @@
-#ifndef WEAPON_H
-#define WEAPON_H
+#pragma once
 
-bool w_new(entity this, int req);
+#include <common/items/item/pickup.qh>
+#include <common/stats.qh>
 
+#ifdef SVQC
+#include <common/effects/qc/all.qh>
+#endif
+
+const int MAX_WEAPONSLOTS = 2;
+.entity weaponentities[MAX_WEAPONSLOTS];
+
+int weaponslot(.entity weaponentity)
+{
+       for (int i = 0; i < MAX_WEAPONSLOTS; ++i)
+       {
+               if (weaponentities[i] == weaponentity)
+               {
+                       return i;
+               }
+       }
+       return 0;
+}
+
+// weapon states (actor.(weaponentity).state)
+/** no weapon selected */
+const int WS_CLEAR  = 0;
+/** raise frame */
+const int WS_RAISE  = 1;
+/** deselecting frame */
+const int WS_DROP   = 2;
+/** fire state */
+const int WS_INUSE  = 3;
+/** idle frame */
+const int WS_READY  = 4;
+
+#ifdef SVQC
+.int ammo_shells;
+.int ammo_nails;
+.int ammo_rockets;
+.int ammo_cells;
+.int ammo_plasma = _STAT(PLASMA);
+.int ammo_fuel = _STAT(FUEL);
+.int ammo_none;
+#else
 .int ammo_shells;
 .int ammo_nails;
 .int ammo_rockets;
@@ -10,55 +50,13 @@ bool w_new(entity this, int req);
 .int ammo_plasma;
 .int ammo_fuel;
 .int ammo_none;
-
-// weapon requests
-const int WR_SETUP          =  1; // (SERVER) setup weapon data
-.bool(entity this) wr_setup;
-/** (SERVER) logic to run every frame */
-.bool(entity this, bool fire1, bool fire2) wr_think;
-const int WR_CHECKAMMO1     =  3; // (SERVER) checks ammo for weapon primary
-.bool(entity this) wr_checkammo1;
-const int WR_CHECKAMMO2     =  4; // (SERVER) checks ammo for weapon second
-.bool(entity this) wr_checkammo2;
-const int WR_AIM            =  5; // (SERVER) runs bot aiming code for this weapon
-.bool(entity this) wr_aim;
-const int WR_INIT           =  6; // (BOTH)   precaches models/sounds used by this weapon, also sets up weapon properties
-.bool(entity this) wr_init;
-const int WR_SUICIDEMESSAGE =  7; // (SERVER) notification number for suicide message (may inspect w_deathtype for details)
-.bool(entity this) wr_suicidemessage;
-const int WR_KILLMESSAGE    =  8; // (SERVER) notification number for kill message (may inspect w_deathtype for details)
-.bool(entity this) wr_killmessage;
-const int WR_RELOAD         =  9; // (SERVER) handles reloading for weapon
-.bool(entity this) wr_reload;
-const int WR_RESETPLAYER    = 10; // (SERVER) clears fields that the weapon may use
-.bool(entity this) wr_resetplayer;
-const int WR_IMPACTEFFECT   = 11; // (CLIENT) impact effect for weapon explosion
-.bool(entity this) wr_impacteffect;
-const int WR_PLAYERDEATH    = 12; // (SERVER) called whenever a player dies
-.bool(entity this) wr_playerdeath;
-const int WR_GONETHINK      = 13; // (SERVER) logic to run when weapon is lost
-.bool(entity this) wr_gonethink;
-const int WR_CONFIG         = 14; // (ALL)    dump weapon cvars to config in data directory (see: sv_cmd dumpweapons)
-.bool(entity this) wr_config;
-const int WR_ZOOMRETICLE    = 15; // (CLIENT) weapon specific zoom reticle
-.bool(entity this) wr_zoomreticle;
-const int WR_DROP           = 16; // (SERVER) the weapon is dropped
-.bool(entity this) wr_drop;
-const int WR_PICKUP         = 17; // (SERVER) a weapon is picked up
-.bool(entity this) wr_pickup;
+#endif
 
 /** fields which are explicitly/manually set are marked with "M", fields set automatically are marked with "A" */
 CLASS(Weapon, Object)
        ATTRIB(Weapon, m_id, int, 0)
-    /**
-     * M: WEP_id    : WEP_...
-     * you can recognize dummies when this == 0
-     */
-    ATTRIB(Weapon, weapon, int, 0);
     /** A: WEPSET_id : WEPSET_... */
     ATTRIB(Weapon, weapons, WepSet, '0 0 0');
-    /** M: function  : w_... */
-    METHOD(Weapon, weapon_func, bool(entity this, int req)) { return w_new(this, req); }
     /** M: ammotype  : main ammo field */
     ATTRIB(Weapon, ammo_field, .int, ammo_none);
     /** M: impulse   : weapon impulse */
@@ -69,8 +67,6 @@ CLASS(Weapon, Object)
     ATTRIB(Weapon, bot_pickupbasevalue, float, 0);
     /** M: color     : waypointsprite color */
     ATTRIB(Weapon, wpcolor, vector, '0 0 0');
-    /** A: wpn-id    : wpn- sprite name */
-    ATTRIB(Weapon, wpmodel, string, "");
     /** M: modelname : name of model (without g_ v_ or h_ prefixes) */
     ATTRIB(Weapon, mdl, string, "");
     /** M: model MDL_id_ITEM */
@@ -84,53 +80,91 @@ CLASS(Weapon, Object)
     /** M: refname   : reference name name */
     ATTRIB(Weapon, netname, string, "");
     /** M: wepname   : human readable name */
-    ATTRIB(Weapon, message, string, "AOL CD Thrower");
-
+    ATTRIB(Weapon, m_name, string, "AOL CD Thrower");
+
+    ATTRIB(Weapon, m_pickup, entity, NULL);
+
+    /** (SERVER) setup weapon data */
+    METHOD(Weapon, wr_setup, void(Weapon this, entity actor)) {}
+    /** (SERVER) logic to run every frame */
+    METHOD(Weapon, wr_think, void(Weapon this, entity actor, .entity weaponentity, int fire)) {}
+    /** (SERVER) checks ammo for weapon primary */
+    METHOD(Weapon, wr_checkammo1, bool(Weapon this, entity actor)) {return false;}
+    /** (SERVER) checks ammo for weapon second */
+    METHOD(Weapon, wr_checkammo2, bool(Weapon this, entity actor)) {return false;}
+    /** (SERVER) runs bot aiming code for this weapon */
+    METHOD(Weapon, wr_aim, void(Weapon this, entity actor)) {}
+    /** (BOTH)   precaches models/sounds used by this weapon, also sets up weapon properties */
+    METHOD(Weapon, wr_init, void(Weapon this)) {}
+    /** (SERVER) notification number for suicide message (may inspect w_deathtype for details) */
+    METHOD(Weapon, wr_suicidemessage, entity(Weapon this)) {return NULL;}
+    /** (SERVER) notification number for kill message (may inspect w_deathtype for details) */
+    METHOD(Weapon, wr_killmessage, entity(Weapon this)) {return NULL;}
+    /** (SERVER) handles reloading for weapon */
+    METHOD(Weapon, wr_reload, void(Weapon this, entity actor, .entity weaponentity)) {}
+    /** (SERVER) clears fields that the weapon may use */
+    METHOD(Weapon, wr_resetplayer, void(Weapon this, entity actor)) {}
+    /** (CLIENT) impact effect for weapon explosion */
+    METHOD(Weapon, wr_impacteffect, void(Weapon this, entity actor)) {}
+    /** (SERVER) called whenever a player dies */
+    METHOD(Weapon, wr_playerdeath, void(Weapon this, entity actor)) {}
+    /** (SERVER) logic to run when weapon is lost */
+    METHOD(Weapon, wr_gonethink, void(Weapon this, entity actor)) {}
+    /** (ALL)    dump weapon cvars to config in data directory (see: sv_cmd dumpweapons) */
+    METHOD(Weapon, wr_config, void(Weapon this)) {}
+    /** (CLIENT) weapon specific zoom reticle */
+    METHOD(Weapon, wr_zoomreticle, bool(Weapon this)) {
+        // no weapon specific image for this weapon
+        return false;
+    }
+    /** (CLIENT) weapon specific glow */
+    METHOD(Weapon, wr_glow, vector(Weapon this)) { return '0 0 0'; }
+    /** (SERVER) the weapon is dropped */
+    METHOD(Weapon, wr_drop, void(Weapon this, entity actor)) {}
+    /** (SERVER) a weapon is picked up */
+    METHOD(Weapon, wr_pickup, void(Weapon this, entity actor)) {}
+    /** (SERVER) update cvar based properties */
+    METHOD(Weapon, wr_update, void(Weapon this)) {}
        METHOD(Weapon, display, void(entity this, void(string name, string icon) returns)) {
-               returns(this.message, this.model2 ? sprintf("/gfx/hud/%s/%s", cvar_string("menu_skin"), this.model2) : string_null);
-       }
-
-       CONSTRUCTOR(Weapon,
-               bool(entity this, int req) function,
-               .int ammotype,
-               int i,
-               int weapontype,
-               float pickupbasevalue,
-               vector clr,
-               string modelname,
-               entity m,
-               string crosshair,
-               string wepimg,
-               string refname,
-               string wepname
-       ) {
-               CONSTRUCT(Weapon);
-               this.weapon_func = function;
-               this.ammo_field = ammotype;
-               this.impulse = i;
-               this.spawnflags = weapontype;
-               this.bot_pickupbasevalue = pickupbasevalue;
-               this.wpcolor = clr;
-               this.mdl = modelname;
-               this.m_model = m;
-               this.w_crosshair = strzone(car(crosshair));
-               string s = cdr(crosshair);
-               this.w_crosshair_size = ((s != "") ? stof(s) : 1); // so that we can scale the crosshair from code (for compat)
-               this.model2 = strzone(wepimg);
-               this.netname = refname;
-               this.message = wepname;
-       }
-       void register_weapon(entity this, int id, WepSet bit)
-       {
-               this.weapon = id;
-               this.weapons = bit;
-               this.wpmodel = strzone(strcat("wpn-", ftos(id)));
-               #ifdef CSQC
-               this.weapon_func(this, WR_INIT);
-               #endif
+               returns(this.m_name, this.model2 ? sprintf("/gfx/hud/%s/%s", cvar_string("menu_skin"), this.model2) : string_null);
        }
 ENDCLASS(Weapon)
 
+#include <common/items/all.qh>
+CLASS(WeaponPickup, Pickup)
+    ATTRIB(WeaponPickup, m_weapon, Weapon, NULL)
+    ATTRIB(WeaponPickup, m_name, string, string_null)
+#ifndef MENUQC
+    ATTRIB(WeaponPickup, m_sound, Sound, SND_WEAPONPICKUP)
+#endif
+#ifdef SVQC
+    ATTRIB(WeaponPickup, m_itemflags, int, FL_WEAPON)
+    float weapon_pickupevalfunc(entity player, entity item);
+    ATTRIB(WeaponPickup, m_pickupevalfunc, float(entity player, entity item), weapon_pickupevalfunc)
+#endif
+    CONSTRUCTOR(WeaponPickup, Weapon w) {
+        CONSTRUCT(WeaponPickup);
+        this.m_weapon = w;
+        this.m_name = w.m_name;
+#ifndef MENUQC
+        this.m_model = w.m_model;
+#endif
+#ifdef SVQC
+        this.m_botvalue = w.bot_pickupbasevalue;
+#endif
+    }
+#ifdef SVQC
+    METHOD(WeaponPickup, giveTo, bool(entity this, entity item, entity player))
+    {
+        bool b = Item_GiveTo(item, player);
+        if (b) {
+            LOG_TRACEF("entity %i picked up %s\n", player, this.m_name);
+        }
+        return b;
+    }
+#endif
+ENDCLASS(WeaponPickup)
+
 CLASS(OffhandWeapon, Object)
     METHOD(OffhandWeapon, offhand_think, void(OffhandWeapon this, entity player, bool key_pressed)) {}
 ENDCLASS(OffhandWeapon)
@@ -157,26 +191,8 @@ const int WEP_FLAG_HIDDEN         =  0x40; // hides from menu
 const int WEP_FLAG_RELOADABLE     =  0x80; // can has reload
 const int WEP_FLAG_SUPERWEAPON    = 0x100; // powerup timer
 const int WEP_FLAG_MUTATORBLOCKED = 0x200; // hides from impulse 99 etc. (mutators are allowed to clear this flag)
-
-bool w_new(entity this, int req) {
-    if (req == WR_SETUP) return this.wr_setup ? this.wr_setup(this) : false;
-    if (req == WR_CHECKAMMO1) return this.wr_checkammo1 ? this.wr_checkammo1(this) : false;
-    if (req == WR_CHECKAMMO2) return this.wr_checkammo2 ? this.wr_checkammo2(this) : false;
-    if (req == WR_AIM) return this.wr_aim ? this.wr_aim(this) : false;
-    if (req == WR_INIT) return this.wr_init ? this.wr_init(this) : false;
-    if (req == WR_SUICIDEMESSAGE) return this.wr_suicidemessage ? this.wr_suicidemessage(this) : false;
-    if (req == WR_KILLMESSAGE) return this.wr_killmessage ? this.wr_killmessage(this) : false;
-    if (req == WR_RELOAD) return this.wr_reload ? this.wr_reload(this) : false;
-    if (req == WR_RESETPLAYER) return this.wr_resetplayer ? this.wr_resetplayer(this) : false;
-    if (req == WR_IMPACTEFFECT) return this.wr_impacteffect ? this.wr_impacteffect(this) : false;
-    if (req == WR_PLAYERDEATH) return this.wr_playerdeath ? this.wr_playerdeath(this) : false;
-    if (req == WR_GONETHINK) return this.wr_gonethink ? this.wr_gonethink(this) : false;
-    if (req == WR_CONFIG) return this.wr_config ? this.wr_config(this) : false;
-    if (req == WR_ZOOMRETICLE) return this.wr_zoomreticle ? this.wr_zoomreticle(this) : false;
-    if (req == WR_DROP) return this.wr_drop ? this.wr_drop(this) : false;
-    if (req == WR_PICKUP) return this.wr_pickup ? this.wr_pickup(this) : false;
-    return false;
-}
+const int WEP_TYPE_MELEE_PRI      = 0x400; // primary attack is melee swing (for animation)
+const int WEP_TYPE_MELEE_SEC      = 0x800; // secondary attack is melee swing (for animation)
 
 // variables:
 string weaponorder_byid;
@@ -187,7 +203,7 @@ string W_UndeprecateName(string s);
 string W_NameWeaponOrder(string order);
 string W_NumberWeaponOrder(string order);
 string W_FixWeaponOrder_BuildImpulseList(string o);
-string W_FixWeaponOrder_AllowIncomplete(string order);
+string W_FixWeaponOrder_AllowIncomplete(entity this, string order);
 string W_FixWeaponOrder_ForceComplete(string order);
 void W_RandomWeapons(entity e, float n);
 
@@ -200,12 +216,3 @@ int GetAmmoStat(.int ammotype);
 
 string W_Sound(string w_snd);
 string W_Model(string w_mdl);
-
-
-// other useful macros
-#define WEP_ACTION(wpn,wrequest) wpn.weapon_func(wpn, wrequest)
-#define _WEP_ACTION(wpn,wrequest) WEP_ACTION(get_weaponinfo(wpn), wrequest)
-#define WEP_AMMO(wpn) (WEP_##wpn.ammo_field) // only used inside weapon files/with direct name, don't duplicate prefix
-#define WEP_NAME(wpn) ((get_weaponinfo(wpn)).message)
-
-#endif