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