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