From 54626fc8dc1b2ec9932b93121994a6d0f2e6643e Mon Sep 17 00:00:00 2001 From: TimePath Date: Fri, 14 Aug 2015 18:14:46 +1000 Subject: [PATCH] Migrate REGISTER_WEAPON to registry system --- qcsrc/client/main.qc | 1 - qcsrc/common/weapons/all.qc | 53 +----------- qcsrc/common/weapons/all.qh | 155 +++++++++++++++++++++--------------- qcsrc/menu/menu.qc | 1 - qcsrc/server/g_world.qc | 2 - 5 files changed, 93 insertions(+), 119 deletions(-) diff --git a/qcsrc/client/main.qc b/qcsrc/client/main.qc index b56887878..e40edfdcc 100644 --- a/qcsrc/client/main.qc +++ b/qcsrc/client/main.qc @@ -141,7 +141,6 @@ void CSQC_Init(void) // needs to be done so early because of the constants they create static_init(); - CALL_ACCUMULATED_FUNCTION(RegisterWeapons); CALL_ACCUMULATED_FUNCTION(RegisterNotifications); CALL_ACCUMULATED_FUNCTION(RegisterDeathtypes); CALL_ACCUMULATED_FUNCTION(RegisterHUD_Panels); diff --git a/qcsrc/common/weapons/all.qc b/qcsrc/common/weapons/all.qc index 00c801240..b0262d433 100644 --- a/qcsrc/common/weapons/all.qc +++ b/qcsrc/common/weapons/all.qc @@ -59,8 +59,6 @@ #undef IMPLEMENTATION // WEAPON PLUGIN SYSTEM -entity weapon_info[WEP_MAXCOUNT]; -entity dummy_weapon_info; #if WEP_MAXCOUNT > 72 # error Kein Weltraum links auf dem Gerät @@ -151,52 +149,6 @@ WepSet ReadWepSet() } #endif -void register_weapon( - int id, - WepSet bit, - bool(int) func, - .int ammotype, - int i, - int weapontype, - float pickupbasevalue, - vector clr, - string modelname, - string simplemdl, - string crosshair, - string wepimg, - string refname, - string wepname) -{ - entity e; - weapon_info[id - 1] = e = spawn(); - e.classname = "weapon_info"; - e.weapon = id; - e.weapons = bit; - e.weapon_func = func; - e.ammo_field = ammotype; - e.impulse = i; - e.spawnflags = weapontype; - e.bot_pickupbasevalue = pickupbasevalue; - e.wpcolor = clr; - e.wpmodel = strzone(strcat("wpn-", ftos(id))); - e.mdl = modelname; - e.model = strzone(strcat("models/weapons/g_", modelname, ".md3")); - e.w_simplemdl = strzone(simplemdl); // simpleitems weapon model/image - e.w_crosshair = strzone(car(crosshair)); - string s = cdr(crosshair); - e.w_crosshair_size = ((s != "") ? stof(s) : 1); // so that we can scale the crosshair from code (for compat) - e.model2 = strzone(wepimg); - e.netname = refname; - e.message = wepname; - - #ifdef CSQC - func(WR_INIT); - #endif -} -bool w_null(int dummy) -{ - return 0; -} void register_weapons_done() { dummy_weapon_info = spawn(); @@ -218,10 +170,9 @@ void register_weapons_done() dummy_weapon_info.w_crosshair_size = 1; dummy_weapon_info.model2 = ""; - int i; weaponorder_byid = ""; - for(i = WEP_MAXCOUNT; i >= 1; --i) - if(weapon_info[i-1]) + for (int i = WEP_MAXCOUNT - 1; i >= 0; --i) + if (weapon_info[i]) weaponorder_byid = strcat(weaponorder_byid, " ", ftos(i)); weaponorder_byid = strzone(substring(weaponorder_byid, 1, strlen(weaponorder_byid) - 1)); } diff --git a/qcsrc/common/weapons/all.qh b/qcsrc/common/weapons/all.qh index eab3b0a5a..b873f8fe1 100644 --- a/qcsrc/common/weapons/all.qh +++ b/qcsrc/common/weapons/all.qh @@ -65,7 +65,7 @@ WepSet ReadWepSet(); #endif // weapon name macros -#define WEP_FIRST 1 +const int WEP_FIRST = 1; #define WEP_MAXCOUNT 24 // Increase as needed. Can be up to three times as much. int WEP_COUNT; int WEP_LAST; @@ -140,69 +140,97 @@ int GetAmmoStat(.int ammotype); // Weapon Registration // ===================== -bool w_null(int dummy); - -void register_weapon( - int id, - WepSet bit, - bool(int) func, - .int ammotype, - int i, - int weapontype, - float pickupbasevalue, - vector clr, - string modelname, - string simplemdl, - string crosshair, - string wepimg, - string refname, - string wepname); +CLASS(Weapon, Object) + ATTRIB(Weapon, m_id, int, 0) + // entity properties of weaponinfo: + // fields which are explicitly/manually set are marked with "M", fields set automatically are marked with "A" + .int weapon; // M: WEP_id // WEP_... + .WepSet weapons; // A: WEPSET_id // WEPSET_... + .float(float) weapon_func; // M: function // w_... + ..int ammo_field; // M: ammotype // main ammo field + .int impulse; // M: impulse // weapon impulse + .int spawnflags; // M: flags // WEPSPAWNFLAG_... combined + .float bot_pickupbasevalue; // M: rating // bot weapon priority + .vector wpcolor; // M: color // waypointsprite color + .string wpmodel; // A: wpn-id // wpn- sprite name + .string mdl; // M: modelname // name of model (without g_ v_ or h_ prefixes) + .string model; // A: modelname // full path to g_ model + .string w_simplemdl; // M: simplemdl // simpleitems weapon model/image + .string w_crosshair; // M: crosshair // per-weapon crosshair: "CrosshairImage Size" + .float w_crosshair_size; // A: crosshair // per-weapon crosshair size (argument two of "crosshair" field) + .string model2; // M: wepimg // "weaponfoobar" side view image file of weapon // WEAPONTODO: Move out of skin files, move to common files + .string netname; // M: refname // reference name name + .string message; // M: wepname // human readable name + CONSTRUCTOR(Weapon, + bool(int) function, + .int ammotype, + int i, + int weapontype, + float pickupbasevalue, + vector clr, + string modelname, + string simplemdl, + 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.model = strzone(strcat("models/weapons/g_", modelname, ".md3")); + this.w_simplemdl = strzone(simplemdl); // simpleitems weapon model/image + 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; + return this; + } + void register_weapon(entity this, int id, WepSet bit) + { + this.classname = "weapon_info"; + this.weapon = id; + this.weapons = bit; + this.wpmodel = strzone(strcat("wpn-", ftos(id))); + #ifdef CSQC + this.weapon_func(WR_INIT); + #endif + } +ENDCLASS(Weapon) + +void RegisterWeapons(); +REGISTER_REGISTRY(RegisterWeapons) +entity weapon_info[WEP_MAXCOUNT], weapon_info_first, weapon_info_last; +entity dummy_weapon_info; + +#define _REGISTER_WEAPON(id, function, ammotype, impulse, flags, rating, color, modelname, simplemdl, crosshair, wepimg, refname, wepname) \ + int WEP_##id; \ + WepSet WEPSET_##id; \ + REGISTER(RegisterWeapons, WEP_, weapon_info, WEP_COUNT, id, m_id, \ + NEW(Weapon, function, ammotype, impulse, flags, rating, color, modelname, simplemdl, crosshair, wepimg, refname, wepname) \ + ) { \ + WEP_LAST = (WEP_##id = WEP_FIRST + WEP_COUNT - 1); \ + WEPSET_ALL |= (WEPSET_##id = WepSet_FromWeapon(WEP_##id)); \ + if ((flags) & WEP_FLAG_SUPERWEAPON) WEPSET_SUPERWEAPONS |= WEPSET_##id; \ + register_weapon(this, WEP_##id, WEPSET_##id); \ + } + +bool w_null(int) { return false; } -void register_weapons_done(); - -// entity properties of weaponinfo: -// fields which are explicitly/manually set are marked with "M", fields set automatically are marked with "A" -.int weapon; // M: WEP_id // WEP_... -.WepSet weapons; // A: WEPSET_id // WEPSET_... -.float(float) weapon_func; // M: function // w_... -..int ammo_field; // M: ammotype // main ammo field -.int impulse; // M: impulse // weapon impulse -.int spawnflags; // M: flags // WEPSPAWNFLAG_... combined -.float bot_pickupbasevalue; // M: rating // bot weapon priority -.vector wpcolor; // M: color // waypointsprite color -.string wpmodel; // A: wpn-id // wpn- sprite name -.string mdl; // M: modelname // name of model (without g_ v_ or h_ prefixes) -.string model; // A: modelname // full path to g_ model -.string w_simplemdl; // M: simplemdl // simpleitems weapon model/image -.string w_crosshair; // M: crosshair // per-weapon crosshair: "CrosshairImage Size" -.float w_crosshair_size; // A: crosshair // per-weapon crosshair size (argument two of "crosshair" field) -.string model2; // M: wepimg // "weaponfoobar" side view image file of weapon // WEAPONTODO: Move out of skin files, move to common files -.string netname; // M: refname // reference name name -.string message; // M: wepname // human readable name - - -// note: the fabs call is just there to hide "if result is constant" warning -#define REGISTER_WEAPON_2(id,bit,function,ammotype,impulse,flags,rating,color,modelname,simplemdl,crosshair,wepimg,refname,wepname) \ - int id; \ - WepSet bit; \ - bool function(int); \ - void RegisterWeapons_##id() \ - { \ - WEP_LAST = (id = WEP_FIRST + WEP_COUNT); \ - bit = WepSet_FromWeapon(id); \ - WEPSET_ALL |= bit; \ - if((flags) & WEP_FLAG_SUPERWEAPON) \ - WEPSET_SUPERWEAPONS |= bit; \ - ++WEP_COUNT; \ - register_weapon(id,bit,function,ammotype,impulse,flags,rating,color,modelname,simplemdl,crosshair,wepimg,refname,wepname); \ - } \ - ACCUMULATE_FUNCTION(RegisterWeapons, RegisterWeapons_##id) -#ifdef MENUQC -#define REGISTER_WEAPON(id,function,ammotype,impulse,flags,rating,color,modelname,simplemdl,crosshair,wepimg,refname,wepname) \ - REGISTER_WEAPON_2(WEP_##id,WEPSET_##id,w_null,ammotype,impulse,flags,rating,color,modelname,simplemdl,crosshair,wepimg,refname,wepname) +#ifndef MENUQC + #define REGISTER_WEAPON(id, function, ammotype, impulse, flags, rating, color, modelname, simplemdl, crosshair, wepimg, refname, wepname) \ + bool function(int); \ + _REGISTER_WEAPON(id, function, ammotype, impulse, flags, rating, color, modelname, simplemdl, crosshair, wepimg, refname, wepname) #else -#define REGISTER_WEAPON(id,function,ammotype,impulse,flags,rating,color,modelname,simplemdl,crosshair,wepimg,refname,wepname) \ - REGISTER_WEAPON_2(WEP_##id,WEPSET_##id,function,ammotype,impulse,flags,rating,color,modelname,simplemdl,crosshair,wepimg,refname,wepname) + #define REGISTER_WEAPON(id, function, ammotype, impulse, flags, rating, color, modelname, simplemdl, crosshair, wepimg, refname, wepname) \ + _REGISTER_WEAPON(id, w_null, ammotype, impulse, flags, rating, color, modelname, simplemdl, crosshair, wepimg, refname, wepname) #endif #include "all.inc" @@ -213,7 +241,6 @@ void register_weapons_done(); #undef WEP_ADD_CVAR_MO_NONE #undef WEP_ADD_CVAR #undef WEP_ADD_PROP -#undef REGISTER_WEAPON - +void register_weapons_done(); ACCUMULATE_FUNCTION(RegisterWeapons, register_weapons_done); #endif diff --git a/qcsrc/menu/menu.qc b/qcsrc/menu/menu.qc index b2a85093c..6b6176bd2 100644 --- a/qcsrc/menu/menu.qc +++ b/qcsrc/menu/menu.qc @@ -81,7 +81,6 @@ void m_init() // needs to be done so early because of the constants they create static_init(); - CALL_ACCUMULATED_FUNCTION(RegisterWeapons); RegisterSLCategories(); diff --git a/qcsrc/server/g_world.qc b/qcsrc/server/g_world.qc index 881f8f071..f40acfd63 100644 --- a/qcsrc/server/g_world.qc +++ b/qcsrc/server/g_world.qc @@ -560,7 +560,6 @@ void spawnfunc___init_dedicated_server(void) // needs to be done so early because of the constants they create static_init(); - CALL_ACCUMULATED_FUNCTION(RegisterWeapons); CALL_ACCUMULATED_FUNCTION(RegisterNotifications); CALL_ACCUMULATED_FUNCTION(RegisterDeathtypes); @@ -608,7 +607,6 @@ void spawnfunc_worldspawn (void) // needs to be done so early because of the constants they create static_init(); - CALL_ACCUMULATED_FUNCTION(RegisterWeapons); CALL_ACCUMULATED_FUNCTION(RegisterNotifications); CALL_ACCUMULATED_FUNCTION(RegisterDeathtypes); -- 2.39.2