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