]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/items/items.qc
Merge branch 'master' into z411/bai-server
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / items / items.qc
index e0013b5ce0d37ff4eff0cd2a5bedd0c52407e189..86ede0b7bd20cd481dc0830905737dfb188d70d6 100644 (file)
@@ -36,6 +36,24 @@ bool ItemSend(entity this, entity to, int sf)
        else
                sf &= ~ISF_DROP;
 
+       // if this item is being spawned (in CSQC's perspective)
+       // reuse ISF_SIZE and ISF_SIZE2 to also tell CSQC its bbox size
+       if(sf & ISF_SIZE)
+       {
+               if(this.maxs == ITEM_S_MAXS) // Small
+               {
+                       sf |= ISF_SIZE;
+                       sf &= ~ISF_SIZE2;
+               }
+               else if(this.maxs == ITEM_L_MAXS) // Large
+               {
+                       sf &= ~ISF_SIZE;
+                       sf |= ISF_SIZE2;
+               }
+               else // Default
+                       sf |= ISF_SIZE | ISF_SIZE2;
+       }
+
        WriteHeader(MSG_ENTITY, ENT_CLIENT_ITEM);
        WriteByte(MSG_ENTITY, sf);
 
@@ -50,13 +68,10 @@ bool ItemSend(entity this, entity to, int sf)
                WriteAngleVector(MSG_ENTITY, this.angles);
        }
 
-       // sets size on the client, unused on server
-       //if(sf & ISF_SIZE)
-
        if(sf & ISF_STATUS)
                WriteByte(MSG_ENTITY, this.ItemStatus);
 
