]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/selection.qc
Split throwing and selection into their own files
[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 // perform weapon to attack (weaponstate and attack_finished check is here)
192 void W_SwitchToOtherWeapon(entity pl)
193 {
194         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
195         float w, ww;
196         w = pl.weapon;
197         if(WEPSET_CONTAINS_EW(pl, w))
198         {
199                 WEPSET_ANDNOT_EW(pl, w);
200                 ww = w_getbestweapon(pl);
201                 WEPSET_OR_EW(pl, w);
202         }
203         else
204                 ww = w_getbestweapon(pl);
205         if(ww)
206                 W_SwitchWeapon_Force(pl, ww);
207 }
208
209 void W_SwitchWeapon(float imp)
210 {
211         if (self.switchweapon != imp)
212         {
213                 if (client_hasweapon(self, imp, TRUE, TRUE))
214                         W_SwitchWeapon_Force(self, imp);
215                 else
216                         self.selectweapon = imp; // update selectweapon ANYWAY
217         }
218         else { WEP_ACTION(self.weapon, WR_RELOAD); }
219 }
220
221 void W_CycleWeapon(string weaponorder, float dir)
222 {
223         float w;
224         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
225         if(w > 0)
226                 W_SwitchWeapon(w);
227 }
228
229 void W_NextWeaponOnImpulse(float imp)
230 {
231         float w;
232         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
233         if(w > 0)
234                 W_SwitchWeapon(w);
235 }
236
237 // next weapon
238 void W_NextWeapon(float list)
239 {
240         if(list == 0)
241                 W_CycleWeapon(weaponorder_byid, -1);
242         else if(list == 1)
243                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
244         else if(list == 2)
245                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
246 }
247
248 // prev weapon
249 void W_PreviousWeapon(float list)
250 {
251         if(list == 0)
252                 W_CycleWeapon(weaponorder_byid, +1);
253         else if(list == 1)
254                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
255         else if(list == 2)
256                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
257 }
258
259 // previously used if exists and has ammo, (second) best otherwise
260 void W_LastWeapon(void)
261 {
262         if(client_hasweapon(self, self.cnt, TRUE, FALSE))
263                 W_SwitchWeapon(self.cnt);
264         else
265                 W_SwitchToOtherWeapon(self);
266 }