From 18dcd0070ab608b7788a7def821088060847fa76 Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 27 Jul 2020 15:51:28 +1000 Subject: [PATCH] Use monster definitions directly instead of a set ID, reduces registry calls needed per-monster (slight performance boost) --- .../gamemode/invasion/sv_invasion.qc | 8 ++--- qcsrc/common/monsters/all.qh | 2 -- qcsrc/common/monsters/monster.qh | 1 + qcsrc/common/monsters/monster/mage.qc | 2 +- qcsrc/common/monsters/monster/shambler.qc | 2 +- qcsrc/common/monsters/monster/spider.qc | 4 +-- qcsrc/common/monsters/monster/wyvern.qc | 2 +- qcsrc/common/monsters/monster/zombie.qc | 2 +- qcsrc/common/monsters/sv_monsters.qc | 33 +++++++++---------- qcsrc/common/monsters/sv_monsters.qh | 2 +- qcsrc/common/monsters/sv_spawn.qc | 13 +++++--- qcsrc/common/monsters/sv_spawn.qh | 4 ++- qcsrc/common/monsters/sv_spawner.qc | 2 +- .../mutators/mutator/instagib/sv_instagib.qc | 2 +- qcsrc/common/mutators/mutator/nades/nades.qc | 2 +- qcsrc/server/command/common.qc | 4 +-- 16 files changed, 44 insertions(+), 41 deletions(-) diff --git a/qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc b/qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc index 31c571142..a21504050 100644 --- a/qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc +++ b/qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc @@ -207,7 +207,7 @@ void invasion_SpawnChosenMonster(Monster mon) setsize(e, mon.m_mins, mon.m_maxs); if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256)) - monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2); + monster = spawnmonster(e, tospawn, mon, NULL, NULL, e.origin, false, false, 2); else { delete(e); @@ -215,7 +215,7 @@ void invasion_SpawnChosenMonster(Monster mon) } } else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour) - monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon.monsterid, spawn_point, spawn_point, spawn_point.origin, false, false, 2); + monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon, spawn_point, spawn_point, spawn_point.origin, false, false, 2); if(!monster) return; @@ -290,7 +290,7 @@ bool Invasion_CheckWinner() IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0, { - if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER) + if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER) ++supermonster_count; ++total_alive_monsters; @@ -467,7 +467,7 @@ MUTATOR_HOOKFUNCTION(inv, MonsterSpawn) mon.monster_skill = inv_monsterskill; - if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER) + if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER) Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name); } diff --git a/qcsrc/common/monsters/all.qh b/qcsrc/common/monsters/all.qh index 58840159e..dfcacdc60 100644 --- a/qcsrc/common/monsters/all.qh +++ b/qcsrc/common/monsters/all.qh @@ -6,8 +6,6 @@ REGISTRY(Monsters, BITS(5)) #define get_monsterinfo(i) REGISTRY_GET(Monsters, i) REGISTER_REGISTRY(Monsters) REGISTRY_CHECK(Monsters) -const int MON_FIRST = 1; -#define MON_LAST (REGISTRY_COUNT(Monsters) - 1) #define REGISTER_MONSTER(id, inst) REGISTER(Monsters, MON, id, monsterid, inst) #include "monster.qh" diff --git a/qcsrc/common/monsters/monster.qh b/qcsrc/common/monsters/monster.qh index aba9c9aa9..146f64d40 100644 --- a/qcsrc/common/monsters/monster.qh +++ b/qcsrc/common/monsters/monster.qh @@ -17,6 +17,7 @@ const int MON_FLAG_HIDDEN = BIT(16); // entity properties of monsterinfo: .bool(int, entity actor, entity targ, .entity weaponentity) monster_attackfunc; +.entity monsterdef; // animations .vector anim_blockend; diff --git a/qcsrc/common/monsters/monster/mage.qc b/qcsrc/common/monsters/monster/mage.qc index 1cba349ed..8addac353 100644 --- a/qcsrc/common/monsters/monster/mage.qc +++ b/qcsrc/common/monsters/monster/mage.qc @@ -388,7 +388,7 @@ bool M_Mage_Attack(int attack_type, entity actor, entity targ, .entity weaponent return false; } -spawnfunc(monster_mage) { Monster_Spawn(this, true, MON_MAGE.monsterid); } +spawnfunc(monster_mage) { Monster_Spawn(this, true, MON_MAGE); } #endif // SVQC diff --git a/qcsrc/common/monsters/monster/shambler.qc b/qcsrc/common/monsters/monster/shambler.qc index 2e3f02a06..c67d90664 100644 --- a/qcsrc/common/monsters/monster/shambler.qc +++ b/qcsrc/common/monsters/monster/shambler.qc @@ -197,7 +197,7 @@ bool M_Shambler_Attack(int attack_type, entity actor, entity targ, .entity weapo return false; } -spawnfunc(monster_shambler) { Monster_Spawn(this, true, MON_SHAMBLER.monsterid); } +spawnfunc(monster_shambler) { Monster_Spawn(this, true, MON_SHAMBLER); } #endif // SVQC #ifdef SVQC diff --git a/qcsrc/common/monsters/monster/spider.qc b/qcsrc/common/monsters/monster/spider.qc index d9a51c05e..08d282187 100644 --- a/qcsrc/common/monsters/monster/spider.qc +++ b/qcsrc/common/monsters/monster/spider.qc @@ -104,7 +104,7 @@ void M_Spider_Attack_Web_Explode(entity this) Send_Effect(EFFECT_ELECTRO_IMPACT, this.origin, '0 0 0', 1); RadiusDamage(this, this.realowner, 0, 0, 25, NULL, NULL, 25, this.projectiledeathtype, DMG_NOWEP, NULL); - FOREACH_ENTITY_RADIUS(this.origin, 25, it != this && it.takedamage && !IS_DEAD(it) && GetResource(it, RES_HEALTH) > 0 && it.monsterid != MON_SPIDER.monsterid, + FOREACH_ENTITY_RADIUS(this.origin, 25, it != this && it.takedamage && !IS_DEAD(it) && GetResource(it, RES_HEALTH) > 0 && it.monsterdef != MON_SPIDER, { it.spider_slowness = time + (autocvar_g_monster_spider_attack_web_damagetime); }); @@ -185,7 +185,7 @@ bool M_Spider_Attack(int attack_type, entity actor, entity targ, .entity weapone return false; } -spawnfunc(monster_spider) { Monster_Spawn(this, true, MON_SPIDER.monsterid); } +spawnfunc(monster_spider) { Monster_Spawn(this, true, MON_SPIDER); } #endif // SVQC #ifdef SVQC diff --git a/qcsrc/common/monsters/monster/wyvern.qc b/qcsrc/common/monsters/monster/wyvern.qc index 4de122e84..0a811cb70 100644 --- a/qcsrc/common/monsters/monster/wyvern.qc +++ b/qcsrc/common/monsters/monster/wyvern.qc @@ -106,7 +106,7 @@ bool M_Wyvern_Attack(int attack_type, entity actor, entity targ, .entity weapone return false; } -spawnfunc(monster_wyvern) { Monster_Spawn(this, true, MON_WYVERN.monsterid); } +spawnfunc(monster_wyvern) { Monster_Spawn(this, true, MON_WYVERN); } #endif // SVQC #ifdef SVQC diff --git a/qcsrc/common/monsters/monster/zombie.qc b/qcsrc/common/monsters/monster/zombie.qc index 3b2c0451b..70981fa00 100644 --- a/qcsrc/common/monsters/monster/zombie.qc +++ b/qcsrc/common/monsters/monster/zombie.qc @@ -125,7 +125,7 @@ bool M_Zombie_Attack(int attack_type, entity actor, entity targ, .entity weapone return false; } -spawnfunc(monster_zombie) { Monster_Spawn(this, true, MON_ZOMBIE.monsterid); } +spawnfunc(monster_zombie) { Monster_Spawn(this, true, MON_ZOMBIE); } #endif // SVQC #ifdef SVQC diff --git a/qcsrc/common/monsters/sv_monsters.qc b/qcsrc/common/monsters/sv_monsters.qc index 4c2b5ff26..b938b10ed 100644 --- a/qcsrc/common/monsters/sv_monsters.qc +++ b/qcsrc/common/monsters/sv_monsters.qc @@ -78,7 +78,7 @@ bool Monster_ValidTarget(entity this, entity targ) if((targ == this) || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen - || (IS_VEHICLE(targ) && !((REGISTRY_GET(Monsters, this.monsterid)).spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless + || (IS_VEHICLE(targ) && !(this.monsterdef.spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless || (time < game_starttime) // monsters do nothing before match has started || (targ.takedamage == DAMAGE_NO) || (game_stopped) @@ -260,7 +260,7 @@ void Monster_Sound_Precache(string f) void Monster_Sounds_Precache(entity this) { - string m = (REGISTRY_GET(Monsters, this.monsterid)).m_model.model_str(); + string m = this.monsterdef.m_model.model_str(); float globhandle, n, i; string f; @@ -465,7 +465,7 @@ void Monster_UpdateModel(entity this) this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/ // then get the real values - Monster mon = REGISTRY_GET(Monsters, this.monsterid); + Monster mon = this.monsterdef; mon.mr_anim(mon, this); } @@ -512,7 +512,7 @@ bool Monster_Respawn_Check(entity this) return true; } -void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterid); } +void Monster_Respawn(entity this) { Monster_Spawn(this, true, this); } .vector pos1, pos2; @@ -878,16 +878,16 @@ void Monster_Dead_Think(entity this) void Monster_Appear(entity this, entity actor, entity trigger) { this.enemy = actor; - Monster_Spawn(this, false, this.monsterid); + Monster_Spawn(this, false, this.monsterdef); } -bool Monster_Appear_Check(entity this, int monster_id) +bool Monster_Appear_Check(entity this, Monster monster_id) { if(!(this.spawnflags & MONSTERFLAG_APPEAR)) return false; setthink(this, func_null); - this.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used) + this.monsterdef = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used) this.nextthink = 0; this.use = Monster_Appear; this.flags = FL_MONSTER; // set so this monster can get butchered @@ -977,7 +977,7 @@ void Monster_Dead(entity this, entity attacker, float gibbed) CSQCModel_UnlinkEntity(this); - Monster mon = REGISTRY_GET(Monsters, this.monsterid); + Monster mon = this.monsterdef; mon.mr_death(mon, this); if(this.candrop && this.weapon) @@ -1008,7 +1008,7 @@ void Monster_Damage(entity this, entity inflictor, entity attacker, float damage float take = v.x; //float save = v.y; - Monster mon = REGISTRY_GET(Monsters, this.monsterid); + Monster mon = this.monsterdef; take = mon.mr_pain(mon, this, take, attacker, deathtype); if(take) @@ -1236,7 +1236,7 @@ void Monster_Think(entity this) this.last_enemycheck = time + 1; // check for enemies every second } - Monster mon = REGISTRY_GET(Monsters, this.monsterid); + Monster mon = this.monsterdef; if(mon.mr_think(mon, this)) { Monster_Move(this, this.speed2, this.speed, this.stopspeed); @@ -1252,7 +1252,7 @@ void Monster_Think(entity this) bool Monster_Spawn_Setup(entity this) { - Monster mon = REGISTRY_GET(Monsters, this.monsterid); + Monster mon = this.monsterdef; mon.mr_setup(mon, this); // ensure some basic needs are met @@ -1297,7 +1297,7 @@ bool Monster_Spawn_Setup(entity this) if(autocvar_g_monsters_healthbars) { entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER); - wp.wp_extra = this.monsterid; + wp.wp_extra = this.monsterdef.monsterid; wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0'); if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE)) { @@ -1315,12 +1315,11 @@ bool Monster_Spawn_Setup(entity this) return true; } -bool Monster_Spawn(entity this, bool check_appear, int mon_id) +bool Monster_Spawn(entity this, bool check_appear, Monster mon) { // setup the basic required properties for a monster - entity mon = REGISTRY_GET(Monsters, mon_id); - if(!mon.monsterid) { return false; } // invalid monster + if(!mon || mon == MON_Null) { return false; } // invalid monster if(!autocvar_g_monsters) { Monster_Remove(this); return false; } if(!(this.spawnflags & MONSTERFLAG_RESPAWNED) && !(this.flags & FL_MONSTER)) @@ -1332,7 +1331,7 @@ bool Monster_Spawn(entity this, bool check_appear, int mon_id) precache_model(this.mdl_dead); } - if(check_appear && Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed + if(check_appear && Monster_Appear_Check(this, mon)) { return true; } // return true so the monster isn't removed if(!this.monster_skill) this.monster_skill = cvar("g_monsters_skill"); @@ -1365,7 +1364,7 @@ bool Monster_Spawn(entity this, bool check_appear, int mon_id) if(!this.damagedbycontents) IL_PUSH(g_damagedbycontents, this); this.damagedbycontents = true; - this.monsterid = mon_id; + this.monsterdef = mon; this.event_damage = Monster_Damage; this.event_heal = Monster_Heal; settouch(this, Monster_Touch); diff --git a/qcsrc/common/monsters/sv_monsters.qh b/qcsrc/common/monsters/sv_monsters.qh index 9d890d735..755abaaf2 100644 --- a/qcsrc/common/monsters/sv_monsters.qh +++ b/qcsrc/common/monsters/sv_monsters.qh @@ -70,7 +70,7 @@ void Monster_Remove(entity this); void monsters_setstatus(entity this); -bool Monster_Spawn(entity this, bool check_appear, int mon_id); +bool Monster_Spawn(entity this, bool check_appear, Monster mon); void monster_setupcolors(entity this); diff --git a/qcsrc/common/monsters/sv_spawn.qc b/qcsrc/common/monsters/sv_spawn.qc index d456282d4..f4ae76df8 100644 --- a/qcsrc/common/monsters/sv_spawn.qc +++ b/qcsrc/common/monsters/sv_spawn.qc @@ -8,7 +8,7 @@ #include #include #endif -entity spawnmonster (entity e, string monster, int monster_id, entity spawnedby, entity own, vector orig, bool respwn, bool removeifinvalid, int moveflag) +entity spawnmonster (entity e, string monster, Monster monster_id, entity spawnedby, entity own, vector orig, bool respwn, bool removeifinvalid, int moveflag) { e.spawnflags = MONSTERFLAG_SPAWNED; @@ -26,7 +26,7 @@ entity spawnmonster (entity e, string monster, int monster_id, entity spawnedby, RandomSelection_AddEnt(it, 1, 1); }); - monster_id = RandomSelection_chosen_ent.monsterid; + monster_id = RandomSelection_chosen_ent; } else if(monster != "") { @@ -36,12 +36,12 @@ entity spawnmonster (entity e, string monster, int monster_id, entity spawnedby, if(it.netname == monster) { found = true; - monster_id = it.monsterid; // we have the monster, old monster id is no longer required + monster_id = it; // we have the monster, old monster id is no longer required break; } }); - if(!found && !monster_id) + if(!found && monster_id == MON_Null) { if(removeifinvalid) { @@ -49,7 +49,10 @@ entity spawnmonster (entity e, string monster, int monster_id, entity spawnedby, return NULL; // no good } else - monster_id = MON_FIRST; + { + // select a random valid monster type if no valid monster was provided + return spawnmonster(e, "random", MON_Null, spawnedby, own, orig, respwn, removeifinvalid, moveflag); + } } } diff --git a/qcsrc/common/monsters/sv_spawn.qh b/qcsrc/common/monsters/sv_spawn.qh index 983676db8..2c1127b7d 100644 --- a/qcsrc/common/monsters/sv_spawn.qh +++ b/qcsrc/common/monsters/sv_spawn.qh @@ -1,3 +1,5 @@ #pragma once -entity spawnmonster (entity e, string monster, int monster_id, entity spawnedby, entity own, vector orig, bool respwn, bool removeifinvalid, int moveflag); +#include + +entity spawnmonster (entity e, string monster, Monster monster_id, entity spawnedby, entity own, vector orig, bool respwn, bool removeifinvalid, int moveflag); diff --git a/qcsrc/common/monsters/sv_spawner.qc b/qcsrc/common/monsters/sv_spawner.qc index 13c98e93f..d12ee2bc6 100644 --- a/qcsrc/common/monsters/sv_spawner.qc +++ b/qcsrc/common/monsters/sv_spawner.qc @@ -17,7 +17,7 @@ void spawner_use(entity this, entity actor, entity trigger) e.angles = this.angles; e.monster_skill = this.monster_skill; e.skin = this.skin; - e = spawnmonster(e, this.spawnmob, 0, this, this, this.origin, false, true, this.monster_moveflags); + e = spawnmonster(e, this.spawnmob, MON_Null, this, this, this.origin, false, true, this.monster_moveflags); } spawnfunc(monster_spawner) diff --git a/qcsrc/common/mutators/mutator/instagib/sv_instagib.qc b/qcsrc/common/mutators/mutator/instagib/sv_instagib.qc index f4ff4d34a..a23fc3697 100644 --- a/qcsrc/common/mutators/mutator/instagib/sv_instagib.qc +++ b/qcsrc/common/mutators/mutator/instagib/sv_instagib.qc @@ -179,7 +179,7 @@ MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterSpawn) entity mon = M_ARGV(0, entity); // always refill ammo - if(mon.monsterid == MON_MAGE.monsterid) + if(mon.monsterdef == MON_MAGE) mon.skin = 1; } diff --git a/qcsrc/common/mutators/mutator/nades/nades.qc b/qcsrc/common/mutators/mutator/nades/nades.qc index 7dffc0f4d..bd539aa8e 100644 --- a/qcsrc/common/mutators/mutator/nades/nades.qc +++ b/qcsrc/common/mutators/mutator/nades/nades.qc @@ -673,7 +673,7 @@ void nade_monster_boom(entity this) { entity e = spawn(); e.noalign = true; // don't drop to floor - e = spawnmonster(e, this.pokenade_type, 0, this.realowner, this.realowner, this.origin, false, false, 1); + e = spawnmonster(e, this.pokenade_type, MON_Null, this.realowner, this.realowner, this.origin, false, false, 1); if(autocvar_g_nades_pokenade_monster_lifetime > 0) e.monster_lifetime = time + autocvar_g_nades_pokenade_monster_lifetime; diff --git a/qcsrc/server/command/common.qc b/qcsrc/server/command/common.qc index f791446eb..32ccd883e 100644 --- a/qcsrc/server/command/common.qc +++ b/qcsrc/server/command/common.qc @@ -395,7 +395,7 @@ void CommonCommand_editmob(int request, entity caller, int argc) totalspawned += 1; WarpZone_TraceBox(CENTER_OR_VIEWOFS(caller), caller.mins, caller.maxs, CENTER_OR_VIEWOFS(caller) + v_forward * 150, true, caller); - mon = spawnmonster(spawn(), arg_lower, 0, caller, caller, trace_endpos, false, false, moveflag); + mon = spawnmonster(spawn(), arg_lower, MON_Null, caller, caller, trace_endpos, false, false, moveflag); print_to(caller, strcat("Spawned ", mon.monster_name)); return; } @@ -416,7 +416,7 @@ void CommonCommand_editmob(int request, entity caller, int argc) if (!autocvar_g_monsters_edit) { print_to(caller, "Monster editing is disabled"); return; } if (!is_visible) { print_to(caller, "You must look at your monster to edit it"); return; } if (mon.realowner != caller && autocvar_g_monsters_edit < 2) { print_to(caller, "This monster does not belong to you"); return; } - if (mon.monsterid == MON_MAGE.monsterid) { print_to(caller, "Mage skins can't be changed"); return; } // TODO + if (mon.monsterdef == MON_MAGE) { print_to(caller, "Mage skins can't be changed"); return; } // TODO mon.skin = stof(argument); print_to(caller, strcat("Monster skin successfully changed to ", ftos(mon.skin))); -- 2.39.2