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