]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
#include this
[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.qc"
5     #include "../../dpdefs/dpextensions.qc"
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, c;
127         string rest;
128         WepSet wepset = '0 0 0';
129         switchtonext = switchtolast = 0;
130         first_valid = prev_valid = 0;
131         float weaponcur;
132         entity wep;
133
134         if(skipmissing || pl.selectweapon == 0)
135                 weaponcur = pl.switchweapon;
136         else
137                 weaponcur = pl.selectweapon;
138
139         if(dir == 0)
140                 switchtonext = 1;
141
142         c = 0;
143
144         rest = weaponorder;
145         while(rest != "")
146         {
147                 weaponwant = stof(car(rest)); rest = cdr(rest);
148                 wep = get_weaponinfo(weaponwant);
149                 wepset = WepSet_FromWeapon(weaponwant);
150                 if(imp >= 0)
151                 if(wep.impulse != imp)
152                         continue;
153
154                 float i, have_other = false;
155                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
156                 {
157                         if(i != weaponwant)
158                         if((get_weaponinfo(i)).impulse == imp || imp < 0)
159                         if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
160                                 have_other = true;
161                 }
162
163                 // skip weapons we don't own that aren't normal and aren't in the map
164                 if(!(pl.weapons & wepset))
165                 if(!(weaponsInMap & wepset))
166                 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
167                         continue;
168
169                 ++c;
170
171                 if(!skipmissing || client_hasweapon(pl, weaponwant, true, false))
172                 {
173                         if(switchtonext)
174                                 return weaponwant;
175                         if(!first_valid)
176                                 first_valid = weaponwant;
177                         if(weaponwant == weaponcur)
178                         {
179                                 if(dir >= 0)
180                                         switchtonext = 1;
181                                 else if(prev_valid)
182                                         return prev_valid;
183                                 else
184                                         switchtolast = 1;
185                         }
186                         prev_valid = weaponwant;
187                 }
188         }
189         if(first_valid)
190         {
191                 if(switchtolast)
192                         return prev_valid;
193                 else
194                         return first_valid;
195         }
196         // complain (but only for one weapon on the button that has been pressed)
197         if(complain)
198         {
199                 self.weaponcomplainindex += 1;
200                 c = (self.weaponcomplainindex % c) + 1;
201                 rest = weaponorder;
202                 while(rest != "")
203                 {
204                         weaponwant = stof(car(rest)); rest = cdr(rest);
205                         wep = get_weaponinfo(weaponwant);
206                         wepset = WepSet_FromWeapon(weaponwant);
207                         if(imp >= 0)
208                                 if(wep.impulse != imp)
209                                         continue;
210
211                         float i, have_other = false;
212                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
213                         {
214                                 if(i != weaponwant)
215                                 if((get_weaponinfo(i)).impulse == imp || imp < 0)
216                                 if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
217                                         have_other = true;
218                         }
219
220                         // skip weapons we don't own that aren't normal and aren't in the map
221                         if(!(pl.weapons & wepset))
222                         if(!(weaponsInMap & wepset))
223                         if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
224                                 continue;
225
226                         --c;
227                         if(c == 0)
228                         {
229                                 client_hasweapon(pl, weaponwant, true, true);
230                                 break;
231                         }
232                 }
233         }
234         return 0;
235 }
236
237 void W_SwitchWeapon_Force(entity e, float w)
238 {
239         e.cnt = e.switchweapon;
240         e.switchweapon = w;
241         e.selectweapon = w;
242 }
243
244 // perform weapon to attack (weaponstate and attack_finished check is here)
245 void W_SwitchToOtherWeapon(entity pl)
246 {
247         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
248         float w, ww;
249         w = pl.weapon;
250         if(pl.weapons & WepSet_FromWeapon(w))
251         {
252                 pl.weapons &= ~WepSet_FromWeapon(w);
253                 ww = w_getbestweapon(pl);
254                 pl.weapons |= WepSet_FromWeapon(w);
255         }
256         else
257                 ww = w_getbestweapon(pl);
258         if(ww)
259                 W_SwitchWeapon_Force(pl, ww);
260 }
261
262 void W_SwitchWeapon(float imp)
263 {
264         if (self.switchweapon != imp)
265         {
266                 if (client_hasweapon(self, imp, true, true))
267                         W_SwitchWeapon_Force(self, imp);
268                 else
269                         self.selectweapon = imp; // update selectweapon ANYWAY
270         }
271         else if(!forbidWeaponUse()) { WEP_ACTION(self.weapon, WR_RELOAD); }
272 }
273
274 void W_CycleWeapon(string weaponorder, float dir)
275 {
276         float w;
277         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
278         if(w > 0)
279                 W_SwitchWeapon(w);
280 }
281
282 void W_NextWeaponOnImpulse(float imp)
283 {
284         float w;
285         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
286         if(w > 0)
287                 W_SwitchWeapon(w);
288 }
289
290 // next weapon
291 void W_NextWeapon(float list)
292 {
293         if(list == 0)
294                 W_CycleWeapon(weaponorder_byid, -1);
295         else if(list == 1)
296                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
297         else if(list == 2)
298                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
299 }
300
301 // prev weapon
302 void W_PreviousWeapon(float list)
303 {
304         if(list == 0)
305                 W_CycleWeapon(weaponorder_byid, +1);
306         else if(list == 1)
307                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
308         else if(list == 2)
309                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
310 }
311
312 // previously used if exists and has ammo, (second) best otherwise
313 void W_LastWeapon(void)
314 {
315         if(client_hasweapon(self, self.cnt, true, false))
316                 W_SwitchWeapon(self.cnt);
317         else
318                 W_SwitchToOtherWeapon(self);
319 }