]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/monsters/sv_monsters.qc
Unhardcode vaporizer primary force and fix monsters targeting players after the match...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / sv_monsters.qc
1 #include "sv_monsters.qh"
2
3 #include <server/g_subs.qh>
4 #include <lib/warpzone/common.qh>
5 #include "../constants.qh"
6 #include "../teams.qh"
7 #include "../util.qh"
8 #include "all.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>
25
26 void monsters_setstatus(entity this)
27 {
28         STAT(MONSTERS_TOTAL, this) = monsters_total;
29         STAT(MONSTERS_KILLED, this) = monsters_killed;
30 }
31
32 void monster_dropitem(entity this, entity attacker)
33 {
34         if(!this.candrop || !this.monster_loot)
35                 return;
36
37         vector org = CENTER_OR_VIEWOFS(this);
38         entity e = spawn();
39         Item_SetLoot(e, true);
40         e.spawnfunc_checked = true;
41
42         e.monster_loot = this.monster_loot;
43
44         MUTATOR_CALLHOOK(MonsterDropItem, this, e, attacker);
45         e = M_ARGV(1, entity);
46
47         if(e && e.monster_loot)
48         {
49                 e.noalign = true;
50                 StartItem(e, e.monster_loot);
51                 e.gravity = 1;
52                 setorigin(e, org);
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);
56         }
57 }
58
59 void monster_makevectors(entity this, entity targ)
60 {
61         if(IS_MONSTER(this))
62         {
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;
66         }
67
68         makevectors(this.v_angle);
69 }
70
71 // ===============
72 // Target handling
73 // ===============
74
75 bool Monster_ValidTarget(entity this, entity targ)
76 {
77         // ensure we're not checking nonexistent monster/target
78         if(!this || !targ) { return false; }
79
80         if((targ == this)
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         || (game_stopped)
86         || (targ.items & IT_INVISIBILITY)
87         || (IS_SPEC(targ) || IS_OBSERVER(targ)) // don't attack spectators
88         || (!IS_VEHICLE(targ) && (IS_DEAD(targ) || IS_DEAD(this) || targ.health <= 0 || this.health <= 0))
89         || (this.monster_follow == targ || targ.monster_follow == this)
90         || (!IS_VEHICLE(targ) && (targ.flags & FL_NOTARGET))
91         || (!autocvar_g_monsters_typefrag && PHYS_INPUT_BUTTON_CHAT(targ))
92         || (SAME_TEAM(targ, this))
93         || (STAT(FROZEN, targ))
94         || (targ.alpha != 0 && targ.alpha < 0.5)
95         || (MUTATOR_CALLHOOK(MonsterValidTarget, this, targ))
96         )
97         {
98                 // if any of the above checks fail, target is not valid
99                 return false;
100         }
101
102         vector targ_origin = ((targ.absmin + targ.absmax) * 0.5);
103         traceline(this.origin + this.view_ofs, targ_origin, MOVE_NOMONSTERS, this);
104
105         if(trace_fraction < 1 && trace_ent != targ)
106                 return false; // solid
107
108         if(autocvar_g_monsters_target_infront || (this.spawnflags & MONSTERFLAG_INFRONT))
109         if(this.enemy != targ)
110         {
111                 makevectors (this.angles);
112                 float dot = normalize (targ.origin - this.origin) * v_forward;
113
114                 if(dot <= autocvar_g_monsters_target_infront_range) { return false; }
115         }
116
117         return true; // this target is valid!
118 }
119
120 entity Monster_FindTarget(entity this)
121 {
122         if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return this.enemy; } // Handled by a mutator
123
124         entity closest_target = NULL;
125         vector my_center = CENTER_OR_VIEWOFS(this);
126
127         // find the closest acceptable target to pass to
128         IL_EACH(g_monster_targets, it.monster_attack && vdist(it.origin - this.origin, <, this.target_range),
129         {
130                 if(Monster_ValidTarget(this, it))
131                 {
132                         // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
133                         vector targ_center = CENTER_OR_VIEWOFS(it);
134
135                         if(closest_target)
136                         {
137                                 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
138                                 if(vlen2(my_center - targ_center) < vlen2(my_center - closest_target_center))
139                                         { closest_target = it; }
140                         }
141                         else { closest_target = it; }
142                 }
143         });
144
145         return closest_target;
146 }
147
148 void monster_setupcolors(entity this)
149 {
150         if(IS_PLAYER(this.realowner))
151                 this.colormap = this.realowner.colormap;
152         else if(teamplay && this.team)
153                 this.colormap = 1024 + (this.team - 1) * 17;
154         else
155         {
156                 if(this.monster_skill <= MONSTER_SKILL_EASY)
157                         this.colormap = 1029;
158                 else if(this.monster_skill <= MONSTER_SKILL_MEDIUM)
159                         this.colormap = 1027;
160                 else if(this.monster_skill <= MONSTER_SKILL_HARD)
161                         this.colormap = 1038;
162                 else if(this.monster_skill <= MONSTER_SKILL_INSANE)
163                         this.colormap = 1028;
164                 else if(this.monster_skill <= MONSTER_SKILL_NIGHTMARE)
165                         this.colormap = 1032;
166                 else
167                         this.colormap = 1024;
168         }
169 }
170
171 void monster_changeteam(entity this, int newteam)
172 {
173         if(!teamplay) { return; }
174
175         this.team = newteam;
176         if(!this.monster_attack)
177                 IL_PUSH(g_monster_targets, this);
178         this.monster_attack = true; // new team, activate attacking
179         monster_setupcolors(this);
180
181         if(this.sprite)
182         {
183                 WaypointSprite_UpdateTeamRadar(this.sprite, RADARICON_DANGER, ((newteam) ? Team_ColorRGB(newteam) : '1 0 0'));
184
185                 this.sprite.team = newteam;
186                 this.sprite.SendFlags |= 1;
187         }
188 }
189
190 .void(entity) monster_delayedfunc;
191 void Monster_Delay_Action(entity this)
192 {
193         if(Monster_ValidTarget(this.owner, this.owner.enemy)) { this.monster_delayedfunc(this.owner); }
194
195         if(this.cnt > 1)
196         {
197                 this.cnt -= 1;
198                 setthink(this, Monster_Delay_Action);
199                 this.nextthink = time + this.count;
200         }
201         else
202         {
203                 setthink(this, SUB_Remove);
204                 this.nextthink = time;
205         }
206 }
207
208 void Monster_Delay(entity this, int repeat_count, float defer_amnt, void(entity) func)
209 {
210         // deferred attacking, checks if monster is still alive and target is still valid before attacking
211         entity e = spawn();
212
213         setthink(e, Monster_Delay_Action);
214         e.nextthink = time + defer_amnt;
215         e.count = defer_amnt;
216         e.owner = this;
217         e.monster_delayedfunc = func;
218         e.cnt = repeat_count;
219 }
220
221
222 // ==============
223 // Monster sounds
224 // ==============
225
226 string get_monster_model_datafilename(string m, float sk, string fil)
227 {
228         if(m)
229                 m = strcat(m, "_");
230         else
231                 m = "models/monsters/*_";
232         if(sk >= 0)
233                 m = strcat(m, ftos(sk));
234         else
235                 m = strcat(m, "*");
236         return strcat(m, ".", fil);
237 }
238
239 void Monster_Sound_Precache(string f)
240 {
241         float fh;
242         string s;
243         fh = fopen(f, FILE_READ);
244         if(fh < 0)
245                 return;
246         while((s = fgets(fh)))
247         {
248                 if(tokenize_console(s) != 3)
249                 {
250                         //LOG_DEBUG("Invalid sound info line: ", s); // probably a comment, no need to spam warnings
251                         continue;
252                 }
253                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
254         }
255         fclose(fh);
256 }
257
258 void Monster_Sounds_Precache(entity this)
259 {
260         string m = (Monsters_from(this.monsterid)).m_model.model_str();
261         float globhandle, n, i;
262         string f;
263
264         globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
265         if (globhandle < 0)
266                 return;
267         n = search_getsize(globhandle);
268         for (i = 0; i < n; ++i)
269         {
270                 //print(search_getfilename(globhandle, i), "\n");
271                 f = search_getfilename(globhandle, i);
272                 Monster_Sound_Precache(f);
273         }
274         search_end(globhandle);
275 }
276
277 void Monster_Sounds_Clear(entity this)
278 {
279 #define _MSOUND(m) if(this.monstersound_##m) { strunzone(this.monstersound_##m); this.monstersound_##m = string_null; }
280         ALLMONSTERSOUNDS
281 #undef _MSOUND
282 }
283
284 .string Monster_Sound_SampleField(string type)
285 {
286         GetMonsterSoundSampleField_notFound = 0;
287         switch(type)
288         {
289 #define _MSOUND(m) case #m: return monstersound_##m;
290                 ALLMONSTERSOUNDS
291 #undef _MSOUND
292         }
293         GetMonsterSoundSampleField_notFound = 1;
294         return string_null;
295 }
296
297 bool Monster_Sounds_Load(entity this, string f, int first)
298 {
299         string s;
300         var .string field;
301         float fh = fopen(f, FILE_READ);
302         if(fh < 0)
303         {
304                 //LOG_DEBUG("Monster sound file not found: ", f); // no biggie, monster has no sounds, let's not spam it
305                 return false;
306         }
307         while((s = fgets(fh)))
308         {
309                 if(tokenize_console(s) != 3)
310                         continue;
311                 field = Monster_Sound_SampleField(argv(0));
312                 if(GetMonsterSoundSampleField_notFound)
313                         continue;
314                 if (this.(field))
315                         strunzone(this.(field));
316                 this.(field) = strzone(strcat(argv(1), " ", argv(2)));
317         }
318         fclose(fh);
319         return true;
320 }
321
322 .int skin_for_monstersound;
323 void Monster_Sounds_Update(entity this)
324 {
325         if(this.skin == this.skin_for_monstersound) { return; }
326
327         this.skin_for_monstersound = this.skin;
328         Monster_Sounds_Clear(this);
329         if(!Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, this.skin, "sounds"), 0))
330                 Monster_Sounds_Load(this, get_monster_model_datafilename(this.model, 0, "sounds"), 0);
331 }
332
333 void Monster_Sound(entity this, .string samplefield, float sound_delay, bool delaytoo, float chan)
334 {
335         if(!autocvar_g_monsters_sounds) { return; }
336
337         if(delaytoo)
338         if(time < this.msound_delay)
339                 return; // too early
340         string sample = this.(samplefield);
341         if (sample != "") sample = GlobalSound_sample(sample, random());
342         float myscale = ((this.scale) ? this.scale : 1); // safety net
343         // TODO: change volume depending on size too?
344         sound7(this, chan, sample, VOL_BASE, ATTEN_NORM, 100 / myscale, 0);
345
346         this.msound_delay = time + sound_delay;
347 }
348
349
350 // =======================
351 // Monster attack handlers
352 // =======================
353
354 bool Monster_Attack_Melee(entity this, entity targ, float damg, vector anim, float er, float animtime, int deathtype, bool dostop)
355 {
356         if(dostop && IS_MONSTER(this)) { this.state = MONSTER_ATTACK_MELEE; }
357
358         setanim(this, anim, false, true, false);
359
360         if(this.animstate_endtime > time && IS_MONSTER(this))
361                 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
362         else
363                 this.attack_finished_single[0] = this.anim_finished = time + animtime;
364
365         monster_makevectors(this, targ);
366
367         traceline(this.origin + this.view_ofs, this.origin + v_forward * er, 0, this);
368
369         if(trace_ent.takedamage)
370                 Damage(trace_ent, this, this, damg * MONSTER_SKILLMOD(this), deathtype, DMG_NOWEP, trace_ent.origin, normalize(trace_ent.origin - this.origin));
371
372         return true;
373 }
374
375 bool Monster_Attack_Leap_Check(entity this, vector vel)
376 {
377         if(this.state && IS_MONSTER(this))
378                 return false; // already attacking
379         if(!IS_ONGROUND(this))
380                 return false; // not on the ground
381         if(this.health <= 0 || IS_DEAD(this))
382                 return false; // called when dead?
383         if(time < this.attack_finished_single[0])
384                 return false; // still attacking
385
386         vector old = this.velocity;
387
388         this.velocity = vel;
389         tracetoss(this, this);
390         this.velocity = old;
391         if(trace_ent != this.enemy)
392                 return false;
393
394         return true;
395 }
396
397 bool Monster_Attack_Leap(entity this, vector anm, void(entity this, entity toucher) touchfunc, vector vel, float animtime)
398 {
399         if(!Monster_Attack_Leap_Check(this, vel))
400                 return false;
401
402         setanim(this, anm, false, true, false);
403
404         if(this.animstate_endtime > time && IS_MONSTER(this))
405                 this.attack_finished_single[0] = this.anim_finished = this.animstate_endtime;
406         else
407                 this.attack_finished_single[0] = this.anim_finished = time + animtime;
408
409         if(IS_MONSTER(this))
410                 this.state = MONSTER_ATTACK_RANGED;
411         settouch(this, touchfunc);
412         this.origin_z += 1;
413         this.velocity = vel;
414         UNSET_ONGROUND(this);
415
416         return true;
417 }
418
419 void Monster_Attack_Check(entity this, entity targ, .entity weaponentity)
420 {
421         int slot = weaponslot(weaponentity);
422
423         if((!this || !targ)
424         || (!this.monster_attackfunc)
425         || (time < this.attack_finished_single[slot])
426         ) { return; }
427
428         if(vdist(targ.origin - this.origin, <=, this.attack_range))
429         {
430                 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_MELEE, this, targ, weaponentity);
431                 if(attack_success == 1)
432                         Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
433                 else if(attack_success > 0)
434                         return;
435         }
436
437         if(vdist(targ.origin - this.origin, >, this.attack_range))
438         {
439                 int attack_success = this.monster_attackfunc(MONSTER_ATTACK_RANGED, this, targ, weaponentity);
440                 if(attack_success == 1)
441                         Monster_Sound(this, monstersound_melee, 0, false, CH_VOICE);
442                 else if(attack_success > 0)
443                         return;
444         }
445 }
446
447
448 // ======================
449 // Main monster functions
450 // ======================
451
452 void Monster_UpdateModel(entity this)
453 {
454         // assume some defaults
455         /*this.anim_idle   = animfixfps(this, '0 1 0.01', '0 0 0');
456         this.anim_walk   = animfixfps(this, '1 1 0.01', '0 0 0');
457         this.anim_run    = animfixfps(this, '2 1 0.01', '0 0 0');
458         this.anim_fire1  = animfixfps(this, '3 1 0.01', '0 0 0');
459         this.anim_fire2  = animfixfps(this, '4 1 0.01', '0 0 0');
460         this.anim_melee  = animfixfps(this, '5 1 0.01', '0 0 0');
461         this.anim_pain1  = animfixfps(this, '6 1 0.01', '0 0 0');
462         this.anim_pain2  = animfixfps(this, '7 1 0.01', '0 0 0');
463         this.anim_die1   = animfixfps(this, '8 1 0.01', '0 0 0');
464         this.anim_die2   = animfixfps(this, '9 1 0.01', '0 0 0');*/
465
466         // then get the real values
467         Monster mon = Monsters_from(this.monsterid);
468         mon.mr_anim(mon, this);
469 }
470
471 void Monster_Touch(entity this, entity toucher)
472 {
473         if(toucher == NULL) { return; }
474
475         if(toucher.monster_attack)
476         if(this.enemy != toucher)
477         if(!IS_MONSTER(toucher))
478         if(Monster_ValidTarget(this, toucher))
479                 this.enemy = toucher;
480 }
481
482 void Monster_Miniboss_Check(entity this)
483 {
484         if(MUTATOR_CALLHOOK(MonsterCheckBossFlag, this))
485                 return;
486
487         float chance = random() * 100;
488
489         // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
490         if ((this.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
491         {
492                 this.health += autocvar_g_monsters_miniboss_healthboost;
493                 this.effects |= EF_RED;
494                 if(!this.weapon)
495                         this.weapon = WEP_VORTEX.m_id;
496         }
497 }
498
499 bool Monster_Respawn_Check(entity this)
500 {
501         if(this.deadflag == DEAD_DEAD) // don't call when monster isn't dead
502         if(MUTATOR_CALLHOOK(MonsterRespawn, this))
503                 return true; // enabled by a mutator
504
505         if(this.spawnflags & MONSTERFLAG_NORESPAWN)
506                 return false;
507
508         if(!autocvar_g_monsters_respawn)
509                 return false;
510
511         return true;
512 }
513
514 void Monster_Respawn(entity this) { Monster_Spawn(this, true, this.monsterid); }
515
516 .vector pos1, pos2;
517
518 void Monster_Dead_Fade(entity this)
519 {
520         if(Monster_Respawn_Check(this))
521         {
522                 this.spawnflags |= MONSTERFLAG_RESPAWNED;
523                 setthink(this, Monster_Respawn);
524                 this.nextthink = time + this.respawntime;
525                 this.monster_lifetime = 0;
526                 this.deadflag = DEAD_RESPAWNING;
527                 if(this.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
528                 {
529                         this.pos1 = this.origin;
530                         this.pos2 = this.angles;
531                 }
532                 this.event_damage = func_null;
533                 this.takedamage = DAMAGE_NO;
534                 setorigin(this, this.pos1);
535                 this.angles = this.pos2;
536                 this.health = this.max_health;
537                 setmodel(this, MDL_Null);
538         }
539         else
540         {
541                 // number of monsters spawned with mobspawn command
542                 totalspawned -= 1;
543
544                 SUB_SetFade(this, time + 3, 1);
545         }
546 }
547
548 void Monster_Use(entity this, entity actor, entity trigger)
549 {
550         if(Monster_ValidTarget(this, actor)) { this.enemy = actor; }
551 }
552
553 .float pass_distance;
554 vector Monster_Move_Target(entity this, entity targ)
555 {
556         // enemy is always preferred target
557         if(this.enemy)
558         {
559                 vector targ_origin = ((this.enemy.absmin + this.enemy.absmax) * 0.5);
560                 targ_origin = WarpZone_RefSys_TransformOrigin(this.enemy, this, targ_origin); // origin of target as seen by the monster (us)
561                 WarpZone_TraceLine(this.origin, targ_origin, MOVE_NOMONSTERS, this);
562
563                 // cases where the enemy may have changed their state (don't need to check everything here)
564                 if((!this.enemy)
565                         || (IS_DEAD(this.enemy) || this.enemy.health < 1)
566                         || (STAT(FROZEN, this.enemy))
567                         || (this.enemy.flags & FL_NOTARGET)
568                         || (this.enemy.alpha < 0.5 && this.enemy.alpha != 0)
569                         || (this.enemy.takedamage == DAMAGE_NO)
570                         || (vdist(this.origin - targ_origin, >, this.target_range))
571                         || ((trace_fraction < 1) && (trace_ent != this.enemy)))
572                 {
573                         this.enemy = NULL;
574                         //this.pass_distance = 0;
575                 }
576
577                 if(this.enemy)
578                 {
579                         /*WarpZone_TrailParticles(NULL, particleeffectnum(EFFECT_RED_PASS), this.origin, targ_origin);
580                         print("Trace origin: ", vtos(targ_origin), "\n");
581                         print("Target origin: ", vtos(this.enemy.origin), "\n");
582                         print("My origin: ", vtos(this.origin), "\n"); */
583
584                         this.monster_movestate = MONSTER_MOVE_ENEMY;
585                         this.last_trace = time + 1.2;
586                         if(this.monster_moveto)
587                                 return this.monster_moveto; // assumes code is properly setting this when monster has an enemy
588                         else
589                                 return targ_origin;
590                 }
591
592                 /*makevectors(this.angles);
593                 this.monster_movestate = MONSTER_MOVE_ENEMY;
594                 this.last_trace = time + 1.2;
595                 return this.enemy.origin; */
596         }
597
598         switch(this.monster_moveflags)
599         {
600                 case MONSTER_MOVE_FOLLOW:
601                 {
602                         this.monster_movestate = MONSTER_MOVE_FOLLOW;
603                         this.last_trace = time + 0.3;
604                         return (this.monster_follow) ? this.monster_follow.origin : this.origin;
605                 }
606                 case MONSTER_MOVE_SPAWNLOC:
607                 {
608                         this.monster_movestate = MONSTER_MOVE_SPAWNLOC;
609                         this.last_trace = time + 2;
610                         return this.pos1;
611                 }
612                 case MONSTER_MOVE_NOMOVE:
613                 {
614                         if(this.monster_moveto)
615                         {
616                                 this.last_trace = time + 0.5;
617                                 return this.monster_moveto;
618                         }
619                         else
620                         {
621                                 this.monster_movestate = MONSTER_MOVE_NOMOVE;
622                                 this.last_trace = time + 2;
623                         }
624                         return this.origin;
625                 }
626                 default:
627                 case MONSTER_MOVE_WANDER:
628                 {
629                         vector pos;
630                         this.monster_movestate = MONSTER_MOVE_WANDER;
631
632                         if(this.monster_moveto)
633                         {
634                                 this.last_trace = time + 0.5;
635                                 pos = this.monster_moveto;
636                         }
637                         else if(targ)
638                         {
639                                 this.last_trace = time + 0.5;
640                                 pos = targ.origin;
641                         }
642                         else
643                         {
644                                 this.last_trace = time + this.wander_delay;
645
646                                 this.angles_y = rint(random() * 500);
647                                 makevectors(this.angles);
648                                 pos = this.origin + v_forward * this.wander_distance;
649
650                                 if(((this.flags & FL_FLY) && (this.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (this.flags & FL_SWIM))
651                                 {
652                                         pos.z = random() * 200;
653                                         if(random() >= 0.5)
654                                                 pos.z *= -1;
655                                 }
656                         }
657
658                         return pos;
659                 }
660         }
661 }
662
663 void Monster_CalculateVelocity(entity this, vector to, vector from, float turnrate, float movespeed)
664 {
665         //float current_distance = vlen((('1 0 0' * to.x) + ('0 1 0' * to.y)) - (('1 0 0' * from.x) + ('0 1 0' * from.y))); // for the sake of this check, exclude Z axis
666         //float initial_height = 0; //min(50, (targ_distance * tanh(20)));
667         //float current_height = (initial_height * min(1, (this.pass_distance) ? (current_distance / this.pass_distance) : current_distance));
668         //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
669
670         vector targpos = to;
671 #if 0
672         if(current_height) // make sure we can actually do this arcing path
673         {
674                 targpos = (to + ('0 0 1' * current_height));
675                 WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
676                 if(trace_fraction < 1)
677                 {
678                         //print("normal arc line failed, trying to find new pos...");
679                         WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, this);
680                         targpos = (trace_endpos + '0 0 -10');
681                         WarpZone_TraceLine(this.origin, targpos, MOVE_NOMONSTERS, this);
682                         if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
683                         /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
684                 }
685         }
686         else { targpos = to; }
687 #endif
688
689         //this.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
690
691         vector desired_direction = normalize(targpos - from);
692         if(turnrate) { this.velocity = (normalize(normalize(this.velocity) + (desired_direction * 50)) * movespeed); }
693         else { this.velocity = (desired_direction * movespeed); }
694
695         //this.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
696         //this.angles = vectoangles(this.velocity);
697 }
698
699 .entity draggedby;
700 .entity target2;
701
702 void Monster_Move(entity this, float runspeed, float walkspeed, float stpspeed)
703 {
704         // update goal entity if lost
705         if(this.target2 && this.target2 != "" && this.goalentity.targetname != this.target2)
706                 this.goalentity = find(NULL, targetname, this.target2);
707
708         if(STAT(FROZEN, this))
709         {
710                 movelib_brake_simple(this, stpspeed);
711                 setanim(this, this.anim_idle, true, false, false);
712                 return; // no physics while frozen!
713         }
714
715         if(this.flags & FL_SWIM)
716         {
717                 if(this.waterlevel < WATERLEVEL_WETFEET)
718                 {
719                         if(time >= this.last_trace)
720                         {
721                                 this.last_trace = time + 0.4;
722
723                                 Damage (this, NULL, NULL, 2, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
724                                 this.angles = '90 90 0';
725                                 if(random() < 0.5)
726                                 {
727                                         this.velocity_y += random() * 50;
728                                         this.velocity_x -= random() * 50;
729                                 }
730                                 else
731                                 {
732                                         this.velocity_y -= random() * 50;
733                                         this.velocity_x += random() * 50;
734                                 }
735                                 this.velocity_z += random() * 150;
736                         }
737
738
739                         set_movetype(this, MOVETYPE_BOUNCE);
740                         //this.velocity_z = -200;
741
742                         return;
743                 }
744                 else if(this.move_movetype == MOVETYPE_BOUNCE)
745                 {
746                         this.angles_x = 0;
747                         set_movetype(this, MOVETYPE_WALK);
748                 }
749         }
750
751         entity targ = this.goalentity;
752
753         if (MUTATOR_CALLHOOK(MonsterMove, this, runspeed, walkspeed, targ)
754                 || game_stopped
755                 || this.draggedby != NULL
756                 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
757                 || time < game_starttime
758                 || (autocvar_g_campaign && !campaign_bots_may_start)
759                 || time < this.spawn_time)
760         {
761                 runspeed = walkspeed = 0;
762                 if(time >= this.spawn_time)
763                         setanim(this, this.anim_idle, true, false, false);
764                 movelib_brake_simple(this, stpspeed);
765                 return;
766         }
767
768         targ = M_ARGV(3, entity);
769         runspeed = bound(0, M_ARGV(1, float) * MONSTER_SKILLMOD(this), runspeed * 2.5); // limit maxspeed to prevent craziness
770         walkspeed = bound(0, M_ARGV(2, float) * MONSTER_SKILLMOD(this), walkspeed * 2.5); // limit maxspeed to prevent craziness
771
772         if(teamplay && autocvar_g_monsters_teams)
773         if(DIFF_TEAM(this.monster_follow, this))
774                 this.monster_follow = NULL;
775
776         if(this.state == MONSTER_ATTACK_RANGED && IS_ONGROUND(this))
777         {
778                 this.state = 0;
779                 settouch(this, Monster_Touch);
780         }
781
782         if(this.state && time >= this.attack_finished_single[0])
783                 this.state = 0; // attack is over
784
785         if(this.state != MONSTER_ATTACK_MELEE) // don't move if set
786         if(time >= this.last_trace || this.enemy) // update enemy or rider instantly
787                 this.moveto = Monster_Move_Target(this, targ);
788
789         if(!this.enemy)
790                 Monster_Sound(this, monstersound_idle, 7, true, CH_VOICE);
791
792         if(this.state == MONSTER_ATTACK_MELEE)
793                 this.moveto = this.origin;
794
795         if(this.enemy && this.enemy.vehicle)
796                 runspeed = 0;
797
798         if(!(this.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(this.flags & FL_SWIM))
799                 this.moveto_z = this.origin_z;
800
801         if(vdist(this.origin - this.moveto, >, 100))
802         {
803                 bool do_run = (this.enemy || this.monster_moveto);
804                 if(IS_ONGROUND(this) || ((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
805                         Monster_CalculateVelocity(this, this.moveto, this.origin, true, ((do_run) ? runspeed : walkspeed));
806
807                 if(time > this.pain_finished && time > this.anim_finished) // TODO: use anim_finished instead!?
808                 if(!this.state)
809                 if(vdist(this.velocity, >, 10))
810                         setanim(this, ((do_run) ? this.anim_run : this.anim_walk), true, false, false);
811                 else
812                         setanim(this, this.anim_idle, true, false, false);
813         }
814         else
815         {
816                 entity e = this.goalentity; //find(NULL, targetname, this.target2);
817                 if(e.target2 && e.target2 != "")
818                         this.target2 = e.target2;
819                 else if(e.target && e.target != "") // compatibility
820                         this.target2 = e.target;
821
822                 movelib_brake_simple(this, stpspeed);
823                 if(time > this.anim_finished && time > this.pain_finished)
824                 if(!this.state)
825                 if(vdist(this.velocity, <=, 30))
826                         setanim(this, this.anim_idle, true, false, false);
827         }
828
829         this.steerto = steerlib_attract2(this, ((this.monster_face) ? this.monster_face : this.moveto), 0.5, 500, 0.95);
830
831         vector real_angle = vectoangles(this.steerto) - this.angles;
832         float turny = 25;
833         if(this.state == MONSTER_ATTACK_MELEE)
834                 turny = 0;
835         if(turny)
836         {
837                 turny = bound(turny * -1, shortangle_f(real_angle.y, this.angles.y), turny);
838                 this.angles_y += turny;
839         }
840 }
841
842 void Monster_Remove(entity this)
843 {
844         if(IS_CLIENT(this))
845                 return; // don't remove it?
846
847         if(!this) { return; }
848
849         if(!MUTATOR_CALLHOOK(MonsterRemove, this))
850                 Send_Effect(EFFECT_ITEM_PICKUP, this.origin, '0 0 0', 1);
851
852         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
853         {
854                 .entity weaponentity = weaponentities[slot];
855                 if(this.(weaponentity))
856                         delete(this.(weaponentity));
857         }
858         if(this.iceblock) { delete(this.iceblock); }
859         WaypointSprite_Kill(this.sprite);
860         delete(this);
861 }
862
863 void Monster_Dead_Think(entity this)
864 {
865         this.nextthink = time + this.ticrate;
866
867         if(this.monster_lifetime != 0)
868         if(time >= this.monster_lifetime)
869         {
870                 Monster_Dead_Fade(this);
871                 return;
872         }
873 }
874
875 void Monster_Appear(entity this, entity actor, entity trigger)
876 {
877         this.enemy = actor;
878         Monster_Spawn(this, false, this.monsterid);
879 }
880
881 bool Monster_Appear_Check(entity this, int monster_id)
882 {
883         if(!(this.spawnflags & MONSTERFLAG_APPEAR))
884                 return false;
885
886         setthink(this, func_null);
887         this.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
888         this.nextthink = 0;
889         this.use = Monster_Appear;
890         this.flags = FL_MONSTER; // set so this monster can get butchered
891
892         return true;
893 }
894
895 void Monster_Reset(entity this)
896 {
897         setorigin(this, this.pos1);
898         this.angles = this.pos2;
899
900         Unfreeze(this); // remove any icy remains
901
902         this.health = this.max_health;
903         this.velocity = '0 0 0';
904         this.enemy = NULL;
905         this.goalentity = NULL;
906         this.attack_finished_single[0] = 0;
907         this.moveto = this.origin;
908 }
909
910 void Monster_Dead_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
911 {
912         this.health -= damage;
913
914         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
915
916         if(this.health <= -50) // 100 health until gone?
917         {
918                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
919
920                 // number of monsters spawned with mobspawn command
921                 totalspawned -= 1;
922
923                 setthink(this, SUB_Remove);
924                 this.nextthink = time + 0.1;
925                 this.event_damage = func_null;
926         }
927 }
928
929 void Monster_Dead(entity this, entity attacker, float gibbed)
930 {
931         setthink(this, Monster_Dead_Think);
932         this.nextthink = time;
933         this.monster_lifetime = time + 5;
934
935         if(STAT(FROZEN, this))
936         {
937                 Unfreeze(this); // remove any icy remains
938                 this.health = 0; // reset by Unfreeze
939         }
940
941         monster_dropitem(this, attacker);
942
943         Monster_Sound(this, monstersound_death, 0, false, CH_VOICE);
944
945         if(!(this.spawnflags & MONSTERFLAG_SPAWNED) && !(this.spawnflags & MONSTERFLAG_RESPAWNED))
946                 monsters_killed += 1;
947
948         if(IS_PLAYER(attacker))
949         if(autocvar_g_monsters_score_spawned || !((this.spawnflags & MONSTERFLAG_SPAWNED) || (this.spawnflags & MONSTERFLAG_RESPAWNED)))
950                 GameRules_scoring_add(attacker, SCORE, +autocvar_g_monsters_score_kill);
951
952         if(gibbed)
953         {
954                 // number of monsters spawned with mobspawn command
955                 totalspawned -= 1;
956         }
957
958         if(!gibbed && this.mdl_dead && this.mdl_dead != "")
959                 _setmodel(this, this.mdl_dead);
960
961         this.event_damage       = ((gibbed) ? func_null : Monster_Dead_Damage);
962         this.solid                      = SOLID_CORPSE;
963         this.takedamage         = DAMAGE_AIM;
964         this.deadflag           = DEAD_DEAD;
965         this.enemy                      = NULL;
966         set_movetype(this, MOVETYPE_TOSS);
967         this.moveto                     = this.origin;
968         settouch(this, Monster_Touch); // reset incase monster was pouncing
969         this.reset                      = func_null;
970         this.state                      = 0;
971         this.attack_finished_single[0] = 0;
972         this.effects = 0;
973
974         if(!((this.flags & FL_FLY) || (this.flags & FL_SWIM)))
975                 this.velocity = '0 0 0';
976
977         CSQCModel_UnlinkEntity(this);
978
979         Monster mon = Monsters_from(this.monsterid);
980         mon.mr_death(mon, this);
981
982         if(this.candrop && this.weapon)
983         {
984                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
985                 W_ThrowNewWeapon(this, this.weapon, 0, this.origin, randomvec() * 150 + '0 0 325', weaponentity);
986         }
987 }
988
989 void Monster_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
990 {
991         if((this.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL.m_id && !ITEM_DAMAGE_NEEDKILL(deathtype))
992                 return;
993
994         if(STAT(FROZEN, this) && deathtype != DEATH_KILL.m_id && deathtype != DEATH_NADE_ICE_FREEZE.m_id)
995                 return;
996
997         //if(time < this.pain_finished && deathtype != DEATH_KILL.m_id)
998                 //return;
999
1000         if(time < this.spawnshieldtime && deathtype != DEATH_KILL.m_id)
1001                 return;
1002
1003         if(deathtype == DEATH_FALL.m_id && this.draggedby != NULL)
1004                 return;
1005
1006         vector v = healtharmor_applydamage(100, this.armorvalue / 100, deathtype, damage);
1007         float take = v.x;
1008         //float save = v.y;
1009
1010         Monster mon = Monsters_from(this.monsterid);
1011         take = mon.mr_pain(mon, this, take, attacker, deathtype);
1012
1013         if(take)
1014         {
1015                 this.health -= take;
1016                 Monster_Sound(this, monstersound_pain, 1.2, true, CH_PAIN);
1017         }
1018
1019         if(this.sprite)
1020                 WaypointSprite_UpdateHealth(this.sprite, this.health);
1021
1022         this.dmg_time = time;
1023
1024         if(deathtype != DEATH_DROWN.m_id && deathtype != DEATH_FIRE.m_id && sound_allowed(MSG_BROADCAST, attacker))
1025                 spamsound (this, CH_PAIN, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
1026
1027         this.velocity += force * this.damageforcescale;
1028
1029         if(deathtype != DEATH_DROWN.m_id && take)
1030         {
1031                 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, this, attacker);
1032                 if (take > 50)
1033                         Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
1034                 if (take > 100)
1035                         Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
1036         }
1037
1038         if(this.health <= 0)
1039         {
1040                 if(deathtype == DEATH_KILL.m_id)
1041                         this.candrop = false; // killed by mobkill command
1042
1043                 // TODO: fix this?
1044                 SUB_UseTargets(this, attacker, this.enemy);
1045                 this.target2 = this.oldtarget2; // reset to original target on death, incase we respawn
1046
1047                 Monster_Dead(this, attacker, (this.health <= -100 || deathtype == DEATH_KILL.m_id));
1048
1049                 WaypointSprite_Kill(this.sprite);
1050
1051                 MUTATOR_CALLHOOK(MonsterDies, this, attacker, deathtype);
1052
1053                 if(this.health <= -100 || deathtype == DEATH_KILL.m_id) // check if we're already gibbed
1054                 {
1055                         Violence_GibSplash(this, 1, 0.5, attacker);
1056
1057                         setthink(this, SUB_Remove);
1058                         this.nextthink = time + 0.1;
1059                 }
1060         }
1061 }
1062
1063 // don't check for enemies, just keep walking in a straight line
1064 void Monster_Move_2D(entity this, float mspeed, bool allow_jumpoff)
1065 {
1066         if(game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || this.draggedby != NULL || time < game_starttime || (autocvar_g_campaign && !campaign_bots_may_start) || time < this.spawn_time)
1067         {
1068                 mspeed = 0;
1069                 if(time >= this.spawn_time)
1070                         setanim(this, this.anim_idle, true, false, false);
1071                 movelib_brake_simple(this, 0.6);
1072                 return;
1073         }
1074
1075         makevectors(this.angles);
1076         vector a = CENTER_OR_VIEWOFS(this);
1077         vector b = CENTER_OR_VIEWOFS(this) + v_forward * 32;
1078
1079         traceline(a, b, MOVE_NORMAL, this);
1080
1081         bool reverse = false;
1082         if(trace_fraction != 1.0)
1083                 reverse = true;
1084         if(trace_ent && IS_PLAYER(trace_ent) && !(trace_ent.items & ITEM_Strength.m_itemid))
1085                 reverse = false;
1086         if(trace_ent && IS_MONSTER(trace_ent))
1087                 reverse = true;
1088
1089         // TODO: fix this... tracing is broken if the floor is thin
1090         /*
1091         if(!allow_jumpoff)
1092         {
1093                 a = b - '0 0 32';
1094                 traceline(b, a, MOVE_WORLDONLY, this);
1095                 if(trace_fraction == 1.0)
1096                         reverse = true;
1097         } */
1098
1099         if(reverse)
1100         {
1101                 this.angles_y = anglemods(this.angles_y - 180);
1102                 makevectors(this.angles);
1103         }
1104
1105         movelib_move_simple_gravity(this, v_forward, mspeed, 1);
1106
1107         if(time > this.pain_finished && time > this.attack_finished_single[0])
1108         if(vdist(this.velocity, >, 10))
1109                 setanim(this, this.anim_walk, true, false, false);
1110         else
1111                 setanim(this, this.anim_idle, true, false, false);
1112 }
1113
1114 void Monster_Anim(entity this)
1115 {
1116         int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1117         if(IS_DEAD(this))
1118         {
1119                 if (!deadbits)
1120                 {
1121                         // Decide on which death animation to use.
1122                         if(random() < 0.5)
1123                                 deadbits = ANIMSTATE_DEAD1;
1124                         else
1125                                 deadbits = ANIMSTATE_DEAD2;
1126                 }
1127         }
1128         else
1129         {
1130                 // Clear a previous death animation.
1131                 deadbits = 0;
1132         }
1133         int animbits = deadbits;
1134         if(STAT(FROZEN, this))
1135                 animbits |= ANIMSTATE_FROZEN;
1136         if(this.crouch)
1137                 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1138         animdecide_setstate(this, animbits, false);
1139         animdecide_setimplicitstate(this, (IS_ONGROUND(this)));
1140
1141         /* // weapon entities for monsters?
1142         if (this.weaponentity)
1143         {
1144                 updateanim(this.weaponentity);
1145                 if (!this.weaponentity.animstate_override)
1146                         setanim(this.weaponentity, this.weaponentity.anim_idle, true, false, false);
1147         }
1148         */
1149 }
1150
1151 void Monster_Frozen_Think(entity this)
1152 {
1153         if(STAT(FROZEN, this) == 2)
1154         {
1155                 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
1156                 this.health = max(1, STAT(REVIVE_PROGRESS, this) * this.max_health);
1157                 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
1158
1159                 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1160                         WaypointSprite_UpdateHealth(this.sprite, this.health);
1161
1162                 if(STAT(REVIVE_PROGRESS, this) >= 1)
1163                         Unfreeze(this);
1164         }
1165         else if(STAT(FROZEN, this) == 3)
1166         {
1167                 STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
1168                 this.health = max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this) );
1169
1170                 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE) && this.sprite)
1171                         WaypointSprite_UpdateHealth(this.sprite, this.health);
1172
1173                 if(this.health < 1)
1174                 {
1175                         Unfreeze(this);
1176                         this.health = 0;
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');
1179                 }
1180
1181                 else if ( STAT(REVIVE_PROGRESS, this) <= 0 )
1182                         Unfreeze(this);
1183         }
1184         // otherwise, no revival!
1185
1186         this.enemy = NULL; // TODO: save enemy, and attack when revived?
1187 }
1188
1189 void Monster_Enemy_Check(entity this)
1190 {
1191         if(!this.enemy)
1192         {
1193                 this.enemy = Monster_FindTarget(this);
1194                 if(this.enemy)
1195                 {
1196                         WarpZone_RefSys_Copy(this.enemy, this);
1197                         WarpZone_RefSys_AddInverse(this.enemy, this); // wz1^-1 ... wzn^-1 receiver
1198                         // update move target immediately?
1199                         this.moveto = WarpZone_RefSys_TransformOrigin(this.enemy, this, (0.5 * (this.enemy.absmin + this.enemy.absmax)));
1200                         this.monster_moveto = '0 0 0';
1201                         this.monster_face = '0 0 0';
1202
1203                         //this.pass_distance = vlen((('1 0 0' * this.enemy.origin_x) + ('0 1 0' * this.enemy.origin_y)) - (('1 0 0' *  this.origin_x) + ('0 1 0' *  this.origin_y)));
1204                         Monster_Sound(this, monstersound_sight, 0, false, CH_VOICE);
1205                 }
1206         }
1207 }
1208
1209 void Monster_Think(entity this)
1210 {
1211         setthink(this, Monster_Think);
1212         this.nextthink = time + this.ticrate;
1213
1214         if(this.monster_lifetime && time >= this.monster_lifetime)
1215         {
1216                 Damage(this, this, this, this.health + this.max_health, DEATH_KILL.m_id, DMG_NOWEP, this.origin, this.origin);
1217                 return;
1218         }
1219
1220         if(STAT(FROZEN, this))
1221                 Monster_Frozen_Think(this);
1222         else if(time >= this.last_enemycheck)
1223         {
1224                 Monster_Enemy_Check(this);
1225                 this.last_enemycheck = time + 1; // check for enemies every second
1226         }
1227
1228         Monster mon = Monsters_from(this.monsterid);
1229         if(mon.mr_think(mon, this))
1230         {
1231                 Monster_Move(this, this.speed2, this.speed, this.stopspeed);
1232
1233                 .entity weaponentity = weaponentities[0]; // TODO?
1234                 Monster_Attack_Check(this, this.enemy, weaponentity);
1235         }
1236
1237         Monster_Anim(this);
1238
1239         CSQCMODEL_AUTOUPDATE(this);
1240 }
1241
1242 bool Monster_Spawn_Setup(entity this)
1243 {
1244         Monster mon = Monsters_from(this.monsterid);
1245         mon.mr_setup(mon, this);
1246
1247         // ensure some basic needs are met
1248         if(!this.health) { this.health = 100; }
1249         if(!this.armorvalue) { this.armorvalue = bound(0.2, 0.5 * MONSTER_SKILLMOD(this), 0.9); }
1250         if(!this.target_range) { this.target_range = autocvar_g_monsters_target_range; }
1251         if(!this.respawntime) { this.respawntime = autocvar_g_monsters_respawn_delay; }
1252         if(!this.monster_moveflags) { this.monster_moveflags = MONSTER_MOVE_WANDER; }
1253         if(!this.attack_range) { this.attack_range = autocvar_g_monsters_attack_range; }
1254         if(!this.damageforcescale) { this.damageforcescale = autocvar_g_monsters_damageforcescale; }
1255
1256         if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1257         {
1258                 Monster_Miniboss_Check(this);
1259                 this.health *= MONSTER_SKILLMOD(this);
1260
1261                 if(!this.skin)
1262                         this.skin = rint(random() * 4);
1263         }
1264
1265         this.max_health = this.health;
1266         this.pain_finished = this.nextthink;
1267
1268         if(IS_PLAYER(this.monster_follow))
1269                 this.effects |= EF_DIMLIGHT;
1270
1271         if(!this.wander_delay) { this.wander_delay = 2; }
1272         if(!this.wander_distance) { this.wander_distance = 600; }
1273
1274         Monster_Sounds_Precache(this);
1275         Monster_Sounds_Update(this);
1276
1277         if(teamplay)
1278         {
1279                 if(!this.monster_attack)
1280                         IL_PUSH(g_monster_targets, this);
1281                 this.monster_attack = true; // we can have monster enemies in team games
1282         }
1283
1284         Monster_Sound(this, monstersound_spawn, 0, false, CH_VOICE);
1285
1286         if(autocvar_g_monsters_healthbars)
1287         {
1288                 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, this, '0 0 1' * (this.maxs.z + 15), NULL, this.team, this, sprite, true, RADARICON_DANGER);
1289                 wp.wp_extra = this.monsterid;
1290                 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0 0');
1291                 if(!(this.spawnflags & MONSTERFLAG_INVINCIBLE))
1292                 {
1293                         WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
1294                         WaypointSprite_UpdateHealth(this.sprite, this.health);
1295                 }
1296         }
1297
1298         setthink(this, Monster_Think);
1299         this.nextthink = time + this.ticrate;
1300
1301         if(MUTATOR_CALLHOOK(MonsterSpawn, this))
1302                 return false;
1303
1304         return true;
1305 }
1306
1307 bool Monster_Spawn(entity this, bool check_appear, int mon_id)
1308 {
1309         // setup the basic required properties for a monster
1310         entity mon = Monsters_from(mon_id);
1311         if(!mon.monsterid) { return false; } // invalid monster
1312
1313         if(!autocvar_g_monsters) { Monster_Remove(this); return false; }
1314
1315         if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1316         {
1317                 IL_PUSH(g_monsters, this);
1318                 if(this.mdl && this.mdl != "")
1319                         precache_model(this.mdl);
1320                 if(this.mdl_dead && this.mdl_dead != "")
1321                         precache_model(this.mdl_dead);
1322         }
1323
1324         if(check_appear && Monster_Appear_Check(this, mon_id)) { return true; } // return true so the monster isn't removed
1325
1326         if(!this.monster_skill)
1327                 this.monster_skill = cvar("g_monsters_skill");
1328
1329         // support for quake style removing monsters based on skill
1330         if(this.monster_skill == MONSTER_SKILL_EASY) if(this.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(this); return false; }
1331         if(this.monster_skill == MONSTER_SKILL_MEDIUM) if(this.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(this); return false; }
1332         if(this.monster_skill == MONSTER_SKILL_HARD) if(this.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(this); return false; }
1333
1334         if(this.team && !teamplay)
1335                 this.team = 0;
1336
1337         if(!(this.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1338         if(!(this.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1339                 monsters_total += 1;
1340
1341         if(this.mdl && this.mdl != "")
1342                 _setmodel(this, this.mdl);
1343         else
1344                 setmodel(this, mon.m_model);
1345
1346         this.flags                              = FL_MONSTER;
1347         this.classname                  = "monster";
1348         this.takedamage                 = DAMAGE_AIM;
1349         if(!this.bot_attack)
1350                 IL_PUSH(g_bot_targets, this);
1351         this.bot_attack                 = true;
1352         this.iscreature                 = true;
1353         this.teleportable               = true;
1354         if(!this.damagedbycontents)
1355                 IL_PUSH(g_damagedbycontents, this);
1356         this.damagedbycontents  = true;
1357         this.monsterid                  = mon_id;
1358         this.event_damage               = Monster_Damage;
1359         settouch(this, Monster_Touch);
1360         this.use                                = Monster_Use;
1361         this.solid                              = SOLID_BBOX;
1362         set_movetype(this, MOVETYPE_WALK);
1363         this.spawnshieldtime    = time + autocvar_g_monsters_spawnshieldtime;
1364         this.enemy                              = NULL;
1365         this.velocity                   = '0 0 0';
1366         this.moveto                             = this.origin;
1367         this.pos1                               = this.origin;
1368         this.pos2                               = this.angles;
1369         this.reset                              = Monster_Reset;
1370         this.netname                    = mon.netname;
1371         this.monster_attackfunc = mon.monster_attackfunc;
1372         this.monster_name               = mon.monster_name;
1373         this.candrop                    = true;
1374         this.view_ofs                   = '0 0 0.7' * (this.maxs_z * 0.5);
1375         this.oldtarget2                 = this.target2;
1376         //this.pass_distance            = 0;
1377         this.deadflag                   = DEAD_NO;
1378         this.spawn_time                 = time;
1379         this.gravity                    = 1;
1380         this.monster_moveto             = '0 0 0';
1381         this.monster_face               = '0 0 0';
1382         this.dphitcontentsmask  = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1383
1384         if(!this.noalign) { this.noalign = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM)); }
1385         if(!this.scale) { this.scale = 1; }
1386         if(autocvar_g_monsters_edit) { this.grab = 1; }
1387         if(autocvar_g_fullbrightplayers) { this.effects |= EF_FULLBRIGHT; }
1388         if(autocvar_g_nodepthtestplayers) { this.effects |= EF_NODEPTHTEST; }
1389         if(mon.spawnflags & MONSTER_TYPE_SWIM) { this.flags |= FL_SWIM; }
1390
1391         if(autocvar_g_playerclip_collisions)
1392                 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1393
1394         if(mon.spawnflags & MONSTER_TYPE_FLY)
1395         {
1396                 this.flags |= FL_FLY;
1397                 set_movetype(this, MOVETYPE_FLY);
1398         }
1399
1400         if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1401         {
1402                 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1403                         this.scale *= 1.3;
1404
1405                 if(mon.spawnflags & MONSTER_SIZE_QUAKE)
1406                 if(autocvar_g_monsters_quake_resize)
1407                         this.scale *= 1.3;
1408         }
1409
1410         setsize(this, mon.m_mins * this.scale, mon.m_maxs * this.scale);
1411
1412         this.ticrate = bound(sys_frametime, ((!this.ticrate) ? autocvar_g_monsters_think_delay : this.ticrate), 60);
1413
1414         Monster_UpdateModel(this);
1415
1416         if(!Monster_Spawn_Setup(this))
1417         {
1418                 Monster_Remove(this);
1419                 return false;
1420         }
1421
1422         if(!this.noalign)
1423         {
1424                 setorigin(this, this.origin + '0 0 20');
1425                 tracebox(this.origin + '0 0 64', this.mins, this.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1426                 setorigin(this, trace_endpos);
1427         }
1428
1429         if(!(this.spawnflags & MONSTERFLAG_RESPAWNED))
1430                 monster_setupcolors(this);
1431
1432         CSQCMODEL_AUTOINIT(this);
1433
1434         return true;
1435 }