-       if(sf & ISF_MODEL)
+       if(sf & ISF_SIZE || sf & ISF_SIZE2) // always true when it's spawned (in CSQC's perspective)
        {
                WriteShort(MSG_ENTITY, bound(0, this.fade_end, 32767));
 
@@ -100,14 +115,7 @@ bool have_pickup_item(entity this)
        if (this.itemdef.spawnflags & ITEM_FLAG_MUTATORBLOCKED)
                return false;
 
-       if(this.itemdef.instanceOfPowerup)
-       {
-               if(autocvar_g_powerups > 0)
-                       return true;
-               if(autocvar_g_powerups == 0)
-                       return false;
-       }
-       else
+       if(!this.itemdef.instanceOfPowerup)
        {
                if(autocvar_g_pickup_items > 0)
                        return true;
@@ -249,6 +257,8 @@ void Item_Respawn(entity this)
 
 void Item_RespawnCountdown(entity this)
 {
+       if(game_timeout) { this.nextthink = time + 1; return; }
+       
        if(this.item_respawncounter >= ITEM_RESPAWN_TICKS)
        {
                if(this.waypointsprite_attached)
@@ -258,7 +268,11 @@ void Item_RespawnCountdown(entity this)
        else
        {
                this.nextthink = time + 1;
-               this.item_respawncounter += 1;
+               this.item_respawncounter = floor((time - game_starttime) - (this.scheduledrespawntime - ITEM_RESPAWN_TICKS)) + 1;
+               //this.item_respawncounter += 1;
+               //LOG_INFOF("Respawncounter: %d", this.item_respawncounter);
+               if(this.item_respawncounter < 1) return;
+               
                if(this.item_respawncounter == 1)
                {
                        do {
@@ -307,12 +321,14 @@ void Item_RespawnCountdown(entity this)
 
 void Item_RespawnThink(entity this)
 {
-       this.nextthink = time;
+       this.nextthink = time + 1;
        if(this.origin != this.oldorigin)
                ItemUpdate(this);
-
-       if(time >= this.wait)
+       
+       if(!game_timeout && time - game_starttime >= this.wait)
                Item_Respawn(this);
+       
+       //LOG_INFOF("time until respawn %d", (this.wait) - (time - game_starttime));
 }
 
 void Item_ScheduleRespawnIn(entity e, float t)
@@ -321,9 +337,12 @@ void Item_ScheduleRespawnIn(entity e, float t)
        if ((Item_ItemsTime_Allow(e.itemdef) || (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS) || MUTATOR_CALLHOOK(Item_ScheduleRespawn, e, t)) && (t - ITEM_RESPAWN_TICKS) > 0)
        {
                setthink(e, Item_RespawnCountdown);
-               e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);
-               e.scheduledrespawntime = e.nextthink + ITEM_RESPAWN_TICKS;
+               //e.nextthink = time - timeout_total_time + max(0, t - ITEM_RESPAWN_TICKS);
+               //e.scheduledrespawntime = e.nextthink + ITEM_RESPAWN_TICKS;
+               e.nextthink = time;
+               e.scheduledrespawntime = time - game_starttime + t;
                e.item_respawncounter = 0;
+               
                if(Item_ItemsTime_Allow(e.itemdef) || (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS))
                {
                        t = Item_ItemsTime_UpdateTime(e, e.scheduledrespawntime);
@@ -335,8 +354,8 @@ void Item_ScheduleRespawnIn(entity e, float t)
        {
                setthink(e, Item_RespawnThink);
                e.nextthink = time;
-               e.scheduledrespawntime = time + t;
-               e.wait = time + t;
+               e.scheduledrespawntime = time - game_starttime + t;
+               e.wait = time - game_starttime + t;
 
                if(Item_ItemsTime_Allow(e.itemdef) || (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS))
                {
@@ -549,6 +568,13 @@ bool Item_GiveTo(entity item, entity player)
        pickedup |= Item_GiveAmmoTo(item, player, RES_CELLS, g_pickup_cells_max);
        pickedup |= Item_GiveAmmoTo(item, player, RES_PLASMA, g_pickup_plasma_max);
        pickedup |= Item_GiveAmmoTo(item, player, RES_FUEL, g_pickup_fuel_max);
+       
+       // for RJZ
+       if (autocvar_rjz_count_shards && !warmup_stage && item.itemdef == ITEM_ArmorSmall) {
+               total_shards++;
+               send_TotalShardsAll();
+       }
+       
        if (item.itemdef.instanceOfWeaponPickup)
        {
                WepSet w, wp;
@@ -584,8 +610,8 @@ bool Item_GiveTo(entity item, entity player)
                        Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_JETPACK_GOT);
        }
 
-       int its;
-       if((its = (item.items - (item.items & player.items)) & IT_PICKUPMASK))
+       int its = (item.items - (item.items & player.items)) & IT_PICKUPMASK;
+       if (its)
        {
                pickedup = true;
                player.items |= its;
@@ -679,6 +705,7 @@ void Item_Touch(entity this, entity toucher)
        {
                if (ITEM_TOUCH_NEEDKILL())
                {
+                       this.SendFlags |= ISF_REMOVEFX;
                        RemoveItem(this);
                        return;
                }
@@ -742,7 +769,8 @@ LABEL(pickup)
 
        if (ITEM_IS_LOOT(this))
        {
-               delete(this);
+               this.SendFlags |= ISF_REMOVEFX;
+               RemoveItem(this);
                return;
        }
        if (!this.spawnshieldtime)
@@ -847,7 +875,16 @@ void RemoveItem(entity this)
        if(wasfreed(this) || !this) { return; }
        if(this.waypointsprite_attached)
                WaypointSprite_Kill(this.waypointsprite_attached);
-       delete(this);
+
+       if (this.SendFlags & ISF_REMOVEFX)
+       {
+               // delay removal until ISF_REMOVEFX has been sent
+               setthink(this, RemoveItem);
+               this.nextthink = time + 2 * autocvar_sys_ticrate; // micro optimisation: next frame will be too soon
+               this.solid = SOLID_NOT; // untouchable
+       }
+       else
+               delete(this);
 }
 
 // pickup evaluation functions
@@ -961,49 +998,56 @@ void item_use(entity this, entity actor, entity trigger)
        gettouch(this)(this, actor);
 }
 
-// if defaultrespawntime is 0 get respawntime from the item definition
-// if defaultrespawntimejitter is 0 get respawntimejitter from the item definition
-void _StartItem(entity this, entity def, float defaultrespawntime, float defaultrespawntimejitter)
+void StartItem(entity this, entity def)
 {
-       string itemname = def.m_name;
-       float(entity player, entity item) pickupevalfunc = def.m_pickupevalfunc;
-       float pickupbasevalue = def.m_botvalue;
+       if (def.m_spawnfunc_hookreplace)
+               def = def.m_spawnfunc_hookreplace(def, this);
+       this.itemdef = def;
+       if (def.m_canonical_spawnfunc != "") // FIXME why do weapons set itemdef to an entity that doesn't have this?
+               this.classname = def.m_canonical_spawnfunc;
 
-       startitem_failed = false;
-
-       this.item_model_ent = def.m_model;
-       this.item_pickupsound_ent = def.m_sound;
-       if (!this.item_pickupsound)
-               this.item_pickupsound = Sound_fixpath(this.item_pickupsound_ent);
+       startitem_failed = true; // early return means failure
 
+       // some mutators check for resources set by m_iteminit in FilterItem
        if(def.m_iteminit)
                def.m_iteminit(def, this);
 
+       // also checked by some mutators in FilterItem
+       this.items = def.m_itemid;
+       this.weapon = def.instanceOfWeaponPickup ? def.m_weapon.m_id : 0;
+       if(this.weapon)
+               STAT(WEAPONS, this) = WepSet_FromWeapon(REGISTRY_GET(Weapons, this.weapon));
+       this.flags = FL_ITEM | def.m_itemflags;
+
+       // FilterItem may change any field of a specific instance of an item, but
+       // it must not change any itemdef field (would cause mutators to break other mutators),
+       // and must not convert items into different ones (StartItem could be refactored to support that).
+       if(MUTATOR_CALLHOOK(FilterItem, this))
+       {
+               delete(this);
+               return;
+       }
+
+       if (!this.item_model_ent)
+               this.item_model_ent = def.m_model;
+
+       if (!this.item_pickupsound_ent)
+               this.item_pickupsound_ent = def.m_sound;
+       if (!this.item_pickupsound && this.item_pickupsound_ent)
+               this.item_pickupsound = Sound_fixpath(this.item_pickupsound_ent);
+       if (this.item_pickupsound == "")
+               LOG_WARNF("No pickup sound set for a %s", this.classname);
+
        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;
-
        // bones_was_here TODO: implement sv_cullentities_dist and replace g_items_maxdist with it
        if(!this.fade_end)
                this.fade_end = autocvar_g_items_maxdist;
 
-       if(weaponid)
-               STAT(WEAPONS, this) = WepSet_FromWeapon(REGISTRY_GET(Weapons, weaponid));
-
-       this.flags = FL_ITEM | def.m_itemflags;
+       // bones_was_here TODO: can we do this after we're sure the entity won't be deleted?
        IL_PUSH(g_items, this);
 
-       if(MUTATOR_CALLHOOK(FilterItem, this)) // error means we do not want the item
-       {
-               startitem_failed = true;
-               delete(this);
-               return;
-       }
-
        this.mdl = this.model ? this.model : strzone(this.item_model_ent.model_str());
        setmodel(this, MDL_Null); // precision set below
        // set item size before we spawn a waypoint or droptofloor or MoveOutOfSolid
@@ -1035,11 +1079,13 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                        this.nextthink = max(this.strength_finished, this.invincible_finished, this.superweapons_finished);
                }
 
+               // most loot items have a bigger horizontal size than a player
+               nudgeoutofsolid(this);
+
                // 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;
                }
@@ -1051,7 +1097,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                // must be done after def.m_iteminit() as that may set ITEM_FLAG_MUTATORBLOCKED
                if(!have_pickup_item(this))
                {
-                       startitem_failed = true;
                        delete(this);
                        return;
                }
@@ -1059,18 +1104,33 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                // must be done before Item_Reset() and after MUTATORBLOCKED check (blocked items may have null func ptrs)
                if(!this.respawntime) // both need to be set
                {
-                       this.respawntime = defaultrespawntime ? defaultrespawntime : def.m_respawntime();
-                       this.respawntimejitter = defaultrespawntimejitter ? defaultrespawntimejitter : def.m_respawntimejitter();
+                       if (def.m_respawntime)
+                               this.respawntime = def.m_respawntime();
+                       else
+                               LOG_WARNF("Default respawntime for a %s is unavailable from its itemdef", this.classname);
+
+                       if (def.m_respawntimejitter)
+                               this.respawntimejitter = def.m_respawntimejitter();
+                       else
+                               LOG_WARNF("Default respawntimejitter for a %s is unavailable from its itemdef", this.classname);
                }
 
                if(this.angles != '0 0 0')
                        this.SendFlags |= ISF_ANGLES;
 
-               if(q3compat && !this.team)
+               if(q3compat)
                {
-                       string t = GetField_fullspawndata(this, "team");
-                       // bones_was_here: this hack is cheaper than changing to a .string strcmp()
-                       if(t) this.team = crc16(false, t);
+                       if (!this.team)
+                       {
+                               string t = GetField_fullspawndata(this, "team");
+                               // bones_was_here: this hack is cheaper than changing to a .string strcmp()
+                               if(t) this.team = crc16(false, t);
+                       }
+
+                       // In Q3 the origin is in the middle of the bbox ("radius" 15), in Xon it's at the bottom
+                       // so we need to offset vertically (only for items placed by the mapper).
+                       this.origin.z += -15 - this.mins.z;
+                       setorigin(this, this.origin);
                }
 
                // it's a level item
@@ -1083,8 +1143,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                // 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');
                        // note droptofloor returns false if stuck/or would fall too far
                        if (!this.noalign)
                                droptofloor(this);
@@ -1099,7 +1157,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                {
                        // target_give not yet supported; maybe later
                        print("removed targeted ", this.classname, "\n");
-                       startitem_failed = true;
                        delete(this);
                        return;
                }
@@ -1112,20 +1169,20 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                {
                        // 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("XXX Found duplicated item: ", def.m_name, 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));
+               weaponsInMap |= WepSet_FromWeapon(REGISTRY_GET(Weapons, this.weapon));
 
                if (        def.instanceOfPowerup
                        ||  def.instanceOfWeaponPickup
                        || (def.instanceOfHealth && def != ITEM_HealthSmall)
                        || (def.instanceOfArmor && def != ITEM_ArmorSmall)
-                       || (itemid & (IT_KEY1 | IT_KEY2))
+                       || (def.m_itemid & (IT_KEY1 | IT_KEY2))
                )
                {
                        if(!this.target || this.target == "")
@@ -1138,9 +1195,9 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
        }
 
        this.bot_pickup = true;
-       this.bot_pickupevalfunc = pickupevalfunc;
-       this.bot_pickupbasevalue = pickupbasevalue;
-       this.netname = itemname;
+       this.bot_pickupevalfunc = def.m_pickupevalfunc;
+       this.bot_pickupbasevalue = def.m_botvalue;
+       this.netname = def.m_name;
        settouch(this, Item_Touch);
        //this.effects |= EF_LOWPRECISION;
 
@@ -1183,7 +1240,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
        // call this hook after everything else has been done
        if (MUTATOR_CALLHOOK(Item_Spawn, this))
        {
-               startitem_failed = true;
                delete(this);
                return;
        }
@@ -1194,16 +1250,8 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
        precache_sound(this.item_pickupsound);
 
        setItemGroup(this);
-}
-
-void StartItem(entity this, GameItem def)
-{
-       def = def.m_spawnfunc_hookreplace(def, this);
 
-       this.classname = def.m_canonical_spawnfunc;
-
-       this.itemdef = def;
-       _StartItem(this, this.itemdef, 0, 0);
+       startitem_failed = false;
 }
 
 #define IS_SMALL(def) ((def.instanceOfHealth && def == ITEM_HealthSmall) || (def.instanceOfArmor && def == ITEM_ArmorSmall))
@@ -1245,7 +1293,7 @@ void setItemGroup(entity this)
 
 void setItemGroupCount()
 {
-       for (int k = 1; k <= group_count; k++)
+       for (int k = 1; k <= group_count; ++k)
        {
                int count = 0;
                IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { count++; });