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