From 14c239fd8716988b837c2fcfe20fe7d9f11dd515 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 23 May 2020 22:01:38 +0200 Subject: [PATCH] Ported the basic test biome generation method to a new BiomeBase class. --- SCsub | 2 + biome_base.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++++ biome_base.h | 43 +++++++++++++++++ register_types.cpp | 3 ++ 4 files changed, 165 insertions(+) create mode 100644 biome_base.cpp create mode 100644 biome_base.h diff --git a/SCsub b/SCsub index f8864aa..44a4201 100644 --- a/SCsub +++ b/SCsub @@ -11,6 +11,8 @@ module_env = env.Clone() sources = [ "register_types.cpp", + + "biome_base.cpp" ] if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes': diff --git a/biome_base.cpp b/biome_base.cpp new file mode 100644 index 0000000..3081fcd --- /dev/null +++ b/biome_base.cpp @@ -0,0 +1,117 @@ +/* +Copyright (c) 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. +*/ + +#include "biome_base.h" + +#include "core/math/math_funcs.h" + +#include "core/engine.h" + +#include "../voxelman/world/default/voxel_chunk_default.h" +#include "../voxelman/world/voxel_chunk.h" + +#include "../entity_spell_system/singletons/ess.h" +#include "../entity_spell_system/spawners/ess_entity_spawner.h" +#include "../opensimplex/open_simplex_noise.h" + +void BiomeBase::generate_simple_terrarin(Ref chunk, bool spawn_mobs) { + Ref noise; + noise.instance(); + noise->set_seed(10 * get_current_seed()); + noise->set_octaves(4); + noise->set_period(280.0); + noise->set_persistence(0.8); + + Ref terr_noise; + terr_noise.instance(); + terr_noise->set_seed(10 * 321 + 112 * get_current_seed()); + terr_noise->set_octaves(4); + terr_noise->set_period(90.0); + terr_noise->set_persistence(0.9); + + Ref det_noise; + det_noise.instance(); + det_noise->set_seed(10 * 3231 + 112 * get_current_seed()); + det_noise->set_octaves(6); + det_noise->set_period(80.0); + det_noise->set_persistence(0.3); + + for (int x = -chunk->get_margin_start(); x < chunk->get_size_x() + chunk->get_margin_end(); ++x) { + for (int z = -chunk->get_margin_start(); z < chunk->get_size_z() + chunk->get_margin_end(); ++z) { + float val = noise->get_noise_2d(x + (chunk->get_position_x() * chunk->get_size_x()), z + (chunk->get_position_z() * chunk->get_size_z())); + val *= val; + val *= 200.0; + val += 2.0; + + float tv = terr_noise->get_noise_2d(x + (chunk->get_position_x() * chunk->get_size_x()), z + (chunk->get_position_z() * chunk->get_size_z())); + tv *= tv * tv * tv; + val += tv * 2.0; + + float dval = noise->get_noise_2d(x + (chunk->get_position_x() * chunk->get_size_x()), z + (chunk->get_position_z() * chunk->get_size_z())); + + val += dval * 6.0; + + int v = (int(val)); + + v -= chunk->get_position_y() * (chunk->get_size_y()); + + if (v > chunk->get_size_y() + chunk->get_margin_end()) + v = chunk->get_size_y() + chunk->get_margin_end(); + + for (int y = -chunk->get_margin_start(); y < v; y++) { + Math::seed(x + (chunk->get_position_x() * chunk->get_size_x()) + z + (chunk->get_position_z() * chunk->get_size_z()) + y + (chunk->get_position_y() * chunk->get_size_y())); + + if (v < 2) + chunk->set_voxel(1, x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE); + else if (v == 2) + chunk->set_voxel(3, x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE); + else + chunk->set_voxel(2, x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE); + + float val2 = (val - static_cast(val)) * 4.0; + val2 = static_cast(val2); + val2 /= 4.0; + + chunk->set_voxel(static_cast(255.0 * val2), x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + } + } + } + + //if (!Engine::get_singleton()->is_editor_hint() && chunk->get_position_y() == 0 && spawn_mobs) { + // Vector3 v = Vector3(chunk->get_position_x() * chunk->get_size_x() * chunk->get_voxel_scale() + chunk->get_size_x() / 2, + // (chunk->get_position_y() + 1) * chunk->get_size_y() * chunk->get_voxel_scale(), + // chunk->get_position_z()) * + // (chunk->get_size_z() * chunk->get_voxel_scale() + chunk->get_size_z() / 2); + // + // ESS::get_singleton()->get_entity_spawner()->call("spawn_mob", 0, Math::rand() % 3, v); + // } +} + +BiomeBase::BiomeBase() { +} + +BiomeBase::~BiomeBase() { +} + +void BiomeBase::_bind_methods() { + ClassDB::bind_method(D_METHOD("generate_simple_terrarin", "chunk", "spawn_mobs"), &BiomeBase::generate_simple_terrarin); +} \ No newline at end of file diff --git a/biome_base.h b/biome_base.h new file mode 100644 index 0000000..4fa6bc1 --- /dev/null +++ b/biome_base.h @@ -0,0 +1,43 @@ +/* +Copyright (c) 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. +*/ + +#ifndef BIOME_BASE_H +#define BIOME_BASE_H + +#include "../world_generator/main/biome.h" + +class VoxelChunk; + +class BiomeBase : public Biome { + GDCLASS(BiomeBase, Biome); + +public: + void generate_simple_terrarin(Ref chunk, bool spawn_mobs); + + BiomeBase(); + ~BiomeBase(); + +protected: + static void _bind_methods(); +}; + +#endif \ No newline at end of file diff --git a/register_types.cpp b/register_types.cpp index 2cd3b87..9fc2ec8 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -24,7 +24,10 @@ SOFTWARE. */ +#include "biome_base.h" + void register_broken_seals_module_types() { + ClassDB::register_class(); } void unregister_broken_seals_module_types() {