Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
d5eb3ca
feat: changed world save file to use FILE* instead of paths
Zffu Nov 13, 2025
fcb6130
feat: added file_handle header
Zffu Nov 13, 2025
3b36cdc
feat: added file_handle impl
Zffu Nov 13, 2025
0272129
feat: put file_handle into save files
Zffu Nov 13, 2025
ac1970e
feat: started working on chunk loading
Zffu Nov 13, 2025
9660c4f
Merge pull request #33 from Cavernfall/master
Zffu Nov 13, 2025
47553d4
Merge branch 'feat/savefiles2' of https://github.com/Cavernfall/Caver…
Zffu Nov 13, 2025
886e41f
feat: added actual chunk loader
Zffu Nov 13, 2025
983304b
feat: added actual chunk saving
Zffu Nov 13, 2025
c3d08a5
feat: added test use save files in world
Zffu Nov 13, 2025
d8f54e8
feat: reverted flag
Zffu Nov 13, 2025
4413a2b
feat: added global number reading
Zffu Nov 13, 2025
27ed19a
feat: added number functions in file_handle
Zffu Nov 13, 2025
31b8c7d
feat: added region_file_chunk read and write
Zffu Nov 13, 2025
d5d7e39
feat: removed chunk_block_data_t and block_data_t
Zffu Nov 13, 2025
b048e43
feat: removed unused types
Zffu Nov 13, 2025
5f9c495
feat: changed Chunk::data to use block_id_t
Zffu Nov 13, 2025
c14384a
feat: removed Chunk::set(int, int, chunk_block_data) and changed Chun…
Zffu Nov 13, 2025
d849dfe
feat: removed complex block hints
Zffu Nov 13, 2025
dd3f744
feat: removed block type indicator
Zffu Nov 13, 2025
b346566
feat: added fallback bitset for knowing if block has data
Zffu Nov 13, 2025
55197d6
feat: modified chunk load packet to handle correctly the chunks data
Zffu Nov 13, 2025
8ba2831
feat: modified Chunk::__update_cached_packet to ensure the new type c…
Zffu Nov 13, 2025
5325c0a
feat: removed complex chunks
Zffu Nov 13, 2025
9ae715b
feat: made Chunk::has_data private
Zffu Nov 13, 2025
4ad9794
feat: added BlockDataContainerBase
Zffu Nov 13, 2025
68908cb
feat: added Chunk::block_data
Zffu Nov 13, 2025
08800a3
feat: added declarations for Chunk::has_data and Chunk::get_data
Zffu Nov 13, 2025
ce68dfd
feat: added implementations for the functions
Zffu Nov 13, 2025
fd2f471
feat: added world level function declarations
Zffu Nov 13, 2025
7ed1448
feat: added world level function impls
Zffu Nov 13, 2025
f719e60
fix: migrated tests
Zffu Nov 13, 2025
55e126c
feat: started working on data tests
Zffu Nov 13, 2025
2dd1915
feat: added create_data decl
Zffu Nov 13, 2025
97fae33
feat: added remove_data decl
Zffu Nov 13, 2025
2072637
feat: added data constructor in BlockType and BlockType::create_data
Zffu Nov 13, 2025
485470c
feat: added get_or_create_data impl
Zffu Nov 13, 2025
785ae66
feat: added remove_data impl
Zffu Nov 13, 2025
5c8f54b
feat: added BlockType constructor with block data constructor
Zffu Nov 13, 2025
893936d
feat: added data creation tests
Zffu Nov 13, 2025
b10d600
feat: updated __erase
Zffu Nov 13, 2025
567499a
feat: added proper chunk save and loaed
Zffu Nov 13, 2025
895867d
feat: added region_file_chunk::read_from_chunk
Zffu Nov 13, 2025
4d6f20e
feat: added proper chunk saving on world savefile manager
Zffu Nov 13, 2025
1697ef0
feat: added Chunk::set_data
Zffu Nov 13, 2025
04d573c
feat: finished region_file_chunk::write_to_chunk
Zffu Nov 13, 2025
5aca03f
feat: finished world_savefile_manager:load_chnk
Zffu Nov 13, 2025
bf1bb3a
feat: moved endian stuff to num.hpp
Zffu Nov 13, 2025
1c4f5d0
fix: fixed world level functions not having the correct pos var name
Zffu Nov 13, 2025
d829335
fix: num functions not having the T template when called
Zffu Nov 13, 2025
45e6fbd
fix: added missing vtable
Zffu Nov 13, 2025
26fdde0
feat: made position use safe floor division instead of unsafe
Zffu Nov 13, 2025
f7181fd
fix: delete instead of free
Zffu Nov 13, 2025
cc8ee8c
feat: added block data destruction on Chunk free
Zffu Nov 13, 2025
6afad97
fix: wrong < way
Zffu Nov 13, 2025
b2ddcf7
fix: fixed some chunk issues
Zffu Nov 13, 2025
3300c1c
fix: fixed network buff not applying read pos correctly
Zffu Nov 13, 2025
abb6bb3
feat: fixed NetworkBuff reading issues
Zffu Nov 13, 2025
1853ce4
chore: removed leftover debug messages
Zffu Nov 13, 2025
28f397a
fix: FINALLY fixed AllocatedAndStaticParity
Zffu Nov 13, 2025
06c064d
memory leaks
Zffu Nov 13, 2025
a9c838b
fix: fixed an extremly dangerous memory leaké
Zffu Nov 15, 2025
ff36d64
chore: removed region files
Zffu Nov 15, 2025
8bf0cb8
feat: added world to .gitignore
Zffu Nov 15, 2025
1d3ea70
feat: changed file_handle behavior to safely create the file if it do…
Zffu Nov 15, 2025
1aa484a
feat: removed file_handle::open indicator to rely on the direct file …
Zffu Nov 15, 2025
2d77839
feat: modified file handle impls
Zffu Nov 15, 2025
38c3fa6
feat: added state dump
Zffu Nov 15, 2025
b1cbcd1
feat: started adding standart c++ file usage
Zffu Nov 15, 2025
3270fa6
feat: added positions in constructr
Zffu Nov 15, 2025
4bfde42
feat: added prepare functions
Zffu Nov 15, 2025
7b725a3
feat: added prepare internals impls
Zffu Nov 15, 2025
1e60851
feat: improved get_sizeé
Zffu Nov 15, 2025
5b47688
feat: made file_handle check file::bad instead of file::good as file:…
Zffu Nov 15, 2025
e728ebc
feat: added fail bit clearing in __prepare functions if file.fail() i…
Zffu Nov 15, 2025
bdf2b79
feat: added optional saving & file loading in World::unload_chunk and…
Zffu Nov 15, 2025
60ea5d1
feat: added region file tests
Zffu Nov 15, 2025
294cc92
feat: added github workflow to run checks on pull request
Zffu Nov 15, 2025
02ac63e
feat: removed file handle state dumping at creation
Zffu Nov 15, 2025
8d176e3
fix: fixed github workflow
Zffu Nov 15, 2025
5bd97a0
fix: made github workflow use submodules
Zffu Nov 15, 2025
974e9fc
fix: made github workflow compile gtest
Zffu Nov 15, 2025
ca185ef
fix:
Zffu Nov 15, 2025
00b4bc8
what the hell is a tab preventing this to build
Zffu Nov 15, 2025
124f80e
feat: changed compile with prepare_tests
Zffu Nov 15, 2025
a12d7bf
fix: fixed github workflow
Zffu Nov 15, 2025
c8821a9
fix: made prepare tests better
Zffu Nov 15, 2025
b754a2e
fix: fixed wrong task
Zffu Nov 15, 2025
02f13c4
fix: typing
Zffu Nov 15, 2025
9bf1429
feat: commented out ServerDisconnectTest as it is impossible to pass …
Zffu Nov 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/tests_on_pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Tests On Pull Request

