|
@@ -1,8 +1,7 @@
|
|
-#include "Chunk.h"
|
|
|
|
-
|
|
|
|
#include <AsynchronCall.h>
|
|
#include <AsynchronCall.h>
|
|
#include <InMemoryBuffer.h>
|
|
#include <InMemoryBuffer.h>
|
|
|
|
|
|
|
|
+#include "Chunk.h"
|
|
#include "Constants.h"
|
|
#include "Constants.h"
|
|
#include "Game.h"
|
|
#include "Game.h"
|
|
#include "NoBlock.h"
|
|
#include "NoBlock.h"
|
|
@@ -80,10 +79,14 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
{
|
|
{
|
|
for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
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++)
|
|
{
|
|
{
|
|
|
|
+ if ((x < 0 || x == CHUNK_SIZE) && (y < 0 || y > CHUNK_SIZE))
|
|
|
|
+ {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
bool needSend = 0;
|
|
bool needSend = 0;
|
|
for (int i = 0; i < 6; i++)
|
|
for (int i = 0; i < 6; i++)
|
|
{
|
|
{
|
|
@@ -102,18 +105,22 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- if (i < 4 && zNeighbours[i])
|
|
|
|
|
|
+ if (x >= 0 && x < CHUNK_SIZE && y >= 0
|
|
|
|
+ && y < CHUNK_SIZE)
|
|
{
|
|
{
|
|
- 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 |= type != BlockTypeEnum::NO_BLOCK
|
|
|
|
- && type != BlockTypeEnum::AIR;
|
|
|
|
|
|
+ if (i < 4 && zNeighbours[i])
|
|
|
|
+ {
|
|
|
|
+ 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 |= type != BlockTypeEnum::NO_BLOCK
|
|
|
|
+ && type != BlockTypeEnum::AIR;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (needSend) break;
|
|
if (needSend) break;
|
|
@@ -121,17 +128,79 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
}
|
|
}
|
|
if (needSend)
|
|
if (needSend)
|
|
{
|
|
{
|
|
- int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
|
|
|
|
- zWriter->schreibe((char*)&index, 4);
|
|
|
|
- zWriter->schreibe((char*)(lightData + index * 6), 6);
|
|
|
|
|
|
+ if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE)
|
|
|
|
+ {
|
|
|
|
+ int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
|
|
|
|
+ zWriter->schreibe((char*)&index, 4);
|
|
|
|
+ zWriter->schreibe((char*)(lightData + index * 6), 6);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ int dir;
|
|
|
|
+ int index = 0;
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+ if (zNeighbours[dir])
|
|
|
|
+ {
|
|
|
|
+ int i = -1;
|
|
|
|
+ zWriter->schreibe((char*)&i, 4);
|
|
|
|
+ zWriter->schreibe((char*)&x, 4);
|
|
|
|
+ zWriter->schreibe((char*)&y, 4);
|
|
|
|
+ zWriter->schreibe((char*)&z, 4);
|
|
|
|
+ zWriter->schreibe(
|
|
|
|
+ (char*)(zNeighbours[dir]->lightData + index * 6),
|
|
|
|
+ 6);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- int end = -1;
|
|
|
|
|
|
+ int end = -2;
|
|
zWriter->schreibe((char*)&end, 4);
|
|
zWriter->schreibe((char*)&end, 4);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Chunk::broadcastLightData(int index, bool foreground)
|
|
|
|
+{
|
|
|
|
+ int x = (index / WORLD_HEIGHT) / CHUNK_SIZE;
|
|
|
|
+ int y = (index / WORLD_HEIGHT) % CHUNK_SIZE;
|
|
|
|
+ int z = index % WORLD_HEIGHT;
|
|
|
|
+ NetworkMessage* msg = new NetworkMessage();
|
|
|
|
+ msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
|
|
+ char* message = new char[19];
|
|
|
|
+ message[0] = 5;
|
|
|
|
+ *(int*)(message + 1) = x;
|
|
|
|
+ *(int*)(message + 5) = y;
|
|
|
|
+ *(int*)(message + 9) = z;
|
|
|
|
+ memcpy(message + 13, lightData + index, 6);
|
|
|
|
+ msg->setMessage(message, 19);
|
|
|
|
+ if (!foreground) msg->setUseBackground();
|
|
|
|
+ notifyObservers(msg);
|
|
|
|
+}
|
|
|
|
+
|
|
Framework::Either<Block*, int> Chunk::zBlockNeighbor(
|
|
Framework::Either<Block*, int> Chunk::zBlockNeighbor(
|
|
Framework::Vec3<int> location)
|
|
Framework::Vec3<int> location)
|
|
{
|
|
{
|
|
@@ -190,7 +259,7 @@ void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
|
|
sendToClient(&buffer);
|
|
sendToClient(&buffer);
|
|
sendLightToClient(&buffer);
|
|
sendLightToClient(&buffer);
|
|
NetworkMessage* msg = new NetworkMessage();
|
|
NetworkMessage* msg = new NetworkMessage();
|
|
- msg->addressDimension();
|
|
|
|
|
|
+ msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
std::cout << "chunk size: " << buffer.getSize() << "b\n";
|
|
std::cout << "chunk size: " << buffer.getSize() << "b\n";
|
|
char* message = new char[buffer.getSize()];
|
|
char* message = new char[buffer.getSize()];
|
|
buffer.lese(message, (int)buffer.getSize());
|
|
buffer.lese(message, (int)buffer.getSize());
|
|
@@ -376,8 +445,7 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
Block* old = blocks[index];
|
|
Block* old = blocks[index];
|
|
if (old && old->isTickSource())
|
|
if (old && old->isTickSource())
|
|
{ // remove from tick sorces
|
|
{ // remove from tick sorces
|
|
- for (Framework::Iterator<Block*> obj = tickSources.begin(); obj;
|
|
|
|
- obj++)
|
|
|
|
|
|
+ for (Framework::Iterator<Block*> obj = tickSources.begin(); obj; obj++)
|
|
{
|
|
{
|
|
if (obj.val() == old)
|
|
if (obj.val() == old)
|
|
{
|
|
{
|
|
@@ -572,103 +640,57 @@ void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
|
|
{
|
|
{
|
|
for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
{
|
|
{
|
|
|
|
+ int index = 0;
|
|
|
|
+ int j = 0;
|
|
if (dir == NORTH)
|
|
if (dir == NORTH)
|
|
{
|
|
{
|
|
- int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
|
- int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
|
- if (blocks[index])
|
|
|
|
- {
|
|
|
|
- if (zChunk && zChunk->blocks[j])
|
|
|
|
- blocks[index]->setNeighbour(NORTH, zChunk->blocks[j]);
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- blocks[index]->setNeighbour(NORTH, 0);
|
|
|
|
- blocks[index]->setNeighbourType(
|
|
|
|
- NORTH, zChunk ? zChunk->blockIds[j] : 0);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else if (zChunk)
|
|
|
|
- {
|
|
|
|
- if (zChunk->blockIds[j] == BlockTypeEnum::AIR
|
|
|
|
- && !blockIds[index])
|
|
|
|
- {
|
|
|
|
- generateBlock(Vec3<int>(i, 0, z));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
|
+ j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
}
|
|
}
|
|
else if (dir == EAST)
|
|
else if (dir == EAST)
|
|
{
|
|
{
|
|
- int index
|
|
|
|
- = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
|
- int j = i * WORLD_HEIGHT + z;
|
|
|
|
- if (blocks[index])
|
|
|
|
- {
|
|
|
|
- if (zChunk && zChunk->blocks[j])
|
|
|
|
- blocks[index]->setNeighbour(EAST, zChunk->blocks[j]);
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- blocks[index]->setNeighbour(EAST, 0);
|
|
|
|
- blocks[index]->setNeighbourType(
|
|
|
|
- EAST, zChunk ? zChunk->blockIds[j] : 0);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else if (zChunk)
|
|
|
|
- {
|
|
|
|
- if (zChunk->blockIds[j] == BlockTypeEnum::AIR
|
|
|
|
- && !blockIds[index])
|
|
|
|
- {
|
|
|
|
- generateBlock(Vec3<int>(CHUNK_SIZE - 1, i, z));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
|
+ j = i * WORLD_HEIGHT + z;
|
|
}
|
|
}
|
|
else if (dir == SOUTH)
|
|
else if (dir == SOUTH)
|
|
{
|
|
{
|
|
- int index
|
|
|
|
- = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
|
- int j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
|
- if (blocks[index])
|
|
|
|
- {
|
|
|
|
- if (zChunk && zChunk->blocks[j])
|
|
|
|
- blocks[index]->setNeighbour(SOUTH, zChunk->blocks[j]);
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- blocks[index]->setNeighbour(SOUTH, 0);
|
|
|
|
- blocks[index]->setNeighbourType(
|
|
|
|
- SOUTH, zChunk ? zChunk->blockIds[j] : 0);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else if (zChunk)
|
|
|
|
- {
|
|
|
|
- if (zChunk->blockIds[j] == BlockTypeEnum::AIR
|
|
|
|
- && !blockIds[index])
|
|
|
|
- {
|
|
|
|
- generateBlock(Vec3<int>(i, CHUNK_SIZE - 1, z));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
|
+ j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
}
|
|
}
|
|
else if (dir == WEST)
|
|
else if (dir == WEST)
|
|
{
|
|
{
|
|
- int index = i * WORLD_HEIGHT + z;
|
|
|
|
- int j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
|
- if (blocks[index])
|
|
|
|
|
|
+ index = i * WORLD_HEIGHT + z;
|
|
|
|
+ j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
|
+ }
|
|
|
|
+ if (blocks[index])
|
|
|
|
+ {
|
|
|
|
+ if (zChunk && zChunk->blocks[j])
|
|
|
|
+ blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
|
|
|
|
+ else
|
|
{
|
|
{
|
|
- if (zChunk && zChunk->blocks[j])
|
|
|
|
- blocks[index]->setNeighbour(WEST, zChunk->blocks[j]);
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- blocks[index]->setNeighbour(WEST, 0);
|
|
|
|
- blocks[index]->setNeighbourType(
|
|
|
|
- WEST, zChunk ? zChunk->blockIds[j] : 0);
|
|
|
|
- }
|
|
|
|
|
|
+ blocks[index]->setNeighbour(dir, 0);
|
|
|
|
+ blocks[index]->setNeighbourType(
|
|
|
|
+ dir, zChunk ? zChunk->blockIds[j] : 0);
|
|
}
|
|
}
|
|
- else if (zChunk)
|
|
|
|
|
|
+ }
|
|
|
|
+ if (zChunk)
|
|
|
|
+ {
|
|
|
|
+ if (!blocks[index])
|
|
{
|
|
{
|
|
if (zChunk->blockIds[j] == BlockTypeEnum::AIR
|
|
if (zChunk->blockIds[j] == BlockTypeEnum::AIR
|
|
&& !blockIds[index])
|
|
&& !blockIds[index])
|
|
{
|
|
{
|
|
- generateBlock(Vec3<int>(0, i, z));
|
|
|
|
|
|
+ generateBlock(
|
|
|
|
+ Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
|
|
|
|
+ (index / WORLD_HEIGHT) % CHUNK_SIZE,
|
|
|
|
+ index % WORLD_HEIGHT));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ if (blockIds[index] != BlockTypeEnum::AIR
|
|
|
|
+ && blockIds[index] != BlockTypeEnum::NO_BLOCK)
|
|
|
|
+ {
|
|
|
|
+ zChunk->broadcastLightData(j, true);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -943,14 +965,6 @@ void Chunk::setLightData(
|
|
}
|
|
}
|
|
if (needSend)
|
|
if (needSend)
|
|
{
|
|
{
|
|
- NetworkMessage* msg = new NetworkMessage();
|
|
|
|
- msg->addressChunck(this);
|
|
|
|
- char* message = new char[11];
|
|
|
|
- message[0] = 1;
|
|
|
|
- *(int*)(message + 1) = index / 6;
|
|
|
|
- memcpy(message + 5, data, 6);
|
|
|
|
- msg->setMessage(message, 11);
|
|
|
|
- if (!foreground) msg->setUseBackground();
|
|
|
|
- notifyObservers(msg);
|
|
|
|
|
|
+ broadcastLightData(index, foreground);
|
|
}
|
|
}
|
|
}
|
|
}
|