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