1
0

Implementation of in-game maps

This commit is contained in:
andrew 2014-02-13 17:13:09 +02:00
parent cc72b656c9
commit 92e85cc960
13 changed files with 596 additions and 0 deletions

View File

@ -2057,6 +2057,24 @@ void cClientHandle::SendInventorySlot(char a_WindowID, short a_SlotNum, const cI
void cClientHandle::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length)
{
m_Protocol->SendMapColumn(a_ID, a_X, a_Y, a_Colors, a_Length);
}
void cClientHandle::SendMapInfo(int a_ID, unsigned int a_Scale)
{
m_Protocol->SendMapInfo(a_ID, a_Scale);
}
void cClientHandle::SendParticleEffect(const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount)
{
m_Protocol->SendParticleEffect(a_ParticleName, a_SrcX, a_SrcY, a_SrcZ, a_OffsetX, a_OffsetY, a_OffsetZ, a_ParticleData, a_ParticleAmmount);

View File

@ -109,6 +109,8 @@ public:
void SendGameMode (eGameMode a_GameMode);
void SendHealth (void);
void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item);
void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override;
void SendMapInfo (int a_ID, unsigned int a_Scale) override;
void SendPickupSpawn (const cPickup & a_Pickup);
void SendEntityAnimation (const cEntity & a_Entity, char a_Animation);
void SendParticleEffect (const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount);

149
src/Map.cpp Normal file
View File

@ -0,0 +1,149 @@
// Map.cpp
#include "Globals.h"
#include "Map.h"
#include "ClientHandle.h"
#include "World.h"
cMap::cMap(unsigned int a_ID, int a_CenterX, int a_CenterZ, cWorld * a_World, unsigned int a_Scale)
: m_ID(a_ID)
, m_Width(128)
, m_Height(128)
, m_Scale(a_Scale)
, m_CenterX(a_CenterX)
, m_CenterZ(a_CenterZ)
, m_World(a_World)
{
m_Data.assign(m_Width * m_Height, 0);
UpdateMap();
}
void cMap::UpdateMap(void)
{
// ASSERT(m_World != NULL);
// TODO
for (unsigned int X = 0; X < m_Width; ++X)
{
for (unsigned int Y = 0; Y < m_Height; ++Y)
{
// Debug
m_Data[Y + X * m_Height] = rand() % 100;
}
}
}
eDimension cMap::GetDimension(void) const
{
ASSERT(m_World != NULL);
return m_World->GetDimension();
}
void cMap::Resize(unsigned int a_Width, unsigned int a_Height)
{
if ((m_Width == a_Width) && (m_Height == a_Height))
{
return;
}
m_Width = a_Width;
m_Height = a_Height;
m_Data.assign(m_Width * m_Height, 0);
UpdateMap();
}
void cMap::SetPosition(int a_CenterX, int a_CenterZ)
{
if ((m_CenterX == a_CenterX) && (m_CenterZ == a_CenterZ))
{
return;
}
m_CenterX = a_CenterX;
m_CenterZ = a_CenterZ;
UpdateMap();
}
void cMap::SetScale(unsigned int a_Scale)
{
if (m_Scale == a_Scale)
{
return;
}
m_Scale = a_Scale;
UpdateMap();
}
void cMap::SendTo(cClientHandle & a_Client)
{
a_Client.SendMapInfo(m_ID, m_Scale);
for (unsigned int i = 0; i < m_Width; ++i)
{
const Byte* Colors = &m_Data[i * m_Height];
a_Client.SendMapColumn(m_ID, i, 0, Colors, m_Height);
}
}
unsigned int cMap::GetNumPixels(void) const
{
return m_Width * m_Height;
}
unsigned int cMap::GetNumBlocksPerPixel(void) const
{
return pow(2, m_Scale);
}

95
src/Map.h Normal file
View File

@ -0,0 +1,95 @@
// Map.h
// Implementation of in-game coloured maps
#pragma once
#include "BlockID.h"
class cClientHandle;
class cWorld;
class cMap
{
public:
typedef Byte ColorID;
typedef std::vector<ColorID> cColorList;
public:
cMap(unsigned int a_ID, int a_CenterX, int a_CenterZ, cWorld * a_World, unsigned int a_Scale = 3);
/** Update the map (Query the world) */
void UpdateMap(void);
/** Send this map to the specified client. */
void SendTo(cClientHandle & a_Client);
void Resize(unsigned int a_Width, unsigned int a_Height);
void SetPosition(int a_CenterX, int a_CenterZ);
void SetScale(unsigned int a_Scale);
unsigned int GetWidth (void) const { return m_Width; }
unsigned int GetHeight(void) const { return m_Height; }
unsigned int GetScale(void) const { return m_Scale; }
int GetCenterX(void) const { return m_CenterX; }
int GetCenterZ(void) const { return m_CenterZ; }
unsigned int GetID(void) const { return m_ID; }
cWorld * GetWorld(void) { return m_World; }
eDimension GetDimension(void) const;
const cColorList & GetData(void) const { return m_Data; }
unsigned int GetNumPixels(void) const;
unsigned int GetNumBlocksPerPixel(void) const;
private:
unsigned int m_ID;
unsigned int m_Width;
unsigned int m_Height;
/** The zoom level, 2^scale square blocks per pixel */
unsigned int m_Scale;
int m_CenterX;
int m_CenterZ;
/** Column-major array of colours */
cColorList m_Data;
cWorld * m_World;
friend class cMapSerializer;
};

