world_generator/main/dungeon.cpp

472 lines
15 KiB
C++
Raw Normal View History

/*
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-09-24 02:42:42 +02:00
#include "dungeon.h"
2019-09-22 22:12:20 +02:00
int Dungeon::get_current_seed() {
return _current_seed;
2019-10-18 09:00:01 +02:00
}
void Dungeon::set_current_seed(int value) {
_current_seed = value;
2019-10-18 09:00:01 +02:00
}
2019-11-01 15:42:03 +01:00
Vector2 Dungeon::get_level_range() {
return _level_range;
}
void Dungeon::set_level_range(Vector2 value) {
_level_range = value;
}
2019-10-18 09:43:45 +02:00
//Position
2019-10-18 09:00:01 +02:00
int Dungeon::get_posx() {
return _posx;
}
void Dungeon::set_posx(int value) {
_posx = value;
}
int Dungeon::get_posy() {
return _posy;
}
void Dungeon::set_posy(int value) {
_posy = value;
}
int Dungeon::get_posz() {
return _posz;
}
void Dungeon::set_posz(int value) {
_posz = value;
}
2019-10-18 09:43:45 +02:00
//Size
2019-10-18 09:00:01 +02:00
int Dungeon::get_sizex() {
return _sizex;
}
void Dungeon::set_sizex(int value) {
_sizex = value;
}
int Dungeon::get_sizey() {
return _sizey;
}
void Dungeon::set_sizey(int value) {
_sizey = value;
}
int Dungeon::get_sizez() {
return _sizez;
}
void Dungeon::set_sizez(int value) {
_sizez = value;
}
2019-10-18 10:22:11 +02:00
int Dungeon::get_room_count() {
return _room_count;
}
void Dungeon::set_room_count(int value) {
_room_count = value;
}
#ifdef VOXELMAN_PRESENT
Ref<EnvironmentData> Dungeon::get_environment() {
return _environment;
}
void Dungeon::set_environment(Ref<EnvironmentData> value) {
_environment = value;
}
#endif
Ref<DungeonData> Dungeon::get_data() {
return _data;
}
void Dungeon::set_data(Ref<DungeonData> value) {
_data = value;
}
2019-10-18 09:00:01 +02:00
//Rooms
Ref<DungeonRoom> Dungeon::get_dungeon_room(const int index) const {
ERR_FAIL_INDEX_V(index, _dungeon_rooms.size(), Ref<DungeonRoom>());
return _dungeon_rooms.get(index);
}
void Dungeon::set_dungeon_room(const int index, const Ref<DungeonRoom> dungeon_room) {
ERR_FAIL_INDEX(index, _dungeon_rooms.size());
_dungeon_rooms.set(index, dungeon_room);
}
void Dungeon::add_dungeon_room(const Ref<DungeonRoom> dungeon_room) {
_dungeon_rooms.push_back(dungeon_room);
}
void Dungeon::remove_dungeon_room(const int index) {
ERR_FAIL_INDEX(index, _dungeon_rooms.size());
_dungeon_rooms.remove(index);
}
int Dungeon::get_dungeon_room_count() const {
return _dungeon_rooms.size();
}
2019-10-18 09:00:01 +02:00
//Start Rooms
Ref<DungeonRoom> Dungeon::get_dungeon_start_room(const int index) const {
ERR_FAIL_INDEX_V(index, _dungeon_start_rooms.size(), Ref<DungeonRoom>());
return _dungeon_start_rooms.get(index);
}
void Dungeon::set_dungeon_start_room(const int index, const Ref<DungeonRoom> dungeon_start_room) {
ERR_FAIL_INDEX(index, _dungeon_start_rooms.size());
_dungeon_start_rooms.set(index, dungeon_start_room);
}
void Dungeon::add_dungeon_start_room(const Ref<DungeonRoom> dungeon_start_room) {
_dungeon_start_rooms.push_back(dungeon_start_room);
}
void Dungeon::remove_dungeon_start_room(const int index) {
ERR_FAIL_INDEX(index, _dungeon_start_rooms.size());
_dungeon_start_rooms.remove(index);
}
int Dungeon::get_dungeon_start_room_count() const {
return _dungeon_start_rooms.size();
}
//End Rooms
Ref<DungeonRoom> Dungeon::get_dungeon_end_room(const int index) const {
ERR_FAIL_INDEX_V(index, _dungeon_end_rooms.size(), Ref<DungeonRoom>());
return _dungeon_end_rooms.get(index);
}
void Dungeon::set_dungeon_end_room(const int index, const Ref<DungeonRoom> dungeon_end_room) {
ERR_FAIL_INDEX(index, _dungeon_end_rooms.size());
_dungeon_end_rooms.set(index, dungeon_end_room);
}
void Dungeon::add_dungeon_end_room(const Ref<DungeonRoom> dungeon_end_room) {
_dungeon_end_rooms.push_back(dungeon_end_room);
}
void Dungeon::remove_dungeon_end_room(const int index) {
ERR_FAIL_INDEX(index, _dungeon_end_rooms.size());
_dungeon_end_rooms.remove(index);
}
int Dungeon::get_dungeon_end_room_count() const {
return _dungeon_end_rooms.size();
}
//Corridors
2019-10-18 09:18:23 +02:00
Ref<DungeonCorridor> Dungeon::get_dungeon_corridor(const int index) const {
ERR_FAIL_INDEX_V(index, _dungeon_corridors.size(), Ref<DungeonCorridor>());
2019-10-18 09:00:01 +02:00
return _dungeon_corridors.get(index);
}
2019-10-18 09:18:23 +02:00
void Dungeon::set_dungeon_corridor(const int index, const Ref<DungeonCorridor> dungeon_corridor) {
2019-10-18 09:00:01 +02:00
ERR_FAIL_INDEX(index, _dungeon_corridors.size());
_dungeon_corridors.set(index, dungeon_corridor);
}
2019-10-18 09:18:23 +02:00
void Dungeon::add_dungeon_corridor(const Ref<DungeonCorridor> dungeon_corridor) {
2019-10-18 09:00:01 +02:00
_dungeon_corridors.push_back(dungeon_corridor);
}
void Dungeon::remove_dungeon_corridor(const int index) {
ERR_FAIL_INDEX(index, _dungeon_corridors.size());
_dungeon_corridors.remove(index);
}
int Dungeon::get_dungeon_corridor_count() const {
return _dungeon_corridors.size();
}
#ifdef ESS_PRESENT
2019-10-18 10:22:11 +02:00
//Entities
2019-10-18 10:45:02 +02:00
Ref<EntityData> Dungeon::get_entity_data(const int index) const {
2019-10-18 10:22:11 +02:00
ERR_FAIL_INDEX_V(index, _entity_datas.size(), Ref<DungeonCorridor>());
return _entity_datas.get(index);
}
2019-10-18 10:45:02 +02:00
void Dungeon::set_entity_data(const int index, const Ref<EntityData> entity_data) {
2019-10-18 10:22:11 +02:00
ERR_FAIL_INDEX(index, _entity_datas.size());
_entity_datas.set(index, entity_data);
}
2019-10-18 10:45:02 +02:00
void Dungeon::add_entity_data(const Ref<EntityData> entity_data) {
2019-10-18 10:22:11 +02:00
_entity_datas.push_back(entity_data);
}
void Dungeon::remove_entity_data(const int index) {
ERR_FAIL_INDEX(index, _entity_datas.size());
_entity_datas.remove(index);
}
int Dungeon::get_entity_data_count() const {
return _entity_datas.size();
}
#endif
2019-10-18 10:22:11 +02:00
2019-10-18 09:00:01 +02:00
void Dungeon::setup() {
if (!_data.is_valid())
return;
2019-10-18 09:00:01 +02:00
if (has_method("_setup")) {
call("_setup");
}
}
#ifdef VOXELMAN_PRESENT
void Dungeon::setup_library(Ref<VoxelmanLibrary> library) {
if (!_data.is_valid())
return;
if (has_method("_setup_library")) {
call("_setup_library", library);
}
}
void Dungeon::_setup_library(Ref<VoxelmanLibrary> library) {
for (int i = 0; i < _data->get_voxel_surface_count(); ++i) {
Ref<VoxelSurface> s = _data->get_voxel_surface(i);
if (s.is_valid()) {
library->add_voxel_surface(s);
}
}
for (int i = 0; i < get_dungeon_corridor_count(); ++i) {
Ref<DungeonCorridor> d = get_dungeon_corridor(i);
if (d.is_valid()) {
d->setup_library(library);
}
}
for (int i = 0; i < get_dungeon_end_room_count(); ++i) {
Ref<DungeonRoom> d = get_dungeon_end_room(i);
if (d.is_valid()) {
d->setup_library(library);
}
}
for (int i = 0; i < get_dungeon_room_count(); ++i) {
Ref<DungeonRoom> d = get_dungeon_room(i);
if (d.is_valid()) {
d->setup_library(library);
}
}
for (int i = 0; i < get_dungeon_start_room_count(); ++i) {
Ref<DungeonRoom> d = get_dungeon_start_room(i);
if (d.is_valid()) {
d->setup_library(library);
}
}
}
void Dungeon::generate_chunk(Ref<VoxelChunk> chunk, bool spawn_mobs) {
ERR_FAIL_COND(!chunk.is_valid());
2019-11-06 03:38:02 +01:00
if (has_method("_generate_chunk")) {
call("_generate_chunk", chunk, spawn_mobs);
}
}
void Dungeon::generate_structure(Ref<VoxelStructure> structure, bool spawn_mobs) {
if (has_method("_generate_structure")) {
call("_generate_structure", structure, spawn_mobs);
2019-09-24 02:42:42 +02:00
}
2019-09-22 22:12:20 +02:00
}
#else
void Dungeon::setup_library(Ref<Resource> library) {
if (!_data.is_valid())
return;
if (has_method("_setup_library")) {
call("_setup_library", library);
}
}
void Dungeon::generate_chunk(Ref<Resource> chunk, bool spawn_mobs) {
ERR_FAIL_COND(!chunk.is_valid());
if (has_method("_generate_chunk")) {
call("_generate_chunk", chunk, spawn_mobs);
}
}
#endif
2019-09-22 22:12:20 +02:00
2019-10-14 21:50:35 +02:00
Ref<Image> Dungeon::generate_map() {
ERR_FAIL_COND_V(!has_method("_generate_map"), Ref<Image>());
return call("_generate_map");
}
2019-09-24 02:42:42 +02:00
Dungeon::Dungeon() {
_current_seed = 0;
2019-10-18 09:43:45 +02:00
_posx = 0;
_posy = 0;
_posz = 0;
_sizex = 0;
_sizey = 0;
_sizez = 0;
2019-10-18 10:22:11 +02:00
_room_count = 0;
2019-09-22 22:12:20 +02:00
}
2019-09-24 02:42:42 +02:00
Dungeon::~Dungeon() {
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
_environment.unref();
2020-01-09 04:28:08 +01:00
#endif
_data.unref();
_dungeon_rooms.clear();
2019-10-18 09:00:01 +02:00
_dungeon_start_rooms.clear();
_dungeon_end_rooms.clear();
_dungeon_corridors.clear();
2020-01-09 04:28:08 +01:00
#ifdef ESS_PRESENT
2019-10-18 10:22:11 +02:00
_entity_datas.clear();
2020-01-09 04:28:08 +01:00
#endif
2019-09-22 22:12:20 +02:00
}
2019-09-24 02:42:42 +02:00
void Dungeon::_bind_methods() {
2019-10-18 09:00:01 +02:00
BIND_VMETHOD(MethodInfo("_setup"));
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
BIND_VMETHOD(MethodInfo("_setup_library", PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanLibrary")));
BIND_VMETHOD(MethodInfo("_generate_structure", PropertyInfo(Variant::OBJECT, "structure", PROPERTY_HINT_RESOURCE_TYPE, "VoxelStructure"), PropertyInfo(Variant::BOOL, "spawn_mobs")));
2019-11-06 03:38:02 +01:00
BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk"), PropertyInfo(Variant::BOOL, "spawn_mobs")));
2020-01-09 04:28:08 +01:00
#else
BIND_VMETHOD(MethodInfo("_setup_library", PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::BOOL, "spawn_mobs")));
2020-01-09 04:28:08 +01:00
#endif
2019-09-24 02:42:42 +02:00
2019-10-18 09:00:01 +02:00
ClassDB::bind_method(D_METHOD("setup"), &Dungeon::setup);
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
ClassDB::bind_method(D_METHOD("setup_library", "library"), &Dungeon::setup_library);
ClassDB::bind_method(D_METHOD("_setup_library", "library"), &Dungeon::_setup_library);
ClassDB::bind_method(D_METHOD("generate_chunk", "chunk", "spawn_mobs"), &Dungeon::generate_chunk);
ClassDB::bind_method(D_METHOD("generate_structure", "structure", "spawn_mobs"), &Dungeon::generate_structure);
2020-01-09 04:28:08 +01:00
#else
ClassDB::bind_method(D_METHOD("setup_library", "library"), &Dungeon::setup_library);
ClassDB::bind_method(D_METHOD("generate_chunk", "chunk", "spawn_mobs"), &Dungeon::generate_chunk);
2020-01-09 04:28:08 +01:00
#endif
ClassDB::bind_method(D_METHOD("get_current_seed"), &Dungeon::get_current_seed);
ClassDB::bind_method(D_METHOD("set_current_seed", "value"), &Dungeon::set_current_seed);
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_seed"), "set_current_seed", "get_current_seed");
2019-10-18 09:00:01 +02:00
2019-11-01 15:42:03 +01:00
ClassDB::bind_method(D_METHOD("get_level_range"), &Dungeon::get_level_range);
ClassDB::bind_method(D_METHOD("set_level_range", "value"), &Dungeon::set_level_range);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "level_range"), "set_level_range", "get_level_range");
2019-10-18 09:00:01 +02:00
//Position
ClassDB::bind_method(D_METHOD("get_posx"), &Dungeon::get_posx);
ClassDB::bind_method(D_METHOD("set_posx", "value"), &Dungeon::set_posx);
ADD_PROPERTY(PropertyInfo(Variant::INT, "posx"), "set_posx", "get_posx");
ClassDB::bind_method(D_METHOD("get_posy"), &Dungeon::get_posy);
ClassDB::bind_method(D_METHOD("set_posy", "value"), &Dungeon::set_posy);
ADD_PROPERTY(PropertyInfo(Variant::INT, "posy"), "set_posy", "get_posy");
ClassDB::bind_method(D_METHOD("get_posz"), &Dungeon::get_posz);
ClassDB::bind_method(D_METHOD("set_posz", "value"), &Dungeon::set_posz);
ADD_PROPERTY(PropertyInfo(Variant::INT, "posz"), "set_posz", "get_posz");
//Size
ClassDB::bind_method(D_METHOD("get_sizex"), &Dungeon::get_sizex);
ClassDB::bind_method(D_METHOD("set_sizex", "value"), &Dungeon::set_sizex);
ADD_PROPERTY(PropertyInfo(Variant::INT, "sizex"), "set_sizex", "get_sizex");
ClassDB::bind_method(D_METHOD("get_sizey"), &Dungeon::get_sizey);
ClassDB::bind_method(D_METHOD("set_sizey", "value"), &Dungeon::set_sizey);
ADD_PROPERTY(PropertyInfo(Variant::INT, "sizey"), "set_sizey", "get_sizey");
ClassDB::bind_method(D_METHOD("get_sizez"), &Dungeon::get_sizez);
ClassDB::bind_method(D_METHOD("set_sizez", "value"), &Dungeon::set_sizez);
ADD_PROPERTY(PropertyInfo(Variant::INT, "sizez"), "set_sizez", "get_sizez");
2019-10-18 10:22:11 +02:00
//Room Count
ClassDB::bind_method(D_METHOD("get_room_count"), &Dungeon::get_room_count);
ClassDB::bind_method(D_METHOD("set_room_count", "value"), &Dungeon::set_room_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "room_count"), "set_room_count", "get_room_count");
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
2019-10-18 09:00:01 +02:00
//Environment
ClassDB::bind_method(D_METHOD("get_environment"), &Dungeon::get_environment);
ClassDB::bind_method(D_METHOD("set_environment", "value"), &Dungeon::set_environment);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "EnvironmentData"), "set_environment", "get_environment");
2020-01-09 04:28:08 +01:00
#endif
ClassDB::bind_method(D_METHOD("get_data"), &Dungeon::get_data);
ClassDB::bind_method(D_METHOD("set_data", "value"), &Dungeon::set_data);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "DungeonData", 0), "set_data", "get_data");
2019-10-18 09:00:01 +02:00
//Rooms
ClassDB::bind_method(D_METHOD("get_dungeon_room", "index"), &Dungeon::get_dungeon_room);
ClassDB::bind_method(D_METHOD("set_dungeon_room", "index", "data"), &Dungeon::set_dungeon_room);
ClassDB::bind_method(D_METHOD("add_dungeon_room", "dungeon_room"), &Dungeon::add_dungeon_room);
ClassDB::bind_method(D_METHOD("remove_dungeon_room", "index"), &Dungeon::remove_dungeon_room);
ClassDB::bind_method(D_METHOD("get_dungeon_room_count"), &Dungeon::get_dungeon_room_count);
2019-10-14 21:50:35 +02:00
2019-10-18 09:00:01 +02:00
//Start Rooms
ClassDB::bind_method(D_METHOD("get_dungeon_start_room", "index"), &Dungeon::get_dungeon_start_room);
ClassDB::bind_method(D_METHOD("set_dungeon_start_room", "index", "data"), &Dungeon::set_dungeon_start_room);
ClassDB::bind_method(D_METHOD("add_dungeon_start_room", "dungeon_start_room"), &Dungeon::add_dungeon_start_room);
ClassDB::bind_method(D_METHOD("remove_dungeon_start_room", "index"), &Dungeon::remove_dungeon_start_room);
ClassDB::bind_method(D_METHOD("get_dungeon_start_room_count"), &Dungeon::get_dungeon_start_room_count);
//End Rooms
ClassDB::bind_method(D_METHOD("get_dungeon_end_room", "index"), &Dungeon::get_dungeon_end_room);
ClassDB::bind_method(D_METHOD("set_dungeon_end_room", "index", "data"), &Dungeon::set_dungeon_end_room);
ClassDB::bind_method(D_METHOD("add_dungeon_end_room", "dungeon_end_room"), &Dungeon::add_dungeon_end_room);
ClassDB::bind_method(D_METHOD("remove_dungeon_end_room", "index"), &Dungeon::remove_dungeon_end_room);
ClassDB::bind_method(D_METHOD("get_dungeon_end_room_count"), &Dungeon::get_dungeon_end_room_count);
//Corridors
ClassDB::bind_method(D_METHOD("get_dungeon_corridor", "index"), &Dungeon::get_dungeon_corridor);
ClassDB::bind_method(D_METHOD("set_dungeon_corridor", "index", "data"), &Dungeon::set_dungeon_corridor);
ClassDB::bind_method(D_METHOD("add_dungeon_corridor", "dungeon_corridor"), &Dungeon::add_dungeon_corridor);
ClassDB::bind_method(D_METHOD("remove_dungeon_corridor", "index"), &Dungeon::remove_dungeon_corridor);
ClassDB::bind_method(D_METHOD("get_dungeon_corridor_count"), &Dungeon::get_dungeon_corridor_count);
2020-01-09 04:28:08 +01:00
#ifdef ESS_PRESENT
2019-10-18 10:22:11 +02:00
//Entities
ClassDB::bind_method(D_METHOD("get_entity_data", "index"), &Dungeon::get_entity_data);
ClassDB::bind_method(D_METHOD("set_entity_data", "index", "data"), &Dungeon::set_entity_data);
ClassDB::bind_method(D_METHOD("add_entity_data", "entity_data"), &Dungeon::add_entity_data);
ClassDB::bind_method(D_METHOD("remove_entity_data", "index"), &Dungeon::remove_entity_data);
ClassDB::bind_method(D_METHOD("get_entity_data_count"), &Dungeon::get_entity_data_count);
2020-01-09 04:28:08 +01:00
#endif
2019-10-18 10:22:11 +02:00
2019-10-14 21:50:35 +02:00
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Image"), "_generate_map"));
ClassDB::bind_method(D_METHOD("generate_map"), &Dungeon::generate_map);
2019-09-22 22:12:20 +02:00
}