]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/items/items.qc
items: misc cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / items / items.qc
index 6562e57533ee57e928c27af5907aa128a6060137..0f764c8627fca5aa90d8867f9937e508d353ba3e 100644 (file)
@@ -59,7 +59,6 @@ bool ItemSend(entity this, entity to, int sf)
        if(sf & ISF_MODEL)
        {
                WriteShort(MSG_ENTITY, bound(0, this.fade_end, 32767));
-               WriteShort(MSG_ENTITY, bound(0, this.fade_start, 32767));
 
                if(this.mdl == "")
                        LOG_TRACE("^1WARNING!^7 this.mdl is unset for item ", this.classname, "expect a crash just about now");
@@ -158,10 +157,7 @@ void Item_Show(entity e, int mode)
                }
                else
                {
-                       //setmodel(e, "null");
                        e.solid = SOLID_NOT;
-                       e.colormod = '0 0 0';
-                       //e.glowmod = e.colormod;
                        e.spawnshieldtime = 1;
                        e.ItemStatus &= ~ITS_AVAILABLE;
                }
@@ -188,9 +184,40 @@ void Item_Show(entity e, int mode)
 
 void Item_Think(entity this)
 {
-       this.nextthink = time;
-       if(this.origin != this.oldorigin)
+       if (Item_IsLoot(this))
+       {
+               if (time < this.wait - IT_DESPAWNFX_TIME)
+                       this.nextthink = min(time + IT_UPDATE_INTERVAL, this.wait - IT_DESPAWNFX_TIME); // ensuring full time for effects
+               else
+               {
+                       // despawning soon, start effects
+                       this.ItemStatus |= ITS_EXPIRING;
+                       this.SendFlags |= ISF_STATUS;
+                       if (time < this.wait - IT_UPDATE_INTERVAL)
+                               this.nextthink = time + IT_UPDATE_INTERVAL;
+                       else
+                       {
+                               setthink(this, RemoveItem);
+                               this.nextthink = this.wait;
+                       }
+               }
+
+               // enable pickup by the player who threw it
+               this.owner = NULL;
+
+               // send slow updates even if the item didn't move
+               // recovers prediction desyncs where server thinks item stopped, client thinks it didn't
                ItemUpdate(this);
+       }
+       else
+       {
+               // bones_was_here: TODO: predict movers, enable client prediction of items with a groundentity,
+               // and then send those less often too (and not all on the same frame)
+               this.nextthink = time;
+
+               if(this.origin != this.oldorigin)
+                       ItemUpdate(this);
+       }
 }
 
 bool Item_ItemsTime_SpectatorOnly(GameItem it);
@@ -213,9 +240,6 @@ void Item_Respawn(entity this)
 
        setthink(this, Item_Think);
        this.nextthink = time;
-
-       //Send_Effect(EFFECT_ITEM_RESPAWN, this.origin + this.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);
-       Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
 }
 
 void Item_RespawnCountdown(entity this)
@@ -701,8 +725,9 @@ LABEL(pickup)
 
        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);
+       GameItem def = this.itemdef;
+       int ch = ((def.instanceOfPowerup && def.m_itemid != IT_RESOURCE) ? CH_TRIGGER_SINGLE : CH_TRIGGER);
+       _sound(toucher, ch, this.item_pickupsound, VOL_BASE, ATTEN_NORM);
 
        MUTATOR_CALLHOOK(ItemTouched, this, toucher);
        if (wasfreed(this))
@@ -817,7 +842,6 @@ void RemoveItem(entity this)
        if(wasfreed(this) || !this) { return; }
        if(this.waypointsprite_attached)
                WaypointSprite_Kill(this.waypointsprite_attached);
-       Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
        delete(this);
 }
 
@@ -932,29 +956,24 @@ 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)
 {
        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;
+       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);
 
        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();
 
@@ -963,16 +982,14 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
        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_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;
