]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weapons.qc
factor out noimpact checks, make ALL projectiles use the anti-skygrapple workaround...
[divverent/nexuiz.git] / data / qcsrc / server / cl_weapons.qc
1 // switch between weapons
2 void W_SwitchWeapon(float imp)
3 {
4         if (self.weapon != imp)
5         if (client_hasweapon(self, imp, TRUE, TRUE))
6                 W_SwitchWeapon_Force(self, imp);
7 };
8
9 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain)
10 {
11         float n, i, weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
12         n = tokenize_sane(weaponorder);
13         switchtonext = switchtolast = 0;
14         first_valid = prev_valid = 0;
15
16         if(dir == 0)
17                 switchtonext = 1;
18
19         for(i = 0; i < n; ++i)
20         {
21                 weaponwant = stof(argv(i));
22
23                 if(imp >= 0)
24                         if((get_weaponinfo(weaponwant)).impulse != imp)
25                                 continue;
26
27                 if(client_hasweapon(pl, weaponwant, TRUE, FALSE))
28                 {
29                         if(switchtonext)
30                                 return weaponwant;
31                         if(!first_valid)
32                                 first_valid = weaponwant;
33                         if(weaponwant == pl.switchweapon)
34                         {
35                                 if(dir >= 0)
36                                         switchtonext = 1;
37                                 else if(prev_valid)
38                                         return prev_valid;
39                                 else
40                                         switchtolast = 1;
41                         }
42                         prev_valid = weaponwant;
43                 }
44         }
45         if(first_valid)
46         {
47                 if(switchtolast)
48                         return prev_valid;
49                 else
50                         return first_valid;
51         }
52         // complain
53         if(complain)
54         {
55                 for(i = 0; i < n; ++i)
56                 {
57                         weaponwant = stof(argv(i));
58                         if(imp >= 0)
59                                 if((get_weaponinfo(weaponwant)).impulse != imp)
60                                         continue;
61                         client_hasweapon(pl, weaponwant, TRUE, TRUE);
62                 }
63         }
64         return 0;
65 }
66
67 void W_CycleWeapon(string weaponorder, float dir)
68 {
69         float w;
70         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1);
71         if(w > 0)
72                 W_SwitchWeapon(w);
73 }
74
75 void W_NextWeaponOnImpulse(float imp)
76 {
77         float w;
78         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1);
79         if(w > 0)
80                 W_SwitchWeapon(w);
81 }
82
83 // next weapon
84 void W_NextWeapon(float list)
85 {
86         if(list == 0)
87                 W_CycleWeapon(weaponpriority_hudselector_0, -1);
88         else if(list == 1)
89                 W_CycleWeapon(weaponpriority_hudselector_1, -1);
90         else if(list == 2)
91                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
92 }
93
94 // prev weapon
95 void W_PreviousWeapon(float list)
96 {
97         if(list == 0)
98                 W_CycleWeapon(weaponpriority_hudselector_0, +1);
99         else if(list == 1)
100                 W_CycleWeapon(weaponpriority_hudselector_1, +1);
101         else if(list == 2)
102                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
103 }
104
105 string W_FixWeaponOrder(string order, float complete)
106 {
107         return fixPriorityList(order, WEP_FIRST, WEP_LAST, complete);
108 }
109
110 string W_FixWeaponOrder_AllowIncomplete(string order)
111 {
112         return W_FixWeaponOrder(order, 0);
113 }
114
115 string W_FixWeaponOrder_ForceComplete(string order)
116 {
117         if(order == "")
118                 order = cvar_string("cl_weaponpriority");
119         return W_FixWeaponOrder(order, 1);
120 }
121
122 float w_getbestweapon(entity e)
123 {
124         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, 0);
125 };
126
127 // generic weapons table
128 // TODO should they be macros instead?
129 float weapon_action(float wpn, float wrequest)
130 {
131         return (get_weaponinfo(wpn)).weapon_func(wrequest);
132 };
133
134 string W_Name(float weaponid)
135 {
136         return (get_weaponinfo(weaponid)).message;
137 }
138
139 float W_WeaponBit(float wpn)
140 {
141         return (get_weaponinfo(wpn)).weapons;
142 }
143
144 float W_AmmoItemCode(float wpn)
145 {
146         return (get_weaponinfo(wpn)).items;
147 }
148
149 void thrown_wep_think()
150 {
151         self.solid = SOLID_TRIGGER;
152         self.owner = world;
153         SUB_SetFade(self, time + 20, 1);
154 }
155
156 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
157 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
158 {
159         entity oldself, wep;
160         float wa, thisammo;
161         var .float ammofield;
162
163         wep = spawn();
164
165         setorigin(wep, org);
166         wep.classname = "droppedweapon";
167         wep.velocity = velo;
168         wep.owner = wep.enemy = own;
169         wep.classname = "droppedweapon";
170         wep.flags = wep.flags | FL_TOSSED;
171         wep.colormap = own.colormap;
172
173         wa = W_AmmoItemCode(wpn);
174         if(wa == IT_SUPERWEAPON || wa == 0)
175         {
176                 oldself = self;
177                 self = wep;
178                 weapon_defaultspawnfunc(wpn);
179                 self = oldself;
180                 if(startitem_failed)
181                         return -1;
182                 wep.think = thrown_wep_think;
183                 wep.nextthink = time + 0.5;
184                 return 0;
185         }
186         else
187         {
188                 ammofield = Item_CounterField(wa);
189                 oldself = self;
190                 self = wep;
191                 weapon_defaultspawnfunc(wpn);
192                 self = oldself;
193                 if(startitem_failed)
194                         return -1;
195                 if(doreduce)
196                 {
197                         thisammo = min(own.ammofield, wep.ammofield);
198                         wep.ammofield = thisammo;
199                         own.ammofield -= thisammo;
200                 }
201                 wep.think = thrown_wep_think;
202                 wep.nextthink = time + 0.5;
203                 return wep.ammofield;
204         }
205 }
206
207 // toss current weapon
208 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
209 {
210         local float w, a, wb, wa;
211
212         w = self.weapon;
213         if (w == 0)
214                 return; // just in case
215         if (w == WEP_LASER)
216                 return; // just in case
217         if (g_weaponarena)
218                 return;
219         if (g_lms)
220                 return;
221         if (g_nixnex)
222                 return;
223         if (!cvar("g_pickup_items"))
224                 return;
225
226         wb = W_WeaponBit(w);
227         wa = W_AmmoItemCode(w);
228         if(!wb)
229                 return;
230         if(self.weapons & wb != wb)
231                 return;
232         if(start_weapons & wb)
233         {
234                 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
235                         return;
236                 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
237                         return;
238                 if(wa == 0)
239                         return;
240         }
241
242         self.weapons = self.weapons - wb;
243         W_SwitchWeapon_Force(self, w_getbestweapon(self));
244         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
245         if(a < 0)
246                 return;
247         if(self.health >= 1)
248         {
249                 if(a == 0)
250                         sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
251                 else
252                         sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", ftos(a), " ", Item_CounterFieldName(W_AmmoItemCode(w)), "\n"));
253         }
254 };
255
256 // Bringed back weapon frame
257 void W_WeaponFrame()
258 {
259         if((arena_roundbased && time < warmup) || ((time < game_starttime) && !cvar("sv_ready_restart_after_countdown")))
260                 return;
261
262         if (!self.weaponentity || self.health < 1)
263                 return; // Dead player can't use weapons and injure impulse commands
264
265         if(!self.switchweapon)
266         {
267                 self.weapon = 0;
268                 self.weaponentity.state = WS_CLEAR;
269                 return;
270         }
271
272         makevectors(self.v_angle);
273
274         // Change weapon
275         if (self.weapon != self.switchweapon)
276         {
277                 if (self.weaponentity.state == WS_CLEAR)
278                 {
279                         player_setanim(self.anim_draw, FALSE, TRUE, TRUE);
280                         self.weaponentity.state = WS_RAISE;
281                         weapon_action(self.switchweapon, WR_SETUP);
282                         // VorteX: add player model weapon select frame here
283                         // setcustomframe(PlayerWeaponRaise);
284                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
285                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
286                 }
287                 else if (self.weaponentity.state == WS_READY)
288                 {
289 #ifndef INDEPENDENT_ATTACK_FINISHED
290                         if(ATTACK_FINISHED(self) <= time + frametime * 0.5)
291                         {
292 #endif
293                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
294                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
295                         self.weaponentity.state = WS_DROP;
296                         // set up weapon switch think in the future, and start drop anim
297                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_clear);
298                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
299 #ifndef INDEPENDENT_ATTACK_FINISHED
300                         }
301 #endif
302                 }
303         }
304
305         float wb;
306         wb = W_WeaponBit(self.weapon);
307
308         // call the think code which may fire the weapon
309         // and do so multiple times to resolve framerate dependency issues if the
310         // server framerate is very low and the weapon fire rate very high
311         local float c;
312         c = 0;
313         while (c < 5)
314         {
315                 c = c + 1;
316                 if(wb && self.weapons & wb == 0)
317                 {
318                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
319                         wb = 0;
320                 }
321                 if(wb)
322                         weapon_action(self.weapon, WR_THINK);
323                 if (time + frametime * 0.5 >= self.weapon_nextthink)
324                         self.weapon_think();
325         }
326
327         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
328         //if (ATTACK_FINISHED(self) < time)
329         //      ATTACK_FINISHED(self) = time;
330
331         //if (self.weapon_nextthink < time)
332         //      self.weapon_nextthink = time;
333
334         // update currentammo incase it has changed
335         if (self.items & IT_CELLS)
336                 self.currentammo = self.ammo_cells;
337         else if (self.items & IT_ROCKETS)
338                 self.currentammo = self.ammo_rockets;
339         else if (self.items & IT_NAILS)
340                 self.currentammo = self.ammo_nails;
341         else if (self.items & IT_SHELLS)
342                 self.currentammo = self.ammo_shells;
343         else
344                 self.currentammo = 1;
345
346 };
347
348 float nixnex_weapon;
349 float nixnex_nextchange;
350 float nixnex_nextweapon;
351 .float nixnex_lastchange_id;
352 .float nixnex_lastinfotime;
353 .float nixnex_nextincr;
354
355 void Nixnex_ChooseNextWeapon()
356 {
357         float numberof, id;
358         numberof = WEP_LAST - WEP_FIRST; // all but the current one
359         if(g_nixnex_with_laser)
360                 numberof = numberof - 1;
361         id = WEP_FIRST + floor(random() * numberof);
362
363         if(g_nixnex_with_laser) // skip the laser if needed
364                 id = id + 1;
365
366         if(id >= nixnex_weapon) // skip the current weapon
367                 id = id + 1;
368
369         if(id < WEP_FIRST) // can't happen, but to be sure...
370         {
371                 dprint("Won't happen (id < WEP_FIRST)\n");
372                 id = WEP_FIRST;
373         }
374         if(id > WEP_LAST) // either
375         {
376                 dprint("Won't happen (id > WEP_LAST)\n");
377                 id = WEP_LAST;
378         }
379
380         nixnex_nextweapon = id;
381 }
382
383 void Nixnex_GiveCurrentWeapon()
384 {
385         float dt;
386         if(g_nixnex)
387         {
388                 if(!nixnex_nextweapon)
389                         Nixnex_ChooseNextWeapon();
390
391                 dt = ceil(nixnex_nextchange - time);
392
393                 if(dt <= 0)
394                 {
395                         nixnex_weapon = nixnex_nextweapon;
396                         nixnex_nextweapon = 0;
397                         nixnex_nextchange = time + cvar("g_balance_nixnex_roundtime");
398                         //weapon_action(nixnex_weapon, WR_PRECACHE); // forget it, too slow
399                 }
400
401                 if(nixnex_nextchange != self.nixnex_lastchange_id) // this shall only be called once per round!
402                 {
403                         self.nixnex_lastchange_id = nixnex_nextchange;
404                         if (self.items & IT_UNLIMITED_WEAPON_AMMO)
405                         {
406                                 self.ammo_shells = cvar("g_pickup_shells_max");
407                                 self.ammo_nails = cvar("g_pickup_nails_max");
408                                 self.ammo_rockets = cvar("g_pickup_rockets_max");
409                                 self.ammo_cells = cvar("g_pickup_cells_max");
410                         }
411                         else
412                         {
413                                 self.ammo_shells = cvar("g_balance_nixnex_ammo_shells");
414                                 self.ammo_nails = cvar("g_balance_nixnex_ammo_nails");
415                                 self.ammo_rockets = cvar("g_balance_nixnex_ammo_rockets");
416                                 self.ammo_cells = cvar("g_balance_nixnex_ammo_cells");
417                         }
418                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
419                         if(dt >= 1 && dt <= 5)
420                                 self.nixnex_lastinfotime = -42;
421                         else
422                                 centerprint(self, strcat("\n\n^2Active weapon: ^3", W_Name(nixnex_weapon), "\n"));
423                 }
424                 if(self.nixnex_lastinfotime != dt)
425                 {
426                         self.nixnex_lastinfotime = dt; // initial value 0 should count as "not seen"
427                         if(dt >= 1 && dt <= 5)
428                                 centerprint(self, strcat("^3", ftos(dt), "^2 seconds until weapon change...\n\nNext weapon: ^3", W_Name(nixnex_nextweapon), "\n"));
429                 }
430
431                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO) && time > self.nixnex_nextincr)
432                 {
433                         self.ammo_shells = self.ammo_shells + cvar("g_balance_nixnex_ammoincr_shells");
434                         self.ammo_nails = self.ammo_nails + cvar("g_balance_nixnex_ammoincr_nails");
435                         self.ammo_rockets = self.ammo_rockets + cvar("g_balance_nixnex_ammoincr_rockets");
436                         self.ammo_cells = self.ammo_cells + cvar("g_balance_nixnex_ammoincr_cells");
437                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
438                 }
439
440                 self.weapons = 0;
441                 if(g_nixnex_with_laser)
442                         self.weapons = self.weapons | WEPBIT_LASER;
443                 self.weapons = self.weapons | W_WeaponBit(nixnex_weapon);
444
445                 if(self.switchweapon != nixnex_weapon)
446                         if(!client_hasweapon(self, self.switchweapon, TRUE, FALSE))
447                                 if(client_hasweapon(self, nixnex_weapon, TRUE, FALSE))
448                                         W_SwitchWeapon(nixnex_weapon);
449         }
450 }
451
452 void RegisterWeapons()
453 {
454         // %weaponaddpoint
455         register_weapon(WEP_LASER,            w_laser,     0,              1, 1,     0, "laser",     "laser",           "Laser");
456         register_weapon(WEP_SHOTGUN,          w_shotgun,   IT_SHELLS,      2, 1,  2500, "shotgun",   "shotgun",         "Shotgun");
457         register_weapon(WEP_UZI,              w_uzi,       IT_NAILS,       3, 1,  5000, "uzi",       "uzi",             "Machine Gun");
458         register_weapon(WEP_GRENADE_LAUNCHER, w_glauncher, IT_ROCKETS,     4, 1,  5000, "gl",        "grenadelauncher", "Mortar");
459         register_weapon(WEP_ELECTRO,          w_electro,   IT_CELLS,       5, 1,  5000, "electro",   "electro",         "Electro");
460         register_weapon(WEP_CRYLINK,          w_crylink,   IT_CELLS,       6, 1,  5000, "crylink",   "crylink",         "Crylink");
461         register_weapon(WEP_NEX,              w_nex,       IT_CELLS,       7, 1, 10000, "nex",       "nex",             "Nex");
462         register_weapon(WEP_HAGAR,            w_hagar,     IT_ROCKETS,     8, 1,  5000, "hagar",     "hagar",           "Hagar");
463         register_weapon(WEP_ROCKET_LAUNCHER,  w_rlauncher, IT_ROCKETS,     9, 1, 10000, "rl",        "rocketlauncher",  "Rocket Launcher");
464         register_weapon(WEP_PORTO,            w_porto,     IT_SUPERWEAPON, 0, 0,     0, "porto" ,    "porto",           "Port-O-Launch");
465         register_weapon(WEP_MINSTANEX,        w_minstanex, IT_CELLS,       7, 0, 10000, "minstanex", "minstanex",       "MinstaNex");
466         register_weapon(WEP_HOOK,             w_hook,      IT_CELLS,       0, 0,     0, "hookgun",   "hook",            "Grappling Hook");
467         register_weapon(WEP_SEEKER,           w_seeker,    IT_ROCKETS,     8, 1,     0, "seeker",    "seeker",          "T.A.G. Seeker");
468         register_weapon(WEP_HLAC,             w_hlac,      IT_CELLS,       6, 1,     0, "hlac",      "hlac",            "Heavy Laser Assault Cannon");
469
470         register_weapons_done();
471 }