]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/spawning.qc
Push down spawning logic from spawnfuncs to dedicated spawning functions
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / spawning.qc
1 #include "spawning.qh"
2
3 #include "weaponsystem.qh"
4 #include "../resources.qh"
5 #include "../mutators/_mod.qh"
6 #include <common/t_items.qh>
7 #include <common/weapons/_all.qh>
8
9 string W_Apply_Weaponreplace(string in)
10 {
11         string out = "";
12         FOREACH_WORD(in, true, {
13                 string replacement = "";
14                 Weapon w = Weapons_fromstr(it);
15                 if (w)
16                 {
17             replacement = w.weaponreplace;
18             if (replacement == "") replacement = it;
19                 }
20                 if (replacement == "0") continue;
21                 out = cons(out, replacement);
22         });
23         return out;
24 }
25
26 void weapon_defaultspawnfunc(entity this, Weapon e)
27 {
28         Weapon wpn = e;
29         e = wpn = wpn.m_spawnfunc_hookreplace(wpn, this);
30         this.classname = wpn.m_canonical_spawnfunc;
31         if (this.classname != "droppedweapon" && this.classname != "replacedweapon")
32         {
33                 if (e.spawnflags & WEP_FLAG_MUTATORBLOCKED)
34                 {
35                         LOG_WARNF("Attempted to spawn a mutator-blocked weapon rejected: prvm_edict server %i", this);
36                         startitem_failed = true;
37                         return;
38                 }
39
40                 string s = W_Apply_Weaponreplace(e.netname);
41                 MUTATOR_CALLHOOK(SetWeaponreplace, this, e, s);
42                 s = M_ARGV(2, string);
43                 if (s == "")
44                 {
45                         delete(this);
46                         startitem_failed = true;
47                         return;
48                 }
49                 int t = tokenize_console(s);
50                 if (t >= 2)
51                 {
52                         this.team = --internalteam;
53                         for (int i = 1; i < t; ++i)
54                         {
55                                 s = argv(i);
56                                 FOREACH(Weapons, it != WEP_Null, {
57                                         if(it.netname == s)
58                                         {
59                                                 entity replacement = spawn();
60                                                 copyentity(this, replacement);
61                                                 replacement.classname = "replacedweapon";
62                                                 weapon_defaultspawnfunc(replacement, it);
63                                                 break;
64                                         }
65                                 });
66                         }
67                 }
68                 if (t >= 1) // always the case!
69                 {
70                         s = argv(0);
71                         wpn = WEP_Null;
72                         FOREACH(Weapons, it != WEP_Null, {
73                                 if(it.netname == s)
74                                 {
75                                         wpn = it;
76                                         break;
77                                 }
78                         });
79                 }
80                 if (wpn == WEP_Null)
81                 {
82                         delete(this);
83                         startitem_failed = true;
84                         return;
85                 }
86         }
87
88         if (!this.respawntime)
89         {
90                 if (wpn.spawnflags & WEP_FLAG_SUPERWEAPON)
91                 {
92                         this.respawntime = g_pickup_respawntime_superweapon;
93                         this.respawntimejitter = g_pickup_respawntimejitter_superweapon;
94                 }
95                 else
96                 {
97                         this.respawntime = g_pickup_respawntime_weapon;
98                         this.respawntimejitter = g_pickup_respawntimejitter_weapon;
99                 }
100         }
101
102         if (wpn.spawnflags & WEP_FLAG_SUPERWEAPON)
103                 if (!this.superweapons_finished)
104                         this.superweapons_finished = autocvar_g_balance_superweapons_time;
105
106         // if we don't already have ammo, give us some ammo
107         if ((wpn.ammo_type != RESOURCE_NONE) && !GetResourceAmount(this, wpn.ammo_type))
108         {
109                 switch (wpn.ammo_type)
110                 {
111                         case RESOURCE_SHELLS:  SetResourceAmount(this, wpn.ammo_type, cvar("g_pickup_shells_weapon"));  break;
112                         case RESOURCE_BULLETS: SetResourceAmount(this, wpn.ammo_type, cvar("g_pickup_nails_weapon"));   break;
113                         case RESOURCE_ROCKETS: SetResourceAmount(this, wpn.ammo_type, cvar("g_pickup_rockets_weapon")); break;
114                         case RESOURCE_CELLS:   SetResourceAmount(this, wpn.ammo_type, cvar("g_pickup_cells_weapon"));   break;
115                         case RESOURCE_PLASMA:  SetResourceAmount(this, wpn.ammo_type, cvar("g_pickup_plasma_weapon"));  break;
116                         case RESOURCE_FUEL:    SetResourceAmount(this, wpn.ammo_type, cvar("g_pickup_fuel_weapon"));    break;
117                 }
118         }
119
120         #if 0 // WEAPONTODO
121         if (wpn.items)
122         {
123                 for (int i = 0, j = 1; i < 24; ++i, j <<= 1)
124                 {
125                         if (wpn.items & j)
126                         {
127                                 ammotype = Item_CounterField(j);
128                                 if (!this.ammotype)
129                                         this.ammotype = cvar(strcat("g_pickup_", Item_CounterFieldName(j), "_weapon"));
130                         }
131                 }
132         }
133         #endif
134
135         // pickup anyway
136         if (g_pickup_weapons_anyway)
137                 this.pickup_anyway = true;
138
139         if(!this.owner)
140                 this.glowmod = wpn.wpcolor;
141
142         GameItem def = wpn.m_pickup;
143         _StartItem(
144                 this,
145                 this.itemdef = def,
146                 this.respawntime, // defaultrespawntime
147                 this.respawntimejitter // defaultrespawntimejitter
148         );
149         #if 0 // WEAPONTODO
150         if (this.modelindex) { // don't precache if this was removed
151                 wpn.wr_init(wpn);
152         }
153         #endif
154 }