]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Merge branch 'master' into Mario/ctf_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / selection.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../../dpdefs/progsdefs.qh"
5     #include "../../dpdefs/dpextensions.qh"
6     #include "../../common/constants.qh"
7     #include "../../common/util.qh"
8     #include "../../common/weapons/weapons.qh"
9     #include "selection.qh"
10     #include "weaponsystem.qh"
11     #include "../t_items.qh"
12     #include "../autocvars.qh"
13     #include "../defs.qh"
14 #endif
15
16 // switch between weapons
17 void Send_WeaponComplain(entity e, float wpn, float type)
18 {
19         msg_entity = e;
20         WriteByte(MSG_ONE, SVC_TEMPENTITY);
21         WriteByte(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
22         WriteByte(MSG_ONE, wpn);
23         WriteByte(MSG_ONE, type);
24 }
25
26 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
27 {
28         float f;
29         entity oldself;
30
31         if(time < self.hasweapon_complain_spam)
32                 complain = 0;
33
34         if(wpn == WEP_HOOK && !g_grappling_hook && autocvar_g_nades && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
35                 complain = 0;
36
37         if(complain)
38                 self.hasweapon_complain_spam = time + 0.2;
39
40         if (wpn < WEP_FIRST || wpn > WEP_LAST)
41         {
42                 if (complain)
43                         sprint(self, "Invalid weapon\n");
44                 return false;
45         }
46         if (cl.weapons & WepSet_FromWeapon(wpn))
47         {
48                 if (andammo)
49                 {
50                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
51                         {
52                                 f = 1;
53                         }
54                         else
55                         {
56                                 oldself = self;
57                                 self = cl;
58                                 f = WEP_ACTION(wpn, WR_CHECKAMMO1);
59                                 f = f + WEP_ACTION(wpn, WR_CHECKAMMO2);
60
61                                 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
62                                 entity mine;
63                                 if(wpn == WEP_MINE_LAYER)
64                                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
65                                         f = 1;
66
67                                 self = oldself;
68                         }
69                         if (!f)
70                         {
71                                 if (complain)
72                                 if(IS_REAL_CLIENT(cl))
73                                 {
74                                         play2(cl, "weapons/unavailable.wav");
75                                         Send_WeaponComplain (cl, wpn, 0);
76                                 }
77                                 return false;
78                         }
79                 }
80                 return true;
81         }
82         if (complain)
83         {
84                 // DRESK - 3/16/07
85                 // Report Proper Weapon Status / Modified Weapon Ownership Message
86                 if (weaponsInMap & WepSet_FromWeapon(wpn))
87                 {
88                         Send_WeaponComplain(cl, wpn, 1);
89
90                         if(autocvar_g_showweaponspawns)
91                         {
92                                 entity e;
93
94                                 for(e = world; (e = findfloat(e, weapon, wpn)); )
95                                 {
96                                         if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
97                                                 continue;
98                                         if(!(e.flags & FL_ITEM))
99                                                 continue;
100                                         WaypointSprite_Spawn(
101                                                 (get_weaponinfo(wpn)).wpmodel,
102                                                 1, 0,
103                                                 world, e.origin + ('0 0 1' * e.maxs.z) * 1.2,
104                                                 self, 0,
105                                                 world, enemy,
106                                                 0,
107                                                 RADARICON_NONE, '0 0 0'
108                                         );
109                                 }
110                         }
111                 }
112                 else
113                 {
114                         Send_WeaponComplain (cl, wpn, 2);
115                 }
116
117                 play2(cl, "weapons/unavailable.wav");
118         }
119         return false;
120 }
121
122 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
123 {
124         // We cannot tokenize in this function, as GiveItems calls this
125         // function. Thus we must use car/cdr.
126         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
127         WepSet wepset = '0 0 0';
128         switchtonext = switchtolast = 0;
129         first_valid = prev_valid = 0;
130         float weaponcur;
131         entity wep;
132
133         if(skipmissing || pl.selectweapon == 0)
134                 weaponcur = pl.switchweapon;
135         else
136                 weaponcur = pl.selectweapon;
137
138         if(dir == 0)
139                 switchtonext = 1;
140
141         int c = 0;
142
143         string rest = weaponorder;
144         while(rest != "")
145         {
146                 weaponwant = stof(car(rest)); rest = cdr(rest);
147                 wep = get_weaponinfo(weaponwant);
148                 wepset = WepSet_FromWeapon(weaponwant);
149                 if(imp >= 0)
150                 if(wep.impulse != imp)
151                         continue;
152
153                 float i, have_other = false;
154                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
155                 {
156                         if(i != weaponwant)
157                         if((get_weaponinfo(i)).impulse == imp || imp < 0)
158                         if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
159                                 have_other = true;
160                 }
161
162                 // skip weapons we don't own that aren't normal and aren't in the map
163                 if(!(pl.weapons & wepset))
164                 if(!(weaponsInMap & wepset))
165                 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
166                         continue;
167
168                 ++c;
169
170                 if(!skipmissing || client_hasweapon(pl, weaponwant, true, false))
171                 {
172                         if(switchtonext)
173                                 return weaponwant;
174                         if(!first_valid)
175                                 first_valid = weaponwant;
176                         if(weaponwant == weaponcur)
177                         {
178                                 if(dir >= 0)
179                                         switchtonext = 1;
180                                 else if(prev_valid)
181                                         return prev_valid;
182                                 else
183                                         switchtolast = 1;
184                         }
185                         prev_valid = weaponwant;
186                 }
187         }
188         if(first_valid)
189         {
190                 if(switchtolast)
191                         return prev_valid;
192                 else
193                         return first_valid;
194         }
195         // complain (but only for one weapon on the button that has been pressed)
196         if(complain)
197         {
198                 self.weaponcomplainindex += 1;
199                 c = (self.weaponcomplainindex % c) + 1;
200                 rest = weaponorder;
201                 while(rest != "")
202                 {
203                         weaponwant = stof(car(rest)); rest = cdr(rest);
204                         wep = get_weaponinfo(weaponwant);
205                         wepset = WepSet_FromWeapon(weaponwant);
206                         if(imp >= 0)
207                                 if(wep.impulse != imp)
208                                         continue;
209
210                         float i, have_other = false;
211                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
212                         {
213                                 if(i != weaponwant)
214                                 if((get_weaponinfo(i)).impulse == imp || imp < 0)
215                                 if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
216                                         have_other = true;
217                         }
218
219                         // skip weapons we don't own that aren't normal and aren't in the map
220                         if(!(pl.weapons & wepset))
221                         if(!(weaponsInMap & wepset))
222                         if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
223                                 continue;
224
225                         --c;
226                         if(c == 0)
227                         {
228                                 client_hasweapon(pl, weaponwant, true, true);
229                                 break;
230                         }
231                 }
232         }
233         return 0;
234 }
235
236 void W_SwitchWeapon_Force(entity e, float w)
237 {
238         e.cnt = e.switchweapon;
239         e.switchweapon = w;
240         e.selectweapon = w;
241 }
242
243 // perform weapon to attack (weaponstate and attack_finished check is here)
244 void W_SwitchToOtherWeapon(entity pl)
245 {
246         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
247         float w, ww;
248         w = pl.weapon;
249         if(pl.weapons & WepSet_FromWeapon(w))
250         {
251                 pl.weapons &= ~WepSet_FromWeapon(w);
252                 ww = w_getbestweapon(pl);
253                 pl.weapons |= WepSet_FromWeapon(w);
254         }
255         else
256                 ww = w_getbestweapon(pl);
257         if(ww)
258                 W_SwitchWeapon_Force(pl, ww);
259 }
260
261 void W_SwitchWeapon(float imp)
262 {
263         if (self.switchweapon != imp)
264         {
265                 if (client_hasweapon(self, imp, true, true))
266                         W_SwitchWeapon_Force(self, imp);
267                 else
268                         self.selectweapon = imp; // update selectweapon ANYWAY
269         }
270         else if(!forbidWeaponUse()) { WEP_ACTION(self.weapon, WR_RELOAD); }
271 }
272
273 void W_CycleWeapon(string weaponorder, float dir)
274 {
275         float w;
276         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
277         if(w > 0)
278                 W_SwitchWeapon(w);
279 }
280
281 void W_NextWeaponOnImpulse(float imp)
282 {
283         float w;
284         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
285         if(w > 0)
286                 W_SwitchWeapon(w);
287 }
288
289 // next weapon
290 void W_NextWeapon(float list)
291 {
292         if(list == 0)
293                 W_CycleWeapon(weaponorder_byid, -1);
294         else if(list == 1)
295                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
296         else if(list == 2)
297                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
298 }
299
300 // prev weapon
301 void W_PreviousWeapon(float list)
302 {
303         if(list == 0)
304                 W_CycleWeapon(weaponorder_byid, +1);
305         else if(list == 1)
306                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
307         else if(list == 2)
308                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
309 }
310
311 // previously used if exists and has ammo, (second) best otherwise
312 void W_LastWeapon(void)
313 {
314         if(client_hasweapon(self, self.cnt, true, false))
315                 W_SwitchWeapon(self.cnt);
316         else
317                 W_SwitchToOtherWeapon(self);
318 }