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