]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/monsters/sv_spawn.qc
Further cleanup of defs.qh
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / sv_spawn.qc
1 #include "sv_spawn.qh"
2 #if defined(CSQC)
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5     #include "../util.qh"
6     #include "all.qh"
7     #include "sv_monsters.qh"
8     #include <server/autocvars.qh>
9     #include <server/defs.qh>
10     #include <server/weapons/common.qh>
11 #endif
12 entity spawnmonster (entity e, string monster, Monster monster_id, entity spawnedby, entity own, vector orig, bool respwn, bool removeifinvalid, int moveflag)
13 {
14         e.spawnflags = MONSTERFLAG_SPAWNED;
15
16         if(!respwn) { e.spawnflags |= MONSTERFLAG_NORESPAWN; }
17         //if(invincible) { e.spawnflags |= MONSTERFLAG_INVINCIBLE; }
18
19         setorigin(e, orig);
20         bool allow_any = boolean(monster == "anyrandom");
21
22         if(monster == "random" || allow_any)
23         {
24                 RandomSelection_Init();
25                 FOREACH(Monsters, it != MON_Null && (allow_any || !(it.spawnflags & MON_FLAG_HIDDEN)) && !(it.spawnflags & MONSTER_TYPE_PASSIVE),
26                 {
27                         RandomSelection_AddEnt(it, 1, 1);
28                 });
29
30                 monster_id = RandomSelection_chosen_ent;
31         }
32         else if(monster != "")
33         {
34                 bool found = false;
35                 FOREACH(Monsters, it != MON_Null,
36                 {
37                         if(it.netname == monster)
38                         {
39                                 found = true;
40                                 monster_id = it; // we have the monster, old monster id is no longer required
41                                 break;
42                         }
43                 });
44
45                 if(!found && monster_id == MON_Null)
46                 {
47                         if(removeifinvalid)
48                         {
49                                 delete(e);
50                                 return NULL; // no good
51                         }
52                         else
53                         {
54                                 // select a random valid monster type if no valid monster was provided
55                                 return spawnmonster(e, "random", MON_Null, spawnedby, own, orig, respwn, removeifinvalid, moveflag);
56                         }
57                 }
58         }
59
60         e.realowner = spawnedby;
61
62         if(moveflag)
63                 e.monster_moveflags = moveflag;
64
65         if(IS_PLAYER(spawnedby))
66         {
67                 if(teamplay && autocvar_g_monsters_teams)
68                         e.team = spawnedby.team; // colors handled in spawn code
69
70                 if(autocvar_g_monsters_owners)
71                         e.monster_follow = own; // using .owner makes the monster non-solid for its master
72
73                 e.angles_y = spawnedby.angles_y;
74         }
75
76         // Monster_Spawn checks if monster is valid
77         if(!Monster_Spawn(e, false, monster_id))
78         {
79                 delete(e);
80                 return NULL; // remove even if told not to, as we didn't spawn any kind of monster
81         }
82
83         return e;
84 }