Use a specialized null value for ChunkPos

This prevents logic errors related to being in chunk 0 0 0.

Also:

* Moved some duplicated chunk teleportation logic to a new helper
  function
* Made ChunkPos into a proper class so it can default to INVALID_CHUNK
  when default-initialized
* Reversed the inclusion order of Chunking.hpp and Entities.hpp to work
  around problems with type definitions
This commit is contained in:
2022-07-22 02:40:33 +02:00
parent 1bd4d2fbee
commit 129d1c2fe3
9 changed files with 46 additions and 27 deletions

View File

@@ -7,11 +7,16 @@
using namespace Chunking;
/*
* The initial chunkPos value before a player is placed into the world.
*/
const ChunkPos Chunking::INVALID_CHUNK = {};
std::map<ChunkPos, Chunk*> Chunking::chunks;
static void newChunk(ChunkPos pos) {
if (chunkExists(pos)) {
std::cout << "[WARN] Tried to create a chunk that already exists\n";
std::cout << "[WARN] Tried to create a chunk that already exists" << std::endl;
return;
}
@@ -27,7 +32,7 @@ static void newChunk(ChunkPos pos) {
static void deleteChunk(ChunkPos pos) {
if (!chunkExists(pos)) {
std::cout << "[WARN] Tried to delete a chunk that doesn't exist\n";
std::cout << "[WARN] Tried to delete a chunk that doesn't exist" << std::endl;
return;
}
@@ -200,7 +205,7 @@ bool Chunking::chunkExists(ChunkPos chunk) {
}
ChunkPos Chunking::chunkPosAt(int posX, int posY, uint64_t instanceID) {
return std::make_tuple(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
return ChunkPos(posX / (settings::VIEWDISTANCE / 3), posY / (settings::VIEWDISTANCE / 3), instanceID);
}
std::set<Chunk*> Chunking::getViewableChunks(ChunkPos chunk) {
@@ -213,7 +218,7 @@ std::set<Chunk*> Chunking::getViewableChunks(ChunkPos chunk) {
// grabs surrounding chunks if they exist
for (int i = -1; i < 2; i++) {
for (int z = -1; z < 2; z++) {
ChunkPos pos = std::make_tuple(x+i, y+z, inst);
ChunkPos pos = ChunkPos(x+i, y+z, inst);
// if chunk exists, add it to the set
if (chunkExists(pos))