]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/cl_weaponsystem.c
players now keep their speed (not optimal: they should keep their angle relative...
[divverent/nexuiz.git] / data / qcsrc / server / gamec / cl_weaponsystem.c
1 /*
2 ===========================================================================
3
4   CLIENT WEAPONSYSTEM CODE
5   Bring back W_Weaponframe
6
7 ===========================================================================
8 */
9
10 vector() W_TrueAim = {
11
12         traceline_hitcorpse(self,self.origin + self.view_ofs,self.origin + self.view_ofs + v_forward * 4096,FALSE,self);
13
14         if ((self.weapon == WEP_NEX || self.weapon == WEP_SHOTGUN || self.weapon == WEP_UZI) && cvar("g_antilag"))
15         {
16                 // if aiming at a player and the original trace won't hit that player
17                 // anymore, try aiming at the player's new position
18
19                 if(self.cursor_trace_ent)
20                 if(self.cursor_trace_ent.takedamage)
21                 if(trace_ent != self.cursor_trace_ent) {
22                         return self.cursor_trace_ent.origin;
23                 }
24
25         }
26
27         return trace_endpos;
28 }
29
30 // this function allows you to spawn projectiles ahead of the player so they appear to come out of
31 // the muzzle properly, but won't put shots inside walls if you're too close.
32 // make sure you call makevectors first (FIXME?)
33 vector W_MuzzleOrigin (entity ent, vector vecs)
34 {
35         vector startorg;
36         vector idealorg;
37
38         startorg = ent.origin + ent.view_ofs - '0 0 8';
39         idealorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
40
41         traceline_hitcorpse (ent, startorg, idealorg, MOVE_NORMAL, ent);
42
43 // if obstructed, back off a bit so the shot will definitely hit it
44         if (trace_fraction < 1.0)
45         {
46         // FIXME! this whole thing messes up W_TrueAim... if you're up against a wall, the trueaim and the muzzle
47         // origin will be about the same distance from the player, causing the bullets to fly out at a near 90
48         // degree angle
49 //              trace_endpos = trace_endpos - normalize (idealorg - startorg);
50
51
52         // nasty placeholder (well I admit you don't really notice it all... especially since the
53         // weapon model sinking into the wall provides such a nice distraction)
54                 trace_endpos = startorg;
55         }
56
57         return trace_endpos;
58 }
59
60 void LaserTarget_Think()
61 {
62         entity e;
63         vector offset;
64         float uselaser;
65         uselaser = 0;
66
67         // list of weapons that will use the laser, and the options that enable it
68         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && cvar("g_laserguided_missile"))
69                 uselaser = 1;
70         // example
71         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
72         //      uselaser = 1;
73
74
75
76         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
77         if(!uselaser)
78         {
79                 // rocket launcher isn't selected, so no laser target.
80                 if(self.lasertarget != world)
81                 {
82                         remove(self.lasertarget);
83                         self.lasertarget = world;
84                 }
85                 return;
86         }
87
88         if(!self.lasertarget)
89         {
90                 // we don't have a lasertarget entity, so spawn one
91                 //bprint("create laser target\n");
92                 e = self.lasertarget = spawn();
93                 e.owner = self.owner;                   // Its owner is my owner
94                 e.classname = "laser_target";
95                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
96                 setmodel(e, "models/laser_dot.mdl");    // what it looks like
97                 e.scale = 1.25;                         // make it larger
98                 e.alpha = 0.25;                         // transparency
99                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
100                 e.effects = EF_FULLBRIGHT;
101                 // make it dynamically glow
102                 // you should avoid over-using this, as it can slow down the player's computer.
103                 e.glow_color = 251; // red color
104                 e.glow_size = 12;
105         }
106         else
107                 e = self.lasertarget;
108
109         // move the laser dot to where the player is looking
110
111         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
112         offset = '0 0 26' + v_right*3;
113         traceline(self.owner.origin + offset, self.owner.origin + offset + v_forward * 2048, FALSE, self); // trace forward until you hit something, like a player or wall
114         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
115         if(trace_plane_normal != '0 0 0')
116                 e.angles = vectoangles(trace_plane_normal);
117         else
118                 e.angles = vectoangles(v_forward);
119 }
120
121 void() CL_Weaponentity_Think =
122 {
123         self.nextthink = time;
124         if (self.owner.weaponentity != self)
125         {
126                 remove(self);
127                 return;
128         }
129
130         if (self.flags & FL_FLY)
131                 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
132                 self.effects = self.owner.effects - (self.owner.effects & EF_NODRAW);
133         else
134                 self.effects = self.owner.effects;
135         
136         self.alpha = self.owner.alpha;
137
138         // create or update the lasertarget entity
139         LaserTarget_Think();
140 };
141
142 void() CL_ExteriorWeaponentity_Think =
143 {
144         self.nextthink = time;
145         if(self.owner.deadflag != DEAD_NO)
146         {
147                 self.model = "";
148                 return;
149         }
150         if (self.owner.exteriorweaponentity != self)
151         {
152                 remove(self);
153                 return;
154         }
155         if (self.cnt != self.owner.weaponentity.modelindex || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
156         {
157                 self.cnt = self.owner.weaponentity.modelindex;
158                 self.dmg = self.owner.modelindex;
159                 self.deadflag = self.owner.deadflag;
160                 if (self.owner.deadflag) return;
161                 else if (self.owner.weapon == WEP_LASER) setmodel(self, "models/weapons/v_laser.md3");
162                 else if (self.owner.weapon == WEP_SHOTGUN) setmodel(self, "models/weapons/v_shotgun.md3");
163                 else if (self.owner.weapon == WEP_UZI) setmodel(self, "models/weapons/v_uzi.md3");
164                 else if (self.owner.weapon == WEP_GRENADE_LAUNCHER) setmodel(self, "models/weapons/v_gl.md3");
165                 else if (self.owner.weapon == WEP_ELECTRO) setmodel(self, "models/weapons/v_electro.md3");
166                 else if (self.owner.weapon == WEP_CRYLINK) setmodel(self, "models/weapons/v_crylink.md3");
167                 else if (self.owner.weapon == WEP_NEX) setmodel(self, "models/weapons/v_nex.md3");
168                 else if (self.owner.weapon == WEP_HAGAR) setmodel(self, "models/weapons/v_hagar.md3");
169                 else if (self.owner.weapon == WEP_ROCKET_LAUNCHER) setmodel(self, "models/weapons/v_rl.md3");
170                 setattachment(self, self.owner, "bip01 r hand");
171                 // if that didn't find a tag, hide the exterior weapon model
172                 if (!self.tag_index)
173                         self.model = "";
174         }
175         self.effects = self.owner.weaponentity.effects;
176 };
177
178 // spawning weaponentity for client
179 void() CL_SpawnWeaponentity =
180 {
181         if (self.weaponentity)
182         {
183                 w_clear();
184                 return;
185         }
186         self.weaponentity = spawn();
187         self.weaponentity.solid = SOLID_NOT;
188         self.weaponentity.owner = self;
189         self.weaponentity.weaponentity = self.weaponentity;
190         setmodel(self.weaponentity, "");
191         self.weaponentity.origin = '0 0 0';
192         self.weaponentity.angles = '0 0 0';
193         self.weaponentity.viewmodelforclient = self;
194         self.weaponentity.flags = 0;
195         self.weaponentity.think = CL_Weaponentity_Think;
196         self.weaponentity.nextthink = time;
197
198         self.exteriorweaponentity = spawn();
199         self.exteriorweaponentity.solid = SOLID_NOT;
200         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
201         self.exteriorweaponentity.owner = self;
202         self.exteriorweaponentity.origin = '0 0 0';
203         self.exteriorweaponentity.angles = '0 0 0';
204         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
205         self.exteriorweaponentity.nextthink = time;
206 };
207
208 // convertion from index (= impulse) to flag in .items
209 float(float index) weapon_translateindextoflag =
210 {
211         if (index == WEP_LASER)
212                 return IT_LASER;
213         else if (index == WEP_SHOTGUN)
214                 return IT_SHOTGUN;
215         else if (index == WEP_UZI)
216                 return IT_UZI;
217         else if (index == WEP_GRENADE_LAUNCHER)
218                 return IT_GRENADE_LAUNCHER;
219         else if (index == WEP_ELECTRO)
220                 return IT_ELECTRO;
221         else if (index == WEP_CRYLINK)
222                 return IT_CRYLINK;
223         else if (index == WEP_NEX)
224                 return IT_NEX;
225         else if (index == WEP_HAGAR)
226                 return IT_HAGAR;
227         else if (index == WEP_ROCKET_LAUNCHER)
228                 return IT_ROCKET_LAUNCHER;
229         else if (index == WEP_LASER)
230                 return IT_LASER;
231         else
232                 return 0;
233 };
234
235 float(entity cl, float wpn, float andammo) client_hasweapon =
236 {
237         local float itemcode;
238         local entity oldself;
239
240         weapon_hasammo = TRUE;
241         if (wpn < WEP_FIRST || wpn > WEP_LAST)
242                 return FALSE;
243         itemcode = weapon_translateindextoflag(wpn);
244         if (cl.items & itemcode)
245         {
246                 if (andammo)
247                 {
248                         oldself = self;
249                         self = cl;
250                         weapon_action(wpn, WR_CHECKAMMO);
251                         self = oldself;
252                         if (weapon_hasammo)
253                                 return TRUE;
254                         return FALSE;
255                 }
256                 return TRUE;
257         }
258         return FALSE;
259 };
260
261 // Weapon subs
262 void() w_clear =
263 {
264         weapon_action(self.weapon, WR_CLEAR);
265         if (self.weapon != -1)
266                 self.weapon = 0;
267         if (self.weaponentity)
268         {
269                 self.weaponentity.state = WS_CLEAR;
270                 setmodel(self.weaponentity, "");
271                 self.weaponentity.effects = 0;
272         }
273 };
274
275 void() w_ready =
276 {
277         self.weaponentity.state = WS_READY;
278         weapon_action(self.weapon, WR_IDLE);
279 };
280
281 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
282 float(entity e) w_getbestweapon
283 {// add new weapons here
284         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE))
285                 return WEP_ROCKET_LAUNCHER;
286         else if (client_hasweapon(e, WEP_NEX, TRUE))
287                 return WEP_NEX;
288         else if (client_hasweapon(e, WEP_HAGAR, TRUE))
289                 return WEP_HAGAR;
290         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE))
291                 return WEP_GRENADE_LAUNCHER;
292         else if (client_hasweapon(e, WEP_ELECTRO, TRUE))
293                 return WEP_ELECTRO;
294         else if (client_hasweapon(e, WEP_CRYLINK, TRUE))
295                 return WEP_CRYLINK;
296         else if (client_hasweapon(e, WEP_UZI, TRUE))
297                 return WEP_UZI;
298         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE))
299                 return WEP_SHOTGUN;
300         else if (client_hasweapon(e, WEP_LASER, FALSE))
301                 return WEP_LASER;
302         else
303                 return 0;
304 };
305
306 // Setup weapon for client (after this raise frame will be launched)
307 void(float windex, string wmodel, float hudammo) weapon_setup =
308 {
309         local string weaponmdl;
310
311         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
312         self.items = self.items | hudammo;
313
314         self.weapon = windex;
315
316         if (wmodel != "")
317         {
318                 weaponmdl = strzone(strcat("models/weapons/", wmodel));
319                 setmodel(self.weaponentity, weaponmdl);
320         }
321         // VorteX: update visible weapon
322         // CL_ViswepUpdate();
323 };
324
325 // perform weapon to attack (weaponstate and attack_finished check is here)
326 void(float() checkfunc1, float() checkfunc2, void() firefunc, float atktime) weapon_prepareattack =
327 {
328         if (!checkfunc1())
329         {
330                 if (!checkfunc2())
331                 {
332                         self.switchweapon = w_getbestweapon(self);
333                         if (self.switchweapon != self.weapon)
334                                 self.cnt = self.weapon;
335                 }
336                 return;
337         }
338         // Don't do shot if previos attack  not finished
339                 if (time < self.attack_finished)
340                         return;
341         // Can't do shot if changing weapon
342                 if (self.weaponentity.state != WS_READY)
343                         return;
344
345         self.attack_finished = time + atktime;
346         firefunc();
347 };
348
349 // perform weapon attack
350 void(float() checkfunc1, float() checkfunc2, void() firefunc) weapon_doattack
351 {
352         if (!checkfunc1())
353         {
354                 if (!checkfunc2())
355                 {
356                         self.switchweapon = w_getbestweapon(self);
357                         if (self.switchweapon != self.weapon)
358                                 self.cnt = self.weapon;
359                 }
360                 return;
361         }
362         self.weaponentity.state = WS_INUSE;
363         firefunc();
364         if (cvar("g_norecoil"))
365                 self.punchangle = '0 0 0';
366         weapon_action(self.weapon, WR_UPDATECOUNTS); // update ammo now
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(cvar("g_runematch"))
378         {
379                 if(self.runes & RUNE_SPEED)
380                 {
381                         if(self.runes & CURSE_SLOW)
382                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
383                         else
384                                 t = t * cvar("g_balance_rune_speed_atkrate");
385                 }
386                 else if(self.runes & CURSE_SLOW)
387                 {
388                         t = t * cvar("g_balance_curse_slow_atkrate");
389                 }
390         }
391
392         // VorteX: haste can be added here
393         self.weapon_nextthink = time + t;
394         self.weapon_think = func;
395 };
396
397 void(float spd, vector org) weapon_boblayer1 =
398 {
399         // VorteX: haste can be added here
400         self.weaponentity.pos1 =org;
401         self.weaponentity.lip = spd;
402 };