1 #include "sv_monsters.qh"
3 #include <lib/warpzone/common.qh>
4 #include "../constants.qh"
8 #include "../physics/movelib.qh"
9 #include "../weapons/_mod.qh"
10 #include <server/autocvars.qh>
11 #include <common/weapons/_all.qh>
12 #include <common/stats.qh>
13 #include <server/g_damage.qh>
14 #include "../deathtypes/all.qh"
15 #include <server/items/items.qh>
16 #include <server/mutators/_mod.qh>
17 #include <server/steerlib.qh>
18 #include <server/sv_main.qh>
19 #include "../turrets/sv_turrets.qh"
20 #include "../turrets/util.qh"
21 #include "../vehicles/all.qh"
22 #include <server/campaign.qh>
23 #include <server/cheats.qh>
24 #include <server/command/_mod.qh>
25 #include <common/items/_mod.qh>
26 #include <common/mapobjects/teleporters.qh>
27 #include "../mapobjects/triggers.qh"
28 #include <lib/csqcmodel/sv_model.qh>
29 #include <server/round_handler.qh>
30 #include <server/weapons/_mod.qh>
32 void monsters_setstatus(entity this)
34 STAT(MONSTERS_TOTAL, this) = monsters_total;
35 STAT(MONSTERS_KILLED, this) = monsters_killed;
38 void monster_dropitem(entity this, entity attacker)
40 if(!this.candrop || !this.monster_loot)
43 vector org = CENTER_OR_VIEWOFS(this);
45 Item_SetLoot(e, true);
46 e.spawnfunc_checked = true;
48 e.monster_loot = this.monster_loot;
50 MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
51 e = M_ARGV(1, entity);
53 if(e && e.monster_loot)
56 StartItem(e, e.monster_loot);
59 e.velocity = randomvec() * 175 + '0 0 325';
60 e.item_spawnshieldtime = time + 0.7;
61 SUB_SetFade(e, time + autocvar_g_monsters_drop_time, 1);
65 void monster_makevectors(entity this, entity targ)
69 vector v = targ.origin + (targ.mins + targ.maxs) * 0.5;
70 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
71 this.v_angle_x = -this.v_angle_x;
74 makevectors(this.v_angle);
81 bool Monster_ValidTarget(entity this, entity targ)
83 // ensure we're not checking nonexistent monster/target
84 if(!this || !targ) { return false; }
87 || (autocvar_g_monsters_lineofsight && !checkpvs(this.origin + this.view_ofs, targ)) // enemy cannot be seen
88 || (IS_VEHICLE(targ) && !(this.monsterdef.spawnflags & MON_FLAG_RANGED)) // melee vs vehicle is useless
89 || (time < game_starttime) // monsters do nothing before match has started
90 || (targ.takedamage == DAMAGE_NO)
92 || (targ.items & IT_INVISIBILITY)
93 || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
94 || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || GetResource(targ, RES_HEALTH) <= 0 || GetResource(this, RES_HEALTH) <= 0))
95 || (this.monster_follow == targ || targ.monster_follow == this)
96 || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
97 || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
98 || (SAME_TEAM(targ, this))
99 || (STAT(FROZEN, targ))
100 || (targ.alpha != 0 && targ.alpha < 0.5)
101 || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
104 // if any of the above checks fail, target is not valid
108 vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
109 traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this);
111 if(trace_fraction < 1 && trace_ent != targ)
112 return false; // solid
114 if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
115 if(this.enemy != targ)
117 makevectors (this.angles);
118 float dot = normalize (targ.origin - this.origin) * v_forward;
120 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
123 return true; // this target is valid!
126 entity Monster_FindTarget(entity this)
128 if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
130 entity closest_target = NULL;
131 vector my_center = CENTER_OR_VIEWOFS(this);
133 // find the closest acceptable target to pass to
134 IL_EACH(g_monster_targets, it.monster_attack && vdist(it.origin - this.origin, <, this.target_range),
136 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; }
151 return closest_target;
154 void monster_setupcolors(entity this)
156 if(IS_PLAYER(this.realowner))
157 this.colormap = this.realowner.colormap;
158 else if(teamplay && this.team)
159 this.colormap = 1024 + (this.team - 1) * 17;
162 if(this.monster_skill <= MONSTER_SKILL_EASY)
163 this.colormap = 1029;
164 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
165 this.colormap = 1027;
166 else if(this.monster_skill <= MONSTER_SKILL_HARD)
167 this.colormap = 1038;
168 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
169 this.colormap = 1028;
170 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
171 this.colormap = 1032;
173 this.colormap = 1024;
177 void monster_changeteam(entity this, int newteam)
179 if(!teamplay) { return; }
182 if(!this.monster_attack)
183 IL_PUSH(g_monster_targets, this);
184 this.monster_attack = true; // new team, activate attacking
185 monster_setupcolors(this);
189 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
191 this.sprite.team = newteam;
192 this.sprite.SendFlags |= 1;
196 .void(entity) monster_delayedfunc;
197 void Monster_Delay_Action(entity this)
199 if(Monster_ValidTarget(this.owner, this.owner.enemy))
201 monster_makevectors(this.owner, this.owner.enemy);
202 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 = this.monsterdef.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) strfree(this.monstersound_##m);
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)
324 strcpy(this.(field), strcat(argv(1), " ", argv(2)));
330 .int skin_for_monstersound;
331 void Monster_Sounds_Update(entity this)
333 if(this.skin == this.skin_for_monstersound) { return; }
335 this.skin_for_monstersound = this.skin;
336 Monster_Sounds_Clear(this);
337 if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
338 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
341 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
343 if(!autocvar_g_monsters_sounds) { return; }
346 if(time < this.msound_delay)
348 string sample = this.(samplefield);
349 if (sample != "") sample = GlobalSound_sample(sample, random());
350 float myscale = ((this.scale) ? this.scale : 1); // safety net
351 // TODO: change volume depending on size too?
352 sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
354 this.msound_delay = time + sound_delay;
358 // =======================
359 // Monster attack handlers
360 // =======================
362 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
364 if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
366 setanim(this, anim, false, true, false);
368 if(this.animstate_endtime > time && IS_MONSTER(this))
369 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
371 this.attack_finished_single[0] = this.anim_finished = time + animtime;
373 traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
375 if(trace_ent.takedamage)
376 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
381 bool Monster_Attack_Leap_Check(entity this, vector vel)
383 if(this.state && IS_MONSTER(this))
384 return false; // already attacking
385 if(!IS_ONGROUND(this))
386 return false; // not on the ground
387 if(GetResource(this, RES_HEALTH) <= 0 || IS_DEAD(this))
388 return false; // called when dead?
389 if(time < this.attack_finished_single[0])
390 return false; // still attacking
392 vector old = this.velocity;
395 tracetoss(this, this);
397 if(trace_ent != this.enemy)
403 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
405 if(!Monster_Attack_Leap_Check(this, vel))
408 setanim(this, anm, false, true, false);
410 if(this.animstate_endtime > time && IS_MONSTER(this))
411 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
413 this.attack_finished_single[0] = this.anim_finished = time + animtime;
416 this.state = MONSTER_ATTACK_RANGED;
417 settouch(this, touchfunc);
420 UNSET_ONGROUND(this);
425 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
427 int slot = weaponslot(weaponentity);
430 || (!this.monster_attackfunc)
431 || (time < this.attack_finished_single[slot])
434 if(vdist(targ.origin - this.origin, <=, this.attack_range))
436 monster_makevectors(this, targ);
437 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
438 if(attack_success == 1)
439 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
440 else if(attack_success > 0)
444 if(vdist(targ.origin - this.origin, >, this.attack_range))
446 monster_makevectors(this, targ);
447 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
448 if(attack_success == 1)
449 Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
450 else if(attack_success > 0)
456 // ======================
457 // Main monster functions
458 // ======================
460 void Monster_UpdateModel(entity this)
462 // assume some defaults
463 /*this.anim_idle = animfixfps(this, '0 1 0.01', '0 0 0');
464 this.anim_walk = animfixfps(this, '1 1 0.01', '0 0 0');
465 this.anim_run = animfixfps(this, '2 1 0.01', '0 0 0');
466 this.anim_fire1 = animfixfps(this, '3 1 0.01', '0 0 0');
467 this.anim_fire2 = animfixfps(this, '4 1 0.01', '0 0 0');
468 this.anim_melee = animfixfps(this, '5 1 0.01', '0 0 0');
469 this.anim_pain1 = animfixfps(this, '6 1 0.01', '0 0 0');
470 this.anim_pain2 = animfixfps(this, '7 1 0.01', '0 0 0');
471 this.anim_die1 = animfixfps(this, '8 1 0.01', '0 0 0');
472 this.anim_die2 = animfixfps(this, '9 1 0.01', '0 0 0');*/
474 // then get the real values
475 Monster mon = this.monsterdef;
476 mon.mr_anim(mon, this);
479 void Monster_Touch(entity this, entity toucher)
481 if(toucher == NULL) { return; }
483 if(toucher.monster_attack)
484 if(this.enemy != toucher)
485 if(!IS_MONSTER(toucher))
486 if(Monster_ValidTarget(this, toucher))
487 this.enemy = toucher;
490 void Monster_Miniboss_Check(entity this)
492 if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
495 float chance = random() * 100;
497 // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
498 if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
500 GiveResource(this, RES_HEALTH, autocvar_g_monsters_miniboss_healthboost);
501 this.effects |= EF_RED;
503 this.weapon = WEP_VORTEX.m_id;
507 bool Monster_Respawn_Check(entity this)
509 if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
510 if(MUTATOR_CALLHOOK(MonsterRespawn, this))
511 return true; // enabled by a mutator
513 if(this.spawnflags & MONSTERFLAG_NORESPAWN)
516 if(!autocvar_g_monsters_respawn)
522 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterdef); }
526 void Monster_Dead_Fade(entity this)
528 if(Monster_Respawn_Check(this))
530 this.spawnflags |= MONSTERFLAG_RESPAWNED;
531 setthink(this, Monster_Respawn);
532 this.nextthink = time + this.respawntime;
533 this.monster_lifetime = 0;
534 this.deadflag = DEAD_RESPAWNING;
535 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
537 this.pos1 = this.origin;
538 this.pos2 = this.angles;
540 this.event_damage = func_null;
541 this.event_heal = func_null;
542 this.takedamage = DAMAGE_NO;
543 setorigin(this, this.pos1);
544 this.angles = this.pos2;
545 SetResourceExplicit(this, RES_HEALTH, this.max_health);
546 setmodel(this, MDL_Null);
550 // number of monsters spawned with mobspawn command
553 SUB_SetFade(this, time + 3, 1);
557 void Monster_Use(entity this, entity actor, entity trigger)
559 if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
562 .float pass_distance;
563 vector Monster_Move_Target(entity this, entity targ)
565 // enemy is always preferred target
568 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
569 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
570 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
572 // cases where the enemy may have changed their state (don't need to check everything here)
574 || (IS_DEAD(this.enemy) || GetResource(this.enemy, RES_HEALTH) < 1)
575 || (STAT(FROZEN, this.enemy))
576 || (this.enemy.flags & FL_NOTARGET)
577 || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
578 || (this.enemy.takedamage == DAMAGE_NO)
579 || (vdist(this.origin - targ_origin, >, this.target_range))
580 || ((trace_fraction < 1) && (trace_ent != this.enemy)))
583 //this.pass_distance = 0;
588 /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
589 print("Trace origin: ", vtos(targ_origin), "\n");
590 print("Target origin: ", vtos(this.enemy.origin), "\n");
591 print("My origin: ", vtos(this.origin), "\n"); */
593 this.monster_movestate = MONSTER_MOVE_ENEMY;
594 this.last_trace = time + 1.2;
595 if(this.monster_moveto)
596 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
601 /*makevectors(this.angles);
602 this.monster_movestate = MONSTER_MOVE_ENEMY;
603 this.last_trace = time + 1.2;
604 return this.enemy.origin; */
607 switch(this.monster_moveflags)
609 case MONSTER_MOVE_FOLLOW:
611 this.monster_movestate = MONSTER_MOVE_FOLLOW;
612 this.last_trace = time + 0.3;
613 return (this.monster_follow) ? this.monster_follow.origin : this.origin;
615 case MONSTER_MOVE_SPAWNLOC:
617 this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
618 this.last_trace = time + 2;
621 case MONSTER_MOVE_NOMOVE:
623 if(this.monster_moveto)
625 this.last_trace = time + 0.5;
626 return this.monster_moveto;
630 this.monster_movestate = MONSTER_MOVE_NOMOVE;
631 this.last_trace = time + 2;
636 case MONSTER_MOVE_WANDER:
639 this.monster_movestate = MONSTER_MOVE_WANDER;
641 if(this.monster_moveto)
643 this.last_trace = time + 0.5;
644 pos = this.monster_moveto;
648 this.last_trace = time + 0.5;
653 this.last_trace = time + this.wander_delay;
655 this.angles_y = rint(random() * 500);
656 makevectors(this.angles);
657 pos = this.origin + v_forward * this.wander_distance;
659 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
661 pos.z = random() * 200;
672 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
674 //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
675 //float initial_height = 0; //min(50, (targ_distance * tanh(20)));
676 //float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
677 //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
681 if(current_height) // make sure we can actually do this arcing path
683 targpos = (to + ('0 0 1' * current_height));
684 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
685 if(trace_fraction < 1)
687 //print("normal arc line failed, trying to find new pos...");
688 WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
689 targpos = (trace_endpos + '0 0 -10');
690 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
691 if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
692 /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
695 else { targpos = to; }
698 //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
700 vector desired_direction = normalize(targpos - from);
701 if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
702 else { this.velocity = (desired_direction * movespeed); }
704 //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
705 //this.angles = vectoangles(this.velocity);
710 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
712 // update goal entity if lost
713 if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
714 this.goalentity = find(NULL, targetname, this.target2);
716 if(STAT(FROZEN, this))
718 movelib_brake_simple(this, stpspeed);
719 setanim(this, this.anim_idle, true, false, false);
720 return; // no physics while frozen!
723 if(this.flags & FL_SWIM)
725 if(this.waterlevel < WATERLEVEL_WETFEET)
727 if(time >= this.last_trace)
729 this.last_trace = time + 0.4;
731 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
732 this.angles = '90 90 0';
735 this.velocity_y += random() * 50;
736 this.velocity_x -= random() * 50;
740 this.velocity_y -= random() * 50;
741 this.velocity_x += random() * 50;
743 this.velocity_z += random() * 150;
747 set_movetype(this, MOVETYPE_BOUNCE);
748 //this.velocity_z = -200;
752 else if(this.move_movetype == MOVETYPE_BOUNCE)
755 set_movetype(this, MOVETYPE_WALK);
759 entity targ = this.goalentity;
761 if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
763 || this.draggedby != NULL
764 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
765 || time < game_starttime
766 || (autocvar_g_campaign && !campaign_bots_may_start)
767 || time < this.spawn_time)
769 runspeed = walkspeed = 0;
770 if(time >= this.spawn_time)
771 setanim(this, this.anim_idle, true, false, false);
772 movelib_brake_simple(this, stpspeed);
776 targ = M_ARGV(3, entity);
777 runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
778 walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
780 if(teamplay && autocvar_g_monsters_teams)
781 if(DIFF_TEAM(this.monster_follow, this))
782 this.monster_follow = NULL;
784 if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
787 settouch(this, Monster_Touch);
790 if(this.state && time >= this.attack_finished_single[0])
791 this.state = 0; // attack is over
793 if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
794 if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
795 this.moveto = Monster_Move_Target(this, targ);
798 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
800 if(this.state == MONSTER_ATTACK_MELEE)
801 this.moveto = this.origin;
803 if(this.enemy && this.enemy.vehicle)
806 if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
807 this.moveto_z = this.origin_z;
809 if(vdist(this.origin - this.moveto, >, 100))
811 bool do_run = (this.enemy || this.monster_moveto);
812 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
813 Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
815 if(time > this.pain_finished && time > this.anim_finished) // TODO: use anim_finished instead!?
818 if(vdist(this.velocity, >, 10))
819 setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
821 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.monsterdef);
891 bool Monster_Appear_Check(entity this, Monster monster_id)
893 if(!(this.spawnflags & MONSTERFLAG_APPEAR))
896 setthink(this, func_null);
897 this.monsterdef = 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, false); // remove any icy remains
912 SetResourceExplicit(this, RES_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 TakeResource(this, RES_HEALTH, damage);
924 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
926 if(GetResource(this, RES_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))
946 Unfreeze(this, false); // remove any icy remains
948 monster_dropitem(this, attacker);
950 Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
952 if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
953 monsters_killed += 1;
955 if(IS_PLAYER(attacker))
956 if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
957 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
961 // number of monsters spawned with mobspawn command
965 if(!gibbed && this.mdl_dead && this.mdl_dead != "")
966 _setmodel(this, this.mdl_dead);
968 this.event_damage = ((gibbed) ? func_null : Monster_Dead_Damage);
969 this.event_heal = func_null;
970 this.solid = SOLID_CORPSE;
971 this.takedamage = DAMAGE_AIM;
972 this.deadflag = DEAD_DEAD;
974 set_movetype(this, MOVETYPE_TOSS);
975 this.moveto = this.origin;
976 settouch(this, Monster_Touch); // reset incase monster was pouncing
977 this.reset = func_null;
979 this.attack_finished_single[0] = 0;
982 if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
983 this.velocity = '0 0 0';
985 CSQCModel_UnlinkEntity(this);
987 Monster mon = this.monsterdef;
988 mon.mr_death(mon, this);
990 if(this.candrop && this.weapon)
992 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
993 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
997 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
999 if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
1002 if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
1005 //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
1008 if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1011 if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
1014 vector v = healtharmor_applydamage(100, GetResource(this, RES_ARMOR) / 100, deathtype, damage);
1018 Monster mon = this.monsterdef;
1019 take = mon.mr_pain(mon, this, take, attacker, deathtype);
1023 TakeResource(this, RES_HEALTH, take);
1024 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1028 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1030 this.dmg_time = time;
1032 if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1033 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
1035 this.velocity += force * this.damageforcescale;
1037 if(deathtype != DEATH_DROWN.m_id && take)
1039 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1041 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1043 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1046 if(GetResource(this, RES_HEALTH) <= 0)
1048 if(deathtype == DEATH_KILL.m_id)
1049 this.candrop = false; // killed by mobkill command
1052 SUB_UseTargets(this, attacker, this.enemy);
1053 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1055 Monster_Dead(this, attacker, (GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id));
1057 WaypointSprite_Kill(this.sprite);
1059 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1061 if(GetResource(this, RES_HEALTH) <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1063 Violence_GibSplash(this, 1, 0.5, attacker);
1065 setthink(this, SUB_Remove);
1066 this.nextthink = time + 0.1;
1071 bool Monster_Heal(entity targ, entity inflictor, float amount, float limit)
1073 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
1074 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
1077 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
1079 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
1083 // don't check for enemies, just keep walking in a straight line
1084 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1086 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)
1089 if(time >= this.spawn_time)
1090 setanim(this, this.anim_idle, true, false, false);
1091 movelib_brake_simple(this, 0.6);
1095 makevectors(this.angles);
1096 vector a = CENTER_OR_VIEWOFS(this);
1097 vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1099 traceline(a, b, MOVE_NORMAL, this);
1101 bool reverse = false;
1102 if(trace_fraction != 1.0)
1104 if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1106 if(trace_ent && IS_MONSTER(trace_ent))
1109 if(!allow_jumpoff && IS_ONGROUND(this))
1111 traceline(b, b - '0 0 32', MOVE_NORMAL, this);
1112 if(trace_fraction == 1.0)
1118 this.angles_y = anglemods(this.angles_y - 180);
1119 makevectors(this.angles);
1122 movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1124 if(time > this.pain_finished && time > this.attack_finished_single[0])
1126 if(vdist(this.velocity, >, 10))
1127 setanim(this, this.anim_walk, true, false, false);
1129 setanim(this, this.anim_idle, true, false, false);
1133 void Monster_Anim(entity this)
1135 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1140 // Decide on which death animation to use.
1142 deadbits = ANIMSTATE_DEAD1;
1144 deadbits = ANIMSTATE_DEAD2;
1149 // Clear a previous death animation.
1152 int animbits = deadbits;
1153 if(STAT(FROZEN, this))
1154 animbits |= ANIMSTATE_FROZEN;
1156 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1157 animdecide_setstate(this, animbits, false);
1158 animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1160 /* // weapon entities for monsters?
1161 if (this.weaponentity)
1163 updateanim(this.weaponentity);
1164 if (!this.weaponentity.animstate_override)
1165 setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1170 void Monster_Frozen_Think(entity this)
1172 if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
1174 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1175 SetResourceExplicit(this, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * this.max_health));
1177 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1179 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1180 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1182 if(STAT(REVIVE_PROGRESS, this) >= 1)
1183 Unfreeze(this, false);
1185 else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
1187 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1188 SetResourceExplicit(this, RES_HEALTH, max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
1190 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1191 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1193 if(GetResource(this, RES_HEALTH) < 1)
1195 Unfreeze(this, false);
1196 if(this.event_damage)
1197 this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
1199 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1200 Unfreeze(this, false);
1202 // otherwise, no revival!
1204 this.enemy = NULL; // TODO: save enemy, and attack when revived?
1207 void Monster_Enemy_Check(entity this)
1211 this.enemy = Monster_FindTarget(this);
1214 WarpZone_RefSys_Copy(this.enemy, this);
1215 WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1216 // update move target immediately?
1217 this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1218 this.monster_moveto = '0 0 0';
1219 this.monster_face = '0 0 0';
1221 //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)));
1222 Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1227 void Monster_Think(entity this)
1229 setthink(this, Monster_Think);
1230 this.nextthink = time + this.ticrate;
1232 if(this.monster_lifetime && time >= this.monster_lifetime)
1234 Damage(this, this, this, GetResource(this, RES_HEALTH) + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1238 if(STAT(FROZEN, this))
1239 Monster_Frozen_Think(this);
1240 else if(time >= this.last_enemycheck)
1242 Monster_Enemy_Check(this);
1243 this.last_enemycheck = time + 1; // check for enemies every second
1246 Monster mon = this.monsterdef;
1247 if(mon.mr_think(mon, this))
1249 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1251 .entity weaponentity = weaponentities[0]; // TODO?
1252 Monster_Attack_Check(this, this.enemy, weaponentity);
1257 CSQCMODEL_AUTOUPDATE(this);
1260 bool Monster_Spawn_Setup(entity this)
1262 Monster mon = this.monsterdef;
1263 mon.mr_setup(mon, this);
1265 // ensure some basic needs are met
1266 if(!GetResource(this, RES_HEALTH)) { SetResourceExplicit(this, RES_HEALTH, 100); }
1267 if(!GetResource(this, RES_ARMOR)) { SetResourceExplicit(this, RES_ARMOR, bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9)); }
1268 if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1269 if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1270 if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1271 if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1272 if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1274 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1276 Monster_Miniboss_Check(this);
1277 SetResourceExplicit(this, RES_HEALTH, GetResource(this, RES_HEALTH) * MONSTER_SKILLMOD(this));
1280 this.skin = rint(random() * 4);
1283 this.max_health = GetResource(this, RES_HEALTH);
1284 this.pain_finished = this.nextthink;
1286 if(IS_PLAYER(this.monster_follow))
1287 this.effects |= EF_DIMLIGHT;
1289 if(!this.wander_delay) { this.wander_delay = 2; }
1290 if(!this.wander_distance) { this.wander_distance = 600; }
1292 Monster_Sounds_Precache(this);
1293 Monster_Sounds_Update(this);
1297 if(!this.monster_attack)
1298 IL_PUSH(g_monster_targets, this);
1299 this.monster_attack = true; // we can have monster enemies in team games
1302 Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1304 if(autocvar_g_monsters_healthbars)
1306 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1307 wp.wp_extra = this.monsterdef.monsterid;
1308 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1309 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1311 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1312 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
1316 setthink(this, Monster_Think);
1317 this.nextthink = time + this.ticrate;
1319 if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1325 bool Monster_Spawn(entity this, bool check_appear, Monster mon)
1327 // setup the basic required properties for a monster
1329 if(!mon || mon == MON_Null) { return false; } // invalid monster
1330 if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1332 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED) && !(this.flags & FL_MONSTER))
1334 IL_PUSH(g_monsters, this);
1335 if(this.mdl && this.mdl != "")
1336 precache_model(this.mdl);
1337 if(this.mdl_dead && this.mdl_dead != "")
1338 precache_model(this.mdl_dead);
1341 if(check_appear && Monster_Appear_Check(this, mon)) { return true; } // return true so the monster isn't removed
1343 if(!this.monster_skill)
1344 this.monster_skill = cvar("g_monsters_skill");
1346 // support for quake style removing monsters based on skill
1347 if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1348 if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1349 if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1351 if(this.team && !teamplay)
1354 if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1355 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1356 monsters_total += 1;
1358 if(this.mdl && this.mdl != "")
1359 _setmodel(this, this.mdl);
1361 setmodel(this, mon.m_model);
1363 this.flags = FL_MONSTER;
1364 this.classname = "monster";
1365 this.takedamage = DAMAGE_AIM;
1366 if(!this.bot_attack)
1367 IL_PUSH(g_bot_targets, this);
1368 this.bot_attack = true;
1369 this.iscreature = true;
1370 this.teleportable = true;
1371 if(!this.damagedbycontents)
1372 IL_PUSH(g_damagedbycontents, this);
1373 this.damagedbycontents = true;
1374 this.monsterdef = mon;
1375 this.event_damage = Monster_Damage;
1376 this.event_heal = Monster_Heal;
1377 settouch(this, Monster_Touch);
1378 this.use = Monster_Use;
1379 this.solid = SOLID_BBOX;
1380 set_movetype(this, MOVETYPE_WALK);
1381 this.spawnshieldtime = time + autocvar_g_monsters_spawnshieldtime;
1383 this.velocity = '0 0 0';
1384 this.moveto = this.origin;
1385 this.pos1 = this.origin;
1386 this.pos2 = this.angles;
1387 this.reset = Monster_Reset;
1388 this.netname = mon.netname;
1389 this.monster_attackfunc = mon.monster_attackfunc;
1390 this.monster_name = mon.monster_name;
1391 this.candrop = true;
1392 this.view_ofs = '0 0 0.7' * (this.maxs_z * 0.5);
1393 this.oldtarget2 = this.target2;
1394 //this.pass_distance = 0;
1395 this.deadflag = DEAD_NO;
1396 this.spawn_time = time;
1398 this.monster_moveto = '0 0 0';
1399 this.monster_face = '0 0 0';
1400 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1402 if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1403 if(!this.scale) { this.scale = 1; }
1404 if(autocvar_g_monsters_edit) { this.grab = 1; }
1405 if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1406 if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1407 if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1409 if(autocvar_g_playerclip_collisions)
1410 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1412 if(mon.spawnflags & MONSTER_TYPE_FLY)
1414 this.flags |= FL_FLY;
1415 set_movetype(this, MOVETYPE_FLY);
1418 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1420 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1423 if(mon.spawnflags & MONSTER_SIZE_QUAKE)
1424 if(autocvar_g_monsters_quake_resize)
1428 setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1430 this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1432 Monster_UpdateModel(this);
1434 if(!Monster_Spawn_Setup(this))
1436 Monster_Remove(this);
1442 setorigin(this, this.origin + '0 0 20');
1443 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1444 setorigin(this, trace_endpos);
1447 if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1448 monster_setupcolors(this);
1450 CSQCMODEL_AUTOINIT(this);