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