]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Move some more things to selection.qc and out of weaponsystem.qc
[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 hasweapon_complain_spam;
13 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
14 {
15         float f;
16         entity oldself;
17
18         if(time < self.hasweapon_complain_spam)
19                 complain = 0;
20         if(complain)
21                 self.hasweapon_complain_spam = time + 0.2;
22
23         if (wpn < WEP_FIRST || wpn > WEP_LAST)
24         {
25                 if (complain)
26                         sprint(self, "Invalid weapon\n");
27                 return FALSE;
28         }
29         if (WEPSET_CONTAINS_EW(cl, wpn))
30         {
31                 if (andammo)
32                 {
33                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
34                         {
35                                 f = 1;
36                         }
37                         else
38                         {
39                                 oldself = self;
40                                 self = cl;
41                                 f = WEP_ACTION(wpn, WR_CHECKAMMO1);
42                                 f = f + WEP_ACTION(wpn, WR_CHECKAMMO2);
43
44                                 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
45                                 entity mine;
46                                 if(wpn == WEP_MINE_LAYER)
47                                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
48                                         f = 1;
49
50                                 self = oldself;
51                         }
52                         if (!f)
53                         {
54                                 if (complain)
55                                 if(IS_REAL_CLIENT(cl))
56                                 {
57                                         play2(cl, "weapons/unavailable.wav");
58                                         Send_WeaponComplain (cl, wpn, W_Name(wpn), 0);
59                                 }
60                                 return FALSE;
61                         }
62                 }
63                 return TRUE;
64         }
65         if (complain)
66         {
67                 // DRESK - 3/16/07
68                 // Report Proper Weapon Status / Modified Weapon Ownership Message
69                 if (WEPSET_CONTAINS_AW(weaponsInMap, wpn))
70                 {
71                         Send_WeaponComplain(cl, wpn, W_Name(wpn), 1);
72
73                         if(autocvar_g_showweaponspawns)
74                         {
75                                 entity e;
76                                 string s;
77
78                                 e = get_weaponinfo(wpn);
79                                 s = e.model2;
80
81                                 for(e = world; (e = findfloat(e, weapon, wpn)); )
82                                 {
83                                         if(e.classname == "droppedweapon")
84                                                 continue;
85                                         if not(e.flags & FL_ITEM)
86                                                 continue;
87                                         WaypointSprite_Spawn(
88                                                 s,
89                                                 1, 0,
90                                                 world, e.origin,
91                                                 self, 0,
92                                                 world, enemy,
93                                                 0,
94                                                 RADARICON_NONE, '0 0 0'
95                                         );
96                                 }
97                         }
98                 }
99                 else
100                 {
101                         Send_WeaponComplain (cl, wpn, W_Name(wpn), 2);
102                 }
103
104                 play2(cl, "weapons/unavailable.wav");
105         }
106         return FALSE;
107 }
108
109 .float weaponcomplainindex;
110 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
111 {
112         // We cannot tokenize in this function, as GiveItems calls this
113         // function. Thus we must use car/cdr.
114         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
115         string rest;
116         switchtonext = switchtolast = 0;
117         first_valid = prev_valid = 0;
118         float weaponcur;
119
120         if(skipmissing || pl.selectweapon == 0)
121                 weaponcur = pl.switchweapon;
122         else
123                 weaponcur = pl.selectweapon;
124
125         if(dir == 0)
126                 switchtonext = 1;
127
128         c = 0;
129
130         rest = weaponorder;
131         while(rest != "")
132         {
133                 weaponwant = stof(car(rest)); rest = cdr(rest);
134                 if(imp >= 0)
135                         if((get_weaponinfo(weaponwant)).impulse != imp)
136                                 continue;
137
138                 ++c;
139
140                 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
141                 {
142                         if(switchtonext)
143                                 return weaponwant;
144                         if(!first_valid)
145                                 first_valid = weaponwant;
146                         if(weaponwant == weaponcur)
147                         {
148                                 if(dir >= 0)
149                                         switchtonext = 1;
150                                 else if(prev_valid)
151                                         return prev_valid;
152                                 else
153                                         switchtolast = 1;
154                         }
155                         prev_valid = weaponwant;
156                 }
157         }
158         if(first_valid)
159         {
160                 if(switchtolast)
161                         return prev_valid;
162                 else
163                         return first_valid;
164         }
165         // complain (but only for one weapon on the button that has been pressed)
166         if(complain)
167         {
168                 self.weaponcomplainindex += 1;
169                 c = mod(self.weaponcomplainindex, c) + 1;
170                 rest = weaponorder;
171                 while(rest != "")
172                 {
173                         weaponwant = stof(car(rest)); rest = cdr(rest);
174                         if(imp >= 0)
175                                 if((get_weaponinfo(weaponwant)).impulse != imp)
176                                         continue;
177
178                         --c;
179                         if(c == 0)
180                         {
181                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
182                                 break;
183                         }
184                 }
185         }
186         return 0;
187 }
188
189 #define w_getbestweapon(ent) W_GetCycleWeapon(ent, ent.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE)
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(WEPSET_CONTAINS_EW(pl, w))
205         {
206                 WEPSET_ANDNOT_EW(pl, w);
207                 ww = w_getbestweapon(pl);
208                 WEPSET_OR_EW(pl, 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 }