+       this.flags = FL_ITEM | def.m_itemflags;
        IL_PUSH(g_items, this);
 
        if(MUTATOR_CALLHOOK(FilterItem, this)) // error means we do not want the item
@@ -982,17 +999,21 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                return;
        }
 
-       precache_model(this.model);
-       precache_sound(this.item_pickupsound);
+       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
+       setsize (this, this.pos1 = def.m_mins, this.pos2 = def.m_maxs);
 
        if (Item_IsLoot(this))
        {
                this.reset = RemoveItem;
+
                set_movetype(this, MOVETYPE_TOSS);
+               this.gravity = 1;
 
-               // Savage: remove thrown items after a certain period of time ("garbage collection")
-               setthink(this, RemoveItem);
-               this.nextthink = time + autocvar_g_items_dropped_lifetime;
+               setthink(this, Item_Think);
+               this.nextthink = time + IT_UPDATE_INTERVAL;
+               this.wait = time + autocvar_g_items_dropped_lifetime;
 
                this.takedamage = DAMAGE_YES;
                this.event_damage = Item_Damage;
@@ -1017,6 +1038,7 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
        }
        else
        {
+               // must be done after def.m_iteminit() as that may set ITEM_FLAG_MUTATORBLOCKED
                if(!have_pickup_item(this))
                {
                        startitem_failed = true;
@@ -1024,6 +1046,13 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                        return;
                }
 
+               // 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(this.angles != '0 0 0')
                        this.SendFlags |= ISF_ANGLES;
 
@@ -1034,7 +1063,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                        if(t) this.team = crc16(false, t);
                }
 
-               this.reset = this.team ? Item_FindTeam : Item_Reset;
                // it's a level item
                if(this.spawnflags & 1)
                        this.noalign = 1;
@@ -1047,9 +1075,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                {
                        // 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);
@@ -1098,25 +1123,21 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                }
 
                Item_ItemsTime_SetTime(this, 0);
+
+               this.glowmod = def.m_color;
        }
 
        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;
 
        // support skinned models for powerups
        if(!this.skin)
                this.skin = def.m_skin;
 
-       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;
@@ -1125,11 +1146,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                        this.ItemStatus |= ITS_ANIMATE2;
        }
 
-       if(Item_IsLoot(this))
-               this.gravity = 1;
-       else
-               this.glowmod = def.m_color;
-
        if(def.instanceOfWeaponPickup)
        {
                if (!Item_IsLoot(this)) // if dropped, colormap is already set up nicely
@@ -1147,9 +1163,13 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
 
                this.effects |= EF_NOGUNBOB; // marker for item team search
                InitializeEntity(this, Item_FindTeam, INITPRIO_FINDTARGET);
+               this.reset = Item_FindTeam;
        }
        else
+       {
+               this.reset = Item_Reset;
                Item_Reset(this);
+       }
 
        Net_LinkEntity(this, !(def.instanceOfPowerup || def.instanceOfHealth || def.instanceOfArmor), 0, ItemSend);
 
@@ -1161,6 +1181,11 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default
                return;
        }
 
+       // we should be sure this item will spawn before loading its assets
+       // CSQC handles model precaching: it may not use this model (eg simple items) and may not have connected yet
+       //precache_model(this.mdl);
+       precache_sound(this.item_pickupsound);
+
        setItemGroup(this);
 }
 
@@ -1170,12 +1195,8 @@ void StartItem(entity this, GameItem def)
 
        this.classname = def.m_canonical_spawnfunc;
 
-       _StartItem(
-               this,
-               this.itemdef = def,
-               def.m_respawntime(), // defaultrespawntime
-               def.m_respawntimejitter() // defaultrespawntimejitter
-       );
+       this.itemdef = def;
+       _StartItem(this, this.itemdef, 0, 0);
 }
 
 #define IS_SMALL(def) ((def.instanceOfHealth && def == ITEM_HealthSmall) || (def.instanceOfArmor && def == ITEM_ArmorSmall))