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