]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/monsters/lib/monsters.qc
Merge branch 'master' into mario/monsters
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / monsters / lib / monsters.qc
1 // TODO: clean up this file?
2
3 void M_Item_Touch ()
4 {
5         if(self && other.classname == STR_PLAYER && other.deadflag == DEAD_NO)
6         {
7                 Item_Touch();
8                 self.think = SUB_Remove;
9                 self.nextthink = time + 0.1;
10         }
11 }
12
13 void Monster_DropItem (string itype, string itemsize)
14 {
15         vector backuporigin = self.origin + ((self.mins + self.maxs) * 0.5);
16         entity oldself;
17         
18         oldself = self;
19         self = spawn();
20         
21         if (itype == "armor")
22         {
23                 if(itemsize == "large") spawnfunc_item_armor_large();
24                 else if (itemsize == "small") spawnfunc_item_armor_small();
25                 else if (itemsize == "medium") spawnfunc_item_armor_medium();
26                 else dprint("Invalid monster drop item selected.\n");
27         }
28         else if (itype == "health")
29         {
30                 if(itemsize == "large") spawnfunc_item_health_large();
31                 else if (itemsize == "small") spawnfunc_item_health_small();
32                 else if (itemsize == "medium") spawnfunc_item_health_medium();
33                 else if (itemsize == "mega") spawnfunc_item_health_mega();
34                 else dprint("Invalid monster drop item selected.\n");
35         }
36         else if (itype == "ammo")
37         {
38                 if(itemsize == "shells") spawnfunc_item_shells();
39                 else if (itemsize == "cells") spawnfunc_item_cells();
40                 else if (itemsize == "bullets") spawnfunc_item_bullets();
41                 else if (itemsize == "rockets") spawnfunc_item_rockets();
42                 else dprint("Invalid monster drop item selected.\n");
43         }
44         else
45         {
46                 dprint("Invalid monster drop item selected.\n");
47                 self = oldself;
48                 return;
49         }
50         
51         self.velocity = randomvec() * 175 + '0 0 325';
52         
53         self.gravity = 1;
54         self.origin = backuporigin;
55         
56         self.touch = M_Item_Touch;
57         
58         SUB_SetFade(self, time + 5, 1);
59         
60         self = oldself;
61 }
62
63 float monster_isvalidtarget (entity targ, entity ent, float neutral)
64 {
65         if(!targ || !ent)
66                 return FALSE; // this check should fix a crash
67                 
68         if(targ.vehicle_flags & VHF_ISVEHICLE)
69                 targ = targ.vehicle;
70                 
71         if(time < game_starttime)
72                 return FALSE; // monsters do nothing before the match has started
73                 
74         traceline(ent.origin, targ.origin, FALSE, ent);
75         
76         if(vlen(targ.origin - ent.origin) >= ent.target_range)
77                 return FALSE; // enemy is too far away
78
79         if(trace_ent != targ)
80                 return FALSE; // we can't see the enemy
81                 
82         if(neutral == TRUE)
83                 return TRUE; // we come in peace!
84                 
85         if(targ.takedamage == DAMAGE_NO)
86                 return FALSE; // enemy can't be damaged
87                 
88         if(targ.items & IT_INVISIBILITY)
89                 return FALSE; // enemy is invisible
90         
91         if(targ.classname == STR_SPECTATOR || targ.classname == STR_OBSERVER)
92                 return FALSE; // enemy is a spectator
93         
94         if(targ.deadflag != DEAD_NO || ent.deadflag != DEAD_NO || targ.health <= 0 || ent.health <= 0)
95                 return FALSE; // enemy/self is dead
96         
97         if(targ.monster_owner == ent || ent.monster_owner == targ)
98                 return FALSE; // enemy owns us, or we own them
99         
100         if(targ.flags & FL_NOTARGET)
101                 return FALSE; // enemy can't be targetted
102         
103         if not(autocvar_g_monsters_typefrag)
104         if(targ.BUTTON_CHAT)
105                 return FALSE; // no typefragging!
106         
107         if not(IsDifferentTeam(targ, ent))
108                 return FALSE; // enemy is on our team
109         
110         return TRUE;
111 }
112
113 float enemy_stillvalidtarget(entity targ, entity e)
114 {
115         if(targ.health < 1 || targ.deadflag)
116                 return FALSE; // let's not keep hurting a corpse
117                 
118         if not(autocvar_g_monsters_typefrag)
119         if(targ.BUTTON_CHAT)
120                 return FALSE; // no typefrags!
121                 
122         if(vlen(targ.origin - e.origin) > e.target_range)
123                 return FALSE; // out of our reach
124                 
125         if not(targ.takedamage)
126                 return FALSE; // can't hurt it
127                 
128         if(targ.flags & FL_NOTARGET)
129                 return FALSE; // can't target it
130                 
131         if(targ.items & IT_INVISIBILITY)
132                 return FALSE; // currently not used
133                 
134         if(!IsDifferentTeam(targ, e))
135                 return FALSE;
136                 
137         return TRUE; // all is good, keep going!
138 }
139
140 entity FindTarget (entity ent) 
141 {
142         if(MUTATOR_CALLHOOK(MonsterFindTarget)) { return ent.enemy; } // Handled by a mutator
143         local entity e;
144         for(e = world; (e = findflags(e, monster_attack, TRUE)); ) 
145         {
146                 if(monster_isvalidtarget(e, ent, FALSE))
147                 {
148                         return e;
149                 }
150         }
151         return world;
152 }
153
154 void MonsterTouch ()
155 {
156         if(other == world)
157                 return;
158                 
159         if(self.enemy != other)
160         if not(other.flags & FL_MONSTER)
161         if(monster_isvalidtarget(other, self, FALSE))
162                 self.enemy = other;
163 }
164
165 void monster_sound(string msound, float sound_delay, float delaytoo)
166 {
167         if(delaytoo && time < self.msound_delay)
168                 return; // too early
169                 
170         if(msound == "")
171                 return; // sound doesn't exist
172
173         sound(self, CH_PAIN_SINGLE, msound, VOL_BASE, ATTN_NORM);
174
175         self.msound_delay = time + sound_delay;
176 }
177
178 void monster_precachesounds()
179 {
180         precache_sound(self.msound_idle);
181         precache_sound(self.msound_death);
182         precache_sound(self.msound_attack_melee);
183         precache_sound(self.msound_attack_ranged);
184         precache_sound(self.msound_sight);
185         precache_sound(self.msound_pain);
186 }
187
188 void monster_melee (entity targ, float damg, float er, float deathtype)
189 {
190         float bigdmg = 0, rdmg = damg * random();
191
192         if (self.health <= 0)
193                 return;
194         if (targ == world)
195                 return;
196
197         if (vlen(self.origin - targ.origin) > er * self.scale)
198                 return;
199                 
200         bigdmg = rdmg * self.scale;
201         
202         Damage(targ, self, self, bigdmg * monster_skill, deathtype, targ.origin, normalize(targ.origin - self.origin));
203 }
204
205 void Monster_CheckDropCvars (string mon)
206 {
207         if not(self.candrop)
208                 return; // forced off
209         
210         string dropitem;
211         string dropsize;
212         
213         dropitem = cvar_string(strcat("g_monster_", mon, "_drop"));
214         dropsize = cvar_string(strcat("g_monster_", mon, "_drop_size"));
215         
216         monster_dropitem = dropitem;
217         monster_dropsize = dropsize;
218         MUTATOR_CALLHOOK(MonsterDropItem);
219         dropitem = monster_dropitem;
220         dropsize = monster_dropsize;
221         
222         if(autocvar_g_monsters_forcedrop)
223                 Monster_DropItem(autocvar_g_monsters_drop_type, autocvar_g_monsters_drop_size);
224         else if(dropitem != "")
225                 Monster_DropItem(dropitem, dropsize);      
226         else
227                 Monster_DropItem("armor", "medium");
228 }
229
230 void ScaleMonster (float scle)
231 {
232         // this should prevent monster from falling through floor when scale changes
233         self.scale = scle;
234         setorigin(self, self.origin + ('0 0 30' * scle));
235 }
236
237 void Monster_CheckMinibossFlag ()
238 {
239         if(MUTATOR_CALLHOOK(MonsterCheckBossFlag))
240                 return;
241                 
242         float r = random() * 4, chance = random() * 100;
243
244         // g_monsters_miniboss_chance cvar or spawnflags 64 causes a monster to be a miniboss
245         if ((self.spawnflags & MONSTERFLAG_MINIBOSS) || (chance < autocvar_g_monsters_miniboss_chance))
246         {
247                 self.health += autocvar_g_monsters_miniboss_healthboost;
248                 ScaleMonster(1.5);
249                 self.flags |= MONSTERFLAG_MINIBOSS;
250                 
251                 if (r < 2 || self.team == NUM_TEAM_2)
252                 {
253                         self.strength_finished = -1;  
254                         self.effects |= (EF_FULLBRIGHT | EF_BLUE);
255                 }
256                 else if (r >= 1 || self.team == NUM_TEAM_1)
257                 {
258                         self.invincible_finished = -1;
259                         self.effects |= (EF_FULLBRIGHT | EF_RED);
260                 }
261                 else
262                         self.effects |= (EF_FULLBRIGHT | EF_RED | EF_BLUE);
263                 
264                 if(teamplay)
265                 if(self.team)
266                         return;
267                         
268                 self.colormod = randomvec() * 4;
269         }
270 }
271
272 float Monster_CanRespawn(entity ent)
273 {
274         other = ent;
275         if(MUTATOR_CALLHOOK(MonsterRespawn))
276                 return TRUE; // enabled by a mutator
277                 
278         if(ent.spawnflags & MONSTERFLAG_NORESPAWN)
279                 return FALSE;
280                 
281         if not(autocvar_g_monsters_respawn)
282                 return FALSE;
283                 
284         return TRUE;
285 }
286
287 void Monster_Fade ()
288 {
289         if(Monster_CanRespawn(self))
290         {
291                 self.monster_respawned = TRUE;
292                 setmodel(self, "");
293                 self.think = self.monster_spawnfunc;
294                 self.nextthink = time + self.respawntime;
295                 setorigin(self, self.pos1);
296                 self.angles = self.pos2;
297                 self.health = self.max_health; // TODO: check if resetting to max_health is wise here
298                 return;
299         }
300         self.think = SUB_Remove;
301         self.nextthink = time + 4;
302         SUB_SetFade(self, time + 3, 1);
303 }
304
305 float Monster_CanJump (vector vel)
306 {
307         local vector old = self.velocity;
308         
309         self.velocity = vel;
310         tracetoss(self, self);
311         self.velocity = old;
312         if (trace_ent != self.enemy)
313                 return FALSE;
314
315         return TRUE;
316 }
317
318 float monster_leap (float anm, void() touchfunc, vector vel, float anim_finished)
319 {
320         if not(self.flags & FL_ONGROUND)
321                 return FALSE;
322         if(self.health < 1)
323                 return FALSE; // called when dead?
324         if not(Monster_CanJump(vel))
325                 return FALSE;
326                 
327         self.frame = anm;
328         self.state = MONSTER_STATE_ATTACK_LEAP;
329         self.touch = touchfunc;
330         self.origin_z += 1;
331         self.velocity = vel;
332         if (self.flags & FL_ONGROUND)
333                 self.flags -= FL_ONGROUND;
334                 
335         self.attack_finished_single = time + anim_finished;
336         
337         return TRUE;
338 }
339
340 float GenericCheckAttack ()
341 {
342         // checking attack while dead?
343         if (self.health <= 0 || self.enemy == world)
344                 return FALSE;
345                 
346         if(self.monster_delayedattack && self.delay != -1)
347         {
348                 if(time < self.delay)
349                         return FALSE;
350                         
351                 self.monster_delayedattack();
352         }
353         
354         if (time < self.attack_finished_single)
355                 return FALSE;
356         
357         if (enemy_range() > 2000) // long traces are slow
358                 return FALSE;   
359                 
360         if(self.attack_melee)
361         if(enemy_range() <= 100 * self.scale)
362         {
363                 monster_sound(self.msound_attack_melee, 0, FALSE); // no delay for attack sounds
364                 self.attack_melee(); // don't wait for nextthink - too slow
365                 return TRUE;
366         }
367         
368         // monster doesn't have a ranged attack function, so stop here
369         if not(self.attack_ranged)
370                 return FALSE;
371
372         // see if any entities are in the way of the shot
373         if not(findtrajectorywithleading(self.origin, '0 0 0', '0 0 0', self.enemy, 800, 0, 2.5, 0, self))
374                 return FALSE;
375
376         if(self.attack_ranged())
377         {
378                 monster_sound(self.msound_attack_ranged, 0, FALSE); // no delay for attack sounds
379                 return TRUE;
380         }
381
382         return FALSE;
383 }
384
385 void monster_use ()
386 {
387         if (self.enemy)
388                 return;
389         if (self.health <= 0)
390                 return;
391
392         if(!monster_isvalidtarget(activator, self, -1))
393                 return;
394
395         self.enemy = activator;
396 }
397
398 float trace_path(vector from, vector to)
399 {
400         vector dir = normalize(to - from) * 15, offset = '0 0 0';
401         float trace1 = trace_fraction;
402         
403         offset_x = dir_y;
404         offset_y = -dir_x;
405         traceline (from+offset, to+offset, TRUE, self);
406         
407         traceline(from-offset, to-offset, TRUE, self);
408                 
409         return ((trace1 < trace_fraction) ? trace1 : trace_fraction);
410 }
411
412 vector monster_pickmovetarget(entity targ)
413 {
414         // enemy is always preferred target
415         if(self.enemy)
416         {
417                 self.monster_movestate = MONSTER_MOVE_ENEMY;
418                 return self.enemy.origin;
419         }
420         if(targ)
421         {
422                 self.monster_movestate = MONSTER_MOVE_WANDER;
423                 return targ.origin;
424         }
425         
426         switch(self.monster_moveflags)
427         {
428                 case MONSTER_MOVE_OWNER:
429                 {
430                         self.monster_movestate = MONSTER_MOVE_OWNER;
431                         if(self.monster_owner && self.monster_owner.classname != "monster_swarm")
432                                 return self.monster_owner.origin;
433                 }
434                 case MONSTER_MOVE_WANDER:
435                 {
436                         self.monster_movestate = MONSTER_MOVE_WANDER;
437                                 
438                         self.angles_y = random() * 500;
439                         makevectors(self.angles);
440                         return self.origin + v_forward * 600;
441                 }
442                 case MONSTER_MOVE_SPAWNLOC:
443                 {
444                         self.monster_movestate = MONSTER_MOVE_SPAWNLOC;
445                         return self.pos1;
446                 }
447                 default:
448                 case MONSTER_MOVE_NOMOVE:
449                 {
450                         self.monster_movestate = MONSTER_MOVE_NOMOVE;
451                         return self.origin;
452                 }
453         }
454 }
455
456 .float last_trace;
457 void monster_move(float runspeed, float walkspeed, float stopspeed, float manim_run, float manim_walk, float manim_idle)
458 {
459         if(self.target)
460                 self.goalentity = find(world, targetname, self.target);
461                 
462         entity targ;
463
464         if(self.frozen)
465         {
466                 self.revive_progress = bound(0, self.revive_progress + frametime * self.revive_speed, 1);
467                 self.health = max(1, self.max_health * self.revive_progress);
468                 
469                 if(self.sprite) WaypointSprite_UpdateHealth(self.sprite, self.health);
470                         
471                 movelib_beak_simple(stopspeed);
472                         
473                 self.velocity = '0 0 0';
474                 self.enemy = world;
475                 self.nextthink = time + 0.1;
476                 
477                 if(self.revive_progress >= 1)
478                         Unfreeze(self); // wait for next think before attacking
479                         
480                 return; // no moving while frozen
481         }
482         
483         if(self.flags & FL_SWIM)
484         {
485                 if(self.waterlevel < WATERLEVEL_WETFEET)
486                 {
487                         if(time >= self.last_trace)
488                         {
489                                 self.last_trace = time + 0.4;
490                                 self.angles = '0 0 -90';
491                                 Damage (self, world, world, 2, DEATH_DROWN, self.origin, '0 0 0');
492                                 if(random() < 0.5)
493                                 {
494                                         self.velocity_y += random() * 50;
495                                         self.velocity_x -= random() * 50;
496                                 }
497                                 else
498                                 {
499                                         self.velocity_y -= random() * 50;
500                                         self.velocity_x += random() * 50;
501                                 }
502                                 //self.velocity_z += random() * 150;
503                                 self.movetype = MOVETYPE_BOUNCE;
504                                 self.velocity_z = -200;
505                         }
506                         return;
507                 }
508                 else
509                 {
510                         self.angles = '0 0 0';
511                         self.movetype = MOVETYPE_WALK;
512                 }
513         }
514         
515         if(gameover || time < game_starttime)
516         {
517                 runspeed = walkspeed = 0;
518                 self.frame = manim_idle;
519                 movelib_beak_simple(stopspeed);
520                 return;
521         }
522         
523         targ = self.goalentity;
524         
525         monster_target = targ;
526         monster_speed_run = runspeed;
527         monster_speed_walk = walkspeed;
528         MUTATOR_CALLHOOK(MonsterMove);
529         targ = monster_target;
530         runspeed = monster_speed_run;
531         walkspeed = monster_speed_walk;
532                 
533         if(IsDifferentTeam(self.monster_owner, self))
534                 self.monster_owner = world;
535                 
536         if(!enemy_stillvalidtarget(self.enemy, self))
537                 self.enemy = world;
538                 
539         if not(self.enemy)
540         {
541                 self.enemy = FindTarget(self);
542                 if(self.enemy)
543                         monster_sound(self.msound_sight, 0, FALSE);
544         }
545                 
546         if(time >= self.last_trace)
547         {
548                 if(self.monster_movestate == MONSTER_MOVE_WANDER && self.goalentity.classname != "td_waypoint")
549                         self.last_trace = time + 2;
550                 else
551                         self.last_trace = time + 0.5;
552                 self.moveto = monster_pickmovetarget(targ);
553         }
554
555         if not(self.enemy)
556                 monster_sound(self.msound_idle, 5, TRUE);
557         
558         vector angles_face = vectoangles(self.moveto - self.origin);
559         vector owner_face = vectoangles(self.monster_owner.origin - self.origin);
560         vector enemy_face = vectoangles(self.enemy.origin - self.origin);
561         self.angles_y = angles_face_y;
562         
563         if(self.state == MONSTER_STATE_ATTACK_LEAP && (self.flags & FL_ONGROUND))
564         {
565                 self.state = 0;
566                 self.touch = MonsterTouch;
567         }
568          
569         v_forward = normalize(self.moveto - self.origin);
570         
571         float l = vlen(self.moveto - self.origin);
572         float t1 = trace_path(self.origin+'0 0 10', self.moveto+'0 0 10');
573         float t2 = trace_path(self.origin-'0 0 15', self.moveto-'0 0 15'); 
574         
575         if(t1*l-t2*l>50 && (t1*l > 100 || t1 > 0.8))
576         if(self.flags & FL_ONGROUND)
577                 movelib_jump_simple(100);
578
579         if(vlen(self.origin - self.moveto) > 64)
580         {
581                 if(self.flags & FL_FLY)
582                         movelib_move_simple(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
583                 else
584                         movelib_move_simple_gravity(v_forward, ((self.enemy) ? runspeed : walkspeed), 0.6);
585                 if(time > self.pain_finished)
586                 if(time > self.attack_finished_single)
587                         self.frame = ((self.enemy) ? manim_run : manim_walk);
588         }
589         else
590         {
591                 movelib_beak_simple(stopspeed);
592                 if(time > self.attack_finished_single)
593                 if(time > self.pain_finished)
594                 if (vlen(self.velocity) <= 30)
595                 {
596                         self.frame = manim_idle;
597                         if(self.enemy)
598                                 self.angles_y = enemy_face_y;
599                         else
600                                 self.angles_y = ((self.monster_owner) ? owner_face_y : self.pos2_y); // reset looking angle now?
601                 }
602         }
603                 
604         if(self.enemy && self.checkattack)
605                 self.checkattack();
606 }
607
608 void monsters_setstatus()
609 {
610         self.stat_monsters_total = monsters_total;
611         self.stat_monsters_killed = monsters_killed;
612 }
613
614 void Monster_Appear ()
615 {
616         self.enemy = activator;
617         self.spawnflags &~= MONSTERFLAG_APPEAR;
618         self.monster_spawnfunc();
619 }
620
621 void monsters_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
622 {
623         if(self.frozen && deathtype != DEATH_KILL)
624                 return;
625                 
626         if(time < self.pain_finished && deathtype != DEATH_KILL)
627                 return;
628                 
629         if((ignore_turrets && !(attacker.turrcaps_flags & TFL_TURRCAPS_ISTURRET)) || !ignore_turrets)
630         if(monster_isvalidtarget(attacker, self, FALSE))
631                 self.enemy = attacker;
632         
633         self.health -= damage;
634         
635         if(self.sprite)
636                 WaypointSprite_UpdateHealth(self.sprite, self.health);
637                 
638         self.dmg_time = time;
639
640         if(sound_allowed(MSG_BROADCAST, attacker) && deathtype != DEATH_DROWN)
641                 spamsound (self, CH_PAIN, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
642         
643         self.velocity += force * self.damageforcescale;
644                 
645         if(deathtype != DEATH_DROWN)
646         {
647                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
648                 if (damage > 50)
649                         Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
650                 if (damage > 100)
651                         Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
652         }
653                 
654         if(self.health <= 0)
655         {        
656                 if(self.sprite)
657                 {
658                         // Update one more time to avoid waypoint fading without emptying healthbar
659                         WaypointSprite_UpdateHealth(self.sprite, 0);
660                 }
661                 
662                 if(deathtype == DEATH_KILL)
663                         self.candrop = FALSE; // killed by mobkill command
664                 
665                 if(self.flags & MONSTERFLAG_MINIBOSS && self.candrop)
666                         W_ThrowNewWeapon(self, WEP_NEX, 0, self.origin, self.velocity);
667                         
668                 activator = attacker;
669                 other = self.enemy;
670                 self.target = self.target2;
671                 self.target2 = "";
672                 SUB_UseTargets();
673         
674                 self.monster_die();
675                 
676                 frag_attacker = attacker;
677                 frag_target = self;
678                 MUTATOR_CALLHOOK(MonsterDies);
679         }
680 }
681
682 // used to hook into monster post death functions without a mutator
683 void monster_hook_death()
684 {
685         if(self.sprite)
686         WaypointSprite_Kill(self.sprite);
687                 
688         monster_sound(self.msound_death, 0, FALSE);
689                 
690         if(!(self.spawnflags & MONSTERFLAG_SPAWNED) && !self.monster_respawned)
691                 monsters_killed += 1;
692                 
693         if(self.realowner.classname == "monster_spawner")
694                 self.realowner.spawner_monstercount -= 1;
695                 
696         if(self.realowner.flags & FL_CLIENT)
697                 self.realowner.monstercount -= 1;
698                 
699         totalspawned -= 1;
700 }
701
702 // used to hook into monster post spawn functions without a mutator
703 void monster_hook_spawn()
704 {
705         Monster_CheckMinibossFlag();
706
707         self.max_health = self.health;
708         self.pain_finished = self.nextthink;
709
710         monster_precachesounds();
711         
712         if(teamplay && self.team)
713         {
714                 self.colormod = Team_ColorRGB(self.team);
715                 self.monster_attack = TRUE;
716         }
717         
718         if (self.target)
719         {
720                 self.target2 = self.target;
721                 self.goalentity = find(world, targetname, self.target);
722         }
723                 
724         if(autocvar_g_monsters_healthbars)
725         {
726                 WaypointSprite_Spawn(self.netname, 0, 600, self, '0 0 1' * self.sprite_height, world, 0, self, sprite, FALSE, RADARICON_DANGER, ((self.team) ? Team_ColorRGB(self.team) : '1 0 0'));    
727                 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
728                 WaypointSprite_UpdateHealth(self.sprite, self.health);
729         }
730         
731         monster_sound(self.msound_spawn, 0, FALSE);
732
733         MUTATOR_CALLHOOK(MonsterSpawn);
734 }
735
736 float monster_initialize(string  net_name,
737                                                  string  bodymodel,
738                                                  vector  min_s,
739                                                  vector  max_s,
740                                                  float   nodrop,
741                                                  void() dieproc,
742                                                  void() spawnproc)
743 {
744         if not(autocvar_g_monsters)
745                 return FALSE;
746                 
747         // support for quake style removing monsters based on skill
748         if(monster_skill <= autocvar_g_monsters_skill_easy && (self.spawnflags & MONSTERSKILL_NOTEASY)) { return FALSE; }
749         if(monster_skill == autocvar_g_monsters_skill_normal && (self.spawnflags & MONSTERSKILL_NOTMEDIUM)) { return FALSE; }
750         if(monster_skill == autocvar_g_monsters_skill_hard && (self.spawnflags & MONSTERSKILL_NOTHARD)) { return FALSE; }
751         if(monster_skill == autocvar_g_monsters_skill_insane && (self.spawnflags & MONSTERSKILL_NOTINSANE)) { return FALSE; }
752         if(monster_skill >= autocvar_g_monsters_skill_nightmare && (self.spawnflags & MONSTERSKILL_NOTNIGHTMARE)) { return FALSE; }
753
754         if(self.model == "")
755         if(bodymodel == "")
756                 error("monsters: missing bodymodel!");
757
758         if(self.netname == "")
759         {
760                 if(net_name != "" && self.realowner.classname == STR_PLAYER)
761                         net_name = strzone(strdecolorize(sprintf("%s's %s", self.realowner.netname, net_name)));
762                 self.netname = ((net_name == "") ? self.classname : net_name);
763         }
764         
765         if not(self.scale)
766                 self.scale = 1;
767         
768         if(self.spawnflags & MONSTERFLAG_GIANT && !autocvar_g_monsters_nogiants)
769                 ScaleMonster(5);
770         else
771                 ScaleMonster(self.scale);
772                 
773         min_s *= self.scale;
774         max_s *= self.scale;
775
776         if(self.team && !teamplay)
777                 self.team = 0;
778
779         self.flags = FL_MONSTER;
780         
781         if(self.model != "")
782                 bodymodel = self.model;
783                 
784         if not(self.spawnflags & MONSTERFLAG_SPAWNED) // naturally spawned monster
785         if not(self.monster_respawned)
786                 monsters_total += 1;
787         
788         precache_model(bodymodel);
789
790         setmodel(self, bodymodel);
791         
792         setsize(self, min_s, max_s);
793
794         self.takedamage                 = DAMAGE_AIM;
795         self.bot_attack                 = TRUE;
796         self.iscreature                 = TRUE;
797         self.teleportable               = TRUE;
798         self.damagedbycontents  = TRUE;
799         self.damageforcescale   = 0.003;
800         self.monster_die                = dieproc;
801         self.event_damage               = monsters_damage;
802         self.touch                              = MonsterTouch;
803         self.use                                = monster_use;
804         self.solid                              = SOLID_BBOX;
805         self.movetype                   = MOVETYPE_WALK;
806         self.delay                              = -1; // used in attack delay code
807         monsters_spawned           += 1;
808         self.think                              = spawnproc;
809         self.nextthink                  = time;
810         self.enemy                              = world;
811         self.velocity                   = '0 0 0';
812         self.moveto                             = self.origin;
813         self.pos1                               = self.origin;
814         self.pos2                               = self.angles;
815         self.candrop                    = TRUE;
816         
817         if not(self.target_range)
818                 self.target_range = autocvar_g_monsters_target_range;
819         
820         if not(self.respawntime)
821                 self.respawntime = autocvar_g_monsters_respawn_delay;
822         
823         if not(self.monster_moveflags)
824                 self.monster_moveflags = MONSTER_MOVE_WANDER;
825
826         if(autocvar_g_nodepthtestplayers)
827                 self.effects |= EF_NODEPTHTEST;
828
829         if(autocvar_g_fullbrightplayers)
830                 self.effects |= EF_FULLBRIGHT;
831
832         if not(nodrop)
833         {
834                 setorigin(self, self.origin);
835                 tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
836                 setorigin(self, trace_endpos);
837         }
838
839         return TRUE;
840 }