From d47ff5520373acb359590fb036b6a761af58cf75 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Mon, 18 Feb 2013 16:48:50 +0000 Subject: Minecarts can now be placed. No interaction, no physics, though. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1219 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- VC2008/MCServer.vcproj | 12 ++++++ source/ClientHandle.cpp | 9 ++++ source/ClientHandle.h | 1 + source/Entity.h | 1 + source/Items/ItemHandler.cpp | 40 ++++++++++------- source/Items/ItemMinecart.h | 79 ++++++++++++++++++++++++++++++++++ source/Minecart.cpp | 62 ++++++++++++++++++++++++++ source/Minecart.h | 46 ++++++++++++++++++++ source/Protocol/Protocol.h | 1 + source/Protocol/Protocol125.cpp | 20 +++++++++ source/Protocol/Protocol125.h | 1 + source/Protocol/Protocol14x.cpp | 22 ++++++++++ source/Protocol/Protocol14x.h | 1 + source/Protocol/ProtocolRecognizer.cpp | 10 +++++ source/Protocol/ProtocolRecognizer.h | 1 + 15 files changed, 290 insertions(+), 16 deletions(-) create mode 100644 source/Items/ItemMinecart.h create mode 100644 source/Minecart.cpp create mode 100644 source/Minecart.h diff --git a/VC2008/MCServer.vcproj b/VC2008/MCServer.vcproj index 3bc8e6808..8dc8afc00 100644 --- a/VC2008/MCServer.vcproj +++ b/VC2008/MCServer.vcproj @@ -974,6 +974,14 @@ RelativePath="..\source\Ladder.h" > + + + + @@ -2050,6 +2058,10 @@ RelativePath="..\source\items\ItemLighter.h" > + + diff --git a/source/ClientHandle.cpp b/source/ClientHandle.cpp index 0f9958e98..e5c1f5786 100644 --- a/source/ClientHandle.cpp +++ b/source/ClientHandle.cpp @@ -1703,6 +1703,15 @@ void cClientHandle::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, +void cClientHandle::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType) +{ + m_Protocol->SendSpawnVehicle(a_Vehicle, a_VehicleType); +} + + + + + void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) { // Check chunks being sent, erase them from m_ChunksToSend: diff --git a/source/ClientHandle.h b/source/ClientHandle.h index a413c6670..19466f145 100644 --- a/source/ClientHandle.h +++ b/source/ClientHandle.h @@ -114,6 +114,7 @@ public: void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock); void SendSpawnMob (const cMonster & a_Mob); void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, short a_SpeedX, short a_SpeedY, short a_SpeedZ, Byte a_Yaw, Byte a_Pitch); + void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType); void SendTeleportEntity (const cEntity & a_Entity); void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ); void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay); diff --git a/source/Entity.h b/source/Entity.h index 2ed255cd0..804e24490 100644 --- a/source/Entity.h +++ b/source/Entity.h @@ -64,6 +64,7 @@ public: etPickup, etMob, etFallingBlock, + etMinecart, // Older constants, left over for compatibility reasons (plugins) eEntityType_Entity = etEntity, diff --git a/source/Items/ItemHandler.cpp b/source/Items/ItemHandler.cpp index 6cb98706b..2b2d9268b 100644 --- a/source/Items/ItemHandler.cpp +++ b/source/Items/ItemHandler.cpp @@ -5,32 +5,33 @@ #include "../World.h" #include "../Player.h" -//Handler +// Handlers: +#include "ItemBed.h" +#include "ItemBrewingStand.h" +#include "ItemBucket.h" +#include "ItemCauldron.h" #include "ItemCloth.h" +#include "ItemDoor.h" +#include "ItemDye.h" +#include "ItemFlowerPot.h" +#include "ItemFood.h" #include "ItemHoe.h" -#include "ItemSlab.h" -#include "ItemWood.h" -#include "ItemShears.h" #include "ItemLeaves.h" -#include "ItemSapling.h" -#include "ItemBucket.h" #include "ItemLighter.h" +#include "ItemMinecart.h" +#include "ItemPickaxe.h" #include "ItemRedstoneDust.h" #include "ItemRedstoneRepeater.h" +#include "ItemSapling.h" #include "ItemSeeds.h" -#include "ItemDye.h" -#include "ItemSugarcane.h" -#include "ItemPickaxe.h" +#include "ItemShears.h" #include "ItemShovel.h" -#include "ItemSword.h" -#include "ItemDoor.h" -#include "ItemFood.h" #include "ItemSign.h" -#include "ItemBed.h" +#include "ItemSlab.h" #include "ItemSpawnEgg.h" -#include "ItemFlowerPot.h" -#include "ItemBrewingStand.h" -#include "ItemCauldron.h" +#include "ItemSugarcane.h" +#include "ItemSword.h" +#include "ItemWood.h" #include "../Blocks/BlockHandler.h" @@ -155,6 +156,13 @@ cItemHandler *cItemHandler::CreateItemHandler(int a_ItemType) return new cItemDoorHandler(a_ItemType); } + case E_ITEM_MINECART: + case E_ITEM_CHEST_MINECART: + case E_ITEM_FURNACE_MINECART: + { + return new cItemMinecartHandler(a_ItemType); + } + // Food: case E_ITEM_BREAD: case E_ITEM_COOKIE: diff --git a/source/Items/ItemMinecart.h b/source/Items/ItemMinecart.h new file mode 100644 index 000000000..5a2d78774 --- /dev/null +++ b/source/Items/ItemMinecart.h @@ -0,0 +1,79 @@ + +// ItemMinecart.h + +// Declares the various minecart ItemHandlers + + + + + +#pragma once + +// Not needed, we're being included only from ItemHandler.cpp which already has this file: #include "ItemHandler.h" +#include "../Minecart.h" + + + + + +class cItemMinecartHandler : + public cItemHandler +{ + typedef cItemHandler super; + +public: + cItemMinecartHandler(int a_ItemType) : + super(a_ItemType) + { + } + + + + virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, cItem * a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Dir) override + { + if (a_Dir < 0) + { + return false; + } + + // Check that there's rail in there: + BLOCKTYPE Block = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ); + switch (Block) + { + case E_BLOCK_MINECART_TRACKS: + case E_BLOCK_POWERED_RAIL: + case E_BLOCK_DETECTOR_RAIL: + { + // These are allowed + break; + } + default: + { + LOGD("Used minecart on an unsuitable block %d (%s)", Block, ItemTypeToString(Block).c_str()); + return false; + } + } + + cMinecart::ePayload Payload = cMinecart::mpNone; + switch (m_ItemType) + { + case E_ITEM_MINECART: Payload = cMinecart::mpNone; break; + case E_ITEM_CHEST_MINECART: Payload = cMinecart::mpChest; break; + case E_ITEM_FURNACE_MINECART: Payload = cMinecart::mpFurnace; break; + default: + { + ASSERT(!"Unhandled minecart item"); + return false; + } + } // switch (m_ItemType) + cMinecart * Minecart = new cMinecart(Payload, (double)a_BlockX + 0.5, a_BlockY, (double)a_BlockZ + 0.5); + a_World->AddEntity(Minecart); + Minecart->Initialize(a_World); + return true; + } + +} ; + + + + diff --git a/source/Minecart.cpp b/source/Minecart.cpp new file mode 100644 index 000000000..10c9fe580 --- /dev/null +++ b/source/Minecart.cpp @@ -0,0 +1,62 @@ + +// Minecart.cpp + +// Implements the cMinecart class representing a minecart in the world + +#include "Globals.h" +#include "Minecart.h" +#include "World.h" +#include "ClientHandle.h" + + + + + +cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) : + super(etMinecart, a_X, a_Y, a_Z), + m_Payload(a_Payload) +{ +} + + + + +void cMinecart::Initialize(cWorld * a_World) +{ + super::Initialize(a_World); + a_World->BroadcastSpawn(*this); +} + + + + + +void cMinecart::SpawnOn(cClientHandle & a_ClientHandle) +{ + char Type = 0; + switch (m_Payload) + { + case mpNone: Type = 10; break; + case mpChest: Type = 11; break; + case mpFurnace: Type = 12; break; + default: + { + ASSERT(!"Unknown payload, cannot spawn on client"); + return; + } + } + a_ClientHandle.SendSpawnVehicle(*this, Type); +} + + + + + +void cMinecart::Tick(float a_Dt, MTRand & a_TickRandom) +{ + // TODO: the physics +} + + + + diff --git a/source/Minecart.h b/source/Minecart.h new file mode 100644 index 000000000..72270df85 --- /dev/null +++ b/source/Minecart.h @@ -0,0 +1,46 @@ + +// Minecart.h + +// Declares the cMinecart class representing a minecart in the world + + + + + +#pragma once + +#include "Entity.h" + + + + + +class cMinecart : + public cEntity +{ + typedef cEntity super; + +public: + enum ePayload + { + mpNone, // Empty minecart, ridable by player or mobs + mpChest, // Minecart-with-chest, can store a grid of 3*8 items + mpFurnace, // Minecart-with-furnace, can be powered + // TODO: Other 1.5 features: hopper, tnt, dispenser, spawner + } ; + cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z); + + // cEntity overrides: + virtual void Initialize(cWorld * a_World) override; + virtual void SpawnOn(cClientHandle & a_ClientHandle) override; + virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + + ePayload GetPayload(void) const { return m_Payload; } + +protected: + ePayload m_Payload; +} ; + + + + diff --git a/source/Protocol/Protocol.h b/source/Protocol/Protocol.h index 52f3f2e3f..ab35adbb5 100644 --- a/source/Protocol/Protocol.h +++ b/source/Protocol/Protocol.h @@ -84,6 +84,7 @@ public: virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) = 0; virtual void SendSpawnMob (const cMonster & a_Mob) = 0; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, short a_SpeedX, short a_SpeedY, short a_SpeedZ, Byte a_Yaw, Byte a_Pitch) = 0; + virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) = 0; virtual void SendTeleportEntity (const cEntity & a_Entity) = 0; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) = 0; virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) = 0; diff --git a/source/Protocol/Protocol125.cpp b/source/Protocol/Protocol125.cpp index 0bed151ed..d45ea8837 100644 --- a/source/Protocol/Protocol125.cpp +++ b/source/Protocol/Protocol125.cpp @@ -670,6 +670,26 @@ void cProtocol125::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, +void cProtocol125::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType) +{ + cCSLock Lock(m_CSPacket); + WriteByte (PACKET_SPAWN_OBJECT); + WriteInt (a_Vehicle.GetUniqueID()); + WriteByte (a_VehicleType); + WriteInt ((int)(a_Vehicle.GetPosX() * 32)); + WriteInt ((int)(a_Vehicle.GetPosY() * 32)); + WriteInt ((int)(a_Vehicle.GetPosZ() * 32)); + WriteInt (1); + WriteShort(0); // TODO: SpeedX + WriteShort(0); // TODO: SpeedY + WriteShort(0); // TODO: SpeedZ + Flush(); +} + + + + + void cProtocol125::SendTeleportEntity(const cEntity & a_Entity) { cCSLock Lock(m_CSPacket); diff --git a/source/Protocol/Protocol125.h b/source/Protocol/Protocol125.h index ec0d976e5..47923235b 100644 --- a/source/Protocol/Protocol125.h +++ b/source/Protocol/Protocol125.h @@ -61,6 +61,7 @@ public: virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; virtual void SendSpawnMob (const cMonster & a_Mob) override; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, short a_SpeedX, short a_SpeedY, short a_SpeedZ, Byte a_Yaw, Byte a_Pitch) override; + virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) override; virtual void SendTeleportEntity (const cEntity & a_Entity) override; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; diff --git a/source/Protocol/Protocol14x.cpp b/source/Protocol/Protocol14x.cpp index 912a1a266..b70d6e0aa 100644 --- a/source/Protocol/Protocol14x.cpp +++ b/source/Protocol/Protocol14x.cpp @@ -235,3 +235,25 @@ void cProtocol146::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, +void cProtocol146::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType) +{ + cCSLock Lock(m_CSPacket); + WriteByte(PACKET_SPAWN_OBJECT); + WriteInt (a_Vehicle.GetUniqueID()); + WriteByte(a_VehicleType); + WriteInt ((int)(a_Vehicle.GetPosX() * 32)); + WriteInt ((int)(a_Vehicle.GetPosY() * 32)); + WriteInt ((int)(a_Vehicle.GetPosZ() * 32)); + WriteInt (1); + WriteShort(0); // TODO: SpeedX + WriteShort(0); // TODO: SpeedY + WriteShort(0); // TODO: SpeedZ + WriteByte (0); // TODO: Yaw + WriteByte (0); // TODO: Pitch + Flush(); +} + + + + + diff --git a/source/Protocol/Protocol14x.h b/source/Protocol/Protocol14x.h index 02888e61f..383544f8f 100644 --- a/source/Protocol/Protocol14x.h +++ b/source/Protocol/Protocol14x.h @@ -55,6 +55,7 @@ public: virtual void SendPickupSpawn (const cPickup & a_Pickup) override; virtual void SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) override; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, short a_SpeedX, short a_SpeedY, short a_SpeedZ, Byte a_Yaw, Byte a_Pitch) override; + virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) override; } ; diff --git a/source/Protocol/ProtocolRecognizer.cpp b/source/Protocol/ProtocolRecognizer.cpp index 4ab163783..0d26cdbd3 100644 --- a/source/Protocol/ProtocolRecognizer.cpp +++ b/source/Protocol/ProtocolRecognizer.cpp @@ -436,6 +436,16 @@ void cProtocolRecognizer::SendSpawnObject(const cEntity & a_Entity, char a_Objec +void cProtocolRecognizer::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType) +{ + ASSERT(m_Protocol != NULL); + m_Protocol->SendSpawnVehicle(a_Vehicle, a_VehicleType); +} + + + + + void cProtocolRecognizer::SendTeleportEntity(const cEntity & a_Entity) { ASSERT(m_Protocol != NULL); diff --git a/source/Protocol/ProtocolRecognizer.h b/source/Protocol/ProtocolRecognizer.h index 53c8fa6e4..f638b08d8 100644 --- a/source/Protocol/ProtocolRecognizer.h +++ b/source/Protocol/ProtocolRecognizer.h @@ -86,6 +86,7 @@ public: virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override; virtual void SendSpawnMob (const cMonster & a_Mob) override; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, short a_SpeedX, short a_SpeedY, short a_SpeedZ, Byte a_Yaw, Byte a_Pitch) override; + virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) override; virtual void SendTeleportEntity (const cEntity & a_Entity) override; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override; -- cgit v1.2.3