1 #include "sv_monsters.qh"
3 #include <common/constants.qh>
4 #include <common/deathtypes/all.qh>
5 #include <common/items/_mod.qh>
6 #include <common/mapobjects/teleporters.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/monsters/all.qh>
9 #include <common/mutators/mutator/nades/nades.qh>
10 #include <common/mutators/mutator/status_effects/_mod.qh>
11 #include <common/physics/movelib.qh>
12 #include <common/stats.qh>
13 #include <common/teams.qh>
14 #include <common/turrets/sv_turrets.qh>
15 #include <common/turrets/util.qh>
16 #include <common/util.qh>
17 #include <common/vehicles/all.qh>
18 #include <common/weapons/_all.qh>
19 #include <common/weapons/_mod.qh>
20 #include <lib/csqcmodel/sv_model.qh>
21 #include <lib/warpzone/common.qh>
22 #include <server/campaign.qh>
23 #include <server/cheats.qh>
24 #include <server/client.qh>
25 #include <server/command/_mod.qh>
26 #include <server/damage.qh>
27 #include <server/items/items.qh>
28 #include <server/mutators/_mod.qh>
29 #include <server/round_handler.qh>
30 #include <server/steerlib.qh>
31 #include <server/weapons/_mod.qh>
33 void monsters_setstatus(entity this)
35 STAT(MONSTERS_TOTAL, this) = monsters_total;
36 STAT(MONSTERS_KILLED, this) = monsters_killed;
39 void monster_dropitem(entity this, entity attacker)
41 if(!this.candrop || !this.monster_loot)
44 vector org = CENTER_OR_VIEWOFS(this);
46 Item_SetLoot(e, true);
47 e.spawnfunc_checked = true;
49 e.monster_loot = this.monster_loot;
51 MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
52 e = M_ARGV(1, entity);
54 if(e && e.monster_loot)
57 StartItem(e, e.monster_loot);
60 e.velocity = randomvec() * 175 + '0 0 325';
61 e.item_spawnshieldtime = time + 0.7;
62 SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
66 bool monster_facing(entity this, entity targ)
68 // relies on target having an origin
69 makevectors(this.angles);
70 vector targ_org = targ.origin, my_org = this.origin;
71 if(autocvar_g_monsters_target_infront_2d)
73 targ_org = vec2(targ_org);
74 my_org = vec2(my_org);
76 float dot = normalize(targ_org - my_org) * v_forward;
78 return !(dot <= autocvar_g_monsters_target_infront_range);
81 void monster_makevectors(entity this, entity targ)
85 vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
86 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
87 this.v_angle_x = -this.v_angle_x;
90 makevectors(this.v_angle);
97 bool Monster_ValidTarget(entity this, entity targ, bool skipfacing)
99 // ensure we're not checking nonexistent monster/target
100 if(!this || !targ) { return false; }
103 || (IS_VEHICLE(targ) && !(this.monsterdef.spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
104 || (time < game_starttime) // monsters do nothing before match has started
105 || (targ.takedamage == DAMAGE_NO)
107 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
108 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || GetResource(targ, RES_HEALTH) <= 0 || GetResource(this, RES_HEALTH) <= 0))
109 || (this.monster_follow == targ || targ.monster_follow == this)
110 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
111 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
112 || (SAME_TEAM(targ, this))
113 || (STAT(FROZEN, targ))
114 || (targ.alpha != 0 && targ.alpha < 0.5)
115 || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
116 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
119 // if any of the above checks fail, target is not valid
123 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
124 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this); // TODO: maybe we can rely a bit on PVS data instead?
126 if(trace_fraction < 1 && trace_ent != targ)
127 return false; // solid
129 if(!skipfacing && (autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT)))
130 if(this.enemy != targ)
132 if(!monster_facing(this, targ))
136 return true; // this target is valid!
139 entity Monster_FindTarget(entity this)
141 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
143 entity closest_target = NULL;
144 vector my_center = CENTER_OR_VIEWOFS(this);
146 // find the closest acceptable target to pass to
147 IL_EACH(g_monster_targets, it.monster_attack,
149 float trange = this.target_range;
150 if(PHYS_INPUT_BUTTON_CROUCH(it))
151 trange *= 0.75; // TODO cvar this
152 vector theirmid = (it.absmin + it.absmax) * 0.5;
153 if(vdist(theirmid - this.origin, >, trange))
155 if(!Monster_ValidTarget(this, it, false))
158 // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
159 vector targ_center = CENTER_OR_VIEWOFS(it);
163 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
164 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
165 { closest_target = it; }
167 else { closest_target = it; }
170 return closest_target;
173 void monster_setupcolors(entity this)
175 if(IS_PLAYER(this.realowner))
176 this.colormap = this.realowner.colormap;
177 else if(teamplay && this.team)
178 this.colormap = 1024 + (this.team - 1) * 17;
181 if(this.monster_skill <= MONSTER_SKILL_EASY)
182 this.colormap = 1126;
183 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
184 this.colormap = 1075;
185 else if(this.monster_skill <= MONSTER_SKILL_HARD)
186 this.colormap = 1228;
187 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
188 this.colormap = 1092;
189 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
190 this.colormap = 1160;
192 this.colormap = 1024;
195 if(this.colormap > 0)
196 this.glowmod = colormapPaletteColor(this.colormap & 0x0F, false);
198 this.glowmod = '1 1 1';
201 void monster_changeteam(entity this, int newteam)
203 if(!teamplay) { return; }
206 if(!this.monster_attack)
207 IL_PUSH(g_monster_targets, this);
208 this.monster_attack = true; // new team, activate attacking
209 monster_setupcolors(this);
213 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
215 this.sprite.team = newteam;
216 this.sprite.SendFlags |= 1;
220 .void(entity) monster_delayedfunc;
221 void Monster_Delay_Action(entity this)
223 // TODO: maybe do check for facing here
224 if(Monster_ValidTarget(this.owner, this.owner.enemy, false))
226 monster_makevectors(this.owner, this.owner.enemy);
227 this.monster_delayedfunc(this.owner);
233 setthink(this, Monster_Delay_Action);
234 this.nextthink = time + this.count;
238 setthink(this, SUB_Remove);
239 this.nextthink = time;
243 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
245 // deferred attacking, checks if monster is still alive and target is still valid before attacking
246 entity e = new_pure(Monster_Delay);
248 setthink(e, Monster_Delay_Action);
249 e.nextthink = time + defer_amnt;
250 e.count = defer_amnt;
252 e.monster_delayedfunc = func;
253 e.cnt = repeat_count;
261 string get_monster_model_datafilename(string m, float sk, string fil)
266 m = "models/monsters/*_";
268 m = strcat(m, ftos(sk));
271 return strcat(m, ".", fil);
274 void Monster_Sound_Precache(string f)
278 fh = fopen(f, FILE_READ);
281 while((s = fgets(fh)))
283 if(tokenize_console(s) != 3)
285 //LOG_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
288 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
293 void Monster_Sounds_Precache(entity this)
295 string m = this.monsterdef.m_model.model_str();
296 float globhandle, n, i;
299 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
302 n = search_getsize(globhandle);
303 for (i = 0; i < n; ++i)
305 //print(search_getfilename(globhandle, i), "\n");
306 f = search_getfilename(globhandle, i);
307 Monster_Sound_Precache(f);
309 search_end(globhandle);
312 void Monster_Sounds_Clear(entity this)
314 #define _MSOUND(m) strfree(this.monstersound_##m);
319 .string Monster_Sound_SampleField(string type)
321 GetMonsterSoundSampleField_notFound = 0;
324 #define _MSOUND(m) case #m: return monstersound_##m;
328 GetMonsterSoundSampleField_notFound = 1;
332 bool Monster_Sounds_Load(entity this, string f, int first)
336 float fh = fopen(f, FILE_READ);
339 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
342 while((s = fgets(fh)))
344 if(tokenize_console(s) != 3)
346 field = Monster_Sound_SampleField(argv(0));
347 if(GetMonsterSoundSampleField_notFound)
349 strcpy(this.(field), strcat(argv(1), " ", argv(2)));
355 .int skin_for_monstersound;
356 void Monster_Sounds_Update(entity this)
358 if(this.skin == this.skin_for_monstersound) { return; }
360 this.skin_for_monstersound = this.skin;
361 Monster_Sounds_Clear(this);
362 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
363 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
366 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
368 if(!autocvar_g_monsters_sounds) { return; }
371 if(time < this.msound_delay)
373 string sample = this.(samplefield);
374 if (sample != "") sample = GlobalSound_sample(sample, random());
375 float myscale = ((this.scale) ? this.scale : 1); // safety net
376 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
378 this.msound_delay = time + sound_delay;
382 // =======================
383 // Monster attack handlers
384 // =======================
386 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
388 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
390 setanim(this, anim, false, true, false);
392 if(this.animstate_endtime > time && IS_MONSTER(this))
393 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
395 this.attack_finished_single[0] = this.anim_finished = time + animtime;
397 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
399 if(trace_ent.takedamage)
400 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
405 bool Monster_Attack_Leap_Check(entity this, vector vel)
407 if(this.state && IS_MONSTER(this))
408 return false; // already attacking
409 if(!IS_ONGROUND(this))
410 return false; // not on the ground
411 if(GetResource(this, RES_HEALTH) <= 0 || IS_DEAD(this))
412 return false; // called when dead?
413 if(time < this.attack_finished_single[0])
414 return false; // still attacking
416 vector old = this.velocity;
419 tracetoss(this, this);
421 if(trace_ent != this.enemy)
427 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
429 if(!Monster_Attack_Leap_Check(this, vel))
432 setanim(this, anm, false, true, false);
434 if(this.animstate_endtime > time && IS_MONSTER(this))
435 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
437 this.attack_finished_single[0] = this.anim_finished = time + animtime;
440 this.state = MONSTER_ATTACK_RANGED;
441 settouch(this, touchfunc);
444 UNSET_ONGROUND(this);
449 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
451 int slot = weaponslot(weaponentity);
454 || (!this.monster_attackfunc)
456 || (time < this.attack_finished_single[slot])
457 || ((autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT)) && !monster_facing(this, targ))
460 if(vdist(targ.origin - this.origin, <=, this.attack_range))
462 monster_makevectors(this, targ);
463 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
464 if(attack_success == 1)
465 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
466 else if(attack_success > 0)
470 if(vdist(targ.origin - this.origin, >, this.attack_range))
472 monster_makevectors(this, targ);
473 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
474 if(attack_success == 1)
475 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
476 else if(attack_success > 0)
482 // ======================
483 // Main monster functions
484 // ======================
486 void Monster_UpdateModel(entity this)
488 // assume some defaults
489 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
490 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
491 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
492 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
493 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
494 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
495 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
496 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
497 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
498 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
500 // then get the real values
501 Monster mon = this.monsterdef;
502 mon.mr_anim(mon, this);
505 void Monster_Touch(entity this, entity toucher)
507 if(!toucher) { return; }
509 if(toucher.monster_attack && this.enemy != toucher && !IS_MONSTER(toucher) && time >= this.spawn_time)
510 if(Monster_ValidTarget(this, toucher, true))
511 this.enemy = toucher;
514 void Monster_Miniboss_Check(entity this)
516 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
519 float chance = random() * 100;
521 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
522 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
524 GiveResource(this, RES_HEALTH, autocvar_g_monsters_miniboss_healthboost);
525 this.effects |= EF_RED;
527 this.weapon = WEP_VORTEX.m_id;
531 bool Monster_Respawn_Check(entity this)
533 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
534 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
535 return true; // enabled by a mutator
537 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
540 if(!autocvar_g_monsters_respawn)
546 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterdef); }
550 void Monster_Dead_Fade(entity this)
552 if(Monster_Respawn_Check(this))
554 this.spawnflags |= MONSTERFLAG_RESPAWNED;
555 setthink(this, Monster_Respawn);
556 this.nextthink = time + this.respawntime;
557 this.monster_lifetime = 0;
558 this.deadflag = DEAD_RESPAWNING;
559 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
561 this.pos1 = this.origin;
562 this.pos2 = this.angles;
564 this.event_damage = func_null;
565 this.event_heal = func_null;
566 this.takedamage = DAMAGE_NO;
567 setorigin(this, this.pos1);
568 this.angles = this.pos2;
569 SetResourceExplicit(this, RES_HEALTH, this.max_health);
570 setmodel(this, MDL_Null);
574 // number of monsters spawned with mobspawn command
577 SUB_SetFade(this, time + 3, 1);
581 void Monster_Use(entity this, entity actor, entity trigger)
583 if(Monster_ValidTarget(this, actor, true)) { this.enemy = actor; }
586 vector Monster_Move_Target(entity this, entity targ)
588 // enemy is always preferred target
591 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
592 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
596 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
597 print("Trace origin: ", vtos(targ_origin), "\n");
598 print("Target origin: ", vtos(this.enemy.origin), "\n");
599 print("My origin: ", vtos(this.origin), "\n"); */
601 this.monster_movestate = MONSTER_MOVE_ENEMY;
602 this.last_trace = time + 1.2;
603 if(this.monster_moveto)
604 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
609 /*makevectors(this.angles);
610 this.monster_movestate = MONSTER_MOVE_ENEMY;
611 this.last_trace = time + 1.2;
612 return this.enemy.origin; */
615 switch(this.monster_moveflags)
617 case MONSTER_MOVE_FOLLOW:
619 this.monster_movestate = MONSTER_MOVE_FOLLOW;
620 this.last_trace = time + 0.3;
621 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
623 case MONSTER_MOVE_SPAWNLOC:
625 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
626 this.last_trace = time + 2;
629 case MONSTER_MOVE_NOMOVE:
631 if(this.monster_moveto)
633 this.last_trace = time + 0.5;
634 return this.monster_moveto;
638 this.monster_movestate = MONSTER_MOVE_NOMOVE;
639 this.last_trace = time + 2;
644 case MONSTER_MOVE_WANDER:
647 this.monster_movestate = MONSTER_MOVE_WANDER;
649 if(this.monster_moveto)
651 this.last_trace = time + 0.5;
652 pos = this.monster_moveto;
656 this.last_trace = time + 0.5;
661 this.last_trace = time + this.wander_delay;
663 this.angles_y = rint(random() * 500);
664 makevectors(this.angles);
665 pos = this.origin + v_forward * this.wander_distance;
667 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
669 pos.z = random() * 200;
682 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
684 // update goal entity if lost
685 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
686 this.goalentity = find(NULL, targetname, this.target2);
688 if(STAT(FROZEN, this))
690 movelib_brake_simple(this, stpspeed);
691 setanim(this, this.anim_idle, true, false, false);
692 return; // no physics while frozen!
695 if(this.flags & FL_SWIM)
697 if(this.waterlevel < WATERLEVEL_WETFEET)
699 if(time >= this.last_trace)
701 this.last_trace = time + 0.4;
703 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
704 this.angles = '90 90 0';
707 this.velocity_y += random() * 50;
708 this.velocity_x -= random() * 50;
712 this.velocity_y -= random() * 50;
713 this.velocity_x += random() * 50;
715 this.velocity_z += random() * 150;
719 set_movetype(this, MOVETYPE_BOUNCE);
720 //this.velocity_z = -200;
724 else if(this.move_movetype == MOVETYPE_BOUNCE)
727 set_movetype(this, MOVETYPE_WALK);
731 entity targ = this.goalentity;
733 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
735 || this.draggedby != NULL
736 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
737 || time < game_starttime
738 || (autocvar_g_campaign && !campaign_bots_may_start)
739 || time < this.spawn_time)
741 runspeed = walkspeed = 0;
742 if(time >= this.spawn_time)
743 setanim(this, this.anim_idle, true, false, false);
744 movelib_brake_simple(this, stpspeed);
748 targ = M_ARGV(3, entity);
749 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
750 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
752 if(teamplay && autocvar_g_monsters_teams)
753 if(DIFF_TEAM(this.monster_follow, this))
754 this.monster_follow = NULL;
756 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
759 settouch(this, Monster_Touch);
762 if(this.state && time >= this.attack_finished_single[0])
763 this.state = 0; // attack is over
765 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
766 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
767 this.moveto = Monster_Move_Target(this, targ);
770 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
772 if(this.state == MONSTER_ATTACK_MELEE)
773 this.moveto = this.origin;
775 if(this.enemy && this.enemy.vehicle)
778 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
779 this.moveto_z = this.origin_z;
781 fixedmakevectors(this.angles);
782 float vz = this.velocity_z;
784 if(!turret_closetotarget(this, this.moveto, 16))
786 bool do_run = (this.enemy || this.monster_moveto);
787 movelib_move_simple(this, v_forward, ((do_run) ? runspeed : walkspeed), 0.4);
789 if(time > this.pain_finished && time > this.anim_finished)
792 if(vdist(this.velocity, >, 10))
793 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
795 setanim(this, this.anim_idle, true, false, false);
800 entity e = this.goalentity; //find(NULL, targetname, this.target2);
801 if(e.target2 && e.target2 != "")
802 this.target2 = e.target2;
803 else if(e.target && e.target != "") // compatibility
804 this.target2 = e.target;
806 movelib_brake_simple(this, stpspeed);
807 if(time > this.anim_finished && time > this.pain_finished)
809 if(vdist(this.velocity, <=, 30))
810 setanim(this, this.anim_idle, true, false, false);
813 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
814 this.velocity_z = vz;
816 this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
818 vector real_angle = vectoangles(this.steerto) - this.angles;
820 if(this.state == MONSTER_ATTACK_MELEE)
824 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
825 this.angles_y += turny;
829 void Monster_Remove(entity this)
832 return; // don't remove it?
834 if(!this) { return; }
836 if(!MUTATOR_CALLHOOK(MonsterRemove, this))
837 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
839 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
841 .entity weaponentity = weaponentities[slot];
842 if(this.(weaponentity))
843 delete(this.(weaponentity));
845 if(this.iceblock) { delete(this.iceblock); }
846 WaypointSprite_Kill(this.sprite);
850 void Monster_Dead_Think(entity this)
852 this.nextthink = time + this.ticrate;
854 Monster mon = REGISTRY_GET(Monsters, this.monsterid);
855 mon.mr_deadthink(mon, this);
857 if(this.monster_lifetime != 0)
858 if(time >= this.monster_lifetime)
860 Monster_Dead_Fade(this);
865 void Monster_Appear(entity this, entity actor, entity trigger)
868 Monster_Spawn(this, false, this.monsterdef);
871 bool Monster_Appear_Check(entity this, Monster monster_id)
873 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
876 setthink(this, func_null);
877 this.monsterdef = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
879 this.use = Monster_Appear;
880 this.flags = FL_MONSTER; // set so this monster can get butchered
885 void Monster_Reset(entity this)
887 setorigin(this, this.pos1);
888 this.angles = this.pos2;
890 Unfreeze(this, false); // remove any icy remains
892 SetResourceExplicit(this, RES_HEALTH, this.max_health);
893 this.velocity = '0 0 0';
895 this.goalentity = NULL;
896 this.attack_finished_single[0] = 0;
897 this.moveto = this.origin;
900 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
902 TakeResource(this, RES_HEALTH, damage);
904 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
906 if(GetResource(this, RES_HEALTH) <= -50) // 100 health until gone?
908 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
910 // number of monsters spawned with mobspawn command
913 setthink(this, SUB_Remove);
914 this.nextthink = time + 0.1;
915 this.event_damage = func_null;
919 void Monster_Dead(entity this, entity attacker, float gibbed)
921 setthink(this, Monster_Dead_Think);
922 this.nextthink = time;
923 this.monster_lifetime = time + 5;
925 if(STAT(FROZEN, this))
926 Unfreeze(this, false); // remove any icy remains
928 monster_dropitem(this, attacker);
930 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
932 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
933 monsters_killed += 1;
935 if(IS_PLAYER(attacker))
936 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
937 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
941 // number of monsters spawned with mobspawn command
945 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
946 _setmodel(this, this.mdl_dead);
948 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
949 this.event_heal = func_null;
950 this.solid = SOLID_CORPSE;
951 this.takedamage = DAMAGE_AIM;
952 this.deadflag = DEAD_DEAD;
954 set_movetype(this, MOVETYPE_TOSS);
955 this.moveto = this.origin;
956 settouch(this, Monster_Touch); // reset incase monster was pouncing
957 this.reset = func_null;
959 this.attack_finished_single[0] = 0;
961 this.dphitcontentsmask &= ~DPCONTENTS_BODY;
963 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
964 this.velocity = '0 0 0';
966 CSQCModel_UnlinkEntity(this);
968 Monster mon = this.monsterdef;
969 mon.mr_death(mon, this);
971 if(this.candrop && this.weapon)
973 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
974 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
978 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
980 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
983 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
986 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
989 if(StatusEffects_active(STATUSEFFECT_SpawnShield, this) && deathtype != DEATH_KILL.m_id)
992 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
995 vector v = healtharmor_applydamage(100, GetResource(this, RES_ARMOR) / 100, deathtype, damage);
999 Monster mon = this.monsterdef;
1000 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1004 TakeResource(this, RES_HEALTH, take);
1005 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1009 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1011 this.dmg_time = time;
1013 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1014 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1016 this.velocity += force * this.damageforcescale;
1018 if(deathtype != DEATH_DROWN.m_id && take)
1020 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1022 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1024 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1027 if(GetResource(this, RES_HEALTH) <= 0)
1029 if(deathtype == DEATH_KILL.m_id)
1030 this.candrop = false; // killed by mobkill command
1033 SUB_UseTargets(this, attacker, this.enemy);
1034 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1036 Monster_Dead(this, attacker, (GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id));
1038 WaypointSprite_Kill(this.sprite);
1040 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1042 if(GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1044 Violence_GibSplash(this, 1, 0.5, attacker);
1046 setthink(this, SUB_Remove);
1047 this.nextthink = time + 0.1;
1052 bool Monster_Heal(entity targ, entity inflictor, float amount, float limit)
1054 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
1055 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
1058 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
1060 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
1064 // don't check for enemies, just keep walking in a straight line
1065 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1067 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)
1070 if(time >= this.spawn_time)
1071 setanim(this, this.anim_idle, true, false, false);
1072 movelib_brake_simple(this, 0.6);
1076 makevectors(this.angles);
1077 vector a = CENTER_OR_VIEWOFS(this);
1078 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1080 traceline(a, b, MOVE_NORMAL, this);
1082 bool reverse = false;
1083 if(trace_fraction != 1.0)
1085 if(trace_ent && IS_PLAYER(trace_ent))
1087 if(trace_ent && IS_MONSTER(trace_ent))
1090 if(!allow_jumpoff && IS_ONGROUND(this))
1092 traceline(b, b - '0 0 32', MOVE_NORMAL, this);
1093 if(trace_fraction == 1.0)
1099 this.angles_y = anglemods(this.angles_y - 180);
1100 makevectors(this.angles);
1103 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1105 if(time > this.pain_finished && time > this.attack_finished_single[0])
1107 if(vdist(this.velocity, >, 10))
1108 setanim(this, this.anim_walk, true, false, false);
1110 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) == FROZEN_TEMP_REVIVING)
1155 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1156 SetResourceExplicit(this, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * this.max_health));
1158 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1160 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1161 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1163 if(STAT(REVIVE_PROGRESS, this) >= 1)
1164 Unfreeze(this, false);
1166 else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
1168 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1169 SetResourceExplicit(this, RES_HEALTH, max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
1171 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1172 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1174 if(GetResource(this, RES_HEALTH) < 1)
1176 Unfreeze(this, false);
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');
1180 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1181 Unfreeze(this, false);
1183 // otherwise, no revival!
1185 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1188 void Monster_Enemy_Check(entity this)
1192 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
1193 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
1194 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
1196 // cases where the enemy may have changed their state (don't need to check everything here)
1197 if( (IS_DEAD(this.enemy) || GetResource(this.enemy, RES_HEALTH) < 1)
1198 || (STAT(FROZEN, this.enemy))
1199 || (this.enemy.flags & FL_NOTARGET)
1200 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
1201 || (this.enemy.takedamage == DAMAGE_NO)
1202 || (vdist(this.origin - targ_origin, >, this.target_range))
1203 || ((trace_fraction < 1) && (trace_ent != this.enemy))
1214 this.enemy = Monster_FindTarget(this);
1217 WarpZone_RefSys_Copy(this.enemy, this);
1218 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1219 // update move target immediately?
1220 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1221 this.monster_moveto = '0 0 0';
1222 this.monster_face = '0 0 0';
1224 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1228 void Monster_Think(entity this)
1230 setthink(this, Monster_Think);
1231 this.nextthink = time + this.ticrate;
1233 if(this.monster_lifetime && time >= this.monster_lifetime)
1235 Damage(this, this, this, GetResource(this, RES_HEALTH) + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1239 if(STAT(FROZEN, this))
1240 Monster_Frozen_Think(this);
1241 else if(time >= this.last_enemycheck)
1243 Monster_Enemy_Check(this);
1244 this.last_enemycheck = time + 1; // check for enemies every second
1247 Monster mon = this.monsterdef;
1248 if(mon.mr_think(mon, this))
1250 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1252 .entity weaponentity = weaponentities[0]; // TODO?
1253 Monster_Attack_Check(this, this.enemy, weaponentity);
1258 CSQCMODEL_AUTOUPDATE(this);
1261 bool Monster_Spawn_Setup(entity this)
1263 Monster mon = this.monsterdef;
1264 mon.mr_setup(mon, this);
1266 // ensure some basic needs are met
1267 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 100); }
1268 if(!GetResource(this, RES_ARMOR)) { SetResourceExplicit(this, RES_ARMOR, bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9)); }
1269 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1270 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1271 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1272 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1273 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1275 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1277 Monster_Miniboss_Check(this);
1278 SetResourceExplicit(this, RES_HEALTH, GetResource(this, RES_HEALTH) * MONSTER_SKILLMOD(this));
1281 this.skin = rint(random() * 4);
1284 this.max_health = GetResource(this, RES_HEALTH);
1285 this.pain_finished = this.nextthink;
1286 this.last_enemycheck = this.spawn_time + random(); // slight delay
1288 if(IS_PLAYER(this.monster_follow))
1289 this.effects |= EF_DIMLIGHT;
1291 if(!this.wander_delay) { this.wander_delay = 2; }
1292 if(!this.wander_distance) { this.wander_distance = 600; }
1294 Monster_Sounds_Precache(this);
1295 Monster_Sounds_Update(this);
1299 if(!this.monster_attack)
1300 IL_PUSH(g_monster_targets, this);
1301 this.monster_attack = true; // we can have monster enemies in team games
1304 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1306 if(autocvar_g_monsters_healthbars)
1308 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1309 wp.wp_extra = this.monsterdef.monsterid;
1310 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1311 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1313 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1314 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1318 setthink(this, Monster_Think);
1319 this.nextthink = time + this.ticrate;
1321 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1327 bool Monster_Spawn(entity this, bool check_appear, Monster mon)
1329 // setup the basic required properties for a monster
1331 if(!mon || mon == MON_Null) { return false; } // invalid monster
1332 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1334 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED) && !(this.flags & FL_MONSTER))
1336 IL_PUSH(g_monsters, this);
1337 if(this.mdl && this.mdl != "")
1338 precache_model(this.mdl);
1339 if(this.mdl_dead && this.mdl_dead != "")
1340 precache_model(this.mdl_dead);
1343 if(check_appear && Monster_Appear_Check(this, mon)) { return true; } // return true so the monster isn't removed
1345 if(!this.monster_skill)
1346 this.monster_skill = cvar("g_monsters_skill");
1348 // support for quake style removing monsters based on skill
1349 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1350 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1351 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1353 if(this.team && !teamplay)
1356 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1357 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1358 monsters_total += 1;
1360 if(this.mdl && this.mdl != "")
1361 _setmodel(this, this.mdl);
1363 setmodel(this, mon.m_model);
1365 if(!this.monster_name || this.monster_name == "")
1366 this.monster_name = mon.monster_name;
1368 if(this.statuseffects && this.statuseffects.owner == this)
1370 StatusEffects_clearall(this.statuseffects);
1371 StatusEffects_update(this);
1374 this.statuseffects = NULL;
1376 this.flags = FL_MONSTER;
1377 this.classname = "monster";
1378 this.takedamage = DAMAGE_AIM;
1379 if(!this.bot_attack)
1380 IL_PUSH(g_bot_targets, this);
1381 this.bot_attack = true;
1382 this.iscreature = true;
1383 this.teleportable = true;
1384 if(!this.damagedbycontents)
1385 IL_PUSH(g_damagedbycontents, this);
1386 this.damagedbycontents = true;
1387 this.monsterdef = mon;
1388 this.event_damage = Monster_Damage;
1389 this.event_heal = Monster_Heal;
1390 settouch(this, Monster_Touch);
1391 this.use = Monster_Use;
1392 this.solid = SOLID_BBOX;
1393 set_movetype(this, MOVETYPE_WALK);
1394 StatusEffects_apply(STATUSEFFECT_SpawnShield, this, time + autocvar_g_monsters_spawnshieldtime, 0);
1396 this.velocity = '0 0 0';
1397 this.moveto = this.origin;
1398 this.pos1 = this.origin;
1399 this.pos2 = this.angles;
1400 this.reset = Monster_Reset;
1401 this.netname = mon.netname;
1402 this.monster_attackfunc = mon.monster_attackfunc;
1403 this.candrop = true;
1404 this.oldtarget2 = this.target2;
1405 this.deadflag = DEAD_NO;
1406 this.spawn_time = time;
1408 this.monster_moveto = '0 0 0';
1409 this.monster_face = '0 0 0';
1410 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1412 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1413 if(!this.scale) { this.scale = 1; }
1414 if(autocvar_g_monsters_edit) { this.grab = 1; }
1415 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1416 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1417 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1419 if(autocvar_g_monsters_playerclip_collisions)
1420 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1422 if(mon.spawnflags & MONSTER_TYPE_FLY)
1424 this.flags |= FL_FLY;
1425 set_movetype(this, MOVETYPE_FLY);
1428 if((mon.spawnflags & MONSTER_SIZE_QUAKE) && autocvar_g_monsters_quake_resize && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
1431 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1432 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1434 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1436 Monster_UpdateModel(this);
1438 if(!Monster_Spawn_Setup(this))
1440 Monster_Remove(this);
1446 setorigin(this, this.origin + '0 0 20');
1447 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1448 setorigin(this, trace_endpos);
1451 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1452 monster_setupcolors(this);
1454 CSQCMODEL_AUTOINIT(this);