Merge branch 'master' into terencehill/music_player
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / spawning.qc
1 string W_Apply_Weaponreplace(string in)
2 {
3         float n = tokenize_console(in);
4         string out = "", s, replacement;
5         float i, j;
6         entity e;
7         for(i = 0; i < n; ++i)
8         {
9                 replacement = "";
10                 s = argv(i);
11                 
12                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
13                 {
14                         e = get_weaponinfo(j);
15                         if(e.netname == s)
16                         {
17                                 replacement = e.weaponreplace;
18                         }
19                 }
20
21                 if(replacement == "")
22                         out = strcat(out, " ", s);
23                 else if(replacement != "0")
24                         out = strcat(out, " ", replacement);
25         }
26         return substring(out, 1, -1);
27 }
28
29 void weapon_defaultspawnfunc(float wpn)
30 {
31         entity e;
32         float t;
33         string s;
34         entity oldself;
35         float i, j;
36         float f;
37
38         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")
39         {
40                 e = get_weaponinfo(wpn);
41
42                 if(e.spawnflags & WEP_FLAG_MUTATORBLOCKED)
43                 {
44                         objerror("Attempted to spawn a mutator-blocked weapon rejected");
45                         startitem_failed = TRUE;
46                         return;
47                 }
48
49                 s = W_Apply_Weaponreplace(e.netname);
50                 ret_string = s;
51                 other = e;
52                 MUTATOR_CALLHOOK(SetWeaponreplace);
53                 s = ret_string;
54                 if(s == "")
55                 {
56                         remove(self);
57                         startitem_failed = TRUE;
58                         return;
59                 }
60                 t = tokenize_console(s);
61                 if(t >= 2)
62                 {
63                         self.team = --internalteam;
64                         oldself = self;
65                         for(i = 1; i < t; ++i)
66                         {
67                                 s = argv(i);
68                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
69                                 {
70                                         e = get_weaponinfo(j);
71                                         if(e.netname == s)
72                                         {
73                                                 self = spawn();
74                                                 copyentity(oldself, self);
75                                                 self.classname = "replacedweapon";
76                                                 weapon_defaultspawnfunc(j);
77                                                 break;
78                                         }
79                                 }
80                                 if(j > WEP_LAST)
81                                 {
82                                         print("The weapon replace list for ", oldself.classname, " contains an unknown weapon ", s, ". Skipped.\n");
83                                 }
84                         }
85                         self = oldself;
86                 }
87                 if(t >= 1) // always the case!
88                 {
89                         s = argv(0);
90                         wpn = 0;
91                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
92                         {
93                                 e = get_weaponinfo(j);
94                                 if(e.netname == s)
95                                 {
96                                         wpn = j;
97                                         break;
98                                 }
99                         }
100                         if(j > WEP_LAST)
101                         {
102                                 print("The weapon replace list for ", self.classname, " contains an unknown weapon ", s, ". Skipped.\n");
103                         }
104                 }
105                 if(wpn == 0)
106                 {
107                         remove(self);
108                         startitem_failed = TRUE;
109                         return;
110                 }
111         }
112
113         e = get_weaponinfo(wpn);
114
115         if(!self.respawntime)
116         {
117                 if(e.weapons & WEPSET_SUPERWEAPONS)
118                 {
119                         self.respawntime = g_pickup_respawntime_superweapon;
120                         self.respawntimejitter = g_pickup_respawntimejitter_superweapon;
121                 }
122                 else
123                 {
124                         self.respawntime = g_pickup_respawntime_weapon;
125                         self.respawntimejitter = g_pickup_respawntimejitter_weapon;
126                 }
127         }
128
129         if(e.weapons & WEPSET_SUPERWEAPONS)
130                 if(!self.superweapons_finished)
131                         self.superweapons_finished = autocvar_g_balance_superweapons_time;
132
133         // if we don't already have ammo, give us some ammo
134         if(!self.(e.ammo_field))
135         {
136                 switch(e.ammo_field)
137                 {
138                         case ammo_shells:  self.ammo_shells  = cvar("g_pickup_shells_weapon");  break;
139                         case ammo_nails:   self.ammo_nails   = cvar("g_pickup_nails_weapon");   break;
140                         case ammo_rockets: self.ammo_rockets = cvar("g_pickup_rockets_weapon"); break;
141                         case ammo_cells:   self.ammo_cells   = cvar("g_pickup_cells_weapon");   break;
142                         case ammo_plasma:  self.ammo_plasma  = cvar("g_pickup_plasma_weapon");  break;
143                         case ammo_fuel:    self.ammo_fuel    = cvar("g_pickup_fuel_weapon");    break;
144                 }
145         }
146
147         #if 0 // WEAPONTODO
148         if(e.items)
149         {
150                 for(i = 0, j = 1; i < 24; ++i, j *= 2)
151                 {
152                         if(e.items & j)
153                         {
154                                 ammotype = Item_CounterField(j);
155                                 if(!self.ammotype)
156                                         self.ammotype = cvar(strcat("g_pickup_", Item_CounterFieldName(j), "_weapon"));
157                         }
158                 }
159         }
160         #endif
161
162         // pickup anyway
163         if(g_pickup_weapons_anyway)
164                 self.pickup_anyway = TRUE;
165
166         f = FL_WEAPON;
167
168         // no weapon-stay on superweapons
169         if(e.weapons & WEPSET_SUPERWEAPONS)
170                 f |= FL_NO_WEAPON_STAY;
171
172         // weapon stay isn't supported for teamed weapons
173         if(self.team)
174                 f |= FL_NO_WEAPON_STAY;
175
176         StartItem(e.model, "weapons/weaponpickup.wav", self.respawntime, self.respawntimejitter, e.message, 0, e.weapon, f, weapon_pickupevalfunc, e.bot_pickupbasevalue);
177         #if 0 // WEAPONTODO
178         if (self.modelindex) // don't precache if self was removed
179                 WEP_ACTION(e.weapon, WR_INIT);
180         #endif
181 }