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