]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/items/items.qc
items: alpha calculation rework
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / items / items.qc
1 #include "items.qh"
2
3 #include <common/constants.qh>
4 #include <common/deathtypes/all.qh>
5 #include <common/gamemodes/gamemode/cts/cts.qh>
6 #include <common/items/_mod.qh>
7 #include <common/mapobjects/subs.qh>
8 #include <common/mapobjects/triggers.qh>
9 #include <common/monsters/_mod.qh>
10 #include <common/mutators/mutator/buffs/buffs.qh>
11 #include <common/mutators/mutator/buffs/sv_buffs.qh>
12 #include <common/mutators/mutator/powerups/_mod.qh>
13 #include <common/mutators/mutator/status_effects/_mod.qh>
14 #include <common/net_linked.qh>
15 #include <common/notifications/all.qh>
16 #include <common/resources/resources.qh>
17 #include <common/util.qh>
18 #include <common/weapons/_all.qh>
19 #include <common/wepent.qh>
20 #include <lib/warpzone/common.qh>
21 #include <lib/warpzone/util_server.qh>
22 #include <server/bot/api.qh>
23 #include <server/command/vote.qh>
24 #include <server/damage.qh>
25 #include <server/mutators/_mod.qh>
26 #include <server/teamplay.qh>
27 #include <server/weapons/common.qh>
28 #include <server/weapons/selection.qh>
29 #include <server/weapons/weaponsystem.qh>
30 #include <server/world.qh>
31
32 bool ItemSend(entity this, entity to, int sf)
33 {
34         if(this.gravity)
35                 sf |= ISF_DROP;
36         else
37                 sf &= ~ISF_DROP;
38
39         WriteHeader(MSG_ENTITY, ENT_CLIENT_ITEM);
40         WriteByte(MSG_ENTITY, sf);
41
42         //WriteByte(MSG_ENTITY, this.cnt);
43         if(sf & ISF_LOCATION)
44         {
45                 WriteVector(MSG_ENTITY, this.origin);
46         }
47
48         if(sf & ISF_ANGLES)
49         {
50                 WriteAngleVector(MSG_ENTITY, this.angles);
51         }
52
53         // sets size on the client, unused on server
54         //if(sf & ISF_SIZE)
55
56         if(sf & ISF_STATUS)
57                 WriteByte(MSG_ENTITY, this.ItemStatus);
58
59         if(sf & ISF_MODEL)
60         {
61                 WriteShort(MSG_ENTITY, bound(0, this.fade_end, 32767));
62
63                 if(this.mdl == "")
64                         LOG_TRACE("^1WARNING!^7 this.mdl is unset for item ", this.classname, "expect a crash just about now");
65
66                 WriteString(MSG_ENTITY, this.mdl);
67                 WriteByte(MSG_ENTITY, bound(0, this.skin, 255));
68         }
69
70         if(sf & ISF_COLORMAP)
71         {
72                 WriteShort(MSG_ENTITY, this.colormap);
73                 WriteByte(MSG_ENTITY, this.glowmod.x * 255.0);
74                 WriteByte(MSG_ENTITY, this.glowmod.y * 255.0);
75                 WriteByte(MSG_ENTITY, this.glowmod.z * 255.0);
76         }
77
78         if(sf & ISF_DROP)
79         {
80                 WriteVector(MSG_ENTITY, this.velocity);
81         }
82
83         return true;
84 }
85
86 void ItemUpdate(entity this)
87 {
88         this.oldorigin = this.origin;
89         this.SendFlags |= ISF_LOCATION;
90 }
91
92 void UpdateItemAfterTeleport(entity this)
93 {
94         if(getSendEntity(this) == ItemSend)
95                 ItemUpdate(this);
96 }
97
98 bool have_pickup_item(entity this)
99 {
100         if (this.itemdef.spawnflags & ITEM_FLAG_MUTATORBLOCKED)
101                 return false;
102
103         if(this.itemdef.instanceOfPowerup)
104         {
105                 if(autocvar_g_powerups > 0)
106                         return true;
107                 if(autocvar_g_powerups == 0)
108                         return false;
109         }
110         else
111         {
112                 if(autocvar_g_pickup_items > 0)
113                         return true;
114                 if(autocvar_g_pickup_items == 0)
115                         return false;
116                 if(g_weaponarena)
117                         if(STAT(WEAPONS, this) || this.itemdef.instanceOfAmmo) // no item or ammo pickups in weaponarena
118                                 return false;
119         }
120         return true;
121 }
122
123 void Item_Show(entity e, int mode)
124 {
125         e.effects &= ~(EF_ADDITIVE | EF_STARDUST | EF_FULLBRIGHT | EF_NODEPTHTEST);
126         e.ItemStatus &= ~ITS_STAYWEP;
127         entity def = e.itemdef;
128         if (mode > 0)
129         {
130                 // make the item look normal, and be touchable
131                 e.model = e.mdl;
132                 e.solid = SOLID_TRIGGER;
133                 e.spawnshieldtime = 1;
134                 e.ItemStatus |= ITS_AVAILABLE;
135         }
136         else if (mode < 0)
137         {
138                 // hide the item completely
139                 e.model = string_null;
140                 e.solid = SOLID_NOT;
141                 e.spawnshieldtime = 1;
142                 e.ItemStatus &= ~ITS_AVAILABLE;
143         }
144         else
145         {
146                 bool nostay = def.instanceOfWeaponPickup ? !!(def.m_weapon.m_wepset & WEPSET_SUPERWEAPONS) : false // no weapon-stay on superweapons
147                         || e.team // weapon stay isn't supported for teamed weapons
148                         ;
149                 if(def.instanceOfWeaponPickup && !nostay && g_weapon_stay)
150                 {
151                         // make the item translucent and not touchable
152                         e.model = e.mdl;
153                         e.solid = SOLID_TRIGGER; // can STILL be picked up!
154                         e.effects |= EF_STARDUST;
155                         e.spawnshieldtime = 0; // field indicates whether picking it up may give you anything other than the weapon
156                         e.ItemStatus |= (ITS_AVAILABLE | ITS_STAYWEP);
157                 }
158                 else
159                 {
160                         //setmodel(e, "null");
161                         e.solid = SOLID_NOT;
162                         e.colormod = '0 0 0';
163                         //e.glowmod = e.colormod;
164                         e.spawnshieldtime = 1;
165                         e.ItemStatus &= ~ITS_AVAILABLE;
166                 }
167         }
168
169         if (def.m_glow)
170                 e.ItemStatus |= ITS_GLOW;
171
172         if (autocvar_g_nodepthtestitems)
173                 e.effects |= EF_NODEPTHTEST;
174
175         if (autocvar_g_fullbrightitems)
176                 e.ItemStatus |= ITS_ALLOWFB;
177         else
178                 e.ItemStatus &= ~ITS_ALLOWFB;
179
180         if (autocvar_sv_simple_items)
181                 e.ItemStatus |= ITS_ALLOWSI;
182
183         // relink entity (because solid may have changed)
184         setorigin(e, e.origin);
185         e.SendFlags |= ISF_STATUS;
186 }
187
188 void Item_Think(entity this)
189 {
190         if (Item_IsLoot(this))
191         {
192                 if (time < this.wait - IT_DESPAWNFX_TIME)
193                         this.nextthink = min(time + IT_UPDATE_INTERVAL, this.wait - IT_DESPAWNFX_TIME); // ensuring full time for effects
194                 else
195                 {
196                         // despawning soon, start effects
197                         this.ItemStatus |= ITS_EXPIRING;
198                         this.SendFlags |= ISF_STATUS;
199                         if (time < this.wait - IT_UPDATE_INTERVAL)
200                                 this.nextthink = time + IT_UPDATE_INTERVAL;
201                         else
202                         {
203                                 setthink(this, RemoveItem);
204                                 this.nextthink = this.wait;
205                         }
206                 }
207
208                 // enable pickup by the player who threw it
209                 this.owner = NULL;
210
211                 // send slow updates even if the item didn't move
212                 // recovers prediction desyncs where server thinks item stopped, client thinks it didn't
213                 ItemUpdate(this);
214         }
215         else
216         {
217                 // bones_was_here: TODO: predict movers, enable client prediction of items with a groundentity,
218                 // and then send those less often too (and not all on the same frame)
219                 this.nextthink = time;
220
221                 if(this.origin != this.oldorigin)
222                         ItemUpdate(this);
223         }
224 }
225
226 bool Item_ItemsTime_SpectatorOnly(GameItem it);
227 bool Item_ItemsTime_Allow(GameItem it);
228 float Item_ItemsTime_UpdateTime(entity e, float t);
229 void Item_ItemsTime_SetTime(entity e, float t);
230 void Item_ItemsTime_SetTimesForAllPlayers();
231
232 void Item_Respawn(entity this)
233 {
234         Item_Show(this, 1);
235         sound(this, CH_TRIGGER, this.itemdef.m_respawnsound, VOL_BASE, ATTEN_NORM);     // play respawn sound
236
237         if (Item_ItemsTime_Allow(this.itemdef) || (STAT(WEAPONS, this) & WEPSET_SUPERWEAPONS))
238         {
239                 float t = Item_ItemsTime_UpdateTime(this, 0);
240                 Item_ItemsTime_SetTime(this, t);
241                 Item_ItemsTime_SetTimesForAllPlayers();
242         }
243
244         setthink(this, Item_Think);
245         this.nextthink = time;
246
247         //Send_Effect(EFFECT_ITEM_RESPAWN, this.origin + this.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);
248         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
249 }
250
251 void Item_RespawnCountdown(entity this)
252 {
253         if(this.item_respawncounter >= ITEM_RESPAWN_TICKS)
254         {
255                 if(this.waypointsprite_attached)
256                         WaypointSprite_Kill(this.waypointsprite_attached);
257                 Item_Respawn(this);
258         }
259         else
260         {
261                 this.nextthink = time + 1;
262                 this.item_respawncounter += 1;
263                 if(this.item_respawncounter == 1)
264                 {
265                         do {
266                                 {
267                                         entity wi = REGISTRY_GET(Weapons, this.weapon);
268                                         if (wi != WEP_Null) {
269                                                 entity wp = WaypointSprite_Spawn(WP_Weapon, 0, 0, this, '0 0 64', NULL, 0, this, waypointsprite_attached, true, RADARICON_Weapon);
270                                                 wp.wp_extra = wi.m_id;
271                                                 break;
272                                         }
273                                 }
274                                 {
275                                         entity ii = this.itemdef;
276                                         if (ii != NULL) {
277                                                 entity wp = WaypointSprite_Spawn(WP_Item, 0, 0, this, '0 0 64', NULL, 0, this, waypointsprite_attached, true, RADARICON_Item);
278                                                 wp.wp_extra = ii.m_id;
279                                                 break;
280                                         }
281                                 }
282                         } while (0);
283                         bool mutator_returnvalue = MUTATOR_CALLHOOK(Item_RespawnCountdown, this);
284                         if(this.waypointsprite_attached)
285                         {
286                                 GameItem def = this.itemdef;
287                                 if (Item_ItemsTime_SpectatorOnly(def) && !mutator_returnvalue)
288                                         WaypointSprite_UpdateRule(this.waypointsprite_attached, 0, SPRITERULE_SPECTATOR);
289                                 WaypointSprite_UpdateBuildFinished(this.waypointsprite_attached, time + ITEM_RESPAWN_TICKS);
290                         }
291                 }
292
293                 if(this.waypointsprite_attached)
294                 {
295                         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
296                                 if(this.waypointsprite_attached.waypointsprite_visible_for_player(this.waypointsprite_attached, it, it))
297                                 {
298                                         msg_entity = it;
299                                         soundto(MSG_ONE, this, CH_TRIGGER, SND(ITEMRESPAWNCOUNTDOWN), VOL_BASE, ATTEN_NORM, 0); // play respawn sound
300                                 }
301                         });
302
303                         WaypointSprite_Ping(this.waypointsprite_attached);
304                         //WaypointSprite_UpdateHealth(this.waypointsprite_attached, this.item_respawncounter);
305                 }
306         }
307 }
308
309 void Item_RespawnThink(entity this)
310 {
311         this.nextthink = time;
312         if(this.origin != this.oldorigin)
313                 ItemUpdate(this);
314
315         if(time >= this.wait)
316                 Item_Respawn(this);
317 }
318
319 void Item_ScheduleRespawnIn(entity e, float t)
320 {
321         // if the respawn time is longer than 10 seconds, show a waypoint, otherwise, just respawn normally
322         if ((Item_ItemsTime_Allow(e.itemdef) || (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS) || MUTATOR_CALLHOOK(Item_ScheduleRespawn, e, t)) && (t - ITEM_RESPAWN_TICKS) > 0)
323         {
324                 setthink(e, Item_RespawnCountdown);
325                 e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);
326                 e.scheduledrespawntime = e.nextthink + ITEM_RESPAWN_TICKS;
327                 e.item_respawncounter = 0;
328                 if(Item_ItemsTime_Allow(e.itemdef) || (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS))
329                 {
330                         t = Item_ItemsTime_UpdateTime(e, e.scheduledrespawntime);
331                         Item_ItemsTime_SetTime(e, t);
332                         Item_ItemsTime_SetTimesForAllPlayers();
333                 }
334         }
335         else
336         {
337                 setthink(e, Item_RespawnThink);
338                 e.nextthink = time;
339                 e.scheduledrespawntime = time + t;
340                 e.wait = time + t;
341
342                 if(Item_ItemsTime_Allow(e.itemdef) || (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS))
343                 {
344                         t = Item_ItemsTime_UpdateTime(e, e.scheduledrespawntime);
345                         Item_ItemsTime_SetTime(e, t);
346                         Item_ItemsTime_SetTimesForAllPlayers();
347                 }
348         }
349 }
350
351 AUTOCVAR(g_pickup_respawntime_scaling_reciprocal, float, 0.0, "Multiply respawn time by `reciprocal / (p + offset) + linear` where `p` is the current number of players, takes effect with 2 or more players present, `reciprocal` (with `offset` and `linear` set to 0) can be used to achieve a constant number of items spawned *per player*");
352 AUTOCVAR(g_pickup_respawntime_scaling_offset, float, 0.0, "Multiply respawn time by `reciprocal / (p + offset) + linear` where `p` is the current number of players, takes effect with 2 or more players present, `offset` offsets the curve left or right - the results are not intuitive and I recommend plotting the respawn time and the number of items per player to see what's happening");
353 AUTOCVAR(g_pickup_respawntime_scaling_linear, float, 1.0, "Multiply respawn time by `reciprocal / (p + offset) + linear` where `p` is the current number of players, takes effect with 2 or more players present, `linear` can be used to simply scale the respawn time linearly");
354
355 /// Adjust respawn time according to the number of players.
356 float adjust_respawntime(float normal_respawntime) {
357         float r = autocvar_g_pickup_respawntime_scaling_reciprocal;
358         float o = autocvar_g_pickup_respawntime_scaling_offset;
359         float l = autocvar_g_pickup_respawntime_scaling_linear;
360
361         if (r == 0 && l == 1) {
362                 return normal_respawntime;
363         }
364
365         entity balance = TeamBalance_CheckAllowedTeams(NULL);
366         TeamBalance_GetTeamCounts(balance, NULL);
367         int players = 0;
368         for (int i = 1; i <= NUM_TEAMS; ++i)
369         {
370                 if (TeamBalance_IsTeamAllowed(balance, i))
371                 {
372                         players += TeamBalance_GetNumberOfPlayers(balance, i);
373                 }
374         }
375         TeamBalance_Destroy(balance);
376
377         if (players >= 2) {
378                 return normal_respawntime * (r / (players + o) + l);
379         } else {
380                 return normal_respawntime;
381         }
382 }
383
384 void Item_ScheduleRespawn(entity e)
385 {
386         if(e.respawntime > 0)
387         {
388                 Item_Show(e, 0);
389
390                 float adjusted_respawntime = adjust_respawntime(e.respawntime);
391                 //LOG_INFOF("item %s will respawn in %f", e.classname, adjusted_respawntime);
392
393                 // range: adjusted_respawntime - respawntimejitter .. adjusted_respawntime + respawntimejitter
394                 float respawn_in = adjusted_respawntime + crandom() * e.respawntimejitter;
395                 Item_ScheduleRespawnIn(e, respawn_in);
396         }
397         else // if respawntime is -1, this item does not respawn
398                 Item_Show(e, -1);
399 }
400
401 AUTOCVAR(g_pickup_respawntime_initial_random, int, 1,
402         "For items that don't start spawned: 0: spawn after their normal respawntime; 1: spawn after `random * respawntime` with the *same* random; 2: same as 1 but each item has separate random");
403
404 void Item_ScheduleInitialRespawn(entity e)
405 {
406         Item_Show(e, 0);
407
408         float spawn_in;
409         if (autocvar_g_pickup_respawntime_initial_random == 0)
410         {
411                 // range: respawntime .. respawntime + respawntimejitter
412                 spawn_in = e.respawntime + random() * e.respawntimejitter;
413         }
414         else
415         {
416                 float rnd;
417                 if (autocvar_g_pickup_respawntime_initial_random == 1)
418                 {
419                         static float shared_random = 0;
420                         // NOTE this code works only if items are scheduled at the same time (normal case)
421                         // NOTE2 random() can't return exactly 1 so this check always work as intended
422                         if (!shared_random || floor(time) > shared_random)
423                                 shared_random = floor(time) + random();
424                         rnd = shared_random - floor(time);
425                 }
426                 else
427                         rnd = random();
428
429                 // range:
430                 // if respawntime >= ITEM_RESPAWN_TICKS: ITEM_RESPAWN_TICKS .. respawntime + respawntimejitter
431                 // else: 0 .. ITEM_RESPAWN_TICKS
432                 // this is to prevent powerups spawning unexpectedly without waypoints
433                 spawn_in = ITEM_RESPAWN_TICKS + rnd * (e.respawntime + e.respawntimejitter - ITEM_RESPAWN_TICKS);
434         }
435
436         Item_ScheduleRespawnIn(e, max(0, game_starttime - time) + ((e.respawntimestart) ? e.respawntimestart : spawn_in));
437 }
438
439 void GiveRandomWeapons(entity receiver, int num_weapons, string weapon_names,
440         entity ammo_entity)
441 {
442         if (num_weapons == 0)
443         {
444                 return;
445         }
446         int num_potential_weapons = tokenize_console(weapon_names);
447         for (int give_attempt = 0; give_attempt < num_weapons; ++give_attempt)
448         {
449                 RandomSelection_Init();
450                 for (int weapon_index = 0; weapon_index < num_potential_weapons;
451                         ++weapon_index)
452                 {
453                         string weapon = argv(weapon_index);
454                         FOREACH(Weapons, it != WEP_Null,
455                         {
456                                 // Finding a weapon which player doesn't have.
457                                 if (!(STAT(WEAPONS, receiver) & it.m_wepset) && (it.netname == weapon))
458                                 {
459                                         RandomSelection_AddEnt(it, 1, 1);
460                                         break;
461                                 }
462                         });
463                 }
464                 if (RandomSelection_chosen_ent == NULL)
465                 {
466                         return;
467                 }
468                 STAT(WEAPONS, receiver) |= RandomSelection_chosen_ent.m_wepset;
469                 if (RandomSelection_chosen_ent.ammo_type == RES_NONE)
470                 {
471                         continue;
472                 }
473                 if (GetResource(receiver,
474                         RandomSelection_chosen_ent.ammo_type) != 0)
475                 {
476                         continue;
477                 }
478                 GiveResource(receiver, RandomSelection_chosen_ent.ammo_type,
479                         GetResource(ammo_entity,
480                         RandomSelection_chosen_ent.ammo_type));
481         }
482 }
483
484 bool Item_GiveAmmoTo(entity item, entity player, Resource res_type, float ammomax)
485 {
486         float amount = GetResource(item, res_type);
487         if (amount == 0)
488         {
489                 return false;
490         }
491         float player_amount = GetResource(player, res_type);
492         if (item.spawnshieldtime)
493         {
494                 if ((player_amount >= ammomax) && (item.pickup_anyway <= 0))
495                         return false;
496         }
497         else if (g_weapon_stay == 2)
498         {
499                 ammomax = min(amount, ammomax);
500                 if(player_amount >= ammomax)
501                         return false;
502         }
503         else
504                 return false;
505         if (amount < 0)
506                 TakeResourceWithLimit(player, res_type, -amount, ammomax);
507         else
508                 GiveResourceWithLimit(player, res_type, amount, ammomax);
509         return true;
510 }
511
512 void Item_NotifyWeapon(entity player, int wep)
513 {
514         FOREACH_CLIENT(IS_REAL_CLIENT(it) && (it == player || (IS_SPEC(it) && it.enemy == player)), {
515                 msg_entity = it;
516                 WriteHeader(MSG_ONE, TE_CSQC_WEAPONPICKUP);
517                 WriteByte(MSG_ONE, wep);
518         });
519 }
520
521 bool Item_GiveTo(entity item, entity player)
522 {
523         // if nothing happens to player, just return without taking the item
524         int _switchweapon = 0;
525         // in case the player has autoswitch enabled do the following:
526         // if the player is using their best weapon before items are given, they
527         // probably want to switch to an even better weapon after items are given
528
529         if(CS_CVAR(player).cvar_cl_autoswitch)
530         {
531                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
532                 {
533                         .entity weaponentity = weaponentities[slot];
534                         if(player.(weaponentity).m_weapon != WEP_Null || slot == 0)
535                         {
536                                 if(player.(weaponentity).m_switchweapon == w_getbestweapon(player, weaponentity))
537                                         _switchweapon |= BIT(slot);
538
539                                 if(!(STAT(WEAPONS, player) & WepSet_FromWeapon(player.(weaponentity).m_switchweapon)))
540                                         _switchweapon |= BIT(slot);
541                         }
542                 }
543         }
544         bool pickedup = false;
545         pickedup |= Item_GiveAmmoTo(item, player, RES_HEALTH, item.max_health);
546         pickedup |= Item_GiveAmmoTo(item, player, RES_ARMOR, item.max_armorvalue);
547         pickedup |= Item_GiveAmmoTo(item, player, RES_SHELLS, g_pickup_shells_max);
548         pickedup |= Item_GiveAmmoTo(item, player, RES_BULLETS, g_pickup_nails_max);
549         pickedup |= Item_GiveAmmoTo(item, player, RES_ROCKETS, g_pickup_rockets_max);
550         pickedup |= Item_GiveAmmoTo(item, player, RES_CELLS, g_pickup_cells_max);
551         pickedup |= Item_GiveAmmoTo(item, player, RES_PLASMA, g_pickup_plasma_max);
552         pickedup |= Item_GiveAmmoTo(item, player, RES_FUEL, g_pickup_fuel_max);
553         if (item.itemdef.instanceOfWeaponPickup)
554         {
555                 WepSet w, wp;
556                 w = STAT(WEAPONS, item);
557                 wp = w & ~STAT(WEAPONS, player);
558
559                 if (wp || (item.spawnshieldtime && item.pickup_anyway > 0))
560                 {
561                         pickedup = true;
562                         FOREACH(Weapons, it != WEP_Null, {
563                                 if(w & (it.m_wepset))
564                                         Item_NotifyWeapon(player, it.m_id);
565
566                                 if(wp & (it.m_wepset))
567                                 {
568                                         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
569                                         {
570                                                 .entity weaponentity = weaponentities[slot];
571                                                 if(player.(weaponentity).m_weapon != WEP_Null || slot == 0)
572                                                         W_DropEvent(wr_pickup, player, it.m_id, item, weaponentity);
573                                         }
574                                         W_GiveWeapon(player, it.m_id);
575                                 }
576                         });
577                 }
578         }
579
580         if (item.itemdef.instanceOfPowerup)
581         {
582                 if ((item.itemdef == ITEM_JetpackRegen) && !(player.items & IT_FUEL_REGEN))
583                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_FUELREGEN_GOT);
584                 else if ((item.itemdef == ITEM_Jetpack) && !(player.items & IT_JETPACK))
585                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_JETPACK_GOT);
586         }
587
588         int its;
589         if((its = (item.items - (item.items & player.items)) & IT_PICKUPMASK))
590         {
591                 pickedup = true;
592                 player.items |= its;
593                 // TODO: we probably want to show a message in the console, but not this one!
594                 //Send_Notification(NOTIF_ONE, player, MSG_INFO, INFO_ITEM_WEAPON_GOT, item.netname);
595         }
596
597         if (item.strength_finished)
598         {
599                 pickedup = true;
600                 float t = max(StatusEffects_gettime(STATUSEFFECT_Strength, player), time);
601                 if (autocvar_g_powerups_stack)
602                         t += item.strength_finished;
603                 else
604                         t = max(t, time + item.strength_finished);
605                 StatusEffects_apply(STATUSEFFECT_Strength, player, t, 0);
606         }
607         if (item.invincible_finished)
608         {
609                 pickedup = true;
610                 float t = max(StatusEffects_gettime(STATUSEFFECT_Shield, player), time);
611                 if (autocvar_g_powerups_stack)
612                         t += item.invincible_finished;
613                 else
614                         t = max(t, time + item.invincible_finished);
615                 StatusEffects_apply(STATUSEFFECT_Shield, player, t, 0);
616         }
617         if (item.speed_finished)
618         {
619                 pickedup = true;
620                 float t = max(StatusEffects_gettime(STATUSEFFECT_Speed, player), time);
621                 if (autocvar_g_powerups_stack)
622                         t += item.speed_finished;
623                 else
624                         t = max(t, time + item.speed_finished);
625                 StatusEffects_apply(STATUSEFFECT_Speed, player, t, 0);
626         }
627         if (item.invisibility_finished)
628         {
629                 pickedup = true;
630                 float t = max(StatusEffects_gettime(STATUSEFFECT_Invisibility, player), time);
631                 if (autocvar_g_powerups_stack)
632                         t += item.invisibility_finished;
633                 else
634                         t = max(t, time + item.invisibility_finished);
635                 StatusEffects_apply(STATUSEFFECT_Invisibility, player, t, 0);
636         }
637         if (item.superweapons_finished)
638         {
639                 pickedup = true;
640                 StatusEffects_apply(STATUSEFFECT_Superweapons, player, max(StatusEffects_gettime(STATUSEFFECT_Superweapons, player), time) + item.superweapons_finished, 0);
641         }
642
643         // always eat teamed entities
644         if(item.team)
645                 pickedup = true;
646
647         if (!pickedup)
648                 return false;
649
650         // crude hack to enforce switching weapons
651         if(g_cts && item.itemdef.instanceOfWeaponPickup && !CS_CVAR(player).cvar_cl_cts_noautoswitch)
652         {
653                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
654                 {
655                         .entity weaponentity = weaponentities[slot];
656                         if(player.(weaponentity).m_weapon != WEP_Null || slot == 0)
657                                 W_SwitchWeapon_Force(player, REGISTRY_GET(Weapons, item.weapon), weaponentity);
658                 }
659                 return true;
660         }
661
662         if(_switchweapon)
663         {
664                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
665                 {
666                         .entity weaponentity = weaponentities[slot];
667                         if(_switchweapon & BIT(slot))
668                         if(player.(weaponentity).m_switchweapon != w_getbestweapon(player, weaponentity))
669                                 W_SwitchWeapon_Force(player, w_getbestweapon(player, weaponentity), weaponentity);
670                 }
671         }
672
673         return true;
674 }
675
676 void Item_Touch(entity this, entity toucher)
677 {
678         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
679         if (Item_IsLoot(this))
680         {
681                 if (ITEM_TOUCH_NEEDKILL())
682                 {
683                         RemoveItem(this);
684                         return;
685                 }
686         }
687
688         if(!(toucher.flags & FL_PICKUPITEMS)
689         || STAT(FROZEN, toucher)
690         || IS_DEAD(toucher)
691         || (this.solid != SOLID_TRIGGER)
692         || (this.owner == toucher)
693         || (time < this.item_spawnshieldtime)
694         ) { return; }
695
696         switch (MUTATOR_CALLHOOK(ItemTouch, this, toucher))
697         {
698                 case MUT_ITEMTOUCH_RETURN: { return; }
699                 case MUT_ITEMTOUCH_PICKUP: { toucher = M_ARGV(1, entity); goto pickup; }
700         }
701
702         toucher = M_ARGV(1, entity);
703
704         if (Item_IsExpiring(this))
705         {
706                 this.strength_finished = max(0, this.strength_finished - time);
707                 this.invincible_finished = max(0, this.invincible_finished - time);
708                 this.speed_finished = max(0, this.speed_finished - time);
709                 this.invisibility_finished = max(0, this.invisibility_finished - time);
710                 this.superweapons_finished = max(0, this.superweapons_finished - time);
711         }
712         bool gave = ITEM_HANDLE(Pickup, this.itemdef, this, toucher);
713         if (!gave)
714         {
715                 if (Item_IsExpiring(this))
716                 {
717                         // undo what we did above
718                         this.strength_finished += time;
719                         this.invincible_finished += time;
720                         this.speed_finished += time;
721                         this.invisibility_finished += time;
722                         this.superweapons_finished += time;
723                 }
724                 return;
725         }
726
727 LABEL(pickup)
728
729         if(this.target && this.target != "" && this.target != "###item###") // defrag support
730                 SUB_UseTargets(this, toucher, NULL);
731
732         STAT(LAST_PICKUP, toucher) = time;
733
734         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
735         GameItem def = this.itemdef;
736         int ch = ((def.instanceOfPowerup && def.m_itemid != IT_RESOURCE) ? CH_TRIGGER_SINGLE : CH_TRIGGER);
737         string snd = (this.item_pickupsound ? this.item_pickupsound : Sound_fixpath(this.item_pickupsound_ent));
738         _sound(toucher, ch, snd, VOL_BASE, ATTEN_NORM);
739
740         MUTATOR_CALLHOOK(ItemTouched, this, toucher);
741         if (wasfreed(this))
742         {
743                 return;
744         }
745
746         if (Item_IsLoot(this))
747         {
748                 delete(this);
749                 return;
750         }
751         if (!this.spawnshieldtime)
752         {
753                 return;
754         }
755         entity e;
756         if (this.team)
757         {
758                 RandomSelection_Init();
759                 IL_EACH(g_items, it.team == this.team,
760                 {
761                         if (it.itemdef) // is a registered item
762                         {
763                                 Item_Show(it, -1);
764                                 it.scheduledrespawntime = 0;
765                                 RandomSelection_AddEnt(it, it.cnt, 0);
766                         }
767                 });
768                 e = RandomSelection_chosen_ent;
769                 Item_Show(e, 1); // reset its state so it is visible (extra sendflags doesn't matter, this happens anyway)
770         }
771         else
772                 e = this;
773         Item_ScheduleRespawn(e);
774 }
775
776 void Item_Reset(entity this)
777 {
778         Item_Show(this, !this.state);
779
780         if (Item_IsLoot(this))
781         {
782                 return;
783         }
784         setthink(this, Item_Think);
785         this.nextthink = time;
786         if (this.waypointsprite_attached)
787         {
788                 WaypointSprite_Kill(this.waypointsprite_attached);
789         }
790         if (this.itemdef.instanceOfPowerup || (STAT(WEAPONS, this) & WEPSET_SUPERWEAPONS)) // do not spawn powerups initially!
791         {
792                 Item_ScheduleInitialRespawn(this);
793         }
794 }
795
796 void Item_FindTeam(entity this)
797 {
798         if(!(this.effects & EF_NOGUNBOB)) // marker for item team search
799                 return;
800
801         LOG_TRACE("Initializing item team ", ftos(this.team));
802         RandomSelection_Init();
803         IL_EACH(g_items, it.team == this.team,
804         {
805                 if(it.itemdef) // is a registered item
806                         RandomSelection_AddEnt(it, it.cnt, 0);
807         });
808
809         entity e = RandomSelection_chosen_ent;
810         if (!e)
811                 return;
812
813         IL_EACH(g_items, it.team == this.team,
814         {
815                 if(it.itemdef) // is a registered item
816                 {
817                         if(it != e)
818                         {
819                                 Item_Show(it, -1); // make it non-spawned
820                                 if (it.waypointsprite_attached)
821                                         WaypointSprite_Kill(it.waypointsprite_attached);
822                                 it.nextthink = 0; // disable any scheduled powerup spawn
823                         }
824                         else
825                                 Item_Reset(it);
826
827                         // leave 'this' marked so Item_FindTeam() works when called again via this.reset
828                         if(it != this)
829                                 it.effects &= ~EF_NOGUNBOB;
830                 }
831         });
832 }
833
834 void Item_CopyFields(entity this, entity to)
835 {
836         setorigin(to, this.origin);
837         to.spawnflags = this.spawnflags;
838         to.noalign = Item_ShouldKeepPosition(this);
839         to.cnt = this.cnt;
840         to.team = this.team;
841         to.spawnfunc_checked = true;
842         // TODO: copy respawn times? this may not be desirable in some cases
843         //to.respawntime = this.respawntime;
844         //to.respawntimejitter = this.respawntimejitter;
845 }
846
847 // Savage: used for item garbage-collection
848 void RemoveItem(entity this)
849 {
850         if(wasfreed(this) || !this) { return; }
851         if(this.waypointsprite_attached)
852                 WaypointSprite_Kill(this.waypointsprite_attached);
853         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
854         delete(this);
855 }
856
857 // pickup evaluation functions
858 // these functions decide how desirable an item is to the bots
859
860 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;}
861
862 float weapon_pickupevalfunc(entity player, entity item)
863 {
864         // See if I have it already
865         if(STAT(WEAPONS, player) & STAT(WEAPONS, item))
866         {
867                 // If I can pick it up
868                 if(!item.spawnshieldtime)
869                         return 0;
870                 return ammo_pickupevalfunc(player, item);
871         }
872
873         // reduce weapon value if bot already got a good arsenal
874         float c = 1;
875         int weapons_value = 0;
876         FOREACH(Weapons, it != WEP_Null && (STAT(WEAPONS, player) & it.m_wepset), {
877                 weapons_value += it.bot_pickupbasevalue;
878         });
879         c -= bound(0, weapons_value / 20000, 1) * 0.5;
880
881         return item.bot_pickupbasevalue * c;
882 }
883
884 float ammo_pickupevalfunc(entity player, entity item)
885 {
886         entity item_resource = NULL; // pointer to the resource that may be associated with the given item
887         entity wpn = NULL;
888         float c = 0;
889         float rating = 0;
890
891         // detect needed ammo
892         if(item.itemdef.instanceOfWeaponPickup)
893         {
894                 entity res = item.itemdef.m_weapon.ammo_type;
895                 entity ammo = (res != RES_NONE) ? GetAmmoItem(res) : NULL;
896                 if(!ammo)
897                         return 0;
898                 if(res != RES_NONE && GetResource(item, res))
899                         item_resource = res;
900
901                 wpn = item;
902                 rating = ammo.m_botvalue;
903         }
904         else
905         {
906                 FOREACH(Weapons, it != WEP_Null, {
907                         if(!(STAT(WEAPONS, player) & (it.m_wepset)))
908                                 continue;
909                         if(it.ammo_type == RES_NONE)
910                                 continue;
911
912                         if(GetResource(item, it.ammo_type))
913                         {
914                                 item_resource = it.ammo_type;
915                                 break;
916                         }
917                 });
918                 rating = item.bot_pickupbasevalue;
919         }
920
921         float noammorating = 0.5;
922
923         if(item_resource && (GetResource(player, item_resource) < GetResourceLimit(player, item_resource)))
924                 c = GetResource(item, item_resource) / max(noammorating, GetResource(player, item_resource));
925
926         rating *= min(c, 2);
927         if(wpn)
928                 rating += wpn.bot_pickupbasevalue * 0.1;
929         return rating;
930 }
931
932 float healtharmor_pickupevalfunc(entity player, entity item)
933 {
934         float c = 0;
935         float rating = item.bot_pickupbasevalue;
936
937         float itemarmor = GetResource(item, RES_ARMOR);
938         float itemhealth = GetResource(item, RES_HEALTH);
939
940         if(item.item_group)
941         {
942                 itemarmor *= min(4, item.item_group_count);
943                 itemhealth *= min(4, item.item_group_count);
944         }
945
946         if (itemarmor && (GetResource(player, RES_ARMOR) < item.max_armorvalue))
947                 c = itemarmor / max(1, GetResource(player, RES_ARMOR) * 2/3 + GetResource(player, RES_HEALTH) * 1/3);
948
949         if (itemhealth && (GetResource(player, RES_HEALTH) < item.max_health))
950                 c = itemhealth / max(1, GetResource(player, RES_HEALTH));
951
952         rating *= min(2, c);
953         return rating;
954 }
955
956 void Item_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
957 {
958         if(ITEM_DAMAGE_NEEDKILL(deathtype))
959                 RemoveItem(this);
960 }
961
962 void item_use(entity this, entity actor, entity trigger)
963 {
964         // use the touch function to handle collection
965         gettouch(this)(this, actor);
966 }
967
968 // if defaultrespawntime is 0 get respawntime from the item definition
969 // if defaultrespawntimejitter is 0 get respawntimejitter from the item definition
970 void _StartItem(entity this, entity def, float defaultrespawntime, float defaultrespawntimejitter)
971 {
972         string itemname = def.m_name;
973         Model itemmodel = def.m_model;
974         Sound pickupsound = def.m_sound;
975         float(entity player, entity item) pickupevalfunc = def.m_pickupevalfunc;
976         float pickupbasevalue = def.m_botvalue;
977         int itemflags = def.m_itemflags;
978
979         startitem_failed = false;
980
981         this.item_model_ent = itemmodel;
982         this.item_pickupsound_ent = pickupsound;
983
984         if(def.m_iteminit)
985                 def.m_iteminit(def, this);
986
987         if(!this.pickup_anyway && def.m_pickupanyway)
988                 this.pickup_anyway = def.m_pickupanyway();
989
990         int itemid = def.m_itemid;
991         this.items = itemid;
992         int weaponid = def.instanceOfWeaponPickup ? def.m_weapon.m_id : 0;
993         this.weapon = weaponid;
994
995         // bones_was_here TODO: implement sv_cullentities_dist and replace g_items_maxdist with it
996         if(!this.fade_end)
997                 this.fade_end = autocvar_g_items_maxdist;
998
999         if(weaponid)
1000                 STAT(WEAPONS, this) = WepSet_FromWeapon(REGISTRY_GET(Weapons, weaponid));
1001
1002         this.flags = FL_ITEM | itemflags;
1003         IL_PUSH(g_items, this);
1004
1005         if(MUTATOR_CALLHOOK(FilterItem, this)) // error means we do not want the item
1006         {
1007                 startitem_failed = true;
1008                 delete(this);
1009                 return;
1010         }
1011
1012         if (Item_IsLoot(this))
1013         {
1014                 this.reset = RemoveItem;
1015                 set_movetype(this, MOVETYPE_TOSS);
1016
1017                 setthink(this, Item_Think);
1018                 this.nextthink = time + IT_UPDATE_INTERVAL;
1019                 this.wait = time + autocvar_g_items_dropped_lifetime;
1020
1021                 this.takedamage = DAMAGE_YES;
1022                 this.event_damage = Item_Damage;
1023                 // enable this to have thrown items burn in lava
1024                 //this.damagedbycontents = true;
1025                 //IL_PUSH(g_damagedbycontents, this);
1026
1027                 if (Item_IsExpiring(this))
1028                 {
1029                         // if item is worthless after a timer, have it expire then
1030                         this.nextthink = max(this.strength_finished, this.invincible_finished, this.superweapons_finished);
1031                 }
1032
1033                 // don't drop if in a NODROP zone (such as lava)
1034                 traceline(this.origin, this.origin, MOVE_NORMAL, this);
1035                 if (trace_dpstartcontents & DPCONTENTS_NODROP)
1036                 {
1037                         startitem_failed = true;
1038                         delete(this);
1039                         return;
1040                 }
1041         }
1042         else
1043         {
1044                 // must be done after def.m_iteminit() as that may set ITEM_FLAG_MUTATORBLOCKED
1045                 if(!have_pickup_item(this))
1046                 {
1047                         startitem_failed = true;
1048                         delete(this);
1049                         return;
1050                 }
1051
1052                 // must be done before Item_Reset() and after MUTATORBLOCKED check (blocked items may have null func ptrs)
1053                 if(!this.respawntime) // both need to be set
1054                 {
1055                         this.respawntime = defaultrespawntime ? defaultrespawntime : def.m_respawntime();
1056                         this.respawntimejitter = defaultrespawntimejitter ? defaultrespawntimejitter : def.m_respawntimejitter();
1057                 }
1058
1059                 if(this.angles != '0 0 0')
1060                         this.SendFlags |= ISF_ANGLES;
1061
1062                 if(q3compat && !this.team)
1063                 {
1064                         string t = GetField_fullspawndata(this, "team");
1065                         // bones_was_here: this hack is cheaper than changing to a .string strcmp()
1066                         if(t) this.team = crc16(false, t);
1067                 }
1068
1069                 this.reset = this.team ? Item_FindTeam : Item_Reset;
1070                 // it's a level item
1071                 if(this.spawnflags & 1)
1072                         this.noalign = 1;
1073                 if (this.noalign > 0)
1074                         set_movetype(this, MOVETYPE_NONE);
1075                 else
1076                         set_movetype(this, MOVETYPE_TOSS);
1077                 // do item filtering according to game mode and other things
1078                 if (this.noalign <= 0)
1079                 {
1080                         // first nudge it off the floor a little bit to avoid math errors
1081                         setorigin(this, this.origin + '0 0 1');
1082                         // set item size before we spawn a spawnfunc_waypoint
1083                         setsize(this, def.m_mins, def.m_maxs);
1084                         this.SendFlags |= ISF_SIZE;
1085                         // note droptofloor returns false if stuck/or would fall too far
1086                         if (!this.noalign)
1087                                 droptofloor(this);
1088                         waypoint_spawnforitem(this);
1089                 }
1090
1091                 /*
1092                  * can't do it that way, as it would break maps
1093                  * TODO make a target_give like entity another way, that perhaps has
1094                  * the weapon name in a key
1095                 if(this.targetname)
1096                 {
1097                         // target_give not yet supported; maybe later
1098                         print("removed targeted ", this.classname, "\n");
1099                         startitem_failed = true;
1100                         delete(this);
1101                         return;
1102                 }
1103                 */
1104
1105                 if(this.targetname != "" && (this.spawnflags & 16))
1106                         this.use = item_use;
1107
1108                 if(autocvar_spawn_debug >= 2)
1109                 {
1110                         // why not flags & fl_item?
1111                         FOREACH_ENTITY_RADIUS(this.origin, 3, it.is_item, {
1112                                 LOG_TRACE("XXX Found duplicated item: ", itemname, vtos(this.origin));
1113                                 LOG_TRACE(" vs ", it.netname, vtos(it.origin));
1114                                 error("Mapper sucks.");
1115                         });
1116                         this.is_item = true;
1117                 }
1118
1119                 weaponsInMap |= WepSet_FromWeapon(REGISTRY_GET(Weapons, weaponid));
1120
1121                 if (        def.instanceOfPowerup
1122                         ||  def.instanceOfWeaponPickup
1123                         || (def.instanceOfHealth && def != ITEM_HealthSmall)
1124                         || (def.instanceOfArmor && def != ITEM_ArmorSmall)
1125                         || (itemid & (IT_KEY1 | IT_KEY2))
1126                 )
1127                 {
1128                         if(!this.target || this.target == "")
1129                                 this.target = "###item###"; // for finding the nearest item using findnearest
1130                 }
1131
1132                 Item_ItemsTime_SetTime(this, 0);
1133         }
1134
1135         this.bot_pickup = true;
1136         this.bot_pickupevalfunc = pickupevalfunc;
1137         this.bot_pickupbasevalue = pickupbasevalue;
1138         this.mdl = this.model ? this.model : strzone(this.item_model_ent.model_str());
1139         this.netname = itemname;
1140         settouch(this, Item_Touch);
1141         setmodel(this, MDL_Null); // precision set below
1142         //this.effects |= EF_LOWPRECISION;
1143
1144         // support skinned models for powerups
1145         if(!this.skin)
1146                 this.skin = def.m_skin;
1147
1148         setsize (this, this.pos1 =  def.m_mins, this.pos2 = def.m_maxs);
1149
1150         this.SendFlags |= ISF_SIZE;
1151
1152         if (!(this.spawnflags & 1024)) {
1153                 if(def.instanceOfPowerup)
1154                         this.ItemStatus |= ITS_ANIMATE1;
1155
1156                 if(GetResource(this, RES_ARMOR) || GetResource(this, RES_HEALTH))
1157                         this.ItemStatus |= ITS_ANIMATE2;
1158         }
1159
1160         if(Item_IsLoot(this))
1161                 this.gravity = 1;
1162         else
1163                 this.glowmod = def.m_color;
1164
1165         if(def.instanceOfWeaponPickup)
1166         {
1167                 if (!Item_IsLoot(this)) // if dropped, colormap is already set up nicely
1168                         this.colormap = 1024; // color shirt=0 pants=0 grey
1169                 if (!(this.spawnflags & 1024))
1170                         this.ItemStatus |= ITS_ANIMATE1;
1171                 this.SendFlags |= ISF_COLORMAP;
1172         }
1173
1174         this.state = 0;
1175         if(this.team)
1176         {
1177                 if(!this.cnt)
1178                         this.cnt = 1; // item probability weight
1179
1180                 this.effects |= EF_NOGUNBOB; // marker for item team search
1181                 InitializeEntity(this, Item_FindTeam, INITPRIO_FINDTARGET);
1182         }
1183         else
1184                 Item_Reset(this);
1185
1186         Net_LinkEntity(this, !(def.instanceOfPowerup || def.instanceOfHealth || def.instanceOfArmor), 0, ItemSend);
1187
1188         // call this hook after everything else has been done
1189         if (MUTATOR_CALLHOOK(Item_Spawn, this))
1190         {
1191                 startitem_failed = true;
1192                 delete(this);
1193                 return;
1194         }
1195
1196         // we should be sure this item will spawn before loading its assets
1197         precache_model(this.model);
1198         precache_sound(this.item_pickupsound);
1199
1200         setItemGroup(this);
1201 }
1202
1203 void StartItem(entity this, GameItem def)
1204 {
1205         def = def.m_spawnfunc_hookreplace(def, this);
1206
1207         this.classname = def.m_canonical_spawnfunc;
1208
1209         this.itemdef = def;
1210         _StartItem(this, this.itemdef, 0, 0);
1211 }
1212
1213 #define IS_SMALL(def) ((def.instanceOfHealth && def == ITEM_HealthSmall) || (def.instanceOfArmor && def == ITEM_ArmorSmall))
1214 int group_count = 1;
1215
1216 void setItemGroup(entity this)
1217 {
1218         if(!IS_SMALL(this.itemdef) || Item_IsLoot(this))
1219                 return;
1220
1221         FOREACH_ENTITY_RADIUS(this.origin, 120, (it != this) && IS_SMALL(it.itemdef),
1222         {
1223                 if(!this.item_group)
1224                 {
1225                         if(!it.item_group)
1226                         {
1227                                 it.item_group = group_count;
1228                                 group_count++;
1229                         }
1230                         this.item_group = it.item_group;
1231                 }
1232                 else // spawning item is already part of a item_group X
1233                 {
1234                         if(!it.item_group)
1235                                 it.item_group = this.item_group;
1236                         else if(it.item_group != this.item_group) // found an item near the spawning item that is part of a different item_group Y
1237                         {
1238                                 int grY = it.item_group;
1239                                 // move all items of item_group Y to item_group X
1240                                 IL_EACH(g_items, IS_SMALL(it.itemdef),
1241                                 {
1242                                         if(it.item_group == grY)
1243                                                 it.item_group = this.item_group;
1244                                 });
1245                         }
1246                 }
1247         });
1248 }
1249
1250 void setItemGroupCount()
1251 {
1252         for (int k = 1; k <= group_count; k++)
1253         {
1254                 int count = 0;
1255                 IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { count++; });
1256                 if (count)
1257                         IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { it.item_group_count = count; });
1258         }
1259 }
1260
1261 void target_items_use(entity this, entity actor, entity trigger)
1262 {
1263         if(Item_IsLoot(actor))
1264         {
1265                 EXACTTRIGGER_TOUCH(this, trigger);
1266                 delete(actor);
1267                 return;
1268         }
1269
1270         if (!IS_PLAYER(actor) || IS_DEAD(actor))
1271                 return;
1272
1273         if(trigger.solid == SOLID_TRIGGER)
1274         {
1275                 EXACTTRIGGER_TOUCH(this, trigger);
1276         }
1277
1278         IL_EACH(g_items, it.enemy == actor && Item_IsLoot(it),
1279         {
1280                 delete(it);
1281         });
1282
1283         if(GiveItems(actor, 0, tokenize_console(this.netname)))
1284                 centerprint(actor, this.message);
1285 }
1286
1287 spawnfunc(target_items)
1288 {
1289         this.use = target_items_use;
1290         if(!this.strength_finished)
1291                 this.strength_finished = autocvar_g_balance_powerup_strength_time;
1292         if(!this.invincible_finished)
1293                 this.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1294         if(!this.speed_finished)
1295                 this.speed_finished = autocvar_g_balance_powerup_speed_time;
1296         if(!this.invisibility_finished)
1297                 this.invisibility_finished = autocvar_g_balance_powerup_invisibility_time;
1298         if(!this.superweapons_finished)
1299                 this.superweapons_finished = autocvar_g_balance_superweapons_time;
1300
1301         string str;
1302         int n = tokenize_console(this.netname);
1303         if(argv(0) == "give")
1304         {
1305                 str = substring(this.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
1306         }
1307         else
1308         {
1309                 for(int j = 0; j < n; ++j)
1310                 {
1311                         // this is from a time when unlimited superweapons were handled together with ammo in some parts of the code
1312                         if     (argv(j) == "unlimited_ammo")         this.items |= IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS;
1313                         else if(argv(j) == "unlimited_weapon_ammo")  this.items |= IT_UNLIMITED_AMMO;
1314                         else if(argv(j) == "unlimited_superweapons") this.items |= IT_UNLIMITED_SUPERWEAPONS;
1315                         else if(argv(j) == "strength")               this.items |= ITEM_Strength.m_itemid;
1316                         else if(argv(j) == "invincible")             this.items |= ITEM_Shield.m_itemid;
1317                         else if(argv(j) == "speed")                  this.items |= ITEM_Speed.m_itemid;
1318                         else if(argv(j) == "invisibility")           this.items |= ITEM_Invisibility.m_itemid;
1319                         else if(argv(j) == "superweapons")           this.items |= IT_SUPERWEAPON;
1320                         else if(argv(j) == "jetpack")                this.items |= ITEM_Jetpack.m_itemid;
1321                         else if(argv(j) == "fuel_regen")             this.items |= ITEM_JetpackRegen.m_itemid;
1322                         else
1323                         {
1324                                 FOREACH(StatusEffect, it.instanceOfBuff,
1325                                 {
1326                                         string s = Buff_CompatName(argv(j));
1327                                         if(s == it.netname)
1328                                         {
1329                                                 this.buffdef = it;
1330                                                 if(!this.buffs_finished)
1331                                                         this.buffs_finished = it.m_time(it);
1332                                                 break;
1333                                         }
1334                                 });
1335                                 FOREACH(Weapons, it != WEP_Null, {
1336                                         string s = argv(j);
1337                                         if(s == it.netname || s == it.m_deprecated_netname)
1338                                         {
1339                                                 STAT(WEAPONS, this) |= (it.m_wepset);
1340                                                 if(this.spawnflags == 0 || this.spawnflags == 2)
1341                                                         it.wr_init(it);
1342                                                 break;
1343                                         }
1344                                 });
1345                         }
1346                 }
1347
1348                 string itemprefix, valueprefix;
1349                 if(this.spawnflags == 0)
1350                 {
1351                         itemprefix = "";
1352                         valueprefix = "";
1353                 }
1354                 else if(this.spawnflags == 1)
1355                 {
1356                         itemprefix = "max ";
1357                         valueprefix = "max ";
1358                 }
1359                 else if(this.spawnflags == 2)
1360                 {
1361                         itemprefix = "min ";
1362                         valueprefix = "min ";
1363                 }
1364                 else if(this.spawnflags == 4)
1365                 {
1366                         itemprefix = "minus ";
1367                         valueprefix = "max ";
1368                 }
1369                 else
1370                 {
1371                         error("invalid spawnflags");
1372                         itemprefix = valueprefix = string_null;
1373                 }
1374
1375                 str = "";
1376                 str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & IT_UNLIMITED_AMMO), "unlimited_weapon_ammo");
1377                 str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
1378                 str = sprintf("%s %s%d %s", str, valueprefix, this.strength_finished * boolean(this.items & ITEM_Strength.m_itemid), "strength");
1379                 str = sprintf("%s %s%d %s", str, valueprefix, this.invincible_finished * boolean(this.items & ITEM_Shield.m_itemid), "invincible");
1380                 str = sprintf("%s %s%d %s", str, valueprefix, this.invisibility_finished * boolean(this.items & ITEM_Invisibility.m_itemid), "invisibility");
1381                 str = sprintf("%s %s%d %s", str, valueprefix, this.speed_finished * boolean(this.items & ITEM_Speed.m_itemid), "speed");
1382                 str = sprintf("%s %s%d %s", str, valueprefix, this.superweapons_finished * boolean(this.items & IT_SUPERWEAPON), "superweapons");
1383                 str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & ITEM_Jetpack.m_itemid), "jetpack");
1384                 str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & ITEM_JetpackRegen.m_itemid), "fuel_regen");
1385                 float res;
1386                 res = GetResource(this, RES_SHELLS);  if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "shells");
1387                 res = GetResource(this, RES_BULLETS); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "nails");
1388                 res = GetResource(this, RES_ROCKETS); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "rockets");
1389                 res = GetResource(this, RES_CELLS);   if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "cells");
1390                 res = GetResource(this, RES_PLASMA);  if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "plasma");
1391                 res = GetResource(this, RES_FUEL);    if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "fuel");
1392                 res = GetResource(this, RES_HEALTH);  if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "health");
1393                 res = GetResource(this, RES_ARMOR);   if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "armor");
1394                 FOREACH(StatusEffect, it.instanceOfBuff, str = sprintf("%s %s%d %s", str, valueprefix, this.buffs_finished * boolean(this.buffdef == it), it.netname));
1395                 FOREACH(Weapons, it != WEP_Null, str = sprintf("%s %s%d %s", str, itemprefix, !!(STAT(WEAPONS, this) & (it.m_wepset)), it.netname));
1396         }
1397         this.netname = strzone(str);
1398
1399         n = tokenize_console(this.netname);
1400         for(int j = 0; j < n; ++j)
1401         {
1402                 string cmd = argv(j);
1403                 FOREACH(Weapons, it != WEP_Null && (cmd == it.netname || cmd == it.m_deprecated_netname), {
1404                         it.wr_init(it);
1405                         break;
1406                 });
1407         }
1408 }
1409
1410 float GiveWeapon(entity e, float wpn, float op, float val)
1411 {
1412         WepSet v0, v1;
1413         WepSet s = WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn));
1414         v0 = (STAT(WEAPONS, e) & s);
1415         switch(op)
1416         {
1417                 case OP_SET:
1418                         if(val > 0)
1419                                 STAT(WEAPONS, e) |= s;
1420                         else
1421                                 STAT(WEAPONS, e) &= ~s;
1422                         break;
1423                 case OP_MIN:
1424                 case OP_PLUS:
1425                         if(val > 0)
1426                                 STAT(WEAPONS, e) |= s;
1427                         break;
1428                 case OP_MAX:
1429                         if(val <= 0)
1430                                 STAT(WEAPONS, e) &= ~s;
1431                         break;
1432                 case OP_MINUS:
1433                         if(val > 0)
1434                                 STAT(WEAPONS, e) &= ~s;
1435                         break;
1436         }
1437         v1 = (STAT(WEAPONS, e) & s);
1438         return (v0 != v1);
1439 }
1440
1441 bool GiveBuff(entity e, Buff thebuff, int op, int val)
1442 {
1443         bool had_buff = StatusEffects_active(thebuff, e);
1444         float new_buff_time = ((had_buff) ? StatusEffects_gettime(thebuff, e) : 0);
1445         switch (op)
1446         {
1447                 case OP_SET:
1448                         new_buff_time = val;
1449                         break;
1450                 case OP_MIN:
1451                         new_buff_time = max(new_buff_time, val);
1452                         break;
1453                 case OP_MAX:
1454                         new_buff_time = min(new_buff_time, val);
1455                         break;
1456                 case OP_PLUS:
1457                         new_buff_time += val;
1458                         break;
1459                 case OP_MINUS:
1460                         new_buff_time -= val;
1461                         break;
1462         }
1463         if(new_buff_time <= 0)
1464         {
1465                 if(had_buff) // only trigger removal mechanics if there is an effect to remove!
1466                         StatusEffects_remove(thebuff, e, STATUSEFFECT_REMOVE_NORMAL);
1467         }
1468         else
1469         {
1470                 buff_RemoveAll(e, STATUSEFFECT_REMOVE_CLEAR); // clear old buffs on the player first!
1471                 StatusEffects_apply(thebuff, e, new_buff_time, 0);
1472         }
1473         bool have_buff = StatusEffects_active(thebuff, e);
1474         return (had_buff != have_buff);
1475 }
1476
1477 void GiveSound(entity e, float v0, float v1, float t, Sound snd_incr, Sound snd_decr)
1478 {
1479         if(v1 == v0)
1480                 return;
1481         if(v1 <= v0 - t)
1482         {
1483                 if(snd_decr != NULL)
1484                         sound(e, CH_TRIGGER, snd_decr, VOL_BASE, ATTEN_NORM);
1485         }
1486         else if(v0 >= v0 + t)
1487         {
1488                 if(snd_incr != NULL)
1489                         sound(e, ((snd_incr == SND_POWERUP) ? CH_TRIGGER_SINGLE : CH_TRIGGER), snd_incr, VOL_BASE, ATTEN_NORM);
1490         }
1491 }
1492
1493 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
1494 {
1495         if(v0 < v1)
1496                 e.(rotfield) = max(e.(rotfield), time + rottime);
1497         else if(v0 > v1)
1498                 e.(regenfield) = max(e.(regenfield), time + regentime);
1499 }
1500 bool GiveResourceValue(entity e, Resource res_type, int op, int val)
1501 {
1502         int v0 = GetResource(e, res_type);
1503         float new_val = 0;
1504         switch (op)
1505         {
1506                 // min 100 cells = at least 100 cells
1507                 case OP_SET: new_val = val; break;
1508                 case OP_MIN: new_val = max(v0, val); break;
1509                 case OP_MAX: new_val = min(v0, val); break;
1510                 case OP_PLUS: new_val = v0 + val; break;
1511                 case OP_MINUS: new_val = v0 - val; break;
1512                 default: return false;
1513         }
1514
1515         return SetResourceExplicit(e, res_type, new_val);
1516 }
1517 bool GiveStatusEffect(entity e, StatusEffects this, int op, float val)
1518 {
1519         bool had_eff = StatusEffects_active(this, e);
1520         float new_eff_time = ((had_eff) ? StatusEffects_gettime(this, e) : 0);
1521         switch (op)
1522         {
1523                 case OP_SET:
1524                         new_eff_time = val;
1525                         break;
1526                 case OP_MIN:
1527                         new_eff_time = max(new_eff_time, val);
1528                         break;
1529                 case OP_MAX:
1530                         new_eff_time = min(new_eff_time, val);
1531                         break;
1532                 case OP_PLUS:
1533                         new_eff_time += val;
1534                         break;
1535                 case OP_MINUS:
1536                         new_eff_time -= val;
1537                         break;
1538         }
1539         if(new_eff_time <= 0)
1540         {
1541                 if(had_eff) // only trigger removal mechanics if there is an effect to remove!
1542                         StatusEffects_remove(this, e, STATUSEFFECT_REMOVE_NORMAL);
1543         }
1544         else
1545                 StatusEffects_apply(this, e, new_eff_time, 0);
1546         bool have_eff = StatusEffects_active(this, e);
1547         return (had_eff != have_eff);
1548 }
1549
1550 float GiveItems(entity e, float beginarg, float endarg)
1551 {
1552         float got, i, val, op;
1553         string cmd;
1554
1555         val = 999;
1556         op = OP_SET;
1557
1558         got = 0;
1559
1560         int _switchweapon = 0;
1561
1562         if(CS_CVAR(e).cvar_cl_autoswitch)
1563         {
1564                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1565                 {
1566                         .entity weaponentity = weaponentities[slot];
1567                         if(e.(weaponentity).m_weapon != WEP_Null || slot == 0)
1568                         if(e.(weaponentity).m_switchweapon == w_getbestweapon(e, weaponentity))
1569                                 _switchweapon |= BIT(slot);
1570                 }
1571         }
1572
1573         if(e.statuseffects)
1574         {
1575                 FOREACH(StatusEffect, true,
1576                 {
1577                         e.statuseffects.statuseffect_time[it.m_id] = max(0, e.statuseffects.statuseffect_time[it.m_id] - time);
1578                 });
1579         }
1580
1581         PREGIVE(e, items);
1582         PREGIVE_WEAPONS(e);
1583         PREGIVE_STATUSEFFECT(e, STATUSEFFECT_Strength);
1584         PREGIVE_STATUSEFFECT(e, STATUSEFFECT_Shield);
1585         PREGIVE_STATUSEFFECT(e, STATUSEFFECT_Speed);
1586         PREGIVE_STATUSEFFECT(e, STATUSEFFECT_Invisibility);
1587         //PREGIVE_STATUSEFFECT(e, STATUSEFFECT_Superweapons);
1588         PREGIVE_RESOURCE(e, RES_BULLETS);
1589         PREGIVE_RESOURCE(e, RES_CELLS);
1590         PREGIVE_RESOURCE(e, RES_PLASMA);
1591         PREGIVE_RESOURCE(e, RES_SHELLS);
1592         PREGIVE_RESOURCE(e, RES_ROCKETS);
1593         PREGIVE_RESOURCE(e, RES_FUEL);
1594         PREGIVE_RESOURCE(e, RES_ARMOR);
1595         PREGIVE_RESOURCE(e, RES_HEALTH);
1596
1597         for(i = beginarg; i < endarg; ++i)
1598         {
1599                 cmd = argv(i);
1600
1601                 if(cmd == "0" || stof(cmd))
1602                 {
1603                         val = stof(cmd);
1604                         continue;
1605                 }
1606                 switch(cmd)
1607                 {
1608                         case "no":
1609                                 op = OP_MAX;
1610                                 val = 0;
1611                                 continue;
1612                         case "max":
1613                                 op = OP_MAX;
1614                                 continue;
1615                         case "min":
1616                                 op = OP_MIN;
1617                                 continue;
1618                         case "plus":
1619                                 op = OP_PLUS;
1620                                 continue;
1621                         case "minus":
1622                                 op = OP_MINUS;
1623                                 continue;
1624                         case "ALL":
1625                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1626                                 FOREACH(StatusEffect, it.instanceOfPowerups, got += GiveStatusEffect(e, it, op, val));
1627                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS, op, val);
1628                         case "all":
1629                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1630                                 got += GiveResourceValue(e, RES_HEALTH, op, val);
1631                                 got += GiveResourceValue(e, RES_ARMOR, op, val);
1632                         case "allweapons":
1633                                 FOREACH(Weapons, it != WEP_Null && !(it.spawnflags & (WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_SPECIALATTACK)), got += GiveWeapon(e, it.m_id, op, val));
1634                         //case "allbuffs": // all buffs makes a player god, do not want!
1635                                 //FOREACH(StatusEffect, it.instanceOfBuff, got += GiveBuff(e, it, op, val));
1636                         case "allammo":
1637                                 got += GiveResourceValue(e, RES_CELLS, op, val);
1638                                 got += GiveResourceValue(e, RES_PLASMA, op, val);
1639                                 got += GiveResourceValue(e, RES_SHELLS, op, val);
1640                                 got += GiveResourceValue(e, RES_BULLETS, op, val);
1641                                 got += GiveResourceValue(e, RES_ROCKETS, op, val);
1642                                 got += GiveResourceValue(e, RES_FUEL, op, val);
1643                                 break;
1644                         case "unlimited_ammo":
1645                                 // this is from a time when unlimited superweapons were handled together with ammo in some parts of the code
1646                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS, op, val);
1647                                 break;
1648                         case "unlimited_weapon_ammo":
1649                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1650                                 break;
1651                         case "unlimited_superweapons":
1652                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
1653                                 break;
1654                         case "jetpack":
1655                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1656                                 break;
1657                         case "fuel_regen":
1658                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1659                                 break;
1660                         case "strength":
1661                                 got += GiveStatusEffect(e, STATUSEFFECT_Strength, op, val);
1662                                 break;
1663                         case "invincible":
1664                         case "shield":
1665                                 got += GiveStatusEffect(e, STATUSEFFECT_Shield, op, val);
1666                                 break;
1667                         case "speed":
1668                                 got += GiveStatusEffect(e, STATUSEFFECT_Speed, op, val);
1669                                 break;
1670                         case "invisibility":
1671                                 got += GiveStatusEffect(e, STATUSEFFECT_Invisibility, op, val);
1672                                 break;
1673                         case "superweapons":
1674                                 got += GiveStatusEffect(e, STATUSEFFECT_Superweapons, op, val);
1675                                 break;
1676                         case "cells":
1677                                 got += GiveResourceValue(e, RES_CELLS, op, val);
1678                                 break;
1679                         case "plasma":
1680                                 got += GiveResourceValue(e, RES_PLASMA, op, val);
1681                                 break;
1682                         case "shells":
1683                                 got += GiveResourceValue(e, RES_SHELLS, op, val);
1684                                 break;
1685                         case "nails":
1686                         case "bullets":
1687                                 got += GiveResourceValue(e, RES_BULLETS, op, val);
1688                                 break;
1689                         case "rockets":
1690                                 got += GiveResourceValue(e, RES_ROCKETS, op, val);
1691                                 break;
1692                         case "health":
1693                                 got += GiveResourceValue(e, RES_HEALTH, op, val);
1694                                 break;
1695                         case "armor":
1696                                 got += GiveResourceValue(e, RES_ARMOR, op, val);
1697                                 break;
1698                         case "fuel":
1699                                 got += GiveResourceValue(e, RES_FUEL, op, val);
1700                                 break;
1701                         default:
1702                                 FOREACH(StatusEffect, it.instanceOfBuff && buff_Available(it) && Buff_CompatName(cmd) == it.netname,
1703                                 {
1704                                         got += GiveBuff(e, it, op, val);
1705                                         break;
1706                                 });
1707                                 FOREACH(Weapons, it != WEP_Null && (cmd == it.netname || cmd == it.m_deprecated_netname), {
1708                     got += GiveWeapon(e, it.m_id, op, val);
1709                     break;
1710                                 });
1711                                 break;
1712                 }
1713                 val = 999;
1714                 op = OP_SET;
1715         }
1716
1717         POSTGIVE_BIT(e, items, ITEM_JetpackRegen.m_itemid, SND_ITEMPICKUP, SND_Null);
1718         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, SND_POWERUP, SND_POWEROFF);
1719         POSTGIVE_BIT(e, items, IT_UNLIMITED_AMMO, SND_POWERUP, SND_POWEROFF);
1720         POSTGIVE_BIT(e, items, ITEM_Jetpack.m_itemid, SND_ITEMPICKUP, SND_Null);
1721         FOREACH(Weapons, it != WEP_Null, {
1722                 POSTGIVE_WEAPON(e, it, SND_WEAPONPICKUP, SND_Null);
1723                 if(!(save_weapons & (it.m_wepset)))
1724                         if(STAT(WEAPONS, e) & (it.m_wepset))
1725                                 it.wr_init(it);
1726         });
1727         POSTGIVE_STATUSEFFECT(e, STATUSEFFECT_Strength, SND_POWERUP, SND_POWEROFF);
1728         POSTGIVE_STATUSEFFECT(e, STATUSEFFECT_Shield, SND_POWERUP, SND_POWEROFF);
1729         POSTGIVE_STATUSEFFECT(e, STATUSEFFECT_Speed, SND_POWERUP, SND_POWEROFF);
1730         POSTGIVE_STATUSEFFECT(e, STATUSEFFECT_Invisibility, SND_POWERUP, SND_POWEROFF);
1731         POSTGIVE_RESOURCE(e, RES_BULLETS, 0, SND_ITEMPICKUP, SND_Null);
1732         POSTGIVE_RESOURCE(e, RES_CELLS, 0, SND_ITEMPICKUP, SND_Null);
1733         POSTGIVE_RESOURCE(e, RES_PLASMA, 0, SND_ITEMPICKUP, SND_Null);
1734         POSTGIVE_RESOURCE(e, RES_SHELLS, 0, SND_ITEMPICKUP, SND_Null);
1735         POSTGIVE_RESOURCE(e, RES_ROCKETS, 0, SND_ITEMPICKUP, SND_Null);
1736         POSTGIVE_RES_ROT(e, RES_FUEL, 1, pauserotfuel_finished, autocvar_g_balance_pause_fuel_rot, pauseregen_finished, autocvar_g_balance_pause_fuel_regen, SND_ITEMPICKUP, SND_Null);
1737         POSTGIVE_RES_ROT(e, RES_ARMOR, 1, pauserotarmor_finished, autocvar_g_balance_pause_armor_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, SND_ARMOR25, SND_Null);
1738         POSTGIVE_RES_ROT(e, RES_HEALTH, 1, pauserothealth_finished, autocvar_g_balance_pause_health_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, SND_MEGAHEALTH, SND_Null);
1739
1740         if(!StatusEffects_active(STATUSEFFECT_Superweapons, e))
1741         {
1742                 if(!g_weaponarena && (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS))
1743                         StatusEffects_apply(STATUSEFFECT_Superweapons, e, autocvar_g_balance_superweapons_time, 0);
1744         }
1745
1746         if(e.statuseffects)
1747         {
1748                 FOREACH(StatusEffect, true,
1749                 {
1750                         if(e.statuseffects.statuseffect_time[it.m_id] <= 0)
1751                                 e.statuseffects.statuseffect_time[it.m_id] = 0;
1752                         else
1753                                 e.statuseffects.statuseffect_time[it.m_id] += time;
1754                 });
1755                         
1756                 StatusEffects_update(e);
1757         }
1758
1759         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1760         {
1761                 .entity weaponentity = weaponentities[slot];
1762                 if(e.(weaponentity).m_weapon != WEP_Null || slot == 0)
1763                 if(!(STAT(WEAPONS, e) & WepSet_FromWeapon(e.(weaponentity).m_switchweapon)))
1764                         _switchweapon |= BIT(slot);
1765         }
1766
1767         if(_switchweapon)
1768         {
1769                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1770                 {
1771                         .entity weaponentity = weaponentities[slot];
1772                         if(_switchweapon & BIT(slot))
1773                         {
1774                                 Weapon wep = w_getbestweapon(e, weaponentity);
1775                                 if(wep != e.(weaponentity).m_switchweapon)
1776                                         W_SwitchWeapon_Force(e, wep, weaponentity);
1777                         }
1778                 }
1779         }
1780
1781         return got;
1782 }