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