on:
pull_request:
branches: ["master"]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Make prepare_tests.sh exec
run: chmod +x ./prepare_tests.sh
- name: Prepare test env
run: ./prepare_tests.sh
- name: Run tests
run: ./testing
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ extension_api.json
build

testing.*
testing
testing

world/
Empty file modified compile.sh
100755 → 100644
Empty file.
13 changes: 4 additions & 9 deletions includes/cavernfall/network/buff.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#pragma once

#include <utils/num.hpp>
#include <utils/typetraits.hpp>

#include <stdint.h>
#include <stdlib.h>

#include <string>

#define STATE_BIG_ENDIAN 0
#define STATE_LITTLE_ENDIAN 1

#define DEFAULT_ENDIAN_STATE STATE_LITTLE_ENDIAN

#define BUFFER_EXPANSION_REALLOC_RATE 2.5

Expand All @@ -27,7 +22,7 @@ class NetworkBuff {
* @param size the amount of bytes
*/
constexpr bool can_read(size_t size) {
return this->readPosition + size < this->sz;
return this->readPosition + size <= this->sz;
}

/**
Expand All @@ -36,7 +31,7 @@ class NetworkBuff {
* @param size the amount of bytes
*/
constexpr bool can_write(size_t size) {
return this->writePosition + size < this->sz;
return this->writePosition + size <= this->sz;
}

/**
Expand Down Expand Up @@ -78,7 +73,7 @@ class NetworkBuff {
~NetworkBuff();

constexpr uint8_t read_byte() {
if(!this->can_read(1)) return false;
if(!this->can_read(1)) return 0;
return this->buff[this->readPosition++];
}

Expand Down
52 changes: 41 additions & 11 deletions includes/cavernfall/world/block.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
#pragma once

#include <stdint.h>
#include <utils/file.hpp>

#include <functional>

namespace cavernfall::world {

typedef uint64_t chunk_block_data_t;
typedef uint32_t block_id_t;
typedef uint32_t block_data_t;
typedef uint8_t block_id_t;

/**
* @brief The base container for all block data.
* @details Manages the block data reading and writing from files
*/
class BlockDataContainerBase {
public:
/**
* @brief Reads / Loads the block data from the given file handle.
* @param handle the file handle
*/
virtual void read(cavernfall::fs::file_handle& handle) {}

/**
* @brief Writes / Saves the block data to the given file handle.
* @param handle the file handle
*/
virtual void write(cavernfall::fs::file_handle& handle) {}
};

class BlockType {
private:
std::function<BlockDataContainerBase*()> data_constructor;
bool has_data_constructor;

public:
int id;
bool tickable;
Expand All @@ -19,12 +43,21 @@ class BlockType {
this->id = 0;
this->tickable = false;
this->textureID = 0;
this->has_data_constructor = false;
}

BlockType(bool tickable, std::function<BlockDataContainerBase*()> constructor) {
this->id = 0;
this->tickable = tickable;
this->data_constructor = constructor;
this->has_data_constructor = true;
}

BlockType(bool tickable, int textureID) {
this->tickable = tickable;
this->textureID = textureID;
this->id = -1;
this->has_data_constructor = false;
}

/**
Expand All @@ -36,6 +69,11 @@ class BlockType {
*/
virtual void tick(long x, long y, long z);

/**
* @brief Creates a BlockDataContainerBase base corresponding to the type.
*/
BlockDataContainerBase* create_data();

};

//TODO: Make this static at compile time somehow
Expand Down Expand Up @@ -83,14 +121,6 @@ class BlockTypeRegister {
}
};

