]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
show weapon to spectators too
[divverent/nexuiz.git] / data / qcsrc / server / cl_weaponsystem.qc
1 /*
2 ===========================================================================
3
4   CLIENT WEAPONSYSTEM CODE
5   Bring back W_Weaponframe
6
7 ===========================================================================
8 */
9
10 // VorteX: static frame globals
11 float WFRAME_FIRE1 = 0;
12 float WFRAME_FIRE2 = 1;
13 float WFRAME_IDLE = 2;
14
15 void(float fr, float t, void() func) weapon_thinkf;
16
17 vector w_shotorg;
18 vector w_shotdir;
19
20 // this function calculates w_shotorg and w_shotdir based on the weapon model
21 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
22 // make sure you call makevectors first (FIXME?)
23 void(entity ent, vector vecs, float antilag, float recoil, string snd) W_SetupShot =
24 {
25         local vector trueaimpoint;
26
27         traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
28         trueaimpoint = trace_endpos;
29
30         // if aiming at a player and the original trace won't hit that player
31         // anymore, try aiming at the player's new position
32         if (antilag)
33         if (!trace_ent.takedamage)                 // shot would miss without antilag
34         if (self.cursor_trace_ent)                 // but it HAD hit on the client
35         if (self.cursor_trace_ent.takedamage)      //   and even something evil
36         // if (trace_ent != self.cursor_trace_ent) // and it is different (redundant check, see takedamage)
37         if (cvar("g_antilag"))
38                 trueaimpoint = self.cursor_trace_ent.origin;
39
40         /*
41         // don't allow the shot to start inside a wall
42         local vector startorg;
43         local vector idealorg;
44         startorg = ent.origin + ent.view_ofs;// - '0 0 8';
45         idealorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
46         traceline_hitcorpse (ent, startorg, idealorg, MOVE_NOMONSTERS, ent);
47         w_shotorg = trace_endpos;
48         */
49         // all positions in the code are now required to be inside the player box
50         w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
51
52         w_shotdir = normalize(trueaimpoint - w_shotorg);
53
54         if (!cvar("g_norecoil"))
55                 self.punchangle_x = recoil * -1;
56
57         if (snd != "")
58                 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
59
60         if (self.items & IT_STRENGTH)
61         if (!cvar("g_minstagib"))
62                 sound (self, CHAN_AUTO, "weapons/strength_fire.ogg", 1, ATTN_NORM);
63 };
64
65 void LaserTarget_Think()
66 {
67         entity e;
68         vector offset;
69         float uselaser;
70         uselaser = 0;
71
72         // list of weapons that will use the laser, and the options that enable it
73         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && cvar("g_laserguided_missile"))
74                 uselaser = 1;
75         // example
76         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
77         //      uselaser = 1;
78
79
80
81         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
82         if(!uselaser)
83         {
84                 // rocket launcher isn't selected, so no laser target.
85                 if(self.lasertarget != world)
86                 {
87                         remove(self.lasertarget);
88                         self.lasertarget = world;
89                 }
90                 return;
91         }
92
93         if(!self.lasertarget)
94         {
95                 // we don't have a lasertarget entity, so spawn one
96                 //bprint("create laser target\n");
97                 e = self.lasertarget = spawn();
98                 e.owner = self.owner;                   // Its owner is my owner
99                 e.classname = "laser_target";
100                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
101                 setmodel(e, "models/laser_dot.mdl");    // what it looks like
102                 e.scale = 1.25;                         // make it larger
103                 e.alpha = 0.25;                         // transparency
104                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
105                 e.effects = EF_FULLBRIGHT;
106                 // make it dynamically glow
107                 // you should avoid over-using this, as it can slow down the player's computer.
108                 e.glow_color = 251; // red color
109                 e.glow_size = 12;
110         }
111         else
112                 e = self.lasertarget;
113
114         // move the laser dot to where the player is looking
115
116         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
117         offset = '0 0 26' + v_right*3;
118         traceline(self.owner.origin + offset, self.owner.origin + offset + v_forward * MAX_SHOT_DISTANCE, FALSE, self); // trace forward until you hit something, like a player or wall
119         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
120         if(trace_plane_normal != '0 0 0')
121                 e.angles = vectoangles(trace_plane_normal);
122         else
123                 e.angles = vectoangles(v_forward);
124 }
125
126 float CL_Weaponentity_CustomizeEntityForClient()
127 {
128         if(other == self.owner)
129                 self.viewmodelforclient = other;
130         else
131                 if(other.classname == "spectator")
132                         if(other.enemy == self.owner)
133                                 self.viewmodelforclient = other;
134         return TRUE;
135 }
136
137 .string weaponname;
138 void() CL_Weaponentity_Think =
139 {
140         self.nextthink = time;
141         if (intermission_running)
142                 self.frame = WFRAME_IDLE;
143         if (self.owner.weaponentity != self)
144         {
145                 remove(self);
146                 return;
147         }
148         if (self.owner.deadflag != DEAD_NO)
149         {
150                 self.model = "";
151                 return;
152         }
153         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
154         {
155                 self.cnt = self.owner.weapon;
156                 self.dmg = self.owner.modelindex;
157                 self.deadflag = self.owner.deadflag;
158                 if (self.owner.weaponname != "")
159                         setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym"));
160                 else
161                         self.model = "";
162         }
163         self.effects = self.owner.effects;
164
165         if (self.flags & FL_FLY)
166                 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
167                 self.effects = self.effects - (self.effects & EF_NODRAW);
168
169         if(self.owner.alpha >= 0)
170                 self.alpha = self.owner.alpha;
171         else
172                 self.alpha = 1;
173         self.colormap = self.owner.colormap;
174
175         self.angles = '0 0 0';
176         local float f;
177         f = 0;
178         if (self.state == WS_RAISE)
179                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
180         else if (self.state == WS_DROP)
181                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
182         else if (self.state == WS_CLEAR)
183                 f = 1;
184         self.angles_x = -90 * f * f;
185
186         // create or update the lasertarget entity
187         LaserTarget_Think();
188 };
189
190 void() CL_ExteriorWeaponentity_Think =
191 {
192         self.nextthink = time;
193         if (self.owner.exteriorweaponentity != self)
194         {
195                 remove(self);
196                 return;
197         }
198         if (self.owner.deadflag != DEAD_NO)
199         {
200                 self.model = "";
201                 return;
202         }
203         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
204         {
205                 self.cnt = self.owner.weapon;
206                 self.dmg = self.owner.modelindex;
207                 self.deadflag = self.owner.deadflag;
208                 if (self.owner.weaponname != "")
209                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3"));
210                 else
211                         self.model = "";
212                 setattachment(self, self.owner, "bip01 r hand");
213                 // if that didn't find a tag, hide the exterior weapon model
214                 if (!self.tag_index)
215                         self.model = "";
216         }
217         self.effects = self.owner.effects;
218         self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
219         if(self.owner.alpha >= 0)
220                 self.alpha = self.owner.alpha;
221         else
222                 self.alpha = 1;
223         self.colormap = self.owner.colormap;
224 };
225
226 // spawning weaponentity for client
227 void() CL_SpawnWeaponentity =
228 {
229         self.weaponentity = spawn();
230         self.weaponentity.solid = SOLID_NOT;
231         self.weaponentity.owner = self;
232         self.weaponentity.weaponentity = self.weaponentity;
233         setmodel(self.weaponentity, "");
234         self.weaponentity.origin = '0 0 0';
235         self.weaponentity.angles = '0 0 0';
236         self.weaponentity.viewmodelforclient = self;
237         self.weaponentity.flags = 0;
238         self.weaponentity.think = CL_Weaponentity_Think;
239         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
240         self.weaponentity.nextthink = time;
241         self.weaponentity.scale = 0.61;
242
243         self.exteriorweaponentity = spawn();
244         self.exteriorweaponentity.solid = SOLID_NOT;
245         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
246         self.exteriorweaponentity.owner = self;
247         self.exteriorweaponentity.origin = '0 0 0';
248         self.exteriorweaponentity.angles = '0 0 0';
249         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
250         self.exteriorweaponentity.nextthink = time;
251 };
252
253 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
254 {
255         local float itemcode, f;
256         local entity oldself;
257
258         if (wpn < WEP_FIRST || wpn > WEP_LAST)
259         {
260                 if (complain)
261                         sprint(self, "Invalid weapon\n");
262                 return FALSE;
263         }
264         itemcode = W_ItemCode(wpn);
265         if (cl.items & itemcode)
266         {
267                 if (andammo)
268                 {
269                         oldself = self;
270                         self = cl;
271                         f = weapon_action(wpn, WR_CHECKAMMO1);
272                         f = f + weapon_action(wpn, WR_CHECKAMMO2);
273                         self = oldself;
274                         if (!f)
275                         {
276                                 if (complain)
277                                         sprint(self, "You don't have any ammo for that weapon\n");
278                                 return FALSE;
279                         }
280                 }
281                 return TRUE;
282         }
283         if (complain)
284                 sprint(self, "You don't own that weapon\n");
285         return FALSE;
286 };
287
288 // Weapon subs
289 void() w_clear =
290 {
291         if (self.weapon != -1)
292                 self.weapon = 0;
293         if (self.weaponentity)
294         {
295                 self.weaponentity.state = WS_CLEAR;
296                 setmodel(self.weaponentity, "");
297                 self.weaponentity.effects = 0;
298         }
299 };
300
301 void() w_ready =
302 {
303         if (self.weaponentity)
304         {
305                 self.weaponentity.state = WS_READY;
306                 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
307         }
308 };
309
310 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
311 float(entity e) w_getbestweapon
312 {// add new weapons here
313         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
314                 return WEP_ROCKET_LAUNCHER;
315         else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
316                 return WEP_NEX;
317         else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
318                 return WEP_HAGAR;
319         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
320                 return WEP_GRENADE_LAUNCHER;
321         else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
322                 return WEP_ELECTRO;
323         else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
324                 return WEP_CRYLINK;
325         else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
326                 return WEP_UZI;
327         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
328                 return WEP_SHOTGUN;
329         else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
330                 return WEP_LASER;
331         else
332                 return 0;
333 };
334
335 // Setup weapon for client (after this raise frame will be launched)
336 void(float windex, string wname, float hudammo) weapon_setup =
337 {
338         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
339         self.items = self.items | hudammo;
340
341         // the two weapon entities will notice this has changed and update their models
342         self.weapon = windex;
343         self.weaponname = wname;
344
345         // might fire faster after switch
346         self.attack_finished = min(max(time, self.attack_finished_old + 1), self.attack_finished);
347 };
348
349 // perform weapon to attack (weaponstate and attack_finished check is here)
350 float(float secondary, float attacktime) weapon_prepareattack =
351 {
352         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
353         {
354                 self.switchweapon = w_getbestweapon(self);
355                 if (self.switchweapon != self.weapon)
356                         self.cnt = self.weapon;
357                 return FALSE;
358         }
359         // don't fire if previous attack is not finished
360         if (self.attack_finished > time)
361                 return FALSE;
362         // don't fire while changing weapon
363         if (self.weaponentity.state != WS_READY)
364                 return FALSE;
365         self.weaponentity.state = WS_INUSE;
366         self.attack_finished_old = self.attack_finished;
367         self.attack_finished = max(time, self.attack_finished + attacktime);
368         return TRUE;
369 };
370
371 void(float fr, float t, void() func) weapon_thinkf =
372 {
373         if (fr >= 0)
374         {
375                 if (self.weaponentity != world)
376                         self.weaponentity.frame = fr;
377         }
378
379         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
380         {
381                 backtrace("Tried to override initial weapon think function - should this really happen?");
382         }
383
384         if(cvar("g_runematch"))
385         {
386                 if(self.runes & RUNE_SPEED)
387                 {
388                         if(self.runes & CURSE_SLOW)
389                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
390                         else
391                                 t = t * cvar("g_balance_rune_speed_atkrate");
392                 }
393                 else if(self.runes & CURSE_SLOW)
394                 {
395                         t = t * cvar("g_balance_curse_slow_atkrate");
396                 }
397         }
398
399         // VorteX: haste can be added here
400         self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
401         self.weapon_think = func;
402 };
403
404 void(float spd, vector org) weapon_boblayer1 =
405 {
406         // VorteX: haste can be added here
407 };
408
409 void(entity missile) W_SetupProjectileVelocity =
410 {
411         vector pvelocity;
412         vector mdirection;
413         float mspeed;
414         float outspeed;
415         float nstyle;
416         vector mvelocity;
417         vector outvelocity;
418
419         if(missile.owner == world)
420                 error("Unowned missile");
421         pvelocity = missile.owner.velocity;
422         mvelocity = missile.velocity;
423         mdirection = normalize(mvelocity);
424         mspeed = vlen(mvelocity);
425
426         nstyle = cvar("g_projectiles_newton_style");
427         if(nstyle == 0)
428         {
429                 // absolute velocity
430                 outvelocity = mvelocity;
431         }
432         else if(nstyle == 1)
433         {
434                 // true Newtonian projectiles
435                 outvelocity = pvelocity + mvelocity;
436         }
437         else if(nstyle == 2)
438         {
439                 // true Newtonian projectiles with automatic aim adjustment
440                 //
441                 // solve: |outspeed * mdirection - pvelocity| = mspeed
442                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
443                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
444                 // PLUS SIGN!
445                 // not defined?
446                 // then...
447                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
448                 // velocity without mdirection component > mspeed
449                 // fire at smallest possible mspeed that works?
450                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
451
452                 float D;
453                 float p;
454                 float q;
455                 p = mdirection * pvelocity;
456                 q = pvelocity * pvelocity - mspeed * mspeed;
457                 D = p * p - q;
458                 if(D < 0)
459                 {
460                         //dprint("impossible shot, adjusting\n");
461                         D = 0;
462                 }
463                 outspeed = p + sqrt(D);
464                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
465                 outvelocity = mdirection * outspeed;
466         }
467         else if(nstyle == 3)
468         {
469                 // pseudo-Newtonian:
470                 outspeed = mspeed + mdirection * pvelocity;
471                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
472                 outvelocity = mdirection * outspeed;
473         }
474         else if(nstyle == 4)
475         {
476                 // tZorkian:
477                 outspeed = mspeed + vlen(pvelocity);
478                 outvelocity = mdirection * outspeed;
479         }
480         else
481                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
482
483         //dprint("Adjusted from ", vtos(missile.velocity));
484         missile.velocity = outvelocity;
485         //dprint(" to ", vtos(missile.velocity), "\n");
486 }