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