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