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