]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
weapons now have force settings
[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
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;
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 .string weaponname;
127 void() CL_Weaponentity_Think =
128 {
129         self.nextthink = time;
130         if (intermission_running)
131                 self.frame = WFRAME_IDLE;
132         if (self.owner.weaponentity != self)
133         {
134                 remove(self);
135                 return;
136         }
137         if (self.owner.deadflag != DEAD_NO)
138         {
139                 self.model = "";
140                 return;
141         }
142         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
143         {
144                 self.cnt = self.owner.weapon;
145                 self.dmg = self.owner.modelindex;
146                 self.deadflag = self.owner.deadflag;
147                 if (self.owner.weaponname != "")
148                         setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym"));
149                 else
150                         self.model = "";
151         }
152         self.effects = self.owner.effects;
153
154         if (self.flags & FL_FLY)
155                 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
156                 self.effects = self.effects - (self.effects & EF_NODRAW);
157
158         if(self.owner.alpha >= 0)
159                 self.alpha = self.owner.alpha;
160         else
161                 self.alpha = 1;
162         self.colormap = self.owner.colormap;
163
164         self.angles = '0 0 0';
165         local float f;
166         f = 0;
167         if (self.state == WS_RAISE)
168                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
169         else if (self.state == WS_DROP)
170                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
171         else if (self.state == WS_CLEAR)
172                 f = 1;
173         self.angles_x = -90 * f * f;
174
175         // create or update the lasertarget entity
176         LaserTarget_Think();
177 };
178
179 void() CL_ExteriorWeaponentity_Think =
180 {
181         self.nextthink = time;
182         if (self.owner.exteriorweaponentity != self)
183         {
184                 remove(self);
185                 return;
186         }
187         if (self.owner.deadflag != DEAD_NO)
188         {
189                 self.model = "";
190                 return;
191         }
192         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
193         {
194                 self.cnt = self.owner.weapon;
195                 self.dmg = self.owner.modelindex;
196                 self.deadflag = self.owner.deadflag;
197                 if (self.owner.weaponname != "")
198                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3"));
199                 else
200                         self.model = "";
201                 setattachment(self, self.owner, "bip01 r hand");
202                 // if that didn't find a tag, hide the exterior weapon model
203                 if (!self.tag_index)
204                         self.model = "";
205         }
206         self.effects = self.owner.effects;
207         self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
208         if(self.owner.alpha >= 0)
209                 self.alpha = self.owner.alpha;
210         else
211                 self.alpha = 1;
212         self.colormap = self.owner.colormap;
213 };
214
215 // spawning weaponentity for client
216 void() CL_SpawnWeaponentity =
217 {
218         self.weaponentity = spawn();
219         self.weaponentity.solid = SOLID_NOT;
220         self.weaponentity.owner = self;
221         self.weaponentity.weaponentity = self.weaponentity;
222         setmodel(self.weaponentity, "");
223         self.weaponentity.origin = '0 0 0';
224         self.weaponentity.angles = '0 0 0';
225         self.weaponentity.viewmodelforclient = self;
226         self.weaponentity.flags = 0;
227         self.weaponentity.think = CL_Weaponentity_Think;
228         self.weaponentity.nextthink = time;
229         self.weaponentity.scale = 0.61;
230
231         self.exteriorweaponentity = spawn();
232         self.exteriorweaponentity.solid = SOLID_NOT;
233         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
234         self.exteriorweaponentity.owner = self;
235         self.exteriorweaponentity.origin = '0 0 0';
236         self.exteriorweaponentity.angles = '0 0 0';
237         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
238         self.exteriorweaponentity.nextthink = time;
239 };
240
241 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
242 {
243         local float itemcode, f;
244         local entity oldself;
245
246         if (wpn < WEP_FIRST || wpn > WEP_LAST)
247         {
248                 if (complain)
249                         sprint(self, "Invalid weapon\n");
250                 return FALSE;
251         }
252         itemcode = W_ItemCode(wpn);
253         if (cl.items & itemcode)
254         {
255                 if (andammo)
256                 {
257                         oldself = self;
258                         self = cl;
259                         f = weapon_action(wpn, WR_CHECKAMMO1);
260                         f = f + weapon_action(wpn, WR_CHECKAMMO2);
261                         self = oldself;
262                         if (!f)
263                         {
264                                 if (complain)
265                                         sprint(self, "You don't have any ammo for that weapon\n");
266                                 return FALSE;
267                         }
268                 }
269                 return TRUE;
270         }
271         if (complain)
272                 sprint(self, "You don't own that weapon\n");
273         return FALSE;
274 };
275
276 // Weapon subs
277 void() w_clear =
278 {
279         if (self.weapon != -1)
280                 self.weapon = 0;
281         if (self.weaponentity)
282         {
283                 self.weaponentity.state = WS_CLEAR;
284                 setmodel(self.weaponentity, "");
285                 self.weaponentity.effects = 0;
286         }
287 };
288
289 void() w_ready =
290 {
291         if (self.weaponentity)
292         {
293                 self.weaponentity.state = WS_READY;
294                 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
295         }
296 };
297
298 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
299 float(entity e) w_getbestweapon
300 {// add new weapons here
301         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
302                 return WEP_ROCKET_LAUNCHER;
303         else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
304                 return WEP_NEX;
305         else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
306                 return WEP_HAGAR;
307         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
308                 return WEP_GRENADE_LAUNCHER;
309         else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
310                 return WEP_ELECTRO;
311         else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
312                 return WEP_CRYLINK;
313         else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
314                 return WEP_UZI;
315         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
316                 return WEP_SHOTGUN;
317         else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
318                 return WEP_LASER;
319         else
320                 return 0;
321 };
322
323 // Setup weapon for client (after this raise frame will be launched)
324 void(float windex, string wname, float hudammo) weapon_setup =
325 {
326         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
327         self.items = self.items | hudammo;
328
329         // the two weapon entities will notice this has changed and update their models
330         self.weapon = windex;
331         self.weaponname = wname;
332
333         // might fire faster after switch
334         self.attack_finished = min(max(time, self.attack_finished_old + 1), self.attack_finished);
335 };
336
337 // perform weapon to attack (weaponstate and attack_finished check is here)
338 float(float secondary, float attacktime) weapon_prepareattack =
339 {
340         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
341         {
342                 self.switchweapon = w_getbestweapon(self);
343                 if (self.switchweapon != self.weapon)
344                         self.cnt = self.weapon;
345                 return FALSE;
346         }
347         // don't fire if previous attack is not finished
348         if (self.attack_finished > time)
349                 return FALSE;
350         // don't fire while changing weapon
351         if (self.weaponentity.state != WS_READY)
352                 return FALSE;
353         self.weaponentity.state = WS_INUSE;
354         self.attack_finished_old = self.attack_finished;
355         self.attack_finished = max(time, self.attack_finished + attacktime);
356         return TRUE;
357 };
358
359 void(float fr, float t, void() func) weapon_thinkf =
360 {
361         if (fr >= 0)
362         {
363                 if (self.weaponentity != world)
364                         self.weaponentity.frame = fr;
365         }
366
367         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
368         {
369                 backtrace("Tried to override initial weapon think function - should this really happen?");
370         }
371
372         if(cvar("g_runematch"))
373         {
374                 if(self.runes & RUNE_SPEED)
375                 {
376                         if(self.runes & CURSE_SLOW)
377                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
378                         else
379                                 t = t * cvar("g_balance_rune_speed_atkrate");
380                 }
381                 else if(self.runes & CURSE_SLOW)
382                 {
383                         t = t * cvar("g_balance_curse_slow_atkrate");
384                 }
385         }
386
387         // VorteX: haste can be added here
388         self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
389         self.weapon_think = func;
390 };
391
392 void(float spd, vector org) weapon_boblayer1 =
393 {
394         // VorteX: haste can be added here
395 };
396
397 void(entity missile) W_SetupProjectileVelocity =
398 {
399         vector pvelocity;
400         vector mdirection;
401         float mspeed;
402         float outspeed;
403         float nstyle;
404         vector mvelocity;
405         vector outvelocity;
406
407         if(missile.owner == world)
408                 error("Unowned missile");
409         pvelocity = missile.owner.velocity;
410         mvelocity = missile.velocity;
411         mdirection = normalize(mvelocity);
412         mspeed = vlen(mvelocity);
413
414         nstyle = cvar("g_projectiles_newton_style");
415         if(nstyle == 0)
416         {
417                 // absolute velocity
418                 outvelocity = mvelocity;
419         }
420         else if(nstyle == 1)
421         {
422                 // true Newtonian projectiles
423                 outvelocity = pvelocity + mvelocity;
424         }
425         else if(nstyle == 2)
426         {
427                 // true Newtonian projectiles with automatic aim adjustment
428                 //
429                 // solve: |outspeed * mdirection - pvelocity| = mspeed
430                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
431                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
432                 // PLUS SIGN!
433                 // not defined?
434                 // then...
435                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
436                 // velocity without mdirection component > mspeed
437                 // fire at smallest possible mspeed that works?
438                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
439
440                 float D;
441                 float p;
442                 float q;
443                 p = mdirection * pvelocity;
444                 q = pvelocity * pvelocity - mspeed * mspeed;
445                 D = p * p - q;
446                 if(D < 0)
447                 {
448                         //dprint("impossible shot, adjusting\n");
449                         D = 0;
450                 }
451                 outspeed = p + sqrt(D);
452                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
453                 outvelocity = mdirection * outspeed;
454         }
455         else if(nstyle == 3)
456         {
457                 // pseudo-Newtonian:
458                 outspeed = mspeed + mdirection * pvelocity;
459                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
460                 outvelocity = mdirection * outspeed;
461         }
462         else if(nstyle == 4)
463         {
464                 // tZorkian:
465                 outspeed = mspeed + vlen(pvelocity);
466                 outvelocity = mdirection * outspeed;
467         }
468         else
469                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
470
471         //dprint("Adjusted from ", vtos(missile.velocity));
472         missile.velocity = outvelocity;
473         //dprint(" to ", vtos(missile.velocity), "\n");
474 }