]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/cl_weaponsystem.c
Stuff now shoots out of what appears to be the actual muzzles of the weapons. I modif...
[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         self.effects = self.owner.effects;
130         self.alpha = self.owner.alpha;
131
132         // create or update the lasertarget entity
133         LaserTarget_Think();
134 };
135
136 void() CL_ExteriorWeaponentity_Think =
137 {
138         self.nextthink = time;
139         if(self.owner.deadflag != DEAD_NO)
140         {
141                 self.model = "";
142                 return;
143         }
144         if (self.owner.exteriorweaponentity != self)
145         {
146                 remove(self);
147                 return;
148         }
149         if (self.cnt != self.owner.weaponentity.modelindex || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
150         {
151                 self.cnt = self.owner.weaponentity.modelindex;
152                 self.dmg = self.owner.modelindex;
153                 self.deadflag = self.owner.deadflag;
154                 if (self.owner.deadflag) return;
155                 else if (self.owner.weapon == WEP_LASER) setmodel(self, "models/weapons/v_laser.md3");
156                 else if (self.owner.weapon == WEP_SHOTGUN) setmodel(self, "models/weapons/v_shotgun.md3");
157                 else if (self.owner.weapon == WEP_UZI) setmodel(self, "models/weapons/v_uzi.md3");
158                 else if (self.owner.weapon == WEP_GRENADE_LAUNCHER) setmodel(self, "models/weapons/v_gl.md3");
159                 else if (self.owner.weapon == WEP_ELECTRO) setmodel(self, "models/weapons/v_electro.md3");
160                 else if (self.owner.weapon == WEP_CRYLINK) setmodel(self, "models/weapons/v_crylink.md3");
161                 else if (self.owner.weapon == WEP_NEX) setmodel(self, "models/weapons/v_nex.md3");
162                 else if (self.owner.weapon == WEP_HAGAR) setmodel(self, "models/weapons/v_hagar.md3");
163                 else if (self.owner.weapon == WEP_ROCKET_LAUNCHER) setmodel(self, "models/weapons/v_rl.md3");
164                 setattachment(self, self.owner, "bip01 r hand");
165                 // if that didn't find a tag, hide the exterior weapon model
166                 if (!self.tag_index)
167                         self.model = "";
168         }
169         self.effects = self.owner.weaponentity.effects;
170 };
171
172 // spawning weaponentity for client
173 void() CL_SpawnWeaponentity =
174 {
175         if (self.weaponentity)
176         {
177                 w_clear();
178                 return;
179         }
180         self.weaponentity = spawn();
181         self.weaponentity.solid = SOLID_NOT;
182         self.weaponentity.owner = self;
183         self.weaponentity.weaponentity = self.weaponentity;
184         setmodel(self.weaponentity, "");
185         self.weaponentity.origin = '0 0 0';
186         self.weaponentity.angles = '0 0 0';
187         self.weaponentity.viewmodelforclient = self;
188         self.weaponentity.flags = 0;
189         self.weaponentity.think = CL_Weaponentity_Think;
190         self.weaponentity.nextthink = time;
191
192         self.exteriorweaponentity = spawn();
193         self.exteriorweaponentity.solid = SOLID_NOT;
194         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
195         self.exteriorweaponentity.owner = self;
196         self.exteriorweaponentity.origin = '0 0 0';
197         self.exteriorweaponentity.angles = '0 0 0';
198         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
199         self.exteriorweaponentity.nextthink = time;
200 };
201
202 // convertion from index (= impulse) to flag in .items
203 float(float index) weapon_translateindextoflag =
204 {
205         if (index == WEP_LASER)
206                 return IT_LASER;
207         else if (index == WEP_SHOTGUN)
208                 return IT_SHOTGUN;
209         else if (index == WEP_UZI)
210                 return IT_UZI;
211         else if (index == WEP_GRENADE_LAUNCHER)
212                 return IT_GRENADE_LAUNCHER;
213         else if (index == WEP_ELECTRO)
214                 return IT_ELECTRO;
215         else if (index == WEP_CRYLINK)
216                 return IT_CRYLINK;
217         else if (index == WEP_NEX)
218                 return IT_NEX;
219         else if (index == WEP_HAGAR)
220                 return IT_HAGAR;
221         else if (index == WEP_ROCKET_LAUNCHER)
222                 return IT_ROCKET_LAUNCHER;
223         else if (index == WEP_LASER)
224                 return IT_LASER;
225         else
226                 return 0;
227 };
228
229 float(entity cl, float wpn, float andammo) client_hasweapon =
230 {
231         local float itemcode;
232         local entity oldself;
233
234         weapon_hasammo = TRUE;
235         if (wpn < WEP_FIRST || wpn > WEP_LAST)
236                 return FALSE;
237         itemcode = weapon_translateindextoflag(wpn);
238         if (cl.items & itemcode)
239         {
240                 if (andammo)
241                 {
242                         oldself = self;
243                         self = cl;
244                         weapon_action(wpn, WR_CHECKAMMO);
245                         self = oldself;
246                         if (weapon_hasammo)
247                                 return TRUE;
248                         return FALSE;
249                 }
250                 return TRUE;
251         }
252         return FALSE;
253 };
254
255 // Weapon subs
256 void() w_clear =
257 {
258         weapon_action(self.weapon, WR_CLEAR);
259         if (self.weapon != -1)
260                 self.weapon = 0;
261         if (self.weaponentity)
262         {
263                 self.weaponentity.state = WS_CLEAR;
264                 setmodel(self.weaponentity, "");
265                 self.weaponentity.effects = 0;
266         }
267 };
268
269 void() w_ready =
270 {
271         self.weaponentity.state = WS_READY;
272         weapon_action(self.weapon, WR_IDLE);
273 };
274
275 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
276 float(entity e) w_getbestweapon
277 {// add new weapons here
278         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE))
279                 return WEP_ROCKET_LAUNCHER;
280         else if (client_hasweapon(e, WEP_NEX, TRUE))
281                 return WEP_NEX;
282         else if (client_hasweapon(e, WEP_HAGAR, TRUE))
283                 return WEP_HAGAR;
284         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE))
285                 return WEP_GRENADE_LAUNCHER;
286         else if (client_hasweapon(e, WEP_ELECTRO, TRUE))
287                 return WEP_ELECTRO;
288         else if (client_hasweapon(e, WEP_CRYLINK, TRUE))
289                 return WEP_CRYLINK;
290         else if (client_hasweapon(e, WEP_UZI, TRUE))
291                 return WEP_UZI;
292         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE))
293                 return WEP_SHOTGUN;
294         else if (client_hasweapon(e, WEP_LASER, FALSE))
295                 return WEP_LASER;
296         else
297                 return 0;
298 };
299
300 // Setup weapon for client (after this raise frame will be launched)
301 void(float windex, string wmodel, float hudammo) weapon_setup =
302 {
303         local string weaponmdl;
304
305         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
306         self.items = self.items | hudammo;
307
308         self.weapon = windex;
309
310         if (wmodel != "")
311         {
312                 weaponmdl = strzone(strcat("models/weapons/", wmodel));
313                 setmodel(self.weaponentity, weaponmdl);
314         }
315         // VorteX: update visible weapon
316         // CL_ViswepUpdate();
317 };
318
319 // perform weapon to attack (weaponstate and attack_finished check is here)
320 void(float() checkfunc1, float() checkfunc2, void() firefunc, float atktime) weapon_prepareattack =
321 {
322         if (!checkfunc1())
323         {
324                 if (!checkfunc2())
325                 {
326                         self.switchweapon = w_getbestweapon(self);
327                         if (self.switchweapon != self.weapon)
328                                 self.cnt = self.weapon;
329                 }
330                 return;
331         }
332         // Don't do shot if previos attack  not finished
333                 if (time < self.attack_finished)
334                         return;
335         // Can't do shot if changing weapon
336                 if (self.weaponentity.state != WS_READY)
337                         return;
338
339         self.attack_finished = time + atktime;
340         firefunc();
341 };
342
343 // perform weapon attack
344 void(float() checkfunc1, float() checkfunc2, void() firefunc) weapon_doattack
345 {
346         if (!checkfunc1())
347         {
348                 if (!checkfunc2())
349                 {
350                         self.switchweapon = w_getbestweapon(self);
351                         if (self.switchweapon != self.weapon)
352                                 self.cnt = self.weapon;
353                 }
354                 return;
355         }
356         self.weaponentity.state = WS_INUSE;
357         firefunc();
358         if (cvar("g_norecoil"))
359                 self.punchangle = '0 0 0';
360         weapon_action(self.weapon, WR_UPDATECOUNTS); // update ammo now
361 };
362
363 void(float fr, float t, void() func) weapon_thinkf =
364 {
365         if (fr >= 0)
366         {
367                 if (self.weaponentity != world)
368                         self.weaponentity.frame = fr;
369         }
370
371         if(cvar("g_runematch"))
372         {
373                 if(self.runes & RUNE_SPEED)
374                 {
375                         if(self.runes & CURSE_SLOW)
376                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
377                         else
378                                 t = t * cvar("g_balance_rune_speed_atkrate");
379                 }
380                 else if(self.runes & CURSE_SLOW)
381                 {
382                         t = t * cvar("g_balance_curse_slow_atkrate");
383                 }
384         }
385
386         // VorteX: haste can be added here
387         self.weapon_nextthink = time + t;
388         self.weapon_think = func;
389 };
390
391 void(float spd, vector org) weapon_boblayer1 =
392 {
393         // VorteX: haste can be added here
394         self.weaponentity.pos1 =org;
395         self.weaponentity.lip = spd;
396 };