]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/weaponsystem.qc
Merge branch 'TimePath/refactor/mod_operator' into 'master'
[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 == WEP_SHOTGUN)
462                 if(!secondary && WEP_CVAR(shotgun, secondary) == 1)
463                         return FALSE; // no clicking, just allow
464
465                 if(self.weapon == self.switchweapon && time - self.prevdryfire > 1) // only play once BEFORE starting to switch weapons
466                 {
467                         sound (self, CH_WEAPON_A, "weapons/dryfire.wav", VOL_BASE, ATTEN_NORM);
468                         self.prevdryfire = time;
469                 }
470
471                 if(WEP_ACTION(self.weapon, WR_CHECKAMMO2 - secondary)) // check if the other firing mode has enough ammo
472                 {
473                         if(time - self.prevwarntime > 1)
474                         {
475                                 Send_Notification(
476                                         NOTIF_ONE,
477                                         self,
478                                         MSG_MULTI,
479                                         ITEM_WEAPON_PRIMORSEC,
480                                         self.weapon,
481                                         secondary,
482                                         (1 - secondary)
483                                 );
484                         }
485                         self.prevwarntime = time;
486                 }
487                 else // this weapon is totally unable to fire, switch to another one
488                 {
489                         W_SwitchToOtherWeapon(self);
490                 }
491
492                 return FALSE;
493         }
494         return TRUE;
495 }
496 .float race_penalty;
497 float weapon_prepareattack_check(float secondary, float attacktime)
498 {
499         if(!weapon_prepareattack_checkammo(secondary))
500                 return FALSE;
501
502         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
503         //if all players readied up and the countdown is running
504         if(time < game_starttime || time < self.race_penalty) {
505                 return FALSE;
506         }
507
508         if (timeout_status == TIMEOUT_ACTIVE) //don't allow the player to shoot while game is paused
509                 return FALSE;
510
511         // do not even think about shooting if switching
512         if(self.switchweapon != self.weapon)
513                 return FALSE;
514
515         if(attacktime >= 0)
516         {
517                 // don't fire if previous attack is not finished
518                 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
519                         return FALSE;
520                 // don't fire while changing weapon
521                 if (self.weaponentity.state != WS_READY)
522                         return FALSE;
523         }
524
525         return TRUE;
526 }
527 float weapon_prepareattack_do(float secondary, float attacktime)
528 {
529         self.weaponentity.state = WS_INUSE;
530
531         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
532
533         // if the weapon hasn't been firing continuously, reset the timer
534         if(attacktime >= 0)
535         {
536                 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
537                 {
538                         ATTACK_FINISHED(self) = time;
539                         //dprint("resetting attack finished to ", ftos(time), "\n");
540                 }
541                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
542         }
543         self.bulletcounter += 1;
544         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
545         return TRUE;
546 }
547 float weapon_prepareattack(float secondary, float attacktime)
548 {
549         if(weapon_prepareattack_check(secondary, attacktime))
550         {
551                 weapon_prepareattack_do(secondary, attacktime);
552                 return TRUE;
553         }
554         else
555                 return FALSE;
556 }
557
558 void weapon_thinkf(float fr, float t, void() func)
559 {
560         vector a;
561         vector of, or, ou;
562         float restartanim;
563
564         if(fr == WFRAME_DONTCHANGE)
565         {
566                 fr = self.weaponentity.wframe;
567                 restartanim = FALSE;
568         }
569         else if (fr == WFRAME_IDLE)
570                 restartanim = FALSE;
571         else
572                 restartanim = TRUE;
573
574         of = v_forward;
575         or = v_right;
576         ou = v_up;
577
578         if (self.weaponentity)
579         {
580                 self.weaponentity.wframe = fr;
581                 a = '0 0 0';
582                 if (fr == WFRAME_IDLE)
583                         a = self.weaponentity.anim_idle;
584                 else if (fr == WFRAME_FIRE1)
585                         a = self.weaponentity.anim_fire1;
586                 else if (fr == WFRAME_FIRE2)
587                         a = self.weaponentity.anim_fire2;
588                 else // if (fr == WFRAME_RELOAD)
589                         a = self.weaponentity.anim_reload;
590                 a_z *= g_weaponratefactor;
591                 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
592         }
593
594         v_forward = of;
595         v_right = or;
596         v_up = ou;
597
598         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
599         {
600                 backtrace("Tried to override initial weapon think function - should this really happen?");
601         }
602
603         t *= W_WeaponRateFactor();
604
605         // VorteX: haste can be added here
606         if (self.weapon_think == w_ready)
607         {
608                 self.weapon_nextthink = time;
609                 //dprint("started firing at ", ftos(time), "\n");
610         }
611         if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
612         {
613                 self.weapon_nextthink = time;
614                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
615         }
616         self.weapon_nextthink = self.weapon_nextthink + t;
617         self.weapon_think = func;
618         //dprint("next ", ftos(self.weapon_nextthink), "\n");
619
620         if((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
621         {
622                 if((self.weapon == WEP_SHOCKWAVE || self.weapon == WEP_SHOTGUN) && fr == WFRAME_FIRE2)
623                         animdecide_setaction(self, ANIMACTION_MELEE, restartanim);
624                 else
625                         animdecide_setaction(self, ANIMACTION_SHOOT, restartanim);
626         }
627         else
628         {
629                 if(self.anim_upper_action == ANIMACTION_SHOOT || self.anim_upper_action == ANIMACTION_MELEE)
630                         self.anim_upper_action = 0;
631         }
632 }
633
634 float forbidWeaponUse()
635 {
636         if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
637                 return 1;
638         if(round_handler_IsActive() && !round_handler_IsRoundStarted())
639                 return 1;
640         if(self.player_blocked)
641                 return 1;
642         if(self.frozen)
643                 return 1;
644         if(self.weapon_blocked)
645                 return 1;
646         return 0;
647 }
648
649 void W_WeaponFrame()
650 {
651         vector fo, ri, up;
652
653         if (frametime)
654                 self.weapon_frametime = frametime;
655
656         if (!self.weaponentity || self.health < 1)
657                 return; // Dead player can't use weapons and injure impulse commands
658
659         if(forbidWeaponUse())
660         if(self.weaponentity.state != WS_CLEAR)
661         {
662                 w_ready();
663                 return;
664         }
665
666         if(!self.switchweapon)
667         {
668                 self.weapon = 0;
669                 self.switchingweapon = 0;
670                 self.weaponentity.state = WS_CLEAR;
671                 self.weaponname = "";
672                 //self.items &= ~IT_AMMO;
673                 return;
674         }
675
676         makevectors(self.v_angle);
677         fo = v_forward; // save them in case the weapon think functions change it
678         ri = v_right;
679         up = v_up;
680
681         // Change weapon
682         if (self.weapon != self.switchweapon)
683         {
684                 if (self.weaponentity.state == WS_CLEAR)
685                 {
686                         // end switching!
687                         self.switchingweapon = self.switchweapon;
688                         entity newwep = get_weaponinfo(self.switchweapon);
689
690                         // the two weapon entities will notice this has changed and update their models
691                         self.weapon = self.switchweapon;
692                         self.weaponname = newwep.mdl;
693                         self.bulletcounter = 0;
694                         self.ammo_field = newwep.ammo_field;
695                         WEP_ACTION(self.switchweapon, WR_SETUP);
696                         self.weaponentity.state = WS_RAISE;
697
698                         // set our clip load to the load of the weapon we switched to, if it's reloadable
699                         if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
700                         {
701                                 self.clip_load = self.(weapon_load[self.switchweapon]);
702                                 self.clip_size = newwep.reloading_ammo;
703                         }
704                         else
705                                 self.clip_load = self.clip_size = 0;
706
707                         weapon_thinkf(WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
708                 }
709                 else if (self.weaponentity.state == WS_DROP)
710                 {
711                         // in dropping phase we can switch at any time
712                         self.switchingweapon = self.switchweapon;
713                 }
714                 else if (self.weaponentity.state == WS_READY)
715                 {
716                         // start switching!
717                         self.switchingweapon = self.switchweapon;
718                         entity oldwep = get_weaponinfo(self.weapon);
719
720                         // set up weapon switch think in the future, and start drop anim
721                         #ifndef INDEPENDENT_ATTACK_FINISHED
722                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
723                         {
724                         #endif
725                                 sound(self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
726                                 self.weaponentity.state = WS_DROP;
727                                 weapon_thinkf(WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
728                         #ifndef INDEPENDENT_ATTACK_FINISHED
729                         }
730                         #endif
731                 }
732         }
733
734         // LordHavoc: network timing test code
735         //if (self.button0)
736         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
737
738         float w;
739         w = self.weapon;
740
741         // call the think code which may fire the weapon
742         // and do so multiple times to resolve framerate dependency issues if the
743         // server framerate is very low and the weapon fire rate very high
744         float c;
745         c = 0;
746         while (c < W_TICSPERFRAME)
747         {
748                 c = c + 1;
749                 if(w && !(self.weapons & WepSet_FromWeapon(w)))
750                 {
751                         if(self.weapon == self.switchweapon)
752                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
753                         w = 0;
754                 }
755
756                 v_forward = fo;
757                 v_right = ri;
758                 v_up = up;
759
760                 if(w)
761                         WEP_ACTION(self.weapon, WR_THINK);
762                 else
763                         WEP_ACTION(self.weapon, WR_GONETHINK);
764
765                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
766                 {
767                         if(self.weapon_think)
768                         {
769                                 v_forward = fo;
770                                 v_right = ri;
771                                 v_up = up;
772                                 self.weapon_think();
773                         }
774                         else
775                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
776                 }
777         }
778 }
779
780 void W_AttachToShotorg(entity flash, vector offset)
781 {
782         entity xflash;
783         flash.owner = self;
784         flash.angles_z = random() * 360;
785
786         if(gettagindex(self.weaponentity, "shot"))
787                 setattachment(flash, self.weaponentity, "shot");
788         else
789                 setattachment(flash, self.weaponentity, "tag_shot");
790         setorigin(flash, offset);
791
792         xflash = spawn();
793         copyentity(flash, xflash);
794
795         flash.viewmodelforclient = self;
796
797         if(self.weaponentity.oldorigin_x > 0)
798         {
799                 setattachment(xflash, self.exteriorweaponentity, "");
800                 setorigin(xflash, self.weaponentity.oldorigin + offset);
801         }
802         else
803         {
804                 if(gettagindex(self.exteriorweaponentity, "shot"))
805                         setattachment(xflash, self.exteriorweaponentity, "shot");
806                 else
807                         setattachment(xflash, self.exteriorweaponentity, "tag_shot");
808                 setorigin(xflash, offset);
809         }
810 }
811
812 void W_DecreaseAmmo(float ammo_use)
813 {
814         entity wep = get_weaponinfo(self.weapon);
815
816         if(cvar("g_overkill"))
817         if(self.ok_use_ammocharge)
818         {
819                 ok_DecreaseCharge(self, self.weapon);
820                 return; // TODO
821         }
822
823         if((self.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo)
824                 return;
825
826         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
827         if(wep.reloading_ammo)
828         {
829                 self.clip_load -= ammo_use;
830                 self.(weapon_load[self.weapon]) = self.clip_load;
831         }
832         else if(wep.ammo_field != ammo_none)
833         {
834                 self.(wep.ammo_field) -= ammo_use;
835                 if(self.(wep.ammo_field) < 0)
836                 {
837                         backtrace(sprintf(
838                                 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
839                                 "Please notify Samual immediately with a copy of this backtrace!\n",
840                                 ammo_use,
841                                 wep.netname,
842                                 GetAmmoPicture(wep.ammo_field),
843                                 self.netname,
844                                 self.(wep.ammo_field)
845                         ));
846                 }
847         }
848 }
849
850 // weapon reloading code
851
852 .float reload_ammo_amount, reload_ammo_min, reload_time;
853 .float reload_complain;
854 .string reload_sound;
855
856 void W_ReloadedAndReady()
857 {
858         // finish the reloading process, and do the ammo transfer
859
860         self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
861
862         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
863         if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO || self.ammo_field == ammo_none)
864                 self.clip_load = self.reload_ammo_amount;
865         else
866         {
867                 // make sure we don't add more ammo than we have
868                 float load = min(self.reload_ammo_amount - self.clip_load, self.(self.ammo_field));
869         self.clip_load += load;
870         self.(self.ammo_field) -= load;
871         }
872         self.(weapon_load[self.weapon]) = self.clip_load;
873
874         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
875         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
876         // so your weapon is disabled for a few seconds without reason
877
878         //ATTACK_FINISHED(self) -= self.reload_time - 1;
879
880         w_ready();
881 }
882
883 void W_Reload(float sent_ammo_min, string sent_sound)
884 {
885         // set global values to work with
886         entity e;
887         e = get_weaponinfo(self.weapon);
888
889         if(cvar("g_overkill"))
890         if(self.ok_use_ammocharge)
891                 return; // TODO
892
893         self.reload_ammo_min = sent_ammo_min;
894         self.reload_ammo_amount = e.reloading_ammo;
895         self.reload_time = e.reloading_time;
896         self.reload_sound = sent_sound;
897
898         // don't reload weapons that don't have the RELOADABLE flag
899         if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
900         {
901                 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
902                 return;
903         }
904
905         // return if reloading is disabled for this weapon
906         if(!self.reload_ammo_amount)
907                 return;
908
909         // our weapon is fully loaded, no need to reload
910         if (self.clip_load >= self.reload_ammo_amount)
911                 return;
912
913         // no ammo, so nothing to load
914         if(self.ammo_field != ammo_none)
915         if(!self.(self.ammo_field) && self.reload_ammo_min)
916         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
917         {
918                 if(IS_REAL_CLIENT(self) && self.reload_complain < time)
919                 {
920                         play2(self, "weapons/unavailable.wav");
921                         sprint(self, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(self.weapon), "\n"));
922                         self.reload_complain = time + 1;
923                 }
924                 // switch away if the amount of ammo is not enough to keep using this weapon
925                 if (!(WEP_ACTION(self.weapon, WR_CHECKAMMO1) + WEP_ACTION(self.weapon, WR_CHECKAMMO2)))
926                 {
927                         self.clip_load = -1; // reload later
928                         W_SwitchToOtherWeapon(self);
929                 }
930                 return;
931         }
932
933         if (self.weaponentity)
934         {
935                 if (self.weaponentity.wframe == WFRAME_RELOAD)
936                         return;
937
938                 // allow switching away while reloading, but this will cause a new reload!
939                 self.weaponentity.state = WS_READY;
940         }
941
942         // now begin the reloading process
943
944         sound(self, CH_WEAPON_SINGLE, self.reload_sound, VOL_BASE, ATTEN_NORM);
945
946         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
947         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
948         // so your weapon is disabled for a few seconds without reason
949
950         //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
951
952         weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
953
954         if(self.clip_load < 0)
955                 self.clip_load = 0;
956         self.old_clip_load = self.clip_load;
957         self.clip_load = self.(weapon_load[self.weapon]) = -1;
958 }
959
960 entity weapon_dropevent_item;
961 void W_DropEvent(float event, entity player, float weapon_type, entity weapon_item)
962 {
963         entity oldself = self;
964         self = player;
965         weapon_dropevent_item = weapon_item;
966         WEP_ACTION(weapon_type, event);
967         self = oldself;
968 }