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