]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/monsters/sv_monsters.qc
Cleanse the touch functions of the other evil
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / sv_monsters.qc
index 249e6f97eb768be7fed885364f200b0915d91baf..795e0310a263f35fd922478f1e9a1ad1125ad47d 100644 (file)
 #if defined(CSQC)
 #elif defined(MENUQC)
 #elif defined(SVQC)
-       #include "../../dpdefs/progsdefs.qh"
-    #include "../../dpdefs/dpextensions.qh"
-    #include "../../warpzonelib/common.qh"
+    #include <lib/warpzone/common.qh>
     #include "../constants.qh"
     #include "../teams.qh"
     #include "../util.qh"
     #include "all.qh"
     #include "sv_monsters.qh"
+       #include "../physics/movelib.qh"
     #include "../weapons/all.qh"
-    #include "../../server/autocvars.qh"
-    #include "../../server/defs.qh"
-    #include "../deathtypes.qh"
-    #include "../../server/mutators/mutators_include.qh"
-       #include "../../server/steerlib.qh"
+    #include <server/autocvars.qh>
+    #include <server/defs.qh>
+    #include "../deathtypes/all.qh"
+    #include <server/mutators/all.qh>
+       #include <server/steerlib.qh>
        #include "../turrets/sv_turrets.qh"
        #include "../turrets/util.qh"
     #include "../vehicles/all.qh"
-    #include "../../server/campaign.qh"
-    #include "../../server/command/common.qh"
-    #include "../../server/command/cmd.qh"
+    #include <server/campaign.qh>
+    #include <server/command/common.qh>
+    #include <server/command/cmd.qh>
        #include "../triggers/triggers.qh"
-    #include "../../csqcmodellib/sv_model.qh"
-    #include "../../server/round_handler.qh"
+    #include <lib/csqcmodel/sv_model.qh>
+    #include <server/round_handler.qh>
 #endif
 
-// =========================
-//     SVQC Monster Properties
-// =========================
-
+void monsters_setstatus(entity this)
+{
+       this.stat_monsters_total = monsters_total;
+       this.stat_monsters_killed = monsters_killed;
+}
 
-void monster_dropitem()
+void monster_dropitem(entity this, entity attacker)
 {
-       if(!self.candrop || !self.monster_loot)
+       if(!this.candrop || !this.monster_loot)
                return;
 
-       vector org = self.origin + ((self.mins + self.maxs) * 0.5);
-       entity e = spawn(), oldself = self;
+       vector org = this.origin + ((this.mins + this.maxs) * 0.5);
+       entity e = new(droppedweapon); // use weapon handling to remove it on touch
+       e.spawnfunc_checked = true;
 
-       e.monster_loot = self.monster_loot;
+       e.monster_loot = this.monster_loot;
 
-       MUTATOR_CALLHOOK(MonsterDropItem, e);
-       e = other;
+       MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
+       e = M_ARGV(1, entity);
 
        if(e && e.monster_loot)
        {
-               self = e;
                e.noalign = true;
-               e.monster_loot();
+               e.monster_loot(e);
                e.gravity = 1;
                e.movetype = MOVETYPE_TOSS;
                e.reset = SUB_Remove;
                setorigin(e, org);
                e.velocity = randomvec() * 175 + '0 0 325';
                e.item_spawnshieldtime = time + 0.7;
-               e.classname = "droppedweapon"; // use weapon handling to remove it on touch
                SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
-               self = oldself;
        }
 }
 
-float Monster_SkillModifier()
+void monster_makevectors(entity this, entity targ)
 {
-       float t = 0.5+self.monster_skill*((1.2-0.3)/10);
+       if(IS_MONSTER(this))
+       {
+               vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
+               this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
+               this.v_angle_x = -this.v_angle_x;
+       }
 
-       return t;
+       makevectors(this.v_angle);
 }
 
-float monster_isvalidtarget (entity targ, entity ent)
-{
-       if(!targ || !ent)
-               return false; // someone doesn't exist
-
-       if(targ == ent)
-               return false; // don't attack ourselves
-
-       //traceline(ent.origin, targ.origin, MOVE_NORMAL, ent);
-
-       //if(trace_ent != targ)
-               //return false;
-
-       if(IS_VEHICLE(targ))
-       if(!((get_monsterinfo(ent.monsterid)).spawnflags & MON_FLAG_RANGED))
-               return false; // melee attacks are useless against vehicles
-
-       if(time < game_starttime)
-               return false; // monsters do nothing before the match has started
-
-       if(targ.takedamage == DAMAGE_NO)
-               return false; // enemy can't be damaged
-
-       if(targ.items & IT_INVISIBILITY)
-               return false; // enemy is invisible
-
-       if(substring(targ.classname, 0, 10) == "onslaught_")
-               return false; // don't attack onslaught targets
-
-       if(IS_SPEC(targ) || IS_OBSERVER(targ))
-               return false; // enemy is a spectator
-
-       if(!IS_VEHICLE(targ))
-       if(targ.deadflag != DEAD_NO || ent.deadflag != DEAD_NO || targ.health <= 0 || ent.health <= 0)
-               return false; // enemy/self is dead
+// ===============
+// Target handling
+// ===============
 
-       if(ent.monster_owner == targ)
-               return false; // don't attack our master
-
-       if(targ.monster_owner == ent)
-               return false; // don't attack our pet
-
-       if(!IS_VEHICLE(targ))
-       if(targ.flags & FL_NOTARGET)
-               return false; // enemy can't be targeted
-
-       if(!autocvar_g_monsters_typefrag)
-       if(targ.BUTTON_CHAT)
-               return false; // no typefragging!
+bool Monster_ValidTarget(entity this, entity targ)
+{
+       // ensure we're not checking nonexistent monster/target
+       if(!this || !targ) { return false; }
+
+       if((targ == this)
+       || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
+       || (IS_VEHICLE(targ) && !((get_monsterinfo(this.monsterid)).spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
+       || (time < game_starttime) // monsters do nothing before match has started
+       || (targ.takedamage == DAMAGE_NO)
+       || (targ.items & IT_INVISIBILITY)
+       || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
+       || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || targ.health <= 0 || this.health <= 0))
+       || (this.monster_follow == targ || targ.monster_follow == this)
+       || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
+       || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
+       || (SAME_TEAM(targ, this))
+       || (STAT(FROZEN, targ))
+       || (targ.alpha != 0 && targ.alpha < 0.5)
+       )
+       {
+               // if any of the above checks fail, target is not valid
+               return false;
+       }
 
-       if(SAME_TEAM(targ, ent))
-               return false; // enemy is on our team
+       traceline(this.origin + this.view_ofs, targ.origin, 0, this);
 
-       if (targ.frozen)
-               return false; // ignore frozen
+       if((trace_fraction < 1) && (trace_ent != targ))
+               return false;
 
-       if(autocvar_g_monsters_target_infront || (ent.spawnflags & MONSTERFLAG_INFRONT))
-       if(ent.enemy != targ)
+       if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
+       if(this.enemy != targ)
        {
                float dot;
 
-               makevectors (ent.angles);
-               dot = normalize (targ.origin - ent.origin) * v_forward;
+               makevectors (this.angles);
+               dot = normalize (targ.origin - this.origin) * v_forward;
 
-               if(dot <= 0.3)
-                       return false;
+               if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
        }
 
-       return true;
+       return true; // this target is valid!
 }
 
-entity FindTarget (entity ent)
+entity Monster_FindTarget(entity mon)
 {
-       if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return ent.enemy; } // Handled by a mutator
+       if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return mon.enemy; } // Handled by a mutator
 
-       entity head, closest_target = world;
-       head = findradius(ent.origin, ent.target_range);
-       //head = WarpZone_FindRadius(ent.origin, ent.target_range, true);
+       entity closest_target = NULL;
 
-       while(head) // find the closest acceptable target to pass to
+       // find the closest acceptable target to pass to
+       FOREACH_ENTITY_RADIUS(mon.origin, mon.target_range, it.monster_attack,
        {
-               if(head.monster_attack)
-               if(monster_isvalidtarget(head, ent))
+               if(Monster_ValidTarget(mon, it))
                {
                        // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
-                       vector head_center = CENTER_OR_VIEWOFS(head);
-                       //vector head_center = WarpZone_UnTransformOrigin(head, CENTER_OR_VIEWOFS(head));
-                       vector ent_center = CENTER_OR_VIEWOFS(ent);
-
-                       traceline(ent_center, head_center, MOVE_NORMAL, ent);
+                       vector head_center = CENTER_OR_VIEWOFS(it);
+                       vector ent_center = CENTER_OR_VIEWOFS(mon);
 
-                       if(trace_ent == head)
                        if(closest_target)
                        {
                                vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
-                               //vector closest_target_center = WarpZone_UnTransformOrigin(closest_target, CENTER_OR_VIEWOFS(closest_target));
-                               if(vlen(ent_center - head_center) < vlen(ent_center - closest_target_center))
-                                       { closest_target = head; }
+                               if(vlen2(ent_center - head_center) < vlen2(ent_center - closest_target_center))
+                                       { closest_target = it; }
                        }
-                       else { closest_target = head; }
+                       else { closest_target = it; }
                }
+       });
+
+       return closest_target;
+}
 
