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