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