class ComplexBlock {
public:
chunk_block_data_t block;

ComplexBlock(chunk_block_data_t block);

};

/**
* @name fill_blocktype_register
* Fills the given register with all given block types
Expand Down
88 changes: 68 additions & 20 deletions includes/cavernfall/world/chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,17 @@ namespace cavernfall::world {

#define CHUNK_MEM_FROMIND(ind) (ind / WORLD_CHUNK_SIZE), (ind % WORLD_CHUNK_SIZE)

#define CHUNK_NO_BLOCK (chunk_block_data_t)(0)

typedef struct chunk_saved_data_t {
int height;
int sz;
chunk_block_data_t* data;
} chunk_saved_data_t;

typedef struct chunk_block_t {
block_id_t id;
block_data_t data;
BlockType* type;
ComplexBlock* block;
};
#define CHUNK_NO_BLOCK 0

/**
* A chunk is a 32*32 region in an Cavernfall world.
*/
class Chunk: public cavernfall::view::ViewEngine, public cavernfall::utils::chunkpos_t {
private:
std::bitset<CHUNK_SIZE_TOTAL> data_presence_cache;

#if !defined(CHUNK_NO_CACHED_PACKET)
void __update_cached_packet(size_t block_ind, chunk_block_data_t data);
void __update_cached_packet(size_t block_ind, block_id_t id);
#endif

inline void __erase(size_t ind);
Expand All @@ -59,8 +48,8 @@ class Chunk: public cavernfall::view::ViewEngine, public cavernfall::utils::chun
void viewer_remove(cavernfall::player::Player* player) override;

public:
uint8_t types[CHUNK_SIZE_TOTAL];
chunk_block_data_t data[CHUNK_SIZE_TOTAL];
block_id_t data[CHUNK_SIZE_TOTAL];
emhash7::HashMap<size_t, BlockDataContainerBase*> block_data;

#if !defined(CHUNK_NO_CACHED_PACKET)
cavernfall::net::NetworkBuff* load_packet;
Expand All @@ -77,15 +66,74 @@ class Chunk: public cavernfall::view::ViewEngine, public cavernfall::utils::chun
#if !defined(CHUNK_NO_CACHED_PACKET)
if(this->load_packet != nullptr) delete this->load_packet;
#endif

for(auto& it : this->block_data) {
delete it.second;
}

}

