|
|
@@ -1,15 +1,15 @@
|
|
|
#include "Chunk.h"
|
|
|
|
|
|
-#include <AsynchronCall.h>
|
|
|
#include <InMemoryBuffer.h>
|
|
|
#include <Logging.h>
|
|
|
|
|
|
+#include "BlockType.h"
|
|
|
#include "Constants.h"
|
|
|
#include "Dimension.h"
|
|
|
#include "Entity.h"
|
|
|
+#include "EntityType.h"
|
|
|
#include "FluidBlock.h"
|
|
|
#include "Game.h"
|
|
|
-#include "NoBlock.h"
|
|
|
#include "WorldGenerator.h"
|
|
|
|
|
|
Chunk::Chunk(Framework::Punkt location, int dimensionId)
|
|
|
@@ -20,13 +20,11 @@ Chunk::Chunk(Framework::Punkt location, int dimensionId)
|
|
|
worldUpdated(1),
|
|
|
currentlyLoading(1)
|
|
|
{
|
|
|
- blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
|
|
|
- blockIds = new unsigned short[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
|
|
|
+ blocks = new Block**[WORLD_HEIGHT];
|
|
|
+ blockIds = new unsigned short*[WORLD_HEIGHT];
|
|
|
lightData = new unsigned char[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6];
|
|
|
- memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
|
|
|
- memset(blockIds,
|
|
|
- 0,
|
|
|
- CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(unsigned short));
|
|
|
+ memset(blocks, 0, WORLD_HEIGHT * sizeof(Block**));
|
|
|
+ memset(blockIds, 0, WORLD_HEIGHT * sizeof(unsigned short*));
|
|
|
memset(lightData, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
|
|
|
zNeighbours[0] = 0;
|
|
|
zNeighbours[1] = 0;
|
|
|
@@ -44,9 +42,16 @@ Chunk::Chunk(Framework::Punkt location,
|
|
|
|
|
|
Chunk::~Chunk()
|
|
|
{
|
|
|
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
|
|
|
+ for (int h = 0; h < WORLD_HEIGHT; h++)
|
|
|
{
|
|
|
- if (blocks[i]) blocks[i]->release();
|
|
|
+ for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
|
|
|
+ {
|
|
|
+ if (blocks[h] && blocks[h][i]) blocks[h][i]->release();
|
|
|
+ }
|
|
|
+ delete[] blocks[h];
|
|
|
+ blocks[h] = 0;
|
|
|
+ delete[] blockIds[h];
|
|
|
+ blockIds[h] = 0;
|
|
|
}
|
|
|
delete[] blocks;
|
|
|
delete[] blockIds;
|
|
|
@@ -83,20 +88,20 @@ void Chunk::tick(TickQueue* zQueue)
|
|
|
|
|
|
void Chunk::postTick() {}
|
|
|
|
|
|
-void Chunk::addLightSource(int index)
|
|
|
+void Chunk::addLightSource(int z, int index)
|
|
|
{
|
|
|
for (int i : lightSources)
|
|
|
{
|
|
|
- if (i == index) return;
|
|
|
+ if (i == index * WORLD_HEIGHT + z) return;
|
|
|
}
|
|
|
- lightSources.add(index);
|
|
|
+ lightSources.add(index * WORLD_HEIGHT + z);
|
|
|
}
|
|
|
|
|
|
-void Chunk::removeLightSource(int index)
|
|
|
+void Chunk::removeLightSource(int z, int index)
|
|
|
{
|
|
|
for (auto i = lightSources.begin(); i; i++)
|
|
|
{
|
|
|
- if (i.val() == index)
|
|
|
+ if (i.val() == index * WORLD_HEIGHT + z)
|
|
|
{
|
|
|
i.remove();
|
|
|
return;
|
|
|
@@ -104,66 +109,186 @@ void Chunk::removeLightSource(int index)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
|
+void Chunk::sendToClient(Framework::StreamWriter* zWriter, bool* instanceMap)
|
|
|
{
|
|
|
- for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ for (int x = 0; x < CHUNK_SIZE; x++)
|
|
|
{
|
|
|
- for (int x = -1; x <= CHUNK_SIZE; x++)
|
|
|
+ for (int y = 0; y < CHUNK_SIZE; y++)
|
|
|
{
|
|
|
- for (int y = -1; y <= CHUNK_SIZE; y++)
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
{
|
|
|
- if ((x < 0 || x == CHUNK_SIZE) && (y < 0 || y > CHUNK_SIZE))
|
|
|
+ int index = Chunk::index(x, y);
|
|
|
+ const BlockType* type = Game::INSTANCE->zBlockType(
|
|
|
+ blockIds[z] ? blockIds[z][index] : 0);
|
|
|
+ if (isVisible(z, index) && type->doesNeedClientInstance())
|
|
|
{
|
|
|
- continue;
|
|
|
+ int mI = index * WORLD_HEIGHT + z;
|
|
|
+ if (z < WORLD_HEIGHT - 1)
|
|
|
+ {
|
|
|
+ instanceMap[mI + (CHUNK_SIZE + 1) * WORLD_HEIGHT + 1]
|
|
|
+ = 1;
|
|
|
+ }
|
|
|
+ if (z > 0)
|
|
|
+ {
|
|
|
+ instanceMap[mI + (CHUNK_SIZE + 1) * WORLD_HEIGHT - 1]
|
|
|
+ = 1;
|
|
|
+ }
|
|
|
+ instanceMap[mI + WORLD_HEIGHT] = 1;
|
|
|
+ instanceMap[mI + (2 * CHUNK_SIZE + 1) * WORLD_HEIGHT] = 1;
|
|
|
+ instanceMap[mI + CHUNK_SIZE * WORLD_HEIGHT] = 1;
|
|
|
+ instanceMap[mI + (CHUNK_SIZE + 2) * WORLD_HEIGHT] = 1;
|
|
|
+ assert(blockIds[z]);
|
|
|
+ zWriter->schreibe((char*)&blockIds[z][index], 2);
|
|
|
+ zWriter->schreibe((char*)&mI, 4);
|
|
|
+ char state = 0;
|
|
|
+ if (type->isFluid())
|
|
|
+ {
|
|
|
+ state |= 1;
|
|
|
+ }
|
|
|
+ if ((blocks[z] && blocks[z][index]
|
|
|
+ && blocks[z][index]->isPassable())
|
|
|
+ || (type->zDefault()->isPassable()))
|
|
|
+ {
|
|
|
+ state |= 2;
|
|
|
+ }
|
|
|
+ zWriter->schreibe((char*)&state, 1);
|
|
|
+ if ((state | 1) == state)
|
|
|
+ {
|
|
|
+ FluidBlock* fluidBlock
|
|
|
+ = blocks[z]
|
|
|
+ ? dynamic_cast<FluidBlock*>(blocks[z][index])
|
|
|
+ : 0;
|
|
|
+ char data
|
|
|
+ = fluidBlock ? fluidBlock->getFlowOptions() : 0;
|
|
|
+ zWriter->schreibe(&data, 1);
|
|
|
+ data = fluidBlock ? fluidBlock->getDistanceToSource()
|
|
|
+ : 0;
|
|
|
+ zWriter->schreibe(&data, 1);
|
|
|
+ }
|
|
|
+ if ((state | 2) == state)
|
|
|
+ {
|
|
|
+ float speedModifier
|
|
|
+ = blocks[z] && blocks[z][index]
|
|
|
+ ? blocks[z][index]->getSpeedModifier()
|
|
|
+ : type->zDefault()->getSpeedModifier();
|
|
|
+ zWriter->schreibe((char*)&speedModifier, 4);
|
|
|
+ }
|
|
|
}
|
|
|
- bool needSend = 0;
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ unsigned short end = 0;
|
|
|
+ zWriter->schreibe((char*)&end, 2);
|
|
|
+}
|
|
|
+
|
|
|
+void Chunk::sendLightToClient(
|
|
|
+ Framework::StreamWriter* zWriter, bool* instanceMap)
|
|
|
+{
|
|
|
+ cs.lock();
|
|
|
+ Chunk* zNeighbours[4];
|
|
|
+ for (int i = 0; i < 4; i++)
|
|
|
+ {
|
|
|
+ zNeighbours[i]
|
|
|
+ = this->zNeighbours[i]
|
|
|
+ ? dynamic_cast<Chunk*>(this->zNeighbours[i]->getThis())
|
|
|
+ : 0;
|
|
|
+ if (zNeighbours[i])
|
|
|
+ {
|
|
|
+ Direction dir = getDirectionFromIndex(i);
|
|
|
+ switch (dir)
|
|
|
+ {
|
|
|
+ case WEST:
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
{
|
|
|
- Framework::Vec3<int> pos
|
|
|
- = Framework::Vec3<int>(x, y, z)
|
|
|
- + getDirection(getDirectionFromIndex(i));
|
|
|
- if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
|
|
|
+ for (int j = 0; j < CHUNK_SIZE; j++)
|
|
|
{
|
|
|
- if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
|
|
|
- && pos.y < CHUNK_SIZE)
|
|
|
+ int type = zNeighbours[i]->getBlockTypeAt(
|
|
|
+ {CHUNK_SIZE - 1, j, z});
|
|
|
+ if (type
|
|
|
+ && Game::INSTANCE->zBlockType(type)
|
|
|
+ ->doesNeedClientInstance())
|
|
|
{
|
|
|
- int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
|
|
|
- + pos.z;
|
|
|
- int type = blockIds[bi];
|
|
|
- needSend |= type != BlockTypeEnum::NO_BLOCK
|
|
|
- && Game::INSTANCE->zBlockType(type)
|
|
|
- ->doesNeedClientInstance();
|
|
|
+ instanceMap[(CHUNK_SIZE + j + 1) * WORLD_HEIGHT + z]
|
|
|
+ = 1;
|
|
|
}
|
|
|
- else
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case NORTH:
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < CHUNK_SIZE; j++)
|
|
|
+ {
|
|
|
+ int type = zNeighbours[i]->getBlockTypeAt(
|
|
|
+ {j, CHUNK_SIZE - 1, z});
|
|
|
+ if (type
|
|
|
+ && Game::INSTANCE->zBlockType(type)
|
|
|
+ ->doesNeedClientInstance())
|
|
|
{
|
|
|
- if (x >= 0 && x < CHUNK_SIZE && y >= 0
|
|
|
- && y < CHUNK_SIZE)
|
|
|
- {
|
|
|
- cs.lock();
|
|
|
- if (i < 4 && zNeighbours[i])
|
|
|
- {
|
|
|
- Framework::Vec3<int> offset
|
|
|
- = getDirection(getDirectionFromIndex(i))
|
|
|
- * 16;
|
|
|
- int bi = ((pos.x - offset.x) * CHUNK_SIZE
|
|
|
- + (pos.y - offset.y))
|
|
|
- * WORLD_HEIGHT
|
|
|
- + (pos.z - offset.z);
|
|
|
- int type = zNeighbours[i]->blockIds[bi];
|
|
|
- needSend |= Game::INSTANCE->zBlockType(type)
|
|
|
- ->doesNeedClientInstance();
|
|
|
- }
|
|
|
- cs.unlock();
|
|
|
- }
|
|
|
+ instanceMap[((j + 1) * CHUNK_SIZE + 1)
|
|
|
+ * WORLD_HEIGHT
|
|
|
+ + z]
|
|
|
+ = 1;
|
|
|
}
|
|
|
- if (needSend) break;
|
|
|
}
|
|
|
}
|
|
|
- if (needSend)
|
|
|
+ break;
|
|
|
+ case EAST:
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < CHUNK_SIZE; j++)
|
|
|
+ {
|
|
|
+ int type = zNeighbours[i]->getBlockTypeAt({0, j, z});
|
|
|
+ if (type
|
|
|
+ && Game::INSTANCE->zBlockType(type)
|
|
|
+ ->doesNeedClientInstance())
|
|
|
+ {
|
|
|
+ instanceMap[((CHUNK_SIZE)*CHUNK_SIZE + j + 1)
|
|
|
+ * WORLD_HEIGHT
|
|
|
+ + z]
|
|
|
+ = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SOUTH:
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < CHUNK_SIZE; j++)
|
|
|
+ {
|
|
|
+ int type = zNeighbours[i]->getBlockTypeAt({j, 0, z});
|
|
|
+ if (type
|
|
|
+ && Game::INSTANCE->zBlockType(type)
|
|
|
+ ->doesNeedClientInstance())
|
|
|
+ {
|
|
|
+ instanceMap[((j + 1) * CHUNK_SIZE + CHUNK_SIZE)
|
|
|
+ * WORLD_HEIGHT
|
|
|
+ + z]
|
|
|
+ = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cs.unlock();
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ {
|
|
|
+ for (int x = -1; x <= CHUNK_SIZE; x++)
|
|
|
+ {
|
|
|
+ for (int y = -1; y <= CHUNK_SIZE; y++)
|
|
|
+ {
|
|
|
+ if ((x < 0 || x == CHUNK_SIZE) && (y < 0 || y == CHUNK_SIZE))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (instanceMap[((x + 1) * CHUNK_SIZE + y + 1) * WORLD_HEIGHT
|
|
|
+ + z])
|
|
|
{
|
|
|
if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE)
|
|
|
{
|
|
|
- int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
|
|
|
+ int index = Chunk::index(x, y) * WORLD_HEIGHT + z;
|
|
|
zWriter->schreibe((char*)&index, 4);
|
|
|
zWriter->schreibe((char*)(lightData + index * 6), 6);
|
|
|
}
|
|
|
@@ -171,32 +296,28 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
|
{
|
|
|
int dir;
|
|
|
int index = 0;
|
|
|
+ int tmpX = x;
|
|
|
+ int tmpY = y;
|
|
|
+ index = (((x + CHUNK_SIZE) % CHUNK_SIZE) * CHUNK_SIZE
|
|
|
+ + ((y + CHUNK_SIZE) % CHUNK_SIZE))
|
|
|
+ * WORLD_HEIGHT
|
|
|
+ + z;
|
|
|
if (x == -1)
|
|
|
{
|
|
|
dir = getDirectionIndex(WEST);
|
|
|
- index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y)
|
|
|
- * WORLD_HEIGHT
|
|
|
- + z;
|
|
|
}
|
|
|
else if (y == -1)
|
|
|
{
|
|
|
dir = getDirectionIndex(NORTH);
|
|
|
- index = (x * CHUNK_SIZE + CHUNK_SIZE - 1)
|
|
|
-
|
|
|
- * WORLD_HEIGHT
|
|
|
- + z;
|
|
|
}
|
|
|
else if (x == CHUNK_SIZE)
|
|
|
{
|
|
|
dir = getDirectionIndex(EAST);
|
|
|
- index = y * WORLD_HEIGHT + z;
|
|
|
}
|
|
|
else if (y == CHUNK_SIZE)
|
|
|
{
|
|
|
dir = getDirectionIndex(SOUTH);
|
|
|
- index = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
|
|
|
}
|
|
|
- cs.lock();
|
|
|
if (zNeighbours[dir])
|
|
|
{
|
|
|
int i = -1;
|
|
|
@@ -209,7 +330,6 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
|
+ index * 6),
|
|
|
6);
|
|
|
}
|
|
|
- cs.unlock();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -217,75 +337,70 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
|
}
|
|
|
int end = -2;
|
|
|
zWriter->schreibe((char*)&end, 4);
|
|
|
+ for (int i = 0; i < 4; i++)
|
|
|
+ {
|
|
|
+ if (zNeighbours[i])
|
|
|
+ {
|
|
|
+ zNeighbours[i]->release();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-bool Chunk::isVisible(int index) const
|
|
|
+bool Chunk::isVisible(int z, int index) const
|
|
|
{
|
|
|
- if (!blocks[index])
|
|
|
+ unsigned short blockType = blockIds[z] ? blockIds[z][index] : 0;
|
|
|
+ if (blockType)
|
|
|
{
|
|
|
- unsigned short blockType
|
|
|
- = blocks[index]
|
|
|
- ? (unsigned short)blocks[index]->zBlockType()->getId()
|
|
|
- : blockIds[index];
|
|
|
- if (blockType)
|
|
|
+ if (CONST_BLOCK(0, blockType)->isTransparent()
|
|
|
+ || CONST_BLOCK(0, blockType)->isPassable())
|
|
|
+ return 1;
|
|
|
+ else
|
|
|
{
|
|
|
- if (CONST_BLOCK(0, blockIds[index])->isTransparent()
|
|
|
- || CONST_BLOCK(0, blockIds[index])->isPassable())
|
|
|
- return 1;
|
|
|
- else
|
|
|
+ Framework::Vec3<int> indexPos
|
|
|
+ = {(index) / CHUNK_SIZE, (index) % CHUNK_SIZE, z};
|
|
|
+ for (int d = 0; d < 6; d++)
|
|
|
{
|
|
|
- Framework::Vec3<int> indexPos
|
|
|
- = {(index / WORLD_HEIGHT) / CHUNK_SIZE,
|
|
|
- (index / WORLD_HEIGHT) % CHUNK_SIZE,
|
|
|
- index % WORLD_HEIGHT};
|
|
|
- for (int d = 0; d < 6; d++)
|
|
|
+ Framework::Either<Block*, int> n = BlockTypeEnum::NO_BLOCK;
|
|
|
+ Framework::Vec3<int> pos
|
|
|
+ = getDirection((Directions)getDirectionFromIndex(d))
|
|
|
+ + indexPos;
|
|
|
+ if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
|
|
|
+ && pos.y < CHUNK_SIZE && pos.z >= 0 && pos.z < WORLD_HEIGHT)
|
|
|
{
|
|
|
- Framework::Either<Block*, int> n = BlockTypeEnum::NO_BLOCK;
|
|
|
- Framework::Vec3<int> pos
|
|
|
- = getDirection((Directions)getDirectionFromIndex(d))
|
|
|
- + indexPos;
|
|
|
- if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
|
|
|
- && pos.y < CHUNK_SIZE && pos.z >= 0
|
|
|
- && pos.z < WORLD_HEIGHT)
|
|
|
- {
|
|
|
- n = zBlockAt(pos);
|
|
|
- }
|
|
|
- else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
|
|
|
- && zNeighbours[d])
|
|
|
- {
|
|
|
- if (pos.x < 0) pos.x += CHUNK_SIZE;
|
|
|
- if (pos.x >= CHUNK_SIZE) pos.x -= CHUNK_SIZE;
|
|
|
- if (pos.y < 0) pos.y += CHUNK_SIZE;
|
|
|
- if (pos.y >= CHUNK_SIZE) pos.y -= CHUNK_SIZE;
|
|
|
- n = zNeighbours[d]->zBlockAt(pos);
|
|
|
- }
|
|
|
- else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
|
|
|
- && !zNeighbours[d])
|
|
|
- {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- if (n.isA()
|
|
|
- && (((Block*)n)->isPassable()
|
|
|
- || ((Block*)n)->isTransparent()))
|
|
|
- return 1;
|
|
|
- if (n.isB()
|
|
|
- && (CONST_BLOCK(0, n)->isTransparent()
|
|
|
- || CONST_BLOCK(0, n)->isPassable()))
|
|
|
- return 1;
|
|
|
+ n = zBlockAt(pos);
|
|
|
}
|
|
|
+ else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
|
|
|
+ && zNeighbours[d])
|
|
|
+ {
|
|
|
+ if (pos.x < 0) pos.x += CHUNK_SIZE;
|
|
|
+ if (pos.x >= CHUNK_SIZE) pos.x -= CHUNK_SIZE;
|
|
|
+ if (pos.y < 0) pos.y += CHUNK_SIZE;
|
|
|
+ if (pos.y >= CHUNK_SIZE) pos.y -= CHUNK_SIZE;
|
|
|
+ n = zNeighbours[d]->zBlockAt(pos);
|
|
|
+ }
|
|
|
+ else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
|
|
|
+ && !zNeighbours[d])
|
|
|
+ {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (n.isA()
|
|
|
+ && (((Block*)n)->isPassable()
|
|
|
+ || ((Block*)n)->isTransparent()))
|
|
|
+ return 1;
|
|
|
+ if (n.isB()
|
|
|
+ && (CONST_BLOCK(0, n)->isTransparent()
|
|
|
+ || CONST_BLOCK(0, n)->isPassable()))
|
|
|
+ return 1;
|
|
|
}
|
|
|
}
|
|
|
- return 0;
|
|
|
}
|
|
|
- else
|
|
|
- return blocks[index]->isVisible();
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-void Chunk::broadcastLightData(int index, bool foreground)
|
|
|
+void Chunk::broadcastLightData(int z, int index, bool foreground)
|
|
|
{
|
|
|
- int x = (index / WORLD_HEIGHT) / CHUNK_SIZE;
|
|
|
- int y = (index / WORLD_HEIGHT) % CHUNK_SIZE;
|
|
|
- int z = index % WORLD_HEIGHT;
|
|
|
+ int x = index / CHUNK_SIZE;
|
|
|
+ int y = index % CHUNK_SIZE;
|
|
|
NetworkMessage* msg = new NetworkMessage();
|
|
|
msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
|
char* message = new char[19];
|
|
|
@@ -293,7 +408,7 @@ void Chunk::broadcastLightData(int index, bool foreground)
|
|
|
*(int*)(message + 1) = x + this->location.x - CHUNK_SIZE / 2;
|
|
|
*(int*)(message + 5) = y + this->location.y - CHUNK_SIZE / 2;
|
|
|
*(int*)(message + 9) = z;
|
|
|
- memcpy(message + 13, lightData + index * 6, 6);
|
|
|
+ memcpy(message + 13, lightData + (index * WORLD_HEIGHT + z) * 6, 6);
|
|
|
msg->setMessage(message, 19);
|
|
|
if (!foreground) msg->setUseBackground();
|
|
|
notifyObservers(msg);
|
|
|
@@ -306,18 +421,17 @@ Framework::Either<Block*, int> Chunk::zBlockNeighbor(
|
|
|
&& location.y < CHUNK_SIZE && location.z >= 0
|
|
|
&& location.z < WORLD_HEIGHT)
|
|
|
{
|
|
|
- int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT
|
|
|
- + location.z;
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
if (zNeighborChunk)
|
|
|
{
|
|
|
*zNeighborChunk = this;
|
|
|
}
|
|
|
- if (blocks[index])
|
|
|
- return blocks[index];
|
|
|
+ if (blocks[location.z] && blocks[location.z][index])
|
|
|
+ return blocks[location.z][index];
|
|
|
else
|
|
|
- return (int)blockIds[index];
|
|
|
+ return blockIds[location.z] ? (int)blockIds[location.z][index] : 0;
|
|
|
}
|
|
|
- if (added && location.z >= 0 && location.z < WORLD_HEIGHT)
|
|
|
+ if (location.z >= 0 && location.z < WORLD_HEIGHT)
|
|
|
return Game::INSTANCE->zBlockAt(
|
|
|
{location.x + this->location.x - CHUNK_SIZE / 2,
|
|
|
location.y + this->location.y - CHUNK_SIZE / 2,
|
|
|
@@ -352,19 +466,24 @@ void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
|
|
|
if (observer->getEntityId() == zEntity->getId()) return;
|
|
|
}
|
|
|
int id = zEntity->getId();
|
|
|
- observers.add(new InformationObserver(id));
|
|
|
+ InformationObserver* observer = new InformationObserver(id);
|
|
|
+ observers.add(observer);
|
|
|
laterHandler.addTodo([this, id]() {
|
|
|
Framework::InMemoryBuffer buffer;
|
|
|
buffer.schreibe("\4", 1);
|
|
|
buffer.schreibe((char*)&location.x, 4);
|
|
|
buffer.schreibe((char*)&location.y, 4);
|
|
|
- sendToClient(&buffer);
|
|
|
- sendLightToClient(&buffer);
|
|
|
+ bool instanceMap[(CHUNK_SIZE + 2) * (CHUNK_SIZE + 2) * WORLD_HEIGHT];
|
|
|
+ memset(instanceMap, 0, sizeof(instanceMap));
|
|
|
+ sendToClient(&buffer, instanceMap);
|
|
|
+ sendLightToClient(&buffer, instanceMap);
|
|
|
NetworkMessage* msg = new NetworkMessage();
|
|
|
msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
|
+#ifdef _DEBUG
|
|
|
Framework::Logging::debug()
|
|
|
<< "chunk size: " << location.x << ", " << location.y << ": "
|
|
|
<< buffer.getSize() << "b";
|
|
|
+#endif
|
|
|
char* message = new char[buffer.getSize()];
|
|
|
buffer.lese(message, (int)buffer.getSize());
|
|
|
msg->setMessage(message, (int)buffer.getSize());
|
|
|
@@ -377,6 +496,15 @@ void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
|
|
|
else
|
|
|
msg->release();
|
|
|
});
|
|
|
+ for (Entity* entity : entitiesInChunk)
|
|
|
+ {
|
|
|
+ NetworkMessage* msg = new NetworkMessage();
|
|
|
+ msg->addEntityMessage(entity);
|
|
|
+ if (!msg->isEmpty())
|
|
|
+ {
|
|
|
+ observer->sendMessage(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void Chunk::removeObserver(Entity* zEntity)
|
|
|
@@ -386,6 +514,15 @@ void Chunk::removeObserver(Entity* zEntity)
|
|
|
{
|
|
|
if (observer->getEntityId() == zEntity->getId())
|
|
|
{
|
|
|
+ for (Entity* entity : entitiesInChunk)
|
|
|
+ {
|
|
|
+ NetworkMessage* msg = new NetworkMessage();
|
|
|
+ msg->removeEntityMessage(entity);
|
|
|
+ if (!msg->isEmpty())
|
|
|
+ {
|
|
|
+ observer->sendMessage(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
observers.remove(index);
|
|
|
return;
|
|
|
}
|
|
|
@@ -423,76 +560,175 @@ void Chunk::api(Framework::StreamReader* zRequest,
|
|
|
|
|
|
void Chunk::initializeLightning()
|
|
|
{
|
|
|
- unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
|
|
|
- unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
|
|
|
- while (true)
|
|
|
+ unsigned char dayLight[3] = {255, 255, 255};
|
|
|
+ unsigned char noLight[3] = {0, 0, 0};
|
|
|
+ unsigned short visited[CHUNK_SIZE][WORLD_HEIGHT];
|
|
|
+ memset(visited, 0, sizeof(visited));
|
|
|
+ int minZ = 0;
|
|
|
+ int goUps = 0;
|
|
|
+ for (int z = WORLD_HEIGHT - 1; z >= 0; z--)
|
|
|
{
|
|
|
- bool changes = false;
|
|
|
- for (int z = WORLD_HEIGHT - 1; z >= 0; z--)
|
|
|
+ minZ = z;
|
|
|
+ unsigned char max[3] = {0, 0, 0};
|
|
|
+ bool allVisited = 1;
|
|
|
+ for (int x = 0; x < CHUNK_SIZE; x++)
|
|
|
{
|
|
|
- for (int x = 0; x < CHUNK_SIZE; x++)
|
|
|
+ for (int y = 0; y < CHUNK_SIZE; y++)
|
|
|
{
|
|
|
- for (int y = 0; y < CHUNK_SIZE; y++)
|
|
|
+ if (visited[y][z] & (1 << x)) continue;
|
|
|
+ unsigned char* lightAbove
|
|
|
+ = z == WORLD_HEIGHT - 1
|
|
|
+ ? dayLight
|
|
|
+ : getLightData(Framework::Vec3<int>(x, y, z + 1));
|
|
|
+ if (lightAbove[0] | lightAbove[1] | lightAbove[2])
|
|
|
{
|
|
|
- int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
|
|
|
+ visited[y][z] |= 1 << x;
|
|
|
unsigned char* light
|
|
|
= getLightData(Framework::Vec3<int>(x, y, z));
|
|
|
- unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
- {
|
|
|
- unsigned char* neighborLeight;
|
|
|
- Framework::Vec3<int> neighborPos
|
|
|
- = Framework::Vec3<int>(x, y, z)
|
|
|
- + getDirection(getDirectionFromIndex(i));
|
|
|
- if (neighborPos.z < 0 || neighborPos.x < 0
|
|
|
- || neighborPos.y < 0 || neighborPos.x >= CHUNK_SIZE
|
|
|
- || neighborPos.y >= CHUNK_SIZE)
|
|
|
- {
|
|
|
- neighborLeight = noLight;
|
|
|
- }
|
|
|
- else if (neighborPos.z >= WORLD_HEIGHT)
|
|
|
- {
|
|
|
- neighborLeight = dayLight;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- neighborLeight = getLightData(neighborPos);
|
|
|
- }
|
|
|
- for (int j = 0; j < 3; j++)
|
|
|
- newLight[j] = (unsigned char)MAX(newLight[j],
|
|
|
- i == getDirectionIndex(TOP)
|
|
|
- ? neighborLeight[j]
|
|
|
- : (unsigned char)((float)neighborLeight[j]
|
|
|
- * 0.8f));
|
|
|
- for (int j = 3; j < 6; j++)
|
|
|
- newLight[j] = (unsigned char)MAX(newLight[j],
|
|
|
- (unsigned char)((float)neighborLeight[j]
|
|
|
- * 0.85f));
|
|
|
- }
|
|
|
+ int index = Chunk::index(x, y);
|
|
|
const Block* current
|
|
|
- = blocks[index]
|
|
|
- ? blocks[index]
|
|
|
- : Game::INSTANCE->zBlockType(blockIds[index])
|
|
|
+ = (blocks[z] && blocks[z][index])
|
|
|
+ ? blocks[z][index]
|
|
|
+ : Game::INSTANCE
|
|
|
+ ->zBlockType(
|
|
|
+ blockIds[z] ? blockIds[z][index] : 0)
|
|
|
->zDefault();
|
|
|
- // add own light emission
|
|
|
- for (int j = 3; j < 6; j++)
|
|
|
- newLight[j] = (unsigned char)MAX(newLight[j],
|
|
|
- current->getLightEmisionColor()[j - 3]);
|
|
|
- current->filterPassingLight(newLight);
|
|
|
- current->filterPassingLight(newLight + 3);
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
+ light[0] = lightAbove[0];
|
|
|
+ light[1] = lightAbove[1];
|
|
|
+ light[2] = lightAbove[2];
|
|
|
+ current->filterPassingLight(light);
|
|
|
+ max[0] = MAX(max[0], lightAbove[0]);
|
|
|
+ max[1] = MAX(max[1], lightAbove[1]);
|
|
|
+ max[2] = MAX(max[2], lightAbove[2]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ allVisited = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!(max[0] | max[1] | max[2])) break;
|
|
|
+ if (!allVisited)
|
|
|
+ {
|
|
|
+ bool goUp = 1;
|
|
|
+ while (goUp)
|
|
|
+ {
|
|
|
+ goUp = 0;
|
|
|
+ bool changes = 1;
|
|
|
+ while (changes)
|
|
|
+ {
|
|
|
+ changes = 0;
|
|
|
+ for (int x = 0; x < CHUNK_SIZE; x++)
|
|
|
{
|
|
|
- if (newLight[i] != light[i])
|
|
|
+ for (int y = 0; y < CHUNK_SIZE; y++)
|
|
|
{
|
|
|
- changes = 1;
|
|
|
- memcpy(light, newLight, 6);
|
|
|
- break;
|
|
|
+ int index = Chunk::index(x, y);
|
|
|
+ unsigned char* light
|
|
|
+ = getLightData(Framework::Vec3<int>(x, y, z));
|
|
|
+ const Block* current
|
|
|
+ = (blocks[z] && blocks[z][index])
|
|
|
+ ? blocks[z][index]
|
|
|
+ : Game::INSTANCE
|
|
|
+ ->zBlockType(blockIds[z]
|
|
|
+ ? blockIds[z][index]
|
|
|
+ : 0)
|
|
|
+ ->zDefault();
|
|
|
+ unsigned char newLight[3] = {0, 0, 0};
|
|
|
+ for (int i = 0; i < 4; i++)
|
|
|
+ {
|
|
|
+ Framework::Vec3<int> neighborPos
|
|
|
+ = Framework::Vec3<int>(x, y, z)
|
|
|
+ + getDirection(getDirectionFromIndex(i));
|
|
|
+ if (neighborPos.x < 0 || neighborPos.y < 0
|
|
|
+ || neighborPos.x >= CHUNK_SIZE
|
|
|
+ || neighborPos.y >= CHUNK_SIZE)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ unsigned char* neighborLeight
|
|
|
+ = getLightData(neighborPos);
|
|
|
+ for (int j = 0; j < 3; j++)
|
|
|
+ {
|
|
|
+ newLight[j] = (unsigned char)MAX(
|
|
|
+ newLight[j],
|
|
|
+ (unsigned char)((float)neighborLeight[j]
|
|
|
+ * 0.8f));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ current->filterPassingLight(newLight);
|
|
|
+ if (newLight[0] > light[0] || newLight[1] > light[1]
|
|
|
+ || newLight[2] > light[2])
|
|
|
+ {
|
|
|
+ changes = 1;
|
|
|
+ light[0] = MAX(light[0], newLight[0]);
|
|
|
+ light[1] = MAX(light[1], newLight[1]);
|
|
|
+ light[2] = MAX(light[2], newLight[2]);
|
|
|
+ if (z < WORLD_HEIGHT - 1
|
|
|
+ && !(visited[y][z + 1] & (1 << x)))
|
|
|
+ {
|
|
|
+ unsigned char* lightAbove = getLightData(
|
|
|
+ Framework::Vec3<int>(x, y, z + 1));
|
|
|
+ newLight[0]
|
|
|
+ = (unsigned char)(light[0] * 0.8f);
|
|
|
+ newLight[1]
|
|
|
+ = (unsigned char)(light[1] * 0.8f);
|
|
|
+ newLight[2]
|
|
|
+ = (unsigned char)(light[2] * 0.8f);
|
|
|
+ const Block* above
|
|
|
+ = blocks[z + 1] && blocks[z + 1][index]
|
|
|
+ ? blocks[z + 1][index]
|
|
|
+ : Game::INSTANCE
|
|
|
+ ->zBlockType(
|
|
|
+ blockIds[z + 1]
|
|
|
+ ? blockIds[z + 1]
|
|
|
+ [index]
|
|
|
+ : 0)
|
|
|
+ ->zDefault();
|
|
|
+ above->filterPassingLight(newLight);
|
|
|
+ if (newLight[0] > lightAbove[0]
|
|
|
+ || newLight[1] > lightAbove[1]
|
|
|
+ || newLight[2] > lightAbove[2])
|
|
|
+ {
|
|
|
+ lightAbove[0]
|
|
|
+ = MAX(lightAbove[0], newLight[0]);
|
|
|
+ lightAbove[1]
|
|
|
+ = MAX(lightAbove[1], newLight[1]);
|
|
|
+ lightAbove[2]
|
|
|
+ = MAX(lightAbove[2], newLight[2]);
|
|
|
+ visited[y][z + 1] |= 1 << x;
|
|
|
+ goUp = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if (goUp)
|
|
|
+ {
|
|
|
+ z++;
|
|
|
+ goUps++;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- if (!changes) break;
|
|
|
+ }
|
|
|
+#ifdef _DEBUG
|
|
|
+ Framework::Logging::debug() << "goUps: " << goUps << " minZ: " << minZ;
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+void Chunk::updateLightSources()
|
|
|
+{
|
|
|
+ Dimension* zDim = Game::INSTANCE->zDimension(dimensionId);
|
|
|
+ for (int i : lightSources)
|
|
|
+ {
|
|
|
+ int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
|
|
|
+ int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
|
|
|
+ int z = i % WORLD_HEIGHT;
|
|
|
+ Framework::Vec3<int> pos = {x, y, z};
|
|
|
+ zDim->updateLightning(
|
|
|
+ Framework::Vec3<int>(x + this->location.x - CHUNK_SIZE / 2,
|
|
|
+ y + this->location.y - CHUNK_SIZE / 2,
|
|
|
+ z));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -500,12 +736,12 @@ Framework::Either<Block*, int> Chunk::zBlockAt(
|
|
|
Framework::Vec3<int> location) const
|
|
|
{
|
|
|
if (location.z < 0 || location.z >= WORLD_HEIGHT) return 0;
|
|
|
- int index = Chunk::index(location);
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
|
|
|
- if (blocks[index])
|
|
|
- return blocks[index];
|
|
|
+ if (blocks[location.z] && blocks[location.z][index])
|
|
|
+ return blocks[location.z][index];
|
|
|
else
|
|
|
- return (int)blockIds[index];
|
|
|
+ return blockIds[location.z] ? (int)blockIds[location.z][index] : 0;
|
|
|
}
|
|
|
|
|
|
const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
|
|
|
@@ -515,12 +751,13 @@ const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
|
|
|
return Game::INSTANCE->zBlockType(b.getB())->zDefault();
|
|
|
}
|
|
|
|
|
|
-const Block* Chunk::zBlockConst(int index) const
|
|
|
+const Block* Chunk::zBlockConst(int z, int index) const
|
|
|
{
|
|
|
- if (blocks[index])
|
|
|
- return blocks[index];
|
|
|
+ if (blocks[z] && blocks[z][index])
|
|
|
+ return blocks[z][index];
|
|
|
else
|
|
|
- return Game::INSTANCE->zBlockType(blockIds[index])->zDefault();
|
|
|
+ return Game::INSTANCE->zBlockType(blockIds[z] ? blockIds[z][index] : 0)
|
|
|
+ ->zDefault();
|
|
|
}
|
|
|
|
|
|
void Chunk::instantiateBlock(Framework::Vec3<int> location)
|
|
|
@@ -541,8 +778,8 @@ void Chunk::instantiateBlock(Framework::Vec3<int> location)
|
|
|
|
|
|
void Chunk::generateBlock(Framework::Vec3<int> location)
|
|
|
{
|
|
|
- int index = Chunk::index(location);
|
|
|
- if (blockIds[index]) return;
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
+ if (blockIds[location.z] && blockIds[location.z][index]) return;
|
|
|
auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
|
|
|
{location.x + this->location.x - CHUNK_SIZE / 2,
|
|
|
location.y + this->location.y - CHUNK_SIZE / 2,
|
|
|
@@ -556,17 +793,17 @@ void Chunk::generateBlock(Framework::Vec3<int> location)
|
|
|
|
|
|
void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
{
|
|
|
- int index = Chunk::index(location);
|
|
|
- assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
|
|
|
- Block* old = blocks[index];
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
+ assert(index < CHUNK_SIZE * CHUNK_SIZE && index >= 0);
|
|
|
+ Block* old = blocks[location.z] ? blocks[location.z][index] : 0;
|
|
|
if (old && old->isTickSource() != TickSourceType::NONE)
|
|
|
{ // remove from tick sorces
|
|
|
if (old->isTickSource() == TickSourceType::EACH_TICK)
|
|
|
{
|
|
|
for (Framework::ArrayIterator<Block*> obj
|
|
|
= tickSourcesEachTick.begin();
|
|
|
- obj;
|
|
|
- obj++)
|
|
|
+ obj;
|
|
|
+ obj++)
|
|
|
{
|
|
|
if (obj.val() == old)
|
|
|
{
|
|
|
@@ -579,8 +816,8 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
{
|
|
|
for (Framework::ArrayIterator<Block*> obj
|
|
|
= tickSourcesAfterUpdate.begin();
|
|
|
- obj;
|
|
|
- obj++)
|
|
|
+ obj;
|
|
|
+ obj++)
|
|
|
{
|
|
|
if (obj.val() == old)
|
|
|
{
|
|
|
@@ -590,52 +827,42 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if (!blockIds[location.z])
|
|
|
+ {
|
|
|
+ blockIds[location.z] = new unsigned short[CHUNK_SIZE * CHUNK_SIZE];
|
|
|
+ memset(blockIds[location.z],
|
|
|
+ 0,
|
|
|
+ CHUNK_SIZE * CHUNK_SIZE * sizeof(unsigned short));
|
|
|
+ }
|
|
|
+ if (!blocks[location.z])
|
|
|
+ {
|
|
|
+ blocks[location.z] = new Block*[CHUNK_SIZE * CHUNK_SIZE];
|
|
|
+ memset(blocks[location.z], 0, CHUNK_SIZE * CHUNK_SIZE * sizeof(Block*));
|
|
|
+ }
|
|
|
bool change = 0;
|
|
|
bool wasLightSource
|
|
|
= old ? old->zBlockType()->isLightSource()
|
|
|
- : Game::INSTANCE->zBlockType(blockIds[index])->isLightSource();
|
|
|
+ : Game::INSTANCE->zBlockType(blockIds[location.z][index])
|
|
|
+ ->isLightSource();
|
|
|
bool isLightSource = 0;
|
|
|
if (block)
|
|
|
{
|
|
|
- change
|
|
|
- = blockIds[index] != (unsigned short)block->zBlockType()->getId();
|
|
|
- blockIds[index] = (unsigned short)block->zBlockType()->getId();
|
|
|
+ change = blockIds[location.z][index]
|
|
|
+ != (unsigned short)block->zBlockType()->getId();
|
|
|
+
|
|
|
+ blockIds[location.z][index]
|
|
|
+ = (unsigned short)block->zBlockType()->getId();
|
|
|
isLightSource = block->zBlockType()->isLightSource();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (old != 0)
|
|
|
{
|
|
|
- blockIds[index] = BlockTypeEnum::NO_BLOCK;
|
|
|
+ blockIds[location.z][index] = BlockTypeEnum::NO_BLOCK;
|
|
|
}
|
|
|
change = old != 0;
|
|
|
}
|
|
|
- blocks[index] = block;
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
- {
|
|
|
- Direction d = getDirectionFromIndex(i);
|
|
|
- Chunk* zNeighborChunk = 0;
|
|
|
- Framework::Either<Block*, int> neighbor
|
|
|
- = zBlockNeighbor(location + getDirection(d), &zNeighborChunk);
|
|
|
- if (neighbor.isA())
|
|
|
- {
|
|
|
- if (block)
|
|
|
- {
|
|
|
- ((Block*)neighbor)
|
|
|
- ->setNeighbour(getOppositeDirection(d), block);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- ((Block*)neighbor)
|
|
|
- ->setNeighbour(getOppositeDirection(d), blockIds[index]);
|
|
|
- }
|
|
|
- }
|
|
|
- if (block) block->setNeighbour(d, neighbor);
|
|
|
- if (zNeighborChunk && zNeighborChunk != this)
|
|
|
- {
|
|
|
- zNeighborChunk->worldUpdated = 1;
|
|
|
- }
|
|
|
- }
|
|
|
+ blocks[location.z][index] = block;
|
|
|
if (old) old->release();
|
|
|
if (block && block->isTickSource() != TickSourceType::NONE)
|
|
|
{ // add to tick sources
|
|
|
@@ -654,9 +881,9 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
if (isLightSource != wasLightSource)
|
|
|
{
|
|
|
if (isLightSource)
|
|
|
- addLightSource(index);
|
|
|
+ addLightSource(location.z, index);
|
|
|
else
|
|
|
- removeLightSource(index);
|
|
|
+ removeLightSource(location.z, index);
|
|
|
}
|
|
|
if (added)
|
|
|
{
|
|
|
@@ -682,34 +909,27 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
|
|
|
void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
|
|
|
{
|
|
|
- int index = Chunk::index(location);
|
|
|
- assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
|
|
|
- bool wasLightSource
|
|
|
- = Game::INSTANCE->zBlockType(blockIds[index])->isLightSource();
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
+ assert(index < CHUNK_SIZE * CHUNK_SIZE);
|
|
|
+ int oldType = blockIds[location.z] ? blockIds[location.z][index] : 0;
|
|
|
+ bool wasLightSource = Game::INSTANCE->zBlockType(oldType)->isLightSource();
|
|
|
bool isLightSource = Game::INSTANCE->zBlockType(type)->isLightSource();
|
|
|
- if (blockIds[index] != (unsigned short)type)
|
|
|
+ if (oldType != (unsigned short)type)
|
|
|
{
|
|
|
- blockIds[index] = (unsigned short)type;
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
+ if (!blockIds[location.z])
|
|
|
{
|
|
|
- Direction d = getDirectionFromIndex(i);
|
|
|
- Chunk* zNeighborChunk = 0;
|
|
|
- Framework::Either<Block*, int> neighbor
|
|
|
- = zBlockNeighbor(location + getDirection(d), &zNeighborChunk);
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)
|
|
|
- ->setNeighbourType(getOppositeDirection(d), type);
|
|
|
- if (zNeighborChunk && zNeighborChunk != this)
|
|
|
- {
|
|
|
- zNeighborChunk->worldUpdated = 1;
|
|
|
- }
|
|
|
+ blockIds[location.z] = new unsigned short[CHUNK_SIZE * CHUNK_SIZE];
|
|
|
+ memset(blockIds[location.z],
|
|
|
+ 0,
|
|
|
+ CHUNK_SIZE * CHUNK_SIZE * sizeof(unsigned short));
|
|
|
}
|
|
|
+ blockIds[location.z][index] = (unsigned short)type;
|
|
|
if (isLightSource != wasLightSource)
|
|
|
{
|
|
|
if (isLightSource)
|
|
|
- addLightSource(index);
|
|
|
+ addLightSource(location.z, index);
|
|
|
else
|
|
|
- removeLightSource(index);
|
|
|
+ removeLightSource(location.z, index);
|
|
|
}
|
|
|
if (added)
|
|
|
{
|
|
|
@@ -730,18 +950,20 @@ void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
|
|
|
|
|
|
void Chunk::sendBlockInfo(Framework::Vec3<int> location)
|
|
|
{
|
|
|
- int index = Chunk::index(location);
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
char* msg = new char[14];
|
|
|
msg[0] = 0; // set block
|
|
|
- *(unsigned short*)(msg + 1) = blockIds[index];
|
|
|
- *(int*)(msg + 3) = index;
|
|
|
+ int typeId = blockIds[location.z] ? blockIds[location.z][index] : 0;
|
|
|
+ *(unsigned short*)(msg + 1) = typeId;
|
|
|
+ *(int*)(msg + 3) = index * WORLD_HEIGHT + location.z;
|
|
|
char state = 0;
|
|
|
- const BlockType* type = Game::INSTANCE->zBlockType(blockIds[index]);
|
|
|
+ const BlockType* type = Game::INSTANCE->zBlockType(typeId);
|
|
|
if (type->isFluid())
|
|
|
{
|
|
|
state |= 1;
|
|
|
}
|
|
|
- if ((blocks[index] && blocks[index]->isPassable())
|
|
|
+ if ((blocks[location.z] && blocks[location.z][index]
|
|
|
+ && blocks[location.z][index]->isPassable())
|
|
|
|| (type->zDefault()->isPassable()))
|
|
|
{
|
|
|
state |= 2;
|
|
|
@@ -749,24 +971,25 @@ void Chunk::sendBlockInfo(Framework::Vec3<int> location)
|
|
|
msg[7] = state;
|
|
|
if ((state | 1) == state)
|
|
|
{
|
|
|
- FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(blocks[index]);
|
|
|
+ FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(
|
|
|
+ blocks[location.z] ? blocks[location.z][index] : 0);
|
|
|
msg[8] = fluidBlock ? fluidBlock->getFlowOptions() : 0;
|
|
|
msg[9] = fluidBlock ? fluidBlock->getDistanceToSource() : 0;
|
|
|
}
|
|
|
if ((state | 2) == state)
|
|
|
{
|
|
|
- *(float*)(msg + 10) = blocks[index]
|
|
|
- ? blocks[index]->getSpeedModifier()
|
|
|
+ *(float*)(msg + 10) = blocks[location.z] && blocks[location.z][index]
|
|
|
+ ? blocks[location.z][index]->getSpeedModifier()
|
|
|
: type->zDefault()->getSpeedModifier();
|
|
|
}
|
|
|
NetworkMessage* message = new NetworkMessage();
|
|
|
message->addressChunck(this);
|
|
|
message->setMessage(msg, 14);
|
|
|
notifyObservers(message);
|
|
|
- if (blocks[index])
|
|
|
+ if (blocks[location.z] && blocks[location.z][index])
|
|
|
{
|
|
|
NetworkMessage* message = new NetworkMessage();
|
|
|
- blocks[index]->sendModelInfo(message);
|
|
|
+ blocks[location.z][index]->sendModelInfo(message);
|
|
|
if (message->isEmpty())
|
|
|
{
|
|
|
message->release();
|
|
|
@@ -784,7 +1007,7 @@ void Chunk::sendBlockInfo(Framework::Vec3<int> location)
|
|
|
if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
|
|
|
&& loc.z >= 0 && loc.z < WORLD_HEIGHT)
|
|
|
{
|
|
|
- broadcastLightData(Chunk::index(loc), true);
|
|
|
+ broadcastLightData(loc.z, Chunk::index(loc.x, loc.y), true);
|
|
|
}
|
|
|
else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
|
|
|
{
|
|
|
@@ -808,87 +1031,15 @@ void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
|
|
|
{
|
|
|
cs.lock();
|
|
|
int dirIndex = getDirectionIndex(dir);
|
|
|
- Chunk* old = zNeighbours[dirIndex];
|
|
|
zNeighbours[dirIndex] = zChunk;
|
|
|
- for (int i = 0; i < CHUNK_SIZE; i++)
|
|
|
- {
|
|
|
- for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
- {
|
|
|
- int index = 0;
|
|
|
- int j = 0;
|
|
|
- if (dir == NORTH)
|
|
|
- {
|
|
|
- index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
- j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
- }
|
|
|
- else if (dir == EAST)
|
|
|
- {
|
|
|
- index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
- j = i * WORLD_HEIGHT + z;
|
|
|
- }
|
|
|
- else if (dir == SOUTH)
|
|
|
- {
|
|
|
- index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
- j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
- }
|
|
|
- else if (dir == WEST)
|
|
|
- {
|
|
|
- index = i * WORLD_HEIGHT + z;
|
|
|
- j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
- }
|
|
|
- bool needsTransmission = 0;
|
|
|
- if (blocks[index])
|
|
|
- {
|
|
|
- bool visible = blocks[index]->isVisible();
|
|
|
- if (zChunk && zChunk->blocks[j])
|
|
|
- blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
|
|
|
- else
|
|
|
- {
|
|
|
- blocks[index]->setNeighbour(dir, 0);
|
|
|
- blocks[index]->setNeighbourType(
|
|
|
- dir, zChunk ? zChunk->blockIds[j] : 0);
|
|
|
- }
|
|
|
- if (!visible && blocks[index]->isVisible())
|
|
|
- {
|
|
|
- needsTransmission = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- zNeighbours[dirIndex] = old;
|
|
|
- bool visible = isVisible(index);
|
|
|
- zNeighbours[dirIndex] = zChunk;
|
|
|
- if (!visible && isVisible(index))
|
|
|
- {
|
|
|
- needsTransmission = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- if (zChunk)
|
|
|
- {
|
|
|
- if (!blocks[index])
|
|
|
- {
|
|
|
- if (zChunk->zBlockConst(j)->isTransparent()
|
|
|
- && !blockIds[index])
|
|
|
- {
|
|
|
- generateBlock(Framework::Vec3<int>(
|
|
|
- (index / WORLD_HEIGHT) / CHUNK_SIZE,
|
|
|
- (index / WORLD_HEIGHT) % CHUNK_SIZE,
|
|
|
- index % WORLD_HEIGHT));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (needsTransmission && added)
|
|
|
- {
|
|
|
- sendBlockInfo(
|
|
|
- Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
|
|
|
- (index / WORLD_HEIGHT) % CHUNK_SIZE,
|
|
|
- index % WORLD_HEIGHT));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
cs.unlock();
|
|
|
}
|
|
|
|
|
|
+Chunk* Chunk::zNeighbor(Direction dir) const
|
|
|
+{
|
|
|
+ return zNeighbours[getDirectionIndex(dir)];
|
|
|
+}
|
|
|
+
|
|
|
void Chunk::load(Framework::StreamReader* zReader)
|
|
|
{
|
|
|
for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
|
|
|
@@ -921,135 +1072,100 @@ void Chunk::load(Framework::StreamReader* zReader)
|
|
|
}
|
|
|
}
|
|
|
initializeLightning();
|
|
|
+ unsigned short entityCount;
|
|
|
+ zReader->lese((char*)&entityCount, 2);
|
|
|
+ lastSavedEntityIds.leeren();
|
|
|
+ entitiesInChunk.leeren();
|
|
|
+ for (int i = 0; i < entityCount; i++)
|
|
|
+ {
|
|
|
+ int type;
|
|
|
+ zReader->lese((char*)&type, 4);
|
|
|
+ Entity* entity = Game::INSTANCE->zEntityType(type)->loadEntity(zReader);
|
|
|
+ entitiesInChunk.add(entity);
|
|
|
+ lastSavedEntityIds.add(entity->getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-void Chunk::save(Framework::StreamWriter* zWriter)
|
|
|
+void Chunk::save(Framework::StreamWriter* zWriter,
|
|
|
+ Framework::Array<Framework::Punkt>& otherChunksToSave)
|
|
|
{
|
|
|
- for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
|
|
|
+ for (int id : lastSavedEntityIds)
|
|
|
{
|
|
|
- unsigned short blockType
|
|
|
- = blocks[index]
|
|
|
- ? (unsigned short)blocks[index]->zBlockType()->getId()
|
|
|
- : blockIds[index];
|
|
|
- zWriter->schreibe((char*)&blockType, 2);
|
|
|
- if (blockType)
|
|
|
+ if (!entitiesInChunk.anyMatch(
|
|
|
+ [id](const Entity* e) { return e->getId() == id; }))
|
|
|
{
|
|
|
- if (blocks[index])
|
|
|
- {
|
|
|
- bool d = 1;
|
|
|
- zWriter->schreibe((char*)&d, 1);
|
|
|
- Game::INSTANCE->zBlockType(blockType)->saveBlock(
|
|
|
- blocks[index], zWriter);
|
|
|
- }
|
|
|
- else
|
|
|
+ Entity* old = Game::INSTANCE->zEntity(id);
|
|
|
+ if (old && old->getDimensionId() == getDimensionId()
|
|
|
+ && !old->isRemoved())
|
|
|
{
|
|
|
- bool d = 0;
|
|
|
- zWriter->schreibe((char*)&d, 1);
|
|
|
+ otherChunksToSave.add(Game::getChunkCenter(
|
|
|
+ (int)old->getPosition().x, (int)old->getPosition().y));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-void Chunk::sendToClient(Framework::StreamWriter* zWriter)
|
|
|
-{
|
|
|
- for (int x = 0; x < CHUNK_SIZE; x++)
|
|
|
+ for (int index = 0; index < CHUNK_SIZE * CHUNK_SIZE; index++)
|
|
|
{
|
|
|
- for (int y = 0; y < CHUNK_SIZE; y++)
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
{
|
|
|
- for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ unsigned short blockType = blockIds[z] ? blockIds[z][index] : 0;
|
|
|
+ zWriter->schreibe((char*)&blockType, 2);
|
|
|
+ if (blockType)
|
|
|
{
|
|
|
- int index = Chunk::index({x, y, z});
|
|
|
- const BlockType* type
|
|
|
- = Game::INSTANCE->zBlockType(blockIds[index]);
|
|
|
- if (isVisible(index) && type->doesNeedClientInstance())
|
|
|
+ if (blocks[z] && blocks[z][index])
|
|
|
{
|
|
|
- zWriter->schreibe((char*)&blockIds[index], 2);
|
|
|
- zWriter->schreibe((char*)&index, 4);
|
|
|
- char state = 0;
|
|
|
- if (type->isFluid())
|
|
|
- {
|
|
|
- state |= 1;
|
|
|
- }
|
|
|
- if ((blocks[index] && blocks[index]->isPassable())
|
|
|
- || (type->zDefault()->isPassable()))
|
|
|
- {
|
|
|
- state |= 2;
|
|
|
- }
|
|
|
- zWriter->schreibe((char*)&state, 1);
|
|
|
- if ((state | 1) == state)
|
|
|
- {
|
|
|
- FluidBlock* fluidBlock
|
|
|
- = dynamic_cast<FluidBlock*>(blocks[index]);
|
|
|
- char data
|
|
|
- = fluidBlock ? fluidBlock->getFlowOptions() : 0;
|
|
|
- zWriter->schreibe(&data, 1);
|
|
|
- data = fluidBlock ? fluidBlock->getDistanceToSource()
|
|
|
- : 0;
|
|
|
- zWriter->schreibe(&data, 1);
|
|
|
- }
|
|
|
- if ((state | 2) == state)
|
|
|
- {
|
|
|
- float speedModifier
|
|
|
- = blocks[index]
|
|
|
- ? blocks[index]->getSpeedModifier()
|
|
|
- : type->zDefault()->getSpeedModifier();
|
|
|
- zWriter->schreibe((char*)&speedModifier, 4);
|
|
|
- }
|
|
|
+ bool d = 1;
|
|
|
+ zWriter->schreibe((char*)&d, 1);
|
|
|
+ Game::INSTANCE->zBlockType(blockType)->saveBlock(
|
|
|
+ blocks[z][index], zWriter);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ bool d = 0;
|
|
|
+ zWriter->schreibe((char*)&d, 1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- unsigned short end = 0;
|
|
|
- zWriter->schreibe((char*)&end, 2);
|
|
|
+ unsigned short len = (unsigned short)entitiesInChunk.getEintragAnzahl();
|
|
|
+ zWriter->schreibe((char*)&len, 2);
|
|
|
+ for (Entity* entity : entitiesInChunk)
|
|
|
+ {
|
|
|
+ int type = entity->zType()->getId();
|
|
|
+ zWriter->schreibe((char*)&type, 4);
|
|
|
+ entity->zType()->saveEntity(entity, zWriter);
|
|
|
+ if (lastSavedEntityIds.getWertIndex(entity->getId()) < 0)
|
|
|
+ {
|
|
|
+ entity->getLastSavedChunkCenter().ifPresent(
|
|
|
+ [&otherChunksToSave](
|
|
|
+ Framework::Punkt p) { otherChunksToSave.add(p); });
|
|
|
+ }
|
|
|
+ entity->setLastSavedChunkCenter(getCenter());
|
|
|
+ }
|
|
|
+ lastSavedEntityIds.leeren();
|
|
|
+ for (Entity* entity : entitiesInChunk)
|
|
|
+ {
|
|
|
+ lastSavedEntityIds.add(entity->getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void Chunk::removeUnusedBlocks()
|
|
|
{
|
|
|
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
|
|
|
+ // no longer needed becaus only used blocks are generated in the first place
|
|
|
+#ifdef _DEBUG
|
|
|
+ int count = 0;
|
|
|
+ for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
|
|
|
{
|
|
|
- if (!blocks[i] && blockIds[i])
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
{
|
|
|
- int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
|
|
|
- int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
|
|
|
- int z = i % WORLD_HEIGHT;
|
|
|
- bool visible = 0;
|
|
|
- if (CONST_BLOCK(0, blockIds[i])->isTransparent()
|
|
|
- || CONST_BLOCK(0, blockIds[i])->isPassable())
|
|
|
- visible = 1;
|
|
|
- else
|
|
|
- {
|
|
|
- for (int d = 0; d < 6 && !visible; d++)
|
|
|
- {
|
|
|
- auto n = zBlockNeighbor(
|
|
|
- getDirection((Directions)getDirectionFromIndex(d))
|
|
|
- + Framework::Vec3<int>(x, y, z),
|
|
|
- 0);
|
|
|
- if (n.isA()
|
|
|
- && (((Block*)n)->isPassable()
|
|
|
- || ((Block*)n)->isTransparent()))
|
|
|
- visible = 1;
|
|
|
- if (n.isB()
|
|
|
- && (CONST_BLOCK(0, n)->isTransparent()
|
|
|
- || CONST_BLOCK(0, n)->isPassable()))
|
|
|
- visible = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- if (!visible)
|
|
|
- {
|
|
|
- putBlockAt({x, y, z}, 0);
|
|
|
- putBlockTypeAt({x, y, z}, 0);
|
|
|
- }
|
|
|
+ if (Game::INSTANCE->zBlockType(blockIds[z] ? blockIds[z][i] : 0)
|
|
|
+ ->doesNeedClientInstance())
|
|
|
+ count++;
|
|
|
}
|
|
|
}
|
|
|
- int count = 0;
|
|
|
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
|
|
|
- {
|
|
|
- if (Game::INSTANCE->zBlockType(blockIds[i])->doesNeedClientInstance())
|
|
|
- count++;
|
|
|
- }
|
|
|
Framework::Logging::debug()
|
|
|
<< "chunk " << location.x << ", " << location.y
|
|
|
<< " was generated with " << count << " blocks.";
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
int Chunk::getDimensionId() const
|
|
|
@@ -1059,18 +1175,24 @@ int Chunk::getDimensionId() const
|
|
|
|
|
|
void Chunk::onLoaded()
|
|
|
{
|
|
|
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
|
|
|
+ for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
|
|
|
{
|
|
|
- if (blocks[i]) blocks[i]->onLoaded();
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ {
|
|
|
+ if (blocks[z] && blocks[z][i]) blocks[z][i]->onLoaded();
|
|
|
+ }
|
|
|
}
|
|
|
currentlyLoading = 0;
|
|
|
}
|
|
|
|
|
|
void Chunk::onUnloaded()
|
|
|
{
|
|
|
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
|
|
|
+ for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
|
|
|
{
|
|
|
- if (blocks[i]) blocks[i]->onUnloaded();
|
|
|
+ for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
|
+ {
|
|
|
+ if (blocks[z] && blocks[z][i]) blocks[z][i]->onUnloaded();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1103,6 +1225,10 @@ void Chunk::prepareRemove()
|
|
|
zNeighbours[i] = 0;
|
|
|
}
|
|
|
}
|
|
|
+ for (Entity* entity : entitiesInChunk)
|
|
|
+ {
|
|
|
+ entity->setRemoved();
|
|
|
+ }
|
|
|
cs.unlock();
|
|
|
}
|
|
|
|
|
|
@@ -1118,7 +1244,9 @@ bool Chunk::hasObservers() const
|
|
|
|
|
|
unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
|
|
|
{
|
|
|
- int index = Chunk::index(location) * 6;
|
|
|
+ int index
|
|
|
+ = (Chunk::index(location.x, location.y) * WORLD_HEIGHT + location.z)
|
|
|
+ * 6;
|
|
|
assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
|
|
|
return lightData + index;
|
|
|
}
|
|
|
@@ -1126,8 +1254,8 @@ unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
|
|
|
void Chunk::setLightData(
|
|
|
Framework::Vec3<int> location, unsigned char* data, bool foreground)
|
|
|
{
|
|
|
- int index = Chunk::index(location);
|
|
|
- memcpy(lightData + index * 6, data, 6);
|
|
|
+ int index = Chunk::index(location.x, location.y);
|
|
|
+ memcpy(lightData + (index * WORLD_HEIGHT + location.z) * 6, data, 6);
|
|
|
// check if neighbor is a visible block and send update to clients
|
|
|
bool needSend = 0;
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
@@ -1139,8 +1267,8 @@ void Chunk::setLightData(
|
|
|
if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
|
|
|
&& pos.y < CHUNK_SIZE)
|
|
|
{
|
|
|
- int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
|
|
|
- int type = blockIds[bi];
|
|
|
+ int bi = (pos.x * CHUNK_SIZE + pos.y);
|
|
|
+ int type = blockIds[pos.z] ? blockIds[pos.z][bi] : 0;
|
|
|
needSend |= Game::INSTANCE->zBlockType(type)
|
|
|
->doesNeedClientInstance();
|
|
|
if (needSend) break;
|
|
|
@@ -1162,11 +1290,88 @@ void Chunk::setLightData(
|
|
|
}
|
|
|
if (needSend)
|
|
|
{
|
|
|
- broadcastLightData(index, foreground);
|
|
|
+ broadcastLightData(location.z, index, foreground);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int Chunk::getBlockTypeAt(Framework::Vec3<int> location) const
|
|
|
{
|
|
|
- return blockIds[index(location)];
|
|
|
-}
|
|
|
+ return blockIds[location.z]
|
|
|
+ ? blockIds[location.z][index(location.x, location.y)]
|
|
|
+ : 0;
|
|
|
+}
|
|
|
+
|
|
|
+int Chunk::getBlockTypeAtWC(int x, int y, int z) const
|
|
|
+{
|
|
|
+ auto pos = Dimension::chunkCoordinates({x, y, z});
|
|
|
+ return blockIds[pos.z] ? blockIds[pos.z][index(pos.x, pos.y)] : 0;
|
|
|
+}
|
|
|
+
|
|
|
+void Chunk::onEntityEnters(Entity* zEntity, Chunk* lastChunk)
|
|
|
+{
|
|
|
+ if (zEntity->zType()->getId() != EntityTypeEnum::PLAYER)
|
|
|
+ {
|
|
|
+ this->entitiesInChunk.add(dynamic_cast<Entity*>(zEntity->getThis()));
|
|
|
+ }
|
|
|
+ NetworkMessage* msg = 0;
|
|
|
+ for (InformationObserver* observer : observers)
|
|
|
+ {
|
|
|
+ if (!lastChunk || !lastChunk->hasObserver(zEntity->getId()))
|
|
|
+ {
|
|
|
+ if (!msg)
|
|
|
+ {
|
|
|
+ msg = new NetworkMessage();
|
|
|
+ msg->addEntityMessage(zEntity);
|
|
|
+ if (msg->isEmpty()) break;
|
|
|
+ }
|
|
|
+ observer->sendMessage(
|
|
|
+ dynamic_cast<NetworkMessage*>(msg->getThis()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (msg) msg->release();
|
|
|
+}
|
|
|
+
|
|
|
+void Chunk::onEntityLeaves(Entity* zEntity, Chunk* zNextChunk)
|
|
|
+{
|
|
|
+ if (zEntity->zType()->getId() != EntityTypeEnum::PLAYER)
|
|
|
+ {
|
|
|
+ this->entitiesInChunk.remove(this->entitiesInChunk.indexOf(zEntity));
|
|
|
+ }
|
|
|
+ NetworkMessage* msg = 0;
|
|
|
+ for (InformationObserver* observer : observers)
|
|
|
+ {
|
|
|
+ if (!zNextChunk || !zNextChunk->hasObserver(zEntity->getId()))
|
|
|
+ {
|
|
|
+ if (!msg)
|
|
|
+ {
|
|
|
+ msg = new NetworkMessage();
|
|
|
+ msg->removeEntityMessage(zEntity);
|
|
|
+ if (msg->isEmpty()) break;
|
|
|
+ }
|
|
|
+ observer->sendMessage(
|
|
|
+ dynamic_cast<NetworkMessage*>(msg->getThis()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (msg) msg->release();
|
|
|
+}
|
|
|
+
|
|
|
+bool Chunk::hasObserver(int entityId) const
|
|
|
+{
|
|
|
+ for (InformationObserver* observer : observers)
|
|
|
+ {
|
|
|
+ if (observer->getEntityId() == entityId) return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+void Chunk::addGeneratedEntity(Entity* entity)
|
|
|
+{
|
|
|
+ entitiesInChunk.add(entity);
|
|
|
+ lastSavedEntityIds.add(entity->getId());
|
|
|
+ entity->setLastSavedChunkCenter(getCenter());
|
|
|
+}
|
|
|
+
|
|
|
+const Framework::RCArray<Entity>& Chunk::getEntitiesInChunk() const
|
|
|
+{
|
|
|
+ return entitiesInChunk;
|
|
|
+}
|