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