]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/gamec/cl_weaponsystem.c
add g_navnodeedit
[divverent/nexuiz.git] / data / qcsrc / 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(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 void LaserTarget_Think()
31 {
32         entity e;
33         vector offset;
34         float uselaser;
35         uselaser = 0;
36
37         // list of weapons that will use the laser, and the options that enable it
38         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && cvar("g_homing_missile"))
39                 uselaser = 1;
40         // example
41         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_homing_electro"))
42         //      uselaser = 1;
43
44
45
46         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
47         if(!uselaser)
48         {
49                 // rocket launcher isn't selected, so no laser target.
50                 if(self.lasertarget != world)
51                 {
52                         remove(self.lasertarget);
53                         self.lasertarget = world;
54                 }
55                 return;
56         }
57
58         if(!self.lasertarget)
59         {
60                 // we don't have a lasertarget entity, so spawn one
61                 //bprint("create laser target\n");
62                 e = self.lasertarget = spawn();
63                 e.owner = self.owner;                   // Its owner is my owner
64                 e.classname = "laser_target";
65                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
66                 setmodel(e, "models/laser_dot.mdl");    // what it looks like
67                 e.scale = 1.25;                         // make it larger
68                 e.alpha = 0.25;                         // transparency
69                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
70                 e.effects = EF_FULLBRIGHT;
71                 // make it dynamically glow
72                 // you should avoid over-using this, as it can slow down the player's computer.
73                 e.glow_color = 251; // red color
74                 e.glow_size = 12;
75         }
76         else
77                 e = self.lasertarget;
78
79         // move the laser dot to where the player is looking
80
81         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
82         offset = '0 0 26' + v_right*3;
83         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
84         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
85         if(trace_plane_normal != '0 0 0')
86                 e.angles = vectoangles(trace_plane_normal);
87         else
88                 e.angles = vectoangles(v_forward);
89 }
90
91 void() CL_Weaponentity_Think =
92 {
93         self.nextthink = time;
94         if (self.owner.weaponentity != self)
95         {
96                 remove(self);
97                 return;
98         }
99         self.effects = self.owner.effects;
100         self.alpha = self.owner.alpha;
101
102         // create or update the lasertarget entity
103         LaserTarget_Think();
104 };
105
106 void() CL_ExteriorWeaponentity_Think =
107 {
108         self.nextthink = time;
109         if (self.owner.exteriorweaponentity != self)
110         {
111                 remove(self);
112                 return;
113         }
114         if (self.cnt != self.owner.weaponentity.modelindex || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
115         {
116                 self.cnt = self.owner.weaponentity.modelindex;
117                 self.dmg = self.owner.modelindex;
118                 self.deadflag = self.owner.deadflag;
119                 if (self.owner.deadflag) return;
120                 else if (self.owner.weapon == WEP_LASER) setmodel(self, "models/weapons/v_laser.md3");
121                 else if (self.owner.weapon == WEP_SHOTGUN) setmodel(self, "models/weapons/v_shotgun.md3");
122                 else if (self.owner.weapon == WEP_UZI) setmodel(self, "models/weapons/v_uzi.md3");
123                 else if (self.owner.weapon == WEP_GRENADE_LAUNCHER) setmodel(self, "models/weapons/v_gl.md3");
124                 else if (self.owner.weapon == WEP_ELECTRO) setmodel(self, "models/weapons/v_electro.md3");
125                 else if (self.owner.weapon == WEP_CRYLINK) setmodel(self, "models/weapons/v_crylink.md3");
126                 else if (self.owner.weapon == WEP_NEX) setmodel(self, "models/weapons/v_nex.md3");
127                 else if (self.owner.weapon == WEP_HAGAR) setmodel(self, "models/weapons/v_hagar.md3");
128                 else if (self.owner.weapon == WEP_ROCKET_LAUNCHER) setmodel(self, "models/weapons/v_rl.md3");
129                 setattachment(self, self.owner, "bip01 r hand");
130                 // if that didn't find a tag, hide the exterior weapon model
131                 if (!self.tag_index)
132                         self.model = "";
133         }
134         self.effects = self.owner.weaponentity.effects;
135 };
136
137 // spawning weaponentity for client
138 void() CL_SpawnWeaponentity =
139 {
140         if (self.weaponentity)
141         {
142                 w_clear();
143                 return;
144         }
145         self.weaponentity = spawn();
146         self.weaponentity.solid = SOLID_NOT;
147         self.weaponentity.owner = self;
148         self.weaponentity.weaponentity = self.weaponentity;
149         setmodel(self.weaponentity, "");
150         self.weaponentity.origin = '0 0 0';
151         self.weaponentity.angles = '0 0 0';
152         self.weaponentity.viewmodelforclient = self;
153         self.weaponentity.flags = 0;
154         self.weaponentity.think = CL_Weaponentity_Think;
155         self.weaponentity.nextthink = time;
156
157         self.exteriorweaponentity = spawn();
158         self.exteriorweaponentity.solid = SOLID_NOT;
159         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
160         self.exteriorweaponentity.owner = self;
161         self.exteriorweaponentity.origin = '0 0 0';
162         self.exteriorweaponentity.angles = '0 0 0';
163         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
164         self.exteriorweaponentity.nextthink = time;
165 };
166
167 // convertion from index (= impulse) to flag in .items
168 float(float index) weapon_translateindextoflag =
169 {
170         if (index == WEP_LASER)
171                 return IT_LASER;
172         else if (index == WEP_SHOTGUN)
173                 return IT_SHOTGUN;
174         else if (index == WEP_UZI)
175                 return IT_UZI;
176         else if (index == WEP_GRENADE_LAUNCHER)
177                 return IT_GRENADE_LAUNCHER;
178         else if (index == WEP_ELECTRO)
179                 return IT_ELECTRO;
180         else if (index == WEP_CRYLINK)
181                 return IT_CRYLINK;
182         else if (index == WEP_NEX)
183                 return IT_NEX;
184         else if (index == WEP_HAGAR)
185                 return IT_HAGAR;
186         else if (index == WEP_ROCKET_LAUNCHER)
187                 return IT_ROCKET_LAUNCHER;
188         else if (index == WEP_LASER)
189                 return IT_LASER;
190         else
191                 return 0;
192 };
193
194 float(entity cl, float wpn, float andammo) client_hasweapon =
195 {
196         local float itemcode;
197         local entity oldself;
198
199         weapon_hasammo = TRUE;
200         if (wpn < WEP_FIRST || wpn > WEP_LAST)
201                 return FALSE;
202         itemcode = weapon_translateindextoflag(wpn);
203         if (cl.items & itemcode)
204         {
205                 if (andammo)
206                 {
207                         oldself = self;
208                         self = cl;
209                         weapon_action(wpn, WR_CHECKAMMO);
210                         self = oldself;
211                         if (weapon_hasammo)
212                                 return TRUE;
213                         return FALSE;
214                 }
215                 return TRUE;
216         }
217         return FALSE;
218 };
219
220 // Weapon subs
221 void() w_clear =
222 {
223         weapon_action(self.weapon, WR_CLEAR);
224         if (self.weapon != -1)
225                 self.weapon = 0;
226         if (self.weaponentity)
227         {
228                 self.weaponentity.state = WS_CLEAR;
229                 setmodel(self.weaponentity, "");
230                 self.weaponentity.effects = 0;
231         }
232 };
233
234 void() w_ready =
235 {
236         self.weaponentity.state = WS_READY;
237         weapon_action(self.weapon, WR_IDLE);
238 };
239
240 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
241 float(entity e) w_getbestweapon
242 {// add new weapons here
243         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE))
244                 return WEP_ROCKET_LAUNCHER;
245         else if (client_hasweapon(e, WEP_NEX, TRUE))
246                 return WEP_NEX;
247         else if (client_hasweapon(e, WEP_HAGAR, TRUE))
248                 return WEP_HAGAR;
249         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE))
250                 return WEP_GRENADE_LAUNCHER;
251         else if (client_hasweapon(e, WEP_ELECTRO, TRUE))
252                 return WEP_ELECTRO;
253         else if (client_hasweapon(e, WEP_CRYLINK, TRUE))
254                 return WEP_CRYLINK;
255         else if (client_hasweapon(e, WEP_UZI, TRUE))
256                 return WEP_UZI;
257         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE))
258                 return WEP_SHOTGUN;
259         else if (client_hasweapon(e, WEP_LASER, FALSE))
260                 return WEP_LASER;
261         else
262                 return 0;
263 };
264
265 // Setup weapon for client (after this raise frame will be launched)
266 void(float windex, string wmodel, float hudammo) weapon_setup =
267 {
268         local string weaponmdl;
269
270         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
271         self.items = self.items | hudammo;
272
273         self.weapon = windex;
274
275         if (wmodel != "")
276         {
277                 weaponmdl = strzone(strcat("models/weapons/", wmodel));
278                 setmodel(self.weaponentity, weaponmdl);
279         }
280         // VorteX: update visible weapon
281         // CL_ViswepUpdate();
282 };
283
284 // shot direction
285 float WEAPON_MAXRELX = 14; // if more, shot can be spawned after wall surface (in empty worldspace) or inside other entity if client stands close to it
286 void(float x, float y, float z) weapon_shotdir =
287 {
288         makevectors(self.v_angle);
289         self.shotorg  = self.origin + self.view_ofs + v_forward*bound(0, x, WEAPON_MAXRELX) + v_right*(y + self.weaponentity.view_ofs_y) + v_up*z;
290         self.shotdir = aim(self, 1000);
291 };
292
293 // perform weapon to attack (weaponstate and attack_finished check is here)
294 void(float() checkfunc1, float() checkfunc2, void() firefunc, float atktime) weapon_prepareattack =
295 {
296         if (!checkfunc1())
297         {
298                 if (!checkfunc2())
299                         self.switchweapon = w_getbestweapon(self);
300                 return;
301         }
302         // Don't do shot if previos attack  not finished
303                 if (time < self.attack_finished)
304                         return;
305         // Can't do shot if changing weapon
306                 if (self.weaponentity.state != WS_READY)
307                         return;
308
309         self.attack_finished = time + atktime;
310         firefunc();
311 };
312
313 // perform weapon attack
314 void(float() checkfunc1, float() checkfunc2, void() firefunc) weapon_doattack
315 {
316         if (!checkfunc1())
317         {
318                 if (!checkfunc2())
319                         self.switchweapon = w_getbestweapon(self);
320                 return;
321         }
322         self.weaponentity.state = WS_INUSE;
323         firefunc();
324         weapon_action(self.weapon, WR_UPDATECOUNTS); // update ammo now
325 };
326
327 void(entity ent, float recoil) weapon_recoil =
328 {
329         ent.punchangle = (randomvec() + '-1 0 0')*recoil;
330         ent.punchangle_z = 0; // don't want roll
331         if (recoil > 3) // push back if large recoil
332                 ent.velocity = ent.velocity - normalize(ent.shotdir)*recoil*25;
333 };
334
335 void(float fr, float t, void() func) weapon_thinkf =
336 {
337         if (fr >= 0)
338         {
339                 if (self.weaponentity != world)
340                         self.weaponentity.frame = fr;
341         }
342
343         if(cvar("g_runematch"))
344         {
345                 if(self.runes & RUNE_SPEED)
346                 {
347                         if(self.runes & CURSE_SLOW)
348                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
349                         else
350                                 t = t * cvar("g_balance_rune_speed_atkrate");
351                 }
352                 else if(self.runes & CURSE_SLOW)
353                 {
354                         t = t * cvar("g_balance_curse_slow_atkrate");
355                 }
356         }
357
358         // VorteX: haste can be added here
359         self.weapon_nextthink = time + t;
360         self.weapon_think = func;
361 };
362
363 void(float spd, vector org) weapon_boblayer1 =
364 {
365         // VorteX: haste can be added here
366         self.weaponentity.pos1 =org;
367         self.weaponentity.lip = spd;
368 };