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