]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Combine client/server waypointsprites
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / selection.qc
1 #include "selection.qh"
2 #include "../_all.qh"
3
4 #include "weaponsystem.qh"
5 #include "../t_items.qh"
6 #include "../../common/constants.qh"
7 #include "../../common/util.qh"
8 #include "../../common/weapons/all.qh"
9
10 // switch between weapons
11 void Send_WeaponComplain(entity e, float wpn, float type)
12 {
13         msg_entity = e;
14         WriteByte(MSG_ONE, SVC_TEMPENTITY);
15         WriteByte(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 {
22         float f;
23         entity oldself;
24
25         if(time < self.hasweapon_complain_spam)
26                 complain = 0;
27
28         if(wpn == WEP_HOOK.m_id && !g_grappling_hook && autocvar_g_nades && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
29                 complain = 0;
30
31         if(complain)
32                 self.hasweapon_complain_spam = time + 0.2;
33
34         if (wpn < WEP_FIRST || wpn > WEP_LAST)
35         {
36                 if (complain)
37                         sprint(self, "Invalid weapon\n");
38                 return false;
39         }
40         if (cl.weapons & WepSet_FromWeapon(wpn))
41         {
42                 if (andammo)
43                 {
44                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
45                         {
46                                 f = 1;
47                         }
48                         else
49                         {
50                                 oldself = self;
51                                 self = cl;
52                                 f = WEP_ACTION(wpn, WR_CHECKAMMO1);
53                                 f = f + WEP_ACTION(wpn, WR_CHECKAMMO2);
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                                 self = oldself;
62                         }
63                         if (!f)
64                         {
65                                 if (complain)
66                                 if(IS_REAL_CLIENT(cl))
67                                 {
68                                         play2(cl, "weapons/unavailable.wav");
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                                         WaypointSprite_Spawn(
95                                                 (get_weaponinfo(wpn)).wpmodel,
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, '0 0 0'
102                                         );
103                                 }
104                         }
105                 }
106                 else
107                 {
108                         Send_WeaponComplain (cl, wpn, 2);
109                 }
110
111                 play2(cl, "weapons/unavailable.wav");
112         }
113         return false;
114 }
115
116 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
117 {
118         // We cannot tokenize in this function, as GiveItems calls this
119         // function. Thus we must use car/cdr.
120         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
121         WepSet wepset = '0 0 0';
122         switchtonext = switchtolast = 0;
123         first_valid = prev_valid = 0;
124         float weaponcur;
125         entity wep;
126
127         if(skipmissing || pl.selectweapon == 0)
128                 weaponcur = pl.switchweapon;
129         else
130                 weaponcur = pl.selectweapon;
131
132         if(dir == 0)
133                 switchtonext = 1;
134
135         int c = 0;
136
137         string rest = weaponorder;
138         while(rest != "")
139         {
140                 weaponwant = stof(car(rest)); rest = cdr(rest);
141                 wep = get_weaponinfo(weaponwant);
142                 wepset = WepSet_FromWeapon(weaponwant);
143                 if(imp >= 0)
144                 if(wep.impulse != imp)
145                         continue;
146
147                 float i, have_other = false;
148                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
149                 {
150                         if(i != weaponwant)
151                         if((get_weaponinfo(i)).impulse == imp || imp < 0)
152                         if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
153                                 have_other = true;
154                 }
155
156                 // skip weapons we don't own that aren't normal and aren't in the map
157                 if(!(pl.weapons & wepset))
158                 if(!(weaponsInMap & wepset))
159                 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
160                         continue;
161
162                 ++c;
163
164                 if(!skipmissing || client_hasweapon(pl, weaponwant, true, false))
165                 {
166                         if(switchtonext)
167                                 return weaponwant;
168                         if(!first_valid)
169                                 first_valid = weaponwant;
170                         if(weaponwant == weaponcur)
171                         {
172                                 if(dir >= 0)
173                                         switchtonext = 1;
174                                 else if(prev_valid)
175                                         return prev_valid;
176                                 else
177                                         switchtolast = 1;
178                         }
179                         prev_valid = weaponwant;
180                 }
181         }
182         if(first_valid)
183         {
184                 if(switchtolast)
185                         return prev_valid;
186                 else
187                         return first_valid;
188         }
189         // complain (but only for one weapon on the button that has been pressed)
190         if(complain)
191         {
192                 self.weaponcomplainindex += 1;
193                 c = (self.weaponcomplainindex % c) + 1;
194                 rest = weaponorder;
195                 while(rest != "")
196                 {
197                         weaponwant = stof(car(rest)); rest = cdr(rest);
198                         wep = get_weaponinfo(weaponwant);
199                         wepset = WepSet_FromWeapon(weaponwant);
200                         if(imp >= 0)
201                                 if(wep.impulse != imp)
202                                         continue;
203
204                         float i, have_other = false;
205                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
206                         {
207                                 if(i != weaponwant)
208                                 if((get_weaponinfo(i)).impulse == imp || imp < 0)
209                                 if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
210                                         have_other = true;
211                         }
212
213                         // skip weapons we don't own that aren't normal and aren't in the map
214                         if(!(pl.weapons & wepset))
215                         if(!(weaponsInMap & wepset))
216                         if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
217                                 continue;
218
219                         --c;
220                         if(c == 0)
221                         {
222                                 client_hasweapon(pl, weaponwant, true, true);
223                                 break;
224                         }
225                 }
226         }
227         return 0;
228 }
229
230 void W_SwitchWeapon_Force(entity e, float w)
231 {
232         e.cnt = e.switchweapon;
233         e.switchweapon = w;
234         e.selectweapon = w;
235 }
236
237 // perform weapon to attack (weaponstate and attack_finished check is here)
238 void W_SwitchToOtherWeapon(entity pl)
239 {
240         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
241         float w, ww;
242         w = pl.weapon;
243         if(pl.weapons & WepSet_FromWeapon(w))
244         {
245                 pl.weapons &= ~WepSet_FromWeapon(w);
246                 ww = w_getbestweapon(pl);
247                 pl.weapons |= WepSet_FromWeapon(w);
248         }
249         else
250                 ww = w_getbestweapon(pl);
251         if(ww)
252                 W_SwitchWeapon_Force(pl, ww);
253 }
254
255 void W_SwitchWeapon(float imp)
256 {
257         if (self.switchweapon != imp)
258         {
259                 if (client_hasweapon(self, imp, true, true))
260                         W_SwitchWeapon_Force(self, imp);
261                 else
262                         self.selectweapon = imp; // update selectweapon ANYWAY
263         }
264         else if(!forbidWeaponUse(self)) { WEP_ACTION(self.weapon, WR_RELOAD); }
265 }
266
267 void W_CycleWeapon(string weaponorder, float dir)
268 {
269         float w;
270         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
271         if(w > 0)
272                 W_SwitchWeapon(w);
273 }
274
275 void W_NextWeaponOnImpulse(float imp)
276 {
277         float w;
278         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
279         if(w > 0)
280                 W_SwitchWeapon(w);
281 }
282
283 // next weapon
284 void W_NextWeapon(float list)
285 {
286         if(list == 0)
287                 W_CycleWeapon(weaponorder_byid, -1);
288         else if(list == 1)
289                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
290         else if(list == 2)
291                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
292 }
293
294 // prev weapon
295 void W_PreviousWeapon(float list)
296 {
297         if(list == 0)
298                 W_CycleWeapon(weaponorder_byid, +1);
299         else if(list == 1)
300                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
301         else if(list == 2)
302                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
303 }
304
305 // previously used if exists and has ammo, (second) best otherwise
306 void W_LastWeapon(void)
307 {
308         if(client_hasweapon(self, self.cnt, true, false))
309                 W_SwitchWeapon(self.cnt);
310         else
311                 W_SwitchToOtherWeapon(self);
312 }