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