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