]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
check for "player", as antilag relies on takeback now
[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 .float antilag_debug;
11
12 // VorteX: static frame globals
13 float WFRAME_FIRE1 = 0;
14 float WFRAME_FIRE2 = 1;
15 float WFRAME_IDLE = 2;
16
17 void(float fr, float t, void() func) weapon_thinkf;
18
19 vector w_shotorg;
20 vector w_shotdir;
21
22 // this function calculates w_shotorg and w_shotdir based on the weapon model
23 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
24 // make sure you call makevectors first (FIXME?)
25 void(entity ent, vector vecs, float antilag, float recoil, string snd) W_SetupShot =
26 {
27         local vector trueaimpoint;
28         traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
29         trueaimpoint = trace_endpos;
30
31         if (cvar("g_shootfromeye"))
32                 w_shotorg = ent.origin + ent.view_ofs;
33         else if (cvar("g_shootfromcenter"))
34                 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
35         else
36                 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
37         // now move the shotorg forward as much as requested if possible
38         traceline_hitcorpse(self, w_shotorg, w_shotorg + v_forward * vecs_x, MOVE_NORMAL, self);
39         w_shotorg = trace_endpos;
40         // calculate the shotdir from the chosen shotorg
41         w_shotdir = normalize(trueaimpoint - w_shotorg);
42
43         // explanation of g_antilag:
44         // if client reports it was aiming at a player, and the serverside trace
45         // says it would miss, change the aim point to the player's new origin,
46         // but only if the shot at the player's new origin would hit of course
47         //
48         // FIXME: a much better method for bullet weapons would be to leave a
49         // trail of lagged 'ghosts' behind players, and see if the bullet hits the
50         // ghost corresponding to this player's ping time, and if so it would do
51         // damage to the real player
52         if (antilag)
53         if (self.cursor_trace_ent)                 // client was aiming at someone
54         if (self.cursor_trace_ent != self)         // just to make sure
55         if (self.cursor_trace_ent.takedamage)      // and that person is killable
56         if (self.cursor_trace_ent.classname == "player") // and actually a player
57         if (cvar("g_antilag"))
58         {
59                 // verify that the shot would miss without antilag
60                 // (avoids an issue where guns would always shoot at their origin)
61                 traceline_hitcorpse(self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
62                 if (!trace_ent.takedamage)
63                 {
64                         // verify that the shot would hit if altered
65                         traceline_hitcorpse(self, w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
66                         if (trace_ent == self.cursor_trace_ent)
67                         {
68                                 // verify that the shot would hit in the past
69                                 if(self.antilag_debug)
70                                         antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
71                                 else
72                                         antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
73
74                                 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
75                                 antilag_restore(self.cursor_trace_ent);
76
77                                 if(trace_ent == self.cursor_trace_ent)
78                                 {
79                                         // HIT!
80                                         w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
81                                         dprint("ANTILAG HIT for ", self.netname, "\n");
82                                 }
83                                 else
84                                 {
85                                         // prydon cursor aimbot or odd network conditions
86                                         dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
87                                 }
88
89                                 if(cvar("developer") >= 2)
90                                 {
91                                         vector v, vplus, vel;
92                                         float X;
93                                         v     = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001       ));
94                                         vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 + 0.01));
95                                         vel = (vplus - v) * (1 / 0.01);
96                                         // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
97                                         v     -= (self.origin + self.view_ofs);
98                                         // solve: v + X * vel = closest to v_forward axis
99                                         // project into 2D by subtracting v_forward components:
100                                         v   -= (v   * v_forward) * v_forward;
101                                         vel -= (vel * v_forward) * v_forward;
102                                         // solve: v + X * vel = closest to origin
103                                         // (v + X * vel)^2 closest to 0
104                                         // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
105                                         X = -(v * vel) / (vel * vel);
106                                         dprint("dead center needs adjustment of ", ftos(X * 1000), " (that is, ", ftos(self.ping + X * 1000), " instead of ", ftos(self.ping), "\n");
107                                 }
108                         }
109                 }
110         }
111
112         if (!g_norecoil)
113                 self.punchangle_x = recoil * -1;
114
115         if (snd != "")
116                 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
117
118         if (self.items & IT_STRENGTH)
119         if (!g_minstagib)
120                 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
121 };
122
123 void LaserTarget_Think()
124 {
125         entity e;
126         vector offset;
127         float uselaser;
128         uselaser = 0;
129
130         // list of weapons that will use the laser, and the options that enable it
131         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
132                 uselaser = 1;
133         // example
134         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
135         //      uselaser = 1;
136
137
138
139         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
140         if(!uselaser)
141         {
142                 // rocket launcher isn't selected, so no laser target.
143                 if(self.lasertarget != world)
144                 {
145                         remove(self.lasertarget);
146                         self.lasertarget = world;
147                 }
148                 return;
149         }
150
151         if(!self.lasertarget)
152         {
153                 // we don't have a lasertarget entity, so spawn one
154                 //bprint("create laser target\n");
155                 e = self.lasertarget = spawn();
156                 e.owner = self.owner;                   // Its owner is my owner
157                 e.classname = "laser_target";
158                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
159                 setmodel(e, "models/laser_dot.mdl");    // what it looks like, precision set below
160                 e.scale = 1.25;                         // make it larger
161                 e.alpha = 0.25;                         // transparency
162                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
163                 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
164                 // make it dynamically glow
165                 // you should avoid over-using this, as it can slow down the player's computer.
166                 e.glow_color = 251; // red color
167                 e.glow_size = 12;
168         }
169         else
170                 e = self.lasertarget;
171
172         // move the laser dot to where the player is looking
173
174         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
175         offset = '0 0 26' + v_right*3;
176         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
177         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
178         if(trace_plane_normal != '0 0 0')
179                 e.angles = vectoangles(trace_plane_normal);
180         else
181                 e.angles = vectoangles(v_forward);
182 }
183
184 float CL_Weaponentity_CustomizeEntityForClient()
185 {
186         self.viewmodelforclient = self.owner;
187         if(other.classname == "spectator")
188                 if(other.enemy == self.owner)
189                         self.viewmodelforclient = other;
190         return TRUE;
191 }
192
193 .string weaponname;
194 void() CL_Weaponentity_Think =
195 {
196         self.nextthink = time;
197         if (intermission_running)
198                 self.frame = WFRAME_IDLE;
199         if (self.owner.weaponentity != self)
200         {
201                 remove(self);
202                 return;
203         }
204         if (self.owner.deadflag != DEAD_NO)
205         {
206                 self.model = "";
207                 return;
208         }
209         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
210         {
211                 self.cnt = self.owner.weapon;
212                 self.dmg = self.owner.modelindex;
213                 self.deadflag = self.owner.deadflag;
214                 if (self.owner.weaponname != "")
215                         setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
216                 else
217                         self.model = "";
218         }
219         self.effects = self.owner.effects | EF_LOWPRECISION;
220         self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
221
222         if (self.flags & FL_FLY)
223                 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
224                 self.effects = self.effects - (self.effects & EF_NODRAW);
225
226         if(self.owner.alpha >= 0)
227                 self.alpha = self.owner.alpha;
228         else
229                 self.alpha = 1;
230         self.colormap = self.owner.colormap;
231
232         self.angles = '0 0 0';
233         local float f;
234         f = 0;
235         if (self.state == WS_RAISE)
236                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
237         else if (self.state == WS_DROP)
238                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
239         else if (self.state == WS_CLEAR)
240                 f = 1;
241         self.angles_x = -90 * f * f;
242
243         // create or update the lasertarget entity
244         LaserTarget_Think();
245 };
246
247 void() CL_ExteriorWeaponentity_Think =
248 {
249         float tag_found;
250         self.nextthink = time;
251         if (self.owner.exteriorweaponentity != self)
252         {
253                 remove(self);
254                 return;
255         }
256         if (self.owner.deadflag != DEAD_NO)
257         {
258                 self.model = "";
259                 return;
260         }
261         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
262         {
263                 self.cnt = self.owner.weapon;
264                 self.dmg = self.owner.modelindex;
265                 self.deadflag = self.owner.deadflag;
266                 if (self.owner.weaponname != "")
267                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
268                 else
269                         self.model = "";
270
271                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
272                 {
273                         self.tag_index = tag_found;
274                         self.tag_entity = self.owner;
275                 }
276                 else
277                         setattachment(self, self.owner, "bip01 r hand");
278
279                 // if that didn't find a tag, hide the exterior weapon model
280                 if (!self.tag_index)
281                         self.model = "";
282         }
283         self.effects = self.owner.effects | EF_LOWPRECISION;
284         self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
285         if(self.owner.alpha >= 0)
286                 self.alpha = self.owner.alpha;
287         else
288                 self.alpha = 1;
289         self.colormap = self.owner.colormap;
290 };
291
292 // spawning weaponentity for client
293 void() CL_SpawnWeaponentity =
294 {
295         self.weaponentity = spawn();
296         self.weaponentity.solid = SOLID_NOT;
297         self.weaponentity.owner = self;
298         self.weaponentity.weaponentity = self.weaponentity;
299         setmodel(self.weaponentity, ""); // precision set when changed
300         self.weaponentity.origin = '0 0 0';
301         self.weaponentity.angles = '0 0 0';
302         self.weaponentity.viewmodelforclient = self;
303         self.weaponentity.flags = 0;
304         self.weaponentity.think = CL_Weaponentity_Think;
305         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
306         self.weaponentity.nextthink = time;
307         self.weaponentity.scale = 0.61;
308
309         self.exteriorweaponentity = spawn();
310         self.exteriorweaponentity.solid = SOLID_NOT;
311         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
312         self.exteriorweaponentity.owner = self;
313         self.exteriorweaponentity.origin = '0 0 0';
314         self.exteriorweaponentity.angles = '0 0 0';
315         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
316         self.exteriorweaponentity.nextthink = time;
317 };
318
319 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
320 {
321         local float itemcode, f;
322         local entity oldself;
323
324         if (wpn < WEP_FIRST || wpn > WEP_LAST)
325         {
326                 if (complain)
327                         sprint(self, "Invalid weapon\n");
328                 return FALSE;
329         }
330         itemcode = W_ItemCode(wpn);
331         if (cl.items & itemcode)
332         {
333                 if (andammo)
334                 {
335                         oldself = self;
336                         self = cl;
337                         f = weapon_action(wpn, WR_CHECKAMMO1);
338                         f = f + weapon_action(wpn, WR_CHECKAMMO2);
339                         self = oldself;
340                         if (!f)
341                         {
342                                 if (complain)
343                                         sprint(self, "You don't have any ammo for that weapon\n");
344                                 return FALSE;
345                         }
346                 }
347                 return TRUE;
348         }
349         if (complain)
350         {
351                 // DRESK - 3/16/07
352                 // Report Proper Weapon Status / Modified Weapon Ownership Message
353                 if(itemsInMap & itemcode)
354                         sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
355                 else
356                         sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
357         }
358         return FALSE;
359 };
360
361 // Weapon subs
362 void() w_clear =
363 {
364         if (self.weapon != -1)
365                 self.weapon = 0;
366         if (self.weaponentity)
367         {
368                 self.weaponentity.state = WS_CLEAR;
369                 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
370                 self.weaponentity.effects = 0;
371         }
372 };
373
374 void() w_ready =
375 {
376         if (self.weaponentity)
377         {
378                 self.weaponentity.state = WS_READY;
379                 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
380         }
381 };
382
383 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
384 float(entity e) w_getbestweapon
385 {// add new weapons here
386         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
387                 return WEP_ROCKET_LAUNCHER;
388         else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
389                 return WEP_NEX;
390         else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
391                 return WEP_HAGAR;
392         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
393                 return WEP_GRENADE_LAUNCHER;
394         else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
395                 return WEP_ELECTRO;
396         else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
397                 return WEP_CRYLINK;
398         else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
399                 return WEP_UZI;
400         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
401                 return WEP_SHOTGUN;
402         else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
403                 return WEP_LASER;
404         else
405                 return 0;
406 };
407
408 // Setup weapon for client (after this raise frame will be launched)
409 void(float windex, string wname, float hudammo) weapon_setup =
410 {
411         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
412         self.items = self.items | hudammo;
413
414         // the two weapon entities will notice this has changed and update their models
415         self.weapon = windex;
416         self.weaponname = wname;
417 };
418
419 // perform weapon to attack (weaponstate and attack_finished check is here)
420 float(float secondary, float attacktime) weapon_prepareattack =
421 {
422         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
423         {
424                 self.switchweapon = w_getbestweapon(self);
425                 if (self.switchweapon != self.weapon)
426                         self.cnt = self.weapon;
427                 return FALSE;
428         }
429         // don't fire if previous attack is not finished
430         if (ATTACK_FINISHED(self) > time)
431                 return FALSE;
432         // don't fire while changing weapon
433         if (self.weaponentity.state != WS_READY)
434                 return FALSE;
435         self.weaponentity.state = WS_INUSE;
436         // if the weapon hasn't been firing continuously, reset the timer
437         if (ATTACK_FINISHED(self) < time - frametime)
438                 ATTACK_FINISHED(self) = time;
439         ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
440         return TRUE;
441 };
442
443 void(float fr, float t, void() func) weapon_thinkf =
444 {
445         if (fr >= 0)
446         {
447                 if (self.weaponentity != world)
448                         self.weaponentity.frame = fr;
449         }
450
451         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
452         {
453                 backtrace("Tried to override initial weapon think function - should this really happen?");
454         }
455
456         if(g_runematch)
457         {
458                 if(self.runes & RUNE_SPEED)
459                 {
460                         if(self.runes & CURSE_SLOW)
461                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
462                         else
463                                 t = t * cvar("g_balance_rune_speed_atkrate");
464                 }
465                 else if(self.runes & CURSE_SLOW)
466                 {
467                         t = t * cvar("g_balance_curse_slow_atkrate");
468                 }
469         }
470
471         // VorteX: haste can be added here
472         if (self.weapon_nextthink < time - frametime)
473                 self.weapon_nextthink = time;
474         self.weapon_nextthink = self.weapon_nextthink + t;
475         self.weapon_think = func;
476
477         if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
478         if (t)
479         if (!self.crouch) // shoot anim stands up, this looks bad
480         {
481                 local vector anim;
482                 anim = self.anim_shoot;
483                 anim_z = anim_y / t;
484                 player_setanim(anim, FALSE, TRUE, TRUE);
485         }
486 };
487
488 void(float spd, vector org) weapon_boblayer1 =
489 {
490         // VorteX: haste can be added here
491 };
492
493 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
494 {
495         vector mdirection;
496         float mspeed;
497         float outspeed;
498         float nstyle;
499         vector outvelocity;
500
501         mdirection = normalize(mvelocity);
502         mspeed = vlen(mvelocity);
503
504         nstyle = cvar("g_projectiles_newton_style");
505         if(nstyle == 0)
506         {
507                 // absolute velocity
508                 outvelocity = mvelocity;
509         }
510         else if(nstyle == 1)
511         {
512                 // true Newtonian projectiles
513                 outvelocity = pvelocity + mvelocity;
514         }
515         else if(nstyle == 2)
516         {
517                 // true Newtonian projectiles with automatic aim adjustment
518                 //
519                 // solve: |outspeed * mdirection - pvelocity| = mspeed
520                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
521                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
522                 // PLUS SIGN!
523                 // not defined?
524                 // then...
525                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
526                 // velocity without mdirection component > mspeed
527                 // fire at smallest possible mspeed that works?
528                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
529
530                 float D;
531                 float p;
532                 float q;
533                 p = mdirection * pvelocity;
534                 q = pvelocity * pvelocity - mspeed * mspeed;
535                 D = p * p - q;
536                 if(D < 0)
537                 {
538                         //dprint("impossible shot, adjusting\n");
539                         D = 0;
540                 }
541                 outspeed = p + sqrt(D);
542                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
543                 outvelocity = mdirection * outspeed;
544         }
545         else if(nstyle == 3)
546         {
547                 // pseudo-Newtonian:
548                 outspeed = mspeed + mdirection * pvelocity;
549                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
550                 outvelocity = mdirection * outspeed;
551         }
552         else if(nstyle == 4)
553         {
554                 // tZorkian:
555                 outspeed = mspeed + vlen(pvelocity);
556                 outvelocity = mdirection * outspeed;
557         }
558         else
559                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
560
561         return outvelocity;
562 }
563
564 void(entity missile) W_SetupProjectileVelocity =
565 {
566         if(missile.owner == world)
567                 error("Unowned missile");
568
569         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
570
571
572 void(float wepcode, float minammo) weapon_register =
573 {
574         string s;
575         float itemcode;
576         s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
577         s = strcat(s, ftos(wepcode), " "); // impulse
578         s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
579         s = strcat(s, ftos(wepcode), " "); // self.weapon code
580
581         // ammo stat
582         itemcode = W_AmmoItemCode(wepcode);
583         if(itemcode == IT_SHELLS)
584                 s = strcat(s, "6 ");
585         else if(itemcode == IT_NAILS)
586                 s = strcat(s, "7 ");
587         else if(itemcode == IT_ROCKETS)
588                 s = strcat(s, "8 ");
589         else // if(itemcode == IT_CELLS)
590                 s = strcat(s, "9 ");
591
592         s = strcat(s, ftos(minammo), "\n");
593         //dprint(s);
594         stuffcmd(self, s);
595 }