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