cMap::UpdateRadius
This commit is contained in:
parent
c7fb000858
commit
cf96e69716
|
@ -1199,6 +1199,8 @@ void cClientHandle::HandleSlotSelected(short a_SlotNum)
|
||||||
|
|
||||||
if (Map != NULL)
|
if (Map != NULL)
|
||||||
{
|
{
|
||||||
|
Map->UpdateRadius(*m_Player, 128); // Temporary
|
||||||
|
|
||||||
// TODO 2014-02-14 xdot: Optimization - Do not send the whole map.
|
// TODO 2014-02-14 xdot: Optimization - Do not send the whole map.
|
||||||
Map->SendTo(*this);
|
Map->SendTo(*this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ class cItemEmptyMapHandler :
|
||||||
public cItemHandler
|
public cItemHandler
|
||||||
{
|
{
|
||||||
typedef cItemHandler super;
|
typedef cItemHandler super;
|
||||||
|
|
||||||
|
static const unsigned int DEFAULT_SCALE = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
cItemEmptyMapHandler() :
|
cItemEmptyMapHandler() :
|
||||||
|
@ -34,12 +36,12 @@ public:
|
||||||
|
|
||||||
// The map center is fixed at the central point of the 8x8 block of chunks you are standing in when you right-click it.
|
// The map center is fixed at the central point of the 8x8 block of chunks you are standing in when you right-click it.
|
||||||
|
|
||||||
const int RegionWidth = cChunkDef::Width * 8;
|
const int RegionWidth = cChunkDef::Width * 8 * pow(2, DEFAULT_SCALE);
|
||||||
|
|
||||||
int CenterX = round(a_Player->GetPosX() / (float) RegionWidth) * RegionWidth;
|
int CenterX = round(a_Player->GetPosX() / (float) RegionWidth) * RegionWidth;
|
||||||
int CenterZ = round(a_Player->GetPosZ() / (float) RegionWidth) * RegionWidth;
|
int CenterZ = round(a_Player->GetPosZ() / (float) RegionWidth) * RegionWidth;
|
||||||
|
|
||||||
a_World->CreateMap(CenterX, CenterZ, 0);
|
a_World->CreateMap(CenterX, CenterZ, DEFAULT_SCALE);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
112
src/Map.cpp
112
src/Map.cpp
|
@ -8,6 +8,7 @@
|
||||||
#include "ClientHandle.h"
|
#include "ClientHandle.h"
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "Chunk.h"
|
#include "Chunk.h"
|
||||||
|
#include "Entities/Player.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,6 +24,8 @@ cMap::cMap(unsigned int a_ID, cWorld * a_World)
|
||||||
, m_World(a_World)
|
, m_World(a_World)
|
||||||
{
|
{
|
||||||
m_Data.assign(m_Width * m_Height, 0);
|
m_Data.assign(m_Width * m_Height, 0);
|
||||||
|
|
||||||
|
Printf(m_Name, "map_%i", m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,12 +43,34 @@ cMap::cMap(unsigned int a_ID, int a_CenterX, int a_CenterZ, cWorld * a_World, un
|
||||||
{
|
{
|
||||||
m_Data.assign(m_Width * m_Height, 0);
|
m_Data.assign(m_Width * m_Height, 0);
|
||||||
|
|
||||||
for (unsigned int X = 0; X < m_Width; ++X)
|
Printf(m_Name, "map_%i", m_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void cMap::UpdateRadius(int a_PixelX, int a_PixelZ, unsigned int a_Radius)
|
||||||
|
{
|
||||||
|
int PixelRadius = a_Radius / GetPixelWidth();
|
||||||
|
|
||||||
|
unsigned int StartX = std::max(a_PixelX - PixelRadius, 0);
|
||||||
|
unsigned int StartZ = std::max(a_PixelZ - PixelRadius, 0);
|
||||||
|
|
||||||
|
unsigned int EndX = std::min(a_PixelX + PixelRadius, (int)m_Width);
|
||||||
|
unsigned int EndZ = std::min(a_PixelZ + PixelRadius, (int)m_Height);
|
||||||
|
|
||||||
|
for (unsigned int X = StartX; X < EndX; ++X)
|
||||||
{
|
{
|
||||||
for (unsigned int Y = 0; Y < m_Height; ++Y)
|
for (unsigned int Z = StartZ; Z < EndZ; ++Z)
|
||||||
{
|
{
|
||||||
// Debug
|
int dX = X - a_PixelX;
|
||||||
m_Data[Y + X * m_Height] = rand() % 100;
|
int dZ = Z - a_PixelZ;
|
||||||
|
|
||||||
|
if ((dX * dX) + (dZ * dZ) < (PixelRadius * PixelRadius))
|
||||||
|
{
|
||||||
|
UpdatePixel(X, Z);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,20 +79,85 @@ cMap::cMap(unsigned int a_ID, int a_CenterX, int a_CenterZ, cWorld * a_World, un
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void cMap::UpdateRadius(cPlayer & a_Player, unsigned int a_Radius)
|
||||||
|
{
|
||||||
|
unsigned int PixelWidth = GetPixelWidth();
|
||||||
|
|
||||||
|
int PixelX = (a_Player.GetPosX() - m_CenterX) / PixelWidth + (m_Width / 2);
|
||||||
|
int PixelZ = (a_Player.GetPosZ() - m_CenterZ) / PixelWidth + (m_Height / 2);
|
||||||
|
|
||||||
|
UpdateRadius(PixelX, PixelZ, a_Radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool cMap::UpdatePixel(unsigned int a_X, unsigned int a_Y)
|
bool cMap::UpdatePixel(unsigned int a_X, unsigned int a_Y)
|
||||||
{
|
{
|
||||||
ASSERT(m_World != NULL);
|
ASSERT(m_World != NULL);
|
||||||
|
|
||||||
cChunk * Chunk = NULL;
|
unsigned int PixelWidth = GetPixelWidth();
|
||||||
|
|
||||||
if (Chunk == NULL)
|
int BlockX = m_CenterX + ((a_X - m_Width) * PixelWidth);
|
||||||
|
int BlockZ = m_CenterZ + ((a_Y - m_Height) * PixelWidth);
|
||||||
|
|
||||||
|
int ChunkX, ChunkY, ChunkZ;
|
||||||
|
m_World->BlockToChunk(BlockX, 0, BlockZ, ChunkX, ChunkY, ChunkZ);
|
||||||
|
|
||||||
|
int RelX = BlockX - (ChunkX * cChunkDef::Width);
|
||||||
|
int RelZ = BlockZ - (ChunkZ * cChunkDef::Width);
|
||||||
|
|
||||||
|
class cCalculatePixelCb :
|
||||||
|
public cChunkCallback
|
||||||
{
|
{
|
||||||
return false;
|
cMap * m_Map;
|
||||||
}
|
|
||||||
|
|
||||||
int Height = Chunk->GetHeight(a_X, a_Y);
|
int m_RelX, m_RelZ;
|
||||||
|
|
||||||
// TODO
|
ColorID m_PixelData;
|
||||||
|
|
||||||
|
public:
|
||||||
|
cCalculatePixelCb(cMap * a_Map, int a_RelX, int a_RelZ)
|
||||||
|
: m_Map(a_Map), m_RelX(a_RelX), m_RelZ(a_RelZ), m_PixelData(0) {}
|
||||||
|
|
||||||
|
virtual bool Item(cChunk * a_Chunk) override
|
||||||
|
{
|
||||||
|
if (a_Chunk == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int PixelWidth = m_Map->GetPixelWidth();
|
||||||
|
|
||||||
|
for (unsigned int X = m_RelX; X < m_RelX + PixelWidth; ++X)
|
||||||
|
{
|
||||||
|
for (unsigned int Z = m_RelZ; Z < m_RelZ + PixelWidth; ++Z)
|
||||||
|
{
|
||||||
|
int Height = a_Chunk->GetHeight(X, Z);
|
||||||
|
|
||||||
|
if (Height > 0)
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_PixelData = 8; // Debug
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ColorID GetPixelData(void) const
|
||||||
|
{
|
||||||
|
return m_PixelData;
|
||||||
|
}
|
||||||
|
} CalculatePixelCb(this, RelX, RelZ);
|
||||||
|
|
||||||
|
ASSERT(m_World != NULL);
|
||||||
|
m_World->DoWithChunk(ChunkX, ChunkZ, CalculatePixelCb);
|
||||||
|
|
||||||
|
m_Data[a_Y + (a_X * m_Height)] = CalculatePixelCb.GetPixelData();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -167,7 +257,7 @@ unsigned int cMap::GetNumPixels(void) const
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned int cMap::GetNumBlocksPerPixel(void) const
|
unsigned int cMap::GetPixelWidth(void) const
|
||||||
{
|
{
|
||||||
return pow(2, m_Scale);
|
return pow(2, m_Scale);
|
||||||
}
|
}
|
||||||
|
|
17
src/Map.h
17
src/Map.h
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
class cClientHandle;
|
class cClientHandle;
|
||||||
class cWorld;
|
class cWorld;
|
||||||
|
class cPlayer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,6 +49,11 @@ public:
|
||||||
/** Send this map to the specified client. */
|
/** Send this map to the specified client. */
|
||||||
void SendTo(cClientHandle & a_Client);
|
void SendTo(cClientHandle & a_Client);
|
||||||
|
|
||||||
|
/** Update a circular region with the specified radius and center (in pixels). */
|
||||||
|
void UpdateRadius(int a_PixelX, int a_PixelZ, unsigned int a_Radius);
|
||||||
|
|
||||||
|
void UpdateRadius(cPlayer & a_Player, unsigned int a_Radius);
|
||||||
|
|
||||||
// tolua_begin
|
// tolua_begin
|
||||||
|
|
||||||
/** Erase pixel data */
|
/** Erase pixel data */
|
||||||
|
@ -71,13 +77,15 @@ public:
|
||||||
|
|
||||||
cWorld * GetWorld(void) { return m_World; }
|
cWorld * GetWorld(void) { return m_World; }
|
||||||
|
|
||||||
|
AString GetName(void) { return m_Name; }
|
||||||
|
|
||||||
eDimension GetDimension(void) const;
|
eDimension GetDimension(void) const;
|
||||||
|
|
||||||
const cColorList & GetData(void) const { return m_Data; }
|
const cColorList & GetData(void) const { return m_Data; }
|
||||||
|
|
||||||
unsigned int GetNumPixels(void) const;
|
unsigned int GetNumPixels(void) const;
|
||||||
|
|
||||||
unsigned int GetNumBlocksPerPixel(void) const;
|
unsigned int GetPixelWidth(void) const;
|
||||||
|
|
||||||
// tolua_end
|
// tolua_end
|
||||||
|
|
||||||
|
@ -87,8 +95,6 @@ private:
|
||||||
/** Update the specified pixel. */
|
/** Update the specified pixel. */
|
||||||
bool UpdatePixel(unsigned int a_X, unsigned int a_Y);
|
bool UpdatePixel(unsigned int a_X, unsigned int a_Y);
|
||||||
|
|
||||||
void PixelToWorldCoords(unsigned int a_X, unsigned int a_Y, int & a_WorldX, int & a_WorldY);
|
|
||||||
|
|
||||||
unsigned int m_ID;
|
unsigned int m_ID;
|
||||||
|
|
||||||
unsigned int m_Width;
|
unsigned int m_Width;
|
||||||
|
@ -105,6 +111,11 @@ private:
|
||||||
|
|
||||||
cWorld * m_World;
|
cWorld * m_World;
|
||||||
|
|
||||||
|
//typedef std::vector<cPlayer*> cPlayerList;
|
||||||
|
//cPlayerList m_TrackedPlayers;
|
||||||
|
|
||||||
|
AString m_Name;
|
||||||
|
|
||||||
friend class cMapSerializer;
|
friend class cMapSerializer;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user