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