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