1 #include "sv_monsters.qh"
3 #include <common/constants.qh>
4 #include <common/deathtypes/all.qh>
5 #include <common/items/_mod.qh>
6 #include <common/mapobjects/teleporters.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/monsters/all.qh>
9 #include <common/physics/movelib.qh>
10 #include <common/stats.qh>
11 #include <common/teams.qh>
12 #include <common/turrets/sv_turrets.qh>
13 #include <common/turrets/util.qh>
14 #include <common/util.qh>
15 #include <common/vehicles/all.qh>
16 #include <common/weapons/_all.qh>
17 #include <common/weapons/_mod.qh>
18 #include <lib/csqcmodel/sv_model.qh>
19 #include <lib/warpzone/common.qh>
20 #include <server/autocvars.qh>
21 #include <server/campaign.qh>
22 #include <server/cheats.qh>
23 #include <server/command/_mod.qh>
24 #include <server/damage.qh>
25 #include <server/items/items.qh>
26 #include <server/mutators/_mod.qh>
27 #include <server/round_handler.qh>
28 #include <server/steerlib.qh>
29 #include <server/weapons/_mod.qh>
31 void monsters_setstatus(entity this)
33 STAT(MONSTERS_TOTAL, this) = monsters_total;
34 STAT(MONSTERS_KILLED, this) = monsters_killed;
37 void monster_dropitem(entity this, entity attacker)
39 if(!this.candrop || !this.monster_loot)
42 vector org = CENTER_OR_VIEWOFS(this);
44 Item_SetLoot(e, true);
45 e.spawnfunc_checked = true;
47 e.monster_loot = this.monster_loot;
49 MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
50 e = M_ARGV(1, entity);
52 if(e && e.monster_loot)
55 StartItem(e, e.monster_loot);
58 e.velocity = randomvec() * 175 + '0 0 325';
59 e.item_spawnshieldtime = time + 0.7;
60 SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
64 void monster_makevectors(entity this, entity targ)
68 vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
69 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
70 this.v_angle_x = -this.v_angle_x;
73 makevectors(this.v_angle);
80 bool Monster_ValidTarget(entity this, entity targ)
82 // ensure we're not checking nonexistent monster/target
83 if(!this || !targ) { return false; }
86 || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
87 || (IS_VEHICLE(targ) && !(this.monsterdef.spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
88 || (time < game_starttime) // monsters do nothing before match has started
89 || (targ.takedamage == DAMAGE_NO)
91 || (targ.items & IT_INVISIBILITY)
92 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
93 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || GetResource(targ, RES_HEALTH) <= 0 || GetResource(this, RES_HEALTH) <= 0))
94 || (this.monster_follow == targ || targ.monster_follow == this)
95 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
96 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
97 || (SAME_TEAM(targ, this))
98 || (STAT(FROZEN, targ))
99 || (targ.alpha != 0 && targ.alpha < 0.5)
100 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
103 // if any of the above checks fail, target is not valid
107 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
108 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this);
110 if(trace_fraction < 1 && trace_ent != targ)
111 return false; // solid
113 if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
114 if(this.enemy != targ)
116 makevectors (this.angles);
117 float dot = normalize (targ.origin - this.origin) * v_forward;
119 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
122 return true; // this target is valid!
125 entity Monster_FindTarget(entity this)
127 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
129 entity closest_target = NULL;
130 vector my_center = CENTER_OR_VIEWOFS(this);
132 // find the closest acceptable target to pass to
133 IL_EACH(g_monster_targets, it.monster_attack && vdist(it.origin - this.origin, <, this.target_range),
135 if(Monster_ValidTarget(this, it))
137 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in damage.qc)
138 vector targ_center = CENTER_OR_VIEWOFS(it);
142 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
143 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
144 { closest_target = it; }
146 else { closest_target = it; }
150 return closest_target;
153 void monster_setupcolors(entity this)
155 if(IS_PLAYER(this.realowner))
156 this.colormap = this.realowner.colormap;
157 else if(teamplay && this.team)
158 this.colormap = 1024 + (this.team - 1) * 17;
161 if(this.monster_skill <= MONSTER_SKILL_EASY)
162 this.colormap = 1029;
163 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
164 this.colormap = 1027;
165 else if(this.monster_skill <= MONSTER_SKILL_HARD)
166 this.colormap = 1038;
167 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
168 this.colormap = 1028;
169 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
170 this.colormap = 1032;
172 this.colormap = 1024;
176 void monster_changeteam(entity this, int newteam)
178 if(!teamplay) { return; }
181 if(!this.monster_attack)
182 IL_PUSH(g_monster_targets, this);
183 this.monster_attack = true; // new team, activate attacking
184 monster_setupcolors(this);
188 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
190 this.sprite.team = newteam;
191 this.sprite.SendFlags |= 1;
195 .void(entity) monster_delayedfunc;
196 void Monster_Delay_Action(entity this)
198 if(Monster_ValidTarget(this.owner, this.owner.enemy))
200 monster_makevectors(this.owner, this.owner.enemy);
201 this.monster_delayedfunc(this.owner);
207 setthink(this, Monster_Delay_Action);
208 this.nextthink = time + this.count;
212 setthink(this, SUB_Remove);
213 this.nextthink = time;
217 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
219 // deferred attacking, checks if monster is still alive and target is still valid before attacking
222 setthink(e, Monster_Delay_Action);
223 e.nextthink = time + defer_amnt;
224 e.count = defer_amnt;
226 e.monster_delayedfunc = func;
227 e.cnt = repeat_count;
235 string get_monster_model_datafilename(string m, float sk, string fil)
240 m = "models/monsters/*_";
242 m = strcat(m, ftos(sk));
245 return strcat(m, ".", fil);
248 void Monster_Sound_Precache(string f)
252 fh = fopen(f, FILE_READ);
255 while((s = fgets(fh)))
257 if(tokenize_console(s) != 3)
259 //LOG_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
262 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
267 void Monster_Sounds_Precache(entity this)
269 string m = this.monsterdef.m_model.model_str();
270 float globhandle, n, i;
273 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
276 n = search_getsize(globhandle);
277 for (i = 0; i < n; ++i)
279 //print(search_getfilename(globhandle, i), "\n");
280 f = search_getfilename(globhandle, i);
281 Monster_Sound_Precache(f);
283 search_end(globhandle);
286 void Monster_Sounds_Clear(entity this)
288 #define _MSOUND(m) strfree(this.monstersound_##m);
293 .string Monster_Sound_SampleField(string type)
295 GetMonsterSoundSampleField_notFound = 0;
298 #define _MSOUND(m) case #m: return monstersound_##m;
302 GetMonsterSoundSampleField_notFound = 1;
306 bool Monster_Sounds_Load(entity this, string f, int first)
310 float fh = fopen(f, FILE_READ);
313 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
316 while((s = fgets(fh)))
318 if(tokenize_console(s) != 3)
320 field = Monster_Sound_SampleField(argv(0));
321 if(GetMonsterSoundSampleField_notFound)
323 strcpy(this.(field), strcat(argv(1), " ", argv(2)));
329 .int skin_for_monstersound;
330 void Monster_Sounds_Update(entity this)
332 if(this.skin == this.skin_for_monstersound) { return; }
334 this.skin_for_monstersound = this.skin;
335 Monster_Sounds_Clear(this);
336 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
337 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
340 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
342 if(!autocvar_g_monsters_sounds) { return; }
345 if(time < this.msound_delay)
347 string sample = this.(samplefield);
348 if (sample != "") sample = GlobalSound_sample(sample, random());
349 float myscale = ((this.scale) ? this.scale : 1); // safety net
350 // TODO: change volume depending on size too?
351 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
353 this.msound_delay = time + sound_delay;
357 // =======================
358 // Monster attack handlers
359 // =======================
361 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
363 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
365 setanim(this, anim, false, true, false);
367 if(this.animstate_endtime > time && IS_MONSTER(this))
368 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
370 this.attack_finished_single[0] = this.anim_finished = time + animtime;
372 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
374 if(trace_ent.takedamage)
375 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
380 bool Monster_Attack_Leap_Check(entity this, vector vel)
382 if(this.state && IS_MONSTER(this))
383 return false; // already attacking
384 if(!IS_ONGROUND(this))
385 return false; // not on the ground
386 if(GetResource(this, RES_HEALTH) <= 0 || IS_DEAD(this))
387 return false; // called when dead?
388 if(time < this.attack_finished_single[0])
389 return false; // still attacking
391 vector old = this.velocity;
394 tracetoss(this, this);
396 if(trace_ent != this.enemy)
402 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
404 if(!Monster_Attack_Leap_Check(this, vel))
407 setanim(this, anm, false, true, false);
409 if(this.animstate_endtime > time && IS_MONSTER(this))
410 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
412 this.attack_finished_single[0] = this.anim_finished = time + animtime;
415 this.state = MONSTER_ATTACK_RANGED;
416 settouch(this, touchfunc);
419 UNSET_ONGROUND(this);
424 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
426 int slot = weaponslot(weaponentity);
429 || (!this.monster_attackfunc)
430 || (time < this.attack_finished_single[slot])
433 if(vdist(targ.origin - this.origin, <=, this.attack_range))
435 monster_makevectors(this, targ);
436 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
437 if(attack_success == 1)
438 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
439 else if(attack_success > 0)
443 if(vdist(targ.origin - this.origin, >, this.attack_range))
445 monster_makevectors(this, targ);
446 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
447 if(attack_success == 1)
448 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
449 else if(attack_success > 0)
455 // ======================
456 // Main monster functions
457 // ======================
459 void Monster_UpdateModel(entity this)
461 // assume some defaults
462 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
463 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
464 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
465 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
466 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
467 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
468 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
469 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
470 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
471 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
473 // then get the real values
474 Monster mon = this.monsterdef;
475 mon.mr_anim(mon, this);
478 void Monster_Touch(entity this, entity toucher)
480 if(toucher == NULL) { return; }
482 if(toucher.monster_attack)
483 if(this.enemy != toucher)
484 if(!IS_MONSTER(toucher))
485 if(Monster_ValidTarget(this, toucher))
486 this.enemy = toucher;
489 void Monster_Miniboss_Check(entity this)
491 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
494 float chance = random() * 100;
496 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
497 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
499 GiveResource(this, RES_HEALTH, autocvar_g_monsters_miniboss_healthboost);
500 this.effects |= EF_RED;
502 this.weapon = WEP_VORTEX.m_id;
506 bool Monster_Respawn_Check(entity this)
508 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
509 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
510 return true; // enabled by a mutator
512 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
515 if(!autocvar_g_monsters_respawn)
521 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterdef); }
525 void Monster_Dead_Fade(entity this)
527 if(Monster_Respawn_Check(this))
529 this.spawnflags |= MONSTERFLAG_RESPAWNED;
530 setthink(this, Monster_Respawn);
531 this.nextthink = time + this.respawntime;
532 this.monster_lifetime = 0;
533 this.deadflag = DEAD_RESPAWNING;
534 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
536 this.pos1 = this.origin;
537 this.pos2 = this.angles;
539 this.event_damage = func_null;
540 this.event_heal = func_null;
541 this.takedamage = DAMAGE_NO;
542 setorigin(this, this.pos1);
543 this.angles = this.pos2;
544 SetResourceExplicit(this, RES_HEALTH, this.max_health);
545 setmodel(this, MDL_Null);
549 // number of monsters spawned with mobspawn command
552 SUB_SetFade(this, time + 3, 1);
556 void Monster_Use(entity this, entity actor, entity trigger)
558 if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
561 .float pass_distance;
562 vector Monster_Move_Target(entity this, entity targ)
564 // enemy is always preferred target
567 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
568 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
569 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
571 // cases where the enemy may have changed their state (don't need to check everything here)
573 || (IS_DEAD(this.enemy) || GetResource(this.enemy, RES_HEALTH) < 1)
574 || (STAT(FROZEN, this.enemy))
575 || (this.enemy.flags & FL_NOTARGET)
576 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
577 || (this.enemy.takedamage == DAMAGE_NO)
578 || (vdist(this.origin - targ_origin, >, this.target_range))
579 || ((trace_fraction < 1) && (trace_ent != this.enemy)))
582 //this.pass_distance = 0;
587 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
588 print("Trace origin: ", vtos(targ_origin), "\n");
589 print("Target origin: ", vtos(this.enemy.origin), "\n");
590 print("My origin: ", vtos(this.origin), "\n"); */
592 this.monster_movestate = MONSTER_MOVE_ENEMY;
593 this.last_trace = time + 1.2;
594 if(this.monster_moveto)
595 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
600 /*makevectors(this.angles);
601 this.monster_movestate = MONSTER_MOVE_ENEMY;
602 this.last_trace = time + 1.2;
603 return this.enemy.origin; */
606 switch(this.monster_moveflags)
608 case MONSTER_MOVE_FOLLOW:
610 this.monster_movestate = MONSTER_MOVE_FOLLOW;
611 this.last_trace = time + 0.3;
612 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
614 case MONSTER_MOVE_SPAWNLOC:
616 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
617 this.last_trace = time + 2;
620 case MONSTER_MOVE_NOMOVE:
622 if(this.monster_moveto)
624 this.last_trace = time + 0.5;
625 return this.monster_moveto;
629 this.monster_movestate = MONSTER_MOVE_NOMOVE;
630 this.last_trace = time + 2;
635 case MONSTER_MOVE_WANDER:
638 this.monster_movestate = MONSTER_MOVE_WANDER;
640 if(this.monster_moveto)
642 this.last_trace = time + 0.5;
643 pos = this.monster_moveto;
647 this.last_trace = time + 0.5;
652 this.last_trace = time + this.wander_delay;
654 this.angles_y = rint(random() * 500);
655 makevectors(this.angles);
656 pos = this.origin + v_forward * this.wander_distance;
658 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
660 pos.z = random() * 200;
671 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
673 //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
674 //float initial_height = 0; //min(50, (targ_distance * tanh(20)));
675 //float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
676 //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
680 if(current_height) // make sure we can actually do this arcing path
682 targpos = (to + ('0 0 1' * current_height));
683 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
684 if(trace_fraction < 1)
686 //print("normal arc line failed, trying to find new pos...");
687 WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
688 targpos = (trace_endpos + '0 0 -10');
689 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
690 if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
691 /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
694 else { targpos = to; }
697 //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
699 vector desired_direction = normalize(targpos - from);
700 if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
701 else { this.velocity = (desired_direction * movespeed); }
703 //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
704 //this.angles = vectoangles(this.velocity);
709 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
711 // update goal entity if lost
712 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
713 this.goalentity = find(NULL, targetname, this.target2);
715 if(STAT(FROZEN, this))
717 movelib_brake_simple(this, stpspeed);
718 setanim(this, this.anim_idle, true, false, false);
719 return; // no physics while frozen!
722 if(this.flags & FL_SWIM)
724 if(this.waterlevel < WATERLEVEL_WETFEET)
726 if(time >= this.last_trace)
728 this.last_trace = time + 0.4;
730 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
731 this.angles = '90 90 0';
734 this.velocity_y += random() * 50;
735 this.velocity_x -= random() * 50;
739 this.velocity_y -= random() * 50;
740 this.velocity_x += random() * 50;
742 this.velocity_z += random() * 150;
746 set_movetype(this, MOVETYPE_BOUNCE);
747 //this.velocity_z = -200;
751 else if(this.move_movetype == MOVETYPE_BOUNCE)
754 set_movetype(this, MOVETYPE_WALK);
758 entity targ = this.goalentity;
760 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
762 || this.draggedby != NULL
763 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
764 || time < game_starttime
765 || (autocvar_g_campaign && !campaign_bots_may_start)
766 || time < this.spawn_time)
768 runspeed = walkspeed = 0;
769 if(time >= this.spawn_time)
770 setanim(this, this.anim_idle, true, false, false);
771 movelib_brake_simple(this, stpspeed);
775 targ = M_ARGV(3, entity);
776 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
777 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
779 if(teamplay && autocvar_g_monsters_teams)
780 if(DIFF_TEAM(this.monster_follow, this))
781 this.monster_follow = NULL;
783 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
786 settouch(this, Monster_Touch);
789 if(this.state && time >= this.attack_finished_single[0])
790 this.state = 0; // attack is over
792 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
793 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
794 this.moveto = Monster_Move_Target(this, targ);
797 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
799 if(this.state == MONSTER_ATTACK_MELEE)
800 this.moveto = this.origin;
802 if(this.enemy && this.enemy.vehicle)
805 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
806 this.moveto_z = this.origin_z;
808 if(vdist(this.origin - this.moveto, >, 100))
810 bool do_run = (this.enemy || this.monster_moveto);
811 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
812 Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
814 if(time > this.pain_finished && time > this.anim_finished) // TODO: use anim_finished instead!?
817 if(vdist(this.velocity, >, 10))
818 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
820 setanim(this, this.anim_idle, true, false, false);
825 entity e = this.goalentity; //find(NULL, targetname, this.target2);
826 if(e.target2 && e.target2 != "")
827 this.target2 = e.target2;
828 else if(e.target && e.target != "") // compatibility
829 this.target2 = e.target;
831 movelib_brake_simple(this, stpspeed);
832 if(time > this.anim_finished && time > this.pain_finished)
834 if(vdist(this.velocity, <=, 30))
835 setanim(this, this.anim_idle, true, false, false);
838 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
840 vector real_angle = vectoangles(this.steerto) - this.angles;
842 if(this.state == MONSTER_ATTACK_MELEE)
846 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
847 this.angles_y += turny;
851 void Monster_Remove(entity this)
854 return; // don't remove it?
856 if(!this) { return; }
858 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
859 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
861 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
863 .entity weaponentity = weaponentities[slot];
864 if(this.(weaponentity))
865 delete(this.(weaponentity));
867 if(this.iceblock) { delete(this.iceblock); }
868 WaypointSprite_Kill(this.sprite);
872 void Monster_Dead_Think(entity this)
874 this.nextthink = time + this.ticrate;
876 if(this.monster_lifetime != 0)
877 if(time >= this.monster_lifetime)
879 Monster_Dead_Fade(this);
884 void Monster_Appear(entity this, entity actor, entity trigger)
887 Monster_Spawn(this, false, this.monsterdef);
890 bool Monster_Appear_Check(entity this, Monster monster_id)
892 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
895 setthink(this, func_null);
896 this.monsterdef = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
898 this.use = Monster_Appear;
899 this.flags = FL_MONSTER; // set so this monster can get butchered
904 void Monster_Reset(entity this)
906 setorigin(this, this.pos1);
907 this.angles = this.pos2;
909 Unfreeze(this, false); // remove any icy remains
911 SetResourceExplicit(this, RES_HEALTH, this.max_health);
912 this.velocity = '0 0 0';
914 this.goalentity = NULL;
915 this.attack_finished_single[0] = 0;
916 this.moveto = this.origin;
919 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
921 TakeResource(this, RES_HEALTH, damage);
923 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
925 if(GetResource(this, RES_HEALTH) <= -50) // 100 health until gone?
927 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
929 // number of monsters spawned with mobspawn command
932 setthink(this, SUB_Remove);
933 this.nextthink = time + 0.1;
934 this.event_damage = func_null;
938 void Monster_Dead(entity this, entity attacker, float gibbed)
940 setthink(this, Monster_Dead_Think);
941 this.nextthink = time;
942 this.monster_lifetime = time + 5;
944 if(STAT(FROZEN, this))
945 Unfreeze(this, false); // remove any icy remains
947 monster_dropitem(this, attacker);
949 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
951 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
952 monsters_killed += 1;
954 if(IS_PLAYER(attacker))
955 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
956 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
960 // number of monsters spawned with mobspawn command
964 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
965 _setmodel(this, this.mdl_dead);
967 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
968 this.event_heal = func_null;
969 this.solid = SOLID_CORPSE;
970 this.takedamage = DAMAGE_AIM;
971 this.deadflag = DEAD_DEAD;
973 set_movetype(this, MOVETYPE_TOSS);
974 this.moveto = this.origin;
975 settouch(this, Monster_Touch); // reset incase monster was pouncing
976 this.reset = func_null;
978 this.attack_finished_single[0] = 0;
981 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
982 this.velocity = '0 0 0';
984 CSQCModel_UnlinkEntity(this);
986 Monster mon = this.monsterdef;
987 mon.mr_death(mon, this);
989 if(this.candrop && this.weapon)
991 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
992 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
996 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
998 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
1001 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
1004 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
1007 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1010 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
1013 vector v = healtharmor_applydamage(100, GetResource(this, RES_ARMOR) / 100, deathtype, damage);
1017 Monster mon = this.monsterdef;
1018 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1022 TakeResource(this, RES_HEALTH, take);
1023 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1027 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1029 this.dmg_time = time;
1031 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1032 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1034 this.velocity += force * this.damageforcescale;
1036 if(deathtype != DEATH_DROWN.m_id && take)
1038 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1040 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1042 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1045 if(GetResource(this, RES_HEALTH) <= 0)
1047 if(deathtype == DEATH_KILL.m_id)
1048 this.candrop = false; // killed by mobkill command
1051 SUB_UseTargets(this, attacker, this.enemy);
1052 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1054 Monster_Dead(this, attacker, (GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id));
1056 WaypointSprite_Kill(this.sprite);
1058 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1060 if(GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1062 Violence_GibSplash(this, 1, 0.5, attacker);
1064 setthink(this, SUB_Remove);
1065 this.nextthink = time + 0.1;
1070 bool Monster_Heal(entity targ, entity inflictor, float amount, float limit)
1072 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
1073 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
1076 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
1078 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
1082 // don't check for enemies, just keep walking in a straight line
1083 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1085 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)
1088 if(time >= this.spawn_time)
1089 setanim(this, this.anim_idle, true, false, false);
1090 movelib_brake_simple(this, 0.6);
1094 makevectors(this.angles);
1095 vector a = CENTER_OR_VIEWOFS(this);
1096 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1098 traceline(a, b, MOVE_NORMAL, this);
1100 bool reverse = false;
1101 if(trace_fraction != 1.0)
1103 if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1105 if(trace_ent && IS_MONSTER(trace_ent))
1108 if(!allow_jumpoff && IS_ONGROUND(this))
1110 traceline(b, b - '0 0 32', MOVE_NORMAL, this);
1111 if(trace_fraction == 1.0)
1117 this.angles_y = anglemods(this.angles_y - 180);
1118 makevectors(this.angles);
1121 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1123 if(time > this.pain_finished && time > this.attack_finished_single[0])
1125 if(vdist(this.velocity, >, 10))
1126 setanim(this, this.anim_walk, true, false, false);
1128 setanim(this, this.anim_idle, true, false, false);
1132 void Monster_Anim(entity this)
1134 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1139 // Decide on which death animation to use.
1141 deadbits = ANIMSTATE_DEAD1;
1143 deadbits = ANIMSTATE_DEAD2;
1148 // Clear a previous death animation.
1151 int animbits = deadbits;
1152 if(STAT(FROZEN, this))
1153 animbits |= ANIMSTATE_FROZEN;
1155 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1156 animdecide_setstate(this, animbits, false);
1157 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1159 /* // weapon entities for monsters?
1160 if (this.weaponentity)
1162 updateanim(this.weaponentity);
1163 if (!this.weaponentity.animstate_override)
1164 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1169 void Monster_Frozen_Think(entity this)
1171 if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
1173 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1174 SetResourceExplicit(this, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * this.max_health));
1176 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1178 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1179 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1181 if(STAT(REVIVE_PROGRESS, this) >= 1)
1182 Unfreeze(this, false);
1184 else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
1186 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1187 SetResourceExplicit(this, RES_HEALTH, max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
1189 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1190 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1192 if(GetResource(this, RES_HEALTH) < 1)
1194 Unfreeze(this, false);
1195 if(this.event_damage)
1196 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
1198 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1199 Unfreeze(this, false);
1201 // otherwise, no revival!
1203 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1206 void Monster_Enemy_Check(entity this)
1210 this.enemy = Monster_FindTarget(this);
1213 WarpZone_RefSys_Copy(this.enemy, this);
1214 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1215 // update move target immediately?
1216 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1217 this.monster_moveto = '0 0 0';
1218 this.monster_face = '0 0 0';
1220 //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)));
1221 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1226 void Monster_Think(entity this)
1228 setthink(this, Monster_Think);
1229 this.nextthink = time + this.ticrate;
1231 if(this.monster_lifetime && time >= this.monster_lifetime)
1233 Damage(this, this, this, GetResource(this, RES_HEALTH) + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1237 if(STAT(FROZEN, this))
1238 Monster_Frozen_Think(this);
1239 else if(time >= this.last_enemycheck)
1241 Monster_Enemy_Check(this);
1242 this.last_enemycheck = time + 1; // check for enemies every second
1245 Monster mon = this.monsterdef;
1246 if(mon.mr_think(mon, this))
1248 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1250 .entity weaponentity = weaponentities[0]; // TODO?
1251 Monster_Attack_Check(this, this.enemy, weaponentity);
1256 CSQCMODEL_AUTOUPDATE(this);
1259 bool Monster_Spawn_Setup(entity this)
1261 Monster mon = this.monsterdef;
1262 mon.mr_setup(mon, this);
1264 // ensure some basic needs are met
1265 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 100); }
1266 if(!GetResource(this, RES_ARMOR)) { SetResourceExplicit(this, RES_ARMOR, bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9)); }
1267 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1268 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1269 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1270 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1271 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1273 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1275 Monster_Miniboss_Check(this);
1276 SetResourceExplicit(this, RES_HEALTH, GetResource(this, RES_HEALTH) * MONSTER_SKILLMOD(this));
1279 this.skin = rint(random() * 4);
1282 this.max_health = GetResource(this, RES_HEALTH);
1283 this.pain_finished = this.nextthink;
1285 if(IS_PLAYER(this.monster_follow))
1286 this.effects |= EF_DIMLIGHT;
1288 if(!this.wander_delay) { this.wander_delay = 2; }
1289 if(!this.wander_distance) { this.wander_distance = 600; }
1291 Monster_Sounds_Precache(this);
1292 Monster_Sounds_Update(this);
1296 if(!this.monster_attack)
1297 IL_PUSH(g_monster_targets, this);
1298 this.monster_attack = true; // we can have monster enemies in team games
1301 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1303 if(autocvar_g_monsters_healthbars)
1305 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1306 wp.wp_extra = this.monsterdef.monsterid;
1307 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1308 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1310 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1311 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1315 setthink(this, Monster_Think);
1316 this.nextthink = time + this.ticrate;
1318 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1324 bool Monster_Spawn(entity this, bool check_appear, Monster mon)
1326 // setup the basic required properties for a monster
1328 if(!mon || mon == MON_Null) { return false; } // invalid monster
1329 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1331 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED) && !(this.flags & FL_MONSTER))
1333 IL_PUSH(g_monsters, this);
1334 if(this.mdl && this.mdl != "")
1335 precache_model(this.mdl);
1336 if(this.mdl_dead && this.mdl_dead != "")
1337 precache_model(this.mdl_dead);
1340 if(check_appear && Monster_Appear_Check(this, mon)) { return true; } // return true so the monster isn't removed
1342 if(!this.monster_skill)
1343 this.monster_skill = cvar("g_monsters_skill");
1345 // support for quake style removing monsters based on skill
1346 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1347 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1348 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1350 if(this.team && !teamplay)
1353 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1354 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1355 monsters_total += 1;
1357 if(this.mdl && this.mdl != "")
1358 _setmodel(this, this.mdl);
1360 setmodel(this, mon.m_model);
1362 this.flags = FL_MONSTER;
1363 this.classname = "monster";
1364 this.takedamage = DAMAGE_AIM;
1365 if(!this.bot_attack)
1366 IL_PUSH(g_bot_targets, this);
1367 this.bot_attack = true;
1368 this.iscreature = true;
1369 this.teleportable = true;
1370 if(!this.damagedbycontents)
1371 IL_PUSH(g_damagedbycontents, this);
1372 this.damagedbycontents = true;
1373 this.monsterdef = mon;
1374 this.event_damage = Monster_Damage;
1375 this.event_heal = Monster_Heal;
1376 settouch(this, Monster_Touch);
1377 this.use = Monster_Use;
1378 this.solid = SOLID_BBOX;
1379 set_movetype(this, MOVETYPE_WALK);
1380 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1382 this.velocity = '0 0 0';
1383 this.moveto = this.origin;
1384 this.pos1 = this.origin;
1385 this.pos2 = this.angles;
1386 this.reset = Monster_Reset;
1387 this.netname = mon.netname;
1388 this.monster_attackfunc = mon.monster_attackfunc;
1389 this.monster_name = mon.monster_name;
1390 this.candrop = true;
1391 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1392 this.oldtarget2 = this.target2;
1393 //this.pass_distance = 0;
1394 this.deadflag = DEAD_NO;
1395 this.spawn_time = time;
1397 this.monster_moveto = '0 0 0';
1398 this.monster_face = '0 0 0';
1399 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1401 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1402 if(!this.scale) { this.scale = 1; }
1403 if(autocvar_g_monsters_edit) { this.grab = 1; }
1404 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1405 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1406 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1408 if(autocvar_g_playerclip_collisions)
1409 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1411 if(mon.spawnflags & MONSTER_TYPE_FLY)
1413 this.flags |= FL_FLY;
1414 set_movetype(this, MOVETYPE_FLY);
1417 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1419 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1422 if(mon.spawnflags & MONSTER_SIZE_QUAKE)
1423 if(autocvar_g_monsters_quake_resize)
1427 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1429 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1431 Monster_UpdateModel(this);
1433 if(!Monster_Spawn_Setup(this))
1435 Monster_Remove(this);
1441 setorigin(this, this.origin + '0 0 20');
1442 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1443 setorigin(this, trace_endpos);
1446 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1447 monster_setupcolors(this);
1449 CSQCMODEL_AUTOINIT(this);