]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/weaponsystem.qc
Merge branch 'divVerent/ballistic2' 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_SHOCKWAVE || 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                         //self.current_ammo = newwep.current_ammo;
690                         WEP_ACTION(self.switchweapon, WR_SETUP);
691                         self.weaponentity.state = WS_RAISE;
692
693                         // set our clip load to the load of the weapon we switched to, if it's reloadable
694                         if(newwep.spawnflags & WEP_FLAG_RELOADABLE && newwep.reloading_ammo) // prevent accessing undefined cvars
695                         {
696                                 self.clip_load = self.(weapon_load[self.switchweapon]);
697                                 self.clip_size = newwep.reloading_ammo;
698                         }
699                         else
700                                 self.clip_load = self.clip_size = 0;
701
702                         // VorteX: add player model weapon select frame here
703                         // setcustomframe(PlayerWeaponRaise);
704                         weapon_thinkf(WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
705                         //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))));
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
717                         entity oldwep = get_weaponinfo(self.weapon);
718                         
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                         // set up weapon switch think in the future, and start drop anim
726                         weapon_thinkf(WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
727                         //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))));
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((self.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo)
817                 return;
818
819         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
820         if(wep.reloading_ammo)
821         {
822                 self.clip_load -= ammo_use;
823                 self.(weapon_load[self.weapon]) = self.clip_load;
824         }
825         else
826                 self.(wep.current_ammo) -= ammo_use;
827 }
828
829 // weapon reloading code
830
831 .float reload_ammo_amount, reload_ammo_min, reload_time;
832 .float reload_complain;
833 .string reload_sound;
834
835 void W_ReloadedAndReady()
836 {
837         // finish the reloading process, and do the ammo transfer
838
839         self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
840
841         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
842         if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO)
843                 self.clip_load = self.reload_ammo_amount;
844         else
845         {
846                 while(self.clip_load < self.reload_ammo_amount && self.(self.current_ammo)) // make sure we don't add more ammo than we have
847                 {
848                         self.clip_load += 1;
849                         self.(self.current_ammo) -= 1;
850                 }
851         }
852         self.(weapon_load[self.weapon]) = self.clip_load;
853
854         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
855         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
856         // so your weapon is disabled for a few seconds without reason
857
858         //ATTACK_FINISHED(self) -= self.reload_time - 1;
859
860         w_ready();
861 }
862
863 void W_Reload(float sent_ammo_min, string sent_sound)
864 {
865         // set global values to work with
866         entity e;
867         e = get_weaponinfo(self.weapon);
868
869         self.reload_ammo_min = sent_ammo_min;
870         self.reload_ammo_amount = e.reloading_ammo;;
871         self.reload_time = e.reloading_time;
872         self.reload_sound = sent_sound;
873
874         // don't reload weapons that don't have the RELOADABLE flag
875         if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
876         {
877                 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
878                 return;
879         }
880
881         // return if reloading is disabled for this weapon
882         if(!self.reload_ammo_amount)
883                 return;
884
885         // our weapon is fully loaded, no need to reload
886         if (self.clip_load >= self.reload_ammo_amount)
887                 return;
888
889         // no ammo, so nothing to load
890         if(!self.(self.current_ammo) && self.reload_ammo_min)
891         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
892         {
893                 if(IS_REAL_CLIENT(self) && self.reload_complain < time)
894                 {
895                         play2(self, "weapons/unavailable.wav");
896                         sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));
897                         self.reload_complain = time + 1;
898                 }
899                 // switch away if the amount of ammo is not enough to keep using this weapon
900                 if (!(WEP_ACTION(self.weapon, WR_CHECKAMMO1) + WEP_ACTION(self.weapon, WR_CHECKAMMO2)))
901                 {
902                         self.clip_load = -1; // reload later
903                         W_SwitchToOtherWeapon(self);
904                 }
905                 return;
906         }
907
908         if (self.weaponentity)
909         {
910                 if (self.weaponentity.wframe == WFRAME_RELOAD)
911                         return;
912
913                 // allow switching away while reloading, but this will cause a new reload!
914                 self.weaponentity.state = WS_READY;
915         }
916
917         // now begin the reloading process
918
919         sound(self, CH_WEAPON_SINGLE, self.reload_sound, VOL_BASE, ATTEN_NORM);
920
921         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
922         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
923         // so your weapon is disabled for a few seconds without reason
924
925         //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
926
927         weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
928
929         if(self.clip_load < 0)
930                 self.clip_load = 0;
931         self.old_clip_load = self.clip_load;
932         self.clip_load = self.(weapon_load[self.weapon]) = -1;
933 }