1 #include "sv_monsters.qh"
3 #include <server/g_subs.qh>
4 #include <lib/warpzone/common.qh>
5 #include "../constants.qh"
9 #include "../physics/movelib.qh"
10 #include "../weapons/_mod.qh"
11 #include <server/autocvars.qh>
12 #include <server/defs.qh>
13 #include "../deathtypes/all.qh"
14 #include <server/mutators/_mod.qh>
15 #include <server/steerlib.qh>
16 #include "../turrets/sv_turrets.qh"
17 #include "../turrets/util.qh"
18 #include "../vehicles/all.qh"
19 #include <server/campaign.qh>
20 #include <server/command/_mod.qh>
21 #include "../triggers/triggers.qh"
22 #include <lib/csqcmodel/sv_model.qh>
23 #include <server/round_handler.qh>
24 #include <server/weapons/_mod.qh>
26 void monsters_setstatus(entity this)
28 STAT(MONSTERS_TOTAL, this) = monsters_total;
29 STAT(MONSTERS_KILLED, this) = monsters_killed;
32 void monster_dropitem(entity this, entity attacker)
34 if(!this.candrop || !this.monster_loot)
37 vector org = CENTER_OR_VIEWOFS(this);
39 Item_SetLoot(e, true);
40 e.spawnfunc_checked = true;
42 e.monster_loot = this.monster_loot;
44 MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
45 e = M_ARGV(1, entity);
47 if(e && e.monster_loot)
50 StartItem(e, e.monster_loot);
53 e.velocity = randomvec() * 175 + '0 0 325';
54 e.item_spawnshieldtime = time + 0.7;
55 SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
59 void monster_makevectors(entity this, entity targ)
63 vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
64 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
65 this.v_angle_x = -this.v_angle_x;
68 makevectors(this.v_angle);
75 bool Monster_ValidTarget(entity this, entity targ)
77 // ensure we're not checking nonexistent monster/target
78 if(!this || !targ) { return false; }
81 || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
82 || (IS_VEHICLE(targ) && !((Monsters_from(this.monsterid)).spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
83 || (time < game_starttime) // monsters do nothing before match has started
84 || (targ.takedamage == DAMAGE_NO)
86 || (targ.items & IT_INVISIBILITY)
87 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
88 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || targ.health <= 0 || this.health <= 0))
89 || (this.monster_follow == targ || targ.monster_follow == this)
90 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
91 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
92 || (SAME_TEAM(targ, this))
93 || (STAT(FROZEN, targ))
94 || (targ.alpha != 0 && targ.alpha < 0.5)
95 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
98 // if any of the above checks fail, target is not valid
102 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
103 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this);
105 if(trace_fraction < 1 && trace_ent != targ)
106 return false; // solid
108 if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
109 if(this.enemy != targ)
111 makevectors (this.angles);
112 float dot = normalize (targ.origin - this.origin) * v_forward;
114 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
117 return true; // this target is valid!
120 entity Monster_FindTarget(entity this)
122 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
124 entity closest_target = NULL;
125 vector my_center = CENTER_OR_VIEWOFS(this);
127 // find the closest acceptable target to pass to
128 IL_EACH(g_monster_targets, it.monster_attack && vdist(it.origin - this.origin, <, this.target_range),
130 if(Monster_ValidTarget(this, it))
132 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
133 vector targ_center = CENTER_OR_VIEWOFS(it);
137 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
138 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
139 { closest_target = it; }
141 else { closest_target = it; }
145 return closest_target;
148 void monster_setupcolors(entity this)
150 if(IS_PLAYER(this.realowner))
151 this.colormap = this.realowner.colormap;
152 else if(teamplay && this.team)
153 this.colormap = 1024 + (this.team - 1) * 17;
156 if(this.monster_skill <= MONSTER_SKILL_EASY)
157 this.colormap = 1029;
158 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
159 this.colormap = 1027;
160 else if(this.monster_skill <= MONSTER_SKILL_HARD)
161 this.colormap = 1038;
162 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
163 this.colormap = 1028;
164 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
165 this.colormap = 1032;
167 this.colormap = 1024;
171 void monster_changeteam(entity this, int newteam)
173 if(!teamplay) { return; }
176 if(!this.monster_attack)
177 IL_PUSH(g_monster_targets, this);
178 this.monster_attack = true; // new team, activate attacking
179 monster_setupcolors(this);
183 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
185 this.sprite.team = newteam;
186 this.sprite.SendFlags |= 1;
190 .void(entity) monster_delayedfunc;
191 void Monster_Delay_Action(entity this)
193 if(Monster_ValidTarget(this.owner, this.owner.enemy)) { this.monster_delayedfunc(this.owner); }
198 setthink(this, Monster_Delay_Action);
199 this.nextthink = time + this.count;
203 setthink(this, SUB_Remove);
204 this.nextthink = time;
208 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
210 // deferred attacking, checks if monster is still alive and target is still valid before attacking
213 setthink(e, Monster_Delay_Action);
214 e.nextthink = time + defer_amnt;
215 e.count = defer_amnt;
217 e.monster_delayedfunc = func;
218 e.cnt = repeat_count;
226 string get_monster_model_datafilename(string m, float sk, string fil)
231 m = "models/monsters/*_";
233 m = strcat(m, ftos(sk));
236 return strcat(m, ".", fil);
239 void Monster_Sound_Precache(string f)
243 fh = fopen(f, FILE_READ);
246 while((s = fgets(fh)))
248 if(tokenize_console(s) != 3)
250 //LOG_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
253 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
258 void Monster_Sounds_Precache(entity this)
260 string m = (Monsters_from(this.monsterid)).m_model.model_str();
261 float globhandle, n, i;
264 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
267 n = search_getsize(globhandle);
268 for (i = 0; i < n; ++i)
270 //print(search_getfilename(globhandle, i), "\n");
271 f = search_getfilename(globhandle, i);
272 Monster_Sound_Precache(f);
274 search_end(globhandle);
277 void Monster_Sounds_Clear(entity this)
279 #define _MSOUND(m) if(this.monstersound_##m) { strunzone(this.monstersound_##m); this.monstersound_##m = string_null; }
284 .string Monster_Sound_SampleField(string type)
286 GetMonsterSoundSampleField_notFound = 0;
289 #define _MSOUND(m) case #m: return monstersound_##m;
293 GetMonsterSoundSampleField_notFound = 1;
297 bool Monster_Sounds_Load(entity this, string f, int first)
301 float fh = fopen(f, FILE_READ);
304 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
307 while((s = fgets(fh)))
309 if(tokenize_console(s) != 3)
311 field = Monster_Sound_SampleField(argv(0));
312 if(GetMonsterSoundSampleField_notFound)
315 strunzone(this.(field));
316 this.(field) = strzone(strcat(argv(1), " ", argv(2)));
322 .int skin_for_monstersound;
323 void Monster_Sounds_Update(entity this)
325 if(this.skin == this.skin_for_monstersound) { return; }
327 this.skin_for_monstersound = this.skin;
328 Monster_Sounds_Clear(this);
329 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
330 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
333 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
335 if(!autocvar_g_monsters_sounds) { return; }
338 if(time < this.msound_delay)
340 string sample = this.(samplefield);
341 if (sample != "") sample = GlobalSound_sample(sample, random());
342 float myscale = ((this.scale) ? this.scale : 1); // safety net
343 // TODO: change volume depending on size too?
344 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
346 this.msound_delay = time + sound_delay;
350 // =======================
351 // Monster attack handlers
352 // =======================
354 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
356 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
358 setanim(this, anim, false, true, false);
360 if(this.animstate_endtime > time && IS_MONSTER(this))
361 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
363 this.attack_finished_single[0] = this.anim_finished = time + animtime;
365 monster_makevectors(this, targ);
367 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
369 if(trace_ent.takedamage)
370 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
375 bool Monster_Attack_Leap_Check(entity this, vector vel)
377 if(this.state && IS_MONSTER(this))
378 return false; // already attacking
379 if(!IS_ONGROUND(this))
380 return false; // not on the ground
381 if(this.health <= 0 || IS_DEAD(this))
382 return false; // called when dead?
383 if(time < this.attack_finished_single[0])
384 return false; // still attacking
386 vector old = this.velocity;
389 tracetoss(this, this);
391 if(trace_ent != this.enemy)
397 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
399 if(!Monster_Attack_Leap_Check(this, vel))
402 setanim(this, anm, false, true, false);
404 if(this.animstate_endtime > time && IS_MONSTER(this))
405 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
407 this.attack_finished_single[0] = this.anim_finished = time + animtime;
410 this.state = MONSTER_ATTACK_RANGED;
411 settouch(this, touchfunc);
414 UNSET_ONGROUND(this);
419 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
421 int slot = weaponslot(weaponentity);
424 || (!this.monster_attackfunc)
425 || (time < this.attack_finished_single[slot])
428 if(vdist(targ.origin - this.origin, <=, this.attack_range))
430 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
431 if(attack_success == 1)
432 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
433 else if(attack_success > 0)
437 if(vdist(targ.origin - this.origin, >, this.attack_range))
439 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
440 if(attack_success == 1)
441 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
442 else if(attack_success > 0)
448 // ======================
449 // Main monster functions
450 // ======================
452 void Monster_UpdateModel(entity this)
454 // assume some defaults
455 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
456 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
457 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
458 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
459 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
460 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
461 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
462 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
463 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
464 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
466 // then get the real values
467 Monster mon = Monsters_from(this.monsterid);
468 mon.mr_anim(mon, this);
471 void Monster_Touch(entity this, entity toucher)
473 if(toucher == NULL) { return; }
475 if(toucher.monster_attack)
476 if(this.enemy != toucher)
477 if(!IS_MONSTER(toucher))
478 if(Monster_ValidTarget(this, toucher))
479 this.enemy = toucher;
482 void Monster_Miniboss_Check(entity this)
484 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
487 float chance = random() * 100;
489 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
490 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
492 this.health += autocvar_g_monsters_miniboss_healthboost;
493 this.effects |= EF_RED;
495 this.weapon = WEP_VORTEX.m_id;
499 bool Monster_Respawn_Check(entity this)
501 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
502 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
503 return true; // enabled by a mutator
505 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
508 if(!autocvar_g_monsters_respawn)
514 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterid); }
518 void Monster_Dead_Fade(entity this)
520 if(Monster_Respawn_Check(this))
522 this.spawnflags |= MONSTERFLAG_RESPAWNED;
523 setthink(this, Monster_Respawn);
524 this.nextthink = time + this.respawntime;
525 this.monster_lifetime = 0;
526 this.deadflag = DEAD_RESPAWNING;
527 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
529 this.pos1 = this.origin;
530 this.pos2 = this.angles;
532 this.event_damage = func_null;
533 this.takedamage = DAMAGE_NO;
534 setorigin(this, this.pos1);
535 this.angles = this.pos2;
536 this.health = this.max_health;
537 setmodel(this, MDL_Null);
541 // number of monsters spawned with mobspawn command
544 SUB_SetFade(this, time + 3, 1);
548 void Monster_Use(entity this, entity actor, entity trigger)
550 if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
553 .float pass_distance;
554 vector Monster_Move_Target(entity this, entity targ)
556 // enemy is always preferred target
559 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
560 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
561 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
563 // cases where the enemy may have changed their state (don't need to check everything here)
565 || (IS_DEAD(this.enemy) || this.enemy.health < 1)
566 || (STAT(FROZEN, this.enemy))
567 || (this.enemy.flags & FL_NOTARGET)
568 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
569 || (this.enemy.takedamage == DAMAGE_NO)
570 || (vdist(this.origin - targ_origin, >, this.target_range))
571 || ((trace_fraction < 1) && (trace_ent != this.enemy)))
574 //this.pass_distance = 0;
579 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
580 print("Trace origin: ", vtos(targ_origin), "\n");
581 print("Target origin: ", vtos(this.enemy.origin), "\n");
582 print("My origin: ", vtos(this.origin), "\n"); */
584 this.monster_movestate = MONSTER_MOVE_ENEMY;
585 this.last_trace = time + 1.2;
586 if(this.monster_moveto)
587 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
592 /*makevectors(this.angles);
593 this.monster_movestate = MONSTER_MOVE_ENEMY;
594 this.last_trace = time + 1.2;
595 return this.enemy.origin; */
598 switch(this.monster_moveflags)
600 case MONSTER_MOVE_FOLLOW:
602 this.monster_movestate = MONSTER_MOVE_FOLLOW;
603 this.last_trace = time + 0.3;
604 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
606 case MONSTER_MOVE_SPAWNLOC:
608 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
609 this.last_trace = time + 2;
612 case MONSTER_MOVE_NOMOVE:
614 if(this.monster_moveto)
616 this.last_trace = time + 0.5;
617 return this.monster_moveto;
621 this.monster_movestate = MONSTER_MOVE_NOMOVE;
622 this.last_trace = time + 2;
627 case MONSTER_MOVE_WANDER:
630 this.monster_movestate = MONSTER_MOVE_WANDER;
632 if(this.monster_moveto)
634 this.last_trace = time + 0.5;
635 pos = this.monster_moveto;
639 this.last_trace = time + 0.5;
644 this.last_trace = time + this.wander_delay;
646 this.angles_y = rint(random() * 500);
647 makevectors(this.angles);
648 pos = this.origin + v_forward * this.wander_distance;
650 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
652 pos.z = random() * 200;
663 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
665 //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
666 //float initial_height = 0; //min(50, (targ_distance * tanh(20)));
667 //float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
668 //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; }
689 //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
691 vector desired_direction = normalize(targpos - from);
692 if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
693 else { this.velocity = (desired_direction * movespeed); }
695 //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
696 //this.angles = vectoangles(this.velocity);
702 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
704 // update goal entity if lost
705 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
706 this.goalentity = find(NULL, targetname, this.target2);
708 if(STAT(FROZEN, this))
710 movelib_brake_simple(this, stpspeed);
711 setanim(this, this.anim_idle, true, false, false);
712 return; // no physics while frozen!
715 if(this.flags & FL_SWIM)
717 if(this.waterlevel < WATERLEVEL_WETFEET)
719 if(time >= this.last_trace)
721 this.last_trace = time + 0.4;
723 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
724 this.angles = '90 90 0';
727 this.velocity_y += random() * 50;
728 this.velocity_x -= random() * 50;
732 this.velocity_y -= random() * 50;
733 this.velocity_x += random() * 50;
735 this.velocity_z += random() * 150;
739 set_movetype(this, MOVETYPE_BOUNCE);
740 //this.velocity_z = -200;
744 else if(this.move_movetype == MOVETYPE_BOUNCE)
747 set_movetype(this, MOVETYPE_WALK);
751 entity targ = this.goalentity;
753 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
755 || this.draggedby != NULL
756 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
757 || time < game_starttime
758 || (autocvar_g_campaign && !campaign_bots_may_start)
759 || time < this.spawn_time)
761 runspeed = walkspeed = 0;
762 if(time >= this.spawn_time)
763 setanim(this, this.anim_idle, true, false, false);
764 movelib_brake_simple(this, stpspeed);
768 targ = M_ARGV(3, entity);
769 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
770 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
772 if(teamplay && autocvar_g_monsters_teams)
773 if(DIFF_TEAM(this.monster_follow, this))
774 this.monster_follow = NULL;
776 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
779 settouch(this, Monster_Touch);
782 if(this.state && time >= this.attack_finished_single[0])
783 this.state = 0; // attack is over
785 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
786 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
787 this.moveto = Monster_Move_Target(this, targ);
790 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
792 if(this.state == MONSTER_ATTACK_MELEE)
793 this.moveto = this.origin;
795 if(this.enemy && this.enemy.vehicle)
798 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
799 this.moveto_z = this.origin_z;
801 if(vdist(this.origin - this.moveto, >, 100))
803 bool do_run = (this.enemy || this.monster_moveto);
804 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
805 Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
807 if(time > this.pain_finished && time > this.anim_finished) // TODO: use anim_finished instead!?
809 if(vdist(this.velocity, >, 10))
810 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
812 setanim(this, this.anim_idle, true, false, false);
816 entity e = this.goalentity; //find(NULL, targetname, this.target2);
817 if(e.target2 && e.target2 != "")
818 this.target2 = e.target2;
819 else if(e.target && e.target != "") // compatibility
820 this.target2 = e.target;
822 movelib_brake_simple(this, stpspeed);
823 if(time > this.anim_finished && time > this.pain_finished)
825 if(vdist(this.velocity, <=, 30))
826 setanim(this, this.anim_idle, true, false, false);
829 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
831 vector real_angle = vectoangles(this.steerto) - this.angles;
833 if(this.state == MONSTER_ATTACK_MELEE)
837 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
838 this.angles_y += turny;
842 void Monster_Remove(entity this)
845 return; // don't remove it?
847 if(!this) { return; }
849 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
850 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
852 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
854 .entity weaponentity = weaponentities[slot];
855 if(this.(weaponentity))
856 delete(this.(weaponentity));
858 if(this.iceblock) { delete(this.iceblock); }
859 WaypointSprite_Kill(this.sprite);
863 void Monster_Dead_Think(entity this)
865 this.nextthink = time + this.ticrate;
867 if(this.monster_lifetime != 0)
868 if(time >= this.monster_lifetime)
870 Monster_Dead_Fade(this);
875 void Monster_Appear(entity this, entity actor, entity trigger)
878 Monster_Spawn(this, false, this.monsterid);
881 bool Monster_Appear_Check(entity this, int monster_id)
883 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
886 setthink(this, func_null);
887 this.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
889 this.use = Monster_Appear;
890 this.flags = FL_MONSTER; // set so this monster can get butchered
895 void Monster_Reset(entity this)
897 setorigin(this, this.pos1);
898 this.angles = this.pos2;
900 Unfreeze(this); // remove any icy remains
902 this.health = this.max_health;
903 this.velocity = '0 0 0';
905 this.goalentity = NULL;
906 this.attack_finished_single[0] = 0;
907 this.moveto = this.origin;
910 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
912 this.health -= damage;
914 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
916 if(this.health <= -50) // 100 health until gone?
918 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
920 // number of monsters spawned with mobspawn command
923 setthink(this, SUB_Remove);
924 this.nextthink = time + 0.1;
925 this.event_damage = func_null;
929 void Monster_Dead(entity this, entity attacker, float gibbed)
931 setthink(this, Monster_Dead_Think);
932 this.nextthink = time;
933 this.monster_lifetime = time + 5;
935 if(STAT(FROZEN, this))
937 Unfreeze(this); // remove any icy remains
938 this.health = 0; // reset by Unfreeze
941 monster_dropitem(this, attacker);
943 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
945 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
946 monsters_killed += 1;
948 if(IS_PLAYER(attacker))
949 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
950 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
954 // number of monsters spawned with mobspawn command
958 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
959 _setmodel(this, this.mdl_dead);
961 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
962 this.solid = SOLID_CORPSE;
963 this.takedamage = DAMAGE_AIM;
964 this.deadflag = DEAD_DEAD;
966 set_movetype(this, MOVETYPE_TOSS);
967 this.moveto = this.origin;
968 settouch(this, Monster_Touch); // reset incase monster was pouncing
969 this.reset = func_null;
971 this.attack_finished_single[0] = 0;
974 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
975 this.velocity = '0 0 0';
977 CSQCModel_UnlinkEntity(this);
979 Monster mon = Monsters_from(this.monsterid);
980 mon.mr_death(mon, this);
982 if(this.candrop && this.weapon)
984 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
985 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
989 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
991 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
994 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
997 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
1000 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1003 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
1006 vector v = healtharmor_applydamage(100, this.armorvalue / 100, deathtype, damage);
1010 Monster mon = Monsters_from(this.monsterid);
1011 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1015 this.health -= take;
1016 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1020 WaypointSprite_UpdateHealth(this.sprite, this.health);
1022 this.dmg_time = time;
1024 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1025 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1027 this.velocity += force * this.damageforcescale;
1029 if(deathtype != DEATH_DROWN.m_id && take)
1031 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1033 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1035 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1038 if(this.health <= 0)
1040 if(deathtype == DEATH_KILL.m_id)
1041 this.candrop = false; // killed by mobkill command
1044 SUB_UseTargets(this, attacker, this.enemy);
1045 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1047 Monster_Dead(this, attacker, (this.health <= -100 || deathtype == DEATH_KILL.m_id));
1049 WaypointSprite_Kill(this.sprite);
1051 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1053 if(this.health <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1055 Violence_GibSplash(this, 1, 0.5, attacker);
1057 setthink(this, SUB_Remove);
1058 this.nextthink = time + 0.1;
1063 // don't check for enemies, just keep walking in a straight line
1064 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1066 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)
1069 if(time >= this.spawn_time)
1070 setanim(this, this.anim_idle, true, false, false);
1071 movelib_brake_simple(this, 0.6);
1075 makevectors(this.angles);
1076 vector a = CENTER_OR_VIEWOFS(this);
1077 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1079 traceline(a, b, MOVE_NORMAL, this);
1081 bool reverse = false;
1082 if(trace_fraction != 1.0)
1084 if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1086 if(trace_ent && IS_MONSTER(trace_ent))
1089 // TODO: fix this... tracing is broken if the floor is thin
1094 traceline(b, a, MOVE_WORLDONLY, this);
1095 if(trace_fraction == 1.0)
1101 this.angles_y = anglemods(this.angles_y - 180);
1102 makevectors(this.angles);
1105 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1107 if(time > this.pain_finished && time > this.attack_finished_single[0])
1108 if(vdist(this.velocity, >, 10))
1109 setanim(this, this.anim_walk, true, false, false);
1111 setanim(this, this.anim_idle, true, false, false);
1114 void Monster_Anim(entity this)
1116 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1121 // Decide on which death animation to use.
1123 deadbits = ANIMSTATE_DEAD1;
1125 deadbits = ANIMSTATE_DEAD2;
1130 // Clear a previous death animation.
1133 int animbits = deadbits;
1134 if(STAT(FROZEN, this))
1135 animbits |= ANIMSTATE_FROZEN;
1137 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1138 animdecide_setstate(this, animbits, false);
1139 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1141 /* // weapon entities for monsters?
1142 if (this.weaponentity)
1144 updateanim(this.weaponentity);
1145 if (!this.weaponentity.animstate_override)
1146 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1151 void Monster_Frozen_Think(entity this)
1153 if(STAT(FROZEN, this) == 2)
1155 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1156 this.health = max(1, STAT(REVIVE_PROGRESS, this) * this.max_health);
1157 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1159 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1160 WaypointSprite_UpdateHealth(this.sprite, this.health);
1162 if(STAT(REVIVE_PROGRESS, this) >= 1)
1165 else if(STAT(FROZEN, this) == 3)
1167 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1168 this.health = max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this) );
1170 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1171 WaypointSprite_UpdateHealth(this.sprite, this.health);
1177 if(this.event_damage)
1178 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
1181 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1184 // otherwise, no revival!
1186 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1189 void Monster_Enemy_Check(entity this)
1193 this.enemy = Monster_FindTarget(this);
1196 WarpZone_RefSys_Copy(this.enemy, this);
1197 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1198 // update move target immediately?
1199 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1200 this.monster_moveto = '0 0 0';
1201 this.monster_face = '0 0 0';
1203 //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)));
1204 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1209 void Monster_Think(entity this)
1211 setthink(this, Monster_Think);
1212 this.nextthink = time + this.ticrate;
1214 if(this.monster_lifetime && time >= this.monster_lifetime)
1216 Damage(this, this, this, this.health + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1220 if(STAT(FROZEN, this))
1221 Monster_Frozen_Think(this);
1222 else if(time >= this.last_enemycheck)
1224 Monster_Enemy_Check(this);
1225 this.last_enemycheck = time + 1; // check for enemies every second
1228 Monster mon = Monsters_from(this.monsterid);
1229 if(mon.mr_think(mon, this))
1231 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1233 .entity weaponentity = weaponentities[0]; // TODO?
1234 Monster_Attack_Check(this, this.enemy, weaponentity);
1239 CSQCMODEL_AUTOUPDATE(this);
1242 bool Monster_Spawn_Setup(entity this)
1244 Monster mon = Monsters_from(this.monsterid);
1245 mon.mr_setup(mon, this);
1247 // ensure some basic needs are met
1248 if(!this.health) { this.health = 100; }
1249 if(!this.armorvalue) { this.armorvalue = bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9); }
1250 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1251 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1252 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1253 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1254 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1256 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1258 Monster_Miniboss_Check(this);
1259 this.health *= MONSTER_SKILLMOD(this);
1262 this.skin = rint(random() * 4);
1265 this.max_health = this.health;
1266 this.pain_finished = this.nextthink;
1268 if(IS_PLAYER(this.monster_follow))
1269 this.effects |= EF_DIMLIGHT;
1271 if(!this.wander_delay) { this.wander_delay = 2; }
1272 if(!this.wander_distance) { this.wander_distance = 600; }
1274 Monster_Sounds_Precache(this);
1275 Monster_Sounds_Update(this);
1279 if(!this.monster_attack)
1280 IL_PUSH(g_monster_targets, this);
1281 this.monster_attack = true; // we can have monster enemies in team games
1284 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1286 if(autocvar_g_monsters_healthbars)
1288 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1289 wp.wp_extra = this.monsterid;
1290 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1291 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1293 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1294 WaypointSprite_UpdateHealth(this.sprite, this.health);
1298 setthink(this, Monster_Think);
1299 this.nextthink = time + this.ticrate;
1301 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1307 bool Monster_Spawn(entity this, bool check_appear, int mon_id)
1309 // setup the basic required properties for a monster
1310 entity mon = Monsters_from(mon_id);
1311 if(!mon.monsterid) { return false; } // invalid monster
1313 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1315 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1317 IL_PUSH(g_monsters, this);
1318 if(this.mdl && this.mdl != "")
1319 precache_model(this.mdl);
1320 if(this.mdl_dead && this.mdl_dead != "")
1321 precache_model(this.mdl_dead);
1324 if(check_appear && Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed
1326 if(!this.monster_skill)
1327 this.monster_skill = cvar("g_monsters_skill");
1329 // support for quake style removing monsters based on skill
1330 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1331 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1332 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1334 if(this.team && !teamplay)
1337 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1338 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1339 monsters_total += 1;
1341 if(this.mdl && this.mdl != "")
1342 _setmodel(this, this.mdl);
1344 setmodel(this, mon.m_model);
1346 this.flags = FL_MONSTER;
1347 this.classname = "monster";
1348 this.takedamage = DAMAGE_AIM;
1349 if(!this.bot_attack)
1350 IL_PUSH(g_bot_targets, this);
1351 this.bot_attack = true;
1352 this.iscreature = true;
1353 this.teleportable = true;
1354 if(!this.damagedbycontents)
1355 IL_PUSH(g_damagedbycontents, this);
1356 this.damagedbycontents = true;
1357 this.monsterid = mon_id;
1358 this.event_damage = Monster_Damage;
1359 settouch(this, Monster_Touch);
1360 this.use = Monster_Use;
1361 this.solid = SOLID_BBOX;
1362 set_movetype(this, MOVETYPE_WALK);
1363 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1365 this.velocity = '0 0 0';
1366 this.moveto = this.origin;
1367 this.pos1 = this.origin;
1368 this.pos2 = this.angles;
1369 this.reset = Monster_Reset;
1370 this.netname = mon.netname;
1371 this.monster_attackfunc = mon.monster_attackfunc;
1372 this.monster_name = mon.monster_name;
1373 this.candrop = true;
1374 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1375 this.oldtarget2 = this.target2;
1376 //this.pass_distance = 0;
1377 this.deadflag = DEAD_NO;
1378 this.spawn_time = time;
1380 this.monster_moveto = '0 0 0';
1381 this.monster_face = '0 0 0';
1382 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1384 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1385 if(!this.scale) { this.scale = 1; }
1386 if(autocvar_g_monsters_edit) { this.grab = 1; }
1387 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1388 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1389 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1391 if(autocvar_g_playerclip_collisions)
1392 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1394 if(mon.spawnflags & MONSTER_TYPE_FLY)
1396 this.flags |= FL_FLY;
1397 set_movetype(this, MOVETYPE_FLY);
1400 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1402 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1405 if(mon.spawnflags & MONSTER_SIZE_QUAKE)
1406 if(autocvar_g_monsters_quake_resize)
1410 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1412 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1414 Monster_UpdateModel(this);
1416 if(!Monster_Spawn_Setup(this))
1418 Monster_Remove(this);
1424 setorigin(this, this.origin + '0 0 20');
1425 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1426 setorigin(this, trace_endpos);
1429 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1430 monster_setupcolors(this);
1432 CSQCMODEL_AUTOINIT(this);