4 #include <lib/warpzone/common.qh>
5 #include "../constants.qh"
9 #include "sv_monsters.qh"
10 #include "../physics/movelib.qh"
11 #include "../weapons/all.qh"
12 #include <server/autocvars.qh>
13 #include <server/defs.qh>
14 #include "../deathtypes/all.qh"
15 #include <server/mutators/all.qh>
16 #include <server/steerlib.qh>
17 #include "../turrets/sv_turrets.qh"
18 #include "../turrets/util.qh"
19 #include "../vehicles/all.qh"
20 #include <server/campaign.qh>
21 #include <server/command/common.qh>
22 #include <server/command/cmd.qh>
23 #include "../triggers/triggers.qh"
24 #include <lib/csqcmodel/sv_model.qh>
25 #include <server/round_handler.qh>
28 void monsters_setstatus(entity this)
30 this.stat_monsters_total = monsters_total;
31 this.stat_monsters_killed = monsters_killed;
34 void monster_dropitem(entity this)
36 if(!this.candrop || !this.monster_loot)
39 vector org = this.origin + ((this.mins + this.maxs) * 0.5);
40 entity e = new(droppedweapon); // use weapon handling to remove it on touch
41 e.spawnfunc_checked = true;
43 e.monster_loot = this.monster_loot;
45 MUTATOR_CALLHOOK(MonsterDropItem, e);
48 if(e && e.monster_loot)
51 WITHSELF(e, e.monster_loot(e));
53 e.movetype = MOVETYPE_TOSS;
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) && !((get_monsterinfo(this.monsterid)).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)
88 || (targ.items & IT_INVISIBILITY)
89 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
90 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || targ.health <= 0 || this.health <= 0))
91 || (this.monster_follow == targ || targ.monster_follow == this)
92 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
93 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
94 || (SAME_TEAM(targ, this))
95 || (STAT(FROZEN, targ))
96 || (targ.alpha != 0 && targ.alpha < 0.5)
99 // if any of the above checks fail, target is not valid
103 traceline(this.origin + this.view_ofs, targ.origin, 0, this);
105 if((trace_fraction < 1) && (trace_ent != targ))
108 if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
109 if(this.enemy != targ)
113 makevectors (this.angles);
114 dot = normalize (targ.origin - this.origin) * v_forward;
116 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
119 return true; // this target is valid!
122 entity Monster_FindTarget(entity mon)
124 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return mon.enemy; } // Handled by a mutator
126 entity head, closest_target = world;
127 head = findradius(mon.origin, mon.target_range);
129 while(head) // find the closest acceptable target to pass to
131 if(head.monster_attack)
132 if(Monster_ValidTarget(mon, head))
134 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
135 vector head_center = CENTER_OR_VIEWOFS(head);
136 vector ent_center = CENTER_OR_VIEWOFS(mon);
140 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
141 if(vlen2(ent_center - head_center) < vlen2(ent_center - closest_target_center))
142 { closest_target = head; }
144 else { closest_target = head; }
150 return closest_target;
153 void monster_setupcolors(entity mon)
155 if(IS_PLAYER(mon.realowner))
156 mon.colormap = mon.realowner.colormap;
157 else if(teamplay && mon.team)
158 mon.colormap = 1024 + (mon.team - 1) * 17;
161 if(mon.monster_skill <= MONSTER_SKILL_EASY)
163 else if(mon.monster_skill <= MONSTER_SKILL_MEDIUM)
165 else if(mon.monster_skill <= MONSTER_SKILL_HARD)
167 else if(mon.monster_skill <= MONSTER_SKILL_INSANE)
169 else if(mon.monster_skill <= MONSTER_SKILL_NIGHTMARE)
176 void monster_changeteam(entity ent, float newteam)
178 if(!teamplay) { return; }
181 ent.monster_attack = true; // new team, activate attacking
182 monster_setupcolors(ent);
186 WaypointSprite_UpdateTeamRadar(ent.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
188 ent.sprite.team = newteam;
189 ent.sprite.SendFlags |= 1;
193 .void(entity) monster_delayedfunc;
194 void Monster_Delay_Action_self();
195 void Monster_Delay_Action(entity this)
197 if(Monster_ValidTarget(this.owner, this.owner.enemy)) { this.monster_delayedfunc(this.owner); }
202 this.think = Monster_Delay_Action_self;
203 this.nextthink = time + this.count;
207 this.think = SUB_Remove_self;
208 this.nextthink = time;
212 void Monster_Delay_Action_self()
215 Monster_Delay_Action(self);
218 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
220 // deferred attacking, checks if monster is still alive and target is still valid before attacking
223 e.think = Monster_Delay_Action_self;
224 e.nextthink = time + defer_amnt;
225 e.count = defer_amnt;
227 e.monster_delayedfunc = func;
228 e.cnt = repeat_count;
236 string get_monster_model_datafilename(string m, float sk, string fil)
241 m = "models/monsters/*_";
243 m = strcat(m, ftos(sk));
246 return strcat(m, ".", fil);
249 void Monster_Sound_Precache(string f)
253 fh = fopen(f, FILE_READ);
256 while((s = fgets(fh)))
258 if(tokenize_console(s) != 3)
260 LOG_TRACE("Invalid sound info line: ", s, "\n");
263 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
268 void Monster_Sounds_Precache(entity this)
270 string m = (Monsters_from(this.monsterid)).m_model.model_str();
271 float globhandle, n, i;
274 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
277 n = search_getsize(globhandle);
278 for (i = 0; i < n; ++i)
280 //print(search_getfilename(globhandle, i), "\n");
281 f = search_getfilename(globhandle, i);
282 Monster_Sound_Precache(f);
284 search_end(globhandle);
287 void Monster_Sounds_Clear(entity this)
289 #define _MSOUND(m) if(this.monstersound_##m) { strunzone(this.monstersound_##m); this.monstersound_##m = string_null; }
294 .string Monster_Sound_SampleField(string type)
296 GetMonsterSoundSampleField_notFound = 0;
299 #define _MSOUND(m) case #m: return monstersound_##m;
303 GetMonsterSoundSampleField_notFound = 1;
307 bool Monster_Sounds_Load(entity this, string f, int first)
312 fh = fopen(f, FILE_READ);
315 LOG_TRACE("Monster sound file not found: ", f, "\n");
318 while((s = fgets(fh)))
320 if(tokenize_console(s) != 3)
322 field = Monster_Sound_SampleField(argv(0));
323 if(GetMonsterSoundSampleField_notFound)
326 strunzone(this.(field));
327 this.(field) = strzone(strcat(argv(1), " ", argv(2)));
333 .int skin_for_monstersound;
334 void Monster_Sounds_Update(entity this)
336 if(this.skin == this.skin_for_monstersound) { return; }
338 this.skin_for_monstersound = this.skin;
339 Monster_Sounds_Clear(this);
340 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
341 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
344 void Monster_Sound(entity this, .string samplefield, float sound_delay, float delaytoo, float chan)
346 if(!autocvar_g_monsters_sounds) { return; }
349 if(time < this.msound_delay)
351 GlobalSound_string(this, this.(samplefield), chan, VOICETYPE_PLAYERSOUND);
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 monster_makevectors(this, targ);
374 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
376 if(trace_ent.takedamage)
377 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, trace_ent.origin, normalize(trace_ent.origin - this.origin));
382 bool Monster_Attack_Leap_Check(entity this, vector vel)
384 if(this.state && IS_MONSTER(this))
385 return false; // already attacking
386 if(!IS_ONGROUND(this))
387 return false; // not on the ground
388 if(this.health <= 0 || IS_DEAD(this))
389 return false; // called when dead?
390 if(time < this.attack_finished_single[0])
391 return false; // still attacking
393 vector old = this.velocity;
396 tracetoss(this, this);
398 if (trace_ent != this.enemy)
404 bool Monster_Attack_Leap(entity this, vector anm, void() touchfunc, vector vel, float animtime)
406 if(!Monster_Attack_Leap_Check(this, vel))
409 setanim(this, anm, false, true, false);
411 if(this.animstate_endtime > time && (this.flags & FL_MONSTER))
412 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
414 this.attack_finished_single[0] = this.anim_finished = time + animtime;
416 if(this.flags & FL_MONSTER)
417 this.state = MONSTER_ATTACK_RANGED;
418 this.touch = touchfunc;
421 UNSET_ONGROUND(this);
426 void Monster_Attack_Check(entity this, entity targ)
428 if((this == world || targ == world)
429 || (!this.monster_attackfunc)
430 || (time < this.attack_finished_single[0])
433 if(vdist(targ.origin - this.origin, <=, this.attack_range))
435 bool attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ);
436 if(attack_success == 1)
437 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
438 else if(attack_success > 0)
442 if(vdist(targ.origin - this.origin, >, this.attack_range))
444 float attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ);
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 = get_monsterinfo(this.monsterid);
473 mon.mr_anim(mon, this);
478 if(other == world) { return; }
480 if(other.monster_attack)
481 if(this.enemy != other)
482 if(!IS_MONSTER(other))
483 if(Monster_ValidTarget(this, other))
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 this.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() { SELFPARAM(); Monster_Spawn(this, this.monsterid); }
521 void Monster_Dead_Fade(entity this)
523 if(Monster_Respawn_Check(this))
525 this.spawnflags |= MONSTERFLAG_RESPAWNED;
526 this.think = Monster_Respawn;
527 this.nextthink = time + this.respawntime;
528 this.monster_lifetime = 0;
529 this.deadflag = DEAD_RESPAWNING;
530 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
532 this.pos1 = this.origin;
533 this.pos2 = this.angles;
535 this.event_damage = func_null;
536 this.takedamage = DAMAGE_NO;
537 setorigin(this, this.pos1);
538 this.angles = this.pos2;
539 this.health = this.max_health;
540 setmodel(this, MDL_Null);
544 // number of monsters spawned with mobspawn command
547 SUB_SetFade(this, time + 3, 1);
553 if(Monster_ValidTarget(this, activator)) { this.enemy = activator; }
556 vector Monster_Move_Target(entity this, entity targ)
558 // enemy is always preferred target
561 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
562 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
563 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
565 if((this.enemy == world)
566 || (IS_DEAD(this.enemy) || this.enemy.health < 1)
567 || (STAT(FROZEN, this.enemy))
568 || (this.enemy.flags & FL_NOTARGET)
569 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
570 || (this.enemy.takedamage == DAMAGE_NO)
571 || (vdist(this.origin - targ_origin, >, this.target_range))
572 || ((trace_fraction < 1) && (trace_ent != this.enemy)))
575 this.pass_distance = 0;
580 /*WarpZone_TrailParticles(world, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
581 print("Trace origin: ", vtos(targ_origin), "\n");
582 print("Target origin: ", vtos(this.enemy.origin), "\n");
583 print("My origin: ", vtos(this.origin), "\n"); */
585 this.monster_movestate = MONSTER_MOVE_ENEMY;
586 this.last_trace = time + 1.2;
587 if(this.monster_moveto)
588 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
593 /*makevectors(this.angles);
594 this.monster_movestate = MONSTER_MOVE_ENEMY;
595 this.last_trace = time + 1.2;
596 return this.enemy.origin; */
599 switch(this.monster_moveflags)
601 case MONSTER_MOVE_FOLLOW:
603 this.monster_movestate = MONSTER_MOVE_FOLLOW;
604 this.last_trace = time + 0.3;
605 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
607 case MONSTER_MOVE_SPAWNLOC:
609 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
610 this.last_trace = time + 2;
613 case MONSTER_MOVE_NOMOVE:
615 if(this.monster_moveto)
617 this.last_trace = time + 0.5;
618 return this.monster_moveto;
622 this.monster_movestate = MONSTER_MOVE_NOMOVE;
623 this.last_trace = time + 2;
628 case MONSTER_MOVE_WANDER:
631 this.monster_movestate = MONSTER_MOVE_WANDER;
633 if(this.monster_moveto)
635 this.last_trace = time + 0.5;
636 pos = this.monster_moveto;
640 this.last_trace = time + 0.5;
645 this.last_trace = time + this.wander_delay;
647 this.angles_y = rint(random() * 500);
648 makevectors(this.angles);
649 pos = this.origin + v_forward * this.wander_distance;
651 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
653 pos.z = random() * 200;
664 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
666 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
667 float initial_height = 0; //min(50, (targ_distance * tanh(20)));
668 float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
669 //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
672 if(current_height) // make sure we can actually do this arcing path
674 targpos = (to + ('0 0 1' * current_height));
675 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
676 if(trace_fraction < 1)
678 //print("normal arc line failed, trying to find new pos...");
679 WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
680 targpos = (trace_endpos + '0 0 -10');
681 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
682 if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
683 /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
686 else { targpos = to; }
688 //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
690 vector desired_direction = normalize(targpos - from);
691 if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
692 else { this.velocity = (desired_direction * movespeed); }
694 //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
695 //this.angles = vectoangles(this.velocity);
698 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
700 if(this.target2) { this.goalentity = find(world, targetname, this.target2); }
704 if(STAT(FROZEN, this) == 2)
706 this.revive_progress = bound(0, this.revive_progress + this.ticrate * this.revive_speed, 1);
707 this.health = max(1, this.revive_progress * this.max_health);
708 this.iceblock.alpha = bound(0.2, 1 - this.revive_progress, 1);
710 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
711 WaypointSprite_UpdateHealth(this.sprite, this.health);
713 movelib_brake_simple(this, stpspeed);
714 setanim(this, this.anim_idle, true, false, false);
717 this.nextthink = time + this.ticrate;
719 if(this.revive_progress >= 1)
724 else if(STAT(FROZEN, this) == 3)
726 this.revive_progress = bound(0, this.revive_progress - this.ticrate * this.revive_speed, 1);
727 this.health = max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * this.revive_progress );
729 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
730 WaypointSprite_UpdateHealth(this.sprite, this.health);
732 movelib_brake_simple(this, stpspeed);
733 setanim(this, this.anim_idle, true, false, false);
736 this.nextthink = time + this.ticrate;
742 if(this.event_damage)
743 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, this.origin, '0 0 0');
746 else if ( this.revive_progress <= 0 )
752 if(this.flags & FL_SWIM)
754 if(this.waterlevel < WATERLEVEL_WETFEET)
756 if(time >= this.last_trace)
758 this.last_trace = time + 0.4;
760 Damage (this, world, world, 2, DEATH_DROWN.m_id, this.origin, '0 0 0');
761 this.angles = '90 90 0';
764 this.velocity_y += random() * 50;
765 this.velocity_x -= random() * 50;
769 this.velocity_y -= random() * 50;
770 this.velocity_x += random() * 50;
772 this.velocity_z += random() * 150;
776 this.movetype = MOVETYPE_BOUNCE;
777 //this.velocity_z = -200;
781 else if(this.movetype == MOVETYPE_BOUNCE)
784 this.movetype = MOVETYPE_WALK;
788 targ = this.goalentity;
790 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
792 || this.draggedby != world
793 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
794 || time < game_starttime
795 || (autocvar_g_campaign && !campaign_bots_may_start)
796 || time < this.spawn_time)
798 runspeed = walkspeed = 0;
799 if(time >= this.spawn_time)
800 setanim(this, this.anim_idle, true, false, false);
801 movelib_brake_simple(this, stpspeed);
805 targ = monster_target;
806 runspeed = bound(0, monster_speed_run * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
807 walkspeed = bound(0, monster_speed_walk * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
810 if(autocvar_g_monsters_teams)
811 if(DIFF_TEAM(this.monster_follow, this))
812 this.monster_follow = world;
814 if(time >= this.last_enemycheck)
818 this.enemy = Monster_FindTarget(this);
821 WarpZone_RefSys_Copy(this.enemy, this);
822 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
823 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
824 this.monster_moveto = '0 0 0';
825 this.monster_face = '0 0 0';
827 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)));
828 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
832 this.last_enemycheck = time + 1; // check for enemies every second
835 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
838 this.touch = Monster_Touch;
841 if(this.state && time >= this.attack_finished_single[0])
842 this.state = 0; // attack is over
844 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
845 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
846 this.moveto = Monster_Move_Target(this, targ);
849 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
851 if(this.state == MONSTER_ATTACK_MELEE)
852 this.moveto = this.origin;
854 if(this.enemy && this.enemy.vehicle)
857 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
858 this.moveto_z = this.origin_z;
860 if(vdist(this.origin - this.moveto, >, 100))
862 float do_run = (this.enemy || this.monster_moveto);
863 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
864 Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
866 if(time > this.pain_finished) // TODO: use anim_finished instead!
868 if(time > this.anim_finished)
869 if(vdist(this.velocity, >, 10))
870 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
872 setanim(this, this.anim_idle, true, false, false);
876 entity e = find(world, targetname, this.target2);
878 this.target2 = e.target2;
880 this.target2 = e.target;
882 movelib_brake_simple(this, stpspeed);
883 if(time > this.anim_finished)
884 if(time > this.pain_finished)
886 if(vdist(this.velocity, <=, 30))
887 setanim(this, this.anim_idle, true, false, false);
890 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
892 vector real_angle = vectoangles(this.steerto) - this.angles;
894 if(this.state == MONSTER_ATTACK_MELEE)
898 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
899 this.angles_y += turny;
902 Monster_Attack_Check(this, this.enemy);
905 void Monster_Remove(entity this)
908 return; // don't remove it?
910 .entity weaponentity = weaponentities[0];
911 if(!this) { return; }
913 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
914 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
916 if(this.(weaponentity)) { remove(this.(weaponentity)); }
917 if(this.iceblock) { remove(this.iceblock); }
918 WaypointSprite_Kill(this.sprite);
922 void Monster_Dead_Think()
924 this.nextthink = time + this.ticrate;
926 if(this.monster_lifetime != 0)
927 if(time >= this.monster_lifetime)
929 Monster_Dead_Fade(this);
934 void Monster_Appear()
936 this.enemy = activator;
937 this.spawnflags &= ~MONSTERFLAG_APPEAR; // otherwise, we get an endless loop
938 Monster_Spawn(this, this.monsterid);
941 bool Monster_Appear_Check(entity this, int monster_id)
943 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
946 this.think = func_null;
947 this.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
949 this.use = Monster_Appear;
950 this.flags = FL_MONSTER; // set so this monster can get butchered
955 void Monster_Reset(entity this)
957 setorigin(this, this.pos1);
958 this.angles = this.pos2;
960 Unfreeze(this); // remove any icy remains
962 this.health = this.max_health;
963 this.velocity = '0 0 0';
965 this.goalentity = world;
966 this.attack_finished_single[0] = 0;
967 this.moveto = this.origin;
970 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
972 this.health -= damage;
974 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
976 if(this.health <= -50) // 100 health until gone?
978 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
980 // number of monsters spawned with mobspawn command
983 this.think = SUB_Remove_self;
984 this.nextthink = time + 0.1;
985 this.event_damage = func_null;
989 void Monster_Dead(entity this, entity attacker, float gibbed)
991 this.think = Monster_Dead_Think;
992 this.nextthink = time;
993 this.monster_lifetime = time + 5;
995 if(STAT(FROZEN, this))
997 Unfreeze(this); // remove any icy remains
998 this.health = 0; // reset by Unfreeze
1001 monster_dropitem(this);
1003 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
1005 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
1006 monsters_killed += 1;
1008 if(IS_PLAYER(attacker))
1009 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
1010 PlayerScore_Add(attacker, SP_SCORE, +autocvar_g_monsters_score_kill);
1014 // number of monsters spawned with mobspawn command
1018 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
1019 this.solid = SOLID_CORPSE;
1020 this.takedamage = DAMAGE_AIM;
1021 this.deadflag = DEAD_DEAD;
1023 this.movetype = MOVETYPE_TOSS;
1024 this.moveto = this.origin;
1025 this.touch = Monster_Touch; // reset incase monster was pouncing
1026 this.reset = func_null;
1028 this.attack_finished_single[0] = 0;
1031 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
1032 this.velocity = '0 0 0';
1034 CSQCModel_UnlinkEntity(this);
1036 Monster mon = get_monsterinfo(this.monsterid);
1037 mon.mr_death(mon, this);
1039 if(this.candrop && this.weapon)
1040 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325');
1043 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
1045 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
1048 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
1051 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
1054 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1057 if(deathtype == DEATH_FALL.m_id && this.draggedby != world)
1063 v = healtharmor_applydamage(100, this.armorvalue / 100, deathtype, damage);
1068 frag_attacker = attacker;
1069 frag_deathtype = deathtype;
1070 Monster mon = get_monsterinfo(this.monsterid);
1071 mon.mr_pain(mon, this);
1076 this.health -= take;
1077 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1081 WaypointSprite_UpdateHealth(this.sprite, this.health);
1083 this.dmg_time = time;
1085 if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN.m_id)
1086 spamsound (this, CH_PAIN, SND(BODYIMPACT1), VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1088 this.velocity += force * this.damageforcescale;
1090 if(deathtype != DEATH_DROWN.m_id && take)
1092 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1094 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1096 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1099 if(this.health <= 0)
1101 if(deathtype == DEATH_KILL.m_id)
1102 this.candrop = false; // killed by mobkill command
1105 activator = attacker;
1107 WITHSELF(this, SUB_UseTargets());
1108 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1110 Monster_Dead(this, attacker, (this.health <= -100 || deathtype == DEATH_KILL.m_id));
1112 WaypointSprite_Kill(this.sprite);
1114 MUTATOR_CALLHOOK(MonsterDies, this, attacker);
1116 if(this.health <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1118 Violence_GibSplash(this, 1, 0.5, attacker);
1120 this.think = SUB_Remove_self;
1121 this.nextthink = time + 0.1;
1126 // don't check for enemies, just keep walking in a straight line
1127 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1129 if(gameover || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || this.draggedby != world || time < game_starttime || (autocvar_g_campaign && !campaign_bots_may_start) || time < this.spawn_time)
1132 if(time >= this.spawn_time)
1133 setanim(this, this.anim_idle, true, false, false);
1134 movelib_brake_simple(this, 0.6);
1138 float reverse = false;
1141 makevectors(this.angles);
1142 a = this.origin + '0 0 16';
1143 b = this.origin + '0 0 16' + v_forward * 32;
1145 traceline(a, b, MOVE_NORMAL, this);
1147 if(trace_fraction != 1.0)
1152 if(IS_PLAYER(trace_ent) && !(trace_ent.items & IT_STRENGTH))
1156 // TODO: fix this... tracing is broken if the floor is thin
1161 traceline(b, a, MOVE_WORLDONLY, this);
1162 if(trace_fraction == 1.0)
1168 this.angles_y = anglemods(this.angles_y - 180);
1169 makevectors(this.angles);
1172 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1174 if(time > this.pain_finished)
1175 if(time > this.attack_finished_single[0])
1176 if(vdist(this.velocity, >, 10))
1177 setanim(this, this.anim_walk, true, false, false);
1179 setanim(this, this.anim_idle, true, false, false);
1182 void Monster_Anim(entity this)
1184 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1189 // Decide on which death animation to use.
1191 deadbits = ANIMSTATE_DEAD1;
1193 deadbits = ANIMSTATE_DEAD2;
1198 // Clear a previous death animation.
1201 int animbits = deadbits;
1202 if(STAT(FROZEN, this))
1203 animbits |= ANIMSTATE_FROZEN;
1205 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1206 animdecide_setstate(this, animbits, false);
1207 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1209 /* // weapon entities for monsters?
1210 if (this.weaponentity)
1212 updateanim(this.weaponentity);
1213 if (!this.weaponentity.animstate_override)
1214 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1219 void Monster_Think()
1221 this.think = Monster_Think;
1222 this.nextthink = this.ticrate;
1224 if(this.monster_lifetime)
1225 if(time >= this.monster_lifetime)
1227 Damage(this, this, this, this.health + this.max_health, DEATH_KILL.m_id, this.origin, this.origin);
1231 Monster mon = get_monsterinfo(this.monsterid);
1232 if(mon.mr_think(mon, this))
1233 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1237 CSQCMODEL_AUTOUPDATE(this);
1240 bool Monster_Spawn_Setup(entity this)
1242 Monster mon = Monsters_from(this.monsterid);
1243 mon.mr_setup(mon, this);
1245 // ensure some basic needs are met
1246 if(!this.health) { this.health = 100; }
1247 if(!this.armorvalue) { this.armorvalue = bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9); }
1248 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1249 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1250 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1251 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1252 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1254 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1256 Monster_Miniboss_Check(this);
1257 this.health *= MONSTER_SKILLMOD(this);
1260 this.skin = rint(random() * 4);
1263 this.max_health = this.health;
1264 this.pain_finished = this.nextthink;
1266 if(IS_PLAYER(this.monster_follow))
1267 this.effects |= EF_DIMLIGHT;
1269 if(!this.wander_delay) { this.wander_delay = 2; }
1270 if(!this.wander_distance) { this.wander_distance = 600; }
1272 Monster_Sounds_Precache(this);
1273 Monster_Sounds_Update(this);
1276 this.monster_attack = true; // we can have monster enemies in team games
1278 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1280 if(autocvar_g_monsters_healthbars)
1282 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), world, this.team, this, sprite, true, RADARICON_DANGER);
1283 wp.wp_extra = this.monsterid;
1284 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1285 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1287 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1288 WaypointSprite_UpdateHealth(this.sprite, this.health);
1292 this.think = Monster_Think;
1293 this.nextthink = time + this.ticrate;
1295 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1301 bool Monster_Spawn(entity this, int mon_id)
1303 // setup the basic required properties for a monster
1304 entity mon = Monsters_from(mon_id);
1305 if(!mon.monsterid) { return false; } // invalid monster
1307 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1309 if(Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed
1311 if(!this.monster_skill)
1312 this.monster_skill = cvar("g_monsters_skill");
1314 // support for quake style removing monsters based on skill
1315 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1316 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1317 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1319 if(this.team && !teamplay)
1322 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1323 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1324 monsters_total += 1;
1326 setmodel(this, mon.m_model);
1327 this.flags = FL_MONSTER;
1328 this.classname = "monster";
1329 this.takedamage = DAMAGE_AIM;
1330 this.bot_attack = true;
1331 this.iscreature = true;
1332 this.teleportable = true;
1333 this.damagedbycontents = true;
1334 this.monsterid = mon_id;
1335 this.event_damage = Monster_Damage;
1336 this.touch = Monster_Touch;
1337 this.use = Monster_Use;
1338 this.solid = SOLID_BBOX;
1339 this.movetype = MOVETYPE_WALK;
1340 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1342 this.velocity = '0 0 0';
1343 this.moveto = this.origin;
1344 this.pos1 = this.origin;
1345 this.pos2 = this.angles;
1346 this.reset = Monster_Reset;
1347 this.netname = mon.netname;
1348 this.monster_attackfunc = mon.monster_attackfunc;
1349 this.monster_name = mon.monster_name;
1350 this.candrop = true;
1351 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1352 this.oldtarget2 = this.target2;
1353 this.pass_distance = 0;
1354 this.deadflag = DEAD_NO;
1355 this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM));
1356 this.spawn_time = time;
1358 this.monster_moveto = '0 0 0';
1359 this.monster_face = '0 0 0';
1360 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1362 if(!this.scale) { this.scale = 1; }
1363 if(autocvar_g_monsters_edit) { this.grab = 1; }
1364 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1365 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1366 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1368 if(autocvar_g_playerclip_collisions)
1369 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1371 if(mon.spawnflags & MONSTER_TYPE_FLY)
1373 this.flags |= FL_FLY;
1374 this.movetype = MOVETYPE_FLY;
1377 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1379 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1382 if(mon.spawnflags & MONSTER_SIZE_QUAKE)
1383 if(autocvar_g_monsters_quake_resize)
1387 setsize(this, mon.mins * this.scale, mon.maxs * this.scale);
1389 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1391 Monster_UpdateModel(this);
1393 if(!Monster_Spawn_Setup(this))
1395 Monster_Remove(this);
1401 setorigin(this, this.origin + '0 0 20');
1402 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1403 setorigin(this, trace_endpos);
1406 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1407 monster_setupcolors(this);
1409 CSQCMODEL_AUTOINIT(this);