]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_weapons.qc
Merge remote branch 'origin/divVerent/menu-imprOOvements' into fruitiex/newpanelhud_s...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_weapons.qc
1 void W_Reload()
2 {
3         if(self.switchweapon == self.weapon)
4         if(self.weaponentity.state == WS_READY)
5                 weapon_action(self.weapon, WR_RELOAD);
6 }
7
8 // switch between weapons
9 void W_SwitchWeapon(float imp)
10 {
11         if (self.switchweapon != imp)
12         {
13                 if (client_hasweapon(self, imp, TRUE, TRUE))
14                         W_SwitchWeapon_Force(self, imp);
15                 else
16                         self.selectweapon = imp; // update selectweapon ANYWAY
17         }
18         else
19         {
20                 W_Reload();
21         }
22 };
23
24 .float weaponcomplainindex;
25 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
26 {
27         // We cannot tokenize in this function, as GiveItems calls this
28         // function. Thus we must use car/cdr.
29         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
30         string rest;
31         switchtonext = switchtolast = 0;
32         first_valid = prev_valid = 0;
33         float weaponcur;
34
35         if(skipmissing || pl.selectweapon == 0)
36                 weaponcur = pl.switchweapon;
37         else
38                 weaponcur = pl.selectweapon;
39
40         if(dir == 0)
41                 switchtonext = 1;
42
43         c = 0;
44
45         rest = weaponorder;
46         while(rest != "")
47         {
48                 weaponwant = stof(car(rest)); rest = cdr(rest);
49                 if(imp >= 0)
50                         if((get_weaponinfo(weaponwant)).impulse != imp)
51                                 continue;
52
53                 ++c;
54
55                 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
56                 {
57                         if(switchtonext)
58                                 return weaponwant;
59                         if(!first_valid)
60                                 first_valid = weaponwant;
61                         if(weaponwant == weaponcur)
62                         {
63                                 if(dir >= 0)
64                                         switchtonext = 1;
65                                 else if(prev_valid)
66                                         return prev_valid;
67                                 else
68                                         switchtolast = 1;
69                         }
70                         prev_valid = weaponwant;
71                 }
72         }
73         if(first_valid)
74         {
75                 if(switchtolast)
76                         return prev_valid;
77                 else
78                         return first_valid;
79         }
80         // complain (but only for one weapon on the button that has been pressed)
81         if(complain)
82         {
83                 self.weaponcomplainindex += 1;
84                 c = mod(self.weaponcomplainindex, c) + 1;
85                 rest = weaponorder;
86                 while(rest != "")
87                 {
88                         weaponwant = stof(car(rest)); rest = cdr(rest);
89                         if(imp >= 0)
90                                 if((get_weaponinfo(weaponwant)).impulse != imp)
91                                         continue;
92
93                         --c;
94                         if(c == 0)
95                         {
96                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
97                                 break;
98                         }
99                 }
100         }
101         return 0;
102 }
103
104 void W_CycleWeapon(string weaponorder, float dir)
105 {
106         float w;
107         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
108         if(w > 0)
109                 W_SwitchWeapon(w);
110 }
111
112 void W_NextWeaponOnImpulse(float imp)
113 {
114         float w;
115         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
116         if(w > 0)
117                 W_SwitchWeapon(w);
118 }
119
120 // next weapon
121 void W_NextWeapon(float list)
122 {
123         if(list == 0)
124                 W_CycleWeapon(weaponorder_byid, -1);
125         else if(list == 1)
126                 W_CycleWeapon(weaponorder_byimpulse, -1);
127         else if(list == 2)
128                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
129 }
130
131 // prev weapon
132 void W_PreviousWeapon(float list)
133 {
134         if(list == 0)
135                 W_CycleWeapon(weaponorder_byid, +1);
136         else if(list == 1)
137                 W_CycleWeapon(weaponorder_byimpulse, +1);
138         else if(list == 2)
139                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
140 }
141
142 string W_FixWeaponOrder_AllowIncomplete(string order)
143 {
144         return W_FixWeaponOrder(order, 0);
145 }
146
147 string W_FixWeaponOrder_ForceComplete(string order)
148 {
149         if(order == "")
150                 order = W_NumberWeaponOrder(cvar_string("cl_weaponpriority"));
151         return W_FixWeaponOrder(order, 1);
152 }
153
154 float w_getbestweapon(entity e)
155 {
156         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE);
157 };
158
159 // generic weapons table
160 // TODO should they be macros instead?
161 float weapon_action(float wpn, float wrequest)
162 {
163         return (get_weaponinfo(wpn)).weapon_func(wrequest);
164 };
165
166 string W_Name(float weaponid)
167 {
168         return (get_weaponinfo(weaponid)).message;
169 }
170
171 float W_WeaponBit(float wpn)
172 {
173         return (get_weaponinfo(wpn)).weapons;
174 }
175
176 float W_AmmoItemCode(float wpn)
177 {
178         return (get_weaponinfo(wpn)).items;
179 }
180
181 void thrown_wep_think()
182 {
183         self.solid = SOLID_TRIGGER;
184         self.owner = world;
185         SUB_SetFade(self, time + 20, 1);
186 }
187
188 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
189 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
190 {
191         entity oldself, wep;
192         float wa, thisammo, i, j;
193         string s;
194         var .float ammofield;
195
196         wep = spawn();
197
198         setorigin(wep, org);
199         wep.classname = "droppedweapon";
200         wep.velocity = velo;
201         wep.owner = wep.enemy = own;
202         wep.flags |= FL_TOSSED;
203         wep.colormap = own.colormap;
204
205         wa = W_AmmoItemCode(wpn);
206         if(wa == IT_SUPERWEAPON || wa == 0)
207         {
208                 oldself = self;
209                 self = wep;
210                 weapon_defaultspawnfunc(wpn);
211                 self = oldself;
212                 if(startitem_failed)
213                         return string_null;
214                 wep.think = thrown_wep_think;
215                 wep.nextthink = time + 0.5;
216                 return "";
217         }
218         else
219         {
220                 s = "";
221                 oldself = self;
222                 self = wep;
223                 weapon_defaultspawnfunc(wpn);
224                 self = oldself;
225                 if(startitem_failed)
226                         return string_null;
227                 if(doreduce)
228                 {
229                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
230                         {
231                                 if(wa & j)
232                                 {
233                                         ammofield = Item_CounterField(j);
234                                         thisammo = min(own.ammofield, wep.ammofield);
235                                         wep.ammofield = thisammo;
236                                         own.ammofield -= thisammo;
237                                         s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
238                                 }
239                         }
240                         s = substring(s, 5, -1);
241                 }
242                 wep.think = thrown_wep_think;
243                 wep.nextthink = time + 0.5;
244                 return s;
245         }
246 }
247
248 float W_IsWeaponThrowable(float w)
249 {
250         float wb, wa;
251         wb = W_WeaponBit(w);
252         if(!wb)
253                 return 0;
254         wa = W_AmmoItemCode(w);
255         if(start_weapons & wb)
256         {
257                 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
258                         return 0;
259                 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
260                         return 0;
261                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
262                 if(wa == 0)
263                         return 0;
264         }
265
266         return 1;
267 }
268
269 // toss current weapon
270 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
271 {
272         local float w, wb;
273         string a;
274
275         w = self.weapon;
276         if (w == 0)
277                 return; // just in case
278         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
279                 return;
280         if (g_weaponarena)
281                 return;
282         if (g_lms)
283                 return;
284         if (g_nexball && w == WEP_GRENADE_LAUNCHER)
285                 return;
286         if (!cvar("g_pickup_items"))
287                 return;
288         if (g_ca)
289                 return;
290         if(!cvar("g_weapon_throwable"))
291                 return;
292         if(cvar("g_weapon_stay") == 1)
293                 return;
294         if(!W_IsWeaponThrowable(w))
295                 return;
296         if(self.weaponentity.state != WS_READY)
297                 return;
298
299         wb = W_WeaponBit(w);
300         if(self.weapons & wb != wb)
301                 return;
302
303         self.weapons &~= wb;
304         W_SwitchWeapon_Force(self, w_getbestweapon(self));
305         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
306         if not(a)
307                 return;
308         if(self.health >= 1)
309         {
310                 if(a == "")
311                         sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
312                 else
313                         sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
314         }
315 };
316
317 // Bringed back weapon frame
318 void W_WeaponFrame()
319 {
320         vector fo, ri, up;
321
322         if (frametime)
323                 self.weapon_frametime = frametime;
324
325         if(((arena_roundbased || g_ca) && time < warmup) || ((time < game_starttime) && !cvar("sv_ready_restart_after_countdown")))
326                 return;
327
328         if (!self.weaponentity || self.health < 1)
329                 return; // Dead player can't use weapons and injure impulse commands
330
331         if(!self.switchweapon)
332         {
333                 self.weapon = 0;
334                 self.weaponentity.state = WS_CLEAR;
335                 self.weaponname = "";
336                 self.items &~= IT_AMMO;
337                 return;
338         }
339
340         makevectors(self.v_angle);
341         fo = v_forward; // save them in case the weapon think functions change it
342         ri = v_right;
343         up = v_up;
344
345         // Change weapon
346         if (self.weapon != self.switchweapon)
347         {
348                 if (self.weaponentity.state == WS_CLEAR)
349                 {
350                         setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
351                         self.weaponentity.state = WS_RAISE;
352                         weapon_action(self.switchweapon, WR_SETUP);
353                         // VorteX: add player model weapon select frame here
354                         // setcustomframe(PlayerWeaponRaise);
355                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
356                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
357                 }
358                 else if (self.weaponentity.state == WS_READY)
359                 {
360 #ifndef INDEPENDENT_ATTACK_FINISHED
361                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
362                         {
363 #endif
364                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
365                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
366                         self.weaponentity.state = WS_DROP;
367                         // set up weapon switch think in the future, and start drop anim
368                         weapon_thinkf(WFRAME_DONTCHANGE, cvar("g_balance_weaponswitchdelay"), w_clear);
369                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
370 #ifndef INDEPENDENT_ATTACK_FINISHED
371                         }
372 #endif
373                 }
374         }
375
376         // LordHavoc: network timing test code
377         //if (self.button0)
378         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
379
380         float wb;
381         wb = W_WeaponBit(self.weapon);
382
383         // call the think code which may fire the weapon
384         // and do so multiple times to resolve framerate dependency issues if the
385         // server framerate is very low and the weapon fire rate very high
386         local float c;
387         c = 0;
388         while (c < 5)
389         {
390                 c = c + 1;
391                 if(wb && ((self.weapons & wb) == 0))
392                 {
393                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
394                         wb = 0;
395                 }
396                 if(wb)
397                 {
398                         v_forward = fo;
399                         v_right = ri;
400                         v_up = up;
401                         weapon_action(self.weapon, WR_THINK);
402                 }
403                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
404                 {
405                         if(self.weapon_think)
406                         {
407                                 v_forward = fo;
408                                 v_right = ri;
409                                 v_up = up;
410                                 self.weapon_think();
411                         }
412                         else
413                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
414                 }
415         }
416
417         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
418         //if (ATTACK_FINISHED(self) < time)
419         //      ATTACK_FINISHED(self) = time;
420
421         //if (self.weapon_nextthink < time)
422         //      self.weapon_nextthink = time;
423
424         // update currentammo incase it has changed
425 #if 0
426         if (self.items & IT_CELLS)
427                 self.currentammo = self.ammo_cells;
428         else if (self.items & IT_ROCKETS)
429                 self.currentammo = self.ammo_rockets;
430         else if (self.items & IT_NAILS)
431                 self.currentammo = self.ammo_nails;
432         else if (self.items & IT_SHELLS)
433                 self.currentammo = self.ammo_shells;
434         else
435                 self.currentammo = 1;
436 #endif
437 };