Spawnfuncs: whitelist more fields
[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)).model;
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);
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);
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         MON_ACTION(self.monsterid, MR_ANIM);
476 }
477
478 void Monster_Touch()
479 {SELFPARAM();
480         if(other == world) { return; }
481
482         if(other.monster_attack)
483         if(self.enemy != other)
484         if(!IS_MONSTER(other))
485         if(Monster_ValidTarget(self, other))
486                 self.enemy = other;
487 }
488
489 void Monster_Miniboss_Check()
490 {SELFPARAM();
491         if(MUTATOR_CALLHOOK(MonsterCheckBossFlag))
492                 return;
493
494         float chance = random() * 100;
495
496         // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
497         if ((self.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
498         {
499                 self.health += autocvar_g_monsters_miniboss_healthboost;
500                 self.effects |= EF_RED;
501                 if(!self.weapon)
502                         self.weapon = WEP_VORTEX.m_id;
503         }
504 }
505
506 bool Monster_Respawn_Check()
507 {SELFPARAM();
508         if(self.deadflag == DEAD_DEAD) // don't call when monster isn't dead
509         if(MUTATOR_CALLHOOK(MonsterRespawn, self))
510                 return true; // enabled by a mutator
511
512         if(self.spawnflags & MONSTERFLAG_NORESPAWN)
513                 return false;
514
515         if(!autocvar_g_monsters_respawn)
516                 return false;
517
518         return true;
519 }
520
521 void Monster_Respawn() { SELFPARAM(); Monster_Spawn(self.monsterid); }
522
523 void Monster_Dead_Fade()
524 {SELFPARAM();
525         if(Monster_Respawn_Check())
526         {
527                 self.spawnflags |= MONSTERFLAG_RESPAWNED;
528                 self.think = Monster_Respawn;
529                 self.nextthink = time + self.respawntime;
530                 self.monster_lifetime = 0;
531                 self.deadflag = DEAD_RESPAWNING;
532                 if(self.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
533                 {
534                         self.pos1 = self.origin;
535                         self.pos2 = self.angles;
536                 }
537                 self.event_damage = func_null;
538                 self.takedamage = DAMAGE_NO;
539                 setorigin(self, self.pos1);
540                 self.angles = self.pos2;
541                 self.health = self.max_health;
542                 setmodel(self, MDL_Null);
543         }
544         else
545         {
546                 // number of monsters spawned with mobspawn command
547                 totalspawned -= 1;
548
549                 SUB_SetFade(self, time + 3, 1);
550         }
551 }
552
553 void Monster_Use()
554 {SELFPARAM();
555         if(Monster_ValidTarget(self, activator)) { self.enemy = activator; }
556 }
557
558 vector Monster_Move_Target(entity targ)
559 {SELFPARAM();
560         // enemy is always preferred target
561         if(self.enemy)
562         {
563                 vector targ_origin = ((self.enemy.absmin + self.enemy.absmax) * 0.5);
564                 targ_origin = WarpZone_RefSys_TransformOrigin(self.enemy, self, targ_origin); // origin of target as seen by the monster (us)
565                 WarpZone_TraceLine(self.origin, targ_origin, MOVE_NOMONSTERS, self);
566
567                 if((self.enemy == world)
568                         || (self.enemy.deadflag != DEAD_NO || self.enemy.health < 1)
569                         || (self.enemy.frozen)
570                         || (self.enemy.flags & FL_NOTARGET)
571                         || (self.enemy.alpha < 0.5 && self.enemy.alpha != 0)
572                         || (self.enemy.takedamage == DAMAGE_NO)
573                         || (vlen(self.origin - targ_origin) > self.target_range)
574                         || ((trace_fraction < 1) && (trace_ent != self.enemy)))
575                         //|| (time > self.ctf_droptime + autocvar_g_ctf_pass_timelimit)) // TODO: chase timelimit?
576                 {
577                         self.enemy = world;
578                         self.pass_distance = 0;
579                 }
580
581                 if(self.enemy)
582                 {
583                         /*WarpZone_TrailParticles(world, particleeffectnum(EFFECT_RED_PASS), self.origin, targ_origin);
584                         print("Trace origin: ", vtos(targ_origin), "\n");
585                         print("Target origin: ", vtos(self.enemy.origin), "\n");
586                         print("My origin: ", vtos(self.origin), "\n"); */
587
588                         self.monster_movestate = MONSTER_MOVE_ENEMY;
589                         self.last_trace = time + 1.2;
590                         if(self.monster_moveto)
591                                 return self.monster_moveto; // assumes code is properly setting this when monster has an enemy
592                         else
593                                 return targ_origin;
594                 }
595
596                 /*makevectors(self.angles);
597                 self.monster_movestate = MONSTER_MOVE_ENEMY;
598                 self.last_trace = time + 1.2;
599                 return self.enemy.origin; */
600         }
601
602         switch(self.monster_moveflags)
603         {
604                 case MONSTER_MOVE_FOLLOW:
605                 {
606                         self.monster_movestate = MONSTER_MOVE_FOLLOW;
607                         self.last_trace = time + 0.3;
608                         return (self.monster_follow) ? self.monster_follow.origin : self.origin;
609                 }
610                 case MONSTER_MOVE_SPAWNLOC:
611                 {
612                         self.monster_movestate = MONSTER_MOVE_SPAWNLOC;
613                         self.last_trace = time + 2;
614                         return self.pos1;
615                 }
616                 case MONSTER_MOVE_NOMOVE:
617                 {
618                         if(self.monster_moveto)
619                         {
620                                 self.last_trace = time + 0.5;
621                                 return self.monster_moveto;
622                         }
623                         else
624                         {
625                                 self.monster_movestate = MONSTER_MOVE_NOMOVE;
626                                 self.last_trace = time + 2;
627                         }
628                         return self.origin;
629                 }
630                 default:
631                 case MONSTER_MOVE_WANDER:
632                 {
633                         vector pos;
634                         self.monster_movestate = MONSTER_MOVE_WANDER;
635
636                         if(self.monster_moveto)
637                         {
638                                 self.last_trace = time + 0.5;
639                                 pos = self.monster_moveto;
640                         }
641                         else if(targ)
642                         {
643                                 self.last_trace = time + 0.5;
644                                 pos = targ.origin;
645                         }
646                         else
647                         {
648                                 self.last_trace = time + self.wander_delay;
649
650                                 self.angles_y = rint(random() * 500);
651                                 makevectors(self.angles);
652                                 pos = self.origin + v_forward * self.wander_distance;
653
654                                 if(((self.flags & FL_FLY) && (self.spawnflags & MONSTERFLAG_FLY_VERTICAL)) || (self.flags & FL_SWIM))
655                                 {
656                                         pos.z = random() * 200;
657                                         if(random() >= 0.5)
658                                                 pos.z *= -1;
659                                 }
660                         }
661
662                         return pos;
663                 }
664         }
665 }
666
667 void Monster_CalculateVelocity(entity mon, vector to, vector from, float turnrate, float movespeed)
668 {SELFPARAM();
669         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
670         float initial_height = 0; //min(50, (targ_distance * tanh(20)));
671         float current_height = (initial_height * min(1, (self.pass_distance) ? (current_distance / self.pass_distance) : current_distance));
672         //print("current_height = ", ftos(current_height), ", initial_height = ", ftos(initial_height), ".\n");
673
674         vector targpos;
675         if(current_height) // make sure we can actually do this arcing path
676         {
677                 targpos = (to + ('0 0 1' * current_height));
678                 WarpZone_TraceLine(mon.origin, targpos, MOVE_NOMONSTERS, mon);
679                 if(trace_fraction < 1)
680                 {
681                         //print("normal arc line failed, trying to find new pos...");
682                         WarpZone_TraceLine(to, targpos, MOVE_NOMONSTERS, mon);
683                         targpos = (trace_endpos + '0 0 -10');
684                         WarpZone_TraceLine(mon.origin, targpos, MOVE_NOMONSTERS, mon);
685                         if(trace_fraction < 1) { targpos = to; /* print(" ^1FAILURE^7, reverting to original direction.\n"); */ }
686                         /*else { print(" ^3SUCCESS^7, using new arc line.\n"); } */
687                 }
688         }
689         else { targpos = to; }
690
691         //mon.angles = normalize(('0 1 0' * to_y) - ('0 1 0' * from_y));
692
693         vector desired_direction = normalize(targpos - from);
694         if(turnrate) { mon.velocity = (normalize(normalize(mon.velocity) + (desired_direction * 50)) * movespeed); }
695         else { mon.velocity = (desired_direction * movespeed); }
696
697         //mon.steerto = steerlib_attract2(targpos, 0.5, 500, 0.95);
698         //mon.angles = vectoangles(mon.velocity);
699 }
700
701 void Monster_Move(float runspeed, float walkspeed, float stpspeed)
702 {SELFPARAM();
703         if(self.target2) { self.goalentity = find(world, targetname, self.target2); }
704
705         entity targ;
706
707         if(self.frozen == 2)
708         {
709                 self.revive_progress = bound(0, self.revive_progress + self.ticrate * self.revive_speed, 1);
710                 self.health = max(1, self.revive_progress * self.max_health);
711                 self.iceblock.alpha = bound(0.2, 1 - self.revive_progress, 1);
712
713                 if(!(self.spawnflags & MONSTERFLAG_INVINCIBLE) && self.sprite)
714                         WaypointSprite_UpdateHealth(self.sprite, self.health);
715
716                 movelib_beak_simple(stpspeed);
717                 setanim(self, self.anim_idle, true, false, false);
718
719                 self.enemy = world;
720                 self.nextthink = time + self.ticrate;
721
722                 if(self.revive_progress >= 1)
723                         Unfreeze(self);
724
725                 return;
726         }
727         else if(self.frozen == 3)
728         {
729                 self.revive_progress = bound(0, self.revive_progress - self.ticrate * self.revive_speed, 1);
730                 self.health = max(0, autocvar_g_nades_ice_health + (self.max_health-autocvar_g_nades_ice_health) * self.revive_progress );
731
732                 if(!(self.spawnflags & MONSTERFLAG_INVINCIBLE) && self.sprite)
733                         WaypointSprite_UpdateHealth(self.sprite, self.health);
734
735                 movelib_beak_simple(stpspeed);
736                 setanim(self, self.anim_idle, true, false, false);
737
738                 self.enemy = world;
739                 self.nextthink = time + self.ticrate;
740
741                 if(self.health < 1)
742                 {
743                         Unfreeze(self);
744                         self.health = 0;
745                         if(self.event_damage)
746                                 self.event_damage(self, self.frozen_by, 1, DEATH_NADE_ICE_FREEZE, self.origin, '0 0 0');
747                 }
748
749                 else if ( self.revive_progress <= 0 )
750                         Unfreeze(self);
751
752                 return;
753         }
754
755         if(self.flags & FL_SWIM)
756         {
757                 if(self.waterlevel < WATERLEVEL_WETFEET)
758                 {
759                         if(time >= self.last_trace)
760                         {
761                                 self.last_trace = time + 0.4;
762
763                                 Damage (self, world, world, 2, DEATH_DROWN, self.origin, '0 0 0');
764                                 self.angles = '90 90 0';
765                                 if(random() < 0.5)
766                                 {
767                                         self.velocity_y += random() * 50;
768                                         self.velocity_x -= random() * 50;
769                                 }
770                                 else
771                                 {
772                                         self.velocity_y -= random() * 50;
773                                         self.velocity_x += random() * 50;
774                                 }
775                                 self.velocity_z += random() * 150;
776                         }
777
778
779                         self.movetype = MOVETYPE_BOUNCE;
780                         //self.velocity_z = -200;
781
782                         return;
783                 }
784                 else if(self.movetype == MOVETYPE_BOUNCE)
785                 {
786                         self.angles_x = 0;
787                         self.movetype = MOVETYPE_WALK;
788                 }
789         }
790
791         targ = self.goalentity;
792
793         if (MUTATOR_CALLHOOK(MonsterMove, runspeed, walkspeed, targ)
794                 || gameover
795                 || self.draggedby != world
796                 || (round_handler_IsActive() && !round_handler_IsRoundStarted())
797                 || time < game_starttime
798                 || (autocvar_g_campaign && !campaign_bots_may_start)
799                 || time < self.spawn_time)
800         {
801                 runspeed = walkspeed = 0;
802                 if(time >= self.spawn_time)
803                         setanim(self, self.anim_idle, true, false, false);
804                 movelib_beak_simple(stpspeed);
805                 return;
806         }
807
808         targ = monster_target;
809         runspeed = bound(0, monster_speed_run * MONSTER_SKILLMOD(self), runspeed * 2.5); // limit maxspeed to prevent craziness
810         walkspeed = bound(0, monster_speed_walk * MONSTER_SKILLMOD(self), walkspeed * 2.5); // limit maxspeed to prevent craziness
811
812         if(time < self.spider_slowness)
813         {
814                 runspeed *= 0.5;
815                 walkspeed *= 0.5;
816         }
817
818         if(teamplay)
819         if(autocvar_g_monsters_teams)
820         if(DIFF_TEAM(self.monster_follow, self))
821                 self.monster_follow = world;
822
823         if(time >= self.last_enemycheck)
824         {
825                 if(!self.enemy)
826                 {
827                         self.enemy = Monster_FindTarget(self);
828                         if(self.enemy)
829                         {
830                                 WarpZone_RefSys_Copy(self.enemy, self);
831                                 WarpZone_RefSys_AddInverse(self.enemy, self); // wz1^-1 ... wzn^-1 receiver
832                                 self.moveto = WarpZone_RefSys_TransformOrigin(self.enemy, self, (0.5 * (self.enemy.absmin + self.enemy.absmax)));
833                                 self.monster_moveto = '0 0 0';
834                                 self.monster_face = '0 0 0';
835
836                                 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)));
837                                 Monster_Sound(monstersound_sight, 0, false, CH_VOICE);
838                         }
839                 }
840
841                 self.last_enemycheck = time + 1; // check for enemies every second
842         }
843
844         if(self.state == MONSTER_ATTACK_RANGED && (self.flags & FL_ONGROUND))
845         {
846                 self.state = 0;
847                 self.touch = Monster_Touch;
848         }
849
850         if(self.state && time >= self.attack_finished_single)
851                 self.state = 0; // attack is over
852
853         if(self.state != MONSTER_ATTACK_MELEE) // don't move if set
854         if(time >= self.last_trace || self.enemy) // update enemy or rider instantly
855                 self.moveto = Monster_Move_Target(targ);
856
857         if(!self.enemy)
858                 Monster_Sound(monstersound_idle, 7, true, CH_VOICE);
859
860         if(self.state == MONSTER_ATTACK_MELEE)
861                 self.moveto = self.origin;
862
863         if(self.enemy && self.enemy.vehicle)
864                 runspeed = 0;
865
866         if(!(self.spawnflags & MONSTERFLAG_FLY_VERTICAL) && !(self.flags & FL_SWIM))
867                 self.moveto_z = self.origin_z;
868
869         if(vlen(self.origin - self.moveto) > 100)
870         {
871                 float do_run = (self.enemy || self.monster_moveto);
872                 if((self.flags & FL_ONGROUND) || ((self.flags & FL_FLY) || (self.flags & FL_SWIM)))
873                         Monster_CalculateVelocity(self, self.moveto, self.origin, true, ((do_run) ? runspeed : walkspeed));
874
875                 if(time > self.pain_finished) // TODO: use anim_finished instead!
876                 if(!self.state)
877                 if(time > self.anim_finished)
878                 if(vlen(self.velocity) > 10)
879                         setanim(self, ((do_run) ? self.anim_run : self.anim_walk), true, false, false);
880                 else
881                         setanim(self, self.anim_idle, true, false, false);
882         }
883         else
884         {
885                 entity e = find(world, targetname, self.target2);
886                 if(e.target2)
887                         self.target2 = e.target2;
888                 else if(e.target)
889                         self.target2 = e.target;
890
891                 movelib_beak_simple(stpspeed);
892                 if(time > self.anim_finished)
893                 if(time > self.pain_finished)
894                 if(!self.state)
895                 if(vlen(self.velocity) <= 30)
896                         setanim(self, self.anim_idle, true, false, false);
897         }
898
899         self.steerto = steerlib_attract2(((self.monster_face) ? self.monster_face : self.moveto), 0.5, 500, 0.95);
900
901         vector real_angle = vectoangles(self.steerto) - self.angles;
902         float turny = 25;
903         if(self.state == MONSTER_ATTACK_MELEE)
904                 turny = 0;
905         if(turny)
906         {
907                 turny = bound(turny * -1, shortangle_f(real_angle.y, self.angles.y), turny);
908                 self.angles_y += turny;
909         }
910
911         Monster_Attack_Check(self, self.enemy);
912 }
913
914 void Monster_Remove(entity mon)
915 {
916         if(!mon) { return; }
917
918         if(!MUTATOR_CALLHOOK(MonsterRemove, mon))
919                 Send_Effect(EFFECT_ITEM_PICKUP, mon.origin, '0 0 0', 1);
920
921         if(mon.weaponentity) { remove(mon.weaponentity); }
922         if(mon.iceblock) { remove(mon.iceblock); }
923         WaypointSprite_Kill(mon.sprite);
924         remove(mon);
925 }
926
927 void Monster_Dead_Think()
928 {SELFPARAM();
929         self.nextthink = time + self.ticrate;
930
931         if(self.monster_lifetime != 0)
932         if(time >= self.monster_lifetime)
933         {
934                 Monster_Dead_Fade();
935                 return;
936         }
937 }
938
939 void Monster_Appear()
940 {SELFPARAM();
941         self.enemy = activator;
942         self.spawnflags &= ~MONSTERFLAG_APPEAR; // otherwise, we get an endless loop
943         Monster_Spawn(self.monsterid);
944 }
945
946 float Monster_Appear_Check(entity ent, float monster_id)
947 {
948         if(!(ent.spawnflags & MONSTERFLAG_APPEAR))
949                 return false;
950
951         ent.think = func_null;
952         ent.monsterid = monster_id; // set so this monster is properly registered (otherwise, normal initialization is used)
953         ent.nextthink = 0;
954         ent.use = Monster_Appear;
955         ent.flags = FL_MONSTER; // set so this monster can get butchered
956
957         return true;
958 }
959
960 void Monster_Reset()
961 {SELFPARAM();
962         setorigin(self, self.pos1);
963         self.angles = self.pos2;
964
965         Unfreeze(self); // remove any icy remains
966
967         self.health = self.max_health;
968         self.velocity = '0 0 0';
969         self.enemy = world;
970         self.goalentity = world;
971         self.attack_finished_single = 0;
972         self.moveto = self.origin;
973 }
974
975 void Monster_Dead_Damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
976 {SELFPARAM();
977         self.health -= damage;
978
979         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
980
981         if(self.health <= -100) // 100 health until gone?
982         {
983                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
984
985                 // number of monsters spawned with mobspawn command
986                 totalspawned -= 1;
987
988                 self.think = SUB_Remove;
989                 self.nextthink = time + 0.1;
990                 self.event_damage = func_null;
991         }
992 }
993
994 void Monster_Dead(entity attacker, float gibbed)
995 {SELFPARAM();
996         self.think = Monster_Dead_Think;
997         self.nextthink = time;
998         self.monster_lifetime = time + 5;
999
1000         if(self.frozen)
1001         {
1002                 Unfreeze(self); // remove any icy remains
1003                 self.health = 0; // reset by Unfreeze
1004         }
1005
1006         monster_dropitem();
1007
1008         Monster_Sound(monstersound_death, 0, false, CH_VOICE);
1009
1010         if(!(self.spawnflags & MONSTERFLAG_SPAWNED) && !(self.spawnflags & MONSTERFLAG_RESPAWNED))
1011                 monsters_killed += 1;
1012
1013         if(IS_PLAYER(attacker))
1014         if(autocvar_g_monsters_score_spawned || !((self.spawnflags & MONSTERFLAG_SPAWNED) || (self.spawnflags & MONSTERFLAG_RESPAWNED)))
1015                 PlayerScore_Add(attacker, SP_SCORE, +autocvar_g_monsters_score_kill);
1016
1017         if(gibbed)
1018         {
1019                 // number of monsters spawned with mobspawn command
1020                 totalspawned -= 1;
1021         }
1022
1023         self.event_damage       = ((gibbed) ? func_null : Monster_Dead_Damage);
1024         self.solid                      = SOLID_CORPSE;
1025         self.takedamage         = DAMAGE_AIM;
1026         self.deadflag           = DEAD_DEAD;
1027         self.enemy                      = world;
1028         self.movetype           = MOVETYPE_TOSS;
1029         self.moveto                     = self.origin;
1030         self.touch                      = Monster_Touch; // reset incase monster was pouncing
1031         self.reset                      = func_null;
1032         self.state                      = 0;
1033         self.attack_finished_single = 0;
1034         self.effects = 0;
1035
1036         if(!((self.flags & FL_FLY) || (self.flags & FL_SWIM)))
1037                 self.velocity = '0 0 0';
1038
1039         CSQCModel_UnlinkEntity();
1040
1041         MON_ACTION(self.monsterid, MR_DEATH);
1042
1043         if(self.candrop && self.weapon)
1044                 W_ThrowNewWeapon(self, self.weapon, 0, self.origin, randomvec() * 150 + '0 0 325');
1045 }
1046
1047 void Monster_Damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
1048 {SELFPARAM();
1049         if((self.spawnflags & MONSTERFLAG_INVINCIBLE) && deathtype != DEATH_KILL && !ITEM_DAMAGE_NEEDKILL(deathtype))
1050                 return;
1051
1052         if(self.frozen && deathtype != DEATH_KILL && deathtype != DEATH_NADE_ICE_FREEZE)
1053                 return;
1054
1055         //if(time < self.pain_finished && deathtype != DEATH_KILL)
1056                 //return;
1057
1058         if(time < self.spawnshieldtime && deathtype != DEATH_KILL)
1059                 return;
1060
1061         if(deathtype == DEATH_FALL && self.draggedby != world)
1062                 return;
1063
1064         vector v;
1065         float take, save;
1066
1067         v = healtharmor_applydamage(100, self.armorvalue / 100, deathtype, damage);
1068         take = v_x;
1069         save = v_y;
1070
1071         damage_take = take;
1072         frag_attacker = attacker;
1073         frag_deathtype = deathtype;
1074         MON_ACTION(self.monsterid, MR_PAIN);
1075         take = damage_take;
1076
1077         if(take)
1078         {
1079                 self.health -= take;
1080                 Monster_Sound(monstersound_pain, 1.2, true, CH_PAIN);
1081         }
1082
1083         if(self.sprite)
1084                 WaypointSprite_UpdateHealth(self.sprite, self.health);
1085
1086         self.dmg_time = time;
1087
1088         if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN)
1089                 spamsound (self, CH_PAIN, SND(BODYIMPACT1), VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
1090
1091         self.velocity += force * self.damageforcescale;
1092
1093         if(deathtype != DEATH_DROWN && take)
1094         {
1095                 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, self, attacker);
1096                 if (take > 50)
1097                         Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
1098                 if (take > 100)
1099                         Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
1100         }
1101
1102         if(self.health <= 0)
1103         {
1104                 if(deathtype == DEATH_KILL)
1105                         self.candrop = false; // killed by mobkill command
1106
1107                 // TODO: fix this?
1108                 activator = attacker;
1109                 other = self.enemy;
1110                 SUB_UseTargets();
1111                 self.target2 = self.oldtarget2; // reset to original target on death, incase we respawn
1112
1113                 Monster_Dead(attacker, (self.health <= -100 || deathtype == DEATH_KILL));
1114
1115                 WaypointSprite_Kill(self.sprite);
1116
1117                 frag_target = self;
1118                 MUTATOR_CALLHOOK(MonsterDies, attacker);
1119
1120                 if(self.health <= -100 || deathtype == DEATH_KILL) // check if we're already gibbed
1121                 {
1122                         Violence_GibSplash(self, 1, 0.5, attacker);
1123
1124                         self.think = SUB_Remove;
1125                         self.nextthink = time + 0.1;
1126                 }
1127         }
1128 }
1129
1130 // don't check for enemies, just keep walking in a straight line
1131 void Monster_Move_2D(float mspeed, float allow_jumpoff)
1132 {SELFPARAM();
1133         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)
1134         {
1135                 mspeed = 0;
1136                 if(time >= self.spawn_time)
1137                         setanim(self, self.anim_idle, true, false, false);
1138                 movelib_beak_simple(0.6);
1139                 return;
1140         }
1141
1142         float reverse = FALSE;
1143         vector a, b;
1144
1145         makevectors(self.angles);
1146         a = self.origin + '0 0 16';
1147         b = self.origin + '0 0 16' + v_forward * 32;
1148
1149         traceline(a, b, MOVE_NORMAL, self);
1150
1151         if(trace_fraction != 1.0)
1152         {
1153                 reverse = TRUE;
1154
1155                 if(trace_ent)
1156                 if(IS_PLAYER(trace_ent) && !(trace_ent.items & IT_STRENGTH))
1157                         reverse = FALSE;
1158         }
1159
1160         // TODO: fix this... tracing is broken if the floor is thin
1161         /*
1162         if(!allow_jumpoff)
1163         {
1164                 a = b - '0 0 32';
1165                 traceline(b, a, MOVE_WORLDONLY, self);
1166                 if(trace_fraction == 1.0)
1167                         reverse = TRUE;
1168         } */
1169
1170         if(reverse)
1171         {
1172                 self.angles_y = anglemods(self.angles_y - 180);
1173                 makevectors(self.angles);
1174         }
1175
1176         movelib_move_simple_gravity(v_forward, mspeed, 1);
1177
1178         if(time > self.pain_finished)
1179         if(time > self.attack_finished_single)
1180         if(vlen(self.velocity) > 10)
1181                 setanim(self, self.anim_walk, true, false, false);
1182         else
1183                 setanim(self, self.anim_idle, true, false, false);
1184 }
1185
1186 void Monster_Anim()
1187 {SELFPARAM();
1188         int deadbits = (self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
1189         if(self.deadflag)
1190         {
1191                 if (!deadbits)
1192                 {
1193                         // Decide on which death animation to use.
1194                         if(random() < 0.5)
1195                                 deadbits = ANIMSTATE_DEAD1;
1196                         else
1197                                 deadbits = ANIMSTATE_DEAD2;
1198                 }
1199         }
1200         else
1201         {
1202                 // Clear a previous death animation.
1203                 deadbits = 0;
1204         }
1205         int animbits = deadbits;
1206         if(self.frozen)
1207                 animbits |= ANIMSTATE_FROZEN;
1208         if(self.crouch)
1209                 animbits |= ANIMSTATE_DUCK; // not that monsters can crouch currently...
1210         animdecide_setstate(self, animbits, false);
1211         animdecide_setimplicitstate(self, (self.flags & FL_ONGROUND));
1212
1213         /* // weapon entities for monsters?
1214         if (self.weaponentity)
1215         {
1216                 updateanim(self.weaponentity);
1217                 if (!self.weaponentity.animstate_override)
1218                         setanim(self.weaponentity, self.weaponentity.anim_idle, true, false, false);
1219         }
1220         */
1221 }
1222
1223 void Monster_Think()
1224 {SELFPARAM();
1225         self.think = Monster_Think;
1226         self.nextthink = self.ticrate;
1227
1228         if(self.monster_lifetime)
1229         if(time >= self.monster_lifetime)
1230         {
1231                 Damage(self, self, self, self.health + self.max_health, DEATH_KILL, self.origin, self.origin);
1232                 return;
1233         }
1234
1235         if(MON_ACTION(self.monsterid, MR_THINK))
1236                 Monster_Move(self.speed2, self.speed, self.stopspeed);
1237
1238         Monster_Anim();
1239
1240         CSQCMODEL_AUTOUPDATE(self);
1241 }
1242
1243 float Monster_Spawn_Setup()
1244 {SELFPARAM();
1245         MON_ACTION(self.monsterid, MR_SETUP);
1246
1247         // ensure some basic needs are met
1248         if(!self.health) { self.health = 100; }
1249         if(!self.armorvalue) { self.armorvalue = bound(0.2, 0.5 * MONSTER_SKILLMOD(self), 0.9); }
1250         if(!self.target_range) { self.target_range = autocvar_g_monsters_target_range; }
1251         if(!self.respawntime) { self.respawntime = autocvar_g_monsters_respawn_delay; }
1252         if(!self.monster_moveflags) { self.monster_moveflags = MONSTER_MOVE_WANDER; }
1253         if(!self.attack_range) { self.attack_range = autocvar_g_monsters_attack_range; }
1254         if(!self.damageforcescale) { self.damageforcescale = autocvar_g_monsters_damageforcescale; }
1255
1256         if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
1257         {
1258                 Monster_Miniboss_Check();
1259                 self.health *= MONSTER_SKILLMOD(self);
1260
1261                 if(!self.skin)
1262                         self.skin = rint(random() * 4);
1263         }
1264
1265         self.max_health = self.health;
1266         self.pain_finished = self.nextthink;
1267
1268         if(IS_PLAYER(self.monster_follow))
1269                 self.effects |= EF_DIMLIGHT;
1270
1271         if(!self.wander_delay) { self.wander_delay = 2; }
1272         if(!self.wander_distance) { self.wander_distance = 600; }
1273
1274         Monster_Sounds_Precache();
1275         Monster_Sounds_Update();
1276
1277         if(teamplay)
1278                 self.monster_attack = true; // we can have monster enemies in team games
1279
1280         Monster_Sound(monstersound_spawn, 0, false, CH_VOICE);
1281
1282         if(autocvar_g_monsters_healthbars)
1283         {
1284                 entity wp = WaypointSprite_Spawn(WP_Monster, 0, 1024, self, '0 0 1' * (self.maxs.z + 15), world, self.team, self, sprite, true, RADARICON_DANGER);
1285                 wp.wp_extra = self.monsterid;
1286                 wp.colormod = ((self.team) ? Team_ColorRGB(self.team) : '1 0 0');
1287                 if(!(self.spawnflags & MONSTERFLAG_INVINCIBLE))
1288                 {
1289                         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
1290                         WaypointSprite_UpdateHealth(self.sprite, self.health);
1291                 }
1292         }
1293
1294         self.think = Monster_Think;
1295         self.nextthink = time + self.ticrate;
1296
1297         if(MUTATOR_CALLHOOK(MonsterSpawn))
1298                 return false;
1299
1300         return true;
1301 }
1302
1303 bool Monster_Spawn(int mon_id)
1304 {SELFPARAM();
1305         // setup the basic required properties for a monster
1306         entity mon = get_monsterinfo(mon_id);
1307         if(!mon.monsterid) { return false; } // invalid monster
1308
1309         if(!autocvar_g_monsters) { Monster_Remove(self); return false; }
1310
1311         self.mdl = mon.model;
1312         if(Monster_Appear_Check(self, mon_id)) { return true; } // return true so the monster isn't removed
1313
1314         if(!self.monster_skill)
1315                 self.monster_skill = cvar("g_monsters_skill");
1316
1317         // support for quake style removing monsters based on skill
1318         if(self.monster_skill == MONSTER_SKILL_EASY) if(self.spawnflags & MONSTERSKILL_NOTEASY) { Monster_Remove(self); return false; }
1319         if(self.monster_skill == MONSTER_SKILL_MEDIUM) if(self.spawnflags & MONSTERSKILL_NOTMEDIUM) { Monster_Remove(self); return false; }
1320         if(self.monster_skill == MONSTER_SKILL_HARD) if(self.spawnflags & MONSTERSKILL_NOTHARD) { Monster_Remove(self); return false; }
1321
1322         if(self.team && !teamplay)
1323                 self.team = 0;
1324
1325         if(!(self.spawnflags & MONSTERFLAG_SPAWNED)) // naturally spawned monster
1326         if(!(self.spawnflags & MONSTERFLAG_RESPAWNED)) // don't count re-spawning monsters either
1327                 monsters_total += 1;
1328
1329         _setmodel(self, self.mdl);
1330         self.flags                              = FL_MONSTER;
1331         self.classname                  = "monster";
1332         self.takedamage                 = DAMAGE_AIM;
1333         self.bot_attack                 = true;
1334         self.iscreature                 = true;
1335         self.teleportable               = true;
1336         self.damagedbycontents  = true;
1337         self.monsterid                  = mon_id;
1338         self.event_damage               = Monster_Damage;
1339         self.touch                              = Monster_Touch;
1340         self.use                                = Monster_Use;
1341         self.solid                              = SOLID_BBOX;
1342         self.movetype                   = MOVETYPE_WALK;
1343         self.spawnshieldtime    = time + autocvar_g_monsters_spawnshieldtime;
1344         self.enemy                              = world;
1345         self.velocity                   = '0 0 0';
1346         self.moveto                             = self.origin;
1347         self.pos1                               = self.origin;
1348         self.pos2                               = self.angles;
1349         self.reset                              = Monster_Reset;
1350         self.netname                    = mon.netname;
1351         self.monster_attackfunc = mon.monster_attackfunc;
1352         self.monster_name               = mon.monster_name;
1353         self.candrop                    = true;
1354         self.view_ofs                   = '0 0 0.7' * (self.maxs_z * 0.5);
1355         self.oldtarget2                 = self.target2;
1356         self.pass_distance              = 0;
1357         self.deadflag                   = DEAD_NO;
1358         self.noalign                    = ((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM));
1359         self.spawn_time                 = time;
1360         self.spider_slowness    = 0;
1361         self.gravity                    = 1;
1362         self.monster_moveto             = '0 0 0';
1363         self.monster_face               = '0 0 0';
1364         self.dphitcontentsmask  = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1365
1366         if(!self.scale) { self.scale = 1; }
1367         if(autocvar_g_monsters_edit) { self.grab = 1; }
1368         if(autocvar_g_fullbrightplayers) { self.effects |= EF_FULLBRIGHT; }
1369         if(autocvar_g_nodepthtestplayers) { self.effects |= EF_NODEPTHTEST; }
1370         if(mon.spawnflags & MONSTER_TYPE_SWIM) { self.flags |= FL_SWIM; }
1371
1372         if(mon.spawnflags & MONSTER_TYPE_FLY)
1373         {
1374                 self.flags |= FL_FLY;
1375                 self.movetype = MOVETYPE_FLY;
1376         }
1377
1378         if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1379         if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
1380                 self.scale *= 1.3;
1381
1382         setsize(self, mon.mins * self.scale, mon.maxs * self.scale);
1383
1384         self.ticrate = bound(sys_frametime, ((!self.ticrate) ? autocvar_g_monsters_think_delay : self.ticrate), 60);
1385
1386         Monster_UpdateModel();
1387
1388         if(!Monster_Spawn_Setup())
1389         {
1390                 Monster_Remove(self);
1391                 return false;
1392         }
1393
1394         if(!self.noalign)
1395         {
1396                 setorigin(self, self.origin + '0 0 20');
1397                 tracebox(self.origin + '0 0 64', self.mins, self.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1398                 setorigin(self, trace_endpos);
1399         }
1400
1401         if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
1402                 monster_setupcolors(self);
1403
1404         CSQCMODEL_AUTOINIT(self);
1405
1406         return true;
1407 }