+
+void Item_Touch(entity this, entity toucher)
+{
+ // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
+ if (Item_IsLoot(this))
+ {
+ if (ITEM_TOUCH_NEEDKILL())
+ {
+ delete(this);
+ return;
+ }
+ }
+
+ if(!(toucher.flags & FL_PICKUPITEMS)
+ || STAT(FROZEN, toucher)
+ || IS_DEAD(toucher)
+ || (this.solid != SOLID_TRIGGER)
+ || (this.owner == toucher)
+ || (time < this.item_spawnshieldtime)
+ ) { return; }
+
+ switch (MUTATOR_CALLHOOK(ItemTouch, this, toucher))
+ {
+ case MUT_ITEMTOUCH_RETURN: { return; }
+ case MUT_ITEMTOUCH_PICKUP: { toucher = M_ARGV(1, entity); goto pickup; }
+ }
+
+ toucher = M_ARGV(1, entity);
+
+ if (Item_IsExpiring(this))
+ {
+ this.strength_finished = max(0, this.strength_finished - time);
+ this.invincible_finished = max(0, this.invincible_finished - time);
+ this.superweapons_finished = max(0, this.superweapons_finished - time);
+ }
+ bool gave = ITEM_HANDLE(Pickup, this.itemdef, this, toucher);
+ if (!gave)
+ {
+ if (Item_IsExpiring(this))
+ {
+ // undo what we did above
+ this.strength_finished += time;
+ this.invincible_finished += time;
+ this.superweapons_finished += time;
+ }
+ return;
+ }
+
+LABEL(pickup)
+
+ if(this.target && this.target != "" && this.target != "###item###") // defrag support
+ SUB_UseTargets(this, toucher, NULL);
+
+ STAT(LAST_PICKUP, toucher) = time;
+
+ Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
+ _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);
+
+ MUTATOR_CALLHOOK(ItemTouched, this, toucher);
+ if (wasfreed(this))
+ {
+ return;
+ }
+
+ if (Item_IsLoot(this))
+ {
+ delete(this);
+ return;
+ }
+ if (!this.spawnshieldtime)
+ {
+ return;
+ }
+ entity e;
+ if (this.team)
+ {
+ RandomSelection_Init();
+ IL_EACH(g_items, it.team == this.team,
+ {
+ if (it.itemdef) // is a registered item
+ {
+ Item_Show(it, -1);
+ it.scheduledrespawntime = 0;
+ RandomSelection_AddEnt(it, it.cnt, 0);
+ }
+ });
+ e = RandomSelection_chosen_ent;
+ Item_Show(e, 1); // reset its state so it is visible (extra sendflags doesn't matter, this happens anyway)
+ }
+ else
+ e = this;
+ Item_ScheduleRespawn(e);
+}
+
+void Item_Reset(entity this)
+{
+ Item_Show(this, !this.state);
+ setorigin(this, this.origin);
+
+ if (Item_IsLoot(this))
+ {
+ return;
+ }
+ setthink(this, Item_Think);
+ this.nextthink = time;
+ if (this.waypointsprite_attached)
+ {
+ WaypointSprite_Kill(this.waypointsprite_attached);
+ }
+ if (this.itemdef.instanceOfPowerup || (STAT(WEAPONS, this) & WEPSET_SUPERWEAPONS)) // do not spawn powerups initially!
+ {
+ Item_ScheduleInitialRespawn(this);
+ }
+}
+
+void Item_FindTeam(entity this)
+{
+ entity e;
+
+ if(this.effects & EF_NODRAW)
+ {
+ // marker for item team search
+ LOG_TRACE("Initializing item team ", ftos(this.team));
+ RandomSelection_Init();
+ IL_EACH(g_items, it.team == this.team,
+ {
+ if(it.itemdef) // is a registered item
+ RandomSelection_AddEnt(it, it.cnt, 0);
+ });
+
+ e = RandomSelection_chosen_ent;
+ if (!e)
+ return;
+
+ IL_EACH(g_items, it.team == this.team,
+ {
+ if(it.itemdef) // is a registered item
+ {
+ if(it != e)
+ {
+ // make it non-spawned
+ Item_Show(it, -1);
+ it.state = 1; // state 1 = initially hidden item, apparently
+ }
+ else
+ Item_Reset(it);
+ it.effects &= ~EF_NODRAW;
+ }
+ });
+ }
+}
+
+// Savage: used for item garbage-collection
+void RemoveItem(entity this)
+{
+ if(wasfreed(this) || !this) { return; }
+ Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
+ delete(this);
+}
+
+// pickup evaluation functions
+// these functions decide how desirable an item is to the bots
+
+float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;}
+
+float weapon_pickupevalfunc(entity player, entity item)
+{
+ // See if I have it already
+ if(STAT(WEAPONS, player) & STAT(WEAPONS, item))
+ {
+ // If I can pick it up
+ if(!item.spawnshieldtime)
+ return 0;
+ return ammo_pickupevalfunc(player, item);
+ }
+
+ // reduce weapon value if bot already got a good arsenal
+ float c = 1;
+ int weapons_value = 0;
+ FOREACH(Weapons, it != WEP_Null && (STAT(WEAPONS, player) & it.m_wepset), {
+ weapons_value += it.bot_pickupbasevalue;
+ });
+ c -= bound(0, weapons_value / 20000, 1) * 0.5;
+
+ return item.bot_pickupbasevalue * c;
+}
+
+float ammo_pickupevalfunc(entity player, entity item)
+{
+ bool need_shells = false, need_nails = false, need_rockets = false, need_cells = false, need_plasma = false, need_fuel = false;
+ entity wpn = NULL;
+ float c = 0;
+ float rating = 0;
+
+ // Detect needed ammo
+ if(item.itemdef.instanceOfWeaponPickup)
+ {
+ entity ammo = NULL;
+ if(GetResource(item, RES_SHELLS)) { need_shells = true; ammo = ITEM_Shells; }
+ else if(GetResource(item, RES_BULLETS)) { need_nails = true; ammo = ITEM_Bullets; }
+ else if(GetResource(item, RES_ROCKETS)) { need_rockets = true; ammo = ITEM_Rockets; }
+ else if(GetResource(item, RES_CELLS)) { need_cells = true; ammo = ITEM_Cells; }
+ else if(GetResource(item, RES_PLASMA)) { need_plasma = true; ammo = ITEM_Plasma; }
+ else if(GetResource(item, RES_FUEL)) { need_fuel = true; ammo = ITEM_JetpackFuel; }
+
+ if(!ammo)
+ return 0;
+ wpn = item;
+ rating = ammo.m_botvalue;
+ }
+ else
+ {
+ FOREACH(Weapons, it != WEP_Null, {
+ if(!(STAT(WEAPONS, player) & (it.m_wepset)))
+ continue;
+
+ switch(it.ammo_type)
+ {
+ case RES_SHELLS: need_shells = true; break;
+ case RES_BULLETS: need_nails = true; break;
+ case RES_ROCKETS: need_rockets = true; break;
+ case RES_CELLS: need_cells = true; break;
+ case RES_PLASMA: need_plasma = true; break;
+ case RES_FUEL: need_fuel = true; break;
+ }
+ });
+ rating = item.bot_pickupbasevalue;
+ }
+
+ float noammorating = 0.5;
+
+ if ((need_shells) && GetResource(item, RES_SHELLS) && (GetResource(player, RES_SHELLS) < g_pickup_shells_max))
+ c = GetResource(item, RES_SHELLS) / max(noammorating, GetResource(player, RES_SHELLS));
+
+ if ((need_nails) && GetResource(item, RES_BULLETS) && (GetResource(player, RES_BULLETS) < g_pickup_nails_max))
+ c = GetResource(item, RES_BULLETS) / max(noammorating, GetResource(player, RES_BULLETS));
+
+ if ((need_rockets) && GetResource(item, RES_ROCKETS) && (GetResource(player, RES_ROCKETS) < g_pickup_rockets_max))
+ c = GetResource(item, RES_ROCKETS) / max(noammorating, GetResource(player, RES_ROCKETS));
+
+ if ((need_cells) && GetResource(item, RES_CELLS) && (GetResource(player, RES_CELLS) < g_pickup_cells_max))
+ c = GetResource(item, RES_CELLS) / max(noammorating, GetResource(player, RES_CELLS));
+
+ if ((need_plasma) && GetResource(item, RES_PLASMA) && (GetResource(player, RES_PLASMA) < g_pickup_plasma_max))
+ c = GetResource(item, RES_PLASMA) / max(noammorating, GetResource(player, RES_PLASMA));
+
+ if ((need_fuel) && GetResource(item, RES_FUEL) && (GetResource(player, RES_FUEL) < g_pickup_fuel_max))
+ c = GetResource(item, RES_FUEL) / max(noammorating, GetResource(player, RES_FUEL));
+
+ rating *= min(c, 2);
+ if(wpn)
+ rating += wpn.bot_pickupbasevalue * 0.1;
+ return rating;
+}
+
+float healtharmor_pickupevalfunc(entity player, entity item)
+{
+ float c = 0;
+ float rating = item.bot_pickupbasevalue;
+
+ float itemarmor = GetResource(item, RES_ARMOR);
+ float itemhealth = GetResource(item, RES_HEALTH);
+
+ if(item.item_group)
+ {
+ itemarmor *= min(4, item.item_group_count);
+ itemhealth *= min(4, item.item_group_count);
+ }
+
+ if (itemarmor && (GetResource(player, RES_ARMOR) < item.max_armorvalue))
+ c = itemarmor / max(1, GetResource(player, RES_ARMOR) * 2/3 + GetResource(player, RES_HEALTH) * 1/3);
+
+ if (itemhealth && (GetResource(player, RES_HEALTH) < item.max_health))
+ c = itemhealth / max(1, GetResource(player, RES_HEALTH));
+
+ rating *= min(2, c);
+ return rating;
+}
+
+void Item_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
+{
+ if(ITEM_DAMAGE_NEEDKILL(deathtype))
+ RemoveItem(this);
+}
+
+void item_use(entity this, entity actor, entity trigger)
+{
+ // use the touch function to handle collection
+ gettouch(this)(this, actor);
+}
+
+void _StartItem(entity this, entity def, float defaultrespawntime, float defaultrespawntimejitter)
+{
+ string itemname = def.m_name;
+ Model itemmodel = def.m_model;
+ Sound pickupsound = def.m_sound;
+ float(entity player, entity item) pickupevalfunc = def.m_pickupevalfunc;
+ float pickupbasevalue = def.m_botvalue;
+ int itemflags = def.m_itemflags;
+
+ startitem_failed = false;
+
+ this.item_model_ent = itemmodel;
+ this.item_pickupsound_ent = pickupsound;
+
+ if(def.m_iteminit)
+ def.m_iteminit(def, this);
+
+ if(!this.respawntime) // both need to be set
+ {
+ this.respawntime = defaultrespawntime;
+ this.respawntimejitter = defaultrespawntimejitter;
+ }
+
+ if(!this.pickup_anyway && def.m_pickupanyway)
+ this.pickup_anyway = def.m_pickupanyway();
+
+ int itemid = def.m_itemid;
+ this.items = itemid;
+ int weaponid = def.instanceOfWeaponPickup ? def.m_weapon.m_id : 0;
+ this.weapon = weaponid;
+
+ if(!this.fade_end)
+ {
+ this.fade_start = autocvar_g_items_mindist;
+ this.fade_end = autocvar_g_items_maxdist;
+ }
+
+ if(weaponid)
+ STAT(WEAPONS, this) = WepSet_FromWeapon(REGISTRY_GET(Weapons, weaponid));
+
+ this.flags = FL_ITEM | itemflags;
+ IL_PUSH(g_items, this);
+
+ if(MUTATOR_CALLHOOK(FilterItem, this)) // error means we do not want the item
+ {
+ startitem_failed = true;
+ delete(this);
+ return;
+ }
+
+ precache_model(this.model);
+ precache_sound(this.item_pickupsound);
+
+ if (Item_IsLoot(this))
+ {
+ this.reset = SUB_Remove;
+ set_movetype(this, MOVETYPE_TOSS);
+
+ // Savage: remove thrown items after a certain period of time ("garbage collection")
+ setthink(this, RemoveItem);
+ this.nextthink = time + 20;
+
+ this.takedamage = DAMAGE_YES;
+ this.event_damage = Item_Damage;
+
+ if (Item_IsExpiring(this))
+ {
+ // if item is worthless after a timer, have it expire then
+ this.nextthink = max(this.strength_finished, this.invincible_finished, this.superweapons_finished);
+ }
+
+ // don't drop if in a NODROP zone (such as lava)
+ traceline(this.origin, this.origin, MOVE_NORMAL, this);
+ if (trace_dpstartcontents & DPCONTENTS_NODROP)
+ {
+ startitem_failed = true;
+ delete(this);
+ return;
+ }
+ }
+ else
+ {
+ if(!have_pickup_item(this))
+ {
+ startitem_failed = true;
+ delete(this);
+ return;
+ }
+
+ if(this.angles != '0 0 0')
+ this.SendFlags |= ISF_ANGLES;
+
+ this.reset = Item_Reset;
+ // it's a level item
+ if(this.spawnflags & 1)
+ this.noalign = 1;
+ if (this.noalign > 0)
+ set_movetype(this, MOVETYPE_NONE);
+ else
+ set_movetype(this, MOVETYPE_TOSS);
+ // do item filtering according to game mode and other things
+ if (this.noalign <= 0)
+ {
+ // first nudge it off the floor a little bit to avoid math errors
+ setorigin(this, this.origin + '0 0 1');
+ // set item size before we spawn a spawnfunc_waypoint
+ setsize(this, def.m_mins, def.m_maxs);
+ this.SendFlags |= ISF_SIZE;
+ // note droptofloor returns false if stuck/or would fall too far
+ if (!this.noalign)
+ droptofloor(this);
+ waypoint_spawnforitem(this);
+ }
+
+ /*
+ * can't do it that way, as it would break maps
+ * TODO make a target_give like entity another way, that perhaps has
+ * the weapon name in a key
+ if(this.targetname)
+ {
+ // target_give not yet supported; maybe later
+ print("removed targeted ", this.classname, "\n");
+ startitem_failed = true;
+ delete(this);
+ return;
+ }
+ */
+
+ if(this.targetname != "" && (this.spawnflags & 16))
+ this.use = item_use;
+
+ if(autocvar_spawn_debug >= 2)
+ {
+ // why not flags & fl_item?
+ FOREACH_ENTITY_RADIUS(this.origin, 3, it.is_item, {
+ LOG_TRACE("XXX Found duplicated item: ", itemname, vtos(this.origin));
+ LOG_TRACE(" vs ", it.netname, vtos(it.origin));
+ error("Mapper sucks.");
+ });
+ this.is_item = true;
+ }
+
+ weaponsInMap |= WepSet_FromWeapon(REGISTRY_GET(Weapons, weaponid));
+
+ if ( def.instanceOfPowerup
+ || def.instanceOfWeaponPickup
+ || (def.instanceOfHealth && def != ITEM_HealthSmall)
+ || (def.instanceOfArmor && def != ITEM_ArmorSmall)
+ || (itemid & (IT_KEY1 | IT_KEY2))
+ )
+ {
+ if(!this.target || this.target == "")
+ this.target = "###item###"; // for finding the nearest item using findnearest
+ }
+
+ Item_ItemsTime_SetTime(this, 0);
+ }
+
+ this.bot_pickup = true;
+ this.bot_pickupevalfunc = pickupevalfunc;
+ this.bot_pickupbasevalue = pickupbasevalue;
+ this.mdl = this.model ? this.model : strzone(this.item_model_ent.model_str());
+ this.netname = itemname;
+ settouch(this, Item_Touch);
+ setmodel(this, MDL_Null); // precision set below
+ //this.effects |= EF_LOWPRECISION;
+
+ setsize (this, this.pos1 = def.m_mins, this.pos2 = def.m_maxs);
+
+ this.SendFlags |= ISF_SIZE;
+
+ if (!(this.spawnflags & 1024)) {
+ if(def.instanceOfPowerup)
+ this.ItemStatus |= ITS_ANIMATE1;
+
+ if(GetResource(this, RES_ARMOR) || GetResource(this, RES_HEALTH))
+ this.ItemStatus |= ITS_ANIMATE2;
+ }
+
+ if(Item_IsLoot(this))
+ this.gravity = 1;
+
+ if(def.instanceOfWeaponPickup)
+ {
+ if (!Item_IsLoot(this)) // if dropped, colormap is already set up nicely
+ this.colormap = 1024; // color shirt=0 pants=0 grey
+ if (!(this.spawnflags & 1024))
+ this.ItemStatus |= ITS_ANIMATE1;
+ this.SendFlags |= ISF_COLORMAP;
+ }
+
+ this.state = 0;
+ if(this.team)
+ {
+ if(!this.cnt)
+ this.cnt = 1; // item probability weight
+
+ this.effects |= EF_NODRAW; // marker for item team search
+ InitializeEntity(this, Item_FindTeam, INITPRIO_FINDTARGET);
+ }
+ else
+ Item_Reset(this);
+
+ Net_LinkEntity(this, !(def.instanceOfPowerup || def.instanceOfHealth || def.instanceOfArmor), 0, ItemSend);
+
+ // call this hook after everything else has been done
+ if (MUTATOR_CALLHOOK(Item_Spawn, this))
+ {
+ startitem_failed = true;
+ delete(this);
+ return;
+ }
+
+ setItemGroup(this);
+}
+
+void StartItem(entity this, GameItem def)
+{
+ def = def.m_spawnfunc_hookreplace(def, this);
+ if (def.spawnflags & ITEM_FLAG_MUTATORBLOCKED)
+ {
+ delete(this);
+ return;
+ }
+ this.classname = def.m_canonical_spawnfunc;
+ _StartItem(
+ this,
+ this.itemdef = def,
+ def.m_respawntime(), // defaultrespawntime
+ def.m_respawntimejitter() // defaultrespawntimejitter
+ );
+}
+
+#define IS_SMALL(def) ((def.instanceOfHealth && def == ITEM_HealthSmall) || (def.instanceOfArmor && def == ITEM_ArmorSmall))
+int group_count = 1;
+
+void setItemGroup(entity this)
+{
+ if(!IS_SMALL(this.itemdef) || Item_IsLoot(this))
+ return;
+
+ FOREACH_ENTITY_RADIUS(this.origin, 120, (it != this) && IS_SMALL(it.itemdef),
+ {
+ if(!this.item_group)
+ {
+ if(!it.item_group)
+ {
+ it.item_group = group_count;
+ group_count++;
+ }
+ this.item_group = it.item_group;
+ }
+ else // spawning item is already part of a item_group X
+ {
+ if(!it.item_group)
+ it.item_group = this.item_group;
+ else if(it.item_group != this.item_group) // found an item near the spawning item that is part of a different item_group Y
+ {
+ int grY = it.item_group;
+ // move all items of item_group Y to item_group X
+ IL_EACH(g_items, IS_SMALL(it.itemdef),
+ {
+ if(it.item_group == grY)
+ it.item_group = this.item_group;
+ });
+ }
+ }
+ });
+}
+
+void setItemGroupCount()
+{
+ for (int k = 1; k <= group_count; k++)
+ {
+ int count = 0;
+ IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { count++; });
+ if (count)
+ IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { it.item_group_count = count; });
+ }
+}
+
+void target_items_use(entity this, entity actor, entity trigger)
+{
+ if(Item_IsLoot(actor))
+ {
+ EXACTTRIGGER_TOUCH(this, trigger);
+ delete(actor);
+ return;
+ }
+
+ if (!IS_PLAYER(actor) || IS_DEAD(actor))
+ return;
+
+ if(trigger.solid == SOLID_TRIGGER)
+ {
+ EXACTTRIGGER_TOUCH(this, trigger);
+ }
+
+ IL_EACH(g_items, it.enemy == actor && Item_IsLoot(it),
+ {
+ delete(it);
+ });
+
+ if(GiveItems(actor, 0, tokenize_console(this.netname)))
+ centerprint(actor, this.message);
+}
+
+spawnfunc(target_items)
+{
+ this.use = target_items_use;
+ if(!this.strength_finished)
+ this.strength_finished = autocvar_g_balance_powerup_strength_time;
+ if(!this.invincible_finished)
+ this.invincible_finished = autocvar_g_balance_powerup_invincible_time;
+ if(!this.superweapons_finished)
+ this.superweapons_finished = autocvar_g_balance_superweapons_time;
+
+ string str;
+ int n = tokenize_console(this.netname);
+ if(argv(0) == "give")
+ {
+ str = substring(this.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
+ }
+ else
+ {
+ for(int j = 0; j < n; ++j)
+ {
+ // this is from a time when unlimited superweapons were handled together with ammo in some parts of the code
+ if (argv(j) == "unlimited_ammo") this.items |= IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS;
+ else if(argv(j) == "unlimited_weapon_ammo") this.items |= IT_UNLIMITED_AMMO;
+ else if(argv(j) == "unlimited_superweapons") this.items |= IT_UNLIMITED_SUPERWEAPONS;
+ else if(argv(j) == "strength") this.items |= ITEM_Strength.m_itemid;
+ else if(argv(j) == "invincible") this.items |= ITEM_Shield.m_itemid;
+ else if(argv(j) == "superweapons") this.items |= IT_SUPERWEAPON;
+ else if(argv(j) == "jetpack") this.items |= ITEM_Jetpack.m_itemid;
+ else if(argv(j) == "fuel_regen") this.items |= ITEM_JetpackRegen.m_itemid;
+ else
+ {
+ FOREACH(Buffs, it != BUFF_Null,
+ {
+ string s = Buff_UndeprecateName(argv(j));
+ if(s == it.netname)
+ {
+ STAT(BUFFS, this) |= (it.m_itemid);
+ if(!STAT(BUFF_TIME, this))
+ STAT(BUFF_TIME, this) = it.m_time(it);
+ break;
+ }
+ });
+ FOREACH(Weapons, it != WEP_Null, {
+ string s = W_UndeprecateName(argv(j));
+ if(s == it.netname)
+ {
+ STAT(WEAPONS, this) |= (it.m_wepset);
+ if(this.spawnflags == 0 || this.spawnflags == 2)
+ it.wr_init(it);
+ break;
+ }
+ });
+ }
+ }
+
+ string itemprefix, valueprefix;
+ if(this.spawnflags == 0)
+ {
+ itemprefix = "";
+ valueprefix = "";
+ }
+ else if(this.spawnflags == 1)
+ {
+ itemprefix = "max ";
+ valueprefix = "max ";
+ }
+ else if(this.spawnflags == 2)
+ {
+ itemprefix = "min ";
+ valueprefix = "min ";
+ }
+ else if(this.spawnflags == 4)
+ {
+ itemprefix = "minus ";
+ valueprefix = "max ";
+ }
+ else
+ {
+ error("invalid spawnflags");
+ itemprefix = valueprefix = string_null;
+ }
+
+ str = "";
+ str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & IT_UNLIMITED_AMMO), "unlimited_weapon_ammo");
+ str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
+ str = sprintf("%s %s%d %s", str, valueprefix, this.strength_finished * boolean(this.items & ITEM_Strength.m_itemid), "strength");
+ str = sprintf("%s %s%d %s", str, valueprefix, this.invincible_finished * boolean(this.items & ITEM_Shield.m_itemid), "invincible");
+ str = sprintf("%s %s%d %s", str, valueprefix, this.superweapons_finished * boolean(this.items & IT_SUPERWEAPON), "superweapons");
+ str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & ITEM_Jetpack.m_itemid), "jetpack");
+ str = sprintf("%s %s%d %s", str, itemprefix, boolean(this.items & ITEM_JetpackRegen.m_itemid), "fuel_regen");
+ float res;
+ res = GetResource(this, RES_SHELLS); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "shells");
+ res = GetResource(this, RES_BULLETS); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "nails");
+ res = GetResource(this, RES_ROCKETS); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "rockets");
+ res = GetResource(this, RES_CELLS); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "cells");
+ res = GetResource(this, RES_PLASMA); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "plasma");
+ res = GetResource(this, RES_FUEL); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "fuel");
+ res = GetResource(this, RES_HEALTH); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "health");
+ res = GetResource(this, RES_ARMOR); if(res != 0) str = sprintf("%s %s%d %s", str, valueprefix, max(0, res), "armor");
+ // HACK: buffs share a single timer, so we need to include enabled buffs AFTER disabled ones to avoid loss
+ FOREACH(Buffs, it != BUFF_Null && !(STAT(BUFFS, this) & it.m_itemid), str = sprintf("%s %s%d %s", str, valueprefix, max(0, STAT(BUFF_TIME, this)), it.netname));
+ FOREACH(Buffs, it != BUFF_Null && (STAT(BUFFS, this) & it.m_itemid), str = sprintf("%s %s%d %s", str, valueprefix, max(0, STAT(BUFF_TIME, this)), it.netname));
+ FOREACH(Weapons, it != WEP_Null, str = sprintf("%s %s%d %s", str, itemprefix, !!(STAT(WEAPONS, this) & (it.m_wepset)), it.netname));
+ }
+ this.netname = strzone(str);
+
+ n = tokenize_console(this.netname);
+ for(int j = 0; j < n; ++j)
+ {
+ FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(argv(j)) == it.netname, {
+ it.wr_init(it);
+ break;
+ });
+ }
+}
+
+float GiveWeapon(entity e, float wpn, float op, float val)
+{
+ WepSet v0, v1;
+ WepSet s = WepSet_FromWeapon(REGISTRY_GET(Weapons, wpn));
+ v0 = (STAT(WEAPONS, e) & s);
+ switch(op)
+ {
+ case OP_SET:
+ if(val > 0)
+ STAT(WEAPONS, e) |= s;
+ else
+ STAT(WEAPONS, e) &= ~s;
+ break;
+ case OP_MIN:
+ case OP_PLUS:
+ if(val > 0)
+ STAT(WEAPONS, e) |= s;
+ break;
+ case OP_MAX:
+ if(val <= 0)
+ STAT(WEAPONS, e) &= ~s;
+ break;
+ case OP_MINUS:
+ if(val > 0)
+ STAT(WEAPONS, e) &= ~s;
+ break;
+ }
+ v1 = (STAT(WEAPONS, e) & s);
+ return (v0 != v1);
+}
+
+bool GiveBuff(entity e, Buff thebuff, int op, int val)
+{
+ bool had_buff = (STAT(BUFFS, e) & thebuff.m_itemid);
+ float new_buff_time = ((had_buff) ? STAT(BUFF_TIME, e) : 0);
+ switch (op)
+ {
+ case OP_SET:
+ new_buff_time = val;
+ break;
+ case OP_MIN:
+ new_buff_time = max(new_buff_time, val);
+ break;
+ case OP_MAX:
+ new_buff_time = min(new_buff_time, val);
+ break;
+ case OP_PLUS:
+ new_buff_time += val;
+ break;
+ case OP_MINUS:
+ new_buff_time -= val;
+ break;
+ }
+ if(new_buff_time <= 0)
+ {
+ if(had_buff)
+ STAT(BUFF_TIME, e) = new_buff_time;
+ STAT(BUFFS, e) &= ~thebuff.m_itemid;
+ }
+ else
+ {
+ STAT(BUFF_TIME, e) = new_buff_time;
+ STAT(BUFFS, e) = thebuff.m_itemid; // NOTE: replaces any existing buffs on the player!
+ }
+ bool have_buff = (STAT(BUFFS, e) & thebuff.m_itemid);
+ return (had_buff != have_buff);
+}
+
+void GiveSound(entity e, float v0, float v1, float t, Sound snd_incr, Sound snd_decr)
+{
+ if(v1 == v0)
+ return;
+ if(v1 <= v0 - t)
+ {
+ if(snd_decr != NULL)
+ sound (e, CH_TRIGGER, snd_decr, VOL_BASE, ATTEN_NORM);
+ }
+ else if(v0 >= v0 + t)
+ {
+ if(snd_incr != NULL)
+ sound (e, CH_TRIGGER, snd_incr, VOL_BASE, ATTEN_NORM);
+ }
+}
+
+void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
+{
+ if(v0 < v1)
+ e.(rotfield) = max(e.(rotfield), time + rottime);
+ else if(v0 > v1)
+ e.(regenfield) = max(e.(regenfield), time + regentime);
+}
+bool GiveResourceValue(entity e, int res_type, int op, int val)
+{
+ int v0 = GetResource(e, res_type);
+ float new_val = 0;
+ switch (op)
+ {
+ // min 100 cells = at least 100 cells
+ case OP_SET: new_val = val; break;
+ case OP_MIN: new_val = max(v0, val); break;
+ case OP_MAX: new_val = min(v0, val); break;
+ case OP_PLUS: new_val = v0 + val; break;
+ case OP_MINUS: new_val = v0 - val; break;
+ default: return false;
+ }
+
+ return SetResourceExplicit(e, res_type, new_val);
+}
+
+float GiveItems(entity e, float beginarg, float endarg)
+{
+ float got, i, val, op;
+ string cmd;
+
+ val = 999;
+ op = OP_SET;
+
+ got = 0;
+
+ int _switchweapon = 0;
+
+ if(CS(e).autoswitch)
+ {
+ for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
+ {
+ .entity weaponentity = weaponentities[slot];
+ if(e.(weaponentity).m_weapon != WEP_Null || slot == 0)
+ if(e.(weaponentity).m_switchweapon == w_getbestweapon(e, weaponentity))
+ _switchweapon |= BIT(slot);
+ }
+ }
+
+ STAT(STRENGTH_FINISHED, e) = max(0, STAT(STRENGTH_FINISHED, e) - time);
+ STAT(INVINCIBLE_FINISHED, e) = max(0, STAT(INVINCIBLE_FINISHED, e) - time);
+ STAT(SUPERWEAPONS_FINISHED, e) = max(0, STAT(SUPERWEAPONS_FINISHED, e) - time);
+ STAT(BUFF_TIME, e) = max(0, STAT(BUFF_TIME, e) - time);
+
+ PREGIVE(e, items);
+ PREGIVE_WEAPONS(e);
+ PREGIVE(e, stat_STRENGTH_FINISHED);
+ PREGIVE(e, stat_INVINCIBLE_FINISHED);
+ PREGIVE(e, stat_SUPERWEAPONS_FINISHED);
+ PREGIVE_RESOURCE(e, RES_BULLETS);
+ PREGIVE_RESOURCE(e, RES_CELLS);
+ PREGIVE_RESOURCE(e, RES_PLASMA);
+ PREGIVE_RESOURCE(e, RES_SHELLS);
+ PREGIVE_RESOURCE(e, RES_ROCKETS);
+ PREGIVE_RESOURCE(e, RES_FUEL);
+ PREGIVE_RESOURCE(e, RES_ARMOR);
+ PREGIVE_RESOURCE(e, RES_HEALTH);
+
+ for(i = beginarg; i < endarg; ++i)
+ {
+ cmd = argv(i);
+
+ if(cmd == "0" || stof(cmd))
+ {
+ val = stof(cmd);
+ continue;
+ }
+ switch(cmd)
+ {
+ case "no":
+ op = OP_MAX;
+ val = 0;
+ continue;
+ case "max":
+ op = OP_MAX;
+ continue;
+ case "min":
+ op = OP_MIN;
+ continue;
+ case "plus":
+ op = OP_PLUS;
+ continue;
+ case "minus":
+ op = OP_MINUS;
+ continue;
+ case "ALL":
+ got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
+ got += GiveValue(e, stat_STRENGTH_FINISHED, op, val);
+ got += GiveValue(e, stat_INVINCIBLE_FINISHED, op, val);
+ got += GiveValue(e, stat_SUPERWEAPONS_FINISHED, op, val);
+ got += GiveBit(e, items, IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS, op, val);
+ case "all":
+ got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
+ got += GiveResourceValue(e, RES_HEALTH, op, val);
+ got += GiveResourceValue(e, RES_ARMOR, op, val);
+ case "allweapons":
+ FOREACH(Weapons, it != WEP_Null && !(it.spawnflags & (WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_SPECIALATTACK)), got += GiveWeapon(e, it.m_id, op, val));
+ //case "allbuffs": // all buffs makes a player god, do not want!
+ //FOREACH(Buffs, it != BUFF_Null, got += GiveBuff(e, it.m_itemid, op, val));
+ case "allammo":
+ got += GiveResourceValue(e, RES_CELLS, op, val);
+ got += GiveResourceValue(e, RES_PLASMA, op, val);
+ got += GiveResourceValue(e, RES_SHELLS, op, val);
+ got += GiveResourceValue(e, RES_BULLETS, op, val);
+ got += GiveResourceValue(e, RES_ROCKETS, op, val);
+ got += GiveResourceValue(e, RES_FUEL, op, val);
+ break;
+ case "unlimited_ammo":
+ // this is from a time when unlimited superweapons were handled together with ammo in some parts of the code
+ got += GiveBit(e, items, IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS, op, val);
+ break;
+ case "unlimited_weapon_ammo":
+ got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
+ break;
+ case "unlimited_superweapons":
+ got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
+ break;
+ case "jetpack":
+ got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
+ break;
+ case "fuel_regen":
+ got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
+ break;
+ case "strength":
+ got += GiveValue(e, stat_STRENGTH_FINISHED, op, val);
+ break;
+ case "invincible":
+ got += GiveValue(e, stat_INVINCIBLE_FINISHED, op, val);
+ break;
+ case "superweapons":
+ got += GiveValue(e, stat_SUPERWEAPONS_FINISHED, op, val);
+ break;
+ case "cells":
+ got += GiveResourceValue(e, RES_CELLS, op, val);
+ break;
+ case "plasma":
+ got += GiveResourceValue(e, RES_PLASMA, op, val);
+ break;
+ case "shells":
+ got += GiveResourceValue(e, RES_SHELLS, op, val);
+ break;
+ case "nails":
+ case "bullets":
+ got += GiveResourceValue(e, RES_BULLETS, op, val);
+ break;
+ case "rockets":
+ got += GiveResourceValue(e, RES_ROCKETS, op, val);
+ break;
+ case "health":
+ got += GiveResourceValue(e, RES_HEALTH, op, val);
+ break;
+ case "armor":
+ got += GiveResourceValue(e, RES_ARMOR, op, val);
+ break;
+ case "fuel":
+ got += GiveResourceValue(e, RES_FUEL, op, val);
+ break;
+ default:
+ FOREACH(Buffs, it != BUFF_Null && buff_Available(it) && Buff_UndeprecateName(cmd) == it.netname,
+ {
+ got += GiveBuff(e, it, op, val);
+ break;
+ });
+ FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(cmd) == it.netname, {
+ got += GiveWeapon(e, it.m_id, op, val);
+ break;
+ });
+ break;
+ }
+ val = 999;
+ op = OP_SET;
+ }
+
+ POSTGIVE_BIT(e, items, ITEM_JetpackRegen.m_itemid, SND_ITEMPICKUP, SND_Null);
+ POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, SND_POWERUP, SND_POWEROFF);
+ POSTGIVE_BIT(e, items, IT_UNLIMITED_AMMO, SND_POWERUP, SND_POWEROFF);
+ POSTGIVE_BIT(e, items, ITEM_Jetpack.m_itemid, SND_ITEMPICKUP, SND_Null);
+ FOREACH(Weapons, it != WEP_Null, {
+ POSTGIVE_WEAPON(e, it, SND_WEAPONPICKUP, SND_Null);
+ if(!(save_weapons & (it.m_wepset)))
+ if(STAT(WEAPONS, e) & (it.m_wepset))
+ it.wr_init(it);
+ });
+ POSTGIVE_VALUE(e, stat_STRENGTH_FINISHED, 1, SND_POWERUP, SND_POWEROFF);
+ POSTGIVE_VALUE(e, stat_INVINCIBLE_FINISHED, 1, SND_Shield, SND_POWEROFF);
+ //POSTGIVE_VALUE(e, stat_SUPERWEAPONS_FINISHED, 1, SND_Null, SND_Null);
+ POSTGIVE_RESOURCE(e, RES_BULLETS, 0, SND_ITEMPICKUP, SND_Null);
+ POSTGIVE_RESOURCE(e, RES_CELLS, 0, SND_ITEMPICKUP, SND_Null);
+ POSTGIVE_RESOURCE(e, RES_PLASMA, 0, SND_ITEMPICKUP, SND_Null);
+ POSTGIVE_RESOURCE(e, RES_SHELLS, 0, SND_ITEMPICKUP, SND_Null);
+ POSTGIVE_RESOURCE(e, RES_ROCKETS, 0, SND_ITEMPICKUP, SND_Null);
+ 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);
+ 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);
+ 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);
+
+ if(STAT(SUPERWEAPONS_FINISHED, e) <= 0)
+ if(!g_weaponarena && (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS))
+ STAT(SUPERWEAPONS_FINISHED, e) = autocvar_g_balance_superweapons_time;
+
+ if(STAT(STRENGTH_FINISHED, e) <= 0)
+ STAT(STRENGTH_FINISHED, e) = 0;
+ else
+ STAT(STRENGTH_FINISHED, e) += time;
+ if(STAT(INVINCIBLE_FINISHED, e) <= 0)
+ STAT(INVINCIBLE_FINISHED, e) = 0;
+ else
+ STAT(INVINCIBLE_FINISHED, e) += time;
+ if(STAT(SUPERWEAPONS_FINISHED, e) <= 0)
+ STAT(SUPERWEAPONS_FINISHED, e) = 0;
+ else
+ STAT(SUPERWEAPONS_FINISHED, e) += time;
+ if(STAT(BUFF_TIME, e) <= 0)
+ STAT(BUFF_TIME, e) = 0;
+ else
+ STAT(BUFF_TIME, e) += time;
+
+ for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
+ {
+ .entity weaponentity = weaponentities[slot];
+ if(e.(weaponentity).m_weapon != WEP_Null || slot == 0)
+ if(!(STAT(WEAPONS, e) & WepSet_FromWeapon(e.(weaponentity).m_switchweapon)))
+ _switchweapon |= BIT(slot);
+ }
+
+ if(_switchweapon)
+ {
+ for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
+ {
+ .entity weaponentity = weaponentities[slot];
+ if(_switchweapon & BIT(slot))
+ {
+ Weapon wep = w_getbestweapon(e, weaponentity);
+ if(wep != e.(weaponentity).m_switchweapon)
+ W_SwitchWeapon_Force(e, wep, weaponentity);
+ }
+ }
+ }
+
+ return got;
+}
+#endif