-               head = head.chain;
+void monster_setupcolors(entity mon)
+{
+       if(IS_PLAYER(mon.realowner))
+               mon.colormap = mon.realowner.colormap;
+       else if(teamplay && mon.team)
+               mon.colormap = 1024 + (mon.team - 1) * 17;
+       else
+       {
+               if(mon.monster_skill <= MONSTER_SKILL_EASY)
+                       mon.colormap = 1029;
+               else if(mon.monster_skill <= MONSTER_SKILL_MEDIUM)
+                       mon.colormap = 1027;
+               else if(mon.monster_skill <= MONSTER_SKILL_HARD)
+                       mon.colormap = 1038;
+               else if(mon.monster_skill <= MONSTER_SKILL_INSANE)
+                       mon.colormap = 1028;
+               else if(mon.monster_skill <= MONSTER_SKILL_NIGHTMARE)
+                       mon.colormap = 1032;
+               else
+                       mon.colormap = 1024;
        }
+}
 
-       return closest_target;
+void monster_changeteam(entity ent, float newteam)
+{
+       if(!teamplay) { return; }
+
+       ent.team = newteam;
+       ent.monster_attack = true; // new team, activate attacking
+       monster_setupcolors(ent);
+
+       if(ent.sprite)
+       {
+               WaypointSprite_UpdateTeamRadar(ent.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
+
+               ent.sprite.team = newteam;
+               ent.sprite.SendFlags |= 1;
+       }
 }
 
-void MonsterTouch ()
+.void(entity) monster_delayedfunc;
+void Monster_Delay_Action(entity this)
 {
-       if(other == world)
-               return;
+       if(Monster_ValidTarget(this.owner, this.owner.enemy)) { this.monster_delayedfunc(this.owner); }
+
+       if(this.cnt > 1)
+       {
+               this.cnt -= 1;
+               setthink(this, Monster_Delay_Action);
+               this.nextthink = time + this.count;
+       }
+       else
+       {
+               setthink(this, SUB_Remove);
+               this.nextthink = time;
+       }
+}
 
-       if(self.enemy != other)
-       if(!IS_MONSTER(other))
-       if(monster_isvalidtarget(other, self))
-               self.enemy = other;
+void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
+{
+       // deferred attacking, checks if monster is still alive and target is still valid before attacking
+       entity e = spawn();
+
+       setthink(e, Monster_Delay_Action);
+       e.nextthink = time + defer_amnt;
+       e.count = defer_amnt;
+       e.owner = this;
+       e.monster_delayedfunc = func;
+       e.cnt = repeat_count;
 }
 
+
+// ==============
+// Monster sounds
+// ==============
+
 string get_monster_model_datafilename(string m, float sk, string fil)
 {
        if(m)
@@ -200,7 +236,7 @@ string get_monster_model_datafilename(string m, float sk, string fil)
        return strcat(m, ".", fil);
 }
 
-void PrecacheMonsterSounds(string f)
+void Monster_Sound_Precache(string f)
 {
        float fh;
        string s;
@@ -211,7 +247,7 @@ void PrecacheMonsterSounds(string f)
        {
                if(tokenize_console(s) != 3)
                {
-                       dprint("Invalid sound info line: ", s, "\n");
+                       LOG_TRACE("Invalid sound info line: ", s, "\n");
                        continue;
                }
                PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
@@ -219,9 +255,9 @@ void PrecacheMonsterSounds(string f)
        fclose(fh);
 }
 
-void precache_monstersounds()
+void Monster_Sounds_Precache(entity this)
 {
-       string m = (get_monsterinfo(self.monsterid)).model;
+       string m = (Monsters_from(this.monsterid)).m_model.model_str();
        float globhandle, n, i;
        string f;
 
@@ -233,19 +269,19 @@ void precache_monstersounds()
        {
                //print(search_getfilename(globhandle, i), "\n");
                f = search_getfilename(globhandle, i);
-               PrecacheMonsterSounds(f);
+               Monster_Sound_Precache(f);
        }
        search_end(globhandle);
 }
 
-void ClearMonsterSounds()
+void Monster_Sounds_Clear(entity this)
 {
-#define _MSOUND(m) if(self.monstersound_##m) { strunzone(self.monstersound_##m); self.monstersound_##m = string_null; }
+#define _MSOUND(m) if(this.monstersound_##m) { strunzone(this.monstersound_##m); this.monstersound_##m = string_null; }
        ALLMONSTERSOUNDS
 #undef _MSOUND
 }
 
-.string GetMonsterSoundSampleField(string type)
+.string Monster_Sound_SampleField(string type)
 {
        GetMonsterSoundSampleField_notFound = 0;
        switch(type)
@@ -258,7 +294,7 @@ void ClearMonsterSounds()
        return string_null;
 }
 
-float LoadMonsterSounds(string f, float first)
+bool Monster_Sounds_Load(entity this, string f, int first)
 {
        float fh;
        string s;
@@ -266,316 +302,343 @@ float LoadMonsterSounds(string f, float first)
        fh = fopen(f, FILE_READ);
        if(fh < 0)
        {
-               dprint("Monster sound file not found: ", f, "\n");
-               return 0;
+               LOG_TRACE("Monster sound file not found: ", f, "\n");
+               return false;
        }
        while((s = fgets(fh)))
        {
                if(tokenize_console(s) != 3)
                        continue;
-               field = GetMonsterSoundSampleField(argv(0));
+               field = Monster_Sound_SampleField(argv(0));
                if(GetMonsterSoundSampleField_notFound)
                        continue;
-               if (self.(field))
-                       strunzone(self.(field));
-               self.(field) = strzone(strcat(argv(1), " ", argv(2)));
+               if (this.(field))
+                       strunzone(this.(field));
+               this.(field) = strzone(strcat(argv(1), " ", argv(2)));
        }
        fclose(fh);
-       return 1;
+       return true;
 }
 
 .int skin_for_monstersound;
-void UpdateMonsterSounds()
+void Monster_Sounds_Update(entity this)
 {
-       entity mon = get_monsterinfo(self.monsterid);
+       if(this.skin == this.skin_for_monstersound) { return; }
 
-       if(self.skin == self.skin_for_monstersound)
-               return;
-       self.skin_for_monstersound = self.skin;
-       ClearMonsterSounds();
-       //LoadMonsterSounds("sound/monsters/default.sounds", 1);
-       if(!autocvar_g_debug_defaultsounds)
-       if(!LoadMonsterSounds(get_monster_model_datafilename(mon.model, self.skin, "sounds"), 0))
-               LoadMonsterSounds(get_monster_model_datafilename(mon.model, 0, "sounds"), 0);
+       this.skin_for_monstersound = this.skin;
+       Monster_Sounds_Clear(this);
+       if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
+               Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
 }
 
-void MonsterSound(.string samplefield, float sound_delay, float delaytoo, float chan)
+void Monster_Sound(entity this, .string samplefield, float sound_delay, float delaytoo, float chan)
 {
        if(!autocvar_g_monsters_sounds) { return; }
 
        if(delaytoo)
-       if(time < self.msound_delay)
+       if(time < this.msound_delay)
                return; // too early
-       GlobalSound(self.(samplefield), chan, VOICETYPE_PLAYERSOUND);
+       GlobalSound_string(this, this.(samplefield), chan, VOICETYPE_PLAYERSOUND);
 
-       self.msound_delay = time + sound_delay;
+       this.msound_delay = time + sound_delay;
 }
 
-void monster_makevectors(entity e)
-{
-       vector v;
-
-       v = e.origin + (e.mins + e.maxs) * 0.5;
-       self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
-       self.v_angle_x = -self.v_angle.x;
 
-       makevectors(self.v_angle);
-}
+// =======================
+// Monster attack handlers
+// =======================
 
-float monster_melee(entity targ, float damg, float anim, float er, float anim_finished, int deathtype, float dostop)
+bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
 {
-       if (self.health <= 0)
-               return false; // attacking while dead?!
-
-       if(dostop)
-       {
-               self.velocity_x = 0;
-               self.velocity_y = 0;
-               self.state = MONSTER_STATE_ATTACK_MELEE;
-       }
+       if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
 
-       self.frame = anim;
+       setanim(this, anim, false, true, false);
 
-       if(anim_finished != 0)
-               self.attack_finished_single = time + anim_finished;
+       if(this.animstate_endtime > time && IS_MONSTER(this))
+               this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
+       else
+               this.attack_finished_single[0] = this.anim_finished = time + animtime;
 
-       monster_makevectors(targ);
+       monster_makevectors(this, targ);
 
-       traceline(self.origin + self.view_ofs, self.origin + v_forward * er, 0, self);
+       traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
 
        if(trace_ent.takedamage)
-               Damage(trace_ent, self, self, damg * Monster_SkillModifier(), deathtype, trace_ent.origin, normalize(trace_ent.origin - self.origin));
+               Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, trace_ent.origin, normalize(trace_ent.origin - this.origin));
 
        return true;
 }
 
-void Monster_CheckMinibossFlag ()
+bool Monster_Attack_Leap_Check(entity this, vector vel)
 {
-       if(MUTATOR_CALLHOOK(MonsterCheckBossFlag))
-               return;
+       if(this.state && IS_MONSTER(this))
+               return false; // already attacking
+       if(!IS_ONGROUND(this))
+               return false; // not on the ground
+       if(this.health <= 0 || IS_DEAD(this))
+               return false; // called when dead?
+       if(time < this.attack_finished_single[0])
+               return false; // still attacking
 
-       float chance = random() * 100;
+       vector old = this.velocity;
 
-       // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
-       if ((self.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
-       {
-               self.health += autocvar_g_monsters_miniboss_healthboost;
-               self.effects |= EF_RED;
-               if(!self.weapon)
-                       self.weapon = WEP_VORTEX.m_id;
-       }
+       this.velocity = vel;
+       tracetoss(this, this);
+       this.velocity = old;
+       if (trace_ent != this.enemy)
+               return false;
+
+       return true;
 }
 
-float Monster_CanRespawn(entity ent)
+bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
 {
-       other = ent;
-       if(ent.deadflag == DEAD_DEAD) // don't call when monster isn't dead
-       if(MUTATOR_CALLHOOK(MonsterRespawn, ent))
-               return true; // enabled by a mutator
-
-       if(ent.spawnflags & MONSTERFLAG_NORESPAWN)
+       if(!Monster_Attack_Leap_Check(this, vel))
                return false;
 
-       if(!autocvar_g_monsters_respawn)
-               return false;
+       setanim(this, anm, false, true, false);
+
+       if(this.animstate_endtime > time && (this.flags & FL_MONSTER))
+               this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
+       else
+               this.attack_finished_single[0] = this.anim_finished = time + animtime;
+
+       if(this.flags & FL_MONSTER)
+               this.state = MONSTER_ATTACK_RANGED;
+       settouch(this, touchfunc);
+       this.origin_z += 1;
+       this.velocity = vel;
+       UNSET_ONGROUND(this);
 
        return true;
 }
 
-void monster_respawn()
+void Monster_Attack_Check(entity this, entity targ)
 {
-       // is this function really needed?
-       monster_initialize(self.monsterid);
-}
+       if((this == NULL || targ == NULL)
+       || (!this.monster_attackfunc)
+       || (time < this.attack_finished_single[0])
+       ) { return; }
 
-void Monster_Fade ()
-{
-       if(Monster_CanRespawn(self))
+       if(vdist(targ.origin - this.origin, <=, this.attack_range))
        {
-               self.spawnflags |= MONSTERFLAG_RESPAWNED;
-               self.think = monster_respawn;
-               self.nextthink = time + self.respawntime;
-               self.monster_lifetime = 0;
-               self.deadflag = DEAD_RESPAWNING;
-               if(self.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
-               {
-                       self.pos1 = self.origin;
-                       self.pos2 = self.angles;
-               }
-               self.event_damage = func_null;
-               self.takedamage = DAMAGE_NO;
-               setorigin(self, self.pos1);
-               self.angles = self.pos2;
-               self.health = self.max_health;
-               setmodel(self, "null");
+               bool attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ);
+               if(attack_success == 1)
+                       Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
+               else if(attack_success > 0)
+                       return;
        }
-       else
-       {
-               // number of monsters spawned with mobspawn command
-               totalspawned -= 1;
 
-               SUB_SetFade(self, time + 3, 1);
+       if(vdist(targ.origin - this.origin, >, this.attack_range))
+       {
+               float attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ);
+               if(attack_success == 1)
+                       Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
+               else if(attack_success > 0)
+                       return;
        }
 }
 
-float Monster_CanJump (vector vel)
-{
-       if(self.state)
-               return false; // already attacking
-       if(!(self.flags & FL_ONGROUND))
-               return false; // not on the ground
-       if(self.health <= 0)
-               return false; // called when dead?
-       if(time < self.attack_finished_single)
-               return false; // still attacking
-
-       vector old = self.velocity;
 
-       self.velocity = vel;
-       tracetoss(self, self);
-       self.velocity = old;
-       if (trace_ent != self.enemy)
-               return false;
+// ======================
+// Main monster functions
+// ======================
 
-       return true;
+void Monster_UpdateModel(entity this)
+{
+       // assume some defaults
+       /*this.anim_idle   = animfixfps(this, '0 1 0.01', '0 0 0');
+       this.anim_walk   = animfixfps(this, '1 1 0.01', '0 0 0');
+       this.anim_run    = animfixfps(this, '2 1 0.01', '0 0 0');
+       this.anim_fire1  = animfixfps(this, '3 1 0.01', '0 0 0');
+       this.anim_fire2  = animfixfps(this, '4 1 0.01', '0 0 0');
+       this.anim_melee  = animfixfps(this, '5 1 0.01', '0 0 0');
+       this.anim_pain1  = animfixfps(this, '6 1 0.01', '0 0 0');
+       this.anim_pain2  = animfixfps(this, '7 1 0.01', '0 0 0');
+       this.anim_die1   = animfixfps(this, '8 1 0.01', '0 0 0');
+       this.anim_die2   = animfixfps(this, '9 1 0.01', '0 0 0');*/
+
+       // then get the real values
+       Monster mon = get_monsterinfo(this.monsterid);
+       mon.mr_anim(mon, this);
 }
 
-float monster_leap (float anm, void() touchfunc, vector vel, float anim_finished)
+void Monster_Touch(entity this, entity toucher)
 {
-       if(!Monster_CanJump(vel))
-               return false;
+       if(toucher == NULL) { return; }
 
-       self.frame = anm;
-       self.state = MONSTER_STATE_ATTACK_LEAP;
-       self.touch = touchfunc;
-       self.origin_z += 1;
-       self.velocity = vel;
-       self.flags &= ~FL_ONGROUND;
+       if(toucher.monster_attack)
+       if(this.enemy != toucher)
+       if(!IS_MONSTER(toucher))
+       if(Monster_ValidTarget(this, toucher))
+               this.enemy = toucher;
+}
 
-       self.attack_finished_single = time + anim_finished;
+void Monster_Miniboss_Check(entity this)
+{
+       if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
+               return;
 
-       return true;
+       float chance = random() * 100;
+
+       // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
+       if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
+       {
+               this.health += autocvar_g_monsters_miniboss_healthboost;
+               this.effects |= EF_RED;
+               if(!this.weapon)
+                       this.weapon = WEP_VORTEX.m_id;
+       }
 }
 
-void monster_checkattack(entity e, entity targ)
+bool Monster_Respawn_Check(entity this)
 {
-       if(e == world)
-               return;
-       if(targ == world)
-               return;
+       if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
+       if(MUTATOR_CALLHOOK(MonsterRespawn, this))
+               return true; // enabled by a mutator
 
-       if(!e.monster_attackfunc)
-               return;
+       if(this.spawnflags & MONSTERFLAG_NORESPAWN)
+               return false;
 
-       if(time < e.attack_finished_single)
-               return;
+       if(!autocvar_g_monsters_respawn)
+               return false;
+
+       return true;
+}
 
-       if(vlen(targ.origin - e.origin) <= e.attack_range)
-       if(e.monster_attackfunc(MONSTER_ATTACK_MELEE))
+void Monster_Respawn(entity this) { Monster_Spawn(this, this.monsterid); }
+
+void Monster_Dead_Fade(entity this)
+{
+       if(Monster_Respawn_Check(this))
        {
-               MonsterSound(monstersound_melee, 0, false, CH_VOICE);
-               return;
+               this.spawnflags |= MONSTERFLAG_RESPAWNED;
+               setthink(this, Monster_Respawn);
+               this.nextthink = time + this.respawntime;
+               this.monster_lifetime = 0;
+               this.deadflag = DEAD_RESPAWNING;
+               if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
+               {
+                       this.pos1 = this.origin;
+                       this.pos2 = this.angles;
+               }
+               this.event_damage = func_null;
+               this.takedamage = DAMAGE_NO;
+               setorigin(this, this.pos1);
+               this.angles = this.pos2;
+               this.health = this.max_health;
+               setmodel(this, MDL_Null);
        }
-
-       if(vlen(targ.origin - e.origin) > e.attack_range)
-       if(e.monster_attackfunc(MONSTER_ATTACK_RANGED))
+       else
        {
-               MonsterSound(monstersound_ranged, 0, false, CH_VOICE);
-               return;
+               // number of monsters spawned with mobspawn command
+               totalspawned -= 1;
+
+               SUB_SetFade(this, time + 3, 1);
        }
 }
 
-void monster_use ()
+void Monster_Use(entity this, entity actor, entity trigger)
 {
-       if(!self.enemy)
-       if(self.health > 0)
-       if(monster_isvalidtarget(activator, self))
-               self.enemy = activator;
+       if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
 }
 
-.float last_trace;
-.float last_enemycheck; // for checking enemy
-vector monster_pickmovetarget(entity targ)
+vector Monster_Move_Target(entity this, entity targ)
 {
        // enemy is always preferred target
-       if(self.enemy)
+       if(this.enemy)
        {
-               vector targ_origin = ((self.enemy.absmin + self.enemy.absmax) * 0.5);
-               targ_origin = WarpZone_RefSys_TransformOrigin(self.enemy, self, targ_origin); // origin of target as seen by the monster (us)
-               WarpZone_TraceLine(self.origin, targ_origin, MOVE_NOMONSTERS, self);
-
-               if((self.enemy == world)
-                       || (self.enemy.deadflag != DEAD_NO || self.enemy.health < 1)
-                       || (self.enemy.frozen)
-                       || (self.enemy.flags & FL_NOTARGET)
-                       || (self.enemy.alpha < 0.5)
-                       || (self.enemy.takedamage == DAMAGE_NO)
-                       || (vlen(self.origin - targ_origin) > self.target_range)
-                       || ((trace_fraction < 1) && (trace_ent != self.enemy)))
-                       //|| (time > self.ctf_droptime + autocvar_g_ctf_pass_timelimit)) // TODO: chase timelimit?
+               vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
+               targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
+               WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
+
+               if((this.enemy == NULL)
+                       || (IS_DEAD(this.enemy) || this.enemy.health < 1)
+                       || (STAT(FROZEN, this.enemy))
+                       || (this.enemy.flags & FL_NOTARGET)
+                       || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
+                       || (this.enemy.takedamage == DAMAGE_NO)
+                       || (vdist(this.origin - targ_origin, >, this.target_range))
+                       || ((trace_fraction < 1) && (trace_ent != this.enemy)))
                {
-                       self.enemy = world;
-                       self.pass_distance = 0;
+                       this.enemy = NULL;
+                       this.pass_distance = 0;
                }
 
-               if(self.enemy)
+               if(this.enemy)
                {
-                       /*WarpZone_TrailParticles(world, particleeffectnum("red_pass"), self.origin, targ_origin);
+                       /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
                        print("Trace origin: ", vtos(targ_origin), "\n");
-                       print("Target origin: ", vtos(self.enemy.origin), "\n");
-                       print("My origin: ", vtos(self.origin), "\n"); */
+                       print("Target origin: ", vtos(this.enemy.origin), "\n");
+                       print("My origin: ", vtos(this.origin), "\n"); */
 
-                       self.monster_movestate = MONSTER_MOVE_ENEMY;
-                       self.last_trace = time + 1.2;
-                       return targ_origin;
+                       this.monster_movestate = MONSTER_MOVE_ENEMY;
+                       this.last_trace = time + 1.2;
+                       if(this.monster_moveto)
+                               return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
+                       else
+                               return targ_origin;
                }
 
-               /*makevectors(self.angles);
-               self.monster_movestate = MONSTER_MOVE_ENEMY;
-               self.last_trace = time + 1.2;
-               return self.enemy.origin; */
+               /*makevectors(this.angles);
+               this.monster_movestate = MONSTER_MOVE_ENEMY;
+               this.last_trace = time + 1.2;
+               return this.enemy.origin; */
        }
 
-       switch(self.monster_moveflags)
+       switch(this.monster_moveflags)
        {
-               case MONSTER_MOVE_OWNER:
+               case MONSTER_MOVE_FOLLOW:
                {
-                       self.monster_movestate = MONSTER_MOVE_OWNER;
-                       self.last_trace = time + 0.3;
-                       return (self.monster_owner) ? self.monster_owner.origin : self.origin;
+                       this.monster_movestate = MONSTER_MOVE_FOLLOW;
+                       this.last_trace = time + 0.3;
+                       return (this.monster_follow) ? this.monster_follow.origin : this.origin;
                }
                case MONSTER_MOVE_SPAWNLOC:
                {
-                       self.monster_movestate = MONSTER_MOVE_SPAWNLOC;
-                       self.last_trace = time + 2;
-                       return self.pos1;
+                       this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
+                       this.last_trace = time + 2;
+                       return this.pos1;
                }
                case MONSTER_MOVE_NOMOVE:
                {
-                       self.monster_movestate = MONSTER_MOVE_NOMOVE;
-                       self.last_trace = time + 2;
-                       return self.origin;
+                       if(this.monster_moveto)
+                       {
+                               this.last_trace = time + 0.5;
+                               return this.monster_moveto;
+                       }
+                       else
+                       {
+                               this.monster_movestate = MONSTER_MOVE_NOMOVE;
+                               this.last_trace = time + 2;
+                       }
+                       return this.origin;
                }
                default:
                case MONSTER_MOVE_WANDER:
                {
                        vector pos;
-                       self.monster_movestate = MONSTER_MOVE_WANDER;
+                       this.monster_movestate = MONSTER_MOVE_WANDER;
 
-                       if(targ)
+                       if(this.monster_moveto)
+                       {
+                               this.last_trace = time + 0.5;
+                               pos = this.monster_moveto;
+                       }
+                       else if(targ)
                        {
-                               self.last_trace = time + 0.5;
+                               this.last_trace = time + 0.5;
                                pos = targ.origin;
                        }
                        else
                        {
-                               self.last_trace = time + self.wander_delay;
+                               this.last_trace = time + this.wander_delay;
 
-                               self.angles_y = rint(random() * 500);
-                               makevectors(self.angles);
-                               pos = self.origin + v_forward * self.wander_distance;
+                               this.angles_y = rint(random() * 500);
+                               makevectors(this.angles);
+                               pos = this.origin + v_forward * this.wander_distance;
 
-                               if(((self.flags & FL_FLY) && (self.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (self.flags & FL_SWIM))
+                               if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
                                {
                                        pos.z = random() * 200;
                                        if(random() >= 0.5)
@@ -588,370 +651,352 @@ vector monster_pickmovetarget(entity targ)
        }
 }
 
-void monster_CalculateVelocity(entity mon, vector to, vector from, float turnrate, float movespeed)
+void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
 {
        float current_distance = vlen((('1 0 0' * to.x) + ('0 1 0' * to.y)) - (('1 0 0' * from.x) + ('0 1 0' * from.y))); // for the sake of this check, exclude Z axis
        float initial_height = 0; //min(50, (targ_distance * tanh(20)));
-       float current_height = (initial_height * min(1, self.pass_distance ? (current_distance / self.pass_distance) : 0));
+       float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
        //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
 
        vector targpos;
        if(current_height) // make sure we can actually do this arcing path
        {
                targpos = (to + ('0 0 1' * current_height));
-               WarpZone_TraceLine(mon.origin, targpos, MOVE_NOMONSTERS, mon);
+               WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
                if(trace_fraction < 1)
                {
                        //print("normal arc line failed, trying to find new pos...");
-                       WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, mon);
+                       WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
                        targpos = (trace_endpos + '0 0 -10');
-                       WarpZone_TraceLine(mon.origin, targpos, MOVE_NOMONSTERS, mon);
+                       WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
                        if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
                        /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
                }
        }
        else { targpos = to; }
 
-       //mon.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
+       //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
 
        vector desired_direction = normalize(targpos - from);
-       if(turnrate) { mon.velocity = (normalize(normalize(mon.velocity) + (desired_direction * 50)) * movespeed); }
-       else { mon.velocity = (desired_direction * movespeed); }
+       if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
+       else { this.velocity = (desired_direction * movespeed); }
 
-       //mon.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
-       //mon.angles = vectoangles(mon.velocity);
+       //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
+       //this.angles = vectoangles(this.velocity);
 }
 
-void monster_move(float runspeed, float walkspeed, float stopspeed, float manim_run, float manim_walk, float manim_idle)
+void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
 {
-       //fixedmakevectors(self.angles);
-
-       if(self.target2)
-               self.goalentity = find(world, targetname, self.target2);
+       if(this.target2) { this.goalentity = find(NULL, targetname, this.target2); }
 
        entity targ;
 
-       if(self.frozen == 2)
+       if(STAT(FROZEN, this) == 2)
        {
-               self.revive_progress = bound(0, self.revive_progress + self.ticrate * self.revive_speed, 1);
-               self.health = max(1, self.revive_progress * self.max_health);
-               self.iceblock.alpha = bound(0.2, 1 - self.revive_progress, 1);
+               this.revive_progress = bound(0, this.revive_progress + this.ticrate * this.revive_speed, 1);
+               this.health = max(1, this.revive_progress * this.max_health);
+               this.iceblock.alpha = bound(0.2, 1 - this.revive_progress, 1);
 
-               WaypointSprite_UpdateHealth(self.sprite, self.health);
+               if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
+                       WaypointSprite_UpdateHealth(this.sprite, this.health);
 
-               movelib_beak_simple(stopspeed);
-               self.frame = manim_idle;
+               movelib_brake_simple(this, stpspeed);
+               setanim(this, this.anim_idle, true, false, false);
 
-               self.enemy = world;
-               self.nextthink = time + self.ticrate;
+               this.enemy = NULL;
+               this.nextthink = time + this.ticrate;
 
-               if(self.revive_progress >= 1)
-                       Unfreeze(self);
+               if(this.revive_progress >= 1)
+                       Unfreeze(this);
 
                return;
        }
-       else if(self.frozen == 3)
+       else if(STAT(FROZEN, this) == 3)
        {
-               self.revive_progress = bound(0, self.revive_progress - self.ticrate * self.revive_speed, 1);
-               self.health = max(0, autocvar_g_nades_ice_health + (self.max_health-autocvar_g_nades_ice_health) * self.revive_progress );
+               this.revive_progress = bound(0, this.revive_progress - this.ticrate * this.revive_speed, 1);
+               this.health = max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * this.revive_progress );
 
-               WaypointSprite_UpdateHealth(self.sprite, self.health);
+               if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
+                       WaypointSprite_UpdateHealth(this.sprite, this.health);
 
-               movelib_beak_simple(stopspeed);
-               self.frame = manim_idle;
+               movelib_brake_simple(this, stpspeed);
+               setanim(this, this.anim_idle, true, false, false);
 
-               self.enemy = world;
-               self.nextthink = time + self.ticrate;
+               this.enemy = NULL;
+               this.nextthink = time + this.ticrate;
 
-               if(self.health < 1)
+               if(this.health < 1)
                {
-                       Unfreeze(self);
-                       self.health = 0;
-                       self.event_damage(self, self.frozen_by, 1, DEATH_NADE_ICE_FREEZE, self.origin, '0 0 0');
+                       Unfreeze(this);
+                       this.health = 0;
+                       if(this.event_damage)
+                               this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, this.origin, '0 0 0');
                }
 
-               else if ( self.revive_progress <= 0 )
-                       Unfreeze(self);
+               else if ( this.revive_progress <= 0 )
+                       Unfreeze(this);
 
                return;
        }
 
-       if(self.flags & FL_SWIM)
+       if(this.flags & FL_SWIM)
        {
-               if(self.waterlevel < WATERLEVEL_WETFEET)
+               if(this.waterlevel < WATERLEVEL_WETFEET)
                {
-                       if(time >= self.last_trace)
+                       if(time >= this.last_trace)
                        {
-                               self.fish_wasdrowning = true;
-                               self.last_trace = time + 0.4;
+                               this.last_trace = time + 0.4;
 
-                               Damage (self, world, world, 2, DEATH_DROWN, self.origin, '0 0 0');
-                               self.angles = '90 90 0';
+                               Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, this.origin, '0 0 0');
+                               this.angles = '90 90 0';
                                if(random() < 0.5)
                                {
-                                       self.velocity_y += random() * 50;
-                                       self.velocity_x -= random() * 50;
+                                       this.velocity_y += random() * 50;
+                                       this.velocity_x -= random() * 50;
                                }
                                else
                                {
-                                       self.velocity_y -= random() * 50;
-                                       self.velocity_x += random() * 50;
+                                       this.velocity_y -= random() * 50;
+                                       this.velocity_x += random() * 50;
                                }
-                               self.velocity_z += random() * 150;
+                               this.velocity_z += random() * 150;
                        }
 
 
-                       self.movetype = MOVETYPE_BOUNCE;
-                       //self.velocity_z = -200;
+                       this.movetype = MOVETYPE_BOUNCE;
+                       //this.velocity_z = -200;
 
                        return;
                }
-               else if(self.fish_wasdrowning)
+               else if(this.movetype == MOVETYPE_BOUNCE)
                {
-                       self.fish_wasdrowning = false;
-                       self.angles_x = 0;
-                       self.movetype = MOVETYPE_WALK;
+                       this.angles_x = 0;
+                       this.movetype = MOVETYPE_WALK;
                }
        }
 
-       targ = self.goalentity;
+       targ = this.goalentity;
 
-       if (MUTATOR_CALLHOOK(MonsterMove, runspeed, walkspeed, targ)
+       if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
                || gameover
-               || self.draggedby != world
+               || this.draggedby != NULL
                || (round_handler_IsActive() && !round_handler_IsRoundStarted())
                || time < game_starttime
                || (autocvar_g_campaign && !campaign_bots_may_start)
-               || time < self.spawn_time)
+               || time < this.spawn_time)
        {
                runspeed = walkspeed = 0;
-               if(time >= self.spawn_time)
-                       self.frame = manim_idle;
-               movelib_beak_simple(stopspeed);
+               if(time >= this.spawn_time)
+                       setanim(this, this.anim_idle, true, false, false);
+               movelib_brake_simple(this, stpspeed);
                return;
        }
 
-       runspeed = bound(0, monster_speed_run * Monster_SkillModifier(), runspeed * 2); // limit maxspeed to prevent craziness
-       walkspeed = bound(0, monster_speed_walk * Monster_SkillModifier(), walkspeed * 2); // limit maxspeed to prevent craziness
-
-       if(time < self.spider_slowness)
-       {
-               runspeed *= 0.5;
-               walkspeed *= 0.5;
-       }
+       targ = M_ARGV(3, entity);
+       runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
+       walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
 
        if(teamplay)
        if(autocvar_g_monsters_teams)
-       if(DIFF_TEAM(self.monster_owner, self))
-               self.monster_owner = world;
+       if(DIFF_TEAM(this.monster_follow, this))
+               this.monster_follow = NULL;
 
-       if(time >= self.last_enemycheck)
+       if(time >= this.last_enemycheck)
        {
-               if(!self.enemy)
+               if(!this.enemy)
                {
-                       self.enemy = FindTarget(self);
-                       if(self.enemy)
+                       this.enemy = Monster_FindTarget(this);
+                       if(this.enemy)
                        {
-                               WarpZone_RefSys_Copy(self.enemy, self);
-                               WarpZone_RefSys_AddInverse(self.enemy, self); // wz1^-1 ... wzn^-1 receiver
-                               self.moveto = WarpZone_RefSys_TransformOrigin(self.enemy, self, (0.5 * (self.enemy.absmin + self.enemy.absmax)));
-
-                               self.pass_distance = vlen((('1 0 0' * self.enemy.origin.x) + ('0 1 0' * self.enemy.origin.y)) - (('1 0 0' *  self.origin.x) + ('0 1 0' *  self.origin.y)));
-                               MonsterSound(monstersound_sight, 0, false, CH_VOICE);
+                               WarpZone_RefSys_Copy(this.enemy, this);
+                               WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
+                               this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
+                               this.monster_moveto = '0 0 0';
+                               this.monster_face = '0 0 0';
+
+                               this.pass_distance = vlen((('1 0 0' * this.enemy.origin_x) + ('0 1 0' * this.enemy.origin_y)) - (('1 0 0' *  this.origin_x) + ('0 1 0' *  this.origin_y)));
+                               Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
                        }
                }
 
-               self.last_enemycheck = time + 1; // check for enemies every second
+               this.last_enemycheck = time + 1; // check for enemies every second
        }
 
-       if(self.state == MONSTER_STATE_ATTACK_MELEE && time >= self.attack_finished_single)
-               self.state = 0;
+       if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
+       {
+               this.state = 0;
+               settouch(this, Monster_Touch);
+       }
 
-       if(self.state != MONSTER_STATE_ATTACK_MELEE) // don't move if set
-       if(time >= self.last_trace || self.enemy) // update enemy instantly
-               self.moveto = monster_pickmovetarget(targ);
+       if(this.state && time >= this.attack_finished_single[0])
+               this.state = 0; // attack is over
 
-       if(!self.enemy)
-               MonsterSound(monstersound_idle, 7, true, CH_VOICE);
+       if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
+       if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
+               this.moveto = Monster_Move_Target(this, targ);
 
-       if(self.state == MONSTER_STATE_ATTACK_LEAP && (self.flags & FL_ONGROUND))
-       {
-               self.state = 0;
-               self.touch = MonsterTouch;
-       }
+       if(!this.enemy)
+               Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
 
-       if(self.state == MONSTER_STATE_ATTACK_MELEE)
-               self.moveto = self.origin;
+       if(this.state == MONSTER_ATTACK_MELEE)
+               this.moveto = this.origin;
 
-       if(self.enemy && self.enemy.vehicle)
+       if(this.enemy && this.enemy.vehicle)
                runspeed = 0;
 
-       if(!(((self.flags & FL_FLY) && (self.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (self.flags & FL_SWIM)))
-               //v_forward = normalize(self.moveto - self.origin);
-       //else
-               self.moveto_z = self.origin.z;
+       if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
+               this.moveto_z = this.origin_z;
 
-       if(vlen(self.origin - self.moveto) > 64)
+       if(vdist(this.origin - this.moveto, >, 100))
        {
-               if((self.flags & FL_ONGROUND) || ((self.flags & FL_FLY) || (self.flags & FL_SWIM)))
-                       monster_CalculateVelocity(self, self.moveto, self.origin, true, ((self.enemy) ? runspeed : walkspeed));
-
-               /*&if(self.flags & FL_FLY || self.flags & FL_SWIM)
-                       movelib_move_simple(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
-               else
-                       movelib_move_simple_gravity(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6); */
-
-               if(time > self.pain_finished)
-               if(time > self.attack_finished_single)
-               if(vlen(self.velocity) > 10)
-                       self.frame = ((self.enemy) ? manim_run : manim_walk);
+               float do_run = (this.enemy || this.monster_moveto);
+               if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
+                       Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
+
+               if(time > this.pain_finished) // TODO: use anim_finished instead!
+               if(!this.state)
+               if(time > this.anim_finished)
+               if(vdist(this.velocity, >, 10))
+                       setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
                else
-                       self.frame = manim_idle;
+                       setanim(this, this.anim_idle, true, false, false);
        }
        else
        {
-               entity e = find(world, targetname, self.target2);
+               entity e = find(NULL, targetname, this.target2);
                if(e.target2)
-                       self.target2 = e.target2;
+                       this.target2 = e.target2;
                else if(e.target)
-                       self.target2 = e.target;
-
-               movelib_beak_simple(stopspeed);
-               if(time > self.attack_finished_single)
-               if(time > self.pain_finished)
-               if (vlen(self.velocity) <= 30)
-                       self.frame = manim_idle;
+                       this.target2 = e.target;
+
+               movelib_brake_simple(this, stpspeed);
+               if(time > this.anim_finished)
+               if(time > this.pain_finished)
+               if(!this.state)
+               if(vdist(this.velocity, <=, 30))
+                       setanim(this, this.anim_idle, true, false, false);
        }
 
-       self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
+       this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
 
-       vector real_angle = vectoangles(self.steerto) - self.angles;
+       vector real_angle = vectoangles(this.steerto) - this.angles;
        float turny = 25;
-       if(self.state == MONSTER_STATE_ATTACK_MELEE)
+       if(this.state == MONSTER_ATTACK_MELEE)
                turny = 0;
        if(turny)
        {
-               turny = bound(turny * -1, shortangle_f(real_angle.y, self.angles.y), turny);
-               self.angles_y += turny;
+               turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
+               this.angles_y += turny;
        }
 
-       monster_checkattack(self, self.enemy);
+       Monster_Attack_Check(this, this.enemy);
 }
 
-void monster_remove(entity mon)
+void Monster_Remove(entity this)
 {
-       if(!mon)
-               return; // nothing to remove
-
-       Send_Effect("item_pickup", mon.origin, '0 0 0', 1);
-
-       if(mon.weaponentity)
-               remove(mon.weaponentity);
+       if(IS_CLIENT(this))
+               return; // don't remove it?
 
-       if(mon.iceblock)
-               remove(mon.iceblock);
+       .entity weaponentity = weaponentities[0];
+       if(!this) { return; }
 
-       WaypointSprite_Kill(mon.sprite);
+       if(!MUTATOR_CALLHOOK(MonsterRemove, this))
+               Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
 
-       remove(mon);
+       if(this.(weaponentity)) { remove(this.(weaponentity)); }
+       if(this.iceblock) { remove(this.iceblock); }
+       WaypointSprite_Kill(this.sprite);
+       remove(this);
 }
 
-void monster_dead_think()
+void Monster_Dead_Think(entity this)
 {
-       self.nextthink = time + self.ticrate;
+       this.nextthink = time + this.ticrate;
 
-       CSQCMODEL_AUTOUPDATE();
-
-       if(self.monster_lifetime != 0)
-       if(time >= self.monster_lifetime)
+       if(this.monster_lifetime != 0)
+       if(time >= this.monster_lifetime)
        {
-               Monster_Fade();
+               Monster_Dead_Fade(this);
                return;
        }
 }
 
-void monsters_setstatus()
-{
-       self.stat_monsters_total = monsters_total;
-       self.stat_monsters_killed = monsters_killed;
-}
-
-void Monster_Appear()
+void Monster_Appear(entity this, entity actor, entity trigger)
 {
-       self.enemy = activator;
-       self.spawnflags &= ~MONSTERFLAG_APPEAR; // otherwise, we get an endless loop
-       monster_initialize(self.monsterid);
+       this.enemy = actor;
+       this.spawnflags &= ~MONSTERFLAG_APPEAR; // otherwise, we get an endless loop
+       Monster_Spawn(this, this.monsterid);
 }
 
-float Monster_CheckAppearFlags(entity ent, float monster_id)
+bool Monster_Appear_Check(entity this, int monster_id)
 {
-       if(!(ent.spawnflags & MONSTERFLAG_APPEAR))
+       if(!(this.spawnflags & MONSTERFLAG_APPEAR))
                return false;
 
-       ent.think = func_null;
-       ent.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
-       ent.nextthink = 0;
-       ent.use = Monster_Appear;
-       ent.flags = FL_MONSTER; // set so this monster can get butchered
+       setthink(this, func_null);
+       this.monsterid = 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
 
        return true;
 }
 
-void monsters_reset()
+void Monster_Reset(entity this)
 {
-       setorigin(self, self.pos1);
-       self.angles = self.pos2;
+       setorigin(this, this.pos1);
+       this.angles = this.pos2;
 
-       Unfreeze(self); // remove any icy remains
+       Unfreeze(this); // remove any icy remains
 
-       self.health = self.max_health;
-       self.velocity = '0 0 0';
-       self.enemy = world;
-       self.goalentity = world;
-       self.attack_finished_single = 0;
-       self.moveto = self.origin;
+       this.health = this.max_health;
+       this.velocity = '0 0 0';
+       this.enemy = NULL;
+       this.goalentity = NULL;
+       this.attack_finished_single[0] = 0;
+       this.moveto = this.origin;
 }
 
-void monsters_corpse_damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
+void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
 {
-       self.health -= damage;
+       this.health -= damage;
 
-       Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
+       Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
 
-       if(self.health <= -100) // 100 health until gone?
+       if(this.health <= -50) // 100 health until gone?
        {
-               Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
+               Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
 
                // number of monsters spawned with mobspawn command
                totalspawned -= 1;
 
-               self.think = SUB_Remove;
-               self.nextthink = time + 0.1;
-               self.event_damage = func_null;
+               setthink(this, SUB_Remove);
+               this.nextthink = time + 0.1;
+               this.event_damage = func_null;
        }
 }
 
-void monster_die(entity attacker, float gibbed)
+void Monster_Dead(entity this, entity attacker, float gibbed)
 {
-       self.think = monster_dead_think;
-       self.nextthink = time;
-       self.monster_lifetime = time + 5;
+       setthink(this, Monster_Dead_Think);
+       this.nextthink = time;
+       this.monster_lifetime = time + 5;
 
-       if(self.frozen)
+       if(STAT(FROZEN, this))
        {
-               Unfreeze(self); // remove any icy remains
-               self.health = 0; // reset by Unfreeze
+               Unfreeze(this); // remove any icy remains
+               this.health = 0; // reset by Unfreeze
        }
 
-       monster_dropitem();
+       monster_dropitem(this, attacker);
 
-       MonsterSound(monstersound_death, 0, false, CH_VOICE);
+       Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
 
-       if(!(self.spawnflags & MONSTERFLAG_SPAWNED) && !(self.spawnflags & MONSTERFLAG_RESPAWNED))
+       if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
                monsters_killed += 1;
 
        if(IS_PLAYER(attacker))
-       if(autocvar_g_monsters_score_spawned || !((self.spawnflags & MONSTERFLAG_SPAWNED) || (self.spawnflags & MONSTERFLAG_RESPAWNED)))
+       if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
                PlayerScore_Add(attacker, SP_SCORE, +autocvar_g_monsters_score_kill);
 
        if(gibbed)
@@ -960,322 +1005,392 @@ void monster_die(entity attacker, float gibbed)
                totalspawned -= 1;
        }
 
-       if(self.candrop && self.weapon)
-               W_ThrowNewWeapon(self, self.weapon, 0, self.origin, randomvec() * 150 + '0 0 325');
-
-       self.event_damage       = ((gibbed) ? func_null : monsters_corpse_damage);
-       self.solid                      = SOLID_CORPSE;
-       self.takedamage         = DAMAGE_AIM;
-       self.deadflag           = DEAD_DEAD;
-       self.enemy                      = world;
-       self.movetype           = MOVETYPE_TOSS;
-       self.moveto                     = self.origin;
-       self.touch                      = MonsterTouch; // reset incase monster was pouncing
-       self.reset                      = func_null;
-       self.state                      = 0;
-       self.attack_finished_single = 0;
-
-       if(!((self.flags & FL_FLY) || (self.flags & FL_SWIM)))
-               self.velocity = '0 0 0';
-
-       MON_ACTION(self.monsterid, MR_DEATH);
+       this.event_damage       = ((gibbed) ? func_null : Monster_Dead_Damage);
+       this.solid                      = SOLID_CORPSE;
+       this.takedamage         = DAMAGE_AIM;
+       this.deadflag           = DEAD_DEAD;
+       this.enemy                      = NULL;
+       this.movetype           = MOVETYPE_TOSS;
+       this.moveto                     = this.origin;
+       settouch(this, Monster_Touch); // reset incase monster was pouncing
+       this.reset                      = func_null;
+       this.state                      = 0;
+       this.attack_finished_single[0] = 0;
+       this.effects = 0;
+
+       if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
+               this.velocity = '0 0 0';
+
+       CSQCModel_UnlinkEntity(this);
+
+       Monster mon = get_monsterinfo(this.monsterid);
+       mon.mr_death(mon, this);
+
+       if(this.candrop && this.weapon)
+               W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325');
 }
 
-void monsters_damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
+void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
 {
-       if(self.frozen && deathtype != DEATH_KILL && deathtype != DEATH_NADE_ICE_FREEZE)
+       if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
                return;
 
-       if((self.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL)
+       if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
                return;
 
-       if(time < self.pain_finished && deathtype != DEATH_KILL)
-               return;
+       //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
+               //return;
 
-       if(time < self.spawnshieldtime && deathtype != DEATH_KILL)
+       if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
                return;
 
-       if(deathtype == DEATH_FALL && self.draggedby != world)
+       if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
                return;
 
        vector v;
        float take, save;
 
-       v = healtharmor_applydamage(self.armorvalue, self.m_armor_blockpercent, deathtype, damage);
-       take = v.x;
-       save = v.y;
+       v = healtharmor_applydamage(100, this.armorvalue / 100, deathtype, damage);
+       take = v_x;
+       save = v_y;
+
+       Monster mon = get_monsterinfo(this.monsterid);
+       take = mon.mr_pain(mon, this, take, attacker, deathtype);
 
-       self.health -= take;
+       if(take)
+       {
+               this.health -= take;
+               Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
+       }
 
-       WaypointSprite_UpdateHealth(self.sprite, self.health);
+       if(this.sprite)
+               WaypointSprite_UpdateHealth(this.sprite, this.health);
 
-       self.dmg_time = time;
+       this.dmg_time = time;
 
-       if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN)
-               spamsound (self, CH_PAIN, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
+       if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN.m_id)
+               spamsound (this, CH_PAIN, SND(BODYIMPACT1), VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
 
-       self.velocity += force * self.damageforcescale;
+       this.velocity += force * this.damageforcescale;
 
-       if(deathtype != DEATH_DROWN)
+       if(deathtype != DEATH_DROWN.m_id && take)
        {
-               Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, self, attacker);
+               Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
                if (take > 50)
-                       Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
+                       Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
                if (take > 100)
-                       Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
+                       Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
        }
 
-       if(self.health <= 0)
+       if(this.health <= 0)
        {
-               if(deathtype == DEATH_KILL)
-                       self.candrop = false; // killed by mobkill command
+               if(deathtype == DEATH_KILL.m_id)
+                       this.candrop = false; // killed by mobkill command
 
                // TODO: fix this?
-               activator = attacker;
-               other = self.enemy;
-               SUB_UseTargets();
-               self.target2 = self.oldtarget2; // reset to original target on death, incase we respawn
+               SUB_UseTargets(this, attacker, this.enemy);
+               this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
 
-               monster_die(attacker, (self.health <= -100 || deathtype == DEATH_KILL));
+               Monster_Dead(this, attacker, (this.health <= -100 || deathtype == DEATH_KILL.m_id));
 
-               WaypointSprite_Kill(self.sprite);
+               WaypointSprite_Kill(this.sprite);
 
-               frag_target = self;
-               MUTATOR_CALLHOOK(MonsterDies, attacker);
+               MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
 
-               if(self.health <= -100 || deathtype == DEATH_KILL) // check if we're already gibbed
+               if(this.health <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
                {
-                       Violence_GibSplash(self, 1, 0.5, attacker);
+                       Violence_GibSplash(this, 1, 0.5, attacker);
 
-                       self.think = SUB_Remove;
-                       self.nextthink = time + 0.1;
+                       setthink(this, SUB_Remove);
+                       this.nextthink = time + 0.1;
                }
        }
 }
 
-void monster_setupcolors(entity mon)
+// don't check for enemies, just keep walking in a straight line
+void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
 {
-       if(IS_PLAYER(mon.monster_owner))
-               mon.colormap = mon.monster_owner.colormap;
-       else if(teamplay && mon.team)
-               mon.colormap = 1024 + (mon.team - 1) * 17;
-       else
+       if(gameover || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || this.draggedby != NULL || time < game_starttime || (autocvar_g_campaign && !campaign_bots_may_start) || time < this.spawn_time)
        {
-               if(mon.monster_skill <= MONSTER_SKILL_EASY)
-                       mon.colormap = 1029;
-               else if(mon.monster_skill <= MONSTER_SKILL_MEDIUM)
-                       mon.colormap = 1027;
-               else if(mon.monster_skill <= MONSTER_SKILL_HARD)
-                       mon.colormap = 1038;
-               else if(mon.monster_skill <= MONSTER_SKILL_INSANE)
-                       mon.colormap = 1028;
-               else if(mon.monster_skill <= MONSTER_SKILL_NIGHTMARE)
-                       mon.colormap = 1032;
-               else
-                       mon.colormap = 1024;
+               mspeed = 0;
+               if(time >= this.spawn_time)
+                       setanim(this, this.anim_idle, true, false, false);
+               movelib_brake_simple(this, 0.6);
+               return;
        }
-}
 
-void monster_changeteam(entity ent, float newteam)
-{
-       if(!teamplay) { return; }
+       float reverse = false;
+       vector a, b;
 
-       ent.team = newteam;
-       ent.monster_attack = true; // new team, activate attacking
-       monster_setupcolors(ent);
+       makevectors(this.angles);
+       a = this.origin + '0 0 16';
+       b = this.origin + '0 0 16' + v_forward * 32;
 
-       if(ent.sprite)
+       traceline(a, b, MOVE_NORMAL, this);
+
+       if(trace_fraction != 1.0)
        {
-               WaypointSprite_UpdateTeamRadar(ent.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
+               reverse = true;
 
-               ent.sprite.team = newteam;
-               ent.sprite.SendFlags |= 1;
+               if(trace_ent)
+               if(IS_PLAYER(trace_ent) && !(trace_ent.items & IT_STRENGTH))
+                       reverse = false;
        }
-}
 
-void monster_think()
-{
-       self.think = monster_think;
-       self.nextthink = self.ticrate;
+       // TODO: fix this... tracing is broken if the floor is thin
+       /*
+       if(!allow_jumpoff)
+       {
+               a = b - '0 0 32';
+               traceline(b, a, MOVE_WORLDONLY, this);
+               if(trace_fraction == 1.0)
+                       reverse = true;
+       } */
 
-       if(self.monster_lifetime)
-       if(time >= self.monster_lifetime)
+       if(reverse)
        {
-               Damage(self, self, self, self.health + self.max_health, DEATH_KILL, self.origin, self.origin);
-               return;
+               this.angles_y = anglemods(this.angles_y - 180);
+               makevectors(this.angles);
        }
 
-       MON_ACTION(self.monsterid, MR_THINK);
+       movelib_move_simple_gravity(this, v_forward, mspeed, 1);
 
-       CSQCMODEL_AUTOUPDATE();
+       if(time > this.pain_finished)
+       if(time > this.attack_finished_single[0])
+       if(vdist(this.velocity, >, 10))
+               setanim(this, this.anim_walk, true, false, false);
+       else
+               setanim(this, this.anim_idle, true, false, false);
 }
 
-float monster_spawn()
+void Monster_Anim(entity this)
 {
-       MON_ACTION(self.monsterid, MR_SETUP);
+       int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
+       if(IS_DEAD(this))
+       {
+               if (!deadbits)
+               {
+                       // Decide on which death animation to use.
+                       if(random() < 0.5)
+                               deadbits = ANIMSTATE_DEAD1;
+                       else
+                               deadbits = ANIMSTATE_DEAD2;
+               }
+       }
+       else
+       {
+               // Clear a previous death animation.
+               deadbits = 0;
+       }
+       int animbits = deadbits;
+       if(STAT(FROZEN, this))
+               animbits |= ANIMSTATE_FROZEN;
+       if(this.crouch)
+               animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
+       animdecide_setstate(this, animbits, false);
+       animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
+
+       /* // weapon entities for monsters?
+       if (this.weaponentity)
+       {
+               updateanim(this.weaponentity);
+               if (!this.weaponentity.animstate_override)
+                       setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
+       }
+       */
+}
+
+void Monster_Think(entity this)
+{
+       setthink(this, Monster_Think);
+       this.nextthink = this.ticrate;
 
-       if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
+       if(this.monster_lifetime)
+       if(time >= this.monster_lifetime)
        {
-               Monster_CheckMinibossFlag();
-               self.health *= Monster_SkillModifier();
+               Damage(this, this, this, this.health + this.max_health, DEATH_KILL.m_id, this.origin, this.origin);
+               return;
        }
 
-       self.max_health = self.health;
-       self.pain_finished = self.nextthink;
+       Monster mon = get_monsterinfo(this.monsterid);
+       if(mon.mr_think(mon, this))
+               Monster_Move(this, this.speed2, this.speed, this.stopspeed);
 
-       if(IS_PLAYER(self.monster_owner))
-               self.effects |= EF_DIMLIGHT;
+       Monster_Anim(this);
 
-       if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
-       if(!self.skin)
-               self.skin = rint(random() * 4);
+       CSQCMODEL_AUTOUPDATE(this);
+}
 
-       if(!self.attack_range)
-               self.attack_range = autocvar_g_monsters_attack_range;
+bool Monster_Spawn_Setup(entity this)
+{
+       Monster mon = Monsters_from(this.monsterid);
+       mon.mr_setup(mon, this);
+
+       // ensure some basic needs are met
+       if(!this.health) { this.health = 100; }
+       if(!this.armorvalue) { this.armorvalue = bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9); }
+       if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
+       if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
+       if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
+       if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
+       if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
+
+       if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
+       {
+               Monster_Miniboss_Check(this);
+               this.health *= MONSTER_SKILLMOD(this);
 
-       if(!self.wander_delay) { self.wander_delay = 2; }
-       if(!self.wander_distance) { self.wander_distance = 600; }
+               if(!this.skin)
+                       this.skin = rint(random() * 4);
+       }
 
-       precache_monstersounds();
-       UpdateMonsterSounds();
+       this.max_health = this.health;
+       this.pain_finished = this.nextthink;
+
+       if(IS_PLAYER(this.monster_follow))
+               this.effects |= EF_DIMLIGHT;
+
+       if(!this.wander_delay) { this.wander_delay = 2; }
+       if(!this.wander_distance) { this.wander_distance = 600; }
+
+       Monster_Sounds_Precache(this);
+       Monster_Sounds_Update(this);
 
        if(teamplay)
-               self.monster_attack = true; // we can have monster enemies in team games
+               this.monster_attack = true; // we can have monster enemies in team games
 
-       MonsterSound(monstersound_spawn, 0, false, CH_VOICE);
+       Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
 
-       WaypointSprite_Spawn(M_NAME(self.monsterid), 0, 1024, self, '0 0 1' * (self.maxs.z + 15), world, self.team, self, sprite, true, RADARICON_DANGER, ((self.team) ? Team_ColorRGB(self.team) : '1 0 0'));
-       if(!(self.spawnflags & MONSTERFLAG_INVINCIBLE))
+       if(autocvar_g_monsters_healthbars)
        {
-               WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
-               WaypointSprite_UpdateHealth(self.sprite, self.health);
+               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.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
+               if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
+               {
+                       WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
+                       WaypointSprite_UpdateHealth(this.sprite, this.health);
+               }
        }
 
-       self.think = monster_think;
-       self.nextthink = time + self.ticrate;
+       setthink(this, Monster_Think);
+       this.nextthink = time + this.ticrate;
 
-       if(MUTATOR_CALLHOOK(MonsterSpawn))
+       if(MUTATOR_CALLHOOK(MonsterSpawn, this))
                return false;
 
        return true;
 }
 
-float monster_initialize(float mon_id)
+bool Monster_Spawn(entity this, int mon_id)
 {
-       if(!autocvar_g_monsters) { return false; }
-       if(!(self.spawnflags & MONSTERFLAG_RESPAWNED)) { MON_ACTION(mon_id, MR_PRECACHE); }
-       if(Monster_CheckAppearFlags(self, mon_id)) { return true; } // return true so the monster isn't removed
+       // setup the basic required properties for a monster
+       entity mon = Monsters_from(mon_id);
+       if(!mon.monsterid) { return false; } // invalid monster
+
+       if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
 
-       entity mon = get_monsterinfo(mon_id);
+       if(Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed
 
-       if(!self.monster_skill)
-               self.monster_skill = cvar("g_monsters_skill");
+       if(!this.monster_skill)
+               this.monster_skill = cvar("g_monsters_skill");
 
        // support for quake style removing monsters based on skill
-       if(self.monster_skill == MONSTER_SKILL_EASY) if(self.spawnflags & MONSTERSKILL_NOTEASY) { return false; }
-       if(self.monster_skill == MONSTER_SKILL_MEDIUM) if(self.spawnflags & MONSTERSKILL_NOTMEDIUM) { return false; }
-       if(self.monster_skill == MONSTER_SKILL_HARD) if(self.spawnflags & MONSTERSKILL_NOTHARD) { return false; }
+       if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
+       if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
+       if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
 
-       if(self.team && !teamplay)
-               self.team = 0;
+       if(this.team && !teamplay)
+               this.team = 0;
 
-       if(!(self.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
-       if(!(self.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
+       if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
+       if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
                monsters_total += 1;
 
-       setmodel(self, mon.model);
-       //setsize(self, mon.mins, mon.maxs);
-       self.flags                              = FL_MONSTER;
-       self.takedamage                 = DAMAGE_AIM;
-       self.bot_attack                 = true;
-       self.iscreature                 = true;
-       self.teleportable               = true;
-       self.damagedbycontents  = true;
-       self.monsterid                  = mon_id;
-       self.damageforcescale   = 0;
-       self.event_damage               = monsters_damage;
-       self.touch                              = MonsterTouch;
-       self.use                                = monster_use;
-       self.solid                              = SOLID_BBOX;
-       self.movetype                   = MOVETYPE_WALK;
-       self.spawnshieldtime    = time + autocvar_g_monsters_spawnshieldtime;
-       self.enemy                              = world;
-       self.velocity                   = '0 0 0';
-       self.moveto                             = self.origin;
-       self.pos1                               = self.origin;
-       self.pos2                               = self.angles;
-       self.reset                              = monsters_reset;
-       self.netname                    = mon.netname;
-       self.monster_name               = M_NAME(mon_id);
-       self.candrop                    = true;
-       self.view_ofs                   = '0 0 1' * (self.maxs.z * 0.5);
-       self.oldtarget2                 = self.target2;
-       self.pass_distance              = 0;
-       self.deadflag                   = DEAD_NO;
-       self.noalign                    = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM));
-       self.spawn_time                 = time;
-       self.spider_slowness    = 0;
-       self.gravity                    = 1;
-       self.dphitcontentsmask  = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
-
-       if(!self.scale)
-               self.scale = 1;
-
-       if(autocvar_g_monsters_edit)
-               self.grab = 1; // owner may carry their monster
-
-       if(autocvar_g_fullbrightplayers)
-               self.effects |= EF_FULLBRIGHT;
-
-       if(autocvar_g_nodepthtestplayers)
-               self.effects |= EF_NODEPTHTEST;
-
-       if(mon.spawnflags & MONSTER_TYPE_SWIM)
-               self.flags |= FL_SWIM;
+       setmodel(this, mon.m_model);
+       this.flags                              = FL_MONSTER;
+       this.classname                  = "monster";
+       this.takedamage                 = DAMAGE_AIM;
+       this.bot_attack                 = true;
+       this.iscreature                 = true;
+       this.teleportable               = true;
+       this.damagedbycontents  = true;
+       this.monsterid                  = mon_id;
+       this.event_damage               = Monster_Damage;
+       settouch(this, Monster_Touch);
+       this.use                                = Monster_Use;
+       this.solid                              = SOLID_BBOX;
+       this.movetype                   = MOVETYPE_WALK;
+       this.spawnshieldtime    = time + autocvar_g_monsters_spawnshieldtime;
+       this.enemy                              = NULL;
+       this.velocity                   = '0 0 0';
+       this.moveto                             = this.origin;
+       this.pos1                               = this.origin;
+       this.pos2                               = this.angles;
+       this.reset                              = Monster_Reset;
+       this.netname                    = mon.netname;
+       this.monster_attackfunc = mon.monster_attackfunc;
+       this.monster_name               = mon.monster_name;
+       this.candrop                    = true;
+       this.view_ofs                   = '0 0 0.7' * (this.maxs_z * 0.5);
+       this.oldtarget2                 = this.target2;
+       this.pass_distance              = 0;
+       this.deadflag                   = DEAD_NO;
+       this.noalign                    = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM));
+       this.spawn_time                 = time;
+       this.gravity                    = 1;
+       this.monster_moveto             = '0 0 0';
+       this.monster_face               = '0 0 0';
+       this.dphitcontentsmask  = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
+
+       if(!this.scale) { this.scale = 1; }
+       if(autocvar_g_monsters_edit) { this.grab = 1; }
+       if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
+       if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
+       if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
+
+       if(autocvar_g_playerclip_collisions)
+               this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
 
        if(mon.spawnflags & MONSTER_TYPE_FLY)
        {
-               self.flags |= FL_FLY;
-               self.movetype = MOVETYPE_FLY;
+               this.flags |= FL_FLY;
+               this.movetype = MOVETYPE_FLY;
        }
 
-       if(mon.spawnflags & MONSTER_SIZE_BROKEN)
-       if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
-               self.scale *= 1.3;
-
-       setsize(self, mon.mins * self.scale, mon.maxs * self.scale);
-
-       if(!self.ticrate)
-               self.ticrate = autocvar_g_monsters_think_delay;
-
-       self.ticrate = bound(sys_frametime, self.ticrate, 60);
+       if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
+       {
+               if(mon.spawnflags & MONSTER_SIZE_BROKEN)
+                       this.scale *= 1.3;
 
-       if(!self.m_armor_blockpercent)
-               self.m_armor_blockpercent = 0.5;
+               if(mon.spawnflags & MONSTER_SIZE_QUAKE)
+               if(autocvar_g_monsters_quake_resize)
+                       this.scale *= 1.3;
+       }
 
-       if(!self.target_range)
-               self.target_range = autocvar_g_monsters_target_range;
+       setsize(this, mon.mins * this.scale, mon.maxs * this.scale);
 
-       if(!self.respawntime)
-               self.respawntime = autocvar_g_monsters_respawn_delay;
+       this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
 
-       if(!self.monster_moveflags)
-               self.monster_moveflags = MONSTER_MOVE_WANDER;
+       Monster_UpdateModel(this);
 
-       if(!self.noalign)
+       if(!Monster_Spawn_Setup(this))
        {
-               setorigin(self, self.origin + '0 0 20');
-               tracebox(self.origin + '0 0 64', self.mins, self.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
-               setorigin(self, trace_endpos);
+               Monster_Remove(this);
+               return false;
        }
 
-       if(!monster_spawn())
-               return false;
+       if(!this.noalign)
+       {
+               setorigin(this, this.origin + '0 0 20');
+               tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
+               setorigin(this, trace_endpos);
+       }
 
-       if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
-               monster_setupcolors(self);
+       if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
+               monster_setupcolors(this);
 
-       CSQCMODEL_AUTOINIT();
+       CSQCMODEL_AUTOINIT(this);
 
        return true;
 }