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