]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weapons.qc
properly hide the weapon after throwing the last one
[divverent/nexuiz.git] / data / 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         }
16         else
17         {
18                 W_Reload();
19         }
20 };
21
22 .float weaponcomplainindex;
23 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain)
24 {
25         float n, i, weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
26         n = tokenize_sane(weaponorder);
27         switchtonext = switchtolast = 0;
28         first_valid = prev_valid = 0;
29
30         if(dir == 0)
31                 switchtonext = 1;
32
33         c = 0;
34
35         for(i = 0; i < n; ++i)
36         {
37                 weaponwant = stof(argv(i));
38                 if(imp >= 0)
39                         if((get_weaponinfo(weaponwant)).impulse != imp)
40                                 continue;
41
42                 ++c;
43
44                 if(client_hasweapon(pl, weaponwant, TRUE, FALSE))
45                 {
46                         if(switchtonext)
47                                 return weaponwant;
48                         if(!first_valid)
49                                 first_valid = weaponwant;
50                         if(weaponwant == pl.switchweapon)
51                         {
52                                 if(dir >= 0)
53                                         switchtonext = 1;
54                                 else if(prev_valid)
55                                         return prev_valid;
56                                 else
57                                         switchtolast = 1;
58                         }
59                         prev_valid = weaponwant;
60                 }
61         }
62         if(first_valid)
63         {
64                 if(switchtolast)
65                         return prev_valid;
66                 else
67                         return first_valid;
68         }
69         // complain (but only for one weapon on the button that has been pressed)
70         if(complain)
71         {
72                 self.weaponcomplainindex += 1;
73                 c = mod(self.weaponcomplainindex, c) + 1;
74                 for(i = 0; i < n; ++i)
75                 {
76                         weaponwant = stof(argv(i));
77                         if(imp >= 0)
78                                 if((get_weaponinfo(weaponwant)).impulse != imp)
79                                         continue;
80
81                         --c;
82                         if(c == 0)
83                         {
84                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
85                                 break;
86                         }
87                 }
88         }
89         return 0;
90 }
91
92 void W_CycleWeapon(string weaponorder, float dir)
93 {
94         float w;
95         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1);
96         if(w > 0)
97                 W_SwitchWeapon(w);
98 }
99
100 void W_NextWeaponOnImpulse(float imp)
101 {
102         float w;
103         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1);
104         if(w > 0)
105                 W_SwitchWeapon(w);
106 }
107
108 // next weapon
109 void W_NextWeapon(float list)
110 {
111         if(list == 0)
112                 W_CycleWeapon(weaponpriority_hudselector_0, -1);
113         else if(list == 1)
114                 W_CycleWeapon(weaponpriority_hudselector_1, -1);
115         else if(list == 2)
116                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
117 }
118
119 // prev weapon
120 void W_PreviousWeapon(float list)
121 {
122         if(list == 0)
123                 W_CycleWeapon(weaponpriority_hudselector_0, +1);
124         else if(list == 1)
125                 W_CycleWeapon(weaponpriority_hudselector_1, +1);
126         else if(list == 2)
127                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
128 }
129
130 string W_FixWeaponOrder_AllowIncomplete(string order)
131 {
132         return W_FixWeaponOrder(order, 0);
133 }
134
135 string W_FixWeaponOrder_ForceComplete(string order)
136 {
137         if(order == "")
138                 order = cvar_string("cl_weaponpriority");
139         return W_FixWeaponOrder(order, 1);
140 }
141
142 float w_getbestweapon(entity e)
143 {
144         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, 0);
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, or -1 for failure, or 0 for no ammo count
177 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
178 {
179         entity oldself, wep;
180         float wa, thisammo;
181         var .float ammofield;
182
183         wep = spawn();
184
185         setorigin(wep, org);
186         wep.classname = "droppedweapon";
187         wep.velocity = velo;
188         wep.owner = wep.enemy = own;
189         wep.classname = "droppedweapon";
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 -1;
202                 wep.think = thrown_wep_think;
203                 wep.nextthink = time + 0.5;
204                 return 0;
205         }
206         else
207         {
208                 ammofield = Item_CounterField(wa);
209                 oldself = self;
210                 self = wep;
211                 weapon_defaultspawnfunc(wpn);
212                 self = oldself;
213                 if(startitem_failed)
214                         return -1;
215                 if(doreduce)
216                 {
217                         thisammo = min(own.ammofield, wep.ammofield);
218                         wep.ammofield = thisammo;
219                         own.ammofield -= thisammo;
220                 }
221                 wep.think = thrown_wep_think;
222                 wep.nextthink = time + 0.5;
223                 return wep.ammofield;
224         }
225 }
226
227 float W_IsWeaponThrowable(float w)
228 {
229         float wb, wa;
230         wb = W_WeaponBit(w);
231         if(!wb)
232                 return 0;
233         wa = W_AmmoItemCode(w);
234         if(start_weapons & wb)
235         {
236                 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
237                         return 0;
238                 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
239                         return 0;
240                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
241                 if(wa == 0)
242                         return 0;
243         }
244
245         return 1;
246 }
247
248 // toss current weapon
249 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
250 {
251         local float w, a, wb;
252
253         w = self.weapon;
254         if (w == 0)
255                 return; // just in case
256         if (g_weaponarena)
257                 return;
258         if (g_lms)
259                 return;
260         if (g_nixnex)
261                 return;
262         if (!cvar("g_pickup_items"))
263                 return;
264         if(!W_IsWeaponThrowable(w))
265                 return;
266
267         wb = W_WeaponBit(w);
268         if(self.weapons & wb != wb)
269                 return;
270
271         self.weapons &~= wb;
272         W_SwitchWeapon_Force(self, w_getbestweapon(self));
273         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
274         if(a < 0)
275                 return;
276         if(self.health >= 1)
277         {
278                 if(a == 0)
279                         sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
280                 else
281                         sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", ftos(a), " ", Item_CounterFieldName(W_AmmoItemCode(w)), "\n"));
282         }
283 };
284
285 // Bringed back weapon frame
286 void W_WeaponFrame()
287 {
288         if((arena_roundbased && time < warmup) || ((time < game_starttime) && !cvar("sv_ready_restart_after_countdown")))
289                 return;
290
291         if (!self.weaponentity || self.health < 1)
292                 return; // Dead player can't use weapons and injure impulse commands
293
294         if(!self.switchweapon)
295         {
296                 self.weapon = 0;
297                 self.weaponentity.state = WS_CLEAR;
298                 self.weaponname = "";
299                 return;
300         }
301
302         makevectors(self.v_angle);
303
304         // Change weapon
305         if (self.weapon != self.switchweapon)
306         {
307                 if (self.weaponentity.state == WS_CLEAR)
308                 {
309                         player_setanim(self.anim_draw, FALSE, TRUE, TRUE);
310                         self.weaponentity.state = WS_RAISE;
311                         weapon_action(self.switchweapon, WR_SETUP);
312                         // VorteX: add player model weapon select frame here
313                         // setcustomframe(PlayerWeaponRaise);
314                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
315                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
316                 }
317                 else if (self.weaponentity.state == WS_READY)
318                 {
319 #ifndef INDEPENDENT_ATTACK_FINISHED
320                         if(ATTACK_FINISHED(self) <= time + frametime * 0.5)
321                         {
322 #endif
323                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
324                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
325                         self.weaponentity.state = WS_DROP;
326                         // set up weapon switch think in the future, and start drop anim
327                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_clear);
328                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
329 #ifndef INDEPENDENT_ATTACK_FINISHED
330                         }
331 #endif
332                 }
333         }
334
335         float wb;
336         wb = W_WeaponBit(self.weapon);
337
338         // call the think code which may fire the weapon
339         // and do so multiple times to resolve framerate dependency issues if the
340         // server framerate is very low and the weapon fire rate very high
341         local float c;
342         c = 0;
343         while (c < 5)
344         {
345                 c = c + 1;
346                 if(wb && self.weapons & wb == 0)
347                 {
348                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
349                         wb = 0;
350                 }
351                 if(wb)
352                         weapon_action(self.weapon, WR_THINK);
353                 if (time + frametime * 0.5 >= self.weapon_nextthink)
354                 {
355                         if(self.weapon_think)
356                                 self.weapon_think();
357                         else
358                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
359                 }
360         }
361
362         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
363         //if (ATTACK_FINISHED(self) < time)
364         //      ATTACK_FINISHED(self) = time;
365
366         //if (self.weapon_nextthink < time)
367         //      self.weapon_nextthink = time;
368
369         // update currentammo incase it has changed
370         if (self.items & IT_CELLS)
371                 self.currentammo = self.ammo_cells;
372         else if (self.items & IT_ROCKETS)
373                 self.currentammo = self.ammo_rockets;
374         else if (self.items & IT_NAILS)
375                 self.currentammo = self.ammo_nails;
376         else if (self.items & IT_SHELLS)
377                 self.currentammo = self.ammo_shells;
378         else
379                 self.currentammo = 1;
380
381 };
382
383 float nixnex_weapon;
384 float nixnex_nextchange;
385 float nixnex_nextweapon;
386 .float nixnex_lastchange_id;
387 .float nixnex_lastinfotime;
388 .float nixnex_nextincr;
389
390 void Nixnex_ChooseNextWeapon()
391 {
392         float j;
393         entity e;
394
395         RandomSelection_Init();
396         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
397         {
398                 if(j == WEP_LASER && g_nixnex_with_laser)
399                         continue;
400                 if(!e.weapons) // skip dummies
401                         continue;
402                 RandomSelection_Add(world, j, 1, 0);
403         }
404         nixnex_nextweapon = RandomSelection_chosen_float;
405 }
406
407 void Nixnex_GiveCurrentWeapon()
408 {
409         float dt;
410         if(g_nixnex)
411         {
412                 if(!nixnex_nextweapon)
413                         Nixnex_ChooseNextWeapon();
414
415                 dt = ceil(nixnex_nextchange - time);
416
417                 if(dt <= 0)
418                 {
419                         nixnex_weapon = nixnex_nextweapon;
420                         nixnex_nextweapon = 0;
421                         nixnex_nextchange = time + cvar("g_balance_nixnex_roundtime");
422                         //weapon_action(nixnex_weapon, WR_PRECACHE); // forget it, too slow
423                 }
424
425                 if(nixnex_nextchange != self.nixnex_lastchange_id) // this shall only be called once per round!
426                 {
427                         self.nixnex_lastchange_id = nixnex_nextchange;
428                         if (self.items & IT_UNLIMITED_WEAPON_AMMO)
429                         {
430                                 self.ammo_shells = cvar("g_pickup_shells_max");
431                                 self.ammo_nails = cvar("g_pickup_nails_max");
432                                 self.ammo_rockets = cvar("g_pickup_rockets_max");
433                                 self.ammo_cells = cvar("g_pickup_cells_max");
434                         }
435                         else
436                         {
437                                 self.ammo_shells = cvar("g_balance_nixnex_ammo_shells");
438                                 self.ammo_nails = cvar("g_balance_nixnex_ammo_nails");
439                                 self.ammo_rockets = cvar("g_balance_nixnex_ammo_rockets");
440                                 self.ammo_cells = cvar("g_balance_nixnex_ammo_cells");
441                         }
442                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
443                         if(dt >= 1 && dt <= 5)
444                                 self.nixnex_lastinfotime = -42;
445                         else
446                                 centerprint(self, strcat("\n\n^2Active weapon: ^3", W_Name(nixnex_weapon)));
447                 }
448                 if(self.nixnex_lastinfotime != dt)
449                 {
450                         self.nixnex_lastinfotime = dt; // initial value 0 should count as "not seen"
451                         if(dt >= 1 && dt <= 5)
452                                 centerprint(self, strcat("^3", ftos(dt), "^2 seconds until weapon change...\n\nNext weapon: ^3", W_Name(nixnex_nextweapon), "\n"));
453                 }
454
455                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO) && time > self.nixnex_nextincr)
456                 {
457                         self.ammo_shells = self.ammo_shells + cvar("g_balance_nixnex_ammoincr_shells");
458                         self.ammo_nails = self.ammo_nails + cvar("g_balance_nixnex_ammoincr_nails");
459                         self.ammo_rockets = self.ammo_rockets + cvar("g_balance_nixnex_ammoincr_rockets");
460                         self.ammo_cells = self.ammo_cells + cvar("g_balance_nixnex_ammoincr_cells");
461                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
462                 }
463
464                 self.weapons = 0;
465                 if(g_nixnex_with_laser)
466                         self.weapons = self.weapons | WEPBIT_LASER;
467                 self.weapons = self.weapons | W_WeaponBit(nixnex_weapon);
468
469                 if(self.switchweapon != nixnex_weapon)
470                         if(!client_hasweapon(self, self.switchweapon, TRUE, FALSE))
471                                 if(client_hasweapon(self, nixnex_weapon, TRUE, FALSE))
472                                         W_SwitchWeapon(nixnex_weapon);
473         }
474 }