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