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