From 793acd267f4a5801e2aa9e9dca7cf51c5a595a02 Mon Sep 17 00:00:00 2001 From: Lane Kolbly Date: Wed, 12 Jul 2017 05:13:27 -0500 Subject: Changing pickup lifetime and combining semantics, making these adjustable in the lua api. (#3843) --- src/Blocks/WorldInterface.h | 2 ++ src/Entities/Pickup.cpp | 16 +++++++++++----- src/Entities/Pickup.h | 18 +++++++++++++++++- src/World.cpp | 16 ++++++++++++++++ src/World.h | 3 +++ 5 files changed, 49 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Blocks/WorldInterface.h b/src/Blocks/WorldInterface.h index c6cf070a0..bac6e2bdf 100644 --- a/src/Blocks/WorldInterface.h +++ b/src/Blocks/WorldInterface.h @@ -33,6 +33,8 @@ public: /** Spawns item pickups for each item in the list. May compress pickups if too many entities. All pickups get the speed specified. */ virtual void SpawnItemPickups(const cItems & a_Pickups, double a_BlockX, double a_BlockY, double a_BlockZ, double a_SpeedX, double a_SpeedY, double a_SpeedZ, bool IsPlayerCreated = false) = 0; + virtual UInt32 SpawnItemPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, float a_SpeedX = 0.f, float a_SpeedY = 0.f, float a_SpeedZ = 0.f, int a_LifetimeTicks = 6000, bool a_CanCombine = true) = 0; + /** Spawns a mob of the specified type. Returns the mob's UniqueID if recognized and spawned, or cEntity::INVALID_ID on failure. */ virtual UInt32 SpawnMob(double a_PosX, double a_PosY, double a_PosZ, eMonsterType a_MonsterType, bool a_Baby) = 0; diff --git a/src/Entities/Pickup.cpp b/src/Entities/Pickup.cpp index 5f64d6fbe..0c6c8feab 100644 --- a/src/Entities/Pickup.cpp +++ b/src/Entities/Pickup.cpp @@ -40,8 +40,9 @@ public: Vector3d EntityPos = a_Entity->GetPosition(); double Distance = (EntityPos - m_Position).Length(); - cItem & Item = static_cast(a_Entity)->GetItem(); - if ((Distance < 1.2) && Item.IsEqual(m_Pickup->GetItem())) + cPickup * OtherPickup = static_cast(a_Entity); + cItem & Item = OtherPickup->GetItem(); + if ((Distance < 1.2) && Item.IsEqual(m_Pickup->GetItem()) && OtherPickup->CanCombine()) { short CombineCount = Item.m_ItemCount; if ((CombineCount + m_Pickup->GetItem().m_ItemCount) > Item.GetMaxStackSize()) @@ -66,6 +67,9 @@ public: a_Entity->GetWorld()->BroadcastEntityRelMove(*a_Entity, static_cast(DiffX), static_cast(DiffY), static_cast(DiffZ)); /* End of experimental animation */ a_Entity->Destroy(); + + // Reset the timer + m_Pickup->SetAge(0); } else { @@ -92,12 +96,14 @@ protected: -cPickup::cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX /* = 0.f */, float a_SpeedY /* = 0.f */, float a_SpeedZ /* = 0.f */) +cPickup::cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX, float a_SpeedY, float a_SpeedZ, int a_LifetimeTicks, bool a_CanCombine) : cEntity(etPickup, a_PosX, a_PosY, a_PosZ, 0.2, 0.2) , m_Timer(0) , m_Item(a_Item) , m_bCollected(false) , m_bIsPlayerCreated(IsPlayerCreated) + , m_bCanCombine(a_CanCombine) + , m_Lifetime(cTickTime(a_LifetimeTicks)) { SetGravity(-16.0f); SetAirDrag(0.02f); @@ -165,7 +171,7 @@ void cPickup::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } // Try to combine the pickup with adjacent same-item pickups: - if ((m_Item.m_ItemCount < m_Item.GetMaxStackSize()) && IsOnGround()) // Don't combine if already full or not on ground + if ((m_Item.m_ItemCount < m_Item.GetMaxStackSize()) && IsOnGround() && CanCombine()) // Don't combine if already full or not on ground { // By using a_Chunk's ForEachEntity() instead of cWorld's, pickups don't combine across chunk boundaries. // That is a small price to pay for not having to traverse the entire world for each entity. @@ -188,7 +194,7 @@ void cPickup::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } } - if (m_Timer > std::chrono::minutes(5)) // 5 minutes + if (m_Timer > m_Lifetime) { Destroy(true); return; diff --git a/src/Entities/Pickup.h b/src/Entities/Pickup.h index ed5949f37..c2fcbd7f2 100644 --- a/src/Entities/Pickup.h +++ b/src/Entities/Pickup.h @@ -25,7 +25,7 @@ public: CLASS_PROTODEF(cPickup) - cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX = 0.f, float a_SpeedY = 0.f, float a_SpeedZ = 0.f); + cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX = 0.f, float a_SpeedY = 0.f, float a_SpeedZ = 0.f, int a_LifetimeTicks = 6000, bool a_CanCombine = true); cItem & GetItem(void) {return m_Item; } // tolua_export const cItem & GetItem(void) const {return m_Item; } @@ -36,12 +36,24 @@ public: virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; + /** Returns whether this pickup is allowed to combine with other similar pickups */ + bool CanCombine(void) const { return m_bCanCombine; } // tolua_export + + /** Sets whether this pickup is allowed to combine with other similar pickups */ + void SetCanCombine(bool a_CanCombine) { m_bCanCombine = a_CanCombine; } // tolua_export + /** Returns the number of ticks that this entity has existed */ int GetAge(void) const { return std::chrono::duration_cast(m_Timer).count(); } // tolua_export /** Set the number of ticks that this entity has existed */ void SetAge(int a_Age) { m_Timer = cTickTime(a_Age); } // tolua_export + /** Returns the number of ticks that this pickup should live for */ + int GetLifetime(void) const { return std::chrono::duration_cast(m_Lifetime).count(); } // tolua_export + + /** Set the number of ticks that this pickup should live for */ + void SetLifetime(int a_Lifetime) { m_Lifetime = cTickTime(a_Lifetime); } // tolua_export + /** Returns true if the pickup has already been collected */ bool IsCollected(void) const { return m_bCollected; } // tolua_export @@ -58,4 +70,8 @@ private: bool m_bCollected; bool m_bIsPlayerCreated; + + bool m_bCanCombine; + + std::chrono::milliseconds m_Lifetime; }; // tolua_export diff --git a/src/World.cpp b/src/World.cpp index fa2116597..da7d8fb2d 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2223,6 +2223,22 @@ void cWorld::SpawnItemPickups(const cItems & a_Pickups, double a_BlockX, double +UInt32 cWorld::SpawnItemPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, float a_SpeedX, float a_SpeedY, float a_SpeedZ, int a_LifetimeTicks, bool a_CanCombine) +{ + cPickup * Pickup = new cPickup(a_PosX, a_PosY, a_PosZ, a_Item, false, a_SpeedX, a_SpeedY, a_SpeedZ, a_LifetimeTicks, a_CanCombine); + if (!Pickup->Initialize(*this)) + { + delete Pickup; + Pickup = nullptr; + return cEntity::INVALID_ID; + } + return Pickup->GetUniqueID(); +} + + + + + UInt32 cWorld::SpawnFallingBlock(int a_X, int a_Y, int a_Z, BLOCKTYPE BlockType, NIBBLETYPE BlockMeta) { cFallingBlock * FallingBlock = new cFallingBlock(Vector3i(a_X, a_Y, a_Z), BlockType, BlockMeta); diff --git a/src/World.h b/src/World.h index bae7adbb9..a3180e008 100644 --- a/src/World.h +++ b/src/World.h @@ -443,6 +443,9 @@ public: /** Spawns item pickups for each item in the list. May compress pickups if too many entities. All pickups get the speed specified. */ virtual void SpawnItemPickups(const cItems & a_Pickups, double a_BlockX, double a_BlockY, double a_BlockZ, double a_SpeedX, double a_SpeedY, double a_SpeedZ, bool IsPlayerCreated = false) override; + /** Spawns a single pickup containing the specified item. */ + virtual UInt32 SpawnItemPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, float a_SpeedX = 0.f, float a_SpeedY = 0.f, float a_SpeedZ = 0.f, int a_LifetimeTicks = 6000, bool a_CanCombine = true) override; + /** Spawns an falling block entity at the given position. Returns the UniqueID of the spawned falling block, or cEntity::INVALID_ID on failure. */ UInt32 SpawnFallingBlock(int a_X, int a_Y, int a_Z, BLOCKTYPE BlockType, NIBBLETYPE BlockMeta); -- cgit v1.2.3