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