Server debug information, distance sorted generation, faster meshing.

master
aurailus 2019-03-15 23:22:53 -07:00
parent e85451abfe
commit 4a195a96cb
16 changed files with 284 additions and 126 deletions

4
.idea/discord.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordIntegrationProjectSettings" description="Multiplayer Voxel Game" />
</project>

View File

@ -70,7 +70,7 @@ void GameScene::update() {
world->loadChunkPacket(&p);
}
debugGui.update(player, world, window, blockAtlas, state->fps, (int)world->getMeshChunks()->size(), drawCalls);
debugGui.update(player, world, window, blockAtlas, state->fps, (int)world->getMeshChunks()->size(), drawCalls, server->serverSideChunkGens, server->recvPackets);
world->update();
}

View File

@ -5,6 +5,7 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#include <syscall.h>
#include "LocalWorld.h"
LocalWorld::LocalWorld(BlockAtlas *atlas) {
@ -42,6 +43,7 @@ void LocalWorld::loadChunkPacket(Packet *p) {
void LocalWorld::commitChunk(glm::vec3 pos, BlockChunk *c) {
if (!blockChunks.count(pos)) {
blockChunks.insert(std::pair<glm::vec3, BlockChunk *>(pos, c));
attemptMeshChunk(pos);
}
}
@ -61,7 +63,7 @@ void LocalWorld::attemptMeshChunk(glm::vec3 pos) {
thisChunk->adjacent[4] = getAdjacentExists(glm::vec3(pos.x, pos.y, pos.z + 1), pos);
thisChunk->adjacent[5] = getAdjacentExists(glm::vec3(pos.x, pos.y, pos.z - 1), pos);
if (thisChunk->allAdjacentsExist()) pendingMesh.push_back(pos);
if (thisChunk->allAdjacentsExist() && !thisChunk->isEmpty()) pendingMesh.push_back(pos);
}
bool LocalWorld::getAdjacentExists(glm::vec3 pos, glm::vec3 otherPos) {
@ -77,7 +79,7 @@ bool LocalWorld::getAdjacentExists(glm::vec3 pos, glm::vec3 otherPos) {
if (diff == glm::vec3(0, 0, 1)) chunk->adjacent[4] = true;
if (diff == glm::vec3(0, 0,-1)) chunk->adjacent[5] = true;
if (chunk->allAdjacentsExist()) pendingMesh.push_back(pos);
if (chunk->allAdjacentsExist() && !chunk->isEmpty()) pendingMesh.push_back(pos);
return true;
}
return false;
@ -306,7 +308,7 @@ void LocalWorld::handleMeshGenQueue() {
glm::vec3 pos = threadData->pos * glm::vec3(CHUNK_SIZE);
meshChunk->setPosition(pos);
meshChunks.insert(std::pair<glm::vec3, MeshChunk *>(threadData->pos, meshChunk));
meshChunks.insert(std::pair<glm::vec3, MeshChunk*>(threadData->pos, meshChunk));
}
iter = finishedMesh.erase(iter);

View File

@ -17,9 +17,9 @@ ChunkThreadDef::ChunkThreadDef(MapGen *mapGen) {
thread = new std::thread(LocalWorld::chunkGenThread, this);
sched_param sch_params{};
sch_params.sched_priority = 1;
pthread_setschedparam(thread->native_handle(), SCHED_RR, &sch_params);
// sched_param sch_params{};
// sch_params.sched_priority = 1;
// pthread_setschedparam(thread->native_handle(), SCHED_RR, &sch_params);
thread->detach();
}
@ -47,9 +47,9 @@ MeshThreadData::~MeshThreadData() {
MeshThreadDef::MeshThreadDef() {
thread = new std::thread(LocalWorld::meshGenThread, this);
sched_param sch_params{};
sch_params.sched_priority = 1;
pthread_setschedparam(thread->native_handle(), SCHED_RR, &sch_params);
// sched_param sch_params{};
// sch_params.sched_priority = 1;
// pthread_setschedparam(thread->native_handle(), SCHED_RR, &sch_params);
thread->detach();
}

View File

@ -36,63 +36,85 @@ DebugGui::DebugGui(glm::vec2 bufferSize) {
glm::vec4(0.1, 0.1, 0.1, 0.5), glm::vec4(0.1, 0.1, 0.1, 0.5));
crosshairBG->setScale(glm::vec3(244, 26, 1));
chunkUpdateHistogram = new Histogram(whiteHistTexture, 120, 64, true);
chunkUpdateHistogram->setScale(glm::vec3(2, 32, 1));
chunkHist = new Histogram(whiteHistTexture, 120, 64, true);
chunkHist->setScale(glm::vec3(2, 32, 1));
chunkUpdateText = new HudText(fontTexture);
chunkUpdateText->setScale(2);
chunkText = new HudText(fontTexture);
chunkText->setScale(2);
chunkUpdateBG = new RectEntity(
chunkBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
chunkUpdateBG->setScale(glm::vec3(244, 64, 1));
chunkBack->setScale(glm::vec3(244, 64, 1));
meshUpdateHistogram = new Histogram(whiteHistTexture, 120, 64, true);
meshUpdateHistogram->setScale(glm::vec3(2, 32, 1));
meshHist = new Histogram(whiteHistTexture, 120, 64, true);
meshHist->setScale(glm::vec3(2, 32, 1));
meshUpdateText = new HudText(fontTexture);
meshUpdateText->setScale(2);
meshText = new HudText(fontTexture);
meshText->setScale(2);
meshUpdateBG = new RectEntity(
meshBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
meshUpdateBG->setScale(glm::vec3(244, 64, 1));
meshBack->setScale(glm::vec3(244, 64, 1));
fpsHistogram = new Histogram(colorHistTexture, 120, 60, true);
fpsHistogram->setScale(glm::vec3(2, 20, 1));
ssGenHist = new Histogram(whiteHistTexture, 120, 64, true);
ssGenHist->setScale(glm::vec3(2, 32, 1));
fpsBG = new RectEntity(
ssGenText = new HudText(fontTexture);
ssGenText->setScale(2);
ssGenBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
fpsBG->setScale(glm::vec3(244, 64, 1));
ssGenBack->setScale(glm::vec3(244, 64, 1));
ssPackHist = new Histogram(whiteHistTexture, 120, -1, true);
ssPackHist->setScale(glm::vec3(2, 32, 1));
ssPackText = new HudText(fontTexture);
ssPackText->setScale(2);
ssPackBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
ssPackBack->setScale(glm::vec3(244, 64, 1));
fpsHist = new Histogram(colorHistTexture, 120, 60, true);
fpsHist->setScale(glm::vec3(2, 20, 1));
fpsBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
fpsBack->setScale(glm::vec3(244, 64, 1));
fpsText = new HudText(fontTexture);
fpsText->setScale(2);
drawCallsHistogram = new Histogram(whiteHistTexture, 240, 1, false);
drawCallsHistogram->setScale(glm::vec3(1, 20, 1));
drawsHist = new Histogram(whiteHistTexture, 240, 1, false);
drawsHist->setScale(glm::vec3(1, 20, 1));
chunkHistogram = new Histogram(transWhiteHistTexture, 240, 0, false);
chunkHistogram->setScale(glm::vec3(1, 20, 1));
tMeshHist = new Histogram(transWhiteHistTexture, 240, 0, false);
tMeshHist->setScale(glm::vec3(1, 20, 1));
drawCallsBG = new RectEntity(
drawsBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
drawCallsBG->setScale(glm::vec3(244, 64, 1));
drawsBack->setScale(glm::vec3(244, 64, 1));
drawCallsText = new HudText(fontTexture);
drawCallsText->setScale(2);
drawsText = new HudText(fontTexture);
drawsText->setScale(2);
videoMemoryHistogram = new Histogram(whiteHistTexture, 240, 1, true);
videoMemoryHistogram->setScale(glm::vec3(1, 32, 1));
vramHist = new Histogram(whiteHistTexture, 240, 1, true);
vramHist->setScale(glm::vec3(1, 32, 1));
videoMemoryText = new HudText(fontTexture);
videoMemoryText->setScale(2);
vramText = new HudText(fontTexture);
vramText->setScale(2);
videoMemoryBG = new RectEntity(
vramBack = new RectEntity(
glm::vec4(0.1, 0.1, 0.1, 0.2), glm::vec4(0.1, 0.1, 0.1, 0.2),
glm::vec4(0.1, 0.1, 0.1, 0.7), glm::vec4(0.1, 0.1, 0.1, 0.7));
videoMemoryBG->setScale(glm::vec3(244, 64, 1));
vramBack->setScale(glm::vec3(244, 64, 1));
positionElements(bufferSize);
}
@ -109,32 +131,42 @@ void DebugGui::positionElements(glm::vec2 bufferSize) {
dataText->setPosition(glm::vec3(10, 10, 0));
dataBG->setPosition(glm::vec3(6, 6, -1));
glm::vec2 ssPackPos(bufferWidth - 254, bufferHeight - 70 - 240);
glm::vec2 ssGenPos(bufferWidth - 254, bufferHeight - 70 - 160);
glm::vec2 meshUpdatePos(bufferWidth - 254, bufferHeight - 70 - 80);
glm::vec2 chunkUpdatePos(bufferWidth - 254, bufferHeight - 70);
glm::vec2 fpsPos(10, bufferHeight - 70);
glm::vec2 drawCallsPos(10, bufferHeight - 70 - 80);
glm::vec2 videoMemoryPos(bufferWidth - 254, 10);
meshUpdateText->setPosition(glm::vec3(meshUpdatePos.x + 4, meshUpdatePos.y + 8, 0));
meshUpdateHistogram->setPosition(glm::vec3(meshUpdatePos.x + 2, meshUpdatePos.y + 60, -1));
meshUpdateBG->setPosition(glm::vec3(meshUpdatePos.x, meshUpdatePos.y, -2));
ssGenText->setPosition(glm::vec3(ssGenPos.x + 4, ssGenPos.y + 8, 0));
ssGenHist->setPosition(glm::vec3(ssGenPos.x + 2, ssGenPos.y + 60, -1));
ssGenBack->setPosition(glm::vec3(ssGenPos.x, ssGenPos.y, -2));
chunkUpdateText->setPosition(glm::vec3(chunkUpdatePos.x + 4, chunkUpdatePos.y + 8, 0));
chunkUpdateHistogram->setPosition(glm::vec3(chunkUpdatePos.x + 2, chunkUpdatePos.y + 60, -1));
chunkUpdateBG->setPosition(glm::vec3(chunkUpdatePos.x, chunkUpdatePos.y, -2));
ssPackText->setPosition(glm::vec3(ssPackPos.x + 4, ssPackPos.y + 8, 0));
ssPackHist->setPosition(glm::vec3(ssPackPos.x + 2, ssPackPos.y + 60, -1));
ssPackBack->setPosition(glm::vec3(ssPackPos.x, ssPackPos.y, -2));
meshText->setPosition(glm::vec3(meshUpdatePos.x + 4, meshUpdatePos.y + 8, 0));
meshHist->setPosition(glm::vec3(meshUpdatePos.x + 2, meshUpdatePos.y + 60, -1));
meshBack->setPosition(glm::vec3(meshUpdatePos.x, meshUpdatePos.y, -2));
chunkText->setPosition(glm::vec3(chunkUpdatePos.x + 4, chunkUpdatePos.y + 8, 0));
chunkHist->setPosition(glm::vec3(chunkUpdatePos.x + 2, chunkUpdatePos.y + 60, -1));
chunkBack->setPosition(glm::vec3(chunkUpdatePos.x, chunkUpdatePos.y, -2));
fpsText->setPosition(glm::vec3(fpsPos.x + 4, fpsPos.y + 8, 0));
fpsHistogram->setPosition(glm::vec3(fpsPos.x + 2, fpsPos.y + 60, -1));
fpsBG->setPosition(glm::vec3(fpsPos.x, fpsPos.y, -2));
fpsHist->setPosition(glm::vec3(fpsPos.x + 2, fpsPos.y + 60, -1));
fpsBack->setPosition(glm::vec3(fpsPos.x, fpsPos.y, -2));
drawCallsText->setPosition(glm::vec3(drawCallsPos.x + 4, drawCallsPos.y + 8, 0));
drawCallsHistogram->setPosition(glm::vec3(drawCallsPos.x + 2, drawCallsPos.y + 60, -1));
chunkHistogram->setPosition(glm::vec3(drawCallsPos.x + 2, drawCallsPos.y + 60, -1));
drawCallsBG->setPosition(glm::vec3(drawCallsPos.x, drawCallsPos.y, -2));
drawsText->setPosition(glm::vec3(drawCallsPos.x + 4, drawCallsPos.y + 8, 0));
drawsHist->setPosition(glm::vec3(drawCallsPos.x + 2, drawCallsPos.y + 60, -1));
tMeshHist->setPosition(glm::vec3(drawCallsPos.x + 2, drawCallsPos.y + 60, -1));
drawsBack->setPosition(glm::vec3(drawCallsPos.x, drawCallsPos.y, -2));
videoMemoryText->setPosition(glm::vec3(videoMemoryPos.x + 4, videoMemoryPos.y + 8, 0));
videoMemoryHistogram->setPosition(glm::vec3(videoMemoryPos.x + 2, videoMemoryPos.y + 60, -1));
videoMemoryBG->setPosition(glm::vec3(videoMemoryPos.x, videoMemoryPos.y, -2));
vramText->setPosition(glm::vec3(videoMemoryPos.x + 4, videoMemoryPos.y + 8, 0));
vramHist->setPosition(glm::vec3(videoMemoryPos.x + 2, videoMemoryPos.y + 60, -1));
vramBack->setPosition(glm::vec3(videoMemoryPos.x, videoMemoryPos.y, -2));
}
void DebugGui::pushGuiObjects(std::vector<Entity*> &list) {
@ -144,26 +176,34 @@ void DebugGui::pushGuiObjects(std::vector<Entity*> &list) {
list.push_back(crosshairBG);
list.push_back(crosshairText);
list.push_back(chunkUpdateBG);
list.push_back(chunkUpdateHistogram);
list.push_back(chunkUpdateText);
list.push_back(chunkBack);
list.push_back(chunkHist);
list.push_back(chunkText);
list.push_back(meshUpdateBG);
list.push_back(meshUpdateHistogram);
list.push_back(meshUpdateText);
list.push_back(meshBack);
list.push_back(meshHist);
list.push_back(meshText);
list.push_back(fpsBG);
list.push_back(fpsHistogram);
list.push_back(ssGenBack);
list.push_back(ssGenHist);
list.push_back(ssGenText);
list.push_back(ssPackBack);
list.push_back(ssPackHist);
list.push_back(ssPackText);
list.push_back(fpsBack);
list.push_back(fpsHist);
list.push_back(fpsText);
list.push_back(drawCallsBG);
list.push_back(drawCallsHistogram);
list.push_back(chunkHistogram);
list.push_back(drawCallsText);
list.push_back(drawsBack);
list.push_back(drawsHist);
list.push_back(tMeshHist);
list.push_back(drawsText);
list.push_back(videoMemoryBG);
list.push_back(videoMemoryHistogram);
list.push_back(videoMemoryText);
list.push_back(vramBack);
list.push_back(vramHist);
list.push_back(vramText);
}
std::string string_float(float val) {
@ -174,7 +214,7 @@ std::string string_float(float val) {
return s;
}
void DebugGui::update(Player* player, LocalWorld* world, Window* window, BlockAtlas* atlas, double fps, int chunks, int drawCalls) {
void DebugGui::update(Player* player, LocalWorld* world, Window* window, BlockAtlas* atlas, double fps, int chunks, int drawCalls, int ssGen, int ssPack) {
using namespace std;
glGetIntegerv(0x9048, &videoMemTotal);
@ -210,22 +250,28 @@ void DebugGui::update(Player* player, LocalWorld* world, Window* window, BlockAt
glm::vec3 loc = LocalWorld::localVec(*player->getPos());
fpsText->set("FPS:" + string_float((float)fps));
fpsHistogram->push_back((float)fps);
fpsHist->push_back((float)fps);
drawCallsText->set("MCD:" + to_string(drawCalls) + "," + to_string(chunks));
chunkHistogram->push_back(chunks);
drawCallsHistogram->setMax(chunks);
drawCallsHistogram->push_back(drawCalls);
drawsText->set("MCD:" + to_string(drawCalls) + "," + to_string(chunks));
tMeshHist->push_back(chunks);
drawsHist->setMax(chunks);
drawsHist->push_back(drawCalls);
meshUpdateHistogram->push_back((float)world->lastMeshUpdates);
meshUpdateText->set("Mesh:" + to_string(world->lastMeshUpdates));
meshHist->push_back((float)world->lastMeshUpdates);
meshText->set("Mesh:" + to_string(world->lastMeshUpdates));
chunkUpdateHistogram->push_back((float)world->lastGenUpdates);
chunkUpdateText->set("Gen:" + to_string(world->lastGenUpdates));
chunkHist->push_back((float)world->lastGenUpdates);
chunkText->set("Gen:" + to_string(world->lastGenUpdates));
videoMemoryHistogram->setMax((float)videoMemTotal / 1024);
videoMemoryHistogram->push_back((float)(videoMemTotal - videoMemAvail) / 1024);
videoMemoryText->set("Total VRam Usage:\n" + to_string((videoMemTotal - videoMemAvail) / 1024) + "MB, (" + to_string((int)std::round((videoMemTotal - videoMemAvail) / (float)videoMemTotal * 100.0f)) + "%)");
ssGenHist->push_back((float)ssGen);
ssGenText->set("Server:" + to_string(ssGen));
ssPackHist->push_back((float)ssPack);
ssPackText->set("Packets:" + to_string(ssPack));
vramHist->setMax((float)videoMemTotal / 1024);
vramHist->push_back((float)(videoMemTotal - videoMemAvail) / 1024);
vramText->set("Total VRam Usage:\n" + to_string((videoMemTotal - videoMemAvail) / 1024) + "MB, (" + to_string((int)std::round((videoMemTotal - videoMemAvail) / (float)videoMemTotal * 100.0f)) + "%)");
dataText->set(
"World: " + to_string((int)player->getPos()->x) + "," + to_string((int)player->getPos()->y) + "," + to_string((int)player->getPos()->z) + "\n" +

View File

@ -15,12 +15,12 @@
class DebugGui {
public:
DebugGui(glm::vec2 bufferSize);
explicit DebugGui(glm::vec2 bufferSize);
void bufferResized(glm::vec2 bufferSize);
void pushGuiObjects(std::vector<Entity*> &list);
void update(Player* player, LocalWorld* world, Window* window, BlockAtlas* atlas, double fps, int chunks, int drawCalls);
void update(Player* player, LocalWorld* world, Window* window, BlockAtlas* atlas, double fps, int chunks, int drawCalls, int ssGen, int ssPack);
void positionElements(glm::vec2 bufferSize);
~DebugGui();
@ -34,9 +34,10 @@ private:
HudText *dataText, *crosshairText;
RectEntity *dataBG, *crosshairBG;
HudText *chunkUpdateText, *meshUpdateText, *fpsText, *drawCallsText, *videoMemoryText;
Histogram *chunkUpdateHistogram, *meshUpdateHistogram, *fpsHistogram, *drawCallsHistogram, *chunkHistogram, *videoMemoryHistogram;
RectEntity *chunkUpdateBG, *meshUpdateBG, *fpsBG, *drawCallsBG, *videoMemoryBG;
HudText *chunkText, *meshText, *fpsText, *drawsText, *vramText, *ssGenText, *ssPackText;
Histogram *chunkHist, *meshHist, *fpsHist, *drawsHist, *tMeshHist, *vramHist, *ssGenHist, *ssPackHist;
RectEntity *chunkBack, *meshBack, *fpsBack, *drawsBack, *vramBack, *ssGenBack, *ssPackBack;
};

View File

@ -19,8 +19,11 @@ void ServerConnection::init() {
void ServerConnection::update(Player &player, std::vector<PlayerEntity*>& playerEntities) {
recvPackets = 0;
ENetEvent event;
while (handler.update(&event)) {
recvPackets++;
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT: {
@ -67,6 +70,10 @@ void ServerConnection::update(Player &player, std::vector<PlayerEntity*>& player
chunkPackets.push_back(std::move(p));
break;
}
case Packet::SERVER_INFO: {
serverSideChunkGens = Serializer::decodeInt(&p.data[0]);
break;
}
default:
break;
}

View File

@ -26,14 +26,13 @@ public:
~ServerConnection();
std::vector<Packet> chunkPackets;
int serverSideChunkGens;
int recvPackets;
private:
bool connected = false;
NetHandler handler;
int sendInterval = 0;
int sendCount = 0;
std::string address;
unsigned short port;
};

View File

@ -11,13 +11,24 @@
BlockChunk::BlockChunk() {
this->blocks = std::vector<int>(4096);
this->empty = true;
}
BlockChunk::BlockChunk(std::vector<int> blocks) {
this->empty = true;
for (int i : blocks) {
if (i != 0) this->empty = false;
}
this->blocks = std::move(blocks);
}
BlockChunk::BlockChunk(std::vector<int> blocks, glm::vec3 pos) {
this->empty = true;
for (int i : blocks) {
if (i != 0) this->empty = false;
}
this->blocks = std::move(blocks);
this->pos = pos;
}
@ -40,6 +51,9 @@ int BlockChunk::getBlock(int x, int y, int z) {
}
bool BlockChunk::setBlock(glm::vec3* pos, int block) {
//TODO: Update emptiness
unsigned int ind = ArrayTrans3D::vecToInd(pos);
if (ind < 0 || ind >= 4096) return false;
if (blocks[ind] != block) {
@ -53,6 +67,10 @@ bool BlockChunk::allAdjacentsExist() {
return adjacent[0] && adjacent[1] && adjacent[2] && adjacent[3] && adjacent[4] && adjacent[5];
}
bool BlockChunk::isEmpty() {
return empty;
}
std::vector<int> BlockChunk::rleEncode() {
std::vector<int> rle;
@ -82,6 +100,7 @@ void BlockChunk::rleDecode(std::vector<int>& blocksRle) {
int ind = 0;
this->empty = true;
for (int i = 0; i < blocksRle.size() / 2; i++) {
int block = blocksRle[i*2];
int count = blocksRle[i*2 + 1];
@ -89,6 +108,10 @@ void BlockChunk::rleDecode(std::vector<int>& blocksRle) {
for (int j = 0; j < count; j++) {
blocks[ind++] = (block);
if (block != 0) {
this->empty = false;
}
if (ind >= 4096) return;
}
}
@ -116,4 +139,4 @@ bool BlockChunk::deserialize(std::string gzip) {
return true;
}
return false;
}
}

View File

@ -20,6 +20,8 @@ public:
glm::vec3 pos;
bool adjacent[6] = {false, false, false, false, false, false};
bool isEmpty();
bool allAdjacentsExist();
int getBlock(int ind);
@ -35,6 +37,7 @@ public:
bool deserialize(std::string gzip);
private:
std::vector<int> blocks;
bool empty;
};
#endif //GLPROJECT_BLOCKCHUNK_H

View File

@ -21,7 +21,8 @@ struct Packet {
AUTHENTICATE,
PLAYER_INFO,
ENTITY_INFO,
CHUNK_INFO
CHUNK_INFO,
SERVER_INFO,
};
Packet() = default;

View File

@ -15,7 +15,8 @@ struct PacketChannel {
AUTHENTICATE,
PLAYER_INFO,
ENTITY_INFO,
WORLD_INFO
WORLD_INFO,
SERVER_INFO
};
};

View File

@ -8,7 +8,6 @@
ServerPlayer::ServerPlayer(ServerPeer *peer) {
this->peer = peer;
peer->player = this;
updateBounds();
}
Packet ServerPlayer::getInitPacket() {
@ -25,33 +24,41 @@ glm::vec3 ServerPlayer::getPos() {
return pos;
}
glm::vec3 ServerPlayer::getChunkPos() {
return chunkPos;
}
void ServerPlayer::setPos(glm::vec3 pos) {
this->lastPos = this->pos;
this->pos = pos;
glm::vec3 newChunkPos(std::floor(this->pos.x / 16), std::floor(this->pos.y / 16), std::floor(this->pos.z / 16));
glm::vec3 chunkPos(std::floor(pos.x / 16), std::floor(pos.y / 16), std::floor(pos.z / 16));
glm::vec3 lastChunkPos(std::floor(lastPos.x / 16), std::floor(lastPos.y / 16), std::floor(lastPos.z / 16));
if (chunkPos != lastChunkPos) {
updateBounds();
if (newChunkPos != chunkPos) {
if (!changedChunks) {
lastChunkPos = chunkPos;
changedChunks = true;
}
chunkPos = newChunkPos;
}
}
std::pair<glm::vec3, glm::vec3> ServerPlayer::getBounds() {
glm::vec3 minBounds(chunkPos.x - ACTIVE_RANGE, chunkPos.y - ACTIVE_RANGE, chunkPos.z - ACTIVE_RANGE);
glm::vec3 maxBounds(chunkPos.x + ACTIVE_RANGE, chunkPos.y + ACTIVE_RANGE, chunkPos.z + ACTIVE_RANGE);
return {minBounds, maxBounds};
}
void ServerPlayer::updateBounds() {
glm::vec3 chunkPos(std::floor(pos.x / 16), std::floor(pos.y / 16), std::floor(pos.z / 16));
std::pair<glm::vec3, glm::vec3> ServerPlayer::getOldBounds() {
glm::vec3 minBounds(lastChunkPos.x - ACTIVE_RANGE, lastChunkPos.y - ACTIVE_RANGE, lastChunkPos.z - ACTIVE_RANGE);
glm::vec3 maxBounds(lastChunkPos.x + ACTIVE_RANGE, lastChunkPos.y + ACTIVE_RANGE, lastChunkPos.z + ACTIVE_RANGE);
minBounds = glm::vec3(chunkPos.x - ACTIVE_RANGE, chunkPos.y - ACTIVE_RANGE, chunkPos.z - ACTIVE_RANGE);
maxBounds = glm::vec3(chunkPos.x + ACTIVE_RANGE, chunkPos.y + ACTIVE_RANGE, chunkPos.z + ACTIVE_RANGE);
return {minBounds, maxBounds};
}
bool ServerPlayer::isInBounds(glm::vec3 pos) {
return (pos.x >= minBounds.x && pos.x <= maxBounds.x
&& pos.y >= minBounds.y && pos.y <= maxBounds.y
&& pos.z >= minBounds.z && pos.z <= maxBounds.z);
bool ServerPlayer::isInBounds(glm::vec3 cPos, std::pair<glm::vec3, glm::vec3> &bounds) {
return (cPos.x >= bounds.first.x && cPos.x <= bounds.second.x
&& cPos.y >= bounds.first.y && cPos.y <= bounds.second.y
&& cPos.z >= bounds.first.z && cPos.z <= bounds.second.z);
}
ServerPlayer::~ServerPlayer() = default;

View File

@ -21,20 +21,22 @@ public:
glm::vec3 getPos();
void setPos(glm::vec3 pos);
glm::vec3 getChunkPos();
bool changedChunks = false;
std::pair<glm::vec3, glm::vec3> getBounds();
bool isInBounds(glm::vec3 pos);
std::pair<glm::vec3, glm::vec3> getOldBounds();
bool isInBounds(glm::vec3 pos, std::pair<glm::vec3, glm::vec3>& bounds);
~ServerPlayer();
ServerPeer* peer;
private:
void updateBounds();
glm::vec3 pos = {0, 0, 0};
glm::vec3 lastPos = {0, 0, 0};
glm::vec3 minBounds = {0, 0, 0};
glm::vec3 maxBounds = {0, 0, 0};
glm::vec3 chunkPos {0, 0, 0};
glm::vec3 lastChunkPos {0, 0, 0};
glm::vec3 pos {0, 0, 0};
};

View File

@ -8,19 +8,65 @@
void World::addPlayer(ServerPlayer *player) {
this->players.push_back(player);
playerChangedChunks(player);
}
void World::playerChangedChunks(ServerPlayer *player) {
auto bounds = player->getBounds();
auto pos = player->getChunkPos();
for (auto i = (int)bounds.first.x; i < (int)bounds.second.x; i++) {
for (auto j = (int)bounds.first.y; j < (int)bounds.second.y; j++) {
for (auto k = (int)bounds.first.z; k < (int)bounds.second.z; k++) {
generate(glm::vec3(i, j, k));
std::vector<glm::vec3> toGenerate;
toGenerate.reserve((unsigned long)pow(ServerPlayer::ACTIVE_RANGE, 3));
for (int i = (int)bounds.first.x; i < (int)bounds.second.x; i++) {
for (int j = (int)bounds.first.x; j < (int)bounds.second.x; j++) {
for (int k = (int) bounds.first.x; k < (int) bounds.second.x; k++) {
toGenerate.emplace_back(i, j, k);
}
}
}
std::sort(toGenerate.begin(), toGenerate.end(), [&](glm::vec3 a, glm::vec3 b) {
return min(min(abs(a.x - pos.x), abs(a.y - pos.y)), abs(a.z - pos.z)) <
min(min(abs(b.x - pos.x), abs(b.y - pos.y)), abs(b.z - pos.z));
});
for (glm::vec3 tPos : toGenerate) {
generate(tPos);
}
}
void World::playerChangedChunks(ServerPlayer *player) {
auto pos = player->getChunkPos();
auto oldBounds = player->getOldBounds();
int chunksToGen = 0;
for (int i = 0; i < ServerPlayer::ACTIVE_RANGE; i++) {
for (int x = (int)pos.x - i; x < (int)pos.x + i; x++) {
for (int y = (int)pos.y - i; y < (int)pos.y + i; y++) {
for (int z = (int)pos.z - i; z < (int)pos.z + i; z++) {
glm::vec3 cPos(x, y, z);
if (!player->isInBounds(cPos, oldBounds)) {
if (chunkMap.count(cPos)) {
//Send the Chunk to the player
Packet r(Packet::CHUNK_INFO);
Serializer::encodeInt(r.data, (int)cPos.x);
Serializer::encodeInt(r.data, (int)cPos.y);
Serializer::encodeInt(r.data, (int)cPos.z);
Serializer::encodeString(r.data, chunkMap[cPos]->serialize());
r.sendTo(player->peer->peer, PacketChannel::WORLD_INFO);
}
else {
generate(pos);
}
chunksToGen++;
}
}
}
}
}
player->changedChunks = false;
}
void World::generate(glm::vec3 pos) {
@ -46,13 +92,16 @@ void World::update() {
}
auto finished = genStream.update();
generatedChunks = (int)finished.size();
for (auto chunk : finished) {
bool didCalcSerialized = false;
std::string serialized;
for (auto player : players) {
if (player->isInBounds(chunk->pos)) {
auto bounds = player->getBounds();
if (player->isInBounds(chunk->pos, bounds)) {
//Serialize the chunk
if (!didCalcSerialized) {
@ -72,4 +121,15 @@ void World::update() {
}
}
}
Packet r(Packet::SERVER_INFO);
Serializer::encodeInt(r.data, generatedChunks);
for (auto player : players) {
r.sendTo(player->peer->peer, PacketChannel::SERVER_INFO);
if (player->changedChunks) {
playerChangedChunks(player);
}
}
}

View File

@ -39,6 +39,8 @@ private:
std::unordered_set<glm::vec3, vec3cmp> generateQueueMap;
std::vector<glm::vec3> generateQueueList;
int generatedChunks = 0;
};