|
@@ -121,7 +121,7 @@ Framework::Either<Block*, int> Chunk::zBlockNeighbor(Framework::Vec3<int> locati
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-void Chunk::notifyObservers(NetworkMessage& msg)
|
|
|
+void Chunk::notifyObservers(NetworkMessage* msg)
|
|
|
{
|
|
|
Array<int> remove;
|
|
|
int index = 0;
|
|
@@ -131,11 +131,12 @@ void Chunk::notifyObservers(NetworkMessage& msg)
|
|
|
if (!zE)
|
|
|
remove.add(index, 0);
|
|
|
else
|
|
|
- Game::INSTANCE->sendMessage(&msg, zE);
|
|
|
+ Game::INSTANCE->sendMessage(dynamic_cast<NetworkMessage*>(msg->getThis()), zE);
|
|
|
index++;
|
|
|
}
|
|
|
for (int i : remove)
|
|
|
observers.remove(i);
|
|
|
+ msg->release();
|
|
|
}
|
|
|
|
|
|
void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
|
|
@@ -155,17 +156,19 @@ void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
|
|
|
buffer.schreibe((char*)&location.y, 4);
|
|
|
sendToClient(&buffer);
|
|
|
sendLightToClient(&buffer);
|
|
|
- NetworkMessage msg;
|
|
|
- msg.addressDimension();
|
|
|
+ NetworkMessage* msg = new NetworkMessage();
|
|
|
+ msg->addressDimension();
|
|
|
char* message = new char[buffer.getSize()];
|
|
|
buffer.lese(message, (int)buffer.getSize());
|
|
|
- msg.setMessage(message, (int)buffer.getSize(), 1);
|
|
|
- msg.setUseBackground();
|
|
|
+ msg->setMessage(message, (int)buffer.getSize());
|
|
|
+ msg->setUseBackground();
|
|
|
Entity* e = Game::INSTANCE->zEntity(id);
|
|
|
if (e)
|
|
|
{
|
|
|
- Game::INSTANCE->sendMessage(&msg, e);
|
|
|
+ Game::INSTANCE->sendMessage(msg, e);
|
|
|
}
|
|
|
+ else
|
|
|
+ msg->release();
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -325,36 +328,15 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
change = old != 0;
|
|
|
}
|
|
|
blocks[index] = block;
|
|
|
- Either<Block*, int> neighbor = zBlockNeighbor(location + getDirection(NORTH));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbour(SOUTH, block);
|
|
|
- if (block)
|
|
|
- block->setNeighbour(NORTH, neighbor);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(EAST));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbour(WEST, block);
|
|
|
- if (block)
|
|
|
- block->setNeighbour(EAST, neighbor);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(SOUTH));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbour(NORTH, block);
|
|
|
- if (block)
|
|
|
- block->setNeighbour(SOUTH, neighbor);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(WEST));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbour(EAST, block);
|
|
|
- if (block)
|
|
|
- block->setNeighbour(WEST, neighbor);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(TOP));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbour(BOTTOM, block);
|
|
|
- if (block)
|
|
|
- block->setNeighbour(TOP, neighbor);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(BOTTOM));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbour(TOP, block);
|
|
|
- if (block)
|
|
|
- block->setNeighbour(BOTTOM, neighbor);
|
|
|
+ for (int i = 0; i < 6; i++)
|
|
|
+ {
|
|
|
+ Direction d = getDirectionFromIndex(i);
|
|
|
+ Either<Block*, int> neighbor = zBlockNeighbor(location + getDirection(d));
|
|
|
+ if (neighbor.isA())
|
|
|
+ ((Block*)neighbor)->setNeighbour(getOppositeDirection(d), block);
|
|
|
+ if (block)
|
|
|
+ block->setNeighbour(d, neighbor);
|
|
|
+ }
|
|
|
if (old)
|
|
|
old->release();
|
|
|
if (change)
|
|
@@ -368,15 +350,45 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
|
|
|
}
|
|
|
if (added)
|
|
|
{
|
|
|
- char msg[9];
|
|
|
+ char* msg = new char[9];
|
|
|
msg[0] = 0; // set block
|
|
|
*(int*)(msg + 1) = index;
|
|
|
*(int*)(msg + 5) = block ? block->zBlockType()->getId() : NoBlockBlockType::ID;
|
|
|
- NetworkMessage message;
|
|
|
- message.addressChunck(this);
|
|
|
- message.setMessage(msg, 9, 0);
|
|
|
+ NetworkMessage* message = new NetworkMessage();
|
|
|
+ message->addressChunck(this);
|
|
|
+ message->setMessage(msg, 9);
|
|
|
notifyObservers(message);
|
|
|
- Game::INSTANCE->updateLightning(getDimensionId(), Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z));
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Game::INSTANCE->updateLightningWithoutWait(getDimensionId(), Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -390,24 +402,13 @@ void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
|
|
|
if (blockIds[index] != (unsigned short)type)
|
|
|
{
|
|
|
blockIds[index] = (unsigned short)type;
|
|
|
- Either<Block*, int> neighbor = zBlockNeighbor(location + getDirection(NORTH));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbourType(SOUTH, type);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(EAST));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbourType(WEST, type);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(SOUTH));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbourType(NORTH, type);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(WEST));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbourType(EAST, type);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(TOP));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbourType(BOTTOM, type);
|
|
|
- neighbor = zBlockNeighbor(location + getDirection(BOTTOM));
|
|
|
- if (neighbor.isA())
|
|
|
- ((Block*)neighbor)->setNeighbourType(TOP, type);
|
|
|
+ for (int i = 0; i < 6; i++)
|
|
|
+ {
|
|
|
+ Direction d = getDirectionFromIndex(i);
|
|
|
+ Either<Block*, int> neighbor = zBlockNeighbor(location + getDirection(d));
|
|
|
+ if (neighbor.isA())
|
|
|
+ ((Block*)neighbor)->setNeighbourType(getOppositeDirection(d), type);
|
|
|
+ }
|
|
|
if (isLightSource != wasLightSource)
|
|
|
{
|
|
|
if (isLightSource)
|
|
@@ -417,15 +418,45 @@ void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
|
|
|
}
|
|
|
if (added)
|
|
|
{
|
|
|
- char msg[9];
|
|
|
+ char* msg = new char[9];
|
|
|
msg[0] = 0; // set block
|
|
|
*(int*)(msg + 1) = index;
|
|
|
*(int*)(msg + 5) = type;
|
|
|
- NetworkMessage message;
|
|
|
- message.addressChunck(this);
|
|
|
- message.setMessage(msg, 9, 0);
|
|
|
+ NetworkMessage* message = new NetworkMessage();
|
|
|
+ message->addressChunck(this);
|
|
|
+ message->setMessage(msg, 9);
|
|
|
notifyObservers(message);
|
|
|
- Game::INSTANCE->updateLightning(getDimensionId(), Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z));
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Game::INSTANCE->updateLightningWithoutWait(getDimensionId(), Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -731,14 +762,14 @@ void Chunk::setLightData(Framework::Vec3<int> location, unsigned char* data)
|
|
|
}
|
|
|
if (needSend)
|
|
|
{
|
|
|
- NetworkMessage msg;
|
|
|
- msg.addressChunck(this);
|
|
|
- char message[11];
|
|
|
+ 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, 0);
|
|
|
- msg.setUseBackground();
|
|
|
+ msg->setMessage(message, 11);
|
|
|
+ msg->setUseBackground();
|
|
|
notifyObservers(msg);
|
|
|
}
|
|
|
}
|