]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Merge remote-tracking branch 'origin/master' 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, 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")
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,
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         switchtonext = switchtolast = 0;
114         first_valid = prev_valid = 0;
115         float weaponcur;
116         entity wep;
117
118         if(skipmissing || pl.selectweapon == 0)
119                 weaponcur = pl.switchweapon;
120         else
121                 weaponcur = pl.selectweapon;
122
123         if(dir == 0)
124                 switchtonext = 1;
125
126         c = 0;
127
128         rest = weaponorder;
129         while(rest != "")
130         {
131                 weaponwant = stof(car(rest)); rest = cdr(rest);
132                 wep = get_weaponinfo(weaponwant);
133                 if(imp >= 0)
134                         if(wep.impulse != imp)
135                                 continue;
136
137                 // skip weapons we don't own that aren't normal and aren't in the map
138                 if(!(pl.weapons & WepSet_FromWeapon(weaponwant)))
139                         if(!((get_weaponinfo(weaponwant)).spawnflags & WEP_FLAG_NORMAL))
140                                 if(!(weaponsInMap & WepSet_FromWeapon(weaponwant)))
141                                         continue;
142
143                 ++c;
144
145                 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
146                 {
147                         if(switchtonext)
148                                 return weaponwant;
149                         if(!first_valid)
150                                 first_valid = weaponwant;
151                         if(weaponwant == weaponcur)
152                         {
153                                 if(dir >= 0)
154                                         switchtonext = 1;
155                                 else if(prev_valid)
156                                         return prev_valid;
157                                 else
158                                         switchtolast = 1;
159                         }
160                         prev_valid = weaponwant;
161                 }
162         }
163         if(first_valid)
164         {
165                 if(switchtolast)
166                         return prev_valid;
167                 else
168                         return first_valid;
169         }
170         // complain (but only for one weapon on the button that has been pressed)
171         if(complain)
172         {
173                 self.weaponcomplainindex += 1;
174                 c = mod(self.weaponcomplainindex, c) + 1;
175                 rest = weaponorder;
176                 while(rest != "")
177                 {
178                         weaponwant = stof(car(rest)); rest = cdr(rest);
179                         wep = get_weaponinfo(weaponwant);
180                         if(imp >= 0)
181                                 if(wep.impulse != imp)
182                                         continue;
183
184                         // skip weapons we don't own that aren't normal and aren't in the map
185                         if(!(pl.weapons & WepSet_FromWeapon(weaponwant)))
186                                 if(!((get_weaponinfo(weaponwant)).spawnflags & WEP_FLAG_NORMAL))
187                                         if(!(weaponsInMap & WepSet_FromWeapon(weaponwant)))
188                                                 continue;
189
190                         --c;
191                         if(c == 0)
192                         {
193                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
194                                 break;
195                         }
196                 }
197         }
198         return 0;
199 }
200
201 void W_SwitchWeapon_Force(entity e, float w)
202 {
203         e.cnt = e.switchweapon;
204         e.switchweapon = w;
205         e.selectweapon = w;
206 }
207
208 // perform weapon to attack (weaponstate and attack_finished check is here)
209 void W_SwitchToOtherWeapon(entity pl)
210 {
211         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
212         float w, ww;
213         w = pl.weapon;
214         if(pl.weapons & WepSet_FromWeapon(w))
215         {
216                 pl.weapons &= ~WepSet_FromWeapon(w);
217                 ww = w_getbestweapon(pl);
218                 pl.weapons |= WepSet_FromWeapon(w);
219         }
220         else
221                 ww = w_getbestweapon(pl);
222         if(ww)
223                 W_SwitchWeapon_Force(pl, ww);
224 }
225
226 void W_SwitchWeapon(float imp)
227 {
228         if (self.switchweapon != imp)
229         {
230                 if (client_hasweapon(self, imp, TRUE, TRUE))
231                         W_SwitchWeapon_Force(self, imp);
232                 else
233                         self.selectweapon = imp; // update selectweapon ANYWAY
234         }
235         else { WEP_ACTION(self.weapon, WR_RELOAD); }
236 }
237
238 void W_CycleWeapon(string weaponorder, float dir)
239 {
240         float w;
241         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
242         if(w > 0)
243                 W_SwitchWeapon(w);
244 }
245
246 void W_NextWeaponOnImpulse(float imp)
247 {
248         float w;
249         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
250         if(w > 0)
251                 W_SwitchWeapon(w);
252 }
253
254 // next weapon
255 void W_NextWeapon(float list)
256 {
257         if(list == 0)
258                 W_CycleWeapon(weaponorder_byid, -1);
259         else if(list == 1)
260                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
261         else if(list == 2)
262                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
263 }
264
265 // prev weapon
266 void W_PreviousWeapon(float list)
267 {
268         if(list == 0)
269                 W_CycleWeapon(weaponorder_byid, +1);
270         else if(list == 1)
271                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
272         else if(list == 2)
273                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
274 }
275
276 // previously used if exists and has ammo, (second) best otherwise
277 void W_LastWeapon(void)
278 {
279         if(client_hasweapon(self, self.cnt, TRUE, FALSE))
280                 W_SwitchWeapon(self.cnt);
281         else
282                 W_SwitchToOtherWeapon(self);
283 }