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