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 <server/g_damage.qh>
13 #include "../deathtypes/all.qh"
14 #include <server/items/items.qh>
15 #include <server/mutators/_mod.qh>
16 #include <server/steerlib.qh>
17 #include <server/sv_main.qh>
18 #include "../turrets/sv_turrets.qh"
19 #include "../turrets/util.qh"
20 #include "../vehicles/all.qh"
21 #include <server/campaign.qh>
22 #include <server/command/_mod.qh>
23 #include <common/mapobjects/teleporters.qh>
24 #include "../mapobjects/triggers.qh"
25 #include <lib/csqcmodel/sv_model.qh>
26 #include <server/round_handler.qh>
27 #include <server/weapons/_mod.qh>
29 void monsters_setstatus(entity this)
31 STAT(MONSTERS_TOTAL, this) = monsters_total;
32 STAT(MONSTERS_KILLED, this) = monsters_killed;
35 void monster_dropitem(entity this, entity attacker)
37 if(!this.candrop || !this.monster_loot)
40 vector org = CENTER_OR_VIEWOFS(this);
42 Item_SetLoot(e, true);
43 e.spawnfunc_checked = true;
45 e.monster_loot = this.monster_loot;
47 MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
48 e = M_ARGV(1, entity);
50 if(e && e.monster_loot)
53 StartItem(e, e.monster_loot);
56 e.velocity = randomvec() * 175 + '0 0 325';
57 e.item_spawnshieldtime = time + 0.7;
58 SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
62 void monster_makevectors(entity this, entity targ)
66 vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
67 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
68 this.v_angle_x = -this.v_angle_x;
71 makevectors(this.v_angle);
78 bool Monster_ValidTarget(entity this, entity targ)
80 // ensure we're not checking nonexistent monster/target
81 if(!this || !targ) { return false; }
84 || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
85 || (IS_VEHICLE(targ) && !(this.monsterdef.spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
86 || (time < game_starttime) // monsters do nothing before match has started
87 || (targ.takedamage == DAMAGE_NO)
89 || (targ.items & IT_INVISIBILITY)
90 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
91 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || GetResource(targ, RES_HEALTH) <= 0 || GetResource(this, RES_HEALTH) <= 0))
92 || (this.monster_follow == targ || targ.monster_follow == this)
93 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
94 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
95 || (SAME_TEAM(targ, this))
96 || (STAT(FROZEN, targ))
97 || (targ.alpha != 0 && targ.alpha < 0.5)
98 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
101 // if any of the above checks fail, target is not valid
105 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
106 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this);
108 if(trace_fraction < 1 && trace_ent != targ)
109 return false; // solid
111 if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
112 if(this.enemy != targ)
114 makevectors (this.angles);
115 float dot = normalize (targ.origin - this.origin) * v_forward;
117 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
120 return true; // this target is valid!
123 entity Monster_FindTarget(entity this)
125 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
127 entity closest_target = NULL;
128 vector my_center = CENTER_OR_VIEWOFS(this);
130 // find the closest acceptable target to pass to
131 IL_EACH(g_monster_targets, it.monster_attack && vdist(it.origin - this.origin, <, this.target_range),
133 if(Monster_ValidTarget(this, it))
135 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
136 vector targ_center = CENTER_OR_VIEWOFS(it);
140 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
141 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
142 { closest_target = it; }
144 else { closest_target = it; }
148 return closest_target;
151 void monster_setupcolors(entity this)
153 if(IS_PLAYER(this.realowner))
154 this.colormap = this.realowner.colormap;
155 else if(teamplay && this.team)
156 this.colormap = 1024 + (this.team - 1) * 17;
159 if(this.monster_skill <= MONSTER_SKILL_EASY)
160 this.colormap = 1029;
161 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
162 this.colormap = 1027;
163 else if(this.monster_skill <= MONSTER_SKILL_HARD)
164 this.colormap = 1038;
165 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
166 this.colormap = 1028;
167 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
168 this.colormap = 1032;
170 this.colormap = 1024;
174 void monster_changeteam(entity this, int newteam)
176 if(!teamplay) { return; }
179 if(!this.monster_attack)
180 IL_PUSH(g_monster_targets, this);
181 this.monster_attack = true; // new team, activate attacking
182 monster_setupcolors(this);
186 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
188 this.sprite.team = newteam;
189 this.sprite.SendFlags |= 1;
193 .void(entity) monster_delayedfunc;
194 void Monster_Delay_Action(entity this)
196 if(Monster_ValidTarget(this.owner, this.owner.enemy))
198 monster_makevectors(this.owner, this.owner.enemy);
199 this.monster_delayedfunc(this.owner);
205 setthink(this, Monster_Delay_Action);
206 this.nextthink = time + this.count;
210 setthink(this, SUB_Remove);
211 this.nextthink = time;
215 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
217 // deferred attacking, checks if monster is still alive and target is still valid before attacking
220 setthink(e, Monster_Delay_Action);
221 e.nextthink = time + defer_amnt;
222 e.count = defer_amnt;
224 e.monster_delayedfunc = func;
225 e.cnt = repeat_count;
233 string get_monster_model_datafilename(string m, float sk, string fil)
238 m = "models/monsters/*_";
240 m = strcat(m, ftos(sk));
243 return strcat(m, ".", fil);
246 void Monster_Sound_Precache(string f)
250 fh = fopen(f, FILE_READ);
253 while((s = fgets(fh)))
255 if(tokenize_console(s) != 3)
257 //LOG_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
260 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
265 void Monster_Sounds_Precache(entity this)
267 string m = this.monsterdef.m_model.model_str();
268 float globhandle, n, i;
271 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
274 n = search_getsize(globhandle);
275 for (i = 0; i < n; ++i)
277 //print(search_getfilename(globhandle, i), "\n");
278 f = search_getfilename(globhandle, i);
279 Monster_Sound_Precache(f);
281 search_end(globhandle);
284 void Monster_Sounds_Clear(entity this)
286 #define _MSOUND(m) strfree(this.monstersound_##m);
291 .string Monster_Sound_SampleField(string type)
293 GetMonsterSoundSampleField_notFound = 0;
296 #define _MSOUND(m) case #m: return monstersound_##m;
300 GetMonsterSoundSampleField_notFound = 1;
304 bool Monster_Sounds_Load(entity this, string f, int first)
308 float fh = fopen(f, FILE_READ);
311 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
314 while((s = fgets(fh)))
316 if(tokenize_console(s) != 3)
318 field = Monster_Sound_SampleField(argv(0));
319 if(GetMonsterSoundSampleField_notFound)
321 strcpy(this.(field), strcat(argv(1), " ", argv(2)));
327 .int skin_for_monstersound;
328 void Monster_Sounds_Update(entity this)
330 if(this.skin == this.skin_for_monstersound) { return; }
332 this.skin_for_monstersound = this.skin;
333 Monster_Sounds_Clear(this);
334 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
335 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
338 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
340 if(!autocvar_g_monsters_sounds) { return; }
343 if(time < this.msound_delay)
345 string sample = this.(samplefield);
346 if (sample != "") sample = GlobalSound_sample(sample, random());
347 float myscale = ((this.scale) ? this.scale : 1); // safety net
348 // TODO: change volume depending on size too?
349 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
351 this.msound_delay = time + sound_delay;
355 // =======================
356 // Monster attack handlers
357 // =======================
359 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
361 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
363 setanim(this, anim, false, true, false);
365 if(this.animstate_endtime > time && IS_MONSTER(this))
366 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
368 this.attack_finished_single[0] = this.anim_finished = time + animtime;
370 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
372 if(trace_ent.takedamage)
373 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
378 bool Monster_Attack_Leap_Check(entity this, vector vel)
380 if(this.state && IS_MONSTER(this))
381 return false; // already attacking
382 if(!IS_ONGROUND(this))
383 return false; // not on the ground
384 if(GetResource(this, RES_HEALTH) <= 0 || IS_DEAD(this))
385 return false; // called when dead?
386 if(time < this.attack_finished_single[0])
387 return false; // still attacking
389 vector old = this.velocity;
392 tracetoss(this, this);
394 if(trace_ent != this.enemy)
400 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
402 if(!Monster_Attack_Leap_Check(this, vel))
405 setanim(this, anm, false, true, false);
407 if(this.animstate_endtime > time && IS_MONSTER(this))
408 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
410 this.attack_finished_single[0] = this.anim_finished = time + animtime;
413 this.state = MONSTER_ATTACK_RANGED;
414 settouch(this, touchfunc);
417 UNSET_ONGROUND(this);
422 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
424 int slot = weaponslot(weaponentity);
427 || (!this.monster_attackfunc)
428 || (time < this.attack_finished_single[slot])
431 if(vdist(targ.origin - this.origin, <=, this.attack_range))
433 monster_makevectors(this, targ);
434 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
435 if(attack_success == 1)
436 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
437 else if(attack_success > 0)
441 if(vdist(targ.origin - this.origin, >, this.attack_range))
443 monster_makevectors(this, targ);
444 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
445 if(attack_success == 1)
446 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
447 else if(attack_success > 0)
453 // ======================
454 // Main monster functions
455 // ======================
457 void Monster_UpdateModel(entity this)
459 // assume some defaults
460 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
461 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
462 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
463 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
464 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
465 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
466 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
467 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
468 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
469 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
471 // then get the real values
472 Monster mon = this.monsterdef;
473 mon.mr_anim(mon, this);
476 void Monster_Touch(entity this, entity toucher)
478 if(toucher == NULL) { return; }
480 if(toucher.monster_attack)
481 if(this.enemy != toucher)
482 if(!IS_MONSTER(toucher))
483 if(Monster_ValidTarget(this, toucher))
484 this.enemy = toucher;
487 void Monster_Miniboss_Check(entity this)
489 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
492 float chance = random() * 100;
494 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
495 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
497 GiveResource(this, RES_HEALTH, autocvar_g_monsters_miniboss_healthboost);
498 this.effects |= EF_RED;
500 this.weapon = WEP_VORTEX.m_id;
504 bool Monster_Respawn_Check(entity this)
506 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
507 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
508 return true; // enabled by a mutator
510 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
513 if(!autocvar_g_monsters_respawn)
519 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterdef); }
523 void Monster_Dead_Fade(entity this)
525 if(Monster_Respawn_Check(this))
527 this.spawnflags |= MONSTERFLAG_RESPAWNED;
528 setthink(this, Monster_Respawn);
529 this.nextthink = time + this.respawntime;
530 this.monster_lifetime = 0;
531 this.deadflag = DEAD_RESPAWNING;
532 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
534 this.pos1 = this.origin;
535 this.pos2 = this.angles;
537 this.event_damage = func_null;
538 this.event_heal = func_null;
539 this.takedamage = DAMAGE_NO;
540 setorigin(this, this.pos1);
541 this.angles = this.pos2;
542 SetResourceExplicit(this, RES_HEALTH, this.max_health);
543 setmodel(this, MDL_Null);
547 // number of monsters spawned with mobspawn command
550 SUB_SetFade(this, time + 3, 1);
554 void Monster_Use(entity this, entity actor, entity trigger)
556 if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
559 .float pass_distance;
560 vector Monster_Move_Target(entity this, entity targ)
562 // enemy is always preferred target
565 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
566 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
567 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
569 // cases where the enemy may have changed their state (don't need to check everything here)
571 || (IS_DEAD(this.enemy) || GetResource(this.enemy, RES_HEALTH) < 1)
572 || (STAT(FROZEN, this.enemy))
573 || (this.enemy.flags & FL_NOTARGET)
574 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
575 || (this.enemy.takedamage == DAMAGE_NO)
576 || (vdist(this.origin - targ_origin, >, this.target_range))
577 || ((trace_fraction < 1) && (trace_ent != this.enemy)))
580 //this.pass_distance = 0;
585 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
586 print("Trace origin: ", vtos(targ_origin), "\n");
587 print("Target origin: ", vtos(this.enemy.origin), "\n");
588 print("My origin: ", vtos(this.origin), "\n"); */
590 this.monster_movestate = MONSTER_MOVE_ENEMY;
591 this.last_trace = time + 1.2;
592 if(this.monster_moveto)
593 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
598 /*makevectors(this.angles);
599 this.monster_movestate = MONSTER_MOVE_ENEMY;
600 this.last_trace = time + 1.2;
601 return this.enemy.origin; */
604 switch(this.monster_moveflags)
606 case MONSTER_MOVE_FOLLOW:
608 this.monster_movestate = MONSTER_MOVE_FOLLOW;
609 this.last_trace = time + 0.3;
610 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
612 case MONSTER_MOVE_SPAWNLOC:
614 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
615 this.last_trace = time + 2;
618 case MONSTER_MOVE_NOMOVE:
620 if(this.monster_moveto)
622 this.last_trace = time + 0.5;
623 return this.monster_moveto;
627 this.monster_movestate = MONSTER_MOVE_NOMOVE;
628 this.last_trace = time + 2;
633 case MONSTER_MOVE_WANDER:
636 this.monster_movestate = MONSTER_MOVE_WANDER;
638 if(this.monster_moveto)
640 this.last_trace = time + 0.5;
641 pos = this.monster_moveto;
645 this.last_trace = time + 0.5;
650 this.last_trace = time + this.wander_delay;
652 this.angles_y = rint(random() * 500);
653 makevectors(this.angles);
654 pos = this.origin + v_forward * this.wander_distance;
656 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
658 pos.z = random() * 200;
669 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
671 //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
672 //float initial_height = 0; //min(50, (targ_distance * tanh(20)));
673 //float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
674 //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
678 if(current_height) // make sure we can actually do this arcing path
680 targpos = (to + ('0 0 1' * current_height));
681 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
682 if(trace_fraction < 1)
684 //print("normal arc line failed, trying to find new pos...");
685 WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
686 targpos = (trace_endpos + '0 0 -10');
687 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
688 if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
689 /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
692 else { targpos = to; }
695 //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
697 vector desired_direction = normalize(targpos - from);
698 if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
699 else { this.velocity = (desired_direction * movespeed); }
701 //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
702 //this.angles = vectoangles(this.velocity);
707 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
709 // update goal entity if lost
710 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
711 this.goalentity = find(NULL, targetname, this.target2);
713 if(STAT(FROZEN, this))
715 movelib_brake_simple(this, stpspeed);
716 setanim(this, this.anim_idle, true, false, false);
717 return; // no physics while frozen!
720 if(this.flags & FL_SWIM)
722 if(this.waterlevel < WATERLEVEL_WETFEET)
724 if(time >= this.last_trace)
726 this.last_trace = time + 0.4;
728 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
729 this.angles = '90 90 0';
732 this.velocity_y += random() * 50;
733 this.velocity_x -= random() * 50;
737 this.velocity_y -= random() * 50;
738 this.velocity_x += random() * 50;
740 this.velocity_z += random() * 150;
744 set_movetype(this, MOVETYPE_BOUNCE);
745 //this.velocity_z = -200;
749 else if(this.move_movetype == MOVETYPE_BOUNCE)
752 set_movetype(this, MOVETYPE_WALK);
756 entity targ = this.goalentity;
758 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
760 || this.draggedby != NULL
761 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
762 || time < game_starttime
763 || (autocvar_g_campaign && !campaign_bots_may_start)
764 || time < this.spawn_time)
766 runspeed = walkspeed = 0;
767 if(time >= this.spawn_time)
768 setanim(this, this.anim_idle, true, false, false);
769 movelib_brake_simple(this, stpspeed);
773 targ = M_ARGV(3, entity);
774 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
775 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
777 if(teamplay && autocvar_g_monsters_teams)
778 if(DIFF_TEAM(this.monster_follow, this))
779 this.monster_follow = NULL;
781 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
784 settouch(this, Monster_Touch);
787 if(this.state && time >= this.attack_finished_single[0])
788 this.state = 0; // attack is over
790 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
791 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
792 this.moveto = Monster_Move_Target(this, targ);
795 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
797 if(this.state == MONSTER_ATTACK_MELEE)
798 this.moveto = this.origin;
800 if(this.enemy && this.enemy.vehicle)
803 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
804 this.moveto_z = this.origin_z;
806 if(vdist(this.origin - this.moveto, >, 100))
808 bool do_run = (this.enemy || this.monster_moveto);
809 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
810 Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
812 if(time > this.pain_finished && time > this.anim_finished) // TODO: use anim_finished instead!?
815 if(vdist(this.velocity, >, 10))
816 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
818 setanim(this, this.anim_idle, true, false, false);
823 entity e = this.goalentity; //find(NULL, targetname, this.target2);
824 if(e.target2 && e.target2 != "")
825 this.target2 = e.target2;
826 else if(e.target && e.target != "") // compatibility
827 this.target2 = e.target;
829 movelib_brake_simple(this, stpspeed);
830 if(time > this.anim_finished && time > this.pain_finished)
832 if(vdist(this.velocity, <=, 30))
833 setanim(this, this.anim_idle, true, false, false);
836 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
838 vector real_angle = vectoangles(this.steerto) - this.angles;
840 if(this.state == MONSTER_ATTACK_MELEE)
844 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
845 this.angles_y += turny;
849 void Monster_Remove(entity this)
852 return; // don't remove it?
854 if(!this) { return; }
856 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
857 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
859 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
861 .entity weaponentity = weaponentities[slot];
862 if(this.(weaponentity))
863 delete(this.(weaponentity));
865 if(this.iceblock) { delete(this.iceblock); }
866 WaypointSprite_Kill(this.sprite);
870 void Monster_Dead_Think(entity this)
872 this.nextthink = time + this.ticrate;
874 if(this.monster_lifetime != 0)
875 if(time >= this.monster_lifetime)
877 Monster_Dead_Fade(this);
882 void Monster_Appear(entity this, entity actor, entity trigger)
885 Monster_Spawn(this, false, this.monsterdef);
888 bool Monster_Appear_Check(entity this, Monster monster_id)
890 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
893 setthink(this, func_null);
894 this.monsterdef = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
896 this.use = Monster_Appear;
897 this.flags = FL_MONSTER; // set so this monster can get butchered
902 void Monster_Reset(entity this)
904 setorigin(this, this.pos1);
905 this.angles = this.pos2;
907 Unfreeze(this, false); // remove any icy remains
909 SetResourceExplicit(this, RES_HEALTH, this.max_health);
910 this.velocity = '0 0 0';
912 this.goalentity = NULL;
913 this.attack_finished_single[0] = 0;
914 this.moveto = this.origin;
917 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
919 TakeResource(this, RES_HEALTH, damage);
921 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
923 if(GetResource(this, RES_HEALTH) <= -50) // 100 health until gone?
925 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
927 // number of monsters spawned with mobspawn command
930 setthink(this, SUB_Remove);
931 this.nextthink = time + 0.1;
932 this.event_damage = func_null;
936 void Monster_Dead(entity this, entity attacker, float gibbed)
938 setthink(this, Monster_Dead_Think);
939 this.nextthink = time;
940 this.monster_lifetime = time + 5;
942 if(STAT(FROZEN, this))
943 Unfreeze(this, false); // remove any icy remains
945 monster_dropitem(this, attacker);
947 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
949 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
950 monsters_killed += 1;
952 if(IS_PLAYER(attacker))
953 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
954 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
958 // number of monsters spawned with mobspawn command
962 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
963 _setmodel(this, this.mdl_dead);
965 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
966 this.event_heal = func_null;
967 this.solid = SOLID_CORPSE;
968 this.takedamage = DAMAGE_AIM;
969 this.deadflag = DEAD_DEAD;
971 set_movetype(this, MOVETYPE_TOSS);
972 this.moveto = this.origin;
973 settouch(this, Monster_Touch); // reset incase monster was pouncing
974 this.reset = func_null;
976 this.attack_finished_single[0] = 0;
979 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
980 this.velocity = '0 0 0';
982 CSQCModel_UnlinkEntity(this);
984 Monster mon = this.monsterdef;
985 mon.mr_death(mon, this);
987 if(this.candrop && this.weapon)
989 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
990 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
994 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
996 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
999 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
1002 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
1005 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1008 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
1011 vector v = healtharmor_applydamage(100, GetResource(this, RES_ARMOR) / 100, deathtype, damage);
1015 Monster mon = this.monsterdef;
1016 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1020 TakeResource(this, RES_HEALTH, take);
1021 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1025 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1027 this.dmg_time = time;
1029 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1030 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1032 this.velocity += force * this.damageforcescale;
1034 if(deathtype != DEATH_DROWN.m_id && take)
1036 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1038 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1040 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1043 if(GetResource(this, RES_HEALTH) <= 0)
1045 if(deathtype == DEATH_KILL.m_id)
1046 this.candrop = false; // killed by mobkill command
1049 SUB_UseTargets(this, attacker, this.enemy);
1050 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1052 Monster_Dead(this, attacker, (GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id));
1054 WaypointSprite_Kill(this.sprite);
1056 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1058 if(GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1060 Violence_GibSplash(this, 1, 0.5, attacker);
1062 setthink(this, SUB_Remove);
1063 this.nextthink = time + 0.1;
1068 bool Monster_Heal(entity targ, entity inflictor, float amount, float limit)
1070 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
1071 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
1074 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
1076 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
1080 // don't check for enemies, just keep walking in a straight line
1081 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1083 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)
1086 if(time >= this.spawn_time)
1087 setanim(this, this.anim_idle, true, false, false);
1088 movelib_brake_simple(this, 0.6);
1092 makevectors(this.angles);
1093 vector a = CENTER_OR_VIEWOFS(this);
1094 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1096 traceline(a, b, MOVE_NORMAL, this);
1098 bool reverse = false;
1099 if(trace_fraction != 1.0)
1101 if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1103 if(trace_ent && IS_MONSTER(trace_ent))
1106 if(!allow_jumpoff && IS_ONGROUND(this))
1108 traceline(b, b - '0 0 32', MOVE_NORMAL, this);
1109 if(trace_fraction == 1.0)
1115 this.angles_y = anglemods(this.angles_y - 180);
1116 makevectors(this.angles);
1119 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1121 if(time > this.pain_finished && time > this.attack_finished_single[0])
1123 if(vdist(this.velocity, >, 10))
1124 setanim(this, this.anim_walk, true, false, false);
1126 setanim(this, this.anim_idle, true, false, false);
1130 void Monster_Anim(entity this)
1132 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1137 // Decide on which death animation to use.
1139 deadbits = ANIMSTATE_DEAD1;
1141 deadbits = ANIMSTATE_DEAD2;
1146 // Clear a previous death animation.
1149 int animbits = deadbits;
1150 if(STAT(FROZEN, this))
1151 animbits |= ANIMSTATE_FROZEN;
1153 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1154 animdecide_setstate(this, animbits, false);
1155 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1157 /* // weapon entities for monsters?
1158 if (this.weaponentity)
1160 updateanim(this.weaponentity);
1161 if (!this.weaponentity.animstate_override)
1162 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1167 void Monster_Frozen_Think(entity this)
1169 if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
1171 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1172 SetResourceExplicit(this, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * this.max_health));
1174 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1176 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1177 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1179 if(STAT(REVIVE_PROGRESS, this) >= 1)
1180 Unfreeze(this, false);
1182 else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
1184 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1185 SetResourceExplicit(this, RES_HEALTH, max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
1187 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1188 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1190 if(GetResource(this, RES_HEALTH) < 1)
1192 Unfreeze(this, false);
1193 if(this.event_damage)
1194 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
1196 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1197 Unfreeze(this, false);
1199 // otherwise, no revival!
1201 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1204 void Monster_Enemy_Check(entity this)
1208 this.enemy = Monster_FindTarget(this);
1211 WarpZone_RefSys_Copy(this.enemy, this);
1212 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1213 // update move target immediately?
1214 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1215 this.monster_moveto = '0 0 0';
1216 this.monster_face = '0 0 0';
1218 //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)));
1219 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1224 void Monster_Think(entity this)
1226 setthink(this, Monster_Think);
1227 this.nextthink = time + this.ticrate;
1229 if(this.monster_lifetime && time >= this.monster_lifetime)
1231 Damage(this, this, this, GetResource(this, RES_HEALTH) + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1235 if(STAT(FROZEN, this))
1236 Monster_Frozen_Think(this);
1237 else if(time >= this.last_enemycheck)
1239 Monster_Enemy_Check(this);
1240 this.last_enemycheck = time + 1; // check for enemies every second
1243 Monster mon = this.monsterdef;
1244 if(mon.mr_think(mon, this))
1246 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1248 .entity weaponentity = weaponentities[0]; // TODO?
1249 Monster_Attack_Check(this, this.enemy, weaponentity);
1254 CSQCMODEL_AUTOUPDATE(this);
1257 bool Monster_Spawn_Setup(entity this)
1259 Monster mon = this.monsterdef;
1260 mon.mr_setup(mon, this);
1262 // ensure some basic needs are met
1263 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 100); }
1264 if(!GetResource(this, RES_ARMOR)) { SetResourceExplicit(this, RES_ARMOR, bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9)); }
1265 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1266 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1267 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1268 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1269 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1271 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1273 Monster_Miniboss_Check(this);
1274 SetResourceExplicit(this, RES_HEALTH, GetResource(this, RES_HEALTH) * MONSTER_SKILLMOD(this));
1277 this.skin = rint(random() * 4);
1280 this.max_health = GetResource(this, RES_HEALTH);
1281 this.pain_finished = this.nextthink;
1283 if(IS_PLAYER(this.monster_follow))
1284 this.effects |= EF_DIMLIGHT;
1286 if(!this.wander_delay) { this.wander_delay = 2; }
1287 if(!this.wander_distance) { this.wander_distance = 600; }
1289 Monster_Sounds_Precache(this);
1290 Monster_Sounds_Update(this);
1294 if(!this.monster_attack)
1295 IL_PUSH(g_monster_targets, this);
1296 this.monster_attack = true; // we can have monster enemies in team games
1299 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1301 if(autocvar_g_monsters_healthbars)
1303 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1304 wp.wp_extra = this.monsterdef.monsterid;
1305 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1306 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1308 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1309 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1313 setthink(this, Monster_Think);
1314 this.nextthink = time + this.ticrate;
1316 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1322 bool Monster_Spawn(entity this, bool check_appear, Monster mon)
1324 // setup the basic required properties for a monster
1326 if(!mon || mon == MON_Null) { return false; } // invalid monster
1327 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1329 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED) && !(this.flags & FL_MONSTER))
1331 IL_PUSH(g_monsters, this);
1332 if(this.mdl && this.mdl != "")
1333 precache_model(this.mdl);
1334 if(this.mdl_dead && this.mdl_dead != "")
1335 precache_model(this.mdl_dead);
1338 if(check_appear && Monster_Appear_Check(this, mon)) { return true; } // return true so the monster isn't removed
1340 if(!this.monster_skill)
1341 this.monster_skill = cvar("g_monsters_skill");
1343 // support for quake style removing monsters based on skill
1344 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1345 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1346 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1348 if(this.team && !teamplay)
1351 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1352 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1353 monsters_total += 1;
1355 if(this.mdl && this.mdl != "")
1356 _setmodel(this, this.mdl);
1358 setmodel(this, mon.m_model);
1360 this.flags = FL_MONSTER;
1361 this.classname = "monster";
1362 this.takedamage = DAMAGE_AIM;
1363 if(!this.bot_attack)
1364 IL_PUSH(g_bot_targets, this);
1365 this.bot_attack = true;
1366 this.iscreature = true;
1367 this.teleportable = true;
1368 if(!this.damagedbycontents)
1369 IL_PUSH(g_damagedbycontents, this);
1370 this.damagedbycontents = true;
1371 this.monsterdef = mon;
1372 this.event_damage = Monster_Damage;
1373 this.event_heal = Monster_Heal;
1374 settouch(this, Monster_Touch);
1375 this.use = Monster_Use;
1376 this.solid = SOLID_BBOX;
1377 set_movetype(this, MOVETYPE_WALK);
1378 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1380 this.velocity = '0 0 0';
1381 this.moveto = this.origin;
1382 this.pos1 = this.origin;
1383 this.pos2 = this.angles;
1384 this.reset = Monster_Reset;
1385 this.netname = mon.netname;
1386 this.monster_attackfunc = mon.monster_attackfunc;
1387 this.monster_name = mon.monster_name;
1388 this.candrop = true;
1389 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1390 this.oldtarget2 = this.target2;
1391 //this.pass_distance = 0;
1392 this.deadflag = DEAD_NO;
1393 this.spawn_time = time;
1395 this.monster_moveto = '0 0 0';
1396 this.monster_face = '0 0 0';
1397 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1399 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1400 if(!this.scale) { this.scale = 1; }
1401 if(autocvar_g_monsters_edit) { this.grab = 1; }
1402 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1403 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1404 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1406 if(autocvar_g_playerclip_collisions)
1407 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1409 if(mon.spawnflags & MONSTER_TYPE_FLY)
1411 this.flags |= FL_FLY;
1412 set_movetype(this, MOVETYPE_FLY);
1415 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1417 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1420 if(mon.spawnflags & MONSTER_SIZE_QUAKE)
1421 if(autocvar_g_monsters_quake_resize)
1425 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1427 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1429 Monster_UpdateModel(this);
1431 if(!Monster_Spawn_Setup(this))
1433 Monster_Remove(this);
1439 setorigin(this, this.origin + '0 0 20');
1440 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1441 setorigin(this, trace_endpos);
1444 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1445 monster_setupcolors(this);
1447 CSQCMODEL_AUTOINIT(this);