Skip to content

Commit

Permalink
Work on BlockVoxelStructure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Oct 30, 2020
1 parent 9a151ff commit fd464c0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 77 deletions.
99 changes: 46 additions & 53 deletions world/block_voxel_structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,95 +22,88 @@ SOFTWARE.

#include "block_voxel_structure.h"

int BlockVoxelStructure::get_channel_count() const {
return _channel_count;
int BlockVoxelStructure::get_channel_type() const {
return _channel_type;
}
void BlockVoxelStructure::set_channel_count(const int value) {
_channel_count = value;
void BlockVoxelStructure::set_channel_type(const int value) {
_channel_type = value;
}

uint8_t BlockVoxelStructure::get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
int BlockVoxelStructure::get_channel_isolevel() const {
return _channel_isolevel;
}
void BlockVoxelStructure::set_channel_isolevel(const int value) {
_channel_isolevel = value;
}

if (!_data.has(p))
return 0;
int BlockVoxelStructure::get_voxel_type(int p_x, int p_y, int p_z) const {
DataEntry p;

PoolByteArray arr = _data[p];
for (int i = 0; i < _data.size(); ++i) {
p = _data[i];

ERR_FAIL_INDEX_V(arr.size(), p_channel_index, 0);
if (p.x == p_x && p.y == p_y && p.z == p_z) {
return p.data_type;
}
}

return arr[p_channel_index];
return 0;
}
void BlockVoxelStructure::set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index) {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;

PoolByteArray arr;
int BlockVoxelStructure::get_voxel_isolevel(int p_x, int p_y, int p_z) const {
DataEntry p;

if (!_data.has(p)) {
arr.resize(_channel_count);
for (int i = 0; i < _data.size(); ++i) {
p = _data[i];

for (int i = 0; i < _channel_count; ++i) {
arr.set(i, p_value);
if (p.x == p_x && p.y == p_y && p.z == p_z) {
return p.data_isolevel;
}
} else {
arr = _data[p];
}

_data[p] = arr;
return 0;
}

PoolByteArray BlockVoxelStructure::get_voxel_data(int p_x, int p_y, int p_z) const {
VSIntPos p;
void BlockVoxelStructure::set_voxel(int p_x, int p_y, int p_z, int p_type, int p_isolevel) {
DataEntry p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
p.data_type = p_type;
p.data_isolevel = p_isolevel;

if (!_data.has(p))
return PoolByteArray();

return _data[p];
_data.push_back(p);
}
void BlockVoxelStructure::set_voxel_data(PoolByteArray p_arr, int p_x, int p_y, int p_z) {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;

_data[p] = p_arr;
void BlockVoxelStructure::_write_to_chunk(Ref<VoxelChunk> chunk) {
//Ref<VoxelChunk> c = Object::cast_to<VoxelChunk>(chunk);
}

//void BlockVoxelStructure::_write_to_chunk(Node *chunk) {
//Ref<VoxelChunk> c = Object::cast_to<VoxelChunk>(chunk);
//}

void BlockVoxelStructure::clear() {
_data.clear();
}

BlockVoxelStructure::BlockVoxelStructure() {
_channel_count = 0;
_channel_type = 0;
_channel_isolevel = 0;
}

BlockVoxelStructure::~BlockVoxelStructure() {
_data.clear();
}

void BlockVoxelStructure::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_channel_count"), &BlockVoxelStructure::get_channel_count);
ClassDB::bind_method(D_METHOD("set_channel_count", "value"), &BlockVoxelStructure::set_channel_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_count"), "set_channel_count", "get_channel_count");
ClassDB::bind_method(D_METHOD("get_channel_type"), &BlockVoxelStructure::get_channel_type);
ClassDB::bind_method(D_METHOD("set_channel_type", "value"), &BlockVoxelStructure::set_channel_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_type"), "set_channel_type", "get_channel_type");

ClassDB::bind_method(D_METHOD("get_channel_isolevel"), &BlockVoxelStructure::get_channel_isolevel);
ClassDB::bind_method(D_METHOD("set_channel_isolevel", "value"), &BlockVoxelStructure::set_channel_isolevel);
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_isolevel"), "set_channel_isolevel", "get_channel_isolevel");

ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "channel_index"), &BlockVoxelStructure::get_voxel, DEFVAL(0));
ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "channel_index"), &BlockVoxelStructure::set_voxel, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_voxel_type", "x", "y", "z"), &BlockVoxelStructure::get_voxel_type);
ClassDB::bind_method(D_METHOD("get_voxel_isolevel", "x", "y", "z"), &BlockVoxelStructure::get_voxel_isolevel);

ClassDB::bind_method(D_METHOD("get_voxel_data", "x", "y", "z"), &BlockVoxelStructure::get_voxel_data);
ClassDB::bind_method(D_METHOD("set_voxel_data", "arr", "x", "y", "z"), &BlockVoxelStructure::set_voxel_data);
ClassDB::bind_method(D_METHOD("set_voxel", "x", "y", "z", "type", "isolevel"), &BlockVoxelStructure::set_voxel);

//ClassDB::bind_method(D_METHOD("_write_to_chunk", "chunk"), &BlockVoxelStructure::_write_to_chunk);
ClassDB::bind_method(D_METHOD("_write_to_chunk", "chunk"), &BlockVoxelStructure::_write_to_chunk);
}
42 changes: 18 additions & 24 deletions world/block_voxel_structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ SOFTWARE.

#include pool_vector_h
include_pool_vector
#include "core/hash_map.h"
#include "core/vector.h"
#include "voxel_chunk.h"
;

class BlockVoxelStructure : public VoxelStructure {
class BlockVoxelStructure : public VoxelStructure {
GDCLASS(BlockVoxelStructure, VoxelStructure);

public:
int get_channel_count() const;
void set_channel_count(const int value);
int get_channel_type() const;
void set_channel_type(const int value);

uint8_t get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const;
void set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index);
int get_channel_isolevel() const;
void set_channel_isolevel(const int value);

PoolByteArray get_voxel_data(int p_x, int p_y, int p_z) const;
void set_voxel_data(PoolByteArray p_arr, int p_x, int p_y, int p_z);
int get_voxel_type(int p_x, int p_y, int p_z) const;
int get_voxel_isolevel(int p_x, int p_y, int p_z) const;

//void _write_to_chunk(Node *chunk);
void set_voxel(int p_x, int p_y, int p_z, int p_type, int p_isolevel);

void _write_to_chunk(Ref<VoxelChunk> chunk);

void clear();

Expand All @@ -56,28 +59,19 @@ include_pool_vector
static void _bind_methods();

public:
struct VSIntPos {
struct DataEntry {
int x;
int y;
int z;
};

struct VSIntPosHasher {
static _FORCE_INLINE_ uint32_t hash(const VSIntPos &v) {
uint32_t hash = hash_djb2_one_32(v.x);
hash = hash_djb2_one_32(v.y, hash);
return hash_djb2_one_32(v.z, hash);
}
int data_type;
int data_isolevel;
};

private:
int _channel_count;
int _channel_type;
int _channel_isolevel;

HashMap<VSIntPos, PoolByteArray, VSIntPosHasher> _data;
Vector<DataEntry> _data;
};

_FORCE_INLINE_ bool operator==(const BlockVoxelStructure::VSIntPos &a, const BlockVoxelStructure::VSIntPos &b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}

#endif

0 comments on commit fd464c0

Please sign in to comment.