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