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)
85 || (targ.items & IT_INVISIBILITY)
86 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
87 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || targ.health <= 0 || this.health <= 0))
88 || (this.monster_follow == targ || targ.monster_follow == this)
89 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
90 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
91 || (SAME_TEAM(targ, this))
92 || (STAT(FROZEN, targ))
93 || (targ.alpha != 0 && targ.alpha < 0.5)
94 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
97 // if any of the above checks fail, target is not valid
101 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
102 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this);
104 if(trace_fraction < 1 && trace_ent != targ)
105 return false; // solid
107 if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
108 if(this.enemy != targ)
110 makevectors (this.angles);
111 float dot = normalize (targ.origin - this.origin) * v_forward;
113 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
116 return true; // this target is valid!
119 entity Monster_FindTarget(entity this)
121 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
123 entity closest_target = NULL;
124 vector my_center = CENTER_OR_VIEWOFS(this);
126 // find the closest acceptable target to pass to
127 IL_EACH(g_monster_targets, it.monster_attack,
129 float trange = this.target_range;
130 if(PHYS_INPUT_BUTTON_CROUCH(it))
131 trange *= 0.75; // TODO cvar this
132 vector theirmid = (it.absmin + it.absmax) * 0.5;
133 if(vdist(theirmid - this.origin, >, trange))
135 if(!Monster_ValidTarget(this, it))
138 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
139 vector targ_center = CENTER_OR_VIEWOFS(it);
143 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
144 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
145 { closest_target = it; }
147 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;
175 if(this.colormap > 0)
176 this.glowmod = colormapPaletteColor(this.colormap & 0x0F, false);
178 this.glowmod = '1 1 1';
181 void monster_changeteam(entity this, int newteam)
183 if(!teamplay) { return; }
186 if(!this.monster_attack)
187 IL_PUSH(g_monster_targets, this);
188 this.monster_attack = true; // new team, activate attacking
189 monster_setupcolors(this);
193 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
195 this.sprite.team = newteam;
196 this.sprite.SendFlags |= 1;
200 .void(entity) monster_delayedfunc;
201 void Monster_Delay_Action(entity this)
203 if(Monster_ValidTarget(this.owner, this.owner.enemy)) { this.monster_delayedfunc(this.owner); }
208 setthink(this, Monster_Delay_Action);
209 this.nextthink = time + this.count;
213 setthink(this, SUB_Remove);
214 this.nextthink = time;
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 setthink(e, Monster_Delay_Action);
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_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
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)
311 float fh = fopen(f, FILE_READ);
314 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
317 while((s = fgets(fh)))
319 if(tokenize_console(s) != 3)
321 field = Monster_Sound_SampleField(argv(0));
322 if(GetMonsterSoundSampleField_notFound)
325 strunzone(this.(field));
326 this.(field) = strzone(strcat(argv(1), " ", argv(2)));
332 .int skin_for_monstersound;
333 void Monster_Sounds_Update(entity this)
335 if(this.skin == this.skin_for_monstersound) { return; }
337 this.skin_for_monstersound = this.skin;
338 Monster_Sounds_Clear(this);
339 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
340 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
343 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
345 if(!autocvar_g_monsters_sounds) { return; }
348 if(time < this.msound_delay)
350 string sample = this.(samplefield);
351 if (sample != "") sample = GlobalSound_sample(sample, random());
352 float myscale = ((this.scale) ? this.scale : 1); // safety net
353 // TODO: change volume depending on size too?
354 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
356 this.msound_delay = time + sound_delay;
360 // =======================
361 // Monster attack handlers
362 // =======================
364 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
366 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
368 setanim(this, anim, false, true, false);
370 if(this.animstate_endtime > time && IS_MONSTER(this))
371 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
373 this.attack_finished_single[0] = this.anim_finished = time + animtime;
375 monster_makevectors(this, targ);
377 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
379 if(trace_ent.takedamage)
380 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
385 bool Monster_Attack_Leap_Check(entity this, vector vel)
387 if(this.state && IS_MONSTER(this))
388 return false; // already attacking
389 if(!IS_ONGROUND(this))
390 return false; // not on the ground
391 if(this.health <= 0 || IS_DEAD(this))
392 return false; // called when dead?
393 if(time < this.attack_finished_single[0])
394 return false; // still attacking
396 vector old = this.velocity;
399 tracetoss(this, this);
401 if(trace_ent != this.enemy)
407 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
409 if(!Monster_Attack_Leap_Check(this, vel))
412 setanim(this, anm, false, true, false);
414 if(this.animstate_endtime > time && IS_MONSTER(this))
415 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
417 this.attack_finished_single[0] = this.anim_finished = time + animtime;
420 this.state = MONSTER_ATTACK_RANGED;
421 settouch(this, touchfunc);
424 UNSET_ONGROUND(this);
429 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
431 int slot = weaponslot(weaponentity);
434 || (!this.monster_attackfunc)
435 || (time < this.attack_finished_single[slot])
438 if(vdist(targ.origin - this.origin, <=, this.attack_range))
440 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
441 if(attack_success == 1)
442 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
443 else if(attack_success > 0)
447 if(vdist(targ.origin - this.origin, >, this.attack_range))
449 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
450 if(attack_success == 1)
451 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
452 else if(attack_success > 0)
458 // ======================
459 // Main monster functions
460 // ======================
462 void Monster_UpdateModel(entity this)
464 // assume some defaults
465 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
466 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
467 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
468 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
469 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
470 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
471 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
472 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
473 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
474 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
476 // then get the real values
477 Monster mon = Monsters_from(this.monsterid);
478 mon.mr_anim(mon, this);
481 void Monster_Touch(entity this, entity toucher)
483 if(toucher == NULL) { return; }
485 if(toucher.monster_attack)
486 if(this.enemy != toucher)
487 if(!IS_MONSTER(toucher))
488 if(Monster_ValidTarget(this, toucher))
489 this.enemy = toucher;
492 void Monster_Miniboss_Check(entity this)
494 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
497 float chance = random() * 100;
499 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
500 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
502 this.health += autocvar_g_monsters_miniboss_healthboost;
503 this.effects |= EF_RED;
505 this.weapon = WEP_VORTEX.m_id;
509 bool Monster_Respawn_Check(entity this)
511 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
512 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
513 return true; // enabled by a mutator
515 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
518 if(!autocvar_g_monsters_respawn)
524 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterid); }
528 void Monster_Dead_Fade(entity this)
530 if(Monster_Respawn_Check(this))
532 this.spawnflags |= MONSTERFLAG_RESPAWNED;
533 setthink(this, Monster_Respawn);
534 this.nextthink = time + this.respawntime;
535 this.monster_lifetime = 0;
536 this.deadflag = DEAD_RESPAWNING;
537 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
539 this.pos1 = this.origin;
540 this.pos2 = this.angles;
542 this.event_damage = func_null;
543 this.takedamage = DAMAGE_NO;
544 setorigin(this, this.pos1);
545 this.angles = this.pos2;
546 this.health = this.max_health;
547 setmodel(this, MDL_Null);
551 // number of monsters spawned with mobspawn command
554 SUB_SetFade(this, time + 3, 1);
558 void Monster_Use(entity this, entity actor, entity trigger)
560 if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
563 .float pass_distance;
564 vector Monster_Move_Target(entity this, entity targ)
566 // enemy is always preferred target
569 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
570 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
571 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
573 // cases where the enemy may have changed their state (don't need to check everything here)
575 || (IS_DEAD(this.enemy) || this.enemy.health < 1)
576 || (STAT(FROZEN, this.enemy))
577 || (this.enemy.flags & FL_NOTARGET)
578 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
579 || (this.enemy.takedamage == DAMAGE_NO)
580 || (vdist(this.origin - targ_origin, >, this.target_range))
581 || ((trace_fraction < 1) && (trace_ent != this.enemy)))
584 //this.pass_distance = 0;
589 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
590 print("Trace origin: ", vtos(targ_origin), "\n");
591 print("Target origin: ", vtos(this.enemy.origin), "\n");
592 print("My origin: ", vtos(this.origin), "\n"); */
594 this.monster_movestate = MONSTER_MOVE_ENEMY;
595 this.last_trace = time + 1.2;
596 if(this.monster_moveto)
597 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
602 /*makevectors(this.angles);
603 this.monster_movestate = MONSTER_MOVE_ENEMY;
604 this.last_trace = time + 1.2;
605 return this.enemy.origin; */
608 switch(this.monster_moveflags)
610 case MONSTER_MOVE_FOLLOW:
612 this.monster_movestate = MONSTER_MOVE_FOLLOW;
613 this.last_trace = time + 0.3;
614 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
616 case MONSTER_MOVE_SPAWNLOC:
618 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
619 this.last_trace = time + 2;
622 case MONSTER_MOVE_NOMOVE:
624 if(this.monster_moveto)
626 this.last_trace = time + 0.5;
627 return this.monster_moveto;
631 this.monster_movestate = MONSTER_MOVE_NOMOVE;
632 this.last_trace = time + 2;
637 case MONSTER_MOVE_WANDER:
640 this.monster_movestate = MONSTER_MOVE_WANDER;
642 if(this.monster_moveto)
644 this.last_trace = time + 0.5;
645 pos = this.monster_moveto;
649 this.last_trace = time + 0.5;
654 this.last_trace = time + this.wander_delay;
656 this.angles_y = rint(random() * 500);
657 makevectors(this.angles);
658 pos = this.origin + v_forward * this.wander_distance;
660 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
662 pos.z = random() * 200;
673 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
675 //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
676 //float initial_height = 0; //min(50, (targ_distance * tanh(20)));
677 //float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
678 //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
682 if(current_height) // make sure we can actually do this arcing path
684 targpos = (to + ('0 0 1' * current_height));
685 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
686 if(trace_fraction < 1)
688 //print("normal arc line failed, trying to find new pos...");
689 WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
690 targpos = (trace_endpos + '0 0 -10');
691 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
692 if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
693 /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
696 else { targpos = to; }
699 //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
701 vector desired_direction = normalize(targpos - from);
702 if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
703 else { this.velocity = (desired_direction * movespeed); }
705 //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
706 //this.angles = vectoangles(this.velocity);
712 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
714 // update goal entity if lost
715 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
716 this.goalentity = find(NULL, targetname, this.target2);
718 if(STAT(FROZEN, this))
720 movelib_brake_simple(this, stpspeed);
721 setanim(this, this.anim_idle, true, false, false);
722 return; // no physics while frozen!
725 if(this.flags & FL_SWIM)
727 if(this.waterlevel < WATERLEVEL_WETFEET)
729 if(time >= this.last_trace)
731 this.last_trace = time + 0.4;
733 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
734 this.angles = '90 90 0';
737 this.velocity_y += random() * 50;
738 this.velocity_x -= random() * 50;
742 this.velocity_y -= random() * 50;
743 this.velocity_x += random() * 50;
745 this.velocity_z += random() * 150;
749 set_movetype(this, MOVETYPE_BOUNCE);
750 //this.velocity_z = -200;
754 else if(this.move_movetype == MOVETYPE_BOUNCE)
757 set_movetype(this, MOVETYPE_WALK);
761 entity targ = this.goalentity;
763 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
765 || this.draggedby != NULL
766 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
767 || time < game_starttime
768 || (autocvar_g_campaign && !campaign_bots_may_start)
769 || time < this.spawn_time)
771 runspeed = walkspeed = 0;
772 if(time >= this.spawn_time)
773 setanim(this, this.anim_idle, true, false, false);
774 movelib_brake_simple(this, stpspeed);
778 targ = M_ARGV(3, entity);
779 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
780 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
782 if(teamplay && autocvar_g_monsters_teams)
783 if(DIFF_TEAM(this.monster_follow, this))
784 this.monster_follow = NULL;
786 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
789 settouch(this, Monster_Touch);
792 if(this.state && time >= this.attack_finished_single[0])
793 this.state = 0; // attack is over
795 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
796 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
797 this.moveto = Monster_Move_Target(this, targ);
800 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
802 if(this.state == MONSTER_ATTACK_MELEE)
803 this.moveto = this.origin;
805 if(this.enemy && this.enemy.vehicle)
808 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
809 this.moveto_z = this.origin_z;
811 if(vdist(this.origin - this.moveto, >, 100))
813 bool do_run = (this.enemy || this.monster_moveto);
814 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
815 Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
817 if(time > this.pain_finished && time > this.anim_finished) // TODO: use anim_finished instead!?
819 if(vdist(this.velocity, >, 10))
820 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
822 setanim(this, this.anim_idle, true, false, false);
826 entity e = this.goalentity; //find(NULL, targetname, this.target2);
827 if(e.target2 && e.target2 != "")
828 this.target2 = e.target2;
829 else if(e.target && e.target != "") // compatibility
830 this.target2 = e.target;
832 movelib_brake_simple(this, stpspeed);
833 if(time > this.anim_finished && time > this.pain_finished)
835 if(vdist(this.velocity, <=, 30))
836 setanim(this, this.anim_idle, true, false, false);
839 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
841 vector real_angle = vectoangles(this.steerto) - this.angles;
843 if(this.state == MONSTER_ATTACK_MELEE)
847 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
848 this.angles_y += turny;
852 void Monster_Remove(entity this)
855 return; // don't remove it?
857 if(!this) { return; }
859 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
860 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
862 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
864 .entity weaponentity = weaponentities[slot];
865 if(this.(weaponentity))
866 delete(this.(weaponentity));
868 if(this.iceblock) { delete(this.iceblock); }
869 WaypointSprite_Kill(this.sprite);
873 void Monster_Dead_Think(entity this)
875 this.nextthink = time + this.ticrate;
877 if(this.monster_lifetime != 0)
878 if(time >= this.monster_lifetime)
880 Monster_Dead_Fade(this);
885 void Monster_Appear(entity this, entity actor, entity trigger)
888 Monster_Spawn(this, false, this.monsterid);
891 bool Monster_Appear_Check(entity this, int monster_id)
893 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
896 setthink(this, func_null);
897 this.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
899 this.use = Monster_Appear;
900 this.flags = FL_MONSTER; // set so this monster can get butchered
905 void Monster_Reset(entity this)
907 setorigin(this, this.pos1);
908 this.angles = this.pos2;
910 Unfreeze(this); // remove any icy remains
912 this.health = this.max_health;
913 this.velocity = '0 0 0';
915 this.goalentity = NULL;
916 this.attack_finished_single[0] = 0;
917 this.moveto = this.origin;
920 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
922 this.health -= damage;
924 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
926 if(this.health <= -50) // 100 health until gone?
928 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
930 // number of monsters spawned with mobspawn command
933 setthink(this, SUB_Remove);
934 this.nextthink = time + 0.1;
935 this.event_damage = func_null;
939 void Monster_Dead(entity this, entity attacker, float gibbed)
941 setthink(this, Monster_Dead_Think);
942 this.nextthink = time;
943 this.monster_lifetime = time + 5;
945 if(STAT(FROZEN, this))
947 Unfreeze(this); // remove any icy remains
948 this.health = 0; // reset by Unfreeze
951 monster_dropitem(this, attacker);
953 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
955 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
956 monsters_killed += 1;
958 if(IS_PLAYER(attacker))
959 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
960 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
964 // number of monsters spawned with mobspawn command
968 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
969 _setmodel(this, this.mdl_dead);
971 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
972 this.solid = SOLID_CORPSE;
973 this.takedamage = DAMAGE_AIM;
974 this.deadflag = DEAD_DEAD;
976 set_movetype(this, MOVETYPE_TOSS);
977 this.moveto = this.origin;
978 settouch(this, Monster_Touch); // reset incase monster was pouncing
979 this.reset = func_null;
981 this.attack_finished_single[0] = 0;
984 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
985 this.velocity = '0 0 0';
987 CSQCModel_UnlinkEntity(this);
989 Monster mon = Monsters_from(this.monsterid);
990 mon.mr_death(mon, this);
992 if(this.candrop && this.weapon)
994 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
995 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
999 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
1001 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
1004 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
1007 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
1010 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1013 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
1016 vector v = healtharmor_applydamage(100, this.armorvalue / 100, deathtype, damage);
1020 Monster mon = Monsters_from(this.monsterid);
1021 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1025 this.health -= take;
1026 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1030 WaypointSprite_UpdateHealth(this.sprite, this.health);
1032 this.dmg_time = time;
1034 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1035 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1037 this.velocity += force * this.damageforcescale;
1039 if(deathtype != DEATH_DROWN.m_id && take)
1041 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1043 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1045 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1048 if(this.health <= 0)
1050 if(deathtype == DEATH_KILL.m_id)
1051 this.candrop = false; // killed by mobkill command
1054 SUB_UseTargets(this, attacker, this.enemy);
1055 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1057 Monster_Dead(this, attacker, (this.health <= -100 || deathtype == DEATH_KILL.m_id));
1059 WaypointSprite_Kill(this.sprite);
1061 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1063 if(this.health <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1065 Violence_GibSplash(this, 1, 0.5, attacker);
1067 setthink(this, SUB_Remove);
1068 this.nextthink = time + 0.1;
1073 // don't check for enemies, just keep walking in a straight line
1074 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1076 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)
1079 if(time >= this.spawn_time)
1080 setanim(this, this.anim_idle, true, false, false);
1081 movelib_brake_simple(this, 0.6);
1085 makevectors(this.angles);
1086 vector a = CENTER_OR_VIEWOFS(this);
1087 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1089 traceline(a, b, MOVE_NORMAL, this);
1091 bool reverse = false;
1092 if(trace_fraction != 1.0)
1094 if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1096 if(trace_ent && IS_MONSTER(trace_ent))
1099 // TODO: fix this... tracing is broken if the floor is thin
1104 traceline(b, a, MOVE_WORLDONLY, this);
1105 if(trace_fraction == 1.0)
1111 this.angles_y = anglemods(this.angles_y - 180);
1112 makevectors(this.angles);
1115 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1117 if(time > this.pain_finished && time > this.attack_finished_single[0])
1118 if(vdist(this.velocity, >, 10))
1119 setanim(this, this.anim_walk, true, false, false);
1121 setanim(this, this.anim_idle, true, false, false);
1124 void Monster_Anim(entity this)
1126 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1131 // Decide on which death animation to use.
1133 deadbits = ANIMSTATE_DEAD1;
1135 deadbits = ANIMSTATE_DEAD2;
1140 // Clear a previous death animation.
1143 int animbits = deadbits;
1144 if(STAT(FROZEN, this))
1145 animbits |= ANIMSTATE_FROZEN;
1147 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1148 animdecide_setstate(this, animbits, false);
1149 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1151 /* // weapon entities for monsters?
1152 if (this.weaponentity)
1154 updateanim(this.weaponentity);
1155 if (!this.weaponentity.animstate_override)
1156 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1161 void Monster_Frozen_Think(entity this)
1163 if(STAT(FROZEN, this) == 2)
1165 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1166 this.health = max(1, STAT(REVIVE_PROGRESS, this) * this.max_health);
1167 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1169 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1170 WaypointSprite_UpdateHealth(this.sprite, this.health);
1172 if(STAT(REVIVE_PROGRESS, this) >= 1)
1175 else if(STAT(FROZEN, this) == 3)
1177 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1178 this.health = max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this) );
1180 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1181 WaypointSprite_UpdateHealth(this.sprite, this.health);
1187 if(this.event_damage)
1188 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
1191 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1194 // otherwise, no revival!
1196 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1199 void Monster_Enemy_Check(entity this)
1203 this.enemy = Monster_FindTarget(this);
1206 WarpZone_RefSys_Copy(this.enemy, this);
1207 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1208 // update move target immediately?
1209 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1210 this.monster_moveto = '0 0 0';
1211 this.monster_face = '0 0 0';
1213 //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)));
1214 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1219 void Monster_Think(entity this)
1221 setthink(this, Monster_Think);
1222 this.nextthink = time + this.ticrate;
1224 if(this.monster_lifetime && time >= this.monster_lifetime)
1226 Damage(this, this, this, this.health + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1230 if(STAT(FROZEN, this))
1231 Monster_Frozen_Think(this);
1232 else if(time >= this.last_enemycheck)
1234 Monster_Enemy_Check(this);
1235 this.last_enemycheck = time + 1; // check for enemies every second
1238 Monster mon = Monsters_from(this.monsterid);
1239 if(mon.mr_think(mon, this))
1241 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1243 .entity weaponentity = weaponentities[0]; // TODO?
1244 Monster_Attack_Check(this, this.enemy, weaponentity);
1249 CSQCMODEL_AUTOUPDATE(this);
1252 bool Monster_Spawn_Setup(entity this)
1254 Monster mon = Monsters_from(this.monsterid);
1255 mon.mr_setup(mon, this);
1257 // ensure some basic needs are met
1258 if(!this.health) { this.health = 100; }
1259 if(!this.armorvalue) { this.armorvalue = bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9); }
1260 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1261 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1262 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1263 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1264 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1266 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1268 Monster_Miniboss_Check(this);
1269 this.health *= MONSTER_SKILLMOD(this);
1272 this.skin = rint(random() * 4);
1275 this.max_health = this.health;
1276 this.pain_finished = this.nextthink;
1278 if(IS_PLAYER(this.monster_follow))
1279 this.effects |= EF_DIMLIGHT;
1281 if(!this.wander_delay) { this.wander_delay = 2; }
1282 if(!this.wander_distance) { this.wander_distance = 600; }
1284 Monster_Sounds_Precache(this);
1285 Monster_Sounds_Update(this);
1289 if(!this.monster_attack)
1290 IL_PUSH(g_monster_targets, this);
1291 this.monster_attack = true; // we can have monster enemies in team games
1294 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1296 if(autocvar_g_monsters_healthbars)
1298 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1299 wp.wp_extra = this.monsterid;
1300 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1301 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1303 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1304 WaypointSprite_UpdateHealth(this.sprite, this.health);
1308 setthink(this, Monster_Think);
1309 this.nextthink = time + this.ticrate;
1311 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1317 bool Monster_Spawn(entity this, bool check_appear, int mon_id)
1319 // setup the basic required properties for a monster
1320 entity mon = Monsters_from(mon_id);
1321 if(!mon.monsterid) { return false; } // invalid monster
1323 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1325 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1327 IL_PUSH(g_monsters, this);
1328 if(this.mdl && this.mdl != "")
1329 precache_model(this.mdl);
1330 if(this.mdl_dead && this.mdl_dead != "")
1331 precache_model(this.mdl_dead);
1334 if(check_appear && Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed
1336 if(!this.monster_skill)
1337 this.monster_skill = cvar("g_monsters_skill");
1339 // support for quake style removing monsters based on skill
1340 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1341 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1342 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1344 if(this.team && !teamplay)
1347 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1348 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1349 monsters_total += 1;
1351 if(this.mdl && this.mdl != "")
1352 _setmodel(this, this.mdl);
1354 setmodel(this, mon.m_model);
1356 this.flags = FL_MONSTER;
1357 this.classname = "monster";
1358 this.takedamage = DAMAGE_AIM;
1359 if(!this.bot_attack)
1360 IL_PUSH(g_bot_targets, this);
1361 this.bot_attack = true;
1362 this.iscreature = true;
1363 this.teleportable = true;
1364 if(!this.damagedbycontents)
1365 IL_PUSH(g_damagedbycontents, this);
1366 this.damagedbycontents = true;
1367 this.monsterid = mon_id;
1368 this.event_damage = Monster_Damage;
1369 settouch(this, Monster_Touch);
1370 this.use = Monster_Use;
1371 this.solid = SOLID_BBOX;
1372 set_movetype(this, MOVETYPE_WALK);
1373 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1375 this.velocity = '0 0 0';
1376 this.moveto = this.origin;
1377 this.pos1 = this.origin;
1378 this.pos2 = this.angles;
1379 this.reset = Monster_Reset;
1380 this.netname = mon.netname;
1381 this.monster_attackfunc = mon.monster_attackfunc;
1382 this.monster_name = mon.monster_name;
1383 this.candrop = true;
1384 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1385 this.oldtarget2 = this.target2;
1386 //this.pass_distance = 0;
1387 this.deadflag = DEAD_NO;
1388 this.spawn_time = time;
1390 this.monster_moveto = '0 0 0';
1391 this.monster_face = '0 0 0';
1392 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1394 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1395 if(!this.scale) { this.scale = 1; }
1396 if(autocvar_g_monsters_edit) { this.grab = 1; }
1397 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1398 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1399 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1401 if(autocvar_g_monsters_playerclip_collisions)
1402 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1404 if(mon.spawnflags & MONSTER_TYPE_FLY)
1406 this.flags |= FL_FLY;
1407 set_movetype(this, MOVETYPE_FLY);
1410 if((mon.spawnflags & MONSTER_SIZE_QUAKE) && autocvar_g_monsters_quake_resize && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
1413 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1415 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1417 Monster_UpdateModel(this);
1419 if(!Monster_Spawn_Setup(this))
1421 Monster_Remove(this);
1427 setorigin(this, this.origin + '0 0 20');
1428 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1429 setorigin(this, trace_endpos);
1432 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1433 monster_setupcolors(this);
1435 CSQCMODEL_AUTOINIT(this);