|
@@ -46,6 +46,16 @@ Chunk::~Chunk()
|
|
delete[] lightData;
|
|
delete[] lightData;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Chunk::lock()
|
|
|
|
+{
|
|
|
|
+ cs.lock();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Chunk::unlock()
|
|
|
|
+{
|
|
|
|
+ cs.unlock();
|
|
|
|
+}
|
|
|
|
+
|
|
void Chunk::tick(TickQueue* zQueue)
|
|
void Chunk::tick(TickQueue* zQueue)
|
|
{
|
|
{
|
|
for (Block* source : tickSources)
|
|
for (Block* source : tickSources)
|
|
@@ -192,10 +202,10 @@ void Chunk::broadcastLightData(int index, bool foreground)
|
|
msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
char* message = new char[19];
|
|
char* message = new char[19];
|
|
message[0] = 5;
|
|
message[0] = 5;
|
|
- *(int*)(message + 1) = x;
|
|
|
|
- *(int*)(message + 5) = y;
|
|
|
|
|
|
+ *(int*)(message + 1) = x + this->location.x - CHUNK_SIZE / 2;
|
|
|
|
+ *(int*)(message + 5) = y + this->location.y - CHUNK_SIZE / 2;
|
|
*(int*)(message + 9) = z;
|
|
*(int*)(message + 9) = z;
|
|
- memcpy(message + 13, lightData + index, 6);
|
|
|
|
|
|
+ memcpy(message + 13, lightData + index * 6, 6);
|
|
msg->setMessage(message, 19);
|
|
msg->setMessage(message, 19);
|
|
if (!foreground) msg->setUseBackground();
|
|
if (!foreground) msg->setUseBackground();
|
|
notifyObservers(msg);
|
|
notifyObservers(msg);
|
|
@@ -469,6 +479,10 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
|
|
+ if (old != 0)
|
|
|
|
+ {
|
|
|
|
+ blockIds[index] = BlockTypeEnum::NO_BLOCK;
|
|
|
|
+ }
|
|
change = old != 0;
|
|
change = old != 0;
|
|
}
|
|
}
|
|
blocks[index] = block;
|
|
blocks[index] = block;
|
|
@@ -478,7 +492,18 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
Either<Block*, int> neighbor
|
|
Either<Block*, int> neighbor
|
|
= zBlockNeighbor(location + getDirection(d));
|
|
= zBlockNeighbor(location + getDirection(d));
|
|
if (neighbor.isA())
|
|
if (neighbor.isA())
|
|
- ((Block*)neighbor)->setNeighbour(getOppositeDirection(d), block);
|
|
|
|
|
|
+ {
|
|
|
|
+ if (block)
|
|
|
|
+ {
|
|
|
|
+ ((Block*)neighbor)
|
|
|
|
+ ->setNeighbour(getOppositeDirection(d), block);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ ((Block*)neighbor)
|
|
|
|
+ ->setNeighbour(getOppositeDirection(d), blockIds[index]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (block) block->setNeighbour(d, neighbor);
|
|
if (block) block->setNeighbour(d, neighbor);
|
|
}
|
|
}
|
|
if (old) old->release();
|
|
if (old) old->release();
|
|
@@ -497,55 +522,14 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
}
|
|
}
|
|
if (added)
|
|
if (added)
|
|
{
|
|
{
|
|
- char* msg = new char[9];
|
|
|
|
- msg[0] = 0; // set block
|
|
|
|
- *(int*)(msg + 1) = index;
|
|
|
|
- *(int*)(msg + 5) = block ? block->zBlockType()->getId()
|
|
|
|
- : BlockTypeEnum::NO_BLOCK;
|
|
|
|
- NetworkMessage* message = new NetworkMessage();
|
|
|
|
- message->addressChunck(this);
|
|
|
|
- message->setMessage(msg, 9);
|
|
|
|
- notifyObservers(message);
|
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
|
|
|
+ sendBlockInfo(location);
|
|
|
|
+ if (block)
|
|
{
|
|
{
|
|
- Direction d = getDirectionFromIndex(i);
|
|
|
|
- Framework::Vec3<int> loc = location + getDirection(d);
|
|
|
|
- if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0
|
|
|
|
- && loc.y < CHUNK_SIZE && loc.z >= 0 && loc.z < WORLD_HEIGHT)
|
|
|
|
- {
|
|
|
|
- NetworkMessage* msg = new NetworkMessage();
|
|
|
|
- msg->addressChunck(this);
|
|
|
|
- char* message = new char[11];
|
|
|
|
- message[0] = 1;
|
|
|
|
- int index
|
|
|
|
- = ((loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z)
|
|
|
|
- * 6;
|
|
|
|
- *(int*)(message + 1) = index / 6;
|
|
|
|
- memcpy(message + 5, lightData + index, 6);
|
|
|
|
- msg->setMessage(message, 11);
|
|
|
|
- notifyObservers(msg);
|
|
|
|
- }
|
|
|
|
- else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4
|
|
|
|
- && zNeighbours[i])
|
|
|
|
- {
|
|
|
|
- NetworkMessage* msg = new NetworkMessage();
|
|
|
|
- msg->addressChunck(zNeighbours[i]);
|
|
|
|
- char* message = new char[11];
|
|
|
|
- message[0] = 1;
|
|
|
|
- loc -= getDirection(d) * CHUNK_SIZE;
|
|
|
|
- int index
|
|
|
|
- = ((loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z)
|
|
|
|
- * 6;
|
|
|
|
- *(int*)(message + 1) = index / 6;
|
|
|
|
- memcpy(message + 5, zNeighbours[i]->getLightData(loc), 6);
|
|
|
|
- msg->setMessage(message, 11);
|
|
|
|
- notifyObservers(msg);
|
|
|
|
- }
|
|
|
|
|
|
+ Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
|
|
|
|
+ Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
|
|
|
|
+ location.y + this->location.y - CHUNK_SIZE / 2,
|
|
|
|
+ location.z));
|
|
}
|
|
}
|
|
- Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
|
|
|
|
- Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
|
|
|
|
- location.y + this->location.y - CHUNK_SIZE / 2,
|
|
|
|
- location.z));
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -581,50 +565,7 @@ void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
|
|
}
|
|
}
|
|
if (added)
|
|
if (added)
|
|
{
|
|
{
|
|
- char* msg = new char[9];
|
|
|
|
- msg[0] = 0; // set block
|
|
|
|
- *(int*)(msg + 1) = index;
|
|
|
|
- *(int*)(msg + 5) = type;
|
|
|
|
- NetworkMessage* message = new NetworkMessage();
|
|
|
|
- message->addressChunck(this);
|
|
|
|
- message->setMessage(msg, 9);
|
|
|
|
- notifyObservers(message);
|
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
|
- {
|
|
|
|
- Direction d = getDirectionFromIndex(i);
|
|
|
|
- Framework::Vec3<int> loc = location + getDirection(d);
|
|
|
|
- if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0
|
|
|
|
- && loc.y < CHUNK_SIZE && loc.z >= 0 && loc.z < WORLD_HEIGHT)
|
|
|
|
- {
|
|
|
|
- NetworkMessage* msg = new NetworkMessage();
|
|
|
|
- msg->addressChunck(this);
|
|
|
|
- char* message = new char[11];
|
|
|
|
- message[0] = 1;
|
|
|
|
- int index
|
|
|
|
- = ((loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z)
|
|
|
|
- * 6;
|
|
|
|
- *(int*)(message + 1) = index / 6;
|
|
|
|
- memcpy(message + 5, lightData + index, 6);
|
|
|
|
- msg->setMessage(message, 11);
|
|
|
|
- notifyObservers(msg);
|
|
|
|
- }
|
|
|
|
- else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4
|
|
|
|
- && zNeighbours[i])
|
|
|
|
- {
|
|
|
|
- NetworkMessage* msg = new NetworkMessage();
|
|
|
|
- msg->addressChunck(zNeighbours[i]);
|
|
|
|
- char* message = new char[11];
|
|
|
|
- message[0] = 1;
|
|
|
|
- loc -= getDirection(d) * CHUNK_SIZE;
|
|
|
|
- int index
|
|
|
|
- = ((loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z)
|
|
|
|
- * 6;
|
|
|
|
- *(int*)(message + 1) = index / 6;
|
|
|
|
- memcpy(message + 5, zNeighbours[i]->getLightData(loc), 6);
|
|
|
|
- msg->setMessage(message, 11);
|
|
|
|
- notifyObservers(msg);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ sendBlockInfo(location);
|
|
Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
|
|
Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
|
|
Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
|
|
Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
|
|
location.y + this->location.y - CHUNK_SIZE / 2,
|
|
location.y + this->location.y - CHUNK_SIZE / 2,
|
|
@@ -633,6 +574,46 @@ void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Chunk::sendBlockInfo(Framework::Vec3<int> location)
|
|
|
|
+{
|
|
|
|
+ int index
|
|
|
|
+ = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
|
+ char* msg = new char[9];
|
|
|
|
+ msg[0] = 0; // set block
|
|
|
|
+ *(int*)(msg + 1) = index;
|
|
|
|
+ *(int*)(msg + 5) = blockIds[index];
|
|
|
|
+ NetworkMessage* message = new NetworkMessage();
|
|
|
|
+ message->addressChunck(this);
|
|
|
|
+ message->setMessage(msg, 9);
|
|
|
|
+ notifyObservers(message);
|
|
|
|
+ for (int i = 0; i < 6; i++)
|
|
|
|
+ {
|
|
|
|
+ Direction d = getDirectionFromIndex(i);
|
|
|
|
+ Framework::Vec3<int> loc = location + getDirection(d);
|
|
|
|
+ if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
|
|
|
|
+ && loc.z >= 0 && loc.z < WORLD_HEIGHT)
|
|
|
|
+ {
|
|
|
|
+ int index
|
|
|
|
+ = (loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z;
|
|
|
|
+ broadcastLightData(index, true);
|
|
|
|
+ }
|
|
|
|
+ else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
|
|
|
|
+ {
|
|
|
|
+ NetworkMessage* msg = new NetworkMessage();
|
|
|
|
+ msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
|
|
|
|
+ char* message = new char[19];
|
|
|
|
+ message[0] = 5;
|
|
|
|
+ *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
|
|
|
|
+ *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
|
|
|
|
+ *(int*)(message + 9) = loc.z;
|
|
|
|
+ loc -= getDirection(d) * CHUNK_SIZE;
|
|
|
|
+ memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
|
|
|
|
+ msg->setMessage(message, 19);
|
|
|
|
+ notifyObservers(msg);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
|
|
void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
|
|
{
|
|
{
|
|
zNeighbours[getDirectionIndex(dir)] = zChunk;
|
|
zNeighbours[getDirectionIndex(dir)] = zChunk;
|
|
@@ -785,10 +766,12 @@ void Chunk::sendToClient(Framework::StreamWriter* zWriter)
|
|
{
|
|
{
|
|
for (int d = 0; d < 6 && !visible; d++)
|
|
for (int d = 0; d < 6 && !visible; d++)
|
|
{
|
|
{
|
|
- auto n = zBlockNeighbor(
|
|
|
|
- getDirection((
|
|
|
|
- Directions)getDirectionFromIndex(d))
|
|
|
|
- + Framework::Vec3<int>(x, y, z));
|
|
|
|
|
|
+ Vec3<int> pos
|
|
|
|
+ = getDirection(
|
|
|
|
+ (Directions)getDirectionFromIndex(
|
|
|
|
+ d))
|
|
|
|
+ + Framework::Vec3<int>(x, y, z);
|
|
|
|
+ auto n = zBlockNeighbor(pos);
|
|
if (n.isA()
|
|
if (n.isA()
|
|
&& (((Block*)n)->isPassable()
|
|
&& (((Block*)n)->isPassable()
|
|
|| ((Block*)n)->isTransparent()))
|
|
|| ((Block*)n)->isTransparent()))
|
|
@@ -797,6 +780,11 @@ void Chunk::sendToClient(Framework::StreamWriter* zWriter)
|
|
&& (CONST_BLOCK(0, n)->isTransparent()
|
|
&& (CONST_BLOCK(0, n)->isTransparent()
|
|
|| CONST_BLOCK(0, n)->isPassable()))
|
|
|| CONST_BLOCK(0, n)->isPassable()))
|
|
visible = 1;
|
|
visible = 1;
|
|
|
|
+ if (pos.x < 0 || pos.y < 0 || pos.z < 0
|
|
|
|
+ || pos.x >= CHUNK_SIZE
|
|
|
|
+ || pos.y >= CHUNK_SIZE
|
|
|
|
+ || pos.z >= WORLD_HEIGHT)
|
|
|
|
+ visible = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -850,7 +838,6 @@ void Chunk::removeUnusedBlocks()
|
|
if (!visible)
|
|
if (!visible)
|
|
{
|
|
{
|
|
putBlockAt({x, y, z}, 0);
|
|
putBlockAt({x, y, z}, 0);
|
|
- putBlockTypeAt({x, y, z}, BlockTypeEnum::NO_BLOCK);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|