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