]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Retrieve weapon name from client, rather than networking it
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / selection.qc
1 // switch between weapons
2 void Send_WeaponComplain(entity e, float wpn, float type)
3 {
4         msg_entity = e;
5         WriteByte(MSG_ONE, SVC_TEMPENTITY);
6         WriteByte(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
7         WriteByte(MSG_ONE, wpn);
8         WriteByte(MSG_ONE, type);
9 }
10
11 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
12 {
13         float f;
14         entity oldself;
15
16         if(time < self.hasweapon_complain_spam)
17                 complain = 0;
18
19         if(wpn == WEP_HOOK && !g_grappling_hook && autocvar_g_nades && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
20                 complain = 0;
21                 
22         if(complain)
23                 self.hasweapon_complain_spam = time + 0.2;
24
25         if (wpn < WEP_FIRST || wpn > WEP_LAST)
26         {
27                 if (complain)
28                         sprint(self, "Invalid weapon\n");
29                 return FALSE;
30         }
31         if (cl.weapons & WepSet_FromWeapon(wpn))
32         {
33                 if (andammo)
34                 {
35                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
36                         {
37                                 f = 1;
38                         }
39                         else
40                         {
41                                 oldself = self;
42                                 self = cl;
43                                 f = WEP_ACTION(wpn, WR_CHECKAMMO1);
44                                 f = f + WEP_ACTION(wpn, WR_CHECKAMMO2);
45
46                                 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
47                                 entity mine;
48                                 if(wpn == WEP_MINE_LAYER)
49                                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
50                                         f = 1;
51
52                                 self = oldself;
53                         }
54                         if (!f)
55                         {
56                                 if (complain)
57                                 if(IS_REAL_CLIENT(cl))
58                                 {
59                                         play2(cl, "weapons/unavailable.wav");
60                                         Send_WeaponComplain (cl, wpn, 0);
61                                 }
62                                 return FALSE;
63                         }
64                 }
65                 return TRUE;
66         }
67         if (complain)
68         {
69                 // DRESK - 3/16/07
70                 // Report Proper Weapon Status / Modified Weapon Ownership Message
71                 if (weaponsInMap & WepSet_FromWeapon(wpn))
72                 {
73                         Send_WeaponComplain(cl, wpn, 1);
74
75                         if(autocvar_g_showweaponspawns)
76                         {
77                                 entity e;
78                                 string s;
79
80                                 e = get_weaponinfo(wpn);
81                                 s = e.model2;
82
83                                 for(e = world; (e = findfloat(e, weapon, wpn)); )
84                                 {
85                                         if(e.classname == "droppedweapon")
86                                                 continue;
87                                         if(!(e.flags & FL_ITEM))
88                                                 continue;
89                                         WaypointSprite_Spawn(
90                                                 s,
91                                                 1, 0,
92                                                 world, e.origin,
93                                                 self, 0,
94                                                 world, enemy,
95                                                 0,
96                                                 RADARICON_NONE, '0 0 0'
97                                         );
98                                 }
99                         }
100                 }
101                 else
102                 {
103                         Send_WeaponComplain (cl, wpn, 2);
104                 }
105
106                 play2(cl, "weapons/unavailable.wav");
107         }
108         return FALSE;
109 }
110
111 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
112 {
113         // We cannot tokenize in this function, as GiveItems calls this
114         // function. Thus we must use car/cdr.
115         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
116         string rest;
117         switchtonext = switchtolast = 0;
118         first_valid = prev_valid = 0;
119         float weaponcur;
120         entity wep;
121
122         if(skipmissing || pl.selectweapon == 0)
123                 weaponcur = pl.switchweapon;
124         else
125                 weaponcur = pl.selectweapon;
126
127         if(dir == 0)
128                 switchtonext = 1;
129
130         c = 0;
131
132         rest = weaponorder;
133         while(rest != "")
134         {
135                 weaponwant = stof(car(rest)); rest = cdr(rest);
136                 wep = get_weaponinfo(weaponwant);
137                 if(imp >= 0)
138                         if(wep.impulse != imp)
139                                 continue;
140
141                 // skip weapons we don't own that aren't normal and aren't in the map
142                 if(!(pl.weapons & WepSet_FromWeapon(weaponwant)))
143                         if(!((get_weaponinfo(weaponwant)).spawnflags & WEP_FLAG_NORMAL))
144                                 if(!(weaponsInMap & WepSet_FromWeapon(weaponwant)))
145                                         continue;
146
147                 ++c;
148
149                 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
150                 {
151                         if(switchtonext)
152                                 return weaponwant;
153                         if(!first_valid)
154                                 first_valid = weaponwant;
155                         if(weaponwant == weaponcur)
156                         {
157                                 if(dir >= 0)
158                                         switchtonext = 1;
159                                 else if(prev_valid)
160                                         return prev_valid;
161                                 else
162                                         switchtolast = 1;
163                         }
164                         prev_valid = weaponwant;
165                 }
166         }
167         if(first_valid)
168         {
169                 if(switchtolast)
170                         return prev_valid;
171                 else
172                         return first_valid;
173         }
174         // complain (but only for one weapon on the button that has been pressed)
175         if(complain)
176         {
177                 self.weaponcomplainindex += 1;
178                 c = mod(self.weaponcomplainindex, c) + 1;
179                 rest = weaponorder;
180                 while(rest != "")
181                 {
182                         weaponwant = stof(car(rest)); rest = cdr(rest);
183                         wep = get_weaponinfo(weaponwant);
184                         if(imp >= 0)
185                                 if(wep.impulse != imp)
186                                         continue;
187
188                         // skip weapons we don't own that aren't normal and aren't in the map
189                         if(!(pl.weapons & WepSet_FromWeapon(weaponwant)))
190                                 if(!((get_weaponinfo(weaponwant)).spawnflags & WEP_FLAG_NORMAL))
191                                         if(!(weaponsInMap & WepSet_FromWeapon(weaponwant)))
192                                                 continue;
193
194                         --c;
195                         if(c == 0)
196                         {
197                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
198                                 break;
199                         }
200                 }
201         }
202         return 0;
203 }
204
205 void W_SwitchWeapon_Force(entity e, float w)
206 {
207         e.cnt = e.switchweapon;
208         e.switchweapon = w;
209         e.selectweapon = w;
210 }
211
212 // perform weapon to attack (weaponstate and attack_finished check is here)
213 void W_SwitchToOtherWeapon(entity pl)
214 {
215         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
216         float w, ww;
217         w = pl.weapon;
218         if(pl.weapons & WepSet_FromWeapon(w))
219         {
220                 pl.weapons &= ~WepSet_FromWeapon(w);
221                 ww = w_getbestweapon(pl);
222                 pl.weapons |= WepSet_FromWeapon(w);
223         }
224         else
225                 ww = w_getbestweapon(pl);
226         if(ww)
227                 W_SwitchWeapon_Force(pl, ww);
228 }
229
230 void W_SwitchWeapon(float imp)
231 {
232         if (self.switchweapon != imp)
233         {
234                 if (client_hasweapon(self, imp, TRUE, TRUE))
235                         W_SwitchWeapon_Force(self, imp);
236                 else
237                         self.selectweapon = imp; // update selectweapon ANYWAY
238         }
239         else { WEP_ACTION(self.weapon, WR_RELOAD); }
240 }
241
242 void W_CycleWeapon(string weaponorder, float dir)
243 {
244         float w;
245         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
246         if(w > 0)
247                 W_SwitchWeapon(w);
248 }
249
250 void W_NextWeaponOnImpulse(float imp)
251 {
252         float w;
253         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
254         if(w > 0)
255                 W_SwitchWeapon(w);
256 }
257
258 // next weapon
259 void W_NextWeapon(float list)
260 {
261         if(list == 0)
262                 W_CycleWeapon(weaponorder_byid, -1);
263         else if(list == 1)
264                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
265         else if(list == 2)
266                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
267 }
268
269 // prev weapon
270 void W_PreviousWeapon(float list)
271 {
272         if(list == 0)
273                 W_CycleWeapon(weaponorder_byid, +1);
274         else if(list == 1)
275                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
276         else if(list == 2)
277                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
278 }
279
280 // previously used if exists and has ammo, (second) best otherwise
281 void W_LastWeapon(void)
282 {
283         if(client_hasweapon(self, self.cnt, TRUE, FALSE))
284                 W_SwitchWeapon(self.cnt);
285         else
286                 W_SwitchToOtherWeapon(self);
287 }