]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
tag_weapon? No, Q3A uses that for some RANDOM PLACE INSIDE THE MODEL. Use tag_attachw...
[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         traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
27         trueaimpoint = trace_endpos;
28
29         if (cvar("g_shootfromeye"))
30                 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x;
31         else if (cvar("g_shootfromcenter"))
32                 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + '0 0 1' * vecs_z;
33         else
34                 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
35         w_shotdir = normalize(trueaimpoint - w_shotorg);
36
37         // explanation of g_antilag:
38         // if client reports it was aiming at a player, and the serverside trace
39         // says it would miss, change the aim point to the player's new origin,
40         // but only if the shot at the player's new origin would hit of course
41         //
42         // FIXME: a much better method for bullet weapons would be to leave a
43         // trail of lagged 'ghosts' behind players, and see if the bullet hits the
44         // ghost corresponding to this player's ping time, and if so it would do
45         // damage to the real player
46         if (antilag)
47         if (self.cursor_trace_ent)                 // client was aiming at someone
48         if (self.cursor_trace_ent.takedamage)      // and that person is killable
49         if (cvar("g_antilag"))
50         {
51                 // verify that the shot would miss without antilag
52                 // (avoids an issue where guns would always shoot at their origin)
53                 traceline_hitcorpse(self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
54                 if (!trace_ent.takedamage)
55                 {
56                         // verify that the shot would hit if altered
57                         traceline_hitcorpse(self, w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
58                         if (trace_ent == self.cursor_trace_ent)
59                                 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
60                 }
61         }
62
63         if (!g_norecoil)
64                 self.punchangle_x = recoil * -1;
65
66         if (snd != "")
67                 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
68
69         if (self.items & IT_STRENGTH)
70         if (!g_minstagib)
71                 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
72 };
73
74 void LaserTarget_Think()
75 {
76         entity e;
77         vector offset;
78         float uselaser;
79         uselaser = 0;
80
81         // list of weapons that will use the laser, and the options that enable it
82         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
83                 uselaser = 1;
84         // example
85         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
86         //      uselaser = 1;
87
88
89
90         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
91         if(!uselaser)
92         {
93                 // rocket launcher isn't selected, so no laser target.
94                 if(self.lasertarget != world)
95                 {
96                         remove(self.lasertarget);
97                         self.lasertarget = world;
98                 }
99                 return;
100         }
101
102         if(!self.lasertarget)
103         {
104                 // we don't have a lasertarget entity, so spawn one
105                 //bprint("create laser target\n");
106                 e = self.lasertarget = spawn();
107                 e.owner = self.owner;                   // Its owner is my owner
108                 e.classname = "laser_target";
109                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
110                 setmodel(e, "models/laser_dot.mdl");    // what it looks like, precision set below
111                 e.scale = 1.25;                         // make it larger
112                 e.alpha = 0.25;                         // transparency
113                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
114                 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
115                 // make it dynamically glow
116                 // you should avoid over-using this, as it can slow down the player's computer.
117                 e.glow_color = 251; // red color
118                 e.glow_size = 12;
119         }
120         else
121                 e = self.lasertarget;
122
123         // move the laser dot to where the player is looking
124
125         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
126         offset = '0 0 26' + v_right*3;
127         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
128         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
129         if(trace_plane_normal != '0 0 0')
130                 e.angles = vectoangles(trace_plane_normal);
131         else
132                 e.angles = vectoangles(v_forward);
133 }
134
135 float CL_Weaponentity_CustomizeEntityForClient()
136 {
137         self.viewmodelforclient = self.owner;
138         if(other.classname == "spectator")
139                 if(other.enemy == self.owner)
140                         self.viewmodelforclient = other;
141         return TRUE;
142 }
143
144 .string weaponname;
145 void() CL_Weaponentity_Think =
146 {
147         self.nextthink = time;
148         if (intermission_running)
149                 self.frame = WFRAME_IDLE;
150         if (self.owner.weaponentity != self)
151         {
152                 remove(self);
153                 return;
154         }
155         if (self.owner.deadflag != DEAD_NO)
156         {
157                 self.model = "";
158                 return;
159         }
160         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
161         {
162                 self.cnt = self.owner.weapon;
163                 self.dmg = self.owner.modelindex;
164                 self.deadflag = self.owner.deadflag;
165                 if (self.owner.weaponname != "")
166                         setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
167                 else
168                         self.model = "";
169         }
170         self.effects = self.owner.effects | EF_LOWPRECISION;
171         self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
172
173         if (self.flags & FL_FLY)
174                 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
175                 self.effects = self.effects - (self.effects & EF_NODRAW);
176
177         if(self.owner.alpha >= 0)
178                 self.alpha = self.owner.alpha;
179         else
180                 self.alpha = 1;
181         self.colormap = self.owner.colormap;
182
183         self.angles = '0 0 0';
184         local float f;
185         f = 0;
186         if (self.state == WS_RAISE)
187                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
188         else if (self.state == WS_DROP)
189                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
190         else if (self.state == WS_CLEAR)
191                 f = 1;
192         self.angles_x = -90 * f * f;
193
194         // create or update the lasertarget entity
195         LaserTarget_Think();
196 };
197
198 void() CL_ExteriorWeaponentity_Think =
199 {
200         float tag_found;
201         self.nextthink = time;
202         if (self.owner.exteriorweaponentity != self)
203         {
204                 remove(self);
205                 return;
206         }
207         if (self.owner.deadflag != DEAD_NO)
208         {
209                 self.model = "";
210                 return;
211         }
212         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
213         {
214                 self.cnt = self.owner.weapon;
215                 self.dmg = self.owner.modelindex;
216                 self.deadflag = self.owner.deadflag;
217                 if (self.owner.weaponname != "")
218                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
219                 else
220                         self.model = "";
221
222                 //if((tag_found = gettagindex(self.owner, "tag_weapon")))
223                 if((tag_found = gettagindex(self.owner, "tag_attachweaponhere"))) // can't use tag_weapon because Q3A uses it for some random place INSIDE the model
224                 {
225                         self.tag_index = tag_found;
226                         self.tag_entity = self.owner;
227                 }
228                 else
229                         setattachment(self, self.owner, "bip01 r hand");
230
231                 // if that didn't find a tag, hide the exterior weapon model
232                 if (!self.tag_index)
233                         self.model = "";
234         }
235         self.effects = self.owner.effects | EF_LOWPRECISION;
236         self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
237         if(self.owner.alpha >= 0)
238                 self.alpha = self.owner.alpha;
239         else
240                 self.alpha = 1;
241         self.colormap = self.owner.colormap;
242 };
243
244 // spawning weaponentity for client
245 void() CL_SpawnWeaponentity =
246 {
247         self.weaponentity = spawn();
248         self.weaponentity.solid = SOLID_NOT;
249         self.weaponentity.owner = self;
250         self.weaponentity.weaponentity = self.weaponentity;
251         setmodel(self.weaponentity, ""); // precision set when changed
252         self.weaponentity.origin = '0 0 0';
253         self.weaponentity.angles = '0 0 0';
254         self.weaponentity.viewmodelforclient = self;
255         self.weaponentity.flags = 0;
256         self.weaponentity.think = CL_Weaponentity_Think;
257         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
258         self.weaponentity.nextthink = time;
259         self.weaponentity.scale = 0.61;
260
261         self.exteriorweaponentity = spawn();
262         self.exteriorweaponentity.solid = SOLID_NOT;
263         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
264         self.exteriorweaponentity.owner = self;
265         self.exteriorweaponentity.origin = '0 0 0';
266         self.exteriorweaponentity.angles = '0 0 0';
267         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
268         self.exteriorweaponentity.nextthink = time;
269 };
270
271 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
272 {
273         local float itemcode, f;
274         local entity oldself;
275
276         if (wpn < WEP_FIRST || wpn > WEP_LAST)
277         {
278                 if (complain)
279                         sprint(self, "Invalid weapon\n");
280                 return FALSE;
281         }
282         itemcode = W_ItemCode(wpn);
283         if (cl.items & itemcode)
284         {
285                 if (andammo)
286                 {
287                         oldself = self;
288                         self = cl;
289                         f = weapon_action(wpn, WR_CHECKAMMO1);
290                         f = f + weapon_action(wpn, WR_CHECKAMMO2);
291                         self = oldself;
292                         if (!f)
293                         {
294                                 if (complain)
295                                         sprint(self, "You don't have any ammo for that weapon\n");
296                                 return FALSE;
297                         }
298                 }
299                 return TRUE;
300         }
301         if (complain)
302         {
303                 // DRESK - 3/16/07
304                 // Report Proper Weapon Status / Modified Weapon Ownership Message
305                 if(itemsInMap & itemcode)
306                         sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
307                 else
308                         sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
309         }
310         return FALSE;
311 };
312
313 // Weapon subs
314 void() w_clear =
315 {
316         if (self.weapon != -1)
317                 self.weapon = 0;
318         if (self.weaponentity)
319         {
320                 self.weaponentity.state = WS_CLEAR;
321                 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
322                 self.weaponentity.effects = 0;
323         }
324 };
325
326 void() w_ready =
327 {
328         if (self.weaponentity)
329         {
330                 self.weaponentity.state = WS_READY;
331                 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
332         }
333 };
334
335 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
336 float(entity e) w_getbestweapon
337 {// add new weapons here
338         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
339                 return WEP_ROCKET_LAUNCHER;
340         else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
341                 return WEP_NEX;
342         else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
343                 return WEP_HAGAR;
344         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
345                 return WEP_GRENADE_LAUNCHER;
346         else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
347                 return WEP_ELECTRO;
348         else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
349                 return WEP_CRYLINK;
350         else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
351                 return WEP_UZI;
352         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
353                 return WEP_SHOTGUN;
354         else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
355                 return WEP_LASER;
356         else
357                 return 0;
358 };
359
360 // Setup weapon for client (after this raise frame will be launched)
361 void(float windex, string wname, float hudammo) weapon_setup =
362 {
363         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
364         self.items = self.items | hudammo;
365
366         // the two weapon entities will notice this has changed and update their models
367         self.weapon = windex;
368         self.weaponname = wname;
369 };
370
371 // perform weapon to attack (weaponstate and attack_finished check is here)
372 float(float secondary, float attacktime) weapon_prepareattack =
373 {
374         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
375         {
376                 self.switchweapon = w_getbestweapon(self);
377                 if (self.switchweapon != self.weapon)
378                         self.cnt = self.weapon;
379                 return FALSE;
380         }
381         // don't fire if previous attack is not finished
382         if (ATTACK_FINISHED(self) > time)
383                 return FALSE;
384         // don't fire while changing weapon
385         if (self.weaponentity.state != WS_READY)
386                 return FALSE;
387         self.weaponentity.state = WS_INUSE;
388         ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self) + attacktime);
389         return TRUE;
390 };
391
392 void(float fr, float t, void() func) weapon_thinkf =
393 {
394         if (fr >= 0)
395         {
396                 if (self.weaponentity != world)
397                         self.weaponentity.frame = fr;
398         }
399
400         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
401         {
402                 backtrace("Tried to override initial weapon think function - should this really happen?");
403         }
404
405         if(g_runematch)
406         {
407                 if(self.runes & RUNE_SPEED)
408                 {
409                         if(self.runes & CURSE_SLOW)
410                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
411                         else
412                                 t = t * cvar("g_balance_rune_speed_atkrate");
413                 }
414                 else if(self.runes & CURSE_SLOW)
415                 {
416                         t = t * cvar("g_balance_curse_slow_atkrate");
417                 }
418         }
419
420         // VorteX: haste can be added here
421         self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
422         self.weapon_think = func;
423
424         if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
425         if (t)
426         {
427                 local vector anim;
428                 anim = self.anim_shoot;
429                 anim_z = anim_y / t;
430                 player_setanim(anim, FALSE, TRUE, TRUE);
431         }
432 };
433
434 void(float spd, vector org) weapon_boblayer1 =
435 {
436         // VorteX: haste can be added here
437 };
438
439 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
440 {
441         vector mdirection;
442         float mspeed;
443         float outspeed;
444         float nstyle;
445         vector outvelocity;
446
447         mdirection = normalize(mvelocity);
448         mspeed = vlen(mvelocity);
449
450         nstyle = cvar("g_projectiles_newton_style");
451         if(nstyle == 0)
452         {
453                 // absolute velocity
454                 outvelocity = mvelocity;
455         }
456         else if(nstyle == 1)
457         {
458                 // true Newtonian projectiles
459                 outvelocity = pvelocity + mvelocity;
460         }
461         else if(nstyle == 2)
462         {
463                 // true Newtonian projectiles with automatic aim adjustment
464                 //
465                 // solve: |outspeed * mdirection - pvelocity| = mspeed
466                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
467                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
468                 // PLUS SIGN!
469                 // not defined?
470                 // then...
471                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
472                 // velocity without mdirection component > mspeed
473                 // fire at smallest possible mspeed that works?
474                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
475
476                 float D;
477                 float p;
478                 float q;
479                 p = mdirection * pvelocity;
480                 q = pvelocity * pvelocity - mspeed * mspeed;
481                 D = p * p - q;
482                 if(D < 0)
483                 {
484                         //dprint("impossible shot, adjusting\n");
485                         D = 0;
486                 }
487                 outspeed = p + sqrt(D);
488                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
489                 outvelocity = mdirection * outspeed;
490         }
491         else if(nstyle == 3)
492         {
493                 // pseudo-Newtonian:
494                 outspeed = mspeed + mdirection * pvelocity;
495                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
496                 outvelocity = mdirection * outspeed;
497         }
498         else if(nstyle == 4)
499         {
500                 // tZorkian:
501                 outspeed = mspeed + vlen(pvelocity);
502                 outvelocity = mdirection * outspeed;
503         }
504         else
505                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
506
507         return outvelocity;
508 }
509
510 void(entity missile) W_SetupProjectileVelocity =
511 {
512         if(missile.owner == world)
513                 error("Unowned missile");
514
515         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
516
517
518 void(float wepcode, float minammo) weapon_register =
519 {
520         string s;
521         float itemcode;
522         s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
523         s = strcat(s, ftos(wepcode), " "); // impulse
524         s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
525         s = strcat(s, ftos(wepcode), " "); // self.weapon code
526
527         // ammo stat
528         itemcode = W_AmmoItemCode(wepcode);
529         if(itemcode == IT_SHELLS)
530                 s = strcat(s, "6 ");
531         else if(itemcode == IT_NAILS)
532                 s = strcat(s, "7 ");
533         else if(itemcode == IT_ROCKETS)
534                 s = strcat(s, "8 ");
535         else // if(itemcode == IT_CELLS)
536                 s = strcat(s, "9 ");
537
538         s = strcat(s, ftos(minammo), "\n");
539         //dprint(s);
540         stuffcmd(self, s);
541 }