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