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