Makefile: use `-I.`
[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                                 entity mine;
77                                 if(wpn == WEP_MINE_LAYER)
78                                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == cl)
79                                         f = 1;
80
81                         }
82                         if (!f)
83                         {
84                                 if (complain)
85                                 if(IS_REAL_CLIENT(cl))
86                                 {
87                                         play2(cl, SND(UNAVAILABLE));
88                                         Send_WeaponComplain (cl, wpn.m_id, 0);
89                                 }
90                                 return false;
91                         }
92                 }
93                 return true;
94         }
95         if (complain)
96         {
97                 // DRESK - 3/16/07
98                 // Report Proper Weapon Status / Modified Weapon Ownership Message
99                 if (weaponsInMap & WepSet_FromWeapon(wpn))
100                 {
101                         Send_WeaponComplain(cl, wpn.m_id, 1);
102                         Weapon_whereis(wpn, cl);
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 = PS(pl).m_switchweapon.m_id;
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         e.cnt = PS(e).m_switchweapon.m_id;
229         PS(e).m_switchweapon = wep;
230         e.selectweapon = wep.m_id;
231 }
232
233 // perform weapon to attack (weaponstate and attack_finished check is here)
234 void W_SwitchToOtherWeapon(entity pl)
235 {
236         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
237         Weapon ww;
238         WepSet set = WepSet_FromWeapon(PS(pl).m_weapon);
239         if (pl.weapons & set)
240         {
241                 pl.weapons &= ~set;
242                 ww = w_getbestweapon(pl);
243                 pl.weapons |= set;
244         }
245         else
246         {
247                 ww = w_getbestweapon(pl);
248         }
249         if (ww == WEP_Null) return;
250         W_SwitchWeapon_Force(pl, ww);
251 }
252
253 void W_SwitchWeapon(Weapon w)
254 {SELFPARAM();
255         if (PS(self).m_switchweapon != w)
256         {
257                 if (client_hasweapon(self, w, true, true))
258                         W_SwitchWeapon_Force(self, w);
259                 else
260                         self.selectweapon = w.m_id; // update selectweapon ANYWAY
261         }
262         else if(!forbidWeaponUse(self)) {
263                 entity actor = this;
264                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
265                 w.wr_reload(w, actor, weaponentity);
266         }
267 }
268
269 void W_CycleWeapon(string weaponorder, float dir)
270 {SELFPARAM();
271         float w;
272         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
273         if(w > 0)
274                 W_SwitchWeapon(Weapons_from(w));
275 }
276
277 void W_NextWeaponOnImpulse(float imp)
278 {SELFPARAM();
279         float w;
280         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
281         if(w > 0)
282                 W_SwitchWeapon(Weapons_from(w));
283 }
284
285 // next weapon
286 void W_NextWeapon(float list)
287 {SELFPARAM();
288         if(list == 0)
289                 W_CycleWeapon(weaponorder_byid, -1);
290         else if(list == 1)
291                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
292         else if(list == 2)
293                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
294 }
295
296 // prev weapon
297 void W_PreviousWeapon(float list)
298 {SELFPARAM();
299         if(list == 0)
300                 W_CycleWeapon(weaponorder_byid, +1);
301         else if(list == 1)
302                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
303         else if(list == 2)
304                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
305 }
306
307 // previously used if exists and has ammo, (second) best otherwise
308 void W_LastWeapon(entity this)
309 {
310         Weapon wep = Weapons_from(this.cnt);
311         if (client_hasweapon(this, wep, true, false))
312                 W_SwitchWeapon(wep);
313         else
314                 W_SwitchToOtherWeapon(this);
315 }