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