View File

@ -28,6 +28,7 @@ class cWorld;
class cMonster;
class cChunkDataSerializer;
class cFallingBlock;
class cMap;
@ -79,6 +80,8 @@ public:
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) = 0;
virtual void SendKeepAlive (int a_PingID) = 0;
virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) = 0;
virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) = 0;
virtual void SendMapInfo (int a_ID, unsigned int a_Scale) = 0;
virtual void SendPickupSpawn (const cPickup & a_Pickup) = 0;
virtual void SendPlayerAbilities (void) = 0;
virtual void SendEntityAnimation (const cEntity & a_Entity, char a_Animation) = 0;

View File

@ -95,6 +95,7 @@ enum
PACKET_WINDOW_PROPERTY = 0x69,
PACKET_CREATIVE_INVENTORY_ACTION = 0x6B,
PACKET_UPDATE_SIGN = 0x82,
PACKET_ITEM_DATA = 0x83,
PACKET_PLAYER_LIST_ITEM = 0xC9,
PACKET_PLAYER_ABILITIES = 0xca,
PACKET_PLUGIN_MESSAGE = 0xfa,
@ -576,6 +577,32 @@ void cProtocol125::SendLogin(const cPlayer & a_Player, const cWorld & a_World)
void cProtocol125::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length)
{
cCSLock Lock(m_CSPacket);
WriteByte (PACKET_ITEM_DATA);
WriteShort(E_ITEM_MAP);
WriteShort(a_ID);
WriteShort(3 + a_Length);
WriteByte(0);
WriteByte(a_X);
WriteByte(a_Y);
for (unsigned int i = 0; i < a_Length; ++i)
{
WriteByte(a_Colors[i]);
}
Flush();
}
void cProtocol125::SendPickupSpawn(const cPickup & a_Pickup)
{
cCSLock Lock(m_CSPacket);

View File

@ -54,6 +54,8 @@ public:
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override;
virtual void SendKeepAlive (int a_PingID) override;
virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override;
virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override;
virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override {} // This protocol doesn't support such message
virtual void SendParticleEffect (const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount) override;
virtual void SendPickupSpawn (const cPickup & a_Pickup) override;
virtual void SendPlayerAbilities (void) override {} // This protocol doesn't support such message

View File

@ -496,6 +496,41 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World)
void cProtocol172::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length)
{
cPacketizer Pkt(*this, 0x34);
Pkt.WriteVarInt(a_ID);
Pkt.WriteShort (3 + a_Length);
Pkt.WriteByte(0);
Pkt.WriteByte(a_X);
Pkt.WriteByte(a_Y);
for (unsigned int i = 0; i < a_Length; ++i)
{
Pkt.WriteByte(a_Colors[i]);
}
}
void cProtocol172::SendMapInfo(int a_ID, unsigned int a_Scale)
{
cPacketizer Pkt(*this, 0x34);
Pkt.WriteVarInt(a_ID);
Pkt.WriteShort (2);
Pkt.WriteByte(2);
Pkt.WriteByte(a_Scale);
}
void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup)
{
{

View File

@ -76,6 +76,8 @@ public:
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override;
virtual void SendKeepAlive (int a_PingID) override;
virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override;
virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override;
virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override;
virtual void SendPickupSpawn (const cPickup & a_Pickup) override;
virtual void SendPlayerAbilities (void) override;
virtual void SendEntityAnimation (const cEntity & a_Entity, char a_Animation) override;

View File

@ -386,6 +386,26 @@ void cProtocolRecognizer::SendLogin(const cPlayer & a_Player, const cWorld & a_W
void cProtocolRecognizer::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length)
{
ASSERT(m_Protocol != NULL);
m_Protocol->SendMapColumn(a_ID, a_X, a_Y, a_Colors, a_Length);
}
void cProtocolRecognizer::SendMapInfo(int a_ID, unsigned int a_Scale)
{
ASSERT(m_Protocol != NULL);
m_Protocol->SendMapInfo(a_ID, a_Scale);
}
void cProtocolRecognizer::SendParticleEffect(const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount)
{
ASSERT(m_Protocol != NULL);

View File

@ -89,6 +89,8 @@ public:
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override;
virtual void SendKeepAlive (int a_PingID) override;
virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override;
virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override;
virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override;
virtual void SendParticleEffect (const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount) override;
virtual void SendPickupSpawn (const cPickup & a_Pickup) override;
virtual void SendPlayerAbilities (void) override;

View File

@ -0,0 +1,189 @@
// MapSerializer.cpp
#include "Globals.h"
#include "MapSerializer.h"
#include "../StringCompression.h"
#include "zlib/zlib.h"
#include "FastNBT.h"
#include "../Map.h"
cMapSerializer::cMapSerializer(const AString& a_WorldName, cMap * a_Map)
: m_Map(a_Map)
{
AString DataPath;
Printf(DataPath, "%s/data", a_WorldName.c_str());
Printf(m_Path, "%s/map_%i.dat", DataPath.c_str(), a_Map->GetID());
cFile::CreateFolder(FILE_IO_PREFIX + DataPath);
}
bool cMapSerializer::Load(void)
{
AString Data = cFile::ReadWholeFile(FILE_IO_PREFIX + m_Path);
if (Data.empty())
{
return false;
}
AString Uncompressed;
int res = UncompressStringGZIP(Data.data(), Data.size(), Uncompressed);
if (res != Z_OK)
{
return false;
}
// Parse the NBT data:
cParsedNBT NBT(Uncompressed.data(), Uncompressed.size());
if (!NBT.IsValid())
{
// NBT Parsing failed
return false;
}
return LoadMapFromNBT(NBT);
}
bool cMapSerializer::Save(void)
{
cFastNBTWriter Writer;
SaveMapToNBT(Writer);
Writer.Finish();
#ifdef _DEBUG
cParsedNBT TestParse(Writer.GetResult().data(), Writer.GetResult().size());
ASSERT(TestParse.IsValid());
#endif // _DEBUG
cFile File;
if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmWrite))
{
return false;
}
AString Compressed;
int res = CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed);
if (res != Z_OK)
{
return false;
}
File.Write(Compressed.data(), Compressed.size());
File.Close();
return true;
}
void cMapSerializer::SaveMapToNBT(cFastNBTWriter & a_Writer)
{
a_Writer.BeginCompound("data");
a_Writer.AddByte("scale", m_Map->GetScale());
a_Writer.AddByte("dimension", (int) m_Map->GetDimension());
a_Writer.AddShort("width", m_Map->GetWidth());
a_Writer.AddShort("height", m_Map->GetHeight());
a_Writer.AddInt("xCenter", m_Map->GetCenterX());
a_Writer.AddInt("zCenter", m_Map->GetCenterZ());
// Potential bug - The internal representation may change
const cMap::cColorList & Data = m_Map->GetData();
a_Writer.AddByteArray("colors", (char *) Data.data(), Data.size());
a_Writer.EndCompound();
}
bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT)
{
int Data = a_NBT.FindChildByName(0, "data");
if (Data < 0)
{
return false;
}
int CurrLine = a_NBT.FindChildByName(Data, "scale");
if (CurrLine >= 0)
{
unsigned int Scale = a_NBT.GetByte(CurrLine);
m_Map->m_Scale = Scale;
}
CurrLine = a_NBT.FindChildByName(Data, "dimension");
if (CurrLine >= 0)
{
eDimension Dimension = (eDimension) a_NBT.GetByte(CurrLine);
// ASSERT(Dimension == m_World.GetDimension());
}
CurrLine = a_NBT.FindChildByName(Data, "width");
if (CurrLine >= 0)
{
unsigned int Width = a_NBT.GetShort(CurrLine);
m_Map->m_Width = Width;
}
CurrLine = a_NBT.FindChildByName(Data, "height");
if (CurrLine >= 0)
{
unsigned int Height = a_NBT.GetShort(CurrLine);
m_Map->m_Height = Height;
}
CurrLine = a_NBT.FindChildByName(Data, "xCenter");
if (CurrLine >= 0)
{
int CenterX = a_NBT.GetInt(CurrLine);
m_Map->m_CenterX = CenterX;
}
CurrLine = a_NBT.FindChildByName(Data, "zCenter");
if (CurrLine >= 0)
{
int CenterZ = a_NBT.GetInt(CurrLine);
m_Map->m_CenterZ = CenterZ;
}
unsigned int NumPixels = m_Map->GetNumPixels();
m_Map->m_Data.resize(NumPixels);
// TODO xdot: Parse the byte array.
return true;
}

View File

@ -0,0 +1,52 @@
// MapSerializer.h
// Declares the cMapSerializer class that is used for saving maps into NBT format used by Anvil
#pragma once
// fwd:
class cFastNBTWriter;
class cParsedNBT;
class cMap;
class cMapSerializer
{
public:
cMapSerializer(const AString& a_WorldName, cMap * a_Map);
/// Try to load the scoreboard
bool Load(void);
/// Try to save the scoreboard
bool Save(void);
private:
void SaveMapToNBT(cFastNBTWriter & a_Writer);
bool LoadMapFromNBT(const cParsedNBT & a_NBT);
cMap * m_Map;
AString m_Path;
} ;