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