2020-01-31 19:52:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
#ifndef VOXEL_WORLD_H
|
|
|
|
#define VOXEL_WORLD_H
|
|
|
|
|
2020-04-09 12:34:39 +02:00
|
|
|
#include "core/version.h"
|
|
|
|
|
|
|
|
#if VERSION_MAJOR < 4
|
|
|
|
#include "scene/3d/navigation.h"
|
|
|
|
#include "scene/3d/spatial.h"
|
|
|
|
#else
|
|
|
|
#include "scene/3d/navigation_3d.h"
|
|
|
|
#include "scene/3d/node_3d.h"
|
|
|
|
|
|
|
|
typedef class Navigation3D Navigation;
|
|
|
|
typedef class Node3D Spatial;
|
|
|
|
#endif
|
|
|
|
|
2019-11-19 16:33:06 +01:00
|
|
|
#include "core/engine.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "core/hash_map.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-11-10 01:03:48 +01:00
|
|
|
#include "../areas/world_area.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "../level_generator/voxelman_level_generator.h"
|
|
|
|
#include "../library/voxelman_library.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-11-19 14:42:21 +01:00
|
|
|
#include "core/os/os.h"
|
|
|
|
|
2020-04-16 13:40:39 +02:00
|
|
|
class VoxelStructure;
|
2019-08-12 20:40:05 +02:00
|
|
|
class VoxelChunk;
|
2020-04-15 13:06:45 +02:00
|
|
|
class VoxelChunkPropData;
|
2019-08-12 20:40:05 +02:00
|
|
|
|
2019-10-05 22:53:48 +02:00
|
|
|
class VoxelWorld : public Navigation {
|
|
|
|
GDCLASS(VoxelWorld, Navigation);
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2020-04-16 21:59:30 +02:00
|
|
|
public:
|
|
|
|
enum ChannelTypeInfo {
|
|
|
|
CHANNEL_TYPE_INFO_TYPE = 0,
|
|
|
|
CHANNEL_TYPE_INFO_ISOLEVEL,
|
2020-04-19 23:35:47 +02:00
|
|
|
CHANNEL_TYPE_INFO_LIQUID_FLOW,
|
2020-04-16 21:59:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const String BINDING_STRING_CHANNEL_TYPE_INFO;
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
public:
|
2020-03-31 13:25:31 +02:00
|
|
|
bool get_editable() const;
|
|
|
|
void set_editable(const bool value);
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
int get_chunk_size_x() const;
|
|
|
|
void set_chunk_size_x(const int value);
|
|
|
|
|
|
|
|
int get_chunk_size_y() const;
|
|
|
|
void set_chunk_size_y(const int value);
|
|
|
|
|
|
|
|
int get_chunk_size_z() const;
|
|
|
|
void set_chunk_size_z(const int value);
|
|
|
|
|
2020-02-14 03:19:15 +01:00
|
|
|
int get_data_margin_start() const;
|
|
|
|
void set_data_margin_start(const int value);
|
|
|
|
|
|
|
|
int get_data_margin_end() const;
|
|
|
|
void set_data_margin_end(const int value);
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
int get_current_seed() const;
|
|
|
|
void set_current_seed(const int value);
|
2019-11-19 14:42:21 +01:00
|
|
|
|
2020-04-16 17:10:04 +02:00
|
|
|
bool get_use_threads() const;
|
|
|
|
void set_use_threads(const bool value);
|
2019-11-19 14:42:21 +01:00
|
|
|
|
2020-03-31 13:32:06 +02:00
|
|
|
int get_max_concurrent_generations() const;
|
|
|
|
void set_max_concurrent_generations(const int value);
|
2020-01-09 04:29:05 +01:00
|
|
|
|
2020-03-31 13:32:06 +02:00
|
|
|
int get_max_frame_chunk_build_steps() const;
|
|
|
|
void set_max_frame_chunk_build_steps(const int value);
|
2020-03-04 14:59:34 +01:00
|
|
|
|
|
|
|
Ref<VoxelmanLibrary> get_library();
|
2020-04-16 17:10:04 +02:00
|
|
|
void set_library(const Ref<VoxelmanLibrary> &library);
|
2020-01-09 04:29:05 +01:00
|
|
|
|
|
|
|
Ref<VoxelmanLevelGenerator> get_level_generator() const;
|
2020-04-16 17:10:04 +02:00
|
|
|
void set_level_generator(const Ref<VoxelmanLevelGenerator> &level_generator);
|
2019-08-12 20:40:05 +02:00
|
|
|
|
|
|
|
float get_voxel_scale() const;
|
|
|
|
void set_voxel_scale(const float value);
|
|
|
|
|
|
|
|
int get_chunk_spawn_range() const;
|
|
|
|
void set_chunk_spawn_range(const int value);
|
|
|
|
|
2020-04-16 17:10:04 +02:00
|
|
|
NodePath get_player_path() const;
|
|
|
|
void set_player_path(const NodePath &player_path);
|
2020-01-09 04:29:05 +01:00
|
|
|
|
|
|
|
Spatial *get_player() const;
|
2019-08-12 20:40:05 +02:00
|
|
|
void set_player(Spatial *player);
|
|
|
|
void set_player_bind(Node *player);
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//World Areas
|
2019-11-10 01:03:48 +01:00
|
|
|
Ref<WorldArea> get_world_area(const int index) const;
|
2020-04-16 13:40:39 +02:00
|
|
|
void add_world_area(const Ref<WorldArea> &area);
|
2019-11-10 01:03:48 +01:00
|
|
|
void remove_world_area(const int index);
|
|
|
|
void clear_world_areas();
|
|
|
|
int get_world_area_count() const;
|
|
|
|
|
2020-04-16 13:40:39 +02:00
|
|
|
//Voxel Structures
|
|
|
|
Ref<VoxelStructure> get_voxel_structure(const int index) const;
|
|
|
|
void add_voxel_structure(const Ref<VoxelStructure> &structure);
|
|
|
|
void remove_voxel_structure(const Ref<VoxelStructure> &structure);
|
|
|
|
void remove_voxel_structure_index(const int index);
|
|
|
|
void clear_voxel_structures();
|
|
|
|
int get_voxel_structure_count() const;
|
|
|
|
void add_voxel_structure_at_position(Ref<VoxelStructure> structure, const Vector3 &world_position);
|
|
|
|
|
2020-04-16 14:07:10 +02:00
|
|
|
Vector<Variant> get_voxel_structures();
|
|
|
|
void set_voxel_structures(const Vector<Variant> &structures);
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//Chunks
|
2020-04-02 21:28:19 +02:00
|
|
|
void add_chunk(Ref<VoxelChunk> chunk, const int x, const int y, const int z);
|
2020-03-04 14:59:34 +01:00
|
|
|
bool has_chunk(const int x, const int y, const int z) const;
|
2020-04-02 21:28:19 +02:00
|
|
|
Ref<VoxelChunk> get_chunk(const int x, const int y, const int z);
|
|
|
|
Ref<VoxelChunk> remove_chunk(const int x, const int y, const int z);
|
|
|
|
Ref<VoxelChunk> remove_chunk_index(const int index);
|
|
|
|
Ref<VoxelChunk> get_chunk_index(const int index);
|
2019-08-12 20:40:05 +02:00
|
|
|
|
|
|
|
int get_chunk_count() const;
|
|
|
|
|
2020-04-16 17:10:04 +02:00
|
|
|
void add_to_generation_queue(const Ref<VoxelChunk> &chunk);
|
|
|
|
Ref<VoxelChunk> get_generation_queue_index(const int index);
|
|
|
|
void remove_generation_queue_index(const int index);
|
|
|
|
int get_generation_queue_size() const;
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2020-04-16 17:10:04 +02:00
|
|
|
void add_to_generation(const Ref<VoxelChunk> &chunk);
|
|
|
|
Ref<VoxelChunk> get_generation_index(const int index);
|
|
|
|
void remove_generation_index(const int index);
|
|
|
|
int get_generation_size() const;
|
2019-11-19 16:33:06 +01:00
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
void clear_chunks();
|
2019-11-19 16:33:06 +01:00
|
|
|
|
2020-04-16 17:10:04 +02:00
|
|
|
Ref<VoxelChunk> get_or_create_chunk(const int x, const int y, const int z);
|
|
|
|
Ref<VoxelChunk> create_chunk(const int x, const int y, const int z);
|
2019-11-19 14:42:21 +01:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void generate_chunk(Ref<VoxelChunk> chunk);
|
2020-03-04 14:59:34 +01:00
|
|
|
|
|
|
|
bool can_chunk_do_build_step();
|
2020-03-04 15:21:32 +01:00
|
|
|
bool is_position_walkable(const Vector3 &p_pos);
|
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void on_chunk_mesh_generation_finished(Ref<VoxelChunk> p_chunk);
|
|
|
|
|
|
|
|
Vector<Variant> get_chunks();
|
|
|
|
void set_chunks(const Vector<Variant> &chunks);
|
2019-11-19 14:42:21 +01:00
|
|
|
|
2020-04-15 13:06:45 +02:00
|
|
|
//Props
|
|
|
|
void add_prop(Ref<VoxelChunkPropData> prop);
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//Lights
|
|
|
|
void add_light(const Ref<VoxelLight> &light);
|
|
|
|
Ref<VoxelLight> get_light(const int index);
|
|
|
|
void remove_light(const int index);
|
|
|
|
int get_light_count() const;
|
|
|
|
void clear_lights();
|
|
|
|
|
|
|
|
Vector<Variant> get_lights();
|
|
|
|
void set_lights(const Vector<Variant> &chunks);
|
|
|
|
|
2020-04-16 14:44:22 +02:00
|
|
|
//Helpers
|
2020-04-17 10:15:50 +02:00
|
|
|
uint8_t get_voxel_at_world_position(const Vector3 &world_position, const int channel_index);
|
2020-04-16 14:44:22 +02:00
|
|
|
void set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index);
|
2020-04-17 10:15:50 +02:00
|
|
|
Ref<VoxelChunk> get_chunk_at_world_position(const Vector3 &world_position);
|
|
|
|
Ref<VoxelChunk> get_or_create_chunk_at_world_position(const Vector3 &world_position);
|
|
|
|
|
2020-04-16 21:59:30 +02:00
|
|
|
int get_channel_index_info(const ChannelTypeInfo channel_type);
|
2020-04-16 14:44:22 +02:00
|
|
|
|
2019-08-12 13:12:42 +02:00
|
|
|
VoxelWorld();
|
2019-06-07 19:13:07 +02:00
|
|
|
~VoxelWorld();
|
2019-06-07 01:33:41 +02:00
|
|
|
|
|
|
|
protected:
|
2020-04-05 01:36:41 +02:00
|
|
|
virtual void _generate_chunk(Ref<VoxelChunk> chunk);
|
|
|
|
virtual Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
|
2020-04-16 21:59:30 +02:00
|
|
|
virtual int _get_channel_index_info(const ChannelTypeInfo channel_type);
|
2020-03-04 14:59:34 +01:00
|
|
|
|
2019-11-19 14:42:21 +01:00
|
|
|
virtual void _notification(int p_what);
|
2019-06-07 01:33:41 +02:00
|
|
|
static void _bind_methods();
|
|
|
|
|
2019-11-18 22:22:41 +01:00
|
|
|
public:
|
2019-11-18 21:53:33 +01:00
|
|
|
struct IntPos {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int z;
|
|
|
|
|
|
|
|
IntPos() {
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
z = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntPos(int p_x, int p_y, int p_z) {
|
|
|
|
x = p_x;
|
|
|
|
y = p_y;
|
|
|
|
z = p_z;
|
|
|
|
}
|
2020-03-04 10:48:53 +01:00
|
|
|
|
|
|
|
IntPos(const Vector3 &p) {
|
|
|
|
x = p.x;
|
|
|
|
y = p.y;
|
|
|
|
z = p.z;
|
|
|
|
}
|
2019-11-18 21:53:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IntPosHasher {
|
|
|
|
static _FORCE_INLINE_ uint32_t hash(const IntPos &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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
private:
|
2020-03-31 13:25:31 +02:00
|
|
|
bool _editable;
|
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
bool _is_priority_generation;
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
int _chunk_size_x;
|
2019-11-18 21:53:33 +01:00
|
|
|
int _chunk_size_y;
|
|
|
|
int _chunk_size_z;
|
2019-11-19 14:42:21 +01:00
|
|
|
int _current_seed;
|
2020-02-14 03:19:15 +01:00
|
|
|
int _data_margin_start;
|
|
|
|
int _data_margin_end;
|
2020-01-09 04:29:05 +01:00
|
|
|
|
|
|
|
Ref<VoxelmanLibrary> _library;
|
|
|
|
Ref<VoxelmanLevelGenerator> _level_generator;
|
2019-08-12 20:40:05 +02:00
|
|
|
float _voxel_scale;
|
|
|
|
int _chunk_spawn_range;
|
2020-01-09 04:29:05 +01:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
HashMap<IntPos, Ref<VoxelChunk>, IntPosHasher> _chunks;
|
|
|
|
Vector<Ref<VoxelChunk> > _chunks_vector;
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-11-10 01:03:48 +01:00
|
|
|
Vector<Ref<WorldArea> > _world_areas;
|
|
|
|
|
2020-04-16 13:40:39 +02:00
|
|
|
Vector<Ref<VoxelStructure> > _voxel_structures;
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
NodePath _player_path;
|
|
|
|
Spatial *_player;
|
2019-11-19 14:42:21 +01:00
|
|
|
|
|
|
|
bool _use_threads;
|
2020-03-31 13:32:06 +02:00
|
|
|
int _max_concurrent_generations;
|
2020-04-02 21:28:19 +02:00
|
|
|
Vector<Ref<VoxelChunk> > _generation_queue;
|
|
|
|
Vector<Ref<VoxelChunk> > _generating;
|
2020-03-31 13:32:06 +02:00
|
|
|
int _max_frame_chunk_build_steps;
|
|
|
|
int _num_frame_chunk_build_steps;
|
2020-04-15 12:41:52 +02:00
|
|
|
|
|
|
|
Vector<Ref<VoxelLight> > _lights;
|
2019-06-07 01:33:41 +02:00
|
|
|
};
|
|
|
|
|
2019-11-18 22:22:41 +01:00
|
|
|
_FORCE_INLINE_ bool operator==(const VoxelWorld::IntPos &a, const VoxelWorld::IntPos &b) {
|
|
|
|
return a.x == b.x && a.y == b.y && a.z == b.z;
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:59:30 +02:00
|
|
|
VARIANT_ENUM_CAST(VoxelWorld::ChannelTypeInfo);
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
#endif
|