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