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