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