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