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