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