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