]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/weaponsystem.qc
Viewmodel: animate clientside
[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/all.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 "../../lib/csqcmodel/sv_model.qh"
16
17 .int state;
18
19 .float weapon_frametime;
20
21 float W_WeaponRateFactor()
22 {
23         float t = 1.0 / g_weaponratefactor;
24
25         MUTATOR_CALLHOOK(WeaponRateFactor, t);
26         t = weapon_rate;
27
28         return t;
29 }
30
31 float W_WeaponSpeedFactor()
32 {
33         float t = 1.0 * g_weaponspeedfactor;
34
35         MUTATOR_CALLHOOK(WeaponSpeedFactor, t);
36         t = ret_float;
37
38         return t;
39 }
40
41
42 void weapon_thinkf(entity actor, .entity weaponentity, float fr, float t, void(Weapon thiswep, entity actor,
43     .entity weaponentity, int fire) func);
44
45 bool CL_Weaponentity_CustomizeEntityForClient()
46 {
47         SELFPARAM();
48         this.viewmodelforclient = this.owner;
49         if (IS_SPEC(other) && other.enemy == this.owner) this.viewmodelforclient = other;
50         return true;
51 }
52
53 vector CL_Weapon_GetShotOrg(int wpn)
54 {
55         entity wi = Weapons_from(wpn);
56         entity e = spawn();
57         CL_WeaponEntity_SetModel(e, wi.mdl);
58         vector ret = e.movedir;
59         CL_WeaponEntity_SetModel(e, "");
60         remove(e);
61         return ret;
62 }
63
64 void CL_Weaponentity_Think()
65 {
66         SELFPARAM();
67         this.nextthink = time;
68         if (intermission_running) this.frame = this.anim_idle.x;
69         .entity weaponentity = weaponentities[0];  // TODO: unhardcode
70         if (this.owner.(weaponentity) != this)
71         {
72                 // owner has new gun; remove self
73                 if (this.weaponchild) remove(this.weaponchild);
74                 remove(this);
75                 return;
76         }
77         if (this.owner.deadflag != DEAD_NO)
78         {
79                 // owner died; disappear
80                 this.model = "";
81                 if (this.weaponchild) this.weaponchild.model = "";
82                 return;
83         }
84         if (this.weaponname != this.owner.weaponname
85             || this.dmg != this.owner.modelindex
86             || this.deadflag != this.owner.deadflag)
87         {
88                 // owner changed weapons; update appearance
89                 this.weaponname = this.owner.weaponname;
90                 this.dmg = this.owner.modelindex;
91                 this.deadflag = this.owner.deadflag;
92
93                 CL_WeaponEntity_SetModel(this, this.owner.weaponname);
94         }
95
96         int tb = (this.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
97         this.effects = this.owner.effects
98             & EFMASK_CHEAP
99             & ~(
100             EF_LOWPRECISION
101             | EF_FULLBRIGHT  // can mask team color, so get rid of it
102             | EF_TELEPORT_BIT
103             | EF_RESTARTANIM_BIT
104                )
105             | tb
106             // TODO: don't render this entity at all
107             | EF_NODRAW;
108
109         if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
110         else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
111         else this.alpha = 1;
112
113         this.glowmod = this.owner.weaponentity_glowmod;
114         this.colormap = this.owner.colormap;
115         if (this.weaponchild)
116         {
117                 this.weaponchild.effects = this.effects;
118                 this.weaponchild.alpha = this.alpha;
119                 this.weaponchild.colormap = this.colormap;
120                 this.weaponchild.glowmod = this.glowmod;
121         }
122 }
123
124 void CL_ExteriorWeaponentity_Think()
125 {
126         SELFPARAM();
127         this.nextthink = time;
128         if (this.owner.exteriorweaponentity != this)
129         {
130                 remove(this);
131                 return;
132         }
133         if (this.owner.deadflag != DEAD_NO)
134         {
135                 this.model = "";
136                 return;
137         }
138         if (this.weaponname != this.owner.weaponname || this.dmg != this.owner.modelindex
139             || this.deadflag != this.owner.deadflag)
140         {
141                 this.weaponname = this.owner.weaponname;
142                 this.dmg = this.owner.modelindex;
143                 this.deadflag = this.owner.deadflag;
144                 if (this.owner.weaponname != "") _setmodel(this, W_Model(strcat("v_", this.owner.weaponname, ".md3")));
145                 else this.model = "";
146
147                 int tag_found;
148                 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
149                 {
150                         this.tag_index = tag_found;
151                         this.tag_entity = this.owner;
152                 }
153                 else
154                 {
155                         setattachment(this, this.owner, "bip01 r hand");
156                 }
157         }
158         this.effects = this.owner.effects;
159         this.effects |= EF_LOWPRECISION;
160         this.effects = this.effects & EFMASK_CHEAP;  // eat performance
161         if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
162         else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
163         else this.alpha = 1;
164
165         this.glowmod = this.owner.weaponentity_glowmod;
166         this.colormap = this.owner.colormap;
167
168         CSQCMODEL_AUTOUPDATE(this);
169 }
170
171 // spawning weaponentity for client
172 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
173 {
174         entity view = actor.(weaponentity) = new(weaponentity);
175         view.solid = SOLID_NOT;
176         view.owner = actor;
177         setmodel(view, MDL_Null);  // precision set when changed
178         setorigin(view, '0 0 0');
179         view.think = CL_Weaponentity_Think;
180         view.nextthink = time;
181         view.viewmodelforclient = actor;
182         view.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
183
184         if (weaponentity == weaponentities[0])
185         {
186                 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
187                 exterior.solid = SOLID_NOT;
188                 exterior.owner = actor;
189                 setorigin(exterior, '0 0 0');
190                 exterior.think = CL_ExteriorWeaponentity_Think;
191                 exterior.nextthink = time;
192
193                 CSQCMODEL_AUTOINIT(exterior);
194         }
195 }
196
197 // Weapon subs
198 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
199 {
200         if (actor.weapon != -1)
201         {
202                 actor.weapon = 0;
203                 actor.switchingweapon = 0;
204         }
205         if (actor.(weaponentity))
206         {
207                 actor.(weaponentity).state = WS_CLEAR;
208                 actor.(weaponentity).effects = 0;
209         }
210 }
211
212 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
213 {
214         if (actor.(weaponentity)) actor.(weaponentity).state = WS_READY;
215         weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
216 }
217
218 .float prevdryfire;
219 .float prevwarntime;
220 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary)
221 {
222         if ((actor.items & IT_UNLIMITED_WEAPON_AMMO)) return true;
223         bool ammo = false;
224         if (secondary) WITH(entity, self, actor, ammo = thiswep.wr_checkammo2(thiswep));
225         else WITH(entity, self, actor, ammo = thiswep.wr_checkammo1(thiswep));
226         if (ammo) return true;
227         // always keep the Mine Layer if we placed mines, so that we can detonate them
228         if (thiswep == WEP_MINE_LAYER)
229                 for (entity mine; (mine = find(mine, classname, "mine")); )
230                         if (mine.owner == actor) return false;
231
232         if (thiswep == WEP_SHOTGUN)
233                 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false;             // no clicking, just allow
234
235         if (thiswep == Weapons_from(actor.switchweapon) && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
236         {
237                 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
238                 actor.prevdryfire = time;
239         }
240
241         // check if the other firing mode has enough ammo
242         bool ammo_other = false;
243         if (secondary) WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo1(thiswep));
244         else WITH(entity, self, actor, ammo_other = thiswep.wr_checkammo2(thiswep));
245         if (ammo_other)
246         {
247                 if (time - actor.prevwarntime > 1)
248                 {
249                         Send_Notification(
250                                 NOTIF_ONE,
251                                 actor,
252                                 MSG_MULTI,
253                                 ITEM_WEAPON_PRIMORSEC,
254                                 thiswep.m_id,
255                                 secondary,
256                                 (1 - secondary)
257                                          );
258                 }
259                 actor.prevwarntime = time;
260         }
261         else  // this weapon is totally unable to fire, switch to another one
262         {
263                 W_SwitchToOtherWeapon(actor);
264         }
265
266         return false;
267 }
268
269 .float race_penalty;
270 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
271 {
272         if (!weapon_prepareattack_checkammo(thiswep, actor, secondary)) return false;
273
274         // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
275         // if all players readied up and the countdown is running
276         if (time < game_starttime || time < actor.race_penalty) return false;
277
278         if (timeout_status == TIMEOUT_ACTIVE)  // don't allow the player to shoot while game is paused
279                 return false;
280
281         // do not even think about shooting if switching
282         if (actor.switchweapon != actor.weapon) return false;
283
284         if (attacktime >= 0)
285         {
286                 int slot = weaponslot(weaponentity);
287                 // don't fire if previous attack is not finished
288                 if (ATTACK_FINISHED(actor, slot) > time + actor.weapon_frametime * 0.5) return false;
289                 // don't fire while changing weapon
290                 if (actor.(weaponentity).state != WS_READY) return false;
291         }
292         return true;
293 }
294
295 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
296 {
297         actor.(weaponentity).state = WS_INUSE;
298
299         actor.spawnshieldtime = min(actor.spawnshieldtime, time);  // kill spawn shield when you fire
300
301         // if the weapon hasn't been firing continuously, reset the timer
302         if (attacktime >= 0)
303         {
304                 int slot = weaponslot(weaponentity);
305                 if (ATTACK_FINISHED(actor, slot) < time - actor.weapon_frametime * 1.5)
306                 {
307                         ATTACK_FINISHED(actor, slot) = time;
308                         // dprint("resetting attack finished to ", ftos(time), "\n");
309                 }
310                 ATTACK_FINISHED(actor, slot) = ATTACK_FINISHED(actor, slot) + attacktime * W_WeaponRateFactor();
311         }
312         actor.bulletcounter += 1;
313         // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, slot)), "\n");
314 }
315
316 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
317 {
318         if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
319         {
320                 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
321                 return true;
322         }
323         return false;
324 }
325
326 void wframe_send(entity actor, .entity weaponentity, vector a, bool restartanim);
327
328 void weapon_thinkf(entity actor, .entity weaponentity, float fr, float t, void(Weapon thiswep, entity actor,
329         .entity weaponentity, int fire) func)
330 {
331         bool restartanim;
332         if (fr == WFRAME_DONTCHANGE)
333         {
334                 fr = actor.(weaponentity).wframe;
335                 restartanim = false;
336         }
337         else if (fr == WFRAME_IDLE)
338         {
339                 restartanim = false;
340         }
341         else
342         {
343                 restartanim = true;
344         }
345
346         vector of = v_forward;
347         vector or = v_right;
348         vector ou = v_up;
349
350         if (actor.(weaponentity))
351         {
352                 actor.(weaponentity).wframe = fr;
353                 vector a = '0 0 0';
354                 if (fr == WFRAME_IDLE) a = actor.(weaponentity).anim_idle;
355                 else if (fr == WFRAME_FIRE1) a = actor.(weaponentity).anim_fire1;
356                 else if (fr == WFRAME_FIRE2) a = actor.(weaponentity).anim_fire2;
357                 else  // if (fr == WFRAME_RELOAD)
358                         a = actor.(weaponentity).anim_reload;
359                 a.z *= g_weaponratefactor;
360                 wframe_send(actor, weaponentity, a, restartanim);
361         }
362
363         v_forward = of;
364         v_right = or;
365         v_up = ou;
366
367         if (actor.weapon_think == w_ready && func != w_ready && actor.(weaponentity).state == WS_RAISE) backtrace(
368                         "Tried to override initial weapon think function - should this really happen?");
369
370         t *= W_WeaponRateFactor();
371
372         // VorteX: haste can be added here
373         if (actor.weapon_think == w_ready)
374         {
375                 actor.weapon_nextthink = time;
376                 // dprint("started firing at ", ftos(time), "\n");
377         }
378         if (actor.weapon_nextthink < time - actor.weapon_frametime * 1.5
379             || actor.weapon_nextthink > time + actor.weapon_frametime * 1.5)
380         {
381                 actor.weapon_nextthink = time;
382                 // dprint("reset weapon animation timer at ", ftos(time), "\n");
383         }
384         actor.weapon_nextthink += t;
385         actor.weapon_think = func;
386         // dprint("next ", ftos(actor.weapon_nextthink), "\n");
387
388         if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
389         {
390                 if ((actor.weapon == WEP_SHOCKWAVE.m_id || actor.weapon == WEP_SHOTGUN.m_id)
391                     && fr == WFRAME_FIRE2) animdecide_setaction(actor, ANIMACTION_MELEE, restartanim);
392                 else animdecide_setaction(actor, ANIMACTION_SHOOT, restartanim);
393         }
394         else
395         {
396                 if (actor.anim_upper_action == ANIMACTION_SHOOT
397                     || actor.anim_upper_action == ANIMACTION_MELEE) actor.anim_upper_action = 0;
398         }
399 }
400
401 bool forbidWeaponUse(entity player)
402 {
403         if (time < game_starttime && !autocvar_sv_ready_restart_after_countdown) return true;
404         if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
405         if (player.player_blocked) return true;
406         if (player.frozen) return true;
407         if (player.weapon_blocked) return true;
408         return false;
409 }
410
411 .bool hook_switchweapon;
412
413 void W_WeaponFrame(entity actor)
414 {
415         .entity weaponentity = weaponentities[0];              // TODO: unhardcode
416         if (frametime) actor.weapon_frametime = frametime;
417
418         if (!actor.(weaponentity) || actor.health < 1) return; // Dead player can't use weapons and injure impulse commands
419
420         if (forbidWeaponUse(actor))
421         {
422                 if (actor.(weaponentity).state != WS_CLEAR)
423                 {
424                         Weapon wpn = Weapons_from(actor.weapon);
425                         w_ready(wpn, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
426                         return;
427                 }
428         }
429
430         if (actor.switchweapon == 0)
431         {
432                 actor.weapon = 0;
433                 actor.switchingweapon = 0;
434                 actor.(weaponentity).state = WS_CLEAR;
435                 actor.weaponname = "";
436                 // actor.items &= ~IT_AMMO;
437                 return;
438         }
439
440         makevectors(actor.v_angle);
441         vector fo = v_forward;  // save them in case the weapon think functions change it
442         vector ri = v_right;
443         vector up = v_up;
444
445         // Change weapon
446         if (actor.weapon != actor.switchweapon)
447         {
448                 switch (actor.(weaponentity).state)
449                 {
450                         default:
451                                 LOG_WARNINGF("unhandled weaponentity (%i) state for player (%i): %d\n", actor.(weaponentity), actor, actor.(weaponentity).state);
452                                 break;
453                         case WS_INUSE:
454                         case WS_RAISE:
455                                 break;
456                         case WS_CLEAR:
457                         {
458                                 // end switching!
459                                 actor.switchingweapon = actor.switchweapon;
460                                 entity newwep = Weapons_from(actor.switchweapon);
461
462                                 // the two weapon entities will notice this has changed and update their models
463                                 actor.weapon = actor.switchweapon;
464                                 actor.weaponname = newwep.mdl;
465                                 actor.bulletcounter = 0;
466                                 actor.ammo_field = newwep.ammo_field;
467                                 newwep.wr_setup(newwep);
468                                 actor.(weaponentity).state = WS_RAISE;
469
470                                 // set our clip load to the load of the weapon we switched to, if it's reloadable
471                                 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo)  // prevent accessing undefined cvars
472                                 {
473                                         actor.clip_load = actor.(weapon_load[actor.switchweapon]);
474                                         actor.clip_size = newwep.reloading_ammo;
475                                 }
476                                 else
477                                 {
478                                         actor.clip_load = actor.clip_size = 0;
479                                 }
480
481                                 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, newwep.switchdelay_raise, w_ready);
482                                 break;
483                         }
484                         case WS_DROP:
485                         {
486                                 // in dropping phase we can switch at any time
487                                 actor.switchingweapon = actor.switchweapon;
488                                 break;
489                         }
490                         case WS_READY:
491                         {
492                                 // start switching!
493                                 actor.switchingweapon = actor.switchweapon;
494                                 entity oldwep = Weapons_from(actor.weapon);
495
496                                 // set up weapon switch think in the future, and start drop anim
497                                 if (
498 #if INDEPENDENT_ATTACK_FINISHED
499                                             true
500 #else
501                                             ATTACK_FINISHED(actor, slot) <= time + actor.weapon_frametime * 0.5
502 #endif
503                                    )
504                                 {
505                                         sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
506                                         actor.(weaponentity).state = WS_DROP;
507                                         weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
508                                 }
509                                 break;
510                         }
511                 }
512         }
513
514         // LordHavoc: network timing test code
515         // if (actor.button0)
516         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, slot)), " >= ", ftos(actor.weapon_nextthink), "\n");
517
518         int w = actor.weapon;
519
520         // call the think code which may fire the weapon
521         // and do so multiple times to resolve framerate dependency issues if the
522         // server framerate is very low and the weapon fire rate very high
523         int c = 0;
524         while (c < W_TICSPERFRAME)
525         {
526                 c += 1;
527                 if (w && !(actor.weapons & WepSet_FromWeapon(w)))
528                 {
529                         if (actor.weapon == actor.switchweapon) W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
530                         w = 0;
531                 }
532
533                 v_forward = fo;
534                 v_right = ri;
535                 v_up = up;
536
537                 bool block_weapon = false;
538                 {
539                         bool key_pressed = actor.BUTTON_HOOK && !actor.vehicle;
540                         Weapon off = actor.offhand;
541                         if (off && !(actor.weapons & WEPSET(HOOK)))
542                         {
543                                 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
544                         }
545                         else
546                         {
547                                 if (key_pressed && actor.switchweapon != WEP_HOOK.m_id && !actor.hook_switchweapon) W_SwitchWeapon(
548                                                 WEP_HOOK.m_id);
549                                 actor.hook_switchweapon = key_pressed;
550                                 Weapon h = WEP_HOOK;
551                                 block_weapon = (actor.weapon == h.m_id && (actor.BUTTON_ATCK || key_pressed));
552                                 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
553                         }
554                 }
555
556                 v_forward = fo;
557                 v_right = ri;
558                 v_up = up;
559
560                 if (!block_weapon)
561                 {
562                         if (w)
563                         {
564                                 Weapon e = Weapons_from(actor.weapon);
565                                 e.wr_think(e, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
566                         }
567                         else
568                         {
569                                 Weapon w = Weapons_from(actor.weapon);
570                                 w.wr_gonethink(w);
571                         }
572                 }
573
574                 if (time + actor.weapon_frametime * 0.5 >= actor.weapon_nextthink)
575                 {
576                         if (actor.weapon_think)
577                         {
578                                 v_forward = fo;
579                                 v_right = ri;
580                                 v_up = up;
581                                 Weapon wpn = Weapons_from(actor.weapon);
582                                 actor.weapon_think(wpn, actor, weaponentity,
583                                         (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
584                         }
585                         else
586                         {
587                                 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
588                         }
589                 }
590         }
591 }
592
593 void W_AttachToShotorg(entity actor, entity flash, vector offset)
594 {
595         .entity weaponentity = weaponentities[0];
596         flash.owner = actor;
597         flash.angles_z = random() * 360;
598
599         entity view = actor.(weaponentity);
600         entity exterior = actor.exteriorweaponentity;
601
602         if (gettagindex(view, "shot")) setattachment(flash, view, "shot");
603         else setattachment(flash, view, "tag_shot");
604         setorigin(flash, offset);
605
606         entity xflash = spawn();
607         copyentity(flash, xflash);
608
609         flash.viewmodelforclient = actor;
610
611         if (view.oldorigin.x > 0)
612         {
613                 setattachment(xflash, exterior, "");
614                 setorigin(xflash, view.oldorigin + offset);
615         }
616         else
617         {
618                 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
619                 else setattachment(xflash, exterior, "tag_shot");
620                 setorigin(xflash, offset);
621         }
622 }
623
624 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use)
625 {
626         if (cvar("g_overkill"))
627         {
628                 if (actor.ok_use_ammocharge)
629                 {
630                         ok_DecreaseCharge(actor, actor.weapon);
631                         return;  // TODO
632                 }
633         }
634
635         if ((actor.items & IT_UNLIMITED_WEAPON_AMMO) && !wep.reloading_ammo) return;
636
637         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
638         if (wep.reloading_ammo)
639         {
640                 actor.clip_load -= ammo_use;
641                 actor.(weapon_load[actor.weapon]) = actor.clip_load;
642         }
643         else if (wep.ammo_field != ammo_none)
644         {
645                 actor.(wep.ammo_field) -= ammo_use;
646                 if (actor.(wep.ammo_field) < 0)
647                 {
648                         backtrace(sprintf(
649                                 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
650                                 "Please notify Samual immediately with a copy of this backtrace!\n",
651                                 ammo_use,
652                                 wep.netname,
653                                 GetAmmoPicture(wep.ammo_field),
654                                 actor.netname,
655                                 actor.(wep.ammo_field)
656                                              ));
657                 }
658         }
659 }
660
661 // weapon reloading code
662
663 .float reload_ammo_amount, reload_ammo_min, reload_time;
664 .float reload_complain;
665 .string reload_sound;
666
667 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
668 {
669         // finish the reloading process, and do the ammo transfer
670
671         actor.clip_load = actor.old_clip_load;  // restore the ammo counter, in case we still had ammo in the weapon before reloading
672
673         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
674         if (!actor.reload_ammo_min || actor.items & IT_UNLIMITED_WEAPON_AMMO || actor.ammo_field == ammo_none)
675         {
676                 actor.clip_load = actor.reload_ammo_amount;
677         }
678         else
679         {
680                 // make sure we don't add more ammo than we have
681                 float load = min(actor.reload_ammo_amount - actor.clip_load, actor.(actor.ammo_field));
682                 actor.clip_load += load;
683                 actor.(actor.ammo_field) -= load;
684         }
685         actor.(weapon_load[actor.weapon]) = actor.clip_load;
686
687         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
688         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
689         // so your weapon is disabled for a few seconds without reason
690
691         // ATTACK_FINISHED(actor, slot) -= actor.reload_time - 1;
692
693         Weapon wpn = Weapons_from(actor.weapon);
694         w_ready(wpn, actor, weaponentity, (actor.BUTTON_ATCK ? 1 : 0) | (actor.BUTTON_ATCK2 ? 2 : 0));
695 }
696
697 void W_Reload(entity actor, float sent_ammo_min, string sent_sound)
698 {
699         .entity weaponentity = weaponentities[0];
700         // set global values to work with
701         entity e = Weapons_from(actor.weapon);
702
703         if (cvar("g_overkill"))
704                 if (actor.ok_use_ammocharge) return;
705         // TODO
706
707         actor.reload_ammo_min = sent_ammo_min;
708         actor.reload_ammo_amount = e.reloading_ammo;
709         actor.reload_time = e.reloading_time;
710         actor.reload_sound = sent_sound;
711
712         // don't reload weapons that don't have the RELOADABLE flag
713         if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
714         {
715                 LOG_TRACE(
716                         "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
717                 return;
718         }
719
720         // return if reloading is disabled for this weapon
721         if (!actor.reload_ammo_amount) return;
722
723         // our weapon is fully loaded, no need to reload
724         if (actor.clip_load >= actor.reload_ammo_amount) return;
725
726         // no ammo, so nothing to load
727         if (actor.ammo_field != ammo_none)
728         {
729                 if (!actor.(actor.ammo_field) && actor.reload_ammo_min)
730                 {
731                         if (!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
732                         {
733                                 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
734                                 {
735                                         play2(actor, SND(UNAVAILABLE));
736                                         sprint(actor, strcat("You don't have enough ammo to reload the ^2", WEP_NAME(actor.weapon), "\n"));
737                                         actor.reload_complain = time + 1;
738                                 }
739                                 // switch away if the amount of ammo is not enough to keep using this weapon
740                                 Weapon w = Weapons_from(actor.weapon);
741                                 if (!(w.wr_checkammo1(w) + w.wr_checkammo2(w)))
742                                 {
743                                         actor.clip_load = -1;  // reload later
744                                         W_SwitchToOtherWeapon(actor);
745                                 }
746                                 return;
747                         }
748                 }
749         }
750
751         if (actor.(weaponentity))
752         {
753                 if (actor.(weaponentity).wframe == WFRAME_RELOAD) return;
754
755                 // allow switching away while reloading, but this will cause a new reload!
756                 actor.(weaponentity).state = WS_READY;
757         }
758
759         // now begin the reloading process
760
761         _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
762
763         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
764         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
765         // so your weapon is disabled for a few seconds without reason
766
767         // ATTACK_FINISHED(actor, slot) = max(time, ATTACK_FINISHED(actor, slot)) + actor.reload_time + 1;
768
769         weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, actor.reload_time, W_ReloadedAndReady);
770
771         if (actor.clip_load < 0) actor.clip_load = 0;
772         actor.old_clip_load = actor.clip_load;
773         actor.clip_load = actor.(weapon_load[actor.weapon]) = -1;
774 }
775
776 void W_DropEvent(.void(Weapon) event, entity player, float weapon_type, entity weapon_item)
777 {
778         Weapon w = Weapons_from(weapon_type);
779         weapon_dropevent_item = weapon_item;
780         WITH(entity, self, player, w.event(w));
781 }