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