Map Block Integrity

master
Nicole Collings 2019-11-23 02:06:37 -08:00
parent 07bf603516
commit 8c3cf3ed40
8 changed files with 78 additions and 25 deletions

View File

@ -13,7 +13,7 @@ Font::Font(TextureAtlas& atlas, std::shared_ptr<AtlasRef> tex) :
uint Font::getCharWidth(char c) {
uint index = static_cast<uint>(c) - 32;
if (index >= AMOUNT_CHARS) {
if (index >= amountOfChars) {
std::cout << Log::err << "Invalid char index!" << std::endl;
return 0;
}
@ -25,17 +25,17 @@ void Font::getCharWidths(TextureAtlas &atlas) {
charWidths[0] = 2;
for (unsigned int i = 1; i < AMOUNT_CHARS; i++) {
glm::vec2 charPos = {i % 18 * CHAR_WIDTH, std::floor(i / 18) * CHAR_HEIGHT};
for (unsigned int i = 1; i < amountOfChars; i++) {
glm::vec2 charPos = {i % 18 * charWidth, std::floor(i / 18) * charHeight};
uint xBase = static_cast<uint>(fontTex->pos.x) + static_cast<uint>(charPos.x);
uint yBase = static_cast<uint>(fontTex->pos.y) + static_cast<uint>(charPos.y);
unsigned short width = 0;
for (uint j = 0; j < CHAR_WIDTH; j++) {
for (uint j = 0; j < charWidth; j++) {
bool empty = true;
for (uint k = 0; k < CHAR_HEIGHT; k++) {
for (uint k = 0; k < charHeight; k++) {
uint xx = xBase + j;
uint yy = yBase + k;
@ -55,20 +55,20 @@ void Font::getCharWidths(TextureAtlas &atlas) {
glm::vec4 Font::getCharUVs(char c) {
uint index = static_cast<uint>(c) - 32;
if (index >= AMOUNT_CHARS) {
if (index >= amountOfChars) {
std::cout << Log::err << "Invalid char index!" << std::endl;
return {};
}
glm::vec4 uv;
glm::vec2 charPos = {((index % 18) * CHAR_WIDTH),
(std::floor(index / 18) * CHAR_HEIGHT)};
glm::vec2 charPos = {((index % 18) * charWidth),
(std::floor(index / 18) * charHeight)};
uv.x = fontTex->uv.x + (charPos.x) / atlasSize.x;
uv.y = fontTex->uv.y + (charPos.y) / atlasSize.y;
uv.z = fontTex->uv.x + (charPos.x + getCharWidth(c) + 1) / atlasSize.x;
uv.w = fontTex->uv.y + (charPos.y + CHAR_HEIGHT) / atlasSize.y;
uv.w = fontTex->uv.y + (charPos.y + charHeight) / atlasSize.y;
return uv;
}

View File

@ -15,9 +15,9 @@ public:
uint getCharWidth(char c);
glm::vec4 getCharUVs(char c);
const static unsigned int AMOUNT_CHARS = 95;
const static unsigned int CHAR_WIDTH = 7;
const static unsigned int CHAR_HEIGHT = 9;
const static unsigned int amountOfChars = 95;
const static unsigned int charWidth = 7;
const static unsigned int charHeight = 9;
private:
void getCharWidths(TextureAtlas& atlas);

View File

@ -37,7 +37,7 @@ void GUIText::setText(std::string text) {
//Draw background & Measure Line Width
int lineWidth = 0;
int xOffset = 0, yOffset = 0;
int h = Font::CHAR_HEIGHT;
int h = Font::charHeight;
for (unsigned int i = 0; i < this->text.length() + 1; i++) {
char c = this->text[i];
@ -82,7 +82,7 @@ void GUIText::setText(std::string text) {
for (unsigned int i = 0; i < this->text.length() + 1; i++) {
char c = this->text[i];
unsigned int h = Font::CHAR_HEIGHT;
unsigned int h = Font::charHeight;
if (c == '\n' || i == this->text.length()) {
yOffset += (emptyLine) ? h / 2 : h + 2;

View File

@ -42,4 +42,13 @@ float ServerPlayer::getAngle() {
glm::vec3 ServerPlayer::getChunkPos() {
return Space::Chunk::world::fromBlock(pos);
}
}
void ServerPlayer::setMapBlockIntegrity(glm::vec3 pos, unsigned long long integrity) {
mapBlockIntegrity[pos] = integrity;
}
unsigned long long ServerPlayer::getMapBlockIntegrity(glm::vec3 pos) {
if (mapBlockIntegrity.count(pos)) return mapBlockIntegrity[pos];
return 0;
}

View File

@ -5,6 +5,8 @@
#pragma once
#include <glm/vec3.hpp>
#include <unordered_map>
#include "../../util/Vec.h"
#include "../../util/Space.h"
class ServerPlayer {
@ -23,11 +25,16 @@ public:
glm::vec3 mapBlock;
glm::vec3 lastMapBlock;
bool changedMapBlocks = true;
void setMapBlockIntegrity(glm::vec3 pos, unsigned long long integrity);
unsigned long long getMapBlockIntegrity(glm::vec3 pos);
private:
std::string username;
unsigned int connectID;
glm::vec3 pos {};
float angle = 0;
std::unordered_map<glm::vec3, unsigned long long, Vec::compareFunc> mapBlockIntegrity {};
};

View File

@ -90,21 +90,27 @@ void ServerWorld::update() {
}
auto finished = genStream->update();
generatedChunks = (int)finished.size();
generatedChunks = static_cast<int>(finished.size());
//TODO: Make this finish MapBlocks @ a time
for (const auto& chunk : finished) {
dimension.setChunk(chunk);
// TODO: Make this only happen once per mapblock, not for every chunk
glm::vec3 mapBlockPos = Space::MapBlock::world::fromChunk(chunk->pos);
unsigned long long mapBlockIntegrity = dimension.getMapBlockIntegrity(mapBlockPos);
for (auto& client : clientList.clients) {
if (client->hasPlayer()) {
auto mapBlock = client->getPlayer().mapBlock;
auto playerMapBlock = client->getPlayer().mapBlock;
std::pair<glm::vec3, glm::vec3> bounds = {
{mapBlock.x - MB_GEN_H, mapBlock.y - MB_GEN_V, mapBlock.z - MB_GEN_H},
{mapBlock.x + MB_GEN_H, mapBlock.y + MB_GEN_V, mapBlock.z + MB_GEN_H}};
{playerMapBlock.x - MB_GEN_H, playerMapBlock.y - MB_GEN_V, playerMapBlock.z - MB_GEN_H},
{playerMapBlock.x + MB_GEN_H, playerMapBlock.y + MB_GEN_V, playerMapBlock.z + MB_GEN_H}};
if (isInBounds(Space::MapBlock::world::fromChunk(chunk->pos), bounds)) {
if (isInBounds(mapBlockPos, bounds) && client->getPlayer().getMapBlockIntegrity(mapBlockPos) < mapBlockIntegrity) {
sendChunk(chunk->pos, *client);
client->getPlayer().setMapBlockIntegrity(mapBlockPos, mapBlockIntegrity);
}
}
}
@ -139,11 +145,15 @@ void ServerWorld::sendChunk(const glm::vec3& pos, ServerClient &peer) {
}
void ServerWorld::sendMapBlock(const glm::vec3& pos, ServerClient &peer) {
auto mapBlock = dimension.getMapBlock(pos);
assert(mapBlock != nullptr);
//TODO: Make this a real function that sends an entire mapblock packet
unsigned long long mapBlockIntegrity = dimension.getMapBlockIntegrity(pos);
if (peer.getPlayer().getMapBlockIntegrity(pos) < mapBlockIntegrity) {
auto mapBlock = dimension.getMapBlock(pos);
assert(mapBlock != nullptr);
for (unsigned short i = 0; i < 63; i++) {
sendChunk((*mapBlock)[i], peer);
for (unsigned short i = 0; i < 63; i++) {
sendChunk((*mapBlock)[i], peer);
}
}
}

View File

@ -2,4 +2,23 @@
// Created by aurailus on 01/10/19.
//
#include "ServerDimension.h"
#include "ServerDimension.h"
bool ServerDimension::setBlock(glm::vec3 pos, unsigned int block) {
bool manip = Dimension::setBlock(pos, block);
if (!manip) return false;
glm::vec3 mb = Space::MapBlock::world::fromBlock(pos);
mapBlockIntegrity[mb] = mapBlockIntegrity[mb] + 1;
return true;
}
void ServerDimension::setChunk(std::shared_ptr<BlockChunk> chunk) {
Dimension::setChunk(chunk);
glm::vec3 mb = Space::MapBlock::world::fromChunk(chunk->pos);
mapBlockIntegrity[mb] = mapBlockIntegrity[mb] + 1;
}
unsigned long long ServerDimension::getMapBlockIntegrity(glm::vec3 mapBlock) {
if (mapBlockIntegrity.count(mapBlock)) return mapBlockIntegrity[mapBlock];
return 0;
}

View File

@ -9,5 +9,13 @@
class ServerDimension : public Dimension {
public:
ServerDimension() = default;
void setChunk(std::shared_ptr<BlockChunk> chunk) override;
bool setBlock(glm::vec3 pos, unsigned int block) override;
unsigned long long getMapBlockIntegrity(glm::vec3 mapBlock);
private:
std::unordered_map<glm::vec3, unsigned long long, Vec::compareFunc> mapBlockIntegrity {};
};