]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Merge branch 'master' into terencehill/menu_hudskin_selector
[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                                 entity e;
84
85                                 for(e = world; (e = findfloat(e, weapon, wpn.m_id)); )
86                                 {
87                                         if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
88                                                 continue;
89                                         if(!(e.flags & FL_ITEM))
90                                                 continue;
91                                         entity wp = WaypointSprite_Spawn(
92                                                 WP_Weapon,
93                                                 1, 0,
94                                                 world, e.origin + ('0 0 1' * e.maxs.z) * 1.2,
95                                                 cl, 0,
96                                                 world, enemy,
97                                                 0,
98                                                 RADARICON_NONE
99                                         );
100                                         wp.wp_extra = wpn.m_id;
101                                 }
102                         }
103                 }
104                 else
105                 {
106                         Send_WeaponComplain (cl, wpn.m_id, 2);
107                 }
108
109                 play2(cl, SND(UNAVAILABLE));
110         }
111         return false;
112 }
113
114 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
115 {SELFPARAM();
116         // We cannot tokenize in this function, as GiveItems calls this
117         // function. Thus we must use car/cdr.
118         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
119         WepSet wepset = '0 0 0';
120         switchtonext = switchtolast = 0;
121         first_valid = prev_valid = 0;
122         float weaponcur;
123         entity wep;
124
125         if(skipmissing || pl.selectweapon == 0)
126                 weaponcur = pl.switchweapon;
127         else
128                 weaponcur = pl.selectweapon;
129
130         if(dir == 0)
131                 switchtonext = 1;
132
133         int c = 0;
134
135         string rest = weaponorder;
136         while(rest != "")
137         {
138                 weaponwant = stof(car(rest)); rest = cdr(rest);
139                 wep = Weapons_from(weaponwant);
140                 wepset = wep.m_wepset;
141                 if(imp >= 0)
142                 if(wep.impulse != imp)
143                         continue;
144
145                 bool have_other = false;
146                 FOREACH(Weapons, it != WEP_Null, {
147                         if(i != weaponwant)
148                         if(it.impulse == imp || imp < 0)
149                         if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
150                                 have_other = true;
151                 });
152
153                 // skip weapons we don't own that aren't normal and aren't in the map
154                 if(!(pl.weapons & wepset))
155                 if(!(weaponsInMap & wepset))
156                 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
157                         continue;
158
159                 ++c;
160
161                 if(!skipmissing || client_hasweapon(pl, wep, true, false))
162                 {
163                         if(switchtonext)
164                                 return weaponwant;
165                         if(!first_valid)
166                                 first_valid = weaponwant;
167                         if(weaponwant == weaponcur)
168                         {
169                                 if(dir >= 0)
170                                         switchtonext = 1;
171                                 else if(prev_valid)
172                                         return prev_valid;
173                                 else
174                                         switchtolast = 1;
175                         }
176                         prev_valid = weaponwant;
177                 }
178         }
179         if(first_valid)
180         {
181                 if(switchtolast)
182                         return prev_valid;
183                 else
184                         return first_valid;
185         }
186         // complain (but only for one weapon on the button that has been pressed)
187         if(complain)
188         {
189                 self.weaponcomplainindex += 1;
190                 c = (self.weaponcomplainindex % c) + 1;
191                 rest = weaponorder;
192                 while(rest != "")
193                 {
194                         weaponwant = stof(car(rest)); rest = cdr(rest);
195                         wep = Weapons_from(weaponwant);
196                         wepset = wep.m_wepset;
197                         if(imp >= 0)
198                                 if(wep.impulse != imp)
199                                         continue;
200
201                         bool have_other = false;
202                         FOREACH(Weapons, it != WEP_Null, {
203                                 if(i != weaponwant)
204                                 if(it.impulse == imp || imp < 0)
205                                 if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
206                                         have_other = true;
207                         });
208
209                         // skip weapons we don't own that aren't normal and aren't in the map
210                         if(!(pl.weapons & wepset))
211                         if(!(weaponsInMap & wepset))
212                         if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
213                                 continue;
214
215                         --c;
216                         if(c == 0)
217                         {
218                                 client_hasweapon(pl, wep, true, true);
219                                 break;
220                         }
221                 }
222         }
223         return 0;
224 }
225
226 void W_SwitchWeapon_Force(entity e, Weapon wep)
227 {
228         int w = wep.m_id;
229         e.cnt = e.switchweapon;
230         e.switchweapon = w;
231         e.selectweapon = w;
232 }
233
234 // perform weapon to attack (weaponstate and attack_finished check is here)
235 void W_SwitchToOtherWeapon(entity pl)
236 {
237         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
238         int ww;
239         WepSet set = WepSet_FromWeapon(Weapons_from(pl.weapon));
240         if(pl.weapons & set)
241         {
242                 pl.weapons &= ~set;
243                 ww = w_getbestweapon(pl);
244                 pl.weapons |= set;
245         }
246         else
247                 ww = w_getbestweapon(pl);
248         if(ww)
249                 W_SwitchWeapon_Force(pl, Weapons_from(ww));
250 }
251
252 void W_SwitchWeapon(Weapon w)
253 {SELFPARAM();
254         int imp = w.m_id;
255         if (self.switchweapon != imp)
256         {
257                 if (client_hasweapon(self, w, true, true))
258                         W_SwitchWeapon_Force(self, w);
259                 else
260                         self.selectweapon = imp; // update selectweapon ANYWAY
261         }
262         else if(!forbidWeaponUse(self)) {
263                 w.wr_reload(w);
264         }
265 }
266
267 void W_CycleWeapon(string weaponorder, float dir)
268 {SELFPARAM();
269         float w;
270         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
271         if(w > 0)
272                 W_SwitchWeapon(Weapons_from(w));
273 }
274
275 void W_NextWeaponOnImpulse(float imp)
276 {SELFPARAM();
277         float w;
278         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
279         if(w > 0)
280                 W_SwitchWeapon(Weapons_from(w));
281 }
282
283 // next weapon
284 void W_NextWeapon(float list)
285 {SELFPARAM();
286         if(list == 0)
287                 W_CycleWeapon(weaponorder_byid, -1);
288         else if(list == 1)
289                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
290         else if(list == 2)
291                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
292 }
293
294 // prev weapon
295 void W_PreviousWeapon(float list)
296 {SELFPARAM();
297         if(list == 0)
298                 W_CycleWeapon(weaponorder_byid, +1);
299         else if(list == 1)
300                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
301         else if(list == 2)
302                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
303 }
304
305 // previously used if exists and has ammo, (second) best otherwise
306 void W_LastWeapon()
307 {SELFPARAM();
308         Weapon wep = Weapons_from(self.cnt);
309         if(client_hasweapon(self, wep, true, false))
310                 W_SwitchWeapon(wep);
311         else
312                 W_SwitchToOtherWeapon(self);
313 }