]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_weapons.qc
reset the weapon glowmod each frame before the WR_THINK function, let WR_THINK functi...
[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(self.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(self.weaponorder_byimpulse, +1);
138         else if(list == 2)
139                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
140 }
141
142 float w_getbestweapon(entity e)
143 {
144         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE);
145 };
146
147 // generic weapons table
148 // TODO should they be macros instead?
149 float weapon_action(float wpn, float wrequest)
150 {
151         return (get_weaponinfo(wpn)).weapon_func(wrequest);
152 };
153
154 string W_Name(float weaponid)
155 {
156         return (get_weaponinfo(weaponid)).message;
157 }
158
159 float W_WeaponBit(float wpn)
160 {
161         return (get_weaponinfo(wpn)).weapons;
162 }
163
164 float W_AmmoItemCode(float wpn)
165 {
166         return (get_weaponinfo(wpn)).items;
167 }
168
169 void thrown_wep_think()
170 {
171         self.solid = SOLID_TRIGGER;
172         self.owner = world;
173         SUB_SetFade(self, time + 20, 1);
174 }
175
176 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
177 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
178 {
179         entity oldself, wep;
180         float wa, thisammo, i, j;
181         string s;
182         var .float ammofield;
183
184         wep = spawn();
185
186         setorigin(wep, org);
187         wep.classname = "droppedweapon";
188         wep.velocity = velo;
189         wep.owner = wep.enemy = own;
190         wep.flags |= FL_TOSSED;
191         wep.colormap = own.colormap;
192
193         wa = W_AmmoItemCode(wpn);
194         if(wa == IT_SUPERWEAPON || wa == 0)
195         {
196                 oldself = self;
197                 self = wep;
198                 weapon_defaultspawnfunc(wpn);
199                 self = oldself;
200                 if(startitem_failed)
201                         return string_null;
202                 wep.think = thrown_wep_think;
203                 wep.nextthink = time + 0.5;
204                 return "";
205         }
206         else
207         {
208                 s = "";
209                 oldself = self;
210                 self = wep;
211                 weapon_defaultspawnfunc(wpn);
212                 self = oldself;
213                 if(startitem_failed)
214                         return string_null;
215                 if(doreduce)
216                 {
217                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
218                         {
219                                 if(wa & j)
220                                 {
221                                         ammofield = Item_CounterField(j);
222                                         thisammo = min(own.ammofield, wep.ammofield);
223                                         wep.ammofield = thisammo;
224                                         own.ammofield -= thisammo;
225                                         s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
226                                 }
227                         }
228                         s = substring(s, 5, -1);
229                 }
230                 wep.think = thrown_wep_think;
231                 wep.nextthink = time + 0.5;
232                 return s;
233         }
234 }
235
236 float W_IsWeaponThrowable(float w)
237 {
238         float wb, wa;
239         wb = W_WeaponBit(w);
240         if(!wb)
241                 return 0;
242         wa = W_AmmoItemCode(w);
243         if(start_weapons & wb)
244         {
245                 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
246                         return 0;
247                 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
248                         return 0;
249                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
250                 if(wa == 0)
251                         return 0;
252         }
253
254         return 1;
255 }
256
257 // toss current weapon
258 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
259 {
260         local float w, wb;
261         string a;
262
263         w = self.weapon;
264         if (w == 0)
265                 return; // just in case
266         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
267                 return;
268         if (g_weaponarena)
269                 return;
270         if (g_lms)
271                 return;
272         if (g_nexball && w == WEP_GRENADE_LAUNCHER)
273                 return;
274         if (!cvar("g_pickup_items"))
275                 return;
276         if (g_ca)
277                 return;
278         if(!cvar("g_weapon_throwable"))
279                 return;
280         if(cvar("g_weapon_stay") == 1)
281                 return;
282         if(!W_IsWeaponThrowable(w))
283                 return;
284         if(self.deadflag == DEAD_NO && self.weaponentity.state != WS_READY)
285                 return;
286
287         wb = W_WeaponBit(w);
288         if(self.weapons & wb != wb)
289                 return;
290
291         self.weapons &~= wb;
292         W_SwitchWeapon_Force(self, w_getbestweapon(self));
293         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
294         if not(a)
295                 return;
296         if(self.health >= 1)
297         {
298                 if(a == "")
299                         sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
300                 else
301                         sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
302         }
303 };
304
305 // Bringed back weapon frame
306 void W_WeaponFrame()
307 {
308         vector fo, ri, up;
309
310         if (frametime)
311                 self.weapon_frametime = frametime;
312
313         if(((arena_roundbased || g_ca || g_freezetag) && time < warmup) || ((time < game_starttime) && !cvar("sv_ready_restart_after_countdown")))
314                 return;
315
316         if(g_freezetag && self.freezetag_frozen == 1)
317                 return;
318
319         if (!self.weaponentity || self.health < 1)
320                 return; // Dead player can't use weapons and injure impulse commands
321
322         if(!self.switchweapon)
323         {
324                 self.weapon = 0;
325                 self.weaponentity.state = WS_CLEAR;
326                 self.weaponname = "";
327                 self.items &~= IT_AMMO;
328                 return;
329         }
330
331         makevectors(self.v_angle);
332         fo = v_forward; // save them in case the weapon think functions change it
333         ri = v_right;
334         up = v_up;
335
336         // Change weapon
337         if (self.weapon != self.switchweapon)
338         {
339                 if (self.weaponentity.state == WS_CLEAR)
340                 {
341                         setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
342                         self.weaponentity.state = WS_RAISE;
343                         weapon_action(self.switchweapon, WR_SETUP);
344                         // VorteX: add player model weapon select frame here
345                         // setcustomframe(PlayerWeaponRaise);
346                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
347                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
348                 }
349                 else if (self.weaponentity.state == WS_READY)
350                 {
351 #ifndef INDEPENDENT_ATTACK_FINISHED
352                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
353                         {
354 #endif
355                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
356                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
357                         self.weaponentity.state = WS_DROP;
358                         // set up weapon switch think in the future, and start drop anim
359                         weapon_thinkf(WFRAME_DONTCHANGE, cvar("g_balance_weaponswitchdelay"), w_clear);
360                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
361 #ifndef INDEPENDENT_ATTACK_FINISHED
362                         }
363 #endif
364                 }
365         }
366
367         // LordHavoc: network timing test code
368         //if (self.button0)
369         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
370
371         float wb;
372         wb = W_WeaponBit(self.weapon);
373
374         // call the think code which may fire the weapon
375         // and do so multiple times to resolve framerate dependency issues if the
376         // server framerate is very low and the weapon fire rate very high
377         local float c;
378         c = 0;
379         while (c < W_TICSPERFRAME)
380         {
381                 c = c + 1;
382                 if(wb && ((self.weapons & wb) == 0))
383                 {
384                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
385                         wb = 0;
386                 }
387                 if(wb)
388                 {
389                         v_forward = fo;
390                         v_right = ri;
391                         v_up = up;
392             self.weaponentity.weaponentity.glowmod = '0 0 0'; // reset glowmod, weapon think function only *might* set it
393                         weapon_action(self.weapon, WR_THINK);
394             self.exteriorweaponentity.glowmod = self.weaponentity.weaponentity.glowmod; // exterior weaponmodel has the same glowmod
395                 }
396                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
397                 {
398                         if(self.weapon_think)
399                         {
400                                 v_forward = fo;
401                                 v_right = ri;
402                                 v_up = up;
403                                 self.weapon_think();
404                         }
405                         else
406                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
407                 }
408         }
409
410         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
411         //if (ATTACK_FINISHED(self) < time)
412         //      ATTACK_FINISHED(self) = time;
413
414         //if (self.weapon_nextthink < time)
415         //      self.weapon_nextthink = time;
416
417         // update currentammo incase it has changed
418 #if 0
419         if (self.items & IT_CELLS)
420                 self.currentammo = self.ammo_cells;
421         else if (self.items & IT_ROCKETS)
422                 self.currentammo = self.ammo_rockets;
423         else if (self.items & IT_NAILS)
424                 self.currentammo = self.ammo_nails;
425         else if (self.items & IT_SHELLS)
426                 self.currentammo = self.ammo_shells;
427         else
428                 self.currentammo = 1;
429 #endif
430 };