From dc878898f36473c4df54457d2738562df4dec908 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Wed, 10 Apr 2013 21:40:30 +0000 Subject: Dispensers rewritten to use ItemGrid git-svn-id: http://mc-server.googlecode.com/svn/trunk@1383 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Bindings.cpp | 45 +++++- source/Bindings.h | 2 +- source/DispenserEntity.cpp | 249 ++++++++++++++--------------- source/DispenserEntity.h | 20 ++- source/Item.cpp | 11 ++ source/Item.h | 13 +- source/UI/SlotArea.cpp | 2 +- source/WorldStorage/NBTChunkSerializer.cpp | 10 +- 8 files changed, 198 insertions(+), 154 deletions(-) diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 1b73d56a2..915e7110f 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 04/10/13 23:01:46. +** Generated automatically by tolua++-1.0.92 on 04/10/13 23:35:42. */ #ifndef __cplusplus @@ -13064,6 +13064,48 @@ static int tolua_AllToLua_cItem_IsSameType00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE +/* method: CopyOne of class cItem */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cItem_CopyOne00 +static int tolua_AllToLua_cItem_CopyOne00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const cItem",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const cItem* self = (const cItem*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'CopyOne'", NULL); +#endif + { + cItem tolua_ret = (cItem) self->CopyOne(); + { +#ifdef __cplusplus + void* tolua_obj = Mtolua_new((cItem)(tolua_ret)); + tolua_pushusertype(tolua_S,tolua_obj,"cItem"); + tolua_register_gc(tolua_S,lua_gettop(tolua_S)); +#else + void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(cItem)); + tolua_pushusertype(tolua_S,tolua_obj,"cItem"); + tolua_register_gc(tolua_S,lua_gettop(tolua_S)); +#endif + } + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'CopyOne'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: GetMaxDuration of class cItem */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cItem_GetMaxDuration00 static int tolua_AllToLua_cItem_GetMaxDuration00(lua_State* tolua_S) @@ -24769,6 +24811,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"IsEmpty",tolua_AllToLua_cItem_IsEmpty00); tolua_function(tolua_S,"IsEqual",tolua_AllToLua_cItem_IsEqual00); tolua_function(tolua_S,"IsSameType",tolua_AllToLua_cItem_IsSameType00); + tolua_function(tolua_S,"CopyOne",tolua_AllToLua_cItem_CopyOne00); tolua_function(tolua_S,"GetMaxDuration",tolua_AllToLua_cItem_GetMaxDuration00); tolua_function(tolua_S,"DamageItem",tolua_AllToLua_cItem_DamageItem00); tolua_function(tolua_S,"HasDuration",tolua_AllToLua_cItem_HasDuration00); diff --git a/source/Bindings.h b/source/Bindings.h index 352afe0d8..a120db988 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 04/10/13 23:01:47. +** Generated automatically by tolua++-1.0.92 on 04/10/13 23:35:43. */ /* Exported function */ diff --git a/source/DispenserEntity.cpp b/source/DispenserEntity.cpp index 5b0af8e42..1c95172e2 100644 --- a/source/DispenserEntity.cpp +++ b/source/DispenserEntity.cpp @@ -31,10 +31,10 @@ -cDispenserEntity::cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) - : cBlockEntity( E_BLOCK_DISPENSER, a_X, a_Y, a_Z, a_World ) - , m_Items( new cItem[9] ) - , m_CanDispense( 0 ) +cDispenserEntity::cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) : + cBlockEntity(E_BLOCK_DISPENSER, a_X, a_Y, a_Z, a_World), + m_Contents(3, 3), + m_ShouldDispense(false) { SetBlockEntity(this); // cBlockEntityWindowOwner } @@ -46,15 +46,10 @@ cDispenserEntity::cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) cDispenserEntity::~cDispenserEntity() { // Tell window its owner is destroyed - if( GetWindow() ) + cWindow * Window = GetWindow(); + if (Window != NULL) { - GetWindow()->OwnerDestroyed(); - } - - // Clean up items - if( m_Items ) - { - delete [] m_Items; + Window->OwnerDestroyed(); } } @@ -62,18 +57,12 @@ cDispenserEntity::~cDispenserEntity() -void cDispenserEntity::Destroy() +void cDispenserEntity::Destroy(void) { // Drop items cItems Pickups; - for( int i = 0; i < 9; i++) - { - if( !m_Items[i].IsEmpty() ) - { - Pickups.push_back(m_Items[i]); - m_Items[i].Empty(); - } - } + m_Contents.CopyToItems(Pickups); + m_Contents.Clear(); m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ); } @@ -81,122 +70,131 @@ void cDispenserEntity::Destroy() -void cDispenserEntity::Dispense() +void cDispenserEntity::Dispense(void) { int Disp_X = m_PosX; int Disp_Y = m_PosY; int Disp_Z = m_PosZ; - NIBBLETYPE Meta = m_World->GetBlockMeta( m_PosX, m_PosY, m_PosZ ); - AddDispenserDir( Disp_X, Disp_Y, Disp_Z, Meta ); - char OccupiedSlots[9]; - char SlotsCnt = 0; - for( int i = 0; i < 9; i++) + NIBBLETYPE Meta = m_World->GetBlockMeta(m_PosX, m_PosY, m_PosZ); + AddDispenserDir(Disp_X, Disp_Y, Disp_Z, Meta); + int OccupiedSlots[9]; + int SlotsCnt = 0; + for (int i = m_Contents.GetNumSlots() - 1; i >= 0; i--) { - if( !m_Items[i].IsEmpty() ) + if (!m_Contents.GetSlot(i).IsEmpty()) { OccupiedSlots[SlotsCnt] = i; SlotsCnt++; } + } // for i - m_Contents[] + + if (SlotsCnt == 0) + { + // Nothing in the dispenser, play the click sound + m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.2f); + return; } - if(SlotsCnt > 0) + + // Pick an item to dispense: + MTRand r1; + int RandomSlot = r1.randInt(SlotsCnt); + cItem & Drop = m_Contents.GetSlot(OccupiedSlots[RandomSlot]); + + // Dispense the item: + switch (Drop.m_ItemType) { - MTRand r1; - char RandomSlot = r1.randInt() % SlotsCnt; - cItem Drop = m_Items[OccupiedSlots[RandomSlot]]; - switch( m_Items[OccupiedSlots[RandomSlot]].m_ItemType ) + case E_ITEM_BUCKET: { - case E_ITEM_BUCKET: + BLOCKTYPE DispBlock = m_World->GetBlock(Disp_X, Disp_Y, Disp_Z); + if (DispBlock == E_BLOCK_STATIONARY_WATER) + { + m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_AIR, 0); + Drop.m_ItemType = E_ITEM_WATER_BUCKET; // TODO: Duplication glitch - bucket stacking allows you to duplicate water + } + else if (DispBlock == E_BLOCK_STATIONARY_LAVA) { - BLOCKTYPE DispBlock = m_World->GetBlock( Disp_X, Disp_Y, Disp_Z ); - if( DispBlock == E_BLOCK_STATIONARY_WATER ) - { - m_World->SetBlock( Disp_X, Disp_Y, Disp_Z, E_BLOCK_AIR, 0 ); - m_Items[OccupiedSlots[RandomSlot]].m_ItemType = E_ITEM_WATER_BUCKET; - } - else if( DispBlock == E_BLOCK_STATIONARY_LAVA ) - { - m_World->SetBlock( Disp_X, Disp_Y, Disp_Z, E_BLOCK_AIR, 0 ); - m_Items[OccupiedSlots[RandomSlot]].m_ItemType = E_ITEM_LAVA_BUCKET; - } - else - { - cItems Pickups; - Pickups.push_back(cItem(Drop.m_ItemType, 1, Drop.m_ItemDamage)); - m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); - m_Items[OccupiedSlots[RandomSlot]].m_ItemCount--; - } - break; + m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_AIR, 0); + Drop.m_ItemType = E_ITEM_LAVA_BUCKET; // TODO: Duplication glitch - bucket stacking allows you to duplicate lava } - case E_ITEM_WATER_BUCKET: + else + { + cItems Pickups; + Pickups.push_back(Drop.CopyOne()); + m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); + Drop.m_ItemCount--; + } + break; + } + + case E_ITEM_WATER_BUCKET: + { + BLOCKTYPE DispBlock = m_World->GetBlock(Disp_X, Disp_Y, Disp_Z); + if ((DispBlock == E_BLOCK_AIR) || IsBlockLiquid(DispBlock) || cFluidSimulator::CanWashAway(DispBlock)) { - BLOCKTYPE DispBlock = m_World->GetBlock( Disp_X, Disp_Y, Disp_Z ); - if( DispBlock == E_BLOCK_AIR || IsBlockLiquid(DispBlock) || cFluidSimulator::CanWashAway(DispBlock) ) - { - m_World->SetBlock( Disp_X, Disp_Y, Disp_Z, E_BLOCK_STATIONARY_WATER, 0 ); - m_Items[OccupiedSlots[RandomSlot]].m_ItemType = E_ITEM_BUCKET; - } - else - { - cItems Pickups; - Pickups.push_back(cItem(Drop.m_ItemType, 1, Drop.m_ItemDamage)); - m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); - m_Items[OccupiedSlots[RandomSlot]].m_ItemCount--; - } - break; + m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_STATIONARY_WATER, 0); + Drop.m_ItemType = E_ITEM_BUCKET; } - case E_ITEM_LAVA_BUCKET: + else { - BLOCKTYPE DispBlock = m_World->GetBlock( Disp_X, Disp_Y, Disp_Z ); - if( DispBlock == E_BLOCK_AIR || IsBlockLiquid(DispBlock) || cFluidSimulator::CanWashAway(DispBlock) ) - { - m_World->SetBlock( Disp_X, Disp_Y, Disp_Z, E_BLOCK_STATIONARY_LAVA, 0 ); - m_Items[OccupiedSlots[RandomSlot]].m_ItemType = E_ITEM_BUCKET; - } - else - { - cItems Pickups; - Pickups.push_back(cItem(Drop.m_ItemType, 1, Drop.m_ItemDamage)); - m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); - m_Items[OccupiedSlots[RandomSlot]].m_ItemCount--; - } - break; + cItems Pickups; + Pickups.push_back(Drop.CopyOne()); + m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); + Drop.m_ItemCount--; } - case E_ITEM_SPAWN_EGG: + break; + } + + case E_ITEM_LAVA_BUCKET: + { + BLOCKTYPE DispBlock = m_World->GetBlock( Disp_X, Disp_Y, Disp_Z ); + if ((DispBlock == E_BLOCK_AIR) || IsBlockLiquid(DispBlock) || cFluidSimulator::CanWashAway(DispBlock)) { - if (m_World->SpawnMob(Disp_X + 0.5, Disp_Y, Disp_Z + 0.5, m_Items[OccupiedSlots[RandomSlot]].m_ItemDamage) >= 0) - { - m_Items[OccupiedSlots[RandomSlot]].m_ItemCount--; - } - break; + m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_STATIONARY_LAVA, 0); + Drop.m_ItemType = E_ITEM_BUCKET; } - default: + else { cItems Pickups; - Pickups.push_back(cItem(Drop.m_ItemType, 1, Drop.m_ItemDamage)); + Pickups.push_back(Drop.CopyOne()); m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); - m_Items[OccupiedSlots[RandomSlot]].m_ItemCount--; - break; + Drop.m_ItemCount--; } + break; } - char SmokeDir; - switch( Meta ) + + case E_ITEM_SPAWN_EGG: { - case 2: SmokeDir = 1; break; - case 3: SmokeDir = 7; break; - case 4: SmokeDir = 3; break; - case 5: SmokeDir = 5; break; + if (m_World->SpawnMob(Disp_X + 0.5, Disp_Y, Disp_Z + 0.5, Drop.m_ItemDamage) >= 0) + { + Drop.m_ItemCount--; + } + break; } - m_World->BroadcastSoundParticleEffect(2000, m_PosX * 8, m_PosY * 8, m_PosZ * 8, SmokeDir); - m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.0f); - cWindow * Window = GetWindow(); - if ( Window != NULL ) + + default: { - Window->BroadcastWholeWindow(); + cItems Pickups; + Pickups.push_back(Drop.CopyOne()); + m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z); + Drop.m_ItemCount--; + break; } + } // switch (ItemType) + + char SmokeDir = 0; + switch (Meta) + { + case 2: SmokeDir = 1; break; + case 3: SmokeDir = 7; break; + case 4: SmokeDir = 3; break; + case 5: SmokeDir = 5; break; } - else + m_World->BroadcastSoundParticleEffect(2000, m_PosX * 8, m_PosY * 8, m_PosZ * 8, SmokeDir); + m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.0f); + cWindow * Window = GetWindow(); + if (Window != NULL) { - m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.2f); + Window->BroadcastWholeWindow(); } } @@ -224,20 +222,20 @@ void cDispenserEntity::UsedBy(cPlayer * a_Player) -void cDispenserEntity::Activate() +void cDispenserEntity::Activate(void) { - m_CanDispense = 1; + m_ShouldDispense = true; } -bool cDispenserEntity::Tick( float a_Dt ) +bool cDispenserEntity::Tick(float a_Dt) { - if(m_CanDispense) + if (m_ShouldDispense) { - m_CanDispense = 0; + m_ShouldDispense = false; Dispense(); } return false; @@ -247,21 +245,7 @@ bool cDispenserEntity::Tick( float a_Dt ) -void cDispenserEntity::SetSlot(int a_Slot, const cItem & a_Item) -{ - if ((a_Slot < 0) || (a_Slot >= 9)) - { - ASSERT(!"Dispenser: slot number out of range"); - return; - } - m_Items[a_Slot] = a_Item; -} - - - - - -bool cDispenserEntity::LoadFromJson( const Json::Value& a_Value ) +bool cDispenserEntity::LoadFromJson(const Json::Value & a_Value) { m_PosX = a_Value.get("x", 0).asInt(); m_PosY = a_Value.get("y", 0).asInt(); @@ -269,9 +253,9 @@ bool cDispenserEntity::LoadFromJson( const Json::Value& a_Value ) Json::Value AllSlots = a_Value.get("Slots", 0); int SlotIdx = 0; - for( Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr ) + for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr) { - m_Items[ SlotIdx ].FromJson( *itr ); + m_Contents.GetSlot(SlotIdx).FromJson(*itr); SlotIdx++; } @@ -282,18 +266,19 @@ bool cDispenserEntity::LoadFromJson( const Json::Value& a_Value ) -void cDispenserEntity::SaveToJson( Json::Value& a_Value ) +void cDispenserEntity::SaveToJson(Json::Value & a_Value) { a_Value["x"] = m_PosX; a_Value["y"] = m_PosY; a_Value["z"] = m_PosZ; Json::Value AllSlots; - for(unsigned int i = 0; i < 3; i++) + int NumSlots = m_Contents.GetNumSlots(); + for (int i = 0; i < NumSlots; i++) { Json::Value Slot; - m_Items[ i ].GetJson( Slot ); - AllSlots.append( Slot ); + m_Contents.GetSlot(i).GetJson(Slot); + AllSlots.append(Slot); } a_Value["Slots"] = AllSlots; } diff --git a/source/DispenserEntity.h b/source/DispenserEntity.h index 9d27c59c4..ad755c7db 100644 --- a/source/DispenserEntity.h +++ b/source/DispenserEntity.h @@ -3,7 +3,7 @@ #include "BlockEntity.h" #include "UI/WindowOwner.h" -#include "Item.h" +#include "ItemGrid.h" @@ -28,7 +28,7 @@ class cDispenserEntity : public: cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World); virtual ~cDispenserEntity(); - virtual void Destroy(); + virtual void Destroy(void); bool LoadFromJson(const Json::Value & a_Value); @@ -38,17 +38,21 @@ public: virtual bool Tick(float a_Dt) override; virtual void UsedBy(cPlayer * a_Player) override; - const cItem * GetSlot(int i) const { return &(m_Items[i]); } + const cItem & GetSlot(int a_SlotNum) const { return m_Contents.GetSlot(a_SlotNum); } - void SetSlot(int a_Slot, const cItem & a_Item); + void SetSlot(int a_SlotNum, const cItem & a_Item) { m_Contents.SetSlot(a_SlotNum, a_Item); } - void Activate(); + /// Sets the dispenser to dispense an item in the next tick + void Activate(void); + + const cItemGrid & GetContents(void) const { return m_Contents; } + cItemGrid & GetContents(void) { return m_Contents; } private: - cItem * m_Items; - bool m_CanDispense; + cItemGrid m_Contents; + bool m_ShouldDispense; ///< If true, the dispenser will dispense an item in the next tick - void Dispense(); + void Dispense(void); }; diff --git a/source/Item.cpp b/source/Item.cpp index b96500a30..15f011c13 100644 --- a/source/Item.cpp +++ b/source/Item.cpp @@ -8,6 +8,17 @@ +cItem cItem::CopyOne(void) const +{ + cItem res(*this); + res.m_ItemCount = 1; + return res; +} + + + + + bool cItem::IsStackableWith(const cItem & a_OtherStack) { if (a_OtherStack.m_ItemType != m_ItemType) diff --git a/source/Item.h b/source/Item.h index 194b6bff6..d2cbd6053 100644 --- a/source/Item.h +++ b/source/Item.h @@ -27,13 +27,15 @@ public: } } - void Empty() + + void Empty(void) { m_ItemType = E_ITEM_EMPTY; m_ItemCount = 0; m_ItemDamage = 0; } + void Clear(void) { m_ItemType = E_ITEM_EMPTY; @@ -41,21 +43,28 @@ public: m_ItemDamage = 0; } + bool IsEmpty(void) const { - return (m_ItemType <= 0 || m_ItemCount <= 0); + return ((m_ItemType <= 0) || (m_ItemCount <= 0)); } + bool IsEqual(const cItem & a_Item) const { return (IsSameType(a_Item) && (m_ItemDamage == a_Item.m_ItemDamage)); } + bool IsSameType(const cItem & a_Item) const { return (m_ItemType == a_Item.m_ItemType) || (IsEmpty() && a_Item.IsEmpty()); } + + /// Returns a copy of this item with m_ItemCount set to 1. Useful to preserve enchantments etc. on stacked items + cItem CopyOne(void) const; + // TODO Sorry for writing the functions in the header. But somehow it doesn´t worked when I put them into the cpp File :s inline int GetMaxDuration(void) const diff --git a/source/UI/SlotArea.cpp b/source/UI/SlotArea.cpp index dd68f029d..00698acda 100644 --- a/source/UI/SlotArea.cpp +++ b/source/UI/SlotArea.cpp @@ -493,7 +493,7 @@ void cSlotAreaDispenser::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRig const cItem * cSlotAreaDispenser::GetSlot(int a_SlotNum, cPlayer & a_Player) { - return m_Dispenser->GetSlot(a_SlotNum); + return &(m_Dispenser->GetSlot(a_SlotNum)); } diff --git a/source/WorldStorage/NBTChunkSerializer.cpp b/source/WorldStorage/NBTChunkSerializer.cpp index cf53e4434..02b1012d8 100644 --- a/source/WorldStorage/NBTChunkSerializer.cpp +++ b/source/WorldStorage/NBTChunkSerializer.cpp @@ -123,15 +123,7 @@ void cNBTChunkSerializer::AddDispenserEntity(cDispenserEntity * a_Entity) m_Writer.BeginCompound(""); AddBasicTileEntity(a_Entity, "Trap"); m_Writer.BeginList("Items", TAG_Compound); - for (int i = 0; i < 9; i++) - { - const cItem * Item = a_Entity->GetSlot(i); - if ((Item == NULL) || Item->IsEmpty()) - { - continue; - } - AddItem(*Item, i); - } // for i - contents[] + AddItemGrid(a_Entity->GetContents()); m_Writer.EndList(); m_Writer.EndCompound(); } -- cgit v1.2.3