|
@@ -1,4 +1,5 @@
|
|
#include <InMemoryBuffer.h>
|
|
#include <InMemoryBuffer.h>
|
|
|
|
+#include <AsynchronCall.h>
|
|
|
|
|
|
#include "Chunk.h"
|
|
#include "Chunk.h"
|
|
#include "Constants.h"
|
|
#include "Constants.h"
|
|
@@ -64,7 +65,7 @@ void Chunk::removeLightSource(int index)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void Chunk::sendLightToClient(Entity* zPlayer)
|
|
|
|
|
|
+void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
|
|
{
|
|
{
|
|
for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
for (int z = 0; z < WORLD_HEIGHT; z++)
|
|
{
|
|
{
|
|
@@ -94,19 +95,15 @@ void Chunk::sendLightToClient(Entity* zPlayer)
|
|
}
|
|
}
|
|
if (needSend)
|
|
if (needSend)
|
|
{
|
|
{
|
|
- NetworkMessage msg;
|
|
|
|
- msg.addressChunck(this);
|
|
|
|
- char message[11];
|
|
|
|
- message[0] = 1;
|
|
|
|
int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
|
|
int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
|
|
- *(int*)(message + 1) = index;
|
|
|
|
- memcpy(message + 5, lightData + index * 6, 6);
|
|
|
|
- msg.setMessage(message, 11, 0);
|
|
|
|
- Game::INSTANCE->sendMessage(&msg, zPlayer);
|
|
|
|
|
|
+ zWriter->schreibe((char*)&index, 4);
|
|
|
|
+ zWriter->schreibe((char*)(lightData + index * 6), 6);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ int end = -1;
|
|
|
|
+ zWriter->schreibe((char*)&end, 4);
|
|
}
|
|
}
|
|
|
|
|
|
Framework::Either<Block*, int> Chunk::zBlockNeighbor(Framework::Vec3<int> location)
|
|
Framework::Either<Block*, int> Chunk::zBlockNeighbor(Framework::Vec3<int> location)
|
|
@@ -141,27 +138,35 @@ void Chunk::notifyObservers(NetworkMessage& msg)
|
|
observers.remove(i);
|
|
observers.remove(i);
|
|
}
|
|
}
|
|
|
|
|
|
-void Chunk::addObserver(Entity* zEntity)
|
|
|
|
|
|
+void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
|
|
{
|
|
{
|
|
for (int id : observers)
|
|
for (int id : observers)
|
|
{
|
|
{
|
|
if (id == zEntity->getId())
|
|
if (id == zEntity->getId())
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- observers.add(zEntity->getId());
|
|
|
|
- InMemoryBuffer buffer;
|
|
|
|
- buffer.schreibe("\4", 1);
|
|
|
|
- buffer.schreibe((char*)&location.x, 4);
|
|
|
|
- buffer.schreibe((char*)&location.y, 4);
|
|
|
|
- sendToClient(&buffer);
|
|
|
|
- NetworkMessage msg;
|
|
|
|
- msg.addressDimension();
|
|
|
|
- char* message = new char[buffer.getSize()];
|
|
|
|
- buffer.lese(message, (int)buffer.getSize());
|
|
|
|
- msg.setMessage(message, (int)buffer.getSize(), 1);
|
|
|
|
- msg.setUseBackground();
|
|
|
|
- Game::INSTANCE->sendMessage(&msg, zEntity);
|
|
|
|
- sendLightToClient(zEntity);
|
|
|
|
|
|
+ int id = zEntity->getId();
|
|
|
|
+ observers.add(id);
|
|
|
|
+ laterHandler.addTodo([this, id]()
|
|
|
|
+ {
|
|
|
|
+ InMemoryBuffer buffer;
|
|
|
|
+ buffer.schreibe("\4", 1);
|
|
|
|
+ buffer.schreibe((char*)&location.x, 4);
|
|
|
|
+ buffer.schreibe((char*)&location.y, 4);
|
|
|
|
+ sendToClient(&buffer);
|
|
|
|
+ sendLightToClient(&buffer);
|
|
|
|
+ NetworkMessage msg;
|
|
|
|
+ msg.addressDimension();
|
|
|
|
+ char* message = new char[buffer.getSize()];
|
|
|
|
+ buffer.lese(message, (int)buffer.getSize());
|
|
|
|
+ msg.setMessage(message, (int)buffer.getSize(), 1);
|
|
|
|
+ msg.setUseBackground();
|
|
|
|
+ Entity* e = Game::INSTANCE->zEntity(id);
|
|
|
|
+ if (e)
|
|
|
|
+ {
|
|
|
|
+ Game::INSTANCE->sendMessage(&msg, e);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
void Chunk::removeObserver(Entity* zEntity)
|
|
void Chunk::removeObserver(Entity* zEntity)
|
|
@@ -178,7 +183,7 @@ void Chunk::removeObserver(Entity* zEntity)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void Chunk::api(Framework::StreamReader* zRequest, Entity* zSource)
|
|
|
|
|
|
+void Chunk::api(Framework::StreamReader* zRequest, Entity* zSource, DoLaterHandler& laterHandler)
|
|
{
|
|
{
|
|
// TODO: answer api messages
|
|
// TODO: answer api messages
|
|
char type;
|
|
char type;
|
|
@@ -187,7 +192,7 @@ void Chunk::api(Framework::StreamReader* zRequest, Entity* zSource)
|
|
{
|
|
{
|
|
case 0:
|
|
case 0:
|
|
// register observer
|
|
// register observer
|
|
- addObserver(zSource);
|
|
|
|
|
|
+ addObserver(zSource, laterHandler);
|
|
break;
|
|
break;
|
|
case 1:
|
|
case 1:
|
|
// unsubscribe
|
|
// unsubscribe
|
|
@@ -226,12 +231,12 @@ void Chunk::initializeLightning()
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- neighborLeight = getLightData(Vec3<int>(x, y, neighborPos.z));
|
|
|
|
|
|
+ neighborLeight = getLightData(neighborPos);
|
|
}
|
|
}
|
|
for (int j = 0; j < 3; j++)
|
|
for (int j = 0; j < 3; j++)
|
|
- newLight[j] = (unsigned char)MAX(newLight[j], i == getDirectionIndex(TOP) ? neighborLeight[j] : neighborLeight[j] - 16);
|
|
|
|
|
|
+ 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++)
|
|
for (int j = 3; j < 6; j++)
|
|
- newLight[j] = (unsigned char)MAX(newLight[j], neighborLeight[j] - 16);
|
|
|
|
|
|
+ newLight[j] = (unsigned char)MAX(newLight[j], (unsigned char)((float)neighborLeight[j] * 0.8f));
|
|
}
|
|
}
|
|
const Block* current = blocks[index] ? blocks[index] : StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])->zDefault();
|
|
const Block* current = blocks[index] ? blocks[index] : StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])->zDefault();
|
|
// add own light emission
|
|
// add own light emission
|
|
@@ -245,6 +250,7 @@ void Chunk::initializeLightning()
|
|
{
|
|
{
|
|
changes = 1;
|
|
changes = 1;
|
|
memcpy(light, newLight, 6);
|
|
memcpy(light, newLight, 6);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -732,6 +738,7 @@ void Chunk::setLightData(Framework::Vec3<int> location, unsigned char* data)
|
|
*(int*)(message + 1) = index / 6;
|
|
*(int*)(message + 1) = index / 6;
|
|
memcpy(message + 5, data, 6);
|
|
memcpy(message + 5, data, 6);
|
|
msg.setMessage(message, 11, 0);
|
|
msg.setMessage(message, 11, 0);
|
|
|
|
+ msg.setUseBackground();
|
|
notifyObservers(msg);
|
|
notifyObservers(msg);
|
|
}
|
|
}
|
|
}
|
|
}
|