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