]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/weaponsystem.qc
Merge remote-tracking branch 'origin/master' into TimePath/combined_updates
[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         return 0;
645 }
646
647 void W_WeaponFrame()
648 {
649         vector fo, ri, up;
650
651         if (frametime)
652                 self.weapon_frametime = frametime;
653
654         if (!self.weaponentity || self.health < 1)
655                 return; // Dead player can't use weapons and injure impulse commands
656
657         if(forbidWeaponUse())
658         if(self.weaponentity.state != WS_CLEAR)
659         {
660                 w_ready();
661                 return;
662         }
663
664         if(!self.switchweapon)
665         {
666                 self.weapon = 0;
667                 self.switchingweapon = 0;
668                 self.weaponentity.state = WS_CLEAR;
669                 self.weaponname = "";
670                 //self.items &= ~IT_AMMO;
671                 return;
672         }
673
674         makevectors(self.v_angle);
675         fo = v_forward; // save them in case the weapon think functions change it
676         ri = v_right;
677         up = v_up;
678
679         // Change weapon
680         if (self.weapon != self.switchweapon)
681         {
682                 if (self.weaponentity.state == WS_CLEAR)
683                 {
684                         // end switching!
685                         self.switchingweapon = self.switchweapon;
686                         entity newwep = get_weaponinfo(self.switchweapon);
687
688                         // the two weapon entities will notice this has changed and update their models
689                         self.weapon = self.switchweapon;
690                         self.weaponname = newwep.mdl;
691                         self.bulletcounter = 0;
692                         //self.ammo_field = newwep.ammo_field;
693                         WEP_ACTION(self.switchweapon, WR_SETUP);
694                         self.weaponentity.state = WS_RAISE;
695
696                         // set our clip load to the load of the weapon we switched to, if it's reloadable
697                         if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
698                         {
699                                 self.clip_load = self.(weapon_load[self.switchweapon]);
700                                 self.clip_size = newwep.reloading_ammo;
701                         }
702                         else
703                                 self.clip_load = self.clip_size = 0;
704
705                         weapon_thinkf(WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
706                 }
707                 else if (self.weaponentity.state == WS_DROP)
708                 {
709                         // in dropping phase we can switch at any time
710                         self.switchingweapon = self.switchweapon;
711                 }
712                 else if (self.weaponentity.state == WS_READY)
713                 {
714                         // start switching!
715                         self.switchingweapon = self.switchweapon;
716                         entity oldwep = get_weaponinfo(self.weapon);
717
718                         // set up weapon switch think in the future, and start drop anim
719                         #ifndef INDEPENDENT_ATTACK_FINISHED
720                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
721                         {
722                         #endif
723                                 sound(self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
724                                 self.weaponentity.state = WS_DROP;
725                                 weapon_thinkf(WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
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 if(wep.ammo_field != ammo_none)
824         {
825                 self.(wep.ammo_field) -= ammo_use;
826                 if(self.(wep.ammo_field) < 0)
827                 {
828                         backtrace(sprintf(
829                                 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
830                                 "Please notify Samual immediately with a copy of this backtrace!\n",
831                                 ammo_use,
832                                 wep.netname,
833                                 GetAmmoPicture(wep.ammo_field),
834                                 self.netname,
835                                 self.(wep.ammo_field)
836                         ));
837                 }
838         }
839 }
840
841 // weapon reloading code
842
843 .float reload_ammo_amount, reload_ammo_min, reload_time;
844 .float reload_complain;
845 .string reload_sound;
846
847 void W_ReloadedAndReady()
848 {
849         // finish the reloading process, and do the ammo transfer
850
851         self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
852
853         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
854         if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO || self.ammo_field == ammo_none)
855                 self.clip_load = self.reload_ammo_amount;
856         else
857         {
858                 while(self.clip_load < self.reload_ammo_amount && self.(self.ammo_field)) // make sure we don't add more ammo than we have
859                 {
860                         self.clip_load += 1;
861                         self.(self.ammo_field) -= 1;
862                 }
863         }
864         self.(weapon_load[self.weapon]) = self.clip_load;
865
866         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
867         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
868         // so your weapon is disabled for a few seconds without reason
869
870         //ATTACK_FINISHED(self) -= self.reload_time - 1;
871
872         w_ready();
873 }
874
875 void W_Reload(float sent_ammo_min, string sent_sound)
876 {
877         // set global values to work with
878         entity e;
879         e = get_weaponinfo(self.weapon);
880
881         self.reload_ammo_min = sent_ammo_min;
882         self.reload_ammo_amount = e.reloading_ammo;;
883         self.reload_time = e.reloading_time;
884         self.reload_sound = sent_sound;
885
886         // don't reload weapons that don't have the RELOADABLE flag
887         if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
888         {
889                 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
890                 return;
891         }
892
893         // return if reloading is disabled for this weapon
894         if(!self.reload_ammo_amount)
895                 return;
896
897         // our weapon is fully loaded, no need to reload
898         if (self.clip_load >= self.reload_ammo_amount)
899                 return;
900
901         // no ammo, so nothing to load
902         if(self.ammo_field != ammo_none)
903         if(!self.(self.ammo_field) && self.reload_ammo_min)
904         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
905         {
906                 if(IS_REAL_CLIENT(self) && self.reload_complain < time)
907                 {
908                         play2(self, "weapons/unavailable.wav");
909                         sprint(self, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(self.weapon), "\n"));
910                         self.reload_complain = time + 1;
911                 }
912                 // switch away if the amount of ammo is not enough to keep using this weapon
913                 if (!(WEP_ACTION(self.weapon, WR_CHECKAMMO1) + WEP_ACTION(self.weapon, WR_CHECKAMMO2)))
914                 {
915                         self.clip_load = -1; // reload later
916                         W_SwitchToOtherWeapon(self);
917                 }
918                 return;
919         }
920
921         if (self.weaponentity)
922         {
923                 if (self.weaponentity.wframe == WFRAME_RELOAD)
924                         return;
925
926                 // allow switching away while reloading, but this will cause a new reload!
927                 self.weaponentity.state = WS_READY;
928         }
929
930         // now begin the reloading process
931
932         sound(self, CH_WEAPON_SINGLE, self.reload_sound, VOL_BASE, ATTEN_NORM);
933
934         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
935         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
936         // so your weapon is disabled for a few seconds without reason
937
938         //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
939
940         weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
941
942         if(self.clip_load < 0)
943                 self.clip_load = 0;
944         self.old_clip_load = self.clip_load;
945         self.clip_load = self.(weapon_load[self.weapon]) = -1;
946 }
947
948 entity weapon_dropevent_item;
949 void W_DropEvent(float event, entity player, float weapon_type, entity weapon_item)
950 {
951         entity oldself = self;
952         self = player;
953         weapon_dropevent_item = weapon_item;
954         WEP_ACTION(weapon_type, event);
955         self = oldself;
956 }