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