]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/monsters/sv_monsters.qc
Change scale if monster is a mini-boss
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / sv_monsters.qc
1 // =========================
2 //  SVQC Monster Properties
3 // =========================
4
5
6 void M_Item_Touch ()
7 {
8         if(self && IS_PLAYER(other) && other.deadflag == DEAD_NO)
9         {
10                 Item_Touch();
11                 self.think = SUB_Remove;
12                 self.nextthink = time + 0.1;
13         }
14 }
15
16 void monster_item_spawn()
17 {
18         if(self.monster_loot)
19                 self.monster_loot();
20
21         self.gravity = 1;
22         self.velocity = randomvec() * 175 + '0 0 325';
23         self.touch = M_Item_Touch;
24
25         SUB_SetFade(self, time + autocvar_g_monsters_drop_time, 1);
26 }
27
28 void monster_dropitem()
29 {
30         if(!self.candrop || !self.monster_loot)
31                 return;
32
33         vector org = self.origin + ((self.mins + self.maxs) * 0.5);
34         entity e = spawn();
35
36         setorigin(e, org);
37
38         e.monster_loot = self.monster_loot;
39
40         other = e;
41         MUTATOR_CALLHOOK(MonsterDropItem);
42         e = other;
43
44         e.think = monster_item_spawn;
45         e.nextthink = time + 0.3;
46 }
47
48 void monsters_setframe(float _frame)
49 {
50         if(self.frame == _frame)
51                 return;
52
53         self.anim_start_time = time;
54         self.frame = _frame;
55         self.SendFlags |= MSF_ANIM;
56 }
57
58 float monster_isvalidtarget (entity targ, entity ent)
59 {
60         if(!targ || !ent)
61                 return FALSE; // someone doesn't exist
62
63         if(targ == ent)
64                 return FALSE; // don't attack ourselves
65
66         traceline(ent.origin, targ.origin, MOVE_NORMAL, ent);
67
68         if(trace_ent != targ)
69                 return FALSE;
70
71         if(targ.vehicle_flags & VHF_ISVEHICLE)
72         if not((get_monsterinfo(ent.monsterid)).spawnflags & MON_FLAG_RANGED)
73                 return FALSE; // melee attacks are useless against vehicles
74
75         if(time < game_starttime)
76                 return FALSE; // monsters do nothing before the match has started
77
78         if(vlen(targ.origin - ent.origin) >= ent.target_range)
79                 return FALSE; // enemy is too far away
80
81         if(targ.takedamage == DAMAGE_NO)
82                 return FALSE; // enemy can't be damaged
83
84         if(targ.items & IT_INVISIBILITY)
85                 return FALSE; // enemy is invisible
86
87         if(substring(targ.classname, 0, 10) == "onslaught_")
88                 return FALSE; // don't attack onslaught targets
89
90         if(IS_SPEC(targ) || IS_OBSERVER(targ))
91                 return FALSE; // enemy is a spectator
92
93         if not(targ.vehicle_flags & VHF_ISVEHICLE)
94         if(targ.deadflag != DEAD_NO || ent.deadflag != DEAD_NO || targ.health <= 0 || ent.health <= 0)
95                 return FALSE; // enemy/self is dead
96
97         if(ent.monster_owner == targ)
98                 return FALSE; // don't attack our master
99
100         if(targ.monster_owner == ent)
101                 return FALSE; // don't attack our pet
102
103         if not(targ.vehicle_flags & VHF_ISVEHICLE)
104         if(targ.flags & FL_NOTARGET)
105                 return FALSE; // enemy can't be targeted
106
107         if not(autocvar_g_monsters_typefrag)
108         if(targ.BUTTON_CHAT)
109                 return FALSE; // no typefragging!
110
111         if not(IsDifferentTeam(targ, ent))
112                 return FALSE; // enemy is on our team
113
114         if(autocvar_g_monsters_target_infront || ent.spawnflags & MONSTERFLAG_INFRONT)
115         if(ent.enemy != targ)
116         {
117                 float dot;
118
119                 makevectors (ent.angles);
120                 dot = normalize (targ.origin - ent.origin) * v_forward;
121
122                 if(dot <= 0.3)
123                         return FALSE;
124         }
125
126         return TRUE;
127 }
128
129 entity FindTarget (entity ent)
130 {
131         if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return ent.enemy; } // Handled by a mutator
132
133         entity head, closest_target = world;
134         head = findradius(ent.origin, ent.target_range);
135
136         while(head) // find the closest acceptable target to pass to
137         {
138                 if(head.monster_attack)
139                 if(monster_isvalidtarget(head, ent))
140                 {
141                         // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
142                         vector head_center = CENTER_OR_VIEWOFS(head);
143                         vector ent_center = CENTER_OR_VIEWOFS(ent);
144
145                         //if(ctf_CheckPassDirection(head_center, ent_center, ent.v_angle, head.WarpZone_findradius_nearest))
146                         if(closest_target)
147                         {
148                                 vector closest_target_center = CENTER_OR_VIEWOFS(closest_target);
149                                 if(vlen(ent_center - head_center) < vlen(ent_center - closest_target_center))
150                                         { closest_target = head; }
151                         }
152                         else { closest_target = head; }
153                 }
154
155                 head = head.chain;
156         }
157
158         return closest_target;
159 }
160
161 void MonsterTouch ()
162 {
163         if(other == world)
164                 return;
165
166         if(self.enemy != other)
167         if not(other.flags & FL_MONSTER)
168         if(monster_isvalidtarget(other, self))
169                 self.enemy = other;
170 }
171
172 string get_monster_model_datafilename(string m, float sk, string fil)
173 {
174         if(m)
175                 m = strcat(m, "_");
176         else
177                 m = "models/monsters/*_";
178         if(sk >= 0)
179                 m = strcat(m, ftos(sk));
180         else
181                 m = strcat(m, "*");
182         return strcat(m, ".", fil);
183 }
184
185 void PrecacheMonsterSounds(string f)
186 {
187         float fh;
188         string s;
189         fh = fopen(f, FILE_READ);
190         if(fh < 0)
191                 return;
192         while((s = fgets(fh)))
193         {
194                 if(tokenize_console(s) != 3)
195                 {
196                         dprint("Invalid sound info line: ", s, "\n");
197                         continue;
198                 }
199                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
200         }
201         fclose(fh);
202 }
203
204 void precache_monstersounds()
205 {
206         string m = (get_monsterinfo(self.monsterid)).model;
207         float globhandle, n, i;
208         string f;
209
210         globhandle = search_begin(strcat(m, "_*.sounds"), TRUE, FALSE);
211         if (globhandle < 0)
212                 return;
213         n = search_getsize(globhandle);
214         for (i = 0; i < n; ++i)
215         {
216                 //print(search_getfilename(globhandle, i), "\n");
217                 f = search_getfilename(globhandle, i);
218                 PrecacheMonsterSounds(f);
219         }
220         search_end(globhandle);
221 }
222
223 void ClearMonsterSounds()
224 {
225 #define _MSOUND(m) if(self.monstersound_##m) { strunzone(self.monstersound_##m); self.monstersound_##m = string_null; }
226         ALLMONSTERSOUNDS
227 #undef _MSOUND
228 }
229
230 .string GetMonsterSoundSampleField(string type)
231 {
232         GetMonsterSoundSampleField_notFound = 0;
233         switch(type)
234         {
235 #define _MSOUND(m) case #m: return monstersound_##m;
236                 ALLMONSTERSOUNDS
237 #undef _MSOUND
238         }
239         GetMonsterSoundSampleField_notFound = 1;
240         return string_null;
241 }
242
243 float LoadMonsterSounds(string f, float first)
244 {
245         float fh;
246         string s;
247         var .string field;
248         fh = fopen(f, FILE_READ);
249         if(fh < 0)
250         {
251                 dprint("Monster sound file not found: ", f, "\n");
252                 return 0;
253         }
254         while((s = fgets(fh)))
255         {
256                 if(tokenize_console(s) != 3)
257                         continue;
258                 field = GetMonsterSoundSampleField(argv(0));
259                 if(GetMonsterSoundSampleField_notFound)
260                         continue;
261                 if(self.field)
262                         strunzone(self.field);
263                 self.field = strzone(strcat(argv(1), " ", argv(2)));
264         }
265         fclose(fh);
266         return 1;
267 }
268
269 .float skin_for_monstersound;
270 void UpdateMonsterSounds()
271 {
272         entity mon = get_monsterinfo(self.monsterid);
273
274         if(self.skin == self.skin_for_monstersound)
275                 return;
276         self.skin_for_monstersound = self.skin;
277         ClearMonsterSounds();
278         //LoadMonsterSounds("sound/monsters/default.sounds", 1);
279         if(!autocvar_g_debug_defaultsounds)
280         if(!LoadMonsterSounds(get_monster_model_datafilename(mon.model, self.skin, "sounds"), 0))
281                 LoadMonsterSounds(get_monster_model_datafilename(mon.model, 0, "sounds"), 0);
282 }
283
284 void MonsterSound(.string samplefield, float sound_delay, float delaytoo, float chan)
285 {
286         if(delaytoo && time < self.msound_delay)
287                 return; // too early
288         GlobalSound(self.samplefield, chan, VOICETYPE_PLAYERSOUND);
289
290         self.msound_delay = time + sound_delay;
291 }
292
293 void monster_makevectors(entity e)
294 {
295         vector v;
296
297         v = CENTER_OR_VIEWOFS(e);
298         self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
299         self.v_angle_x = -self.v_angle_x;
300
301         makevectors(self.v_angle);
302 }
303
304 float monster_melee(entity targ, float damg, float anim, float er, float anim_finished, float deathtype, float dostop)
305 {
306         float rdmg = damg * random();
307
308         if (self.health <= 0)
309                 return FALSE; // attacking while dead?!
310
311         if(dostop)
312         {
313                 self.velocity_x = 0;
314                 self.velocity_y = 0;
315                 self.state = MONSTER_STATE_ATTACK_MELEE;
316                 self.SendFlags |= MSF_MOVE;
317         }
318
319         monsters_setframe(anim);
320
321         if(anim_finished != 0)
322                 self.attack_finished_single = time + anim_finished;
323
324         monster_makevectors(targ);
325
326         traceline(self.origin + self.view_ofs, self.origin + v_forward * er, 0, self);
327
328         if(trace_ent.takedamage)
329                 Damage(trace_ent, self, self, rdmg * monster_skill, deathtype, trace_ent.origin, normalize(trace_ent.origin - self.origin));
330
331         return TRUE;
332 }
333
334 void Monster_CheckMinibossFlag ()
335 {
336         if(MUTATOR_CALLHOOK(MonsterCheckBossFlag))
337                 return;
338
339         float chance = random() * 100;
340
341         // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
342         if ((self.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
343         {
344                 self.health += autocvar_g_monsters_miniboss_healthboost;
345                 if not(self.weapon)
346                         self.weapon = WEP_NEX;
347                 
348                 entity mon = get_monsterinfo(self.monsterid);
349                 
350                 if(mon.spawnflags & MONSTER_SIZE_BROKEN)
351                         self.scale = 1.95;
352                 else
353                         self.scale = 1.5;
354                         
355                 setsize(self, mon.mins * 1.5, mon.maxs * 1.5);
356                 setorigin(self, self.origin + '0 0 25'); // offset so we don't fall through the floor
357         }
358 }
359
360 float Monster_CanRespawn(entity ent)
361 {
362         other = ent;
363         if(MUTATOR_CALLHOOK(MonsterRespawn))
364                 return TRUE; // enabled by a mutator
365
366         if(ent.spawnflags & MONSTERFLAG_NORESPAWN)
367                 return FALSE;
368
369         if not(autocvar_g_monsters_respawn)
370                 return FALSE;
371
372         return TRUE;
373 }
374
375 void Monster_Fade ()
376 {
377         if(Monster_CanRespawn(self))
378         {
379                 self.monster_respawned = TRUE;
380                 self.think = self.monster_spawnfunc;
381                 self.nextthink = time + self.respawntime;
382                 self.deadflag = DEAD_RESPAWNING;
383                 if(self.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
384                 {
385                         self.pos1 = self.origin;
386                         self.pos2 = self.angles;
387                 }
388                 self.event_damage = func_null;
389                 self.takedamage = DAMAGE_NO;
390                 setorigin(self, self.pos1);
391                 self.angles = self.pos2;
392                 self.health = self.max_health;
393
394                 self.SendFlags |= MSF_MOVE;
395                 self.SendFlags |= MSF_STATUS;
396         }
397         else
398                 SUB_SetFade(self, time + 3, 1);
399 }
400
401 float Monster_CanJump (vector vel)
402 {
403         if(self.state)
404                 return FALSE; // already attacking
405         if not(self.flags & FL_ONGROUND)
406                 return FALSE; // not on the ground
407         if(self.health <= 0)
408                 return FALSE; // called when dead?
409         if(time < self.attack_finished_single)
410                 return FALSE; // still attacking
411
412         vector old = self.velocity;
413
414         self.velocity = vel;
415         tracetoss(self, self);
416         self.velocity = old;
417         if (trace_ent != self.enemy)
418                 return FALSE;
419
420         return TRUE;
421 }
422
423 float monster_leap (float anm, void() touchfunc, vector vel, float anim_finished)
424 {
425         if(!Monster_CanJump(vel))
426                 return FALSE;
427
428         monsters_setframe(anm);
429         self.state = MONSTER_STATE_ATTACK_LEAP;
430         self.touch = touchfunc;
431         self.origin_z += 1;
432         self.velocity = vel;
433         self.flags &= ~FL_ONGROUND;
434
435         self.attack_finished_single = time + anim_finished;
436
437         return TRUE;
438 }
439
440 void monster_checkattack(entity e, entity targ)
441 {
442         if(e == world)
443                 return;
444         if(targ == world)
445                 return;
446
447         if not(e.monster_attackfunc)
448                 return;
449
450         if(time < e.attack_finished_single)
451                 return;
452
453         if(vlen(targ.origin - e.origin) <= e.attack_range)
454         if(e.monster_attackfunc(MONSTER_ATTACK_MELEE))
455         {
456                 //monster_sound(e.msound_attack_melee, 0, FALSE);
457                 MonsterSound(monstersound_melee, 0, FALSE, CH_VOICE);
458                 return;
459         }
460
461         if(vlen(targ.origin - e.origin) > e.attack_range)
462         if(e.monster_attackfunc(MONSTER_ATTACK_RANGED))
463         {
464                 //monster_sound(e.msound_attack_ranged, 0, FALSE);
465                 MonsterSound(monstersound_ranged, 0, FALSE, CH_VOICE);
466                 return;
467         }
468 }
469
470 void monster_use ()
471 {
472         if not(self.enemy)
473         if(self.health > 0)
474         if(monster_isvalidtarget(activator, self))
475                 self.enemy = activator;
476 }
477
478 .float last_trace;
479 .float last_enemycheck; // for checking enemy
480 vector monster_pickmovetarget(entity targ)
481 {
482         // enemy is always preferred target
483         if(self.enemy)
484         {
485                 makevectors(self.angles);
486                 self.monster_movestate = MONSTER_MOVE_ENEMY;
487                 self.last_trace = time + 1.2;
488                 return self.enemy.origin;
489         }
490
491         switch(self.monster_moveflags)
492         {
493                 case MONSTER_MOVE_OWNER:
494                 {
495                         self.monster_movestate = MONSTER_MOVE_OWNER;
496                         self.last_trace = time + 0.3;
497                         return (self.monster_owner) ? self.monster_owner.origin : self.origin;
498                 }
499                 case MONSTER_MOVE_SPAWNLOC:
500                 {
501                         self.monster_movestate = MONSTER_MOVE_SPAWNLOC;
502                         self.last_trace = time + 2;
503                         return self.pos1;
504                 }
505                 case MONSTER_MOVE_NOMOVE:
506                 {
507                         self.monster_movestate = MONSTER_MOVE_NOMOVE;
508                         self.last_trace = time + 2;
509                         return self.origin;
510                 }
511                 default:
512                 case MONSTER_MOVE_WANDER:
513                 {
514                         vector pos;
515                         self.monster_movestate = MONSTER_MOVE_WANDER;
516                         self.last_trace = time + 2;
517
518                         self.angles_y = rint(random() * 500);
519                         makevectors(self.angles);
520                         pos = self.origin + v_forward * 600;
521
522                         if(self.flags & FL_FLY || self.flags & FL_SWIM)
523                         if(self.spawnflags & MONSTERFLAG_FLY_VERTICAL)
524                         {
525                                 pos_z = random() * 200;
526                                 if(random() >= 0.5)
527                                         pos_z *= -1;
528                         }
529
530                         if(targ)
531                         {
532                                 self.last_trace = time + 0.5;
533                                 pos = targ.origin;
534                         }
535
536                         return pos;
537                 }
538         }
539 }
540
541 void monster_move(float runspeed, float walkspeed, float stopspeed, float manim_run, float manim_walk, float manim_idle)
542 {
543         fixedmakevectors(self.angles);
544
545         if(self.target2)
546                 self.goalentity = find(world, targetname, self.target2);
547
548         entity targ;
549
550         if(self.frozen)
551         {
552                 self.revive_progress = bound(0, self.revive_progress + frametime * self.revive_speed, 1);
553                 self.health = max(1, self.max_health * self.revive_progress);
554
555                 self.SendFlags |= MSF_STATUS;
556
557                 movelib_beak_simple(stopspeed);
558
559                 self.velocity = '0 0 0';
560                 self.enemy = world;
561                 self.nextthink = time + 0.1;
562
563                 if(self.revive_progress >= 1)
564                         Unfreeze(self); // wait for next think before attacking
565
566                 // don't bother updating angles here?
567                 if(self.origin != self.oldorigin)
568                 {
569                         self.oldorigin = self.origin;
570                         self.SendFlags |= MSF_MOVE;
571                 }
572
573                 return; // no moving while frozen
574         }
575
576         if(self.flags & FL_SWIM)
577         {
578                 if(self.waterlevel < WATERLEVEL_WETFEET)
579                 {
580                         if(time >= self.last_trace)
581                         {
582                                 self.fish_wasdrowning = TRUE;
583                                 self.last_trace = time + 0.4;
584
585                                 Damage (self, world, world, 2, DEATH_DROWN, self.origin, '0 0 0');
586                                 self.angles = '90 90 0';
587                                 if(random() < 0.5)
588                                 {
589                                         self.velocity_y += random() * 50;
590                                         self.velocity_x -= random() * 50;
591                                 }
592                                 else
593                                 {
594                                         self.velocity_y -= random() * 50;
595                                         self.velocity_x += random() * 50;
596                                 }
597                                 self.velocity_z += random() * 150;
598                         }
599
600
601                         self.movetype = MOVETYPE_BOUNCE;
602                         //self.velocity_z = -200;
603
604                         self.SendFlags |= MSF_MOVE | MSF_ANG;
605
606                         return;
607                 }
608                 else if(self.fish_wasdrowning)
609                 {
610                         self.fish_wasdrowning = FALSE;
611                         self.angles_x = 0;
612                         self.movetype = MOVETYPE_WALK;
613                 }
614         }
615
616         targ = self.goalentity;
617
618         monster_target = targ;
619         monster_speed_run = runspeed;
620         monster_speed_walk = walkspeed;
621
622         if(MUTATOR_CALLHOOK(MonsterMove) || gameover || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || time < game_starttime || (autocvar_g_campaign && !campaign_bots_may_start) || time < self.spawn_time)
623         {
624                 runspeed = walkspeed = 0;
625                 if(time >= self.spawn_time)
626                         monsters_setframe(manim_idle);
627                 movelib_beak_simple(stopspeed);
628                 if(self.oldorigin != self.origin)
629                 {
630                         self.oldorigin = self.origin;
631                         self.SendFlags |= MSF_MOVE;
632                 }
633                 return;
634         }
635
636         targ = monster_target;
637         runspeed = monster_speed_run;
638         walkspeed = monster_speed_walk;
639
640         if(teamplay)
641         if(autocvar_g_monsters_teams)
642         if(IsDifferentTeam(self.monster_owner, self))
643                 self.monster_owner = world;
644
645         if(self.enemy && self.enemy.health < 1)
646                 self.enemy = world; // enough!
647
648         if(time >= self.last_enemycheck)
649         {
650                 if not(monster_isvalidtarget(self.enemy, self))
651                         self.enemy = world;
652
653                 if not(self.enemy)
654                 {
655                         self.enemy = FindTarget(self);
656                         if(self.enemy)
657                                 MonsterSound(monstersound_sight, 0, FALSE, CH_VOICE);
658                                 //monster_sound(self.msound_sight, 0, FALSE);
659                 }
660
661                 self.last_enemycheck = time + 0.5;
662         }
663
664         if(self.state == MONSTER_STATE_ATTACK_MELEE && time >= self.attack_finished_single)
665                 self.state = 0;
666
667         if(self.state != MONSTER_STATE_ATTACK_MELEE) // don't move if set
668         if(time >= self.last_trace || self.enemy) // update enemy instantly
669                 self.moveto = monster_pickmovetarget(targ);
670
671         if not(self.enemy)
672                 MonsterSound(monstersound_idle, 5, TRUE, CH_VOICE);
673                 //monster_sound(self.msound_idle, 5, TRUE);
674
675         if(self.state != MONSTER_STATE_ATTACK_LEAP && self.state != MONSTER_STATE_ATTACK_MELEE)
676                 self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
677
678         if(self.state == MONSTER_STATE_ATTACK_LEAP && (self.flags & FL_ONGROUND))
679         {
680                 self.state = 0;
681                 self.touch = MonsterTouch;
682         }
683
684         //self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
685
686         float turny = 0;
687         vector real_angle = vectoangles(self.steerto) - self.angles;
688
689         if(self.state != MONSTER_STATE_ATTACK_LEAP && self.state != MONSTER_STATE_ATTACK_MELEE)
690                 turny = 20;
691
692         if(self.flags & FL_SWIM)
693                 turny = vlen(self.angles - self.moveto);
694
695         if(turny)
696         {
697                 turny = bound(turny * -1, shortangle_f(real_angle_y, self.angles_y), turny);
698                 self.angles_y += turny;
699         }
700
701         if(self.state == MONSTER_STATE_ATTACK_MELEE)
702                 self.moveto = self.origin;
703
704         if(self.enemy && self.enemy.vehicle)
705                 runspeed = 0;
706
707         if(((self.flags & FL_FLY) || (self.flags & FL_SWIM)) && self.spawnflags & MONSTERFLAG_FLY_VERTICAL)
708                 v_forward = normalize(self.moveto - self.origin);
709         else
710                 self.moveto_z = self.origin_z;
711
712         if(vlen(self.origin - self.moveto) > 64)
713         {
714                 if(self.flags & FL_FLY || self.flags & FL_SWIM)
715                         movelib_move_simple(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
716                 else
717                         movelib_move_simple_gravity(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
718
719                 if(time > self.pain_finished)
720                 if(time > self.attack_finished_single)
721                 if(vlen(self.velocity) > 10)
722                         monsters_setframe((self.enemy) ? manim_run : manim_walk);
723                 else
724                         monsters_setframe(manim_idle);
725         }
726         else
727         {
728                 entity e = find(world, targetname, self.target2);
729                 if(e.target2)
730                         self.target2 = e.target2;
731                 else if(e.target)
732                         self.target2 = e.target;
733
734                 movelib_beak_simple(stopspeed);
735                 if(time > self.attack_finished_single)
736                 if(time > self.pain_finished)
737                 if (vlen(self.velocity) <= 30)
738                         monsters_setframe(manim_idle);
739         }
740
741         monster_checkattack(self, self.enemy);
742
743         if(self.angles != self.oldangles)
744         {
745                 self.oldangles = self.angles;
746                 self.SendFlags |= MSF_ANG;
747         }
748
749         if(self.origin != self.oldorigin)
750         {
751                 self.oldorigin = self.origin;
752                 self.SendFlags |= MSF_MOVE;
753         }
754 }
755
756 void monster_dead_think()
757 {
758         self.think = monster_dead_think;
759         self.nextthink = time + self.ticrate;
760
761         self.deadflag = DEAD_DEAD;
762
763         if(self.ltime != 0)
764         if(time >= self.ltime)
765         {
766                 Monster_Fade();
767                 return;
768         }
769
770         if(self.oldorigin != self.origin)
771         {
772                 self.oldorigin = self.origin;
773                 self.SendFlags |= MSF_MOVE;
774         }
775 }
776
777 void monsters_setstatus()
778 {
779         self.stat_monsters_total = monsters_total;
780         self.stat_monsters_killed = monsters_killed;
781 }
782
783 void Monster_Appear()
784 {
785         self.enemy = activator;
786         self.spawnflags &= ~MONSTERFLAG_APPEAR;
787         self.monster_spawnfunc();
788 }
789
790 float Monster_CheckAppearFlags(entity ent)
791 {
792         if not(ent.spawnflags & MONSTERFLAG_APPEAR)
793                 return FALSE;
794
795         ent.think = func_null;
796         ent.nextthink = 0;
797         ent.use = Monster_Appear;
798         ent.flags = FL_MONSTER; // set so this monster can get butchered
799
800         return TRUE;
801 }
802
803 void monsters_reset()
804 {
805         setorigin(self, self.pos1);
806         self.angles = self.pos2;
807
808         self.health = self.max_health;
809         self.velocity = '0 0 0';
810         self.enemy = world;
811         self.goalentity = world;
812         self.attack_finished_single = 0;
813         self.moveto = self.origin;
814
815         self.SendFlags |= MSF_STATUS;
816 }
817
818 float monster_send(entity to, float sf)
819 {
820         WriteByte(MSG_ENTITY, ENT_CLIENT_MONSTER);
821         WriteByte(MSG_ENTITY, sf);
822         if(sf & MSF_SETUP)
823         {
824                 WriteByte(MSG_ENTITY, self.monsterid);
825
826                 WriteCoord(MSG_ENTITY, self.origin_x);
827                 WriteCoord(MSG_ENTITY, self.origin_y);
828                 WriteCoord(MSG_ENTITY, self.origin_z);
829
830                 WriteAngle(MSG_ENTITY, self.angles_x);
831                 WriteAngle(MSG_ENTITY, self.angles_y);
832
833                 WriteByte(MSG_ENTITY, self.skin);
834                 WriteByte(MSG_ENTITY, self.team);
835                 WriteByte(MSG_ENTITY, (self.spawnflags & MONSTERFLAG_MINIBOSS));
836         }
837
838         if(sf & MSF_ANG)
839         {
840                 WriteShort(MSG_ENTITY, rint(self.angles_x));
841                 WriteShort(MSG_ENTITY, rint(self.angles_y));
842         }
843
844         if(sf & MSF_MOVE)
845         {
846                 WriteShort(MSG_ENTITY, rint(self.origin_x));
847                 WriteShort(MSG_ENTITY, rint(self.origin_y));
848                 WriteShort(MSG_ENTITY, rint(self.origin_z));
849
850                 WriteShort(MSG_ENTITY, rint(self.velocity_x));
851                 WriteShort(MSG_ENTITY, rint(self.velocity_y));
852                 WriteShort(MSG_ENTITY, rint(self.velocity_z));
853
854                 WriteShort(MSG_ENTITY, rint(self.angles_y));
855         }
856
857         if(sf & MSF_ANIM)
858         {
859                 WriteCoord(MSG_ENTITY, self.anim_start_time);
860                 WriteByte(MSG_ENTITY, self.frame);
861         }
862
863         if(sf & MSF_STATUS)
864         {
865                 WriteByte(MSG_ENTITY, self.skin);
866
867                 WriteByte(MSG_ENTITY, self.team);
868
869                 WriteByte(MSG_ENTITY, self.deadflag);
870
871                 if(self.health <= 0)
872                         WriteByte(MSG_ENTITY, 0);
873                 else
874                         WriteByte(MSG_ENTITY, ceil((self.health / self.max_health) * 255));
875         }
876
877         return TRUE;
878 }
879
880 void monster_link(void() spawnproc)
881 {
882         Net_LinkEntity(self, TRUE, 0, monster_send);
883         self.think        = spawnproc;
884         self.nextthink  = time;
885 }
886
887 void monsters_corpse_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
888 {
889         self.health -= damage;
890
891         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
892
893         if(self.health <= -100) // 100 health until gone?
894         {
895                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
896
897                 self.think = SUB_Remove;
898                 self.nextthink = time + 0.1;
899         }
900 }
901
902 void monster_die()
903 {
904         self.think = monster_dead_think;
905         self.nextthink = self.ticrate;
906         self.ltime = time + 5;
907
908         monster_dropitem();
909
910         MonsterSound(monstersound_death, 0, FALSE, CH_VOICE);
911         //monster_sound(self.msound_death, 0, FALSE);
912
913         if(!(self.spawnflags & MONSTERFLAG_SPAWNED) && !self.monster_respawned)
914                 monsters_killed += 1;
915
916         if(self.candrop && self.weapon)
917                 W_ThrowNewWeapon(self, self.weapon, 0, self.origin, randomvec() * 150 + '0 0 325');
918
919         if(IS_CLIENT(self.realowner))
920                 self.realowner.monstercount -= 1;
921
922         self.event_damage       = monsters_corpse_damage;
923         self.solid                      = SOLID_CORPSE;
924         self.takedamage         = DAMAGE_AIM;
925         self.enemy                      = world;
926         self.movetype           = MOVETYPE_TOSS;
927         self.moveto                     = self.origin;
928         self.touch                      = MonsterTouch; // reset incase monster was pouncing
929         self.reset                      = func_null;
930
931         if not(self.flags & FL_FLY)
932                 self.velocity = '0 0 0';
933
934         self.SendFlags |= MSF_MOVE;
935
936         // number of monsters spawned with mobspawn command
937         totalspawned -= 1;
938
939         MON_ACTION(self.monsterid, MR_DEATH);
940 }
941
942 void monsters_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
943 {
944         if(self.frozen && deathtype != DEATH_KILL)
945                 return;
946
947         if(time < self.pain_finished && deathtype != DEATH_KILL)
948                 return;
949
950         if(time < self.spawnshieldtime)
951                 return;
952
953         vector v;
954         float take, save;
955
956         v = healtharmor_applydamage(self.armorvalue, self.m_armor_blockpercent, damage);
957         take = v_x;
958         save = v_y;
959
960         self.health -= take;
961
962         self.dmg_time = time;
963
964         if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN)
965                 spamsound (self, CH_PAIN, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
966
967         self.velocity += force * self.damageforcescale;
968
969         if(deathtype != DEATH_DROWN)
970         {
971                 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, self, attacker);
972                 if (take > 50)
973                         Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
974                 if (take > 100)
975                         Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
976         }
977
978         if(self.health <= 0)
979         {
980                 if(deathtype == DEATH_KILL)
981                         self.candrop = FALSE; // killed by mobkill command
982
983                 // TODO: fix this?
984                 activator = attacker;
985                 other = self.enemy;
986                 SUB_UseTargets();
987                 self.target2 = self.oldtarget2; // reset to original target on death, incase we respawn
988
989                 monster_die();
990
991                 frag_attacker = attacker;
992                 frag_target = self;
993                 MUTATOR_CALLHOOK(MonsterDies);
994
995                 if(self.health <= -100) // check if we're already gibbed
996                 {
997                         Violence_GibSplash(self, 1, 0.5, attacker);
998
999                         self.think = SUB_Remove;
1000                         self.nextthink = time + 0.1;
1001                 }
1002         }
1003
1004         self.SendFlags |= MSF_STATUS;
1005 }
1006
1007 void monster_think()
1008 {
1009         self.think = monster_think;
1010         self.nextthink = self.ticrate;
1011
1012         MON_ACTION(self.monsterid, MR_THINK);
1013 }
1014
1015 void monster_spawn()
1016 {
1017         MON_ACTION(self.monsterid, MR_SETUP);
1018
1019         if not(self.monster_respawned)
1020                 Monster_CheckMinibossFlag();
1021
1022         self.max_health = self.health;
1023         self.pain_finished = self.nextthink;
1024         self.anim_start_time = time;
1025
1026         if not(self.noalign)
1027         {
1028                 setorigin(self, self.origin + '0 0 20');
1029                 tracebox(self.origin + '0 0 100', self.mins, self.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1030                 setorigin(self, trace_endpos);
1031         }
1032
1033         if not(self.monster_respawned)
1034         if not(self.skin)
1035                 self.skin = rint(random() * 4);
1036
1037         if not(self.attack_range)
1038                 self.attack_range = autocvar_g_monsters_attack_range;
1039
1040         self.pos1 = self.origin;
1041
1042         //monster_setupsounds(self.netname);
1043         precache_monstersounds();
1044         UpdateMonsterSounds();
1045         //monster_precachesounds(self);
1046
1047         if(teamplay)
1048                 self.monster_attack = TRUE; // we can have monster enemies in team games
1049
1050         //monster_sound(self.msound_spawn, 0, FALSE);
1051         MonsterSound(monstersound_spawn, 0, FALSE, CH_VOICE);
1052
1053         self.think = monster_think;
1054         self.nextthink = time + self.ticrate;
1055
1056         self.SendFlags |= MSF_SETUP;
1057
1058         MUTATOR_CALLHOOK(MonsterSpawn);
1059 }
1060
1061 float monster_initialize(float mon_id, float nodrop)
1062 {
1063         if not(autocvar_g_monsters)
1064                 return FALSE;
1065
1066         entity mon = get_monsterinfo(mon_id);
1067
1068         // support for quake style removing monsters based on skill
1069         switch(monster_skill)
1070         {
1071                 case 0:
1072                 case 1: if(self.spawnflags & MONSTERSKILL_NOTEASY)              return FALSE; break;
1073                 case 2: if(self.spawnflags & MONSTERSKILL_NOTMEDIUM)    return FALSE; break;
1074                 default:
1075                 case 3: if(self.spawnflags & MONSTERSKILL_NOTHARD)              return FALSE; break;
1076         }
1077
1078         if(self.team && !teamplay)
1079                 self.team = 0;
1080
1081         if not(self.spawnflags & MONSTERFLAG_SPAWNED) // naturally spawned monster
1082         if not(self.monster_respawned)
1083                 monsters_total += 1;
1084         
1085         setsize(self, mon.mins, mon.maxs);
1086         self.flags                              = FL_MONSTER;
1087         self.takedamage                 = DAMAGE_AIM;
1088         self.bot_attack                 = TRUE;
1089         self.iscreature                 = TRUE;
1090         self.teleportable               = TRUE;
1091         self.damagedbycontents  = TRUE;
1092         self.monsterid                  = mon_id;
1093         self.damageforcescale   = 0;
1094         self.event_damage               = monsters_damage;
1095         self.touch                              = MonsterTouch;
1096         self.use                                = monster_use;
1097         self.solid                              = SOLID_BBOX;
1098         self.movetype                   = MOVETYPE_WALK;
1099         self.spawnshieldtime    = time + autocvar_g_monsters_spawnshieldtime;
1100         self.enemy                              = world;
1101         self.velocity                   = '0 0 0';
1102         self.moveto                             = self.origin;
1103         self.pos2                               = self.angles;
1104         self.reset                              = monsters_reset;
1105         self.netname                    = mon.netname;
1106         self.monster_name               = M_NAME(mon_id);
1107         self.scale                              = 1;
1108         self.candrop                    = TRUE;
1109         self.view_ofs                   = '0 0 1' * (self.maxs_z * 0.5);
1110         self.oldtarget2                 = self.target2;
1111         self.deadflag                   = DEAD_NO;
1112         self.noalign                    = nodrop;
1113         self.spawn_time                 = time;
1114         self.gravity                    = 1;
1115         self.dphitcontentsmask  = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1116
1117         if(mon.spawnflags & MONSTER_TYPE_SWIM)
1118                 self.flags |= FL_SWIM;
1119
1120         if(mon.spawnflags & MONSTER_TYPE_FLY)
1121         {
1122                 self.flags |= FL_FLY;
1123                 self.movetype = MOVETYPE_FLY;
1124         }
1125         
1126         if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1127                 self.scale = 1.3;
1128
1129         if not(self.ticrate)
1130                 self.ticrate = autocvar_g_monsters_think_delay;
1131
1132         self.ticrate = bound(sys_frametime, self.ticrate, 60);
1133
1134         if not(self.m_armor_blockpercent)
1135                 self.m_armor_blockpercent = 0.5;
1136
1137         if not(self.target_range)
1138                 self.target_range = autocvar_g_monsters_target_range;
1139
1140         if not(self.respawntime)
1141                 self.respawntime = autocvar_g_monsters_respawn_delay;
1142
1143         if not(self.monster_moveflags)
1144                 self.monster_moveflags = MONSTER_MOVE_WANDER;
1145
1146         monster_link(monster_spawn);
1147
1148         return TRUE;
1149 }