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