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