]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/monsters/sv_monsters.qc
Remove monster accuracy check (monsters no longer use normal weapon functions, this...
[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.deadflag = DEAD_RESPAWNING;
369                 if(self.spawnflags & MONSTER_RESPAWN_DEATHPOINT)
370                 {
371                         self.pos1 = self.origin;
372                         self.pos2 = self.angles;
373                 }
374                 self.event_damage = func_null;
375                 self.takedamage = DAMAGE_NO;
376                 setorigin(self, self.pos1);
377                 self.angles = self.pos2;
378                 self.health = self.max_health;
379
380                 self.SendFlags |= MSF_MOVE;
381                 self.SendFlags |= MSF_STATUS;
382         }
383         else
384                 SUB_SetFade(self, time + 3, 1);
385 }
386
387 float Monster_CanJump (vector vel)
388 {
389         if(self.state)
390                 return FALSE; // already attacking
391         if not(self.flags & FL_ONGROUND)
392                 return FALSE; // not on the ground
393         if(self.health <= 0)
394                 return FALSE; // called when dead?
395         if(time < self.attack_finished_single)
396                 return FALSE; // still attacking
397
398         vector old = self.velocity;
399
400         self.velocity = vel;
401         tracetoss(self, self);
402         self.velocity = old;
403         if (trace_ent != self.enemy)
404                 return FALSE;
405
406         return TRUE;
407 }
408
409 float monster_leap (float anm, void() touchfunc, vector vel, float anim_finished)
410 {
411         if(!Monster_CanJump(vel))
412                 return FALSE;
413
414         monsters_setframe(anm);
415         self.state = MONSTER_STATE_ATTACK_LEAP;
416         self.touch = touchfunc;
417         self.origin_z += 1;
418         self.velocity = vel;
419         self.flags &= ~FL_ONGROUND;
420
421         self.attack_finished_single = time + anim_finished;
422
423         return TRUE;
424 }
425
426 void monster_checkattack(entity e, entity targ)
427 {
428         if(e == world)
429                 return;
430         if(targ == world)
431                 return;
432
433         if not(e.monster_attackfunc)
434                 return;
435
436         if(time < e.attack_finished_single)
437                 return;
438
439         if(vlen(targ.origin - e.origin) <= e.attack_range)
440         if(e.monster_attackfunc(MONSTER_ATTACK_MELEE))
441         {
442                 MonsterSound(monstersound_melee, 0, FALSE, CH_VOICE);
443                 return;
444         }
445
446         if(vlen(targ.origin - e.origin) > e.attack_range)
447         if(e.monster_attackfunc(MONSTER_ATTACK_RANGED))
448         {
449                 MonsterSound(monstersound_ranged, 0, FALSE, CH_VOICE);
450                 return;
451         }
452 }
453
454 void monster_use ()
455 {
456         if not(self.enemy)
457         if(self.health > 0)
458         if(monster_isvalidtarget(activator, self))
459                 self.enemy = activator;
460 }
461
462 .float last_trace;
463 .float last_enemycheck; // for checking enemy
464 vector monster_pickmovetarget(entity targ)
465 {
466         // enemy is always preferred target
467         if(self.enemy)
468         {
469                 makevectors(self.angles);
470                 self.monster_movestate = MONSTER_MOVE_ENEMY;
471                 self.last_trace = time + 1.2;
472                 return self.enemy.origin;
473         }
474
475         switch(self.monster_moveflags)
476         {
477                 case MONSTER_MOVE_OWNER:
478                 {
479                         self.monster_movestate = MONSTER_MOVE_OWNER;
480                         self.last_trace = time + 0.3;
481                         return (self.monster_owner) ? self.monster_owner.origin : self.origin;
482                 }
483                 case MONSTER_MOVE_SPAWNLOC:
484                 {
485                         self.monster_movestate = MONSTER_MOVE_SPAWNLOC;
486                         self.last_trace = time + 2;
487                         return self.pos1;
488                 }
489                 case MONSTER_MOVE_NOMOVE:
490                 {
491                         self.monster_movestate = MONSTER_MOVE_NOMOVE;
492                         self.last_trace = time + 2;
493                         return self.origin;
494                 }
495                 default:
496                 case MONSTER_MOVE_WANDER:
497                 {
498                         vector pos;
499                         self.monster_movestate = MONSTER_MOVE_WANDER;
500                         self.last_trace = time + 2;
501
502                         self.angles_y = rint(random() * 500);
503                         makevectors(self.angles);
504                         pos = self.origin + v_forward * 600;
505
506                         if(self.flags & FL_FLY || self.flags & FL_SWIM)
507                         if(self.spawnflags & MONSTERFLAG_FLY_VERTICAL)
508                         {
509                                 pos_z = random() * 200;
510                                 if(random() >= 0.5)
511                                         pos_z *= -1;
512                         }
513
514                         if(targ)
515                         {
516                                 self.last_trace = time + 0.5;
517                                 pos = targ.origin;
518                         }
519
520                         return pos;
521                 }
522         }
523 }
524
525 void monster_move(float runspeed, float walkspeed, float stopspeed, float manim_run, float manim_walk, float manim_idle)
526 {
527         fixedmakevectors(self.angles);
528
529         if(self.target2)
530                 self.goalentity = find(world, targetname, self.target2);
531
532         entity targ;
533
534         if(self.frozen)
535         {
536                 self.revive_progress = bound(0, self.revive_progress + frametime * self.revive_speed, 1);
537                 self.health = max(1, self.max_health * self.revive_progress);
538
539                 self.SendFlags |= MSF_STATUS;
540
541                 movelib_beak_simple(stopspeed);
542                 
543                 monsters_setframe(manim_idle);
544                 
545                 self.enemy = world;
546                 self.nextthink = time + self.ticrate;
547
548                 if(self.revive_progress >= 1)
549                         Unfreeze(self); // wait for next think before attacking
550
551                 // don't bother updating angles here?
552                 if(self.origin != self.oldorigin)
553                 {
554                         self.oldorigin = self.origin;
555                         self.SendFlags |= MSF_MOVE;
556                 }
557
558                 return; // no moving while frozen
559         }
560
561         if(self.flags & FL_SWIM)
562         {
563                 if(self.waterlevel < WATERLEVEL_WETFEET)
564                 {
565                         if(time >= self.last_trace)
566                         {
567                                 self.fish_wasdrowning = TRUE;
568                                 self.last_trace = time + 0.4;
569
570                                 Damage (self, world, world, 2, DEATH_DROWN, self.origin, '0 0 0');
571                                 self.angles = '90 90 0';
572                                 if(random() < 0.5)
573                                 {
574                                         self.velocity_y += random() * 50;
575                                         self.velocity_x -= random() * 50;
576                                 }
577                                 else
578                                 {
579                                         self.velocity_y -= random() * 50;
580                                         self.velocity_x += random() * 50;
581                                 }
582                                 self.velocity_z += random() * 150;
583                         }
584
585
586                         self.movetype = MOVETYPE_BOUNCE;
587                         //self.velocity_z = -200;
588
589                         self.SendFlags |= MSF_MOVE | MSF_ANG;
590
591                         return;
592                 }
593                 else if(self.fish_wasdrowning)
594                 {
595                         self.fish_wasdrowning = FALSE;
596                         self.angles_x = 0;
597                         self.movetype = MOVETYPE_WALK;
598                 }
599         }
600
601         targ = self.goalentity;
602
603         monster_target = targ;
604         monster_speed_run = runspeed;
605         monster_speed_walk = walkspeed;
606
607         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)
608         {
609                 runspeed = walkspeed = 0;
610                 if(time >= self.spawn_time)
611                         monsters_setframe(manim_idle);
612                 movelib_beak_simple(stopspeed);
613                 if(self.oldorigin != self.origin)
614                 {
615                         self.oldorigin = self.origin;
616                         self.SendFlags |= MSF_MOVE;
617                 }
618                 return;
619         }
620
621         targ = monster_target;
622         runspeed = monster_speed_run;
623         walkspeed = monster_speed_walk;
624
625         if(teamplay)
626         if(autocvar_g_monsters_teams)
627         if(DIFF_TEAM(self.monster_owner, self))
628                 self.monster_owner = world;
629
630         if(self.enemy && self.enemy.health < 1)
631                 self.enemy = world; // enough!
632
633         if(time >= self.last_enemycheck)
634         {
635                 if not(monster_isvalidtarget(self.enemy, self))
636                         self.enemy = world;
637
638                 if not(self.enemy)
639                 {
640                         self.enemy = FindTarget(self);
641                         if(self.enemy)
642                                 MonsterSound(monstersound_sight, 0, FALSE, CH_VOICE);
643                 }
644
645                 self.last_enemycheck = time + 0.5;
646         }
647
648         if(self.state == MONSTER_STATE_ATTACK_MELEE && time >= self.attack_finished_single)
649                 self.state = 0;
650
651         if(self.state != MONSTER_STATE_ATTACK_MELEE) // don't move if set
652         if(time >= self.last_trace || self.enemy) // update enemy instantly
653                 self.moveto = monster_pickmovetarget(targ);
654
655         if not(self.enemy)
656                 MonsterSound(monstersound_idle, 5, TRUE, CH_VOICE);
657
658         if(self.state != MONSTER_STATE_ATTACK_LEAP && self.state != MONSTER_STATE_ATTACK_MELEE)
659                 self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
660
661         if(self.state == MONSTER_STATE_ATTACK_LEAP && (self.flags & FL_ONGROUND))
662         {
663                 self.state = 0;
664                 self.touch = MonsterTouch;
665         }
666
667         //self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
668
669         float turny = 0;
670         vector real_angle = vectoangles(self.steerto) - self.angles;
671
672         if(self.state != MONSTER_STATE_ATTACK_LEAP && self.state != MONSTER_STATE_ATTACK_MELEE)
673                 turny = 20;
674
675         if(self.flags & FL_SWIM)
676                 turny = vlen(self.angles - self.moveto);
677
678         if(turny)
679         {
680                 turny = bound(turny * -1, shortangle_f(real_angle_y, self.angles_y), turny);
681                 self.angles_y += turny;
682         }
683
684         if(self.state == MONSTER_STATE_ATTACK_MELEE)
685                 self.moveto = self.origin;
686
687         if(self.enemy && self.enemy.vehicle)
688                 runspeed = 0;
689
690         if(((self.flags & FL_FLY) || (self.flags & FL_SWIM)) && self.spawnflags & MONSTERFLAG_FLY_VERTICAL)
691                 v_forward = normalize(self.moveto - self.origin);
692         else
693                 self.moveto_z = self.origin_z;
694
695         if(vlen(self.origin - self.moveto) > 64)
696         {
697                 if(self.flags & FL_FLY || self.flags & FL_SWIM)
698                         movelib_move_simple(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
699                 else
700                         movelib_move_simple_gravity(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
701
702                 if(time > self.pain_finished)
703                 if(time > self.attack_finished_single)
704                 if(vlen(self.velocity) > 10)
705                         monsters_setframe((self.enemy) ? manim_run : manim_walk);
706                 else
707                         monsters_setframe(manim_idle);
708         }
709         else
710         {
711                 entity e = find(world, targetname, self.target2);
712                 if(e.target2)
713                         self.target2 = e.target2;
714                 else if(e.target)
715                         self.target2 = e.target;
716
717                 movelib_beak_simple(stopspeed);
718                 if(time > self.attack_finished_single)
719                 if(time > self.pain_finished)
720                 if (vlen(self.velocity) <= 30)
721                         monsters_setframe(manim_idle);
722         }
723
724         monster_checkattack(self, self.enemy);
725
726         if(self.angles != self.oldangles)
727         {
728                 self.oldangles = self.angles;
729                 self.SendFlags |= MSF_ANG;
730         }
731
732         if(self.origin != self.oldorigin)
733         {
734                 self.oldorigin = self.origin;
735                 self.SendFlags |= MSF_MOVE;
736         }
737 }
738
739 void monster_dead_think()
740 {
741         self.think = monster_dead_think;
742         self.nextthink = time + self.ticrate;
743
744         self.deadflag = DEAD_DEAD;
745
746         if(self.ltime != 0)
747         if(time >= self.ltime)
748         {
749                 Monster_Fade();
750                 return;
751         }
752
753         if(self.oldorigin != self.origin)
754         {
755                 self.oldorigin = self.origin;
756                 self.SendFlags |= MSF_MOVE;
757         }
758 }
759
760 void monsters_setstatus()
761 {
762         self.stat_monsters_total = monsters_total;
763         self.stat_monsters_killed = monsters_killed;
764 }
765
766 void Monster_Appear()
767 {
768         self.enemy = activator;
769         self.spawnflags &= ~MONSTERFLAG_APPEAR;
770         self.monster_spawnfunc();
771 }
772
773 float Monster_CheckAppearFlags(entity ent)
774 {
775         if not(ent.spawnflags & MONSTERFLAG_APPEAR)
776                 return FALSE;
777
778         ent.think = func_null;
779         ent.nextthink = 0;
780         ent.use = Monster_Appear;
781         ent.flags = FL_MONSTER; // set so this monster can get butchered
782
783         return TRUE;
784 }
785
786 void monsters_reset()
787 {
788         setorigin(self, self.pos1);
789         self.angles = self.pos2;
790         
791         Unfreeze(self); // remove any icy remains
792
793         self.health = self.max_health;
794         self.velocity = '0 0 0';
795         self.enemy = world;
796         self.goalentity = world;
797         self.attack_finished_single = 0;
798         self.moveto = self.origin;
799
800         self.SendFlags |= MSF_STATUS;
801 }
802
803 float monster_send(entity to, float sf)
804 {
805         WriteByte(MSG_ENTITY, ENT_CLIENT_MONSTER);
806         WriteByte(MSG_ENTITY, sf);
807         if(sf & MSF_SETUP)
808         {
809                 WriteByte(MSG_ENTITY, self.monsterid);
810
811                 WriteCoord(MSG_ENTITY, self.origin_x);
812                 WriteCoord(MSG_ENTITY, self.origin_y);
813                 WriteCoord(MSG_ENTITY, self.origin_z);
814
815                 WriteAngle(MSG_ENTITY, self.angles_x);
816                 WriteAngle(MSG_ENTITY, self.angles_y);
817
818                 WriteByte(MSG_ENTITY, self.skin);
819                 WriteByte(MSG_ENTITY, self.team);
820         }
821
822         if(sf & MSF_ANG)
823         {
824                 WriteShort(MSG_ENTITY, rint(self.angles_x));
825                 WriteShort(MSG_ENTITY, rint(self.angles_y));
826         }
827
828         if(sf & MSF_MOVE)
829         {
830                 WriteShort(MSG_ENTITY, rint(self.origin_x));
831                 WriteShort(MSG_ENTITY, rint(self.origin_y));
832                 WriteShort(MSG_ENTITY, rint(self.origin_z));
833
834                 WriteShort(MSG_ENTITY, rint(self.velocity_x));
835                 WriteShort(MSG_ENTITY, rint(self.velocity_y));
836                 WriteShort(MSG_ENTITY, rint(self.velocity_z));
837
838                 WriteShort(MSG_ENTITY, rint(self.angles_y));
839         }
840
841         if(sf & MSF_ANIM)
842         {
843                 WriteCoord(MSG_ENTITY, self.anim_start_time);
844                 WriteByte(MSG_ENTITY, self.frame);
845         }
846
847         if(sf & MSF_STATUS)
848         {
849                 WriteByte(MSG_ENTITY, self.skin);
850
851                 WriteByte(MSG_ENTITY, self.team);
852
853                 WriteByte(MSG_ENTITY, self.deadflag);
854
855                 if(self.health <= 0)
856                         WriteByte(MSG_ENTITY, 0);
857                 else
858                         WriteByte(MSG_ENTITY, ceil((self.health / self.max_health) * 255));
859         }
860
861         return TRUE;
862 }
863
864 void monster_link(void() spawnproc)
865 {
866         Net_LinkEntity(self, TRUE, 0, monster_send);
867         self.think        = spawnproc;
868         self.nextthink  = time;
869 }
870
871 void monsters_corpse_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
872 {
873         self.health -= damage;
874
875         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
876
877         if(self.health <= -100) // 100 health until gone?
878         {
879                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
880
881                 self.think = SUB_Remove;
882                 self.nextthink = time + 0.1;
883         }
884 }
885
886 void monster_die()
887 {
888         self.think = monster_dead_think;
889         self.nextthink = self.ticrate;
890         self.ltime = time + 5;
891         
892         Unfreeze(self); // remove any icy remains
893         self.health = 0; // reset by Unfreeze
894
895         monster_dropitem();
896
897         MonsterSound(monstersound_death, 0, FALSE, CH_VOICE);
898
899         if(!(self.spawnflags & MONSTERFLAG_SPAWNED) && !self.monster_respawned)
900                 monsters_killed += 1;
901
902         if(self.candrop && self.weapon)
903                 W_ThrowNewWeapon(self, self.weapon, 0, self.origin, randomvec() * 150 + '0 0 325');
904
905         if(IS_CLIENT(self.realowner))
906         if not(self.monster_respawned)
907                 self.realowner.monstercount -= 1;
908
909         self.event_damage       = monsters_corpse_damage;
910         self.solid                      = SOLID_CORPSE;
911         self.takedamage         = DAMAGE_AIM;
912         self.enemy                      = world;
913         self.movetype           = MOVETYPE_TOSS;
914         self.moveto                     = self.origin;
915         self.touch                      = MonsterTouch; // reset incase monster was pouncing
916         self.reset                      = func_null;
917         self.state                      = 0;
918         self.attack_finished_single = 0;
919
920         if not(self.flags & FL_FLY)
921                 self.velocity = '0 0 0';
922
923         self.SendFlags |= MSF_MOVE;
924
925         // number of monsters spawned with mobspawn command
926         totalspawned -= 1;
927
928         MON_ACTION(self.monsterid, MR_DEATH);
929 }
930
931 void monsters_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
932 {
933         if(self.frozen && deathtype != DEATH_KILL)
934                 return;
935
936         if(time < self.pain_finished && deathtype != DEATH_KILL)
937                 return;
938
939         if(time < self.spawnshieldtime)
940                 return;
941
942         vector v;
943         float take, save;
944
945         v = healtharmor_applydamage(self.armorvalue, self.m_armor_blockpercent, damage);
946         take = v_x;
947         save = v_y;
948
949         self.health -= take;
950
951         self.dmg_time = time;
952
953         if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN)
954                 spamsound (self, CH_PAIN, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
955
956         self.velocity += force * self.damageforcescale;
957
958         if(deathtype != DEATH_DROWN)
959         {
960                 Violence_GibSplash_At(hitloc, force, 2, bound(0, take, 200) / 16, self, attacker);
961                 if (take > 50)
962                         Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
963                 if (take > 100)
964                         Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
965         }
966
967         if(self.health <= 0)
968         {
969                 if(deathtype == DEATH_KILL)
970                         self.candrop = FALSE; // killed by mobkill command
971
972                 // TODO: fix this?
973                 activator = attacker;
974                 other = self.enemy;
975                 SUB_UseTargets();
976                 self.target2 = self.oldtarget2; // reset to original target on death, incase we respawn
977
978                 monster_die();
979
980                 frag_attacker = attacker;
981                 frag_target = self;
982                 MUTATOR_CALLHOOK(MonsterDies);
983
984                 if(self.health <= -100) // check if we're already gibbed
985                 {
986                         Violence_GibSplash(self, 1, 0.5, attacker);
987
988                         self.think = SUB_Remove;
989                         self.nextthink = time + 0.1;
990                 }
991         }
992
993         self.SendFlags |= MSF_STATUS;
994 }
995
996 void monster_think()
997 {
998         self.think = monster_think;
999         self.nextthink = self.ticrate;
1000         
1001         if(self.ltime)
1002         if(time >= self.ltime)
1003         {
1004                 Damage(self, self, self, self.health + self.max_health, DEATH_KILL, self.origin, self.origin);
1005                 return;
1006         }
1007
1008         MON_ACTION(self.monsterid, MR_THINK);
1009 }
1010
1011 void monster_spawn()
1012 {
1013         MON_ACTION(self.monsterid, MR_SETUP);
1014
1015         if not(self.monster_respawned)
1016                 Monster_CheckMinibossFlag();
1017
1018         self.max_health = self.health;
1019         self.pain_finished = self.nextthink;
1020         self.anim_start_time = time;
1021
1022         if not(self.noalign)
1023         {
1024                 setorigin(self, self.origin + '0 0 20');
1025                 tracebox(self.origin + '0 0 100', self.mins, self.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1026                 setorigin(self, trace_endpos);
1027         }
1028
1029         if not(self.monster_respawned)
1030         if not(self.skin)
1031                 self.skin = rint(random() * 4);
1032
1033         if not(self.attack_range)
1034                 self.attack_range = autocvar_g_monsters_attack_range;
1035
1036         self.pos1 = self.origin;
1037
1038         //monster_setupsounds(self.netname);
1039         precache_monstersounds();
1040         UpdateMonsterSounds();
1041         //monster_precachesounds(self);
1042
1043         if(teamplay)
1044                 self.monster_attack = TRUE; // we can have monster enemies in team games
1045                 
1046         MonsterSound(monstersound_spawn, 0, FALSE, CH_VOICE);
1047
1048         self.think = monster_think;
1049         self.nextthink = time + self.ticrate;
1050
1051         self.SendFlags |= MSF_SETUP;
1052
1053         MUTATOR_CALLHOOK(MonsterSpawn);
1054 }
1055
1056 float monster_initialize(float mon_id, float nodrop)
1057 {
1058         if not(autocvar_g_monsters)
1059                 return FALSE;
1060
1061         entity mon = get_monsterinfo(mon_id);
1062
1063         // support for quake style removing monsters based on skill
1064         switch(monster_skill)
1065         {
1066                 case 0:
1067                 case 1: if(self.spawnflags & MONSTERSKILL_NOTEASY)              return FALSE; break;
1068                 case 2: if(self.spawnflags & MONSTERSKILL_NOTMEDIUM)    return FALSE; break;
1069                 default:
1070                 case 3: if(self.spawnflags & MONSTERSKILL_NOTHARD)              return FALSE; break;
1071         }
1072
1073         if(self.team && !teamplay)
1074                 self.team = 0;
1075
1076         if not(self.spawnflags & MONSTERFLAG_SPAWNED) // naturally spawned monster
1077         if not(self.monster_respawned)
1078                 monsters_total += 1;
1079
1080         setsize(self, mon.mins, mon.maxs);
1081         self.flags                              = FL_MONSTER;
1082         self.takedamage                 = DAMAGE_AIM;
1083         self.bot_attack                 = TRUE;
1084         self.iscreature                 = TRUE;
1085         self.teleportable               = TRUE;
1086         self.damagedbycontents  = TRUE;
1087         self.monsterid                  = mon_id;
1088         self.damageforcescale   = 0;
1089         self.event_damage               = monsters_damage;
1090         self.touch                              = MonsterTouch;
1091         self.use                                = monster_use;
1092         self.solid                              = SOLID_BBOX;
1093         self.movetype                   = MOVETYPE_WALK;
1094         self.spawnshieldtime    = time + autocvar_g_monsters_spawnshieldtime;
1095         self.enemy                              = world;
1096         self.velocity                   = '0 0 0';
1097         self.moveto                             = self.origin;
1098         self.pos2                               = self.angles;
1099         self.reset                              = monsters_reset;
1100         self.netname                    = mon.netname;
1101         self.monster_name               = M_NAME(mon_id);
1102         self.candrop                    = TRUE;
1103         self.view_ofs                   = '0 0 1' * (self.maxs_z * 0.5);
1104         self.oldtarget2                 = self.target2;
1105         self.deadflag                   = DEAD_NO;
1106         self.scale                              = 1;
1107         self.noalign                    = nodrop;
1108         self.spawn_time                 = time;
1109         self.gravity                    = 1;
1110         self.dphitcontentsmask  = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
1111
1112         if(mon.spawnflags & MONSTER_TYPE_SWIM)
1113                 self.flags |= FL_SWIM;
1114
1115         if(mon.spawnflags & MONSTER_TYPE_FLY)
1116         {
1117                 self.flags |= FL_FLY;
1118                 self.movetype = MOVETYPE_FLY;
1119         }
1120
1121         if(mon.spawnflags & MONSTER_SIZE_BROKEN)
1122                 self.scale = 1.3;
1123
1124         if not(self.ticrate)
1125                 self.ticrate = autocvar_g_monsters_think_delay;
1126
1127         self.ticrate = bound(sys_frametime, self.ticrate, 60);
1128
1129         if not(self.m_armor_blockpercent)
1130                 self.m_armor_blockpercent = 0.5;
1131
1132         if not(self.target_range)
1133                 self.target_range = autocvar_g_monsters_target_range;
1134
1135         if not(self.respawntime)
1136                 self.respawntime = autocvar_g_monsters_respawn_delay;
1137
1138         if not(self.monster_moveflags)
1139                 self.monster_moveflags = MONSTER_MOVE_WANDER;
1140
1141         monster_link(monster_spawn);
1142
1143         return TRUE;
1144 }