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