authority/modules/terrain/terrain.cpp

208 lines
5.3 KiB
C++

#include "terrain.h"
#include "terrain/terrain_chunk.h"
#include "terrain/terrain_modifier.h"
void Terrain::_bind_methods() {
BIND_PROPERTY(Variant::INT, side_length);
BIND_PROPERTY(Variant::INT, chunk_size);
BIND_PROPERTY(Variant::INT, detail);
BIND_PROPERTY(Variant::INT, thread_count);
}
void Terrain::child_order_changed() {
this->modifiers.clear();
for (Variant var : get_children()) {
if (TerrainModifier * mod{ cast_to<TerrainModifier>(var) }) {
mod->set_terrain(this);
this->modifiers.push_back(mod);
}
}
}
void Terrain::update_meshes() {
size_t num{ 1 };
this->dirty_meshes_lock.lock();
num = num > this->dirty_meshes.size() ? this->dirty_meshes.size() : num;
this->dirty_meshes_lock.unlock();
for (size_t i{ 0 }; i < num; i++) {
this->dirty_meshes_lock.lock();
TerrainChunkMesh *mesh{ this->dirty_meshes[0] };
this->dirty_meshes.remove_at(0);
this->dirty_meshes_lock.unlock();
mesh->apply_new_mesh();
}
if (this->dirty_meshes.is_empty()) {
set_process(false);
}
}
void Terrain::_notification(int what) {
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
if (!is_ready()) {
connect("child_order_changed", callable_mp(this, &self_type::child_order_changed));
}
return;
case NOTIFICATION_READY:
construct_chunk_grid();
return;
case NOTIFICATION_PROCESS:
update_meshes();
return;
case NOTIFICATION_EXIT_TREE:
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
return;
}
}
void Terrain::generate_meshes_thread(void *terrain) {
Terrain *self{ static_cast<Terrain *>(terrain) };
print_line("thread", Thread::get_caller_id(), "start");
for (;;) {
self->workload_lock.lock();
if (self->threads_stop) {
self->workload_lock.unlock();
print_line(Thread::get_caller_id(), "exiting");
break;
}
if (self->workload.is_empty()) {
self->workload_lock.unlock();
Thread::yield();
continue;
}
TerrainChunkMesh *mesh{ self->workload[0] };
self->workload.remove_at(0);
self->workload_lock.unlock();
if (!mesh->is_inside_tree()) {
print_line(Thread::get_caller_id(), "mesh is outside tree, exiting");
break;
}
mesh->update_mesh();
Thread::yield();
}
print_line(Thread::get_caller_id(), "done");
return;
}
void Terrain::construct_chunk_grid() {
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
this->workload_lock.lock();
for (TerrainChunkMesh *mesh : this->meshes) {
remove_child(mesh);
mesh->queue_free();
}
size_t const chunks_per_side{ this->side_length / this->chunk_size };
Vector3 const origin{ (float)this->chunk_size / 2.f, 0.f, (float)this->chunk_size / 2.f };
this->meshes.clear();
this->workload.clear();
for (size_t y{ 0 }; y < chunks_per_side; ++y) {
for (size_t x{ 0 }; x < chunks_per_side; ++x) {
TerrainChunkMesh *chunk{ memnew(TerrainChunkMesh) };
chunk->set_size(this->chunk_size);
chunk->set_detail(this->detail);
chunk->set_terrain(this);
chunk->set_position(origin + Vector3{ (float)this->chunk_size * (float)x, 0.f, (float)this->chunk_size * (float)y });
add_child(chunk);
chunk->set_owner(this);
this->meshes.push_back(chunk);
this->workload.push_back(chunk);
}
}
this->threads_stop = false;
this->dirty_meshes.clear();
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.start(&self_type::generate_meshes_thread, this);
}
}
float Terrain::height_at(Vector2 world_coordinate) {
float height{ 0 };
for (TerrainModifier *mod : this->modifiers) {
if (!mod->is_inside_tree()) {
return height;
}
height = mod->evaluate_at(world_coordinate, height);
}
return height;
}
void Terrain::push_changed(Rect2 area) {
for (TerrainChunkMesh *mesh : this->meshes) {
this->workload_lock.lock();
if (area.intersects(mesh->get_bounds()) && !this->workload.has(mesh)) {
workload.push_back(mesh);
}
this->workload_lock.unlock();
}
}
void Terrain::mesh_dirty(TerrainChunkMesh *mesh) {
this->dirty_meshes_lock.lock();
this->dirty_meshes.push_back(mesh);
callable_mp(cast_to<Node>(this), &self_type::set_process).call_deferred(true);
this->dirty_meshes_lock.unlock();
}
void Terrain::set_side_length(size_t length) {
this->side_length = length;
if (is_inside_tree()) {
construct_chunk_grid();
}
}
size_t Terrain::get_side_length() const {
return this->side_length;
}
void Terrain::set_chunk_size(size_t size) {
this->chunk_size = size;
if (is_inside_tree()) {
construct_chunk_grid();
}
}
size_t Terrain::get_chunk_size() const {
return this->chunk_size;
}
void Terrain::set_detail(size_t detail) {
this->detail = detail;
if (is_inside_tree()) {
construct_chunk_grid();
}
}
size_t Terrain::get_detail() const {
return this->detail;
}
void Terrain::set_thread_count(size_t num) {
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
this->threads_stop = false;
this->threads.resize_initialized(num);
for (Thread &thread : this->threads) {
thread.start(&self_type::generate_meshes_thread, this);
}
}
size_t Terrain::get_thread_count() const {
return this->threads.size();
}