From c789a8ddf5840cf7861c73536279da8bbd9281c3 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 8 Sep 2013 00:14:57 +0100 Subject: [PATCH] Initial boat support + Boats are saved + Boats have physics + Boats spawn --- source/Entities/Boat.cpp | 84 ++++++++++++++++++++++ source/Entities/Boat.h | 34 +++++++++ source/Items/ItemBoat.h | 54 ++++++++++++++ source/Items/ItemHandler.cpp | 6 ++ source/WorldStorage/NBTChunkSerializer.cpp | 16 +++++ source/WorldStorage/NBTChunkSerializer.h | 2 + source/WorldStorage/WSSAnvil.cpp | 21 +++++- source/WorldStorage/WSSAnvil.h | 1 + 8 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 source/Entities/Boat.cpp create mode 100644 source/Entities/Boat.h create mode 100644 source/Items/ItemBoat.h diff --git a/source/Entities/Boat.cpp b/source/Entities/Boat.cpp new file mode 100644 index 000000000..f29766fc1 --- /dev/null +++ b/source/Entities/Boat.cpp @@ -0,0 +1,84 @@ + +// Minecart.cpp + +// Implements the cMinecart class representing a minecart in the world +// Indiana Jones! + +#include "Globals.h" +#include "Boat.h" +#include "../World.h" +#include "../ClientHandle.h" +#include "Player.h" + + + + + +cBoat::cBoat(double a_X, double a_Y, double a_Z) : + super(etBoat, a_X, a_Y, a_Z, 0.98, 0.7) +{ + SetMass(20.f); + SetMaxHealth(6); + SetHealth(6); +} + + + + +void cBoat::SpawnOn(cClientHandle & a_ClientHandle) +{ + a_ClientHandle.SendSpawnVehicle(*this, 1, 0); +} + + + + + +void cBoat::DoTakeDamage(TakeDamageInfo & TDI) +{ + super::DoTakeDamage(TDI); + + if (GetHealth() == 0) + { + Destroy(true); + } +} + + + + + +void cBoat::OnRightClicked(cPlayer & a_Player) +{ + if (m_Attachee != NULL) + { + if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID()) + { + // This player is already sitting in, they want out. + a_Player.Detach(); + return; + } + + if (m_Attachee->IsPlayer()) + { + // Another player is already sitting in here, cannot attach + return; + } + + // Detach whatever is sitting in this minecart now: + m_Attachee->Detach(); + } + + // Attach the player to this minecart + a_Player.AttachTo(this); +} + + + + + +void cBoat::HandlePhysics(float a_Dt, cChunk & a_Chunk) +{ + super::HandlePhysics(a_Dt, a_Chunk); + BroadcastMovementUpdate(); +} diff --git a/source/Entities/Boat.h b/source/Entities/Boat.h new file mode 100644 index 000000000..439b73a09 --- /dev/null +++ b/source/Entities/Boat.h @@ -0,0 +1,34 @@ + +// Boat.h + +// Declares the cBoat class representing a minecart in the world + + + + + +#pragma once + +#include "Entity.h" +#include "../Item.h" + + + + + +class cBoat : + public cEntity +{ + typedef cEntity super; + +public: + CLASS_PROTODEF(cBoat); + + // cEntity overrides: + virtual void SpawnOn(cClientHandle & a_ClientHandle) override; + virtual void OnRightClicked(cPlayer & a_Player) override; + virtual void DoTakeDamage(TakeDamageInfo & TDI) override; + virtual void HandlePhysics(float a_Dt, cChunk & a_Chunk) override; + + cBoat(double a_X, double a_Y, double a_Z); +} ; \ No newline at end of file diff --git a/source/Items/ItemBoat.h b/source/Items/ItemBoat.h new file mode 100644 index 000000000..6e3395f1d --- /dev/null +++ b/source/Items/ItemBoat.h @@ -0,0 +1,54 @@ + +// ItemBoat.h + +// Declares the various boat ItemHandlers + + + + + +#pragma once + +#include "../Entities/Boat.h" + + + + + +class cItemBoatHandler : + public cItemHandler +{ + typedef cItemHandler super; + +public: + cItemBoatHandler(int a_ItemType) : + super(a_ItemType) + { + } + + + + virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Dir) override + { + if (a_Dir < 0) + { + return false; + } + + double x = (double)a_BlockX + 0.5; + double y = (double)a_BlockY + 0.5; + double z = (double)a_BlockZ + 0.5; + + cBoat * Boat = NULL; + + Boat = new cBoat (x, y, z); + Boat->Initialize(a_World); + + return true; + } + +} ; + + + + diff --git a/source/Items/ItemHandler.cpp b/source/Items/ItemHandler.cpp index 2ae193d52..374d0ebf5 100644 --- a/source/Items/ItemHandler.cpp +++ b/source/Items/ItemHandler.cpp @@ -8,6 +8,7 @@ // Handlers: #include "ItemBed.h" +#include "ItemBoat.h" #include "ItemBow.h" #include "ItemBrewingStand.h" #include "ItemBucket.h" @@ -180,6 +181,11 @@ cItemHandler *cItemHandler::CreateItemHandler(int a_ItemType) return new cItemMinecartHandler(a_ItemType); } + case E_ITEM_BOAT: + { + return new cItemBoatHandler(a_ItemType); + } + // Food: case E_ITEM_BREAD: case E_ITEM_COOKIE: diff --git a/source/WorldStorage/NBTChunkSerializer.cpp b/source/WorldStorage/NBTChunkSerializer.cpp index baae0dc01..8b2d969f8 100644 --- a/source/WorldStorage/NBTChunkSerializer.cpp +++ b/source/WorldStorage/NBTChunkSerializer.cpp @@ -19,6 +19,7 @@ #include "../OSSupport/MakeDir.h" #include "FastNBT.h" #include "../Entities/FallingBlock.h" +#include "../Entities/Boat.h" #include "../Entities/Minecart.h" #include "../Mobs/Monster.h" #include "../Entities/Pickup.h" @@ -252,6 +253,20 @@ void cNBTChunkSerializer::AddBasicEntity(cEntity * a_Entity, const AString & a_C +void cNBTChunkSerializer::AddBoatEntity(cBoat * a_Boat) +{ + const char * EntityClass = NULL; + EntityClass = "Boat"; + + m_Writer.BeginCompound(""); + AddBasicEntity(a_Boat, EntityClass); + m_Writer.EndCompound(); +} + + + + + void cNBTChunkSerializer::AddFallingBlockEntity(cFallingBlock * a_FallingBlock) { m_Writer.BeginCompound(""); @@ -461,6 +476,7 @@ void cNBTChunkSerializer::Entity(cEntity * a_Entity) switch (a_Entity->GetEntityType()) { + case cEntity::etBoat: AddBoatEntity ((cBoat *) a_Entity); break; case cEntity::etFallingBlock: AddFallingBlockEntity((cFallingBlock *) a_Entity); break; case cEntity::etMinecart: AddMinecartEntity ((cMinecart *) a_Entity); break; case cEntity::etMonster: AddMonsterEntity ((cMonster *) a_Entity); break; diff --git a/source/WorldStorage/NBTChunkSerializer.h b/source/WorldStorage/NBTChunkSerializer.h index 481c578f3..9d4ac208c 100644 --- a/source/WorldStorage/NBTChunkSerializer.h +++ b/source/WorldStorage/NBTChunkSerializer.h @@ -19,6 +19,7 @@ class cFastNBTWriter; class cEntity; class cBlockEntity; +class cBoat; class cChestEntity; class cDispenserEntity; class cDropperEntity; @@ -94,6 +95,7 @@ protected: // Entities: void AddBasicEntity (cEntity * a_Entity, const AString & a_ClassName); + void AddBoatEntity (cBoat * a_Boat); void AddFallingBlockEntity(cFallingBlock * a_FallingBlock); void AddMinecartEntity (cMinecart * a_Minecart); void AddMonsterEntity (cMonster * a_Monster); diff --git a/source/WorldStorage/WSSAnvil.cpp b/source/WorldStorage/WSSAnvil.cpp index 3ab64148e..3d69fe5ef 100644 --- a/source/WorldStorage/WSSAnvil.cpp +++ b/source/WorldStorage/WSSAnvil.cpp @@ -23,6 +23,7 @@ #include "../OSSupport/MakeDir.h" #include "FastNBT.h" #include "../Mobs/Monster.h" +#include "../Entities/Boat.h" #include "../Entities/FallingBlock.h" #include "../Entities/Minecart.h" #include "../Entities/Pickup.h" @@ -911,7 +912,11 @@ void cWSSAnvil::LoadSignFromNBT(cBlockEntityList & a_BlockEntities, const cParse void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength) { - if (strncmp(a_IDTag, "FallingBlock", a_IDTagLength) == 0) + if (strncmp(a_IDTag, "Boat", a_IDTagLength) == 0) + { + LoadBoatFromNBT(a_Entities, a_NBT, a_EntityTagIdx); + } + else if (strncmp(a_IDTag, "FallingBlock", a_IDTagLength) == 0) { LoadFallingBlockFromNBT(a_Entities, a_NBT, a_EntityTagIdx); } @@ -967,6 +972,20 @@ void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a +void cWSSAnvil::LoadBoatFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx) +{ + std::auto_ptr Boat(new cBoat(0, 0, 0)); + if (!LoadEntityBaseFromNBT(*Boat.get(), a_NBT, a_TagIdx)) + { + return; + } + a_Entities.push_back(Boat.release()); +} + + + + + void cWSSAnvil::LoadFallingBlockFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx) { // TODO diff --git a/source/WorldStorage/WSSAnvil.h b/source/WorldStorage/WSSAnvil.h index b2556ab50..b364e18fa 100644 --- a/source/WorldStorage/WSSAnvil.h +++ b/source/WorldStorage/WSSAnvil.h @@ -138,6 +138,7 @@ protected: void LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength); + void LoadBoatFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIndx); void LoadFallingBlockFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadMinecartRFromNBT (cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadMinecartCFromNBT (cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx);