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