void set(int x, int z, block_id_t id);

void set(int x, int z, chunk_block_data_t blockData);
void set(int x, int z, BlockType* type);

chunk_block_t get(int x, int z);
block_id_t get(int x, int z);

/**
* @brief Checks if the block at the given position has data.
* @details Uses the data presence cache bitset to avoid repetitive HashMap calls.
*
* @param x the X coordinate of the block (within the chunk)
* @param z the Z coordinate of the block (within the chunk)
*/
bool has_data(int x, int z);

/**
* @brief Gets the stored chunk data of the block at the given position.
* @details First uses the presence cache bitset to check for presence then uses the Hashmap to get the data.
*
* @param x the X coordinate of the block (within the chunk)
* @param z the Z coordinate of the block (within the chunk)
* @return the chunk data if there is some or nullptr if none
*/
BlockDataContainerBase* get_data(int x, int z);

/**
* @brief Sets the stored block data of the block at the given position to the given container.
* @details First uses the presence cache bitset to check for presence then uses the Hashmap to set the data, also frees the older value if there is one.
*
* @param x the X coordinate of the block (within the chunk)
* @param z the Z coordinate of the block (within the chunk)
* @param base the new container
*/
void set_data(int x, int z, BlockDataContainerBase* base);

/**
* @brief Creates a BlockDataContainerBase for the block at the given position or get the current one.
* @details Creates a BlockDataContainerBase by taking the constructor from the block type corresponding to the current block at the position.
*
* @param x the X coordinate of the block (within the chunk)
* @param z the Z coordinate of the block (within the chunk)
*
* @info will do nothing if chunk data already exists for the block.
* @return the created / gathered block data
*/
BlockDataContainerBase* get_or_create_data(int x, int z);

/**
* @brief Removes the data for the block at the given position.
* @details Erases the BlockDataContainerBase for the block at the given position and removes it from the block data having cache.
*
* @param x the X coordinate of the block (within the chunk)
* @param z the Z coordinate of the block (within the chunk)
*
* @info will do nothing if the block data doesn't have any data
*/
void remove_data(int x, int z);

/**
* @brief Get the current amount of data containers within the chunk.
*/
size_t get_active_data_containers();

/**
* @name is_chunk_in_render_distance
Expand Down
Loading