]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Merge branch 'master' into Mario/weapons_new
[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
79                                 for(e = world; (e = findfloat(e, weapon, wpn)); )
80                                 {
81                                         if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
82                                                 continue;
83                                         if(!(e.flags & FL_ITEM))
84                                                 continue;
85                                         WaypointSprite_Spawn(
86                                                 (get_weaponinfo(wpn)).wpmodel,
87                                                 1, 0,
88                                                 world, e.origin + ('0 0 1' * e.maxs_z) * 1.2,
89                                                 self, 0,
90                                                 world, enemy,
91                                                 0,
92                                                 RADARICON_NONE, '0 0 0'
93                                         );
94                                 }
95                         }
96                 }
97                 else
98                 {
99                         Send_WeaponComplain (cl, wpn, 2);
100                 }
101
102                 play2(cl, "weapons/unavailable.wav");
103         }
104         return FALSE;
105 }
106
107 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
108 {
109         // We cannot tokenize in this function, as GiveItems calls this
110         // function. Thus we must use car/cdr.
111         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
112         string rest;
113         WepSet wepset = '0 0 0';
114         switchtonext = switchtolast = 0;
115         first_valid = prev_valid = 0;
116         float weaponcur;
117         entity wep;
118
119         if(skipmissing || pl.selectweapon == 0)
120                 weaponcur = pl.switchweapon;
121         else
122                 weaponcur = pl.selectweapon;
123
124         if(dir == 0)
125                 switchtonext = 1;
126
127         c = 0;
128
129         rest = weaponorder;
130         while(rest != "")
131         {
132                 weaponwant = stof(car(rest)); rest = cdr(rest);
133                 wep = get_weaponinfo(weaponwant);
134                 wepset = WepSet_FromWeapon(weaponwant);
135                 if(imp >= 0)
136                 if(wep.impulse != imp)
137                         continue;
138
139                 float i, have_other = FALSE;
140                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
141                 {
142                         if(i != weaponwant)
143                         if((get_weaponinfo(i)).impulse == imp || imp < 0)
144                         if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
145                                 have_other = TRUE;
146                 }
147
148                 // skip weapons we don't own that aren't normal and aren't in the map
149                 if(!(pl.weapons & wepset))
150                 if(!(weaponsInMap & wepset))
151                 if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
152                         continue;
153
154                 ++c;
155
156                 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
157                 {
158                         if(switchtonext)
159                                 return weaponwant;
160                         if(!first_valid)
161                                 first_valid = weaponwant;
162                         if(weaponwant == weaponcur)
163                         {
164                                 if(dir >= 0)
165                                         switchtonext = 1;
166                                 else if(prev_valid)
167                                         return prev_valid;
168                                 else
169                                         switchtolast = 1;
170                         }
171                         prev_valid = weaponwant;
172                 }
173         }
174         if(first_valid)
175         {
176                 if(switchtolast)
177                         return prev_valid;
178                 else
179                         return first_valid;
180         }
181         // complain (but only for one weapon on the button that has been pressed)
182         if(complain)
183         {
184                 self.weaponcomplainindex += 1;
185                 c = mod(self.weaponcomplainindex, c) + 1;
186                 rest = weaponorder;
187                 while(rest != "")
188                 {
189                         weaponwant = stof(car(rest)); rest = cdr(rest);
190                         wep = get_weaponinfo(weaponwant);
191                         wepset = WepSet_FromWeapon(weaponwant);
192                         if(imp >= 0)
193                                 if(wep.impulse != imp)
194                                         continue;
195
196                         float i, have_other = FALSE;
197                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
198                         {
199                                 if(i != weaponwant)
200                                 if((get_weaponinfo(i)).impulse == imp || imp < 0)
201                                 if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
202                                         have_other = TRUE;
203                         }
204
205                         // skip weapons we don't own that aren't normal and aren't in the map
206                         if(!(pl.weapons & wepset))
207                         if(!(weaponsInMap & wepset))
208                         if((wep.spawnflags & WEP_FLAG_MUTATORBLOCKED) || have_other)
209                                 continue;
210
211                         --c;
212                         if(c == 0)
213                         {
214                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
215                                 break;
216                         }
217                 }
218         }
219         return 0;
220 }
221
222 void W_SwitchWeapon_Force(entity e, float w)
223 {
224         e.cnt = e.switchweapon;
225         e.switchweapon = w;
226         e.selectweapon = w;
227 }
228
229 // perform weapon to attack (weaponstate and attack_finished check is here)
230 void W_SwitchToOtherWeapon(entity pl)
231 {
232         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
233         float w, ww;
234         w = pl.weapon;
235         if(pl.weapons & WepSet_FromWeapon(w))
236         {
237                 pl.weapons &= ~WepSet_FromWeapon(w);
238                 ww = w_getbestweapon(pl);
239                 pl.weapons |= WepSet_FromWeapon(w);
240         }
241         else
242                 ww = w_getbestweapon(pl);
243         if(ww)
244                 W_SwitchWeapon_Force(pl, ww);
245 }
246
247 void W_SwitchWeapon(float imp)
248 {
249         if (self.switchweapon != imp)
250         {
251                 if (client_hasweapon(self, imp, TRUE, TRUE))
252                         W_SwitchWeapon_Force(self, imp);
253                 else
254                         self.selectweapon = imp; // update selectweapon ANYWAY
255         }
256         else if(!forbidWeaponUse()) { WEP_ACTION(self.weapon, WR_RELOAD); }
257 }
258
259 void W_CycleWeapon(string weaponorder, float dir)
260 {
261         float w;
262         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
263         if(w > 0)
264                 W_SwitchWeapon(w);
265 }
266
267 void W_NextWeaponOnImpulse(float imp)
268 {
269         float w;
270         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
271         if(w > 0)
272                 W_SwitchWeapon(w);
273 }
274
275 // next weapon
276 void W_NextWeapon(float list)
277 {
278         if(list == 0)
279                 W_CycleWeapon(weaponorder_byid, -1);
280         else if(list == 1)
281                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
282         else if(list == 2)
283                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
284 }
285
286 // prev weapon
287 void W_PreviousWeapon(float list)
288 {
289         if(list == 0)
290                 W_CycleWeapon(weaponorder_byid, +1);
291         else if(list == 1)
292                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
293         else if(list == 2)
294                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
295 }
296
297 // previously used if exists and has ammo, (second) best otherwise
298 void W_LastWeapon(void)
299 {
300         if(client_hasweapon(self, self.cnt, TRUE, FALSE))
301                 W_SwitchWeapon(self.cnt);
302         else
303                 W_SwitchToOtherWeapon(self);
304 }