1 #include "sv_monsters.qh"
3 #include <lib/warpzone/common.qh>
4 #include "../constants.qh"
8 #include "../physics/movelib.qh"
9 #include "../weapons/_mod.qh"
10 #include <server/autocvars.qh>
11 #include <server/defs.qh>
12 #include "../deathtypes/all.qh"
13 #include <server/mutators/_mod.qh>
14 #include <server/steerlib.qh>
15 #include "../turrets/sv_turrets.qh"
16 #include "../turrets/util.qh"
17 #include "../vehicles/all.qh"
18 #include <server/campaign.qh>
19 #include <server/command/_mod.qh>
20 #include "../mapobjects/triggers.qh"
21 #include <lib/csqcmodel/sv_model.qh>
22 #include <server/round_handler.qh>
23 #include <server/weapons/_mod.qh>
25 void monsters_setstatus(entity this)
27 STAT(MONSTERS_TOTAL, this) = monsters_total;
28 STAT(MONSTERS_KILLED, this) = monsters_killed;
31 void monster_dropitem(entity this, entity attacker)
33 if(!this.candrop || !this.monster_loot)
36 vector org = CENTER_OR_VIEWOFS(this);
38 Item_SetLoot(e, true);
39 e.spawnfunc_checked = true;
41 e.monster_loot = this.monster_loot;
43 MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
44 e = M_ARGV(1, entity);
46 if(e && e.monster_loot)
49 StartItem(e, e.monster_loot);
52 e.velocity = randomvec() * 175 + '0 0 325';
53 e.item_spawnshieldtime = time + 0.7;
54 SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
58 bool monster_facing(entity this, entity targ)
60 // relies on target having an origin
61 makevectors(this.angles);
62 vector targ_org = targ.origin, my_org = this.origin;
63 if(autocvar_g_monsters_target_infront_2d)
65 targ_org = vec2(targ_org);
66 my_org = vec2(my_org);
68 float dot = normalize(targ_org - my_org) * v_forward;
70 return !(dot <= autocvar_g_monsters_target_infront_range);
73 void monster_makevectors(entity this, entity targ)
77 vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
78 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
79 this.v_angle_x = -this.v_angle_x;
82 makevectors(this.v_angle);
89 bool Monster_ValidTarget(entity this, entity targ, bool skipfacing)
91 // ensure we're not checking nonexistent monster/target
92 if(!this || !targ) { return false; }
95 || (time < game_starttime) // monsters do nothing before match has started
96 || (targ.takedamage == DAMAGE_NO)
98 || (targ.items & IT_INVISIBILITY)
99 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
100 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || GetResourceAmount(targ, RESOURCE_HEALTH) <= 0 || GetResourceAmount(this, RESOURCE_HEALTH) <= 0))
101 || (this.monster_follow == targ || targ.monster_follow == this)
102 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
103 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
104 || (IS_VEHICLE(targ) && !((Monsters_from(this.monsterid)).spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
105 || (SAME_TEAM(targ, this))
106 || (STAT(FROZEN, targ))
107 || (targ.alpha != 0 && targ.alpha < 0.5)
108 || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
109 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
112 // if any of the above checks fail, target is not valid
116 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
117 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this); // TODO: maybe we can rely a bit on PVS data instead?
119 if(trace_fraction < 1 && trace_ent != targ)
120 return false; // solid
122 if(!skipfacing && (autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT)))
123 if(this.enemy != targ)
125 if(!monster_facing(this, targ))
129 return true; // this target is valid!
132 entity Monster_FindTarget(entity this)
134 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
136 entity closest_target = NULL;
137 vector my_center = CENTER_OR_VIEWOFS(this);
139 // find the closest acceptable target to pass to
140 IL_EACH(g_monster_targets, it.monster_attack,
142 float trange = this.target_range;
143 if(PHYS_INPUT_BUTTON_CROUCH(it))
144 trange *= 0.75; // TODO cvar this
145 vector theirmid = (it.absmin + it.absmax) * 0.5;
146 if(vdist(theirmid - this.origin, >, trange))
148 if(!Monster_ValidTarget(this, it, false))
151 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
152 vector targ_center = CENTER_OR_VIEWOFS(it);
156 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
157 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
158 { closest_target = it; }
160 else { closest_target = it; }
163 return closest_target;
166 void monster_setupcolors(entity this)
168 if(IS_PLAYER(this.realowner))
169 this.colormap = this.realowner.colormap;
170 else if(teamplay && this.team)
171 this.colormap = 1024 + (this.team - 1) * 17;
174 if(this.monster_skill <= MONSTER_SKILL_EASY)
175 this.colormap = 1126;
176 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
177 this.colormap = 1075;
178 else if(this.monster_skill <= MONSTER_SKILL_HARD)
179 this.colormap = 1228;
180 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
181 this.colormap = 1092;
182 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
183 this.colormap = 1160;
185 this.colormap = 1024;
188 if(this.colormap > 0)
189 this.glowmod = colormapPaletteColor(this.colormap & 0x0F, false);
191 this.glowmod = '1 1 1';
194 void monster_changeteam(entity this, int newteam)
196 if(!teamplay) { return; }
199 if(!this.monster_attack)
200 IL_PUSH(g_monster_targets, this);
201 this.monster_attack = true; // new team, activate attacking
202 monster_setupcolors(this);
206 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
208 this.sprite.team = newteam;
209 this.sprite.SendFlags |= 1;
213 .void(entity) monster_delayedfunc;
214 void Monster_Delay_Action(entity this)
216 // TODO: maybe do check for facing here
217 if(Monster_ValidTarget(this.owner, this.owner.enemy, false)) { this.monster_delayedfunc(this.owner); }
222 setthink(this, Monster_Delay_Action);
223 this.nextthink = time + this.count;
227 setthink(this, SUB_Remove);
228 this.nextthink = time;
232 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
234 // deferred attacking, checks if monster is still alive and target is still valid before attacking
237 setthink(e, Monster_Delay_Action);
238 e.nextthink = time + defer_amnt;
239 e.count = defer_amnt;
241 e.monster_delayedfunc = func;
242 e.cnt = repeat_count;
250 string get_monster_model_datafilename(string m, float sk, string fil)
255 m = "models/monsters/*_";
257 m = strcat(m, ftos(sk));
260 return strcat(m, ".", fil);
263 void Monster_Sound_Precache(string f)
267 fh = fopen(f, FILE_READ);
270 while((s = fgets(fh)))
272 if(tokenize_console(s) != 3)
274 //LOG_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
277 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
282 void Monster_Sounds_Precache(entity this)
284 string m = (Monsters_from(this.monsterid)).m_model.model_str();
285 float globhandle, n, i;
288 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
291 n = search_getsize(globhandle);
292 for (i = 0; i < n; ++i)
294 //print(search_getfilename(globhandle, i), "\n");
295 f = search_getfilename(globhandle, i);
296 Monster_Sound_Precache(f);
298 search_end(globhandle);
301 void Monster_Sounds_Clear(entity this)
303 #define _MSOUND(m) strfree(this.monstersound_##m);
308 .string Monster_Sound_SampleField(string type)
310 GetMonsterSoundSampleField_notFound = 0;
313 #define _MSOUND(m) case #m: return monstersound_##m;
317 GetMonsterSoundSampleField_notFound = 1;
321 bool Monster_Sounds_Load(entity this, string f, int first)
325 float fh = fopen(f, FILE_READ);
328 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
331 while((s = fgets(fh)))
333 if(tokenize_console(s) != 3)
335 field = Monster_Sound_SampleField(argv(0));
336 if(GetMonsterSoundSampleField_notFound)
338 strcpy(this.(field), strcat(argv(1), " ", argv(2)));
344 .int skin_for_monstersound;
345 void Monster_Sounds_Update(entity this)
347 if(this.skin == this.skin_for_monstersound) { return; }
349 this.skin_for_monstersound = this.skin;
350 Monster_Sounds_Clear(this);
351 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
352 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
355 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
357 if(!autocvar_g_monsters_sounds) { return; }
360 if(time < this.msound_delay)
362 string sample = this.(samplefield);
363 if (sample != "") sample = GlobalSound_sample(sample, random());
364 float myscale = ((this.scale) ? this.scale : 1); // safety net
365 // TODO: change volume depending on size too?
366 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
368 this.msound_delay = time + sound_delay;
372 // =======================
373 // Monster attack handlers
374 // =======================
376 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
378 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
380 setanim(this, anim, false, true, false);
382 if(this.animstate_endtime > time && IS_MONSTER(this))
383 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
385 this.attack_finished_single[0] = this.anim_finished = time + animtime;
387 monster_makevectors(this, targ);
389 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
391 if(trace_ent.takedamage)
392 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
397 bool Monster_Attack_Leap_Check(entity this, vector vel)
399 if(this.state && IS_MONSTER(this))
400 return false; // already attacking
401 if(!IS_ONGROUND(this))
402 return false; // not on the ground
403 if(GetResourceAmount(this, RESOURCE_HEALTH) <= 0 || IS_DEAD(this))
404 return false; // called when dead?
405 if(time < this.attack_finished_single[0])
406 return false; // still attacking
408 vector old = this.velocity;
411 tracetoss(this, this);
413 if(trace_ent != this.enemy)
419 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
421 if(!Monster_Attack_Leap_Check(this, vel))
424 setanim(this, anm, false, true, false);
426 if(this.animstate_endtime > time && IS_MONSTER(this))
427 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
429 this.attack_finished_single[0] = this.anim_finished = time + animtime;
432 this.state = MONSTER_ATTACK_RANGED;
433 settouch(this, touchfunc);
436 UNSET_ONGROUND(this);
441 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
443 int slot = weaponslot(weaponentity);
446 || (!this.monster_attackfunc)
447 || (time < this.attack_finished_single[slot])
448 || ((autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT)) && !monster_facing(this, targ))
451 if(vdist(targ.origin - this.origin, <=, this.attack_range))
453 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
454 if(attack_success == 1)
455 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
456 else if(attack_success > 0)
460 if(vdist(targ.origin - this.origin, >, this.attack_range))
462 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
463 if(attack_success == 1)
464 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
465 else if(attack_success > 0)
471 // ======================
472 // Main monster functions
473 // ======================
475 void Monster_UpdateModel(entity this)
477 // assume some defaults
478 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
479 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
480 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
481 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
482 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
483 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
484 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
485 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
486 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
487 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
489 // then get the real values
490 Monster mon = Monsters_from(this.monsterid);
491 mon.mr_anim(mon, this);
494 void Monster_Touch(entity this, entity toucher)
496 if(toucher == NULL) { return; }
498 if(toucher.monster_attack && this.enemy != toucher && !IS_MONSTER(toucher) && time >= this.spawn_time)
499 if(Monster_ValidTarget(this, toucher, true))
500 this.enemy = toucher;
503 void Monster_Miniboss_Check(entity this)
505 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
508 float chance = random() * 100;
510 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
511 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
513 GiveResource(this, RESOURCE_HEALTH, autocvar_g_monsters_miniboss_healthboost);
514 this.effects |= EF_RED;
516 this.weapon = WEP_VORTEX.m_id;
520 bool Monster_Respawn_Check(entity this)
522 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
523 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
524 return true; // enabled by a mutator
526 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
529 if(!autocvar_g_monsters_respawn)
535 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterid); }
539 void Monster_Dead_Fade(entity this)
541 if(Monster_Respawn_Check(this))
543 this.spawnflags |= MONSTERFLAG_RESPAWNED;
544 setthink(this, Monster_Respawn);
545 this.nextthink = time + this.respawntime;
546 this.monster_lifetime = 0;
547 this.deadflag = DEAD_RESPAWNING;
548 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
550 this.pos1 = this.origin;
551 this.pos2 = this.angles;
553 this.event_damage = func_null;
554 this.event_heal = func_null;
555 this.takedamage = DAMAGE_NO;
556 setorigin(this, this.pos1);
557 this.angles = this.pos2;
558 SetResourceAmountExplicit(this, RESOURCE_HEALTH, this.max_health);
559 setmodel(this, MDL_Null);
563 // number of monsters spawned with mobspawn command
566 SUB_SetFade(this, time + 3, 1);
570 void Monster_Use(entity this, entity actor, entity trigger)
572 if(Monster_ValidTarget(this, actor, true)) { this.enemy = actor; }
575 .float pass_distance;
576 vector Monster_Move_Target(entity this, entity targ)
578 // enemy is always preferred target
581 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
582 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
583 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
585 // cases where the enemy may have changed their state (don't need to check everything here)
586 if( (IS_DEAD(this.enemy) || GetResourceAmount(this.enemy, RESOURCE_HEALTH) < 1)
587 || (STAT(FROZEN, this.enemy))
588 || (this.enemy.flags & FL_NOTARGET)
589 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
590 || (this.enemy.takedamage == DAMAGE_NO)
591 || (vdist(this.origin - targ_origin, >, this.target_range))
592 || ((trace_fraction < 1) && (trace_ent != this.enemy))
596 //this.pass_distance = 0;
601 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
602 print("Trace origin: ", vtos(targ_origin), "\n");
603 print("Target origin: ", vtos(this.enemy.origin), "\n");
604 print("My origin: ", vtos(this.origin), "\n"); */
606 this.monster_movestate = MONSTER_MOVE_ENEMY;
607 this.last_trace = time + 1.2;
608 if(this.monster_moveto)
609 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
614 /*makevectors(this.angles);
615 this.monster_movestate = MONSTER_MOVE_ENEMY;
616 this.last_trace = time + 1.2;
617 return this.enemy.origin; */
620 switch(this.monster_moveflags)
622 case MONSTER_MOVE_FOLLOW:
624 this.monster_movestate = MONSTER_MOVE_FOLLOW;
625 this.last_trace = time + 0.3;
626 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
628 case MONSTER_MOVE_SPAWNLOC:
630 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
631 this.last_trace = time + 2;
634 case MONSTER_MOVE_NOMOVE:
636 if(this.monster_moveto)
638 this.last_trace = time + 0.5;
639 return this.monster_moveto;
643 this.monster_movestate = MONSTER_MOVE_NOMOVE;
644 this.last_trace = time + 2;
649 case MONSTER_MOVE_WANDER:
652 this.monster_movestate = MONSTER_MOVE_WANDER;
654 if(this.monster_moveto)
656 this.last_trace = time + 0.5;
657 pos = this.monster_moveto;
661 this.last_trace = time + 0.5;
666 this.last_trace = time + this.wander_delay;
668 this.angles_y = rint(random() * 500);
669 makevectors(this.angles);
670 pos = this.origin + v_forward * this.wander_distance;
672 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
674 pos.z = random() * 200;
687 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
689 // update goal entity if lost
690 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
691 this.goalentity = find(NULL, targetname, this.target2);
693 if(STAT(FROZEN, this))
695 movelib_brake_simple(this, stpspeed);
696 setanim(this, this.anim_idle, true, false, false);
697 return; // no physics while frozen!
700 if(this.flags & FL_SWIM)
702 if(this.waterlevel < WATERLEVEL_WETFEET)
704 if(time >= this.last_trace)
706 this.last_trace = time + 0.4;
708 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
709 this.angles = '90 90 0';
712 this.velocity_y += random() * 50;
713 this.velocity_x -= random() * 50;
717 this.velocity_y -= random() * 50;
718 this.velocity_x += random() * 50;
720 this.velocity_z += random() * 150;
724 set_movetype(this, MOVETYPE_BOUNCE);
725 //this.velocity_z = -200;
729 else if(this.move_movetype == MOVETYPE_BOUNCE)
732 set_movetype(this, MOVETYPE_WALK);
736 entity targ = this.goalentity;
738 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
740 || this.draggedby != NULL
741 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
742 || time < game_starttime
743 || (autocvar_g_campaign && !campaign_bots_may_start)
744 || time < this.spawn_time)
746 runspeed = walkspeed = 0;
747 if(time >= this.spawn_time)
748 setanim(this, this.anim_idle, true, false, false);
749 movelib_brake_simple(this, stpspeed);
753 targ = M_ARGV(3, entity);
754 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
755 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
757 if(teamplay && autocvar_g_monsters_teams)
758 if(DIFF_TEAM(this.monster_follow, this))
759 this.monster_follow = NULL;
761 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
764 settouch(this, Monster_Touch);
767 if(this.state && time >= this.attack_finished_single[0])
768 this.state = 0; // attack is over
770 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
771 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
772 this.moveto = Monster_Move_Target(this, targ);
775 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
777 if(this.state == MONSTER_ATTACK_MELEE)
778 this.moveto = this.origin;
780 if(this.enemy && this.enemy.vehicle)
783 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
784 this.moveto_z = this.origin_z;
786 fixedmakevectors(this.angles);
787 float vz = this.velocity_z;
789 if(!turret_closetotarget(this, this.moveto, 16))
791 bool do_run = (this.enemy || this.monster_moveto);
792 movelib_move_simple(this, v_forward, ((do_run) ? runspeed : walkspeed), 0.4);
794 if(time > this.pain_finished && time > this.anim_finished)
796 if(vdist(this.velocity, >, 10))
797 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
799 setanim(this, this.anim_idle, true, false, false);
803 entity e = this.goalentity; //find(NULL, targetname, this.target2);
804 if(e.target2 && e.target2 != "")
805 this.target2 = e.target2;
806 else if(e.target && e.target != "") // compatibility
807 this.target2 = e.target;
809 movelib_brake_simple(this, stpspeed);
810 if(time > this.anim_finished && time > this.pain_finished)
812 if(vdist(this.velocity, <=, 30))
813 setanim(this, this.anim_idle, true, false, false);
816 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
817 this.velocity_z = vz;
819 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
821 vector real_angle = vectoangles(this.steerto) - this.angles;
823 if(this.state == MONSTER_ATTACK_MELEE)
827 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
828 this.angles_y += turny;
832 void Monster_Remove(entity this)
835 return; // don't remove it?
837 if(!this) { return; }
839 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
840 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
842 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
844 .entity weaponentity = weaponentities[slot];
845 if(this.(weaponentity))
846 delete(this.(weaponentity));
848 if(this.iceblock) { delete(this.iceblock); }
849 WaypointSprite_Kill(this.sprite);
853 void Monster_Dead_Think(entity this)
855 this.nextthink = time + this.ticrate;
857 Monster mon = Monsters_from(this.monsterid);
858 mon.mr_deadthink(mon, this);
860 if(this.monster_lifetime != 0)
861 if(time >= this.monster_lifetime)
863 Monster_Dead_Fade(this);
868 void Monster_Appear(entity this, entity actor, entity trigger)
871 Monster_Spawn(this, false, this.monsterid);
874 bool Monster_Appear_Check(entity this, int monster_id)
876 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
879 setthink(this, func_null);
880 this.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
882 this.use = Monster_Appear;
883 this.flags = FL_MONSTER; // set so this monster can get butchered
888 void Monster_Reset(entity this)
890 setorigin(this, this.pos1);
891 this.angles = this.pos2;
893 Unfreeze(this, false); // remove any icy remains
895 SetResourceAmountExplicit(this, RESOURCE_HEALTH, this.max_health);
896 this.velocity = '0 0 0';
898 this.goalentity = NULL;
899 this.attack_finished_single[0] = 0;
900 this.moveto = this.origin;
903 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
905 TakeResource(this, RESOURCE_HEALTH, damage);
907 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
909 if(GetResourceAmount(this, RESOURCE_HEALTH) <= -50) // 100 health until gone?
911 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
913 // number of monsters spawned with mobspawn command
916 setthink(this, SUB_Remove);
917 this.nextthink = time + 0.1;
918 this.event_damage = func_null;
922 void Monster_Dead(entity this, entity attacker, float gibbed)
924 setthink(this, Monster_Dead_Think);
925 this.nextthink = time;
926 this.monster_lifetime = time + 5;
928 if(STAT(FROZEN, this))
929 Unfreeze(this, false); // remove any icy remains
931 monster_dropitem(this, attacker);
933 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
935 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
936 monsters_killed += 1;
938 if(IS_PLAYER(attacker))
939 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
940 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
944 // number of monsters spawned with mobspawn command
948 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
949 _setmodel(this, this.mdl_dead);
951 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
952 this.event_heal = func_null;
953 this.solid = SOLID_CORPSE;
954 this.takedamage = DAMAGE_AIM;
955 this.deadflag = DEAD_DEAD;
957 set_movetype(this, MOVETYPE_TOSS);
958 this.moveto = this.origin;
959 settouch(this, Monster_Touch); // reset incase monster was pouncing
960 this.reset = func_null;
962 this.attack_finished_single[0] = 0;
964 this.dphitcontentsmask &= ~DPCONTENTS_BODY;
966 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
967 this.velocity = '0 0 0';
969 CSQCModel_UnlinkEntity(this);
971 Monster mon = Monsters_from(this.monsterid);
972 mon.mr_death(mon, this);
974 if(this.candrop && this.weapon)
976 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
977 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
981 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
983 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
986 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
989 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
992 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
995 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
998 vector v = healtharmor_applydamage(100, GetResourceAmount(this, RESOURCE_ARMOR) / 100, deathtype, damage);
1002 Monster mon = Monsters_from(this.monsterid);
1003 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1007 TakeResource(this, RESOURCE_HEALTH, take);
1008 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1012 WaypointSprite_UpdateHealth(this.sprite, GetResourceAmount(this, RESOURCE_HEALTH));
1014 this.dmg_time = time;
1016 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1017 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1019 this.velocity += force * this.damageforcescale;
1021 if(deathtype != DEATH_DROWN.m_id && take)
1023 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1025 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1027 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1030 if(GetResourceAmount(this, RESOURCE_HEALTH) <= 0)
1032 if(deathtype == DEATH_KILL.m_id)
1033 this.candrop = false; // killed by mobkill command
1036 SUB_UseTargets(this, attacker, this.enemy);
1037 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1039 Monster_Dead(this, attacker, (GetResourceAmount(this, RESOURCE_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id));
1041 WaypointSprite_Kill(this.sprite);
1043 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1045 if(GetResourceAmount(this, RESOURCE_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1047 Violence_GibSplash(this, 1, 0.5, attacker);
1049 setthink(this, SUB_Remove);
1050 this.nextthink = time + 0.1;
1055 bool Monster_Heal(entity targ, entity inflictor, float amount, float limit)
1057 float true_limit = ((limit != RESOURCE_LIMIT_NONE) ? limit : targ.max_health);
1058 if(GetResourceAmount(targ, RESOURCE_HEALTH) <= 0 || GetResourceAmount(targ, RESOURCE_HEALTH) >= true_limit)
1061 GiveResourceWithLimit(targ, RESOURCE_HEALTH, amount, true_limit);
1063 WaypointSprite_UpdateHealth(targ.sprite, GetResourceAmount(targ, RESOURCE_HEALTH));
1067 // don't check for enemies, just keep walking in a straight line
1068 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1070 if(game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || this.draggedby != NULL || time < game_starttime || (autocvar_g_campaign && !campaign_bots_may_start) || time < this.spawn_time)
1073 if(time >= this.spawn_time)
1074 setanim(this, this.anim_idle, true, false, false);
1075 movelib_brake_simple(this, 0.6);
1079 makevectors(this.angles);
1080 vector a = CENTER_OR_VIEWOFS(this);
1081 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1083 traceline(a, b, MOVE_NORMAL, this);
1085 bool reverse = false;
1086 if(trace_fraction != 1.0)
1088 if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1090 if(trace_ent && IS_MONSTER(trace_ent))
1093 // TODO: fix this... tracing is broken if the floor is thin
1098 traceline(b, a, MOVE_WORLDONLY, this);
1099 if(trace_fraction == 1.0)
1105 this.angles_y = anglemods(this.angles_y - 180);
1106 makevectors(this.angles);
1109 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1111 if(time > this.pain_finished && time > this.attack_finished_single[0])
1112 if(vdist(this.velocity, >, 10))
1113 setanim(this, this.anim_walk, true, false, false);
1115 setanim(this, this.anim_idle, true, false, false);
1118 void Monster_Anim(entity this)
1120 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1125 // Decide on which death animation to use.
1127 deadbits = ANIMSTATE_DEAD1;
1129 deadbits = ANIMSTATE_DEAD2;
1134 // Clear a previous death animation.
1137 int animbits = deadbits;
1138 if(STAT(FROZEN, this))
1139 animbits |= ANIMSTATE_FROZEN;
1141 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1142 animdecide_setstate(this, animbits, false);
1143 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1145 /* // weapon entities for monsters?
1146 if (this.weaponentity)
1148 updateanim(this.weaponentity);
1149 if (!this.weaponentity.animstate_override)
1150 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1155 void Monster_Frozen_Think(entity this)
1157 if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
1159 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1160 SetResourceAmountExplicit(this, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * this.max_health));
1161 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1163 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1164 WaypointSprite_UpdateHealth(this.sprite, GetResourceAmount(this, RESOURCE_HEALTH));
1166 if(STAT(REVIVE_PROGRESS, this) >= 1)
1167 Unfreeze(this, false);
1169 else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
1171 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1172 SetResourceAmountExplicit(this, RESOURCE_HEALTH, max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
1174 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1175 WaypointSprite_UpdateHealth(this.sprite, GetResourceAmount(this, RESOURCE_HEALTH));
1177 if(GetResourceAmount(this, RESOURCE_HEALTH) < 1)
1179 Unfreeze(this, false);
1180 if(this.event_damage)
1181 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
1183 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1184 Unfreeze(this, false);
1186 // otherwise, no revival!
1188 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1191 void Monster_Enemy_Check(entity this)
1196 this.enemy = Monster_FindTarget(this);
1199 WarpZone_RefSys_Copy(this.enemy, this);
1200 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1201 // update move target immediately?
1202 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1203 this.monster_moveto = '0 0 0';
1204 this.monster_face = '0 0 0';
1206 //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)));
1207 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1211 void Monster_Think(entity this)
1213 setthink(this, Monster_Think);
1214 this.nextthink = time + this.ticrate;
1216 if(this.monster_lifetime && time >= this.monster_lifetime)
1218 Damage(this, this, this, GetResourceAmount(this, RESOURCE_HEALTH) + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1222 if(STAT(FROZEN, this))
1223 Monster_Frozen_Think(this);
1224 else if(time >= this.last_enemycheck)
1226 Monster_Enemy_Check(this);
1227 this.last_enemycheck = time + 1; // check for enemies every second
1230 Monster mon = Monsters_from(this.monsterid);
1231 if(mon.mr_think(mon, this))
1233 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1235 .entity weaponentity = weaponentities[0]; // TODO?
1236 Monster_Attack_Check(this, this.enemy, weaponentity);
1241 CSQCMODEL_AUTOUPDATE(this);
1244 bool Monster_Spawn_Setup(entity this)
1246 Monster mon = Monsters_from(this.monsterid);
1247 mon.mr_setup(mon, this);
1249 // ensure some basic needs are met
1250 if(!GetResourceAmount(this, RESOURCE_HEALTH)) { SetResourceAmountExplicit(this, RESOURCE_HEALTH, 100); }
1251 if(!GetResourceAmount(this, RESOURCE_ARMOR)) { SetResourceAmountExplicit(this, RESOURCE_ARMOR, bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9)); }
1252 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1253 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1254 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1255 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1256 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1258 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1260 Monster_Miniboss_Check(this);
1261 SetResourceAmountExplicit(this, RESOURCE_HEALTH, GetResourceAmount(this, RESOURCE_HEALTH) * MONSTER_SKILLMOD(this));
1264 this.skin = rint(random() * 4);
1267 this.max_health = GetResourceAmount(this, RESOURCE_HEALTH);
1268 this.pain_finished = this.nextthink;
1269 this.last_enemycheck = this.spawn_time + random(); // slight delay
1271 if(IS_PLAYER(this.monster_follow))
1272 this.effects |= EF_DIMLIGHT;
1274 if(!this.wander_delay) { this.wander_delay = 2; }
1275 if(!this.wander_distance) { this.wander_distance = 600; }
1277 Monster_Sounds_Precache(this);
1278 Monster_Sounds_Update(this);
1282 if(!this.monster_attack)
1283 IL_PUSH(g_monster_targets, this);
1284 this.monster_attack = true; // we can have monster enemies in team games
1287 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1289 if(autocvar_g_monsters_healthbars)
1291 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1292 wp.wp_extra = this.monsterid;
1293 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1294 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1296 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1297 WaypointSprite_UpdateHealth(this.sprite, GetResourceAmount(this, RESOURCE_HEALTH));
1301 setthink(this, Monster_Think);
1302 this.nextthink = time + this.ticrate;
1304 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1310 bool Monster_Spawn(entity this, bool check_appear, int mon_id)
1312 // setup the basic required properties for a monster
1313 entity mon = Monsters_from(mon_id);
1314 if(!mon.monsterid) { return false; } // invalid monster
1316 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1318 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1320 IL_PUSH(g_monsters, this);
1321 if(this.mdl && this.mdl != "")
1322 precache_model(this.mdl);
1323 if(this.mdl_dead && this.mdl_dead != "")
1324 precache_model(this.mdl_dead);
1327 if(check_appear && Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed
1329 if(!this.monster_skill)
1330 this.monster_skill = cvar("g_monsters_skill");
1332 // support for quake style removing monsters based on skill
1333 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1334 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1335 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1337 if(this.team && !teamplay)
1340 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1341 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1342 monsters_total += 1;
1344 if(this.mdl && this.mdl != "")
1345 _setmodel(this, this.mdl);
1347 setmodel(this, mon.m_model);
1349 if(!this.monster_name || this.monster_name == "")
1350 this.monster_name = mon.monster_name;
1352 this.flags = FL_MONSTER;
1353 this.classname = "monster";
1354 this.takedamage = DAMAGE_AIM;
1355 if(!this.bot_attack)
1356 IL_PUSH(g_bot_targets, this);
1357 this.bot_attack = true;
1358 this.iscreature = true;
1359 this.teleportable = true;
1360 if(!this.damagedbycontents)
1361 IL_PUSH(g_damagedbycontents, this);
1362 this.damagedbycontents = true;
1363 this.monsterid = mon_id;
1364 this.event_damage = Monster_Damage;
1365 this.event_heal = Monster_Heal;
1366 settouch(this, Monster_Touch);
1367 this.use = Monster_Use;
1368 this.solid = SOLID_BBOX;
1369 set_movetype(this, MOVETYPE_WALK);
1370 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1372 this.velocity = '0 0 0';
1373 this.moveto = this.origin;
1374 this.pos1 = this.origin;
1375 this.pos2 = this.angles;
1376 this.reset = Monster_Reset;
1377 this.netname = mon.netname;
1378 this.monster_attackfunc = mon.monster_attackfunc;
1379 this.candrop = true;
1380 this.oldtarget2 = this.target2;
1381 //this.pass_distance = 0;
1382 this.deadflag = DEAD_NO;
1383 this.spawn_time = time;
1385 this.monster_moveto = '0 0 0';
1386 this.monster_face = '0 0 0';
1387 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1389 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1390 if(!this.scale) { this.scale = 1; }
1391 if(autocvar_g_monsters_edit) { this.grab = 1; }
1392 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1393 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1394 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1396 if(autocvar_g_monsters_playerclip_collisions)
1397 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1399 if(mon.spawnflags & MONSTER_TYPE_FLY)
1401 this.flags |= FL_FLY;
1402 set_movetype(this, MOVETYPE_FLY);
1405 if((mon.spawnflags & MONSTER_SIZE_QUAKE) && autocvar_g_monsters_quake_resize && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
1408 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1409 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1411 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1413 Monster_UpdateModel(this);
1415 if(!Monster_Spawn_Setup(this))
1417 Monster_Remove(this);
1423 setorigin(this, this.origin + '0 0 20');
1424 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1425 setorigin(this, trace_endpos);
1428 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1429 monster_setupcolors(this);
1431 CSQCMODEL_AUTOINIT(this);