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