fix glowmod of weapon entity (no longer assign it to world, make code more consistent)
[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 (!autocvar_g_pickup_items)
275                 return;
276         if (g_ca)
277                 return;
278     if (g_cts)
279         return;
280         if(!autocvar_g_weapon_throwable)
281                 return;
282         if(autocvar_g_weapon_stay == 1)
283                 return;
284         if(!W_IsWeaponThrowable(w))
285                 return;
286         if(self.deadflag == DEAD_NO && self.weaponentity.state != WS_READY)
287                 return;
288
289         wb = W_WeaponBit(w);
290         if(self.weapons & wb != wb)
291                 return;
292
293         self.weapons &~= wb;
294         W_SwitchWeapon_Force(self, w_getbestweapon(self));
295         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
296         if not(a)
297                 return;
298         if(self.health >= 1)
299         {
300                 if(a == "")
301                         sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
302                 else
303                         sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
304         }
305 };
306
307 // Bringed back weapon frame
308 void W_WeaponFrame()
309 {
310         vector fo, ri, up;
311
312         if (frametime)
313                 self.weapon_frametime = frametime;
314
315         if(((arena_roundbased || g_ca || g_freezetag) && time < warmup) || ((time < game_starttime) && !autocvar_sv_ready_restart_after_countdown))
316                 return;
317
318         if(g_freezetag && self.freezetag_frozen == 1)
319                 return;
320
321         if (!self.weaponentity || self.health < 1)
322                 return; // Dead player can't use weapons and injure impulse commands
323
324         if(!self.switchweapon)
325         {
326                 self.weapon = 0;
327                 self.weaponentity.state = WS_CLEAR;
328                 self.weaponname = "";
329                 self.items &~= IT_AMMO;
330                 return;
331         }
332
333         makevectors(self.v_angle);
334         fo = v_forward; // save them in case the weapon think functions change it
335         ri = v_right;
336         up = v_up;
337
338         // Change weapon
339         if (self.weapon != self.switchweapon)
340         {
341                 if (self.weaponentity.state == WS_CLEAR)
342                 {
343                         setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
344                         self.weaponentity.state = WS_RAISE;
345                         weapon_action(self.switchweapon, WR_SETUP);
346                         // VorteX: add player model weapon select frame here
347                         // setcustomframe(PlayerWeaponRaise);
348                         weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_weaponswitchdelay, w_ready);
349                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
350                 }
351                 else if (self.weaponentity.state == WS_READY)
352                 {
353 #ifndef INDEPENDENT_ATTACK_FINISHED
354                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
355                         {
356 #endif
357                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
358                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
359                         self.weaponentity.state = WS_DROP;
360                         // set up weapon switch think in the future, and start drop anim
361                         weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_weaponswitchdelay, w_clear);
362                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
363 #ifndef INDEPENDENT_ATTACK_FINISHED
364                         }
365 #endif
366                 }
367         }
368
369         // LordHavoc: network timing test code
370         //if (self.button0)
371         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
372
373         float wb;
374         wb = W_WeaponBit(self.weapon);
375
376         // call the think code which may fire the weapon
377         // and do so multiple times to resolve framerate dependency issues if the
378         // server framerate is very low and the weapon fire rate very high
379         local float c;
380         c = 0;
381         while (c < W_TICSPERFRAME)
382         {
383                 c = c + 1;
384                 if(wb && ((self.weapons & wb) == 0))
385                 {
386                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
387                         wb = 0;
388                 }
389                 if(wb)
390                 {
391                         v_forward = fo;
392                         v_right = ri;
393                         v_up = up;
394                         self.weaponentity_glowmod = '0 0 0'; // reset glowmod, weapon think function only *might* set it
395                         weapon_action(self.weapon, WR_THINK);
396                 }
397                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
398                 {
399                         if(self.weapon_think)
400                         {
401                                 v_forward = fo;
402                                 v_right = ri;
403                                 v_up = up;
404                                 self.weapon_think();
405                         }
406                         else
407                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
408                 }
409         }
410
411         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
412         //if (ATTACK_FINISHED(self) < time)
413         //      ATTACK_FINISHED(self) = time;
414
415         //if (self.weapon_nextthink < time)
416         //      self.weapon_nextthink = time;
417
418         // update currentammo incase it has changed
419 #if 0
420         if (self.items & IT_CELLS)
421                 self.currentammo = self.ammo_cells;
422         else if (self.items & IT_ROCKETS)
423                 self.currentammo = self.ammo_rockets;
424         else if (self.items & IT_NAILS)
425                 self.currentammo = self.ammo_nails;
426         else if (self.items & IT_SHELLS)
427                 self.currentammo = self.ammo_shells;
428         else
429                 self.currentammo = 1;
430 #endif
431 };