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