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