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