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