497dee86664b54d644f405e4be5ea47fc01969e9
[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 #endif
11 entity spawnmonster (entity e, string monster, int monster_id, entity spawnedby, entity own, vector orig, bool respwn, bool removeifinvalid, int moveflag)
12 {
13         e.spawnflags = MONSTERFLAG_SPAWNED;
14
15         if(!respwn) { e.spawnflags |= MONSTERFLAG_NORESPAWN; }
16         //if(invincible) { e.spawnflags |= MONSTERFLAG_INVINCIBLE; }
17
18         setorigin(e, orig);
19
20         if(monster == "random")
21         {
22                 RandomSelection_Init(); 
23                 FOREACH(Monsters, it != MON_Null && !(it.spawnflags & MONSTER_TYPE_PASSIVE),
24                 {
25                         RandomSelection_AddEnt(it, 1, 1);
26                 });
27
28                 monster_id = RandomSelection_chosen_ent.monsterid;
29         }
30         else if(monster != "")
31         {
32                 bool found = false;
33                 FOREACH(Monsters, it != MON_Null,
34                 {
35                         if(it.netname == monster)
36                         {
37                                 found = true;
38                                 monster_id = it.monsterid; // we have the monster, old monster id is no longer required
39                                 break;
40                         }
41                 });
42
43                 if(!found && !monster_id)
44                 {
45                         if(removeifinvalid)
46                         {
47                                 delete(e);
48                                 return NULL; // no good
49                         }
50                         else
51                                 monster_id = MON_FIRST;
52                 }
53         }
54
55         e.realowner = spawnedby;
56
57         if(moveflag)
58                 e.monster_moveflags = moveflag;
59
60         if(IS_PLAYER(spawnedby))
61         {
62                 if(teamplay && autocvar_g_monsters_teams)
63                         e.team = spawnedby.team; // colors handled in spawn code
64
65                 if(autocvar_g_monsters_owners)
66                         e.monster_follow = own; // using .owner makes the monster non-solid for its master
67
68                 e.angles_y = spawnedby.angles_y;
69         }
70
71         // Monster_Spawn checks if monster is valid
72         if(!Monster_Spawn(e, false, monster_id))
73         {
74                 delete(e);
75                 return NULL; // remove even if told not to, as we didn't spawn any kind of monster
76         }
77
78         return e;
79 }