]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/weaponsystem.qc
Merge remote-tracking branch 'origin/master' into samual/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / weaponsystem.qc
1 /*
2 ===========================================================================
3
4   CLIENT WEAPONSYSTEM CODE
5   Bring back W_Weaponframe
6
7 ===========================================================================
8 */
9
10 .float weapon_frametime;
11
12 float W_WeaponRateFactor()
13 {
14         float t;
15         t = 1.0 / g_weaponratefactor;
16
17         return t;
18 }
19
20 // VorteX: static frame globals
21 const float WFRAME_DONTCHANGE = -1;
22 const float WFRAME_FIRE1 = 0;
23 const float WFRAME_FIRE2 = 1;
24 const float WFRAME_IDLE = 2;
25 const float WFRAME_RELOAD = 3;
26 .float wframe;
27
28 void(float fr, float t, void() func) weapon_thinkf;
29
30 float CL_Weaponentity_CustomizeEntityForClient()
31 {
32         self.viewmodelforclient = self.owner;
33         if(IS_SPEC(other))
34                 if(other.enemy == self.owner)
35                         self.viewmodelforclient = other;
36         return TRUE;
37 }
38
39 /*
40  * supported formats:
41  *
42  * 1. simple animated model, muzzle flash handling on h_ model:
43  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
44  *      tags:
45  *        shot = muzzle end (shot origin, also used for muzzle flashes)
46  *        shell = casings ejection point (must be on the right hand side of the gun)
47  *        weapon = attachment for v_tuba.md3
48  *    v_tuba.md3 - first and third person model
49  *    g_tuba.md3 - pickup model
50  *
51  * 2. simple animated model, muzzle flash handling on v_ model:
52  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
53  *      tags:
54  *        weapon = attachment for v_tuba.md3
55  *    v_tuba.md3 - first and third person model
56  *      tags:
57  *        shot = muzzle end (shot origin, also used for muzzle flashes)
58  *        shell = casings ejection point (must be on the right hand side of the gun)
59  *    g_tuba.md3 - pickup model
60  *
61  * 3. fully animated model, muzzle flash handling on h_ model:
62  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
63  *      tags:
64  *        shot = muzzle end (shot origin, also used for muzzle flashes)
65  *        shell = casings ejection point (must be on the right hand side of the gun)
66  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
67  *    v_tuba.md3 - third person model
68  *    g_tuba.md3 - pickup model
69  *
70  * 4. fully animated model, muzzle flash handling on v_ model:
71  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
72  *      tags:
73  *        shot = muzzle end (shot origin)
74  *        shell = casings ejection point (must be on the right hand side of the gun)
75  *    v_tuba.md3 - third person model
76  *      tags:
77  *        shot = muzzle end (for muzzle flashes)
78  *    g_tuba.md3 - pickup model
79  */
80
81 // writes:
82 //   self.origin, self.angles
83 //   self.weaponentity
84 //   self.movedir, self.view_ofs
85 //   attachment stuff
86 //   anim stuff
87 // to free:
88 //   call again with ""
89 //   remove the ent
90 void CL_WeaponEntity_SetModel(string name)
91 {
92         float v_shot_idx;
93         if (name != "")
94         {
95                 // if there is a child entity, hide it until we're sure we use it
96                 if (self.weaponentity)
97                         self.weaponentity.model = "";
98                 setmodel(self, strcat("models/weapons/v_", name, ".md3")); // precision set below
99                 v_shot_idx = gettagindex(self, "shot"); // used later
100                 if(!v_shot_idx)
101                         v_shot_idx = gettagindex(self, "tag_shot");
102
103                 setmodel(self, strcat("models/weapons/h_", name, ".iqm")); // precision set below
104                 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
105                 self.anim_fire1  = animfixfps(self, '0 1 0.01', '0 0 0');
106                 self.anim_fire2  = animfixfps(self, '1 1 0.01', '0 0 0');
107                 self.anim_idle   = animfixfps(self, '2 1 0.01', '0 0 0');
108                 self.anim_reload = animfixfps(self, '3 1 0.01', '0 0 0');
109
110                 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
111                 // if we don't, this is a "real" animated model
112                 if(gettagindex(self, "weapon"))
113                 {
114                         if (!self.weaponentity)
115                                 self.weaponentity = spawn();
116                         setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
117                         setattachment(self.weaponentity, self, "weapon");
118                 }
119                 else if(gettagindex(self, "tag_weapon"))
120                 {
121                         if (!self.weaponentity)
122                                 self.weaponentity = spawn();
123                         setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
124                         setattachment(self.weaponentity, self, "tag_weapon");
125                 }
126                 else
127                 {
128                         if(self.weaponentity)
129                                 remove(self.weaponentity);
130                         self.weaponentity = world;
131                 }
132
133                 setorigin(self,'0 0 0');
134                 self.angles = '0 0 0';
135                 self.frame = 0;
136                 self.viewmodelforclient = world;
137
138                 float idx;
139
140                 if(v_shot_idx) // v_ model attached to invisible h_ model
141                 {
142                         self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
143                 }
144                 else
145                 {
146                         idx = gettagindex(self, "shot");
147                         if(!idx)
148                                 idx = gettagindex(self, "tag_shot");
149                         if(idx)
150                                 self.movedir = gettaginfo(self, idx);
151                         else
152                         {
153                                 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
154                                 self.movedir = '0 0 0';
155                         }
156                 }
157
158                 if(self.weaponentity) // v_ model attached to invisible h_ model
159                 {
160                         idx = gettagindex(self.weaponentity, "shell");
161                         if(!idx)
162                                 idx = gettagindex(self.weaponentity, "tag_shell");
163                         if(idx)
164                                 self.spawnorigin = gettaginfo(self.weaponentity, idx);
165                 }
166                 else
167                         idx = 0;
168                 if(!idx)
169                 {
170                         idx = gettagindex(self, "shell");
171                         if(!idx)
172                                 idx = gettagindex(self, "tag_shell");
173                         if(idx)
174                                 self.spawnorigin = gettaginfo(self, idx);
175                         else
176                         {
177                                 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
178                                 self.spawnorigin = self.movedir;
179                         }
180                 }
181
182                 if(v_shot_idx)
183                 {
184                         self.oldorigin = '0 0 0'; // use regular attachment
185                 }
186                 else
187                 {
188                         if(self.weaponentity)
189                         {
190                                 idx = gettagindex(self, "weapon");
191                                 if(!idx)
192                                         idx = gettagindex(self, "tag_weapon");
193                         }
194                         else
195                         {
196                                 idx = gettagindex(self, "handle");
197                                 if(!idx)
198                                         idx = gettagindex(self, "tag_handle");
199                         }
200                         if(idx)
201                         {
202                                 self.oldorigin = self.movedir - gettaginfo(self, idx);
203                         }
204                         else
205                         {
206                                 print("WARNING: weapon model ", self.model, " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
207                                 self.oldorigin = '0 0 0'; // there is no way to recover from this
208                         }
209                 }
210
211                 self.viewmodelforclient = self.owner;
212         }
213         else
214         {
215                 self.model = "";
216                 if(self.weaponentity)
217                         remove(self.weaponentity);
218                 self.weaponentity = world;
219                 self.movedir = '0 0 0';
220                 self.spawnorigin = '0 0 0';
221                 self.oldorigin = '0 0 0';
222                 self.anim_fire1  = '0 1 0.01';
223                 self.anim_fire2  = '0 1 0.01';
224                 self.anim_idle   = '0 1 0.01';
225                 self.anim_reload = '0 1 0.01';
226         }
227
228         self.view_ofs = '0 0 0';
229
230         if(self.movedir_x >= 0)
231         {
232                 vector v0;
233                 v0 = self.movedir;
234                 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
235                 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
236         }
237         self.owner.stat_shotorg = compressShotOrigin(self.movedir);
238         self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
239
240         self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
241
242         // check if an instant weapon switch occurred
243         setorigin(self, self.view_ofs);
244         // reset animstate now
245         self.wframe = WFRAME_IDLE;
246         setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
247 }
248
249 vector CL_Weapon_GetShotOrg(float wpn)
250 {
251         entity wi, oldself;
252         vector ret;
253         wi = get_weaponinfo(wpn);
254         oldself = self;
255         self = spawn();
256         CL_WeaponEntity_SetModel(wi.mdl);
257         ret = self.movedir;
258         CL_WeaponEntity_SetModel("");
259         remove(self);
260         self = oldself;
261         return ret;
262 }
263
264 void CL_Weaponentity_Think()
265 {
266         float tb;
267         self.nextthink = time;
268         if (intermission_running)
269                 self.frame = self.anim_idle_x;
270         if (self.owner.weaponentity != self)
271         {
272                 if (self.weaponentity)
273                         remove(self.weaponentity);
274                 remove(self);
275                 return;
276         }
277         if (self.owner.deadflag != DEAD_NO)
278         {
279                 self.model = "";
280                 if (self.weaponentity)
281                         self.weaponentity.model = "";
282                 return;
283         }
284         if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
285         {
286                 self.weaponname = self.owner.weaponname;
287                 self.dmg = self.owner.modelindex;
288                 self.deadflag = self.owner.deadflag;
289
290                 CL_WeaponEntity_SetModel(self.owner.weaponname);
291         }
292
293         tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
294         self.effects = self.owner.effects & EFMASK_CHEAP;
295         self.effects &= ~EF_LOWPRECISION;
296         self.effects &= ~EF_FULLBRIGHT; // can mask team color, so get rid of it
297         self.effects &= ~EF_TELEPORT_BIT;
298         self.effects &= ~EF_RESTARTANIM_BIT;
299         self.effects |= tb;
300
301         if(self.owner.alpha == default_player_alpha)
302                 self.alpha = default_weapon_alpha;
303         else if(self.owner.alpha != 0)
304                 self.alpha = self.owner.alpha;
305         else
306                 self.alpha = 1;
307
308         self.glowmod = self.owner.weaponentity_glowmod;
309         self.colormap = self.owner.colormap;
310         if (self.weaponentity)
311         {
312                 self.weaponentity.effects = self.effects;
313                 self.weaponentity.alpha = self.alpha;
314                 self.weaponentity.colormap = self.colormap;
315                 self.weaponentity.glowmod = self.glowmod;
316         }
317
318         self.angles = '0 0 0';
319
320         float f = (self.owner.weapon_nextthink - time);
321         if (self.state == WS_RAISE && !intermission_running)
322         {
323                 entity newwep = get_weaponinfo(self.owner.switchweapon);
324                 f = f * g_weaponratefactor / max(f, newwep.switchdelay_raise);
325                 self.angles_x = -90 * f * f;
326         }
327         else if (self.state == WS_DROP && !intermission_running)
328         {
329                 entity oldwep = get_weaponinfo(self.owner.weapon);
330                 f = 1 - f * g_weaponratefactor / max(f, oldwep.switchdelay_drop);
331                 self.angles_x = -90 * f * f;
332         }
333         else if (self.state == WS_CLEAR)
334         {
335                 f = 1;
336                 self.angles_x = -90 * f * f;
337         }
338 }
339
340 void CL_ExteriorWeaponentity_Think()
341 {
342         float tag_found;
343         self.nextthink = time;
344         if (self.owner.exteriorweaponentity != self)
345         {
346                 remove(self);
347                 return;
348         }
349         if (self.owner.deadflag != DEAD_NO)
350         {
351                 self.model = "";
352                 return;
353         }
354         if (self.weaponname != self.owner.weaponname || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
355         {
356                 self.weaponname = self.owner.weaponname;
357                 self.dmg = self.owner.modelindex;
358                 self.deadflag = self.owner.deadflag;
359                 if (self.owner.weaponname != "")
360                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
361                 else
362                         self.model = "";
363
364                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
365                 {
366                         self.tag_index = tag_found;
367                         self.tag_entity = self.owner;
368                 }
369                 else
370                         setattachment(self, self.owner, "bip01 r hand");
371         }
372         self.effects = self.owner.effects;
373         self.effects |= EF_LOWPRECISION;
374         self.effects = self.effects & EFMASK_CHEAP; // eat performance
375         if(self.owner.alpha == default_player_alpha)
376                 self.alpha = default_weapon_alpha;
377         else if(self.owner.alpha != 0)
378                 self.alpha = self.owner.alpha;
379         else
380                 self.alpha = 1;
381
382         self.glowmod = self.owner.weaponentity_glowmod;
383         self.colormap = self.owner.colormap;
384
385         CSQCMODEL_AUTOUPDATE();
386 }
387
388 // spawning weaponentity for client
389 void CL_SpawnWeaponentity()
390 {
391         self.weaponentity = spawn();
392         self.weaponentity.classname = "weaponentity";
393         self.weaponentity.solid = SOLID_NOT;
394         self.weaponentity.owner = self;
395         setmodel(self.weaponentity, ""); // precision set when changed
396         setorigin(self.weaponentity, '0 0 0');
397         self.weaponentity.angles = '0 0 0';
398         self.weaponentity.viewmodelforclient = self;
399         self.weaponentity.flags = 0;
400         self.weaponentity.think = CL_Weaponentity_Think;
401         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
402         self.weaponentity.nextthink = time;
403
404         self.exteriorweaponentity = spawn();
405         self.exteriorweaponentity.classname = "exteriorweaponentity";
406         self.exteriorweaponentity.solid = SOLID_NOT;
407         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
408         self.exteriorweaponentity.owner = self;
409         setorigin(self.exteriorweaponentity, '0 0 0');
410         self.exteriorweaponentity.angles = '0 0 0';
411         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
412         self.exteriorweaponentity.nextthink = time;
413
414         {
415                 entity oldself = self;
416                 self = self.exteriorweaponentity;
417                 CSQCMODEL_AUTOINIT();
418                 self = oldself;
419         }
420 }
421
422 // Weapon subs
423 void w_clear()
424 {
425         if (self.weapon != -1)
426         {
427                 self.weapon = 0;
428                 self.switchingweapon = 0;
429         }
430         if (self.weaponentity)
431         {
432                 self.weaponentity.state = WS_CLEAR;
433                 self.weaponentity.effects = 0;
434         }
435 }
436
437 void w_ready()
438 {
439         if (self.weaponentity)
440                 self.weaponentity.state = WS_READY;
441         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
442 }
443
444 .float prevdryfire;
445 .float prevwarntime;
446 float weapon_prepareattack_checkammo(float secondary)
447 {
448         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
449         if (!WEP_ACTION(self.weapon, WR_CHECKAMMO1 + secondary))
450         {
451                 // always keep the Mine Layer if we placed mines, so that we can detonate them
452                 entity mine;
453                 if(self.weapon == WEP_MINE_LAYER)
454                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
455                         return FALSE;
456
457                 if(self.weapon == self.switchweapon && time - self.prevdryfire > 1) // only play once BEFORE starting to switch weapons
458                 {
459                         sound (self, CH_WEAPON_A, "weapons/dryfire.wav", VOL_BASE, ATTEN_NORM);
460                         self.prevdryfire = time;
461                 }
462
463                 if(WEP_ACTION(self.weapon, WR_CHECKAMMO2 - secondary)) // check if the other firing mode has enough ammo
464                 {
465                         if(time - self.prevwarntime > 1)
466                         {
467                                 Send_Notification(
468                                         NOTIF_ONE,
469                                         self,
470                                         MSG_MULTI,
471                                         ITEM_WEAPON_PRIMORSEC,
472                                         self.weapon,
473                                         secondary,
474                                         (1 - secondary)
475                                 );
476                         }
477                         self.prevwarntime = time;
478                 }
479                 else // this weapon is totally unable to fire, switch to another one
480                 {
481                         W_SwitchToOtherWeapon(self);
482                 }
483
484                 return FALSE;
485         }
486         return TRUE;
487 }
488 .float race_penalty;
489 float weapon_prepareattack_check(float secondary, float attacktime)
490 {
491         if(!weapon_prepareattack_checkammo(secondary))
492                 return FALSE;
493
494         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
495         //if all players readied up and the countdown is running
496         if(time < game_starttime || time < self.race_penalty) {
497                 return FALSE;
498         }
499
500         if (timeout_status == TIMEOUT_ACTIVE) //don't allow the player to shoot while game is paused
501                 return FALSE;
502
503         // do not even think about shooting if switching
504         if(self.switchweapon != self.weapon)
505                 return FALSE;
506
507         if(attacktime >= 0)
508         {
509                 // don't fire if previous attack is not finished
510                 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
511                         return FALSE;
512                 // don't fire while changing weapon
513                 if (self.weaponentity.state != WS_READY)
514                         return FALSE;
515         }
516
517         return TRUE;
518 }
519 float weapon_prepareattack_do(float secondary, float attacktime)
520 {
521         self.weaponentity.state = WS_INUSE;
522
523         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
524
525         // if the weapon hasn't been firing continuously, reset the timer
526         if(attacktime >= 0)
527         {
528                 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
529                 {
530                         ATTACK_FINISHED(self) = time;
531                         //dprint("resetting attack finished to ", ftos(time), "\n");
532                 }
533                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
534         }
535         self.bulletcounter += 1;
536         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
537         return TRUE;
538 }
539 float weapon_prepareattack(float secondary, float attacktime)
540 {
541         if(weapon_prepareattack_check(secondary, attacktime))
542         {
543                 weapon_prepareattack_do(secondary, attacktime);
544                 return TRUE;
545         }
546         else
547                 return FALSE;
548 }
549
550 void weapon_thinkf(float fr, float t, void() func)
551 {
552         vector a;
553         vector of, or, ou;
554         float restartanim;
555
556         if(fr == WFRAME_DONTCHANGE)
557         {
558                 fr = self.weaponentity.wframe;
559                 restartanim = FALSE;
560         }
561         else if (fr == WFRAME_IDLE)
562                 restartanim = FALSE;
563         else
564                 restartanim = TRUE;
565
566         of = v_forward;
567         or = v_right;
568         ou = v_up;
569
570         if (self.weaponentity)
571         {
572                 self.weaponentity.wframe = fr;
573                 a = '0 0 0';
574                 if (fr == WFRAME_IDLE)
575                         a = self.weaponentity.anim_idle;
576                 else if (fr == WFRAME_FIRE1)
577                         a = self.weaponentity.anim_fire1;
578                 else if (fr == WFRAME_FIRE2)
579                         a = self.weaponentity.anim_fire2;
580                 else // if (fr == WFRAME_RELOAD)
581                         a = self.weaponentity.anim_reload;
582                 a_z *= g_weaponratefactor;
583                 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
584         }
585
586         v_forward = of;
587         v_right = or;
588         v_up = ou;
589
590         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
591         {
592                 backtrace("Tried to override initial weapon think function - should this really happen?");
593         }
594
595         t *= W_WeaponRateFactor();
596
597         // VorteX: haste can be added here
598         if (self.weapon_think == w_ready)
599         {
600                 self.weapon_nextthink = time;
601                 //dprint("started firing at ", ftos(time), "\n");
602         }
603         if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
604         {
605                 self.weapon_nextthink = time;
606                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
607         }
608         self.weapon_nextthink = self.weapon_nextthink + t;
609         self.weapon_think = func;
610         //dprint("next ", ftos(self.weapon_nextthink), "\n");
611
612         if((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
613         {
614                 if((self.weapon == WEP_SHOCKWAVE || self.weapon == WEP_SHOTGUN) && fr == WFRAME_FIRE2)
615                         animdecide_setaction(self, ANIMACTION_MELEE, restartanim);
616                 else
617                         animdecide_setaction(self, ANIMACTION_SHOOT, restartanim);
618         }
619         else
620         {
621                 if(self.anim_upper_action == ANIMACTION_SHOOT || self.anim_upper_action == ANIMACTION_MELEE)
622                         self.anim_upper_action = 0;
623         }
624 }
625
626 float forbidWeaponUse()
627 {
628         if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
629                 return 1;
630         if(round_handler_IsActive() && !round_handler_IsRoundStarted())
631                 return 1;
632         if(self.player_blocked)
633                 return 1;
634         if(self.freezetag_frozen)
635                 return 1;
636         return 0;
637 }
638
639 void W_WeaponFrame()
640 {
641         vector fo, ri, up;
642
643         if (frametime)
644                 self.weapon_frametime = frametime;
645
646         if (!self.weaponentity || self.health < 1)
647                 return; // Dead player can't use weapons and injure impulse commands
648
649         if(forbidWeaponUse())
650         if(self.weaponentity.state != WS_CLEAR)
651         {
652                 w_ready();
653                 return;
654         }
655
656         if(!self.switchweapon)
657         {
658                 self.weapon = 0;
659                 self.switchingweapon = 0;
660                 self.weaponentity.state = WS_CLEAR;
661                 self.weaponname = "";
662                 //self.items &= ~IT_AMMO;
663                 return;
664         }
665
666         makevectors(self.v_angle);
667         fo = v_forward; // save them in case the weapon think functions change it
668         ri = v_right;
669         up = v_up;
670
671         // Change weapon
672         if (self.weapon != self.switchweapon)
673         {
674                 if (self.weaponentity.state == WS_CLEAR)
675                 {
676                         // end switching!
677                         self.switchingweapon = self.switchweapon;
678                         entity newwep = get_weaponinfo(self.switchweapon);
679
680                         //self.items &= ~IT_AMMO;
681                         //self.items = self.items | (newwep.items & IT_AMMO);
682
683                         // the two weapon entities will notice this has changed and update their models
684                         self.weapon = self.switchweapon;
685                         self.weaponname = newwep.mdl;
686                         self.bulletcounter = 0; // WEAPONTODO
687                         //self.ammo_field = newwep.ammo_field;
688                         WEP_ACTION(self.switchweapon, WR_SETUP);
689                         self.weaponentity.state = WS_RAISE;
690
691                         // set our clip load to the load of the weapon we switched to, if it's reloadable
692                         if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
693                         {
694                                 self.clip_load = self.(weapon_load[self.switchweapon]);
695                                 self.clip_size = newwep.reloading_ammo;
696                         }
697                         else
698                                 self.clip_load = self.clip_size = 0;
699
700                         // VorteX: add player model weapon select frame here
701                         // setcustomframe(PlayerWeaponRaise);
702                         weapon_thinkf(WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
703                         //print(sprintf("W_WeaponFrame(): cvar: %s, value: %f\n", sprintf("g_balance_%s_switchdelay_raise", newwep.netname), cvar(sprintf("g_balance_%s_switchdelay_raise", newwep.netname))));
704                 }
705                 else if (self.weaponentity.state == WS_DROP)
706                 {
707                         // in dropping phase we can switch at any time
708                         self.switchingweapon = self.switchweapon;
709                 }
710                 else if (self.weaponentity.state == WS_READY)
711                 {
712                         // start switching!
713                         self.switchingweapon = self.switchweapon;
714
715                         entity oldwep = get_weaponinfo(self.weapon);
716                         
717 #ifndef INDEPENDENT_ATTACK_FINISHED
718                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
719                         {
720 #endif
721                         sound (self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
722                         self.weaponentity.state = WS_DROP;
723                         // set up weapon switch think in the future, and start drop anim
724                         weapon_thinkf(WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
725                         //print(sprintf("W_WeaponFrame(): cvar: %s, value: %f\n", sprintf("g_balance_%s_switchdelay_drop", oldwep.netname), cvar(sprintf("g_balance_%s_switchdelay_drop", oldwep.netname))));
726 #ifndef INDEPENDENT_ATTACK_FINISHED
727                         }
728 #endif
729                 }
730         }
731
732         // LordHavoc: network timing test code
733         //if (self.button0)
734         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
735
736         float w;
737         w = self.weapon;
738
739         // call the think code which may fire the weapon
740         // and do so multiple times to resolve framerate dependency issues if the
741         // server framerate is very low and the weapon fire rate very high
742         float c;
743         c = 0;
744         while (c < W_TICSPERFRAME)
745         {
746                 c = c + 1;
747                 if(w && !(self.weapons & WepSet_FromWeapon(w)))
748                 {
749                         if(self.weapon == self.switchweapon)
750                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
751                         w = 0;
752                 }
753
754                 v_forward = fo;
755                 v_right = ri;
756                 v_up = up;
757
758                 if(w)
759                         WEP_ACTION(self.weapon, WR_THINK);
760                 else
761                         WEP_ACTION(self.weapon, WR_GONETHINK);
762
763                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
764                 {
765                         if(self.weapon_think)
766                         {
767                                 v_forward = fo;
768                                 v_right = ri;
769                                 v_up = up;
770                                 self.weapon_think();
771                         }
772                         else
773                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
774                 }
775         }
776 }
777
778 void W_AttachToShotorg(entity flash, vector offset)
779 {
780         entity xflash;
781         flash.owner = self;
782         flash.angles_z = random() * 360;
783
784         if(gettagindex(self.weaponentity, "shot"))
785                 setattachment(flash, self.weaponentity, "shot");
786         else
787                 setattachment(flash, self.weaponentity, "tag_shot");
788         setorigin(flash, offset);
789
790         xflash = spawn();
791         copyentity(flash, xflash);
792
793         flash.viewmodelforclient = self;
794
795         if(self.weaponentity.oldorigin_x > 0)
796         {
797                 setattachment(xflash, self.exteriorweaponentity, "");
798                 setorigin(xflash, self.weaponentity.oldorigin + offset);
799         }
800         else
801         {
802                 if(gettagindex(self.exteriorweaponentity, "shot"))
803                         setattachment(xflash, self.exteriorweaponentity, "shot");
804                 else
805                         setattachment(xflash, self.exteriorweaponentity, "tag_shot");
806                 setorigin(xflash, offset);
807         }
808 }
809
810 void W_DecreaseAmmo(float ammo_use)
811 {
812         entity wep = get_weaponinfo(self.weapon);
813
814         if((self.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo)
815                 return;
816
817         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
818         if(wep.reloading_ammo)
819         {
820                 self.clip_load -= ammo_use;
821                 self.(weapon_load[self.weapon]) = self.clip_load;
822         }
823         else
824                 self.(wep.ammo_field) -= ammo_use;
825 }
826
827 // weapon reloading code
828
829 .float reload_ammo_amount, reload_ammo_min, reload_time;
830 .float reload_complain;
831 .string reload_sound;
832
833 void W_ReloadedAndReady()
834 {
835         // finish the reloading process, and do the ammo transfer
836
837         self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
838
839         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
840         if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO || self.ammo_field == ammo_none)
841                 self.clip_load = self.reload_ammo_amount;
842         else
843         {
844                 while(self.clip_load < self.reload_ammo_amount && self.(self.ammo_field)) // make sure we don't add more ammo than we have
845                 {
846                         self.clip_load += 1;
847                         self.(self.ammo_field) -= 1;
848                 }
849         }
850         self.(weapon_load[self.weapon]) = self.clip_load;
851
852         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
853         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
854         // so your weapon is disabled for a few seconds without reason
855
856         //ATTACK_FINISHED(self) -= self.reload_time - 1;
857
858         w_ready();
859 }
860
861 void W_Reload(float sent_ammo_min, string sent_sound)
862 {
863         // set global values to work with
864         entity e;
865         e = get_weaponinfo(self.weapon);
866
867         self.reload_ammo_min = sent_ammo_min;
868         self.reload_ammo_amount = e.reloading_ammo;;
869         self.reload_time = e.reloading_time;
870         self.reload_sound = sent_sound;
871
872         // don't reload weapons that don't have the RELOADABLE flag
873         if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
874         {
875                 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
876                 return;
877         }
878
879         // return if reloading is disabled for this weapon
880         if(!self.reload_ammo_amount)
881                 return;
882
883         // our weapon is fully loaded, no need to reload
884         if (self.clip_load >= self.reload_ammo_amount)
885                 return;
886
887         // no ammo, so nothing to load
888         if(self.ammo_field != ammo_none)
889         if(!self.(self.ammo_field) && self.reload_ammo_min)
890         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
891         {
892                 if(IS_REAL_CLIENT(self) && self.reload_complain < time)
893                 {
894                         play2(self, "weapons/unavailable.wav");
895                         sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));
896                         self.reload_complain = time + 1;
897                 }
898                 // switch away if the amount of ammo is not enough to keep using this weapon
899                 if (!(WEP_ACTION(self.weapon, WR_CHECKAMMO1) + WEP_ACTION(self.weapon, WR_CHECKAMMO2)))
900                 {
901                         self.clip_load = -1; // reload later
902                         W_SwitchToOtherWeapon(self);
903                 }
904                 return;
905         }
906
907         if (self.weaponentity)
908         {
909                 if (self.weaponentity.wframe == WFRAME_RELOAD)
910                         return;
911
912                 // allow switching away while reloading, but this will cause a new reload!
913                 self.weaponentity.state = WS_READY;
914         }
915
916         // now begin the reloading process
917
918         sound(self, CH_WEAPON_SINGLE, self.reload_sound, VOL_BASE, ATTEN_NORM);
919
920         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
921         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
922         // so your weapon is disabled for a few seconds without reason
923
924         //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
925
926         weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
927
928         if(self.clip_load < 0)
929                 self.clip_load = 0;
930         self.old_clip_load = self.clip_load;
931         self.clip_load = self.(weapon_load[self.weapon]) = -1;
932 }