]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Merge branch 'master' into terencehill/tooltips_cleanup
[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         WriteByte(MSG_ONE, SVC_TEMPENTITY);
16         WriteByte(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
17         WriteByte(MSG_ONE, wpn);
18         WriteByte(MSG_ONE, type);
19 }
20
21 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
22 {SELFPARAM();
23         float f;
24
25         if(time < self.hasweapon_complain_spam)
26                 complain = 0;
27
28         // ignore hook button when using other offhand equipment
29         if (cl.offhand != OFFHAND_HOOK)
30         if (wpn == WEP_HOOK.m_id && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
31             complain = 0;
32
33         if(complain)
34                 self.hasweapon_complain_spam = time + 0.2;
35
36         if (wpn < WEP_FIRST || wpn > WEP_LAST)
37         {
38                 if (complain)
39                         sprint(self, "Invalid weapon\n");
40                 return false;
41         }
42         if (cl.weapons & WepSet_FromWeapon(wpn))
43         {
44                 if (andammo)
45                 {
46                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
47                         {
48                                 f = 1;
49                         }
50                         else
51                         {
52                                 setself(cl);
53                                 Weapon w = get_weaponinfo(wpn);
54                                 f = w.wr_checkammo1(w) + w.wr_checkammo2(w);
55
56                                 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
57                                 entity mine;
58                                 if(wpn == WEP_MINE_LAYER.m_id)
59                                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
60                                         f = 1;
61
62                                 setself(this);
63                         }
64                         if (!f)
65                         {
66                                 if (complain)
67                                 if(IS_REAL_CLIENT(cl))
68                                 {
69                                         play2(cl, SND(UNAVAILABLE));
70                                         Send_WeaponComplain (cl, wpn, 0);
71                                 }
72                                 return false;
73                         }
74                 }
75                 return true;
76         }
77         if (complain)
78         {
79                 // DRESK - 3/16/07
80                 // Report Proper Weapon Status / Modified Weapon Ownership Message
81                 if (weaponsInMap & WepSet_FromWeapon(wpn))
82                 {
83                         Send_WeaponComplain(cl, wpn, 1);
84
85                         if(autocvar_g_showweaponspawns)
86                         {
87                                 entity e;
88
89                                 for(e = world; (e = findfloat(e, weapon, wpn)); )
90                                 {
91                                         if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
92                                                 continue;
93                                         if(!(e.flags & FL_ITEM))
94                                                 continue;
95                                         entity wp = WaypointSprite_Spawn(
96                                                 WP_Weapon,
97                                                 1, 0,
98                                                 world, e.origin + ('0 0 1' * e.maxs.z) * 1.2,
99                                                 self, 0,
100                                                 world, enemy,
101                                                 0,
102                                                 RADARICON_NONE
103                                         );
104                                         wp.wp_extra = wpn;
105                                 }
106                         }
107                 }
108                 else
109                 {
110                         Send_WeaponComplain (cl, wpn, 2);
111                 }
112
113                 play2(cl, SND(UNAVAILABLE));
114         }
115         return false;
116 }
117
118 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
119 {SELFPARAM();
120         // We cannot tokenize in this function, as GiveItems calls this
121         // function. Thus we must use car/cdr.
122         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
123         WepSet wepset = '0 0 0';
124         switchtonext = switchtolast = 0;
125         first_valid = prev_valid = 0;
126         float weaponcur;
127         entity wep;
128
129         if(skipmissing || pl.selectweapon == 0)
130                 weaponcur = pl.switchweapon;
131         else
132                 weaponcur = pl.selectweapon;
133
134         if(dir == 0)
135                 switchtonext = 1;
136
137         int c = 0;
138
139         string rest = weaponorder;
140         while(rest != "")
141         {
142                 weaponwant = stof(car(rest)); rest = cdr(rest);
143                 wep = get_weaponinfo(weaponwant);
144                 wepset = WepSet_FromWeapon(weaponwant);
145                 if(imp >= 0)
146                 if(wep.impulse != imp)
147                         continue;
148
149                 float i, have_other = false;
150                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
151                 {
152                         if(i != weaponwant)
153                         if((get_weaponinfo(i)).impulse == imp || imp < 0)
154                         if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
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 = WepSet_FromWeapon(weaponwant);
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                                 if(i != weaponwant)
210                                 if((get_weaponinfo(i)).impulse == imp || imp < 0)
211                                 if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
212                                         have_other = true;
213                         }
214
215                         // skip weapons we don't own that aren't normal and aren't in the map
216                         if(!(pl.weapons & wepset))
217                         if(!(weaponsInMap & wepset))
218                         if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
219                                 continue;
220
221                         --c;
222                         if(c == 0)
223                         {
224                                 client_hasweapon(pl, weaponwant, true, true);
225                                 break;
226                         }
227                 }
228         }
229         return 0;
230 }
231
232 void W_SwitchWeapon_Force(entity e, float w)
233 {
234         e.cnt = e.switchweapon;
235         e.switchweapon = w;
236         e.selectweapon = w;
237 }
238
239 // perform weapon to attack (weaponstate and attack_finished check is here)
240 void W_SwitchToOtherWeapon(entity pl)
241 {
242         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
243         float w, ww;
244         w = pl.weapon;
245         if(pl.weapons & WepSet_FromWeapon(w))
246         {
247                 pl.weapons &= ~WepSet_FromWeapon(w);
248                 ww = w_getbestweapon(pl);
249                 pl.weapons |= WepSet_FromWeapon(w);
250         }
251         else
252                 ww = w_getbestweapon(pl);
253         if(ww)
254                 W_SwitchWeapon_Force(pl, ww);
255 }
256
257 void W_SwitchWeapon(float imp)
258 {SELFPARAM();
259         if (self.switchweapon != imp)
260         {
261                 if (client_hasweapon(self, imp, true, true))
262                         W_SwitchWeapon_Force(self, imp);
263                 else
264                         self.selectweapon = imp; // update selectweapon ANYWAY
265         }
266         else if(!forbidWeaponUse(self)) {
267                 Weapon w = get_weaponinfo(self.weapon);
268                 w.wr_reload(w);
269         }
270 }
271
272 void W_CycleWeapon(string weaponorder, float dir)
273 {SELFPARAM();
274         float w;
275         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
276         if(w > 0)
277                 W_SwitchWeapon(w);
278 }
279
280 void W_NextWeaponOnImpulse(float imp)
281 {SELFPARAM();
282         float w;
283         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
284         if(w > 0)
285                 W_SwitchWeapon(w);
286 }
287
288 // next weapon
289 void W_NextWeapon(float list)
290 {SELFPARAM();
291         if(list == 0)
292                 W_CycleWeapon(weaponorder_byid, -1);
293         else if(list == 1)
294                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
295         else if(list == 2)
296                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
297 }
298
299 // prev weapon
300 void W_PreviousWeapon(float list)
301 {SELFPARAM();
302         if(list == 0)
303                 W_CycleWeapon(weaponorder_byid, +1);
304         else if(list == 1)
305                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
306         else if(list == 2)
307                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
308 }
309
310 // previously used if exists and has ammo, (second) best otherwise
311 void W_LastWeapon(void)
312 {SELFPARAM();
313         if(client_hasweapon(self, self.cnt, true, false))
314                 W_SwitchWeapon(self.cnt);
315         else
316                 W_SwitchToOtherWeapon(self);
317 }