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