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