LootDataBase should be a lot more usable from now.

This commit is contained in:
Relintai 2020-02-26 21:05:39 +01:00
parent ceb71f56d4
commit 2486590bd3
9 changed files with 297 additions and 259 deletions

2
SCsub
View File

@ -102,8 +102,6 @@ sources = [
"utility/category_cooldown.cpp",
"data/loot/loot_data_base.cpp",
"data/loot/loot_data_container.cpp",
"data/loot/loot_data_item.cpp",
"entities/ai/entity_ai.cpp",

View File

@ -70,8 +70,6 @@ def get_doc_classes():
"Bag",
"LootDataBase",
"LootDataContainter",
"LootDataItem",
"SpellDamageInfo",
"SpellHealInfo",

View File

@ -22,28 +22,267 @@ SOFTWARE.
#include "loot_data_base.h"
float LootDataBase::get_chance() const {
return _chance;
int LootDataBase::get_loot_db_size() const {
return _loot_dbs.size();
}
void LootDataBase::set_chance(const float value) {
_chance = value;
void LootDataBase::set_loot_db_size(const float value) {
_loot_dbs.resize(value);
}
void LootDataBase::get_loot(Array into) {
int LootDataBase::get_items_size() const {
return _items.size();
}
void LootDataBase::set_items_size(const int value) {
_items.resize(value);
}
float LootDataBase::get_loot_db_chance(const int index) const {
ERR_FAIL_INDEX_V(index, _loot_dbs.size(), 0);
return _loot_dbs.get(index).chance;
}
void LootDataBase::set_loot_db_chance(const int index, const float value) {
ERR_FAIL_INDEX(index, _loot_dbs.size());
LootDBLDF l = _loot_dbs.get(index);
l.chance = value;
_loot_dbs.set(index, l);
}
Ref<LootDataBase> LootDataBase::get_loot_db(const int index) const {
ERR_FAIL_INDEX_V(index, _loot_dbs.size(), 0);
return _loot_dbs.get(index).loot_db;
}
void LootDataBase::set_loot_db(const int index, const Ref<LootDataBase> &value) {
ERR_FAIL_INDEX(index, _loot_dbs.size());
LootDBLDF l = _loot_dbs.get(index);
l.loot_db = value;
_loot_dbs.set(index, l);
}
float LootDataBase::get_item_chance(const int index) const {
ERR_FAIL_INDEX_V(index, _items.size(), 0);
return _items.get(index).chance;
}
void LootDataBase::set_item_chance(const int index, const float value) {
ERR_FAIL_INDEX(index, _items.size());
LootDBItem l = _items.get(index);
l.chance = value;
_items.set(index, l);
}
Ref<ItemTemplate> LootDataBase::get_item(const int index) const {
ERR_FAIL_INDEX_V(index, _items.size(), 0);
return _items.get(index).item;
}
void LootDataBase::set_item(const int index, const Ref<ItemTemplate> &value) {
ERR_FAIL_INDEX(index, _items.size());
LootDBItem l = _items.get(index);
l.item = value;
_items.set(index, l);
}
Array LootDataBase::get_loot() {
if (has_method("_get_loot"))
call("_get_loot", into);
return call("_get_loot");
}
Array LootDataBase::_get_loot() {
Array arr;
for (int i = 0; i < _loot_dbs.size(); ++i) {
LootDBLDF l = _loot_dbs.get(i);
if (!l.loot_db.is_valid())
continue;
Math::randomize();
float val = Math::randf() * 100.0;
if (val < l.chance) {
arr.append(l.loot_db->get_loot());
}
}
for (int i = 0; i < _items.size(); ++i) {
LootDBItem l = _items.get(i);
if (!l.item.is_valid())
continue;
Math::randomize();
float val = Math::randf() * 100.0;
if (val < l.chance) {
arr.append(l.item);
}
}
return arr;
}
LootDataBase::LootDataBase() {
_chance = 100;
}
LootDataBase::~LootDataBase() {
_loot_dbs.clear();
_items.clear();
}
bool LootDataBase::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("loot_dbs/")) {
int index = name.get_slicec('/', 1).to_int();
String what = name.get_slicec('/', 2);
if (_loot_dbs.size() <= index) {
set_loot_db_size(index);
}
LootDBLDF ldb = _loot_dbs.get(index);
if (what == "chance") {
ldb.chance = p_value;
_loot_dbs.set(index, ldb);
return true;
} else if (what == "loot_db") {
ldb.loot_db = p_value;
_loot_dbs.set(index, ldb);
return true;
}
return false;
} else if (name.begins_with("items/")) {
int index = name.get_slicec('/', 1).to_int();
String what = name.get_slicec('/', 2);
if (_items.size() <= index) {
set_items_size(index);
}
LootDBItem ldb = _items.get(index);
if (what == "chance") {
ldb.chance = p_value;
_items.set(index, ldb);
return true;
} else if (what == "item_template") {
ldb.item = p_value;
_items.set(index, ldb);
return true;
}
return false;
}
return true;
}
bool LootDataBase::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name.begins_with("loot_dbs/")) {
int index = name.get_slicec('/', 1).to_int();
String what = name.get_slicec('/', 2);
if (_loot_dbs.size() <= index) {
return false;
}
LootDBLDF ldb = _loot_dbs.get(index);
if (what == "chance") {
r_ret = ldb.chance;
return true;
} else if (what == "loot_db") {
r_ret = ldb.loot_db;
return true;
}
return false;
} else if (name.begins_with("items/")) {
int index = name.get_slicec('/', 1).to_int();
String what = name.get_slicec('/', 2);
if (_items.size() <= index) {
return false;
}
LootDBItem ldb = _items.get(index);
if (what == "chance") {
r_ret = ldb.chance;
return true;
} else if (what == "item_template") {
r_ret = ldb.item;
return true;
}
return false;
}
return true;
}
void LootDataBase::_get_property_list(List<PropertyInfo> *p_list) const {
int property_usange = PROPERTY_USAGE_DEFAULT;
for (int i = 0; i < _loot_dbs.size(); ++i) {
p_list->push_back(PropertyInfo(Variant::REAL, "loot_dbs/" + itos(i) + "/chance", PROPERTY_HINT_NONE, "", property_usange));
p_list->push_back(PropertyInfo(Variant::OBJECT, "loot_dbs/" + itos(i) + "/loot_db", PROPERTY_HINT_RESOURCE_TYPE, "LootDataBase", property_usange));
}
for (int i = 0; i < _items.size(); ++i) {
p_list->push_back(PropertyInfo(Variant::REAL, "items/" + itos(i) + "/chance", PROPERTY_HINT_NONE, "", property_usange));
p_list->push_back(PropertyInfo(Variant::OBJECT, "items/" + itos(i) + "/item_template", PROPERTY_HINT_RESOURCE_TYPE, "ItemTemplate", property_usange));
}
}
void LootDataBase::_bind_methods() {
BIND_VMETHOD(MethodInfo("_get_loot", PropertyInfo(Variant::ARRAY, "into")));
ClassDB::bind_method(D_METHOD("get_loot_db_size"), &LootDataBase::get_loot_db_size);
ClassDB::bind_method(D_METHOD("set_loot_db_size", "value"), &LootDataBase::set_loot_db_size);
ADD_PROPERTY(PropertyInfo(Variant::INT, "loot_db_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_loot_db_size", "get_loot_db_size");
ClassDB::bind_method(D_METHOD("get_items_size"), &LootDataBase::get_items_size);
ClassDB::bind_method(D_METHOD("set_items_size", "value"), &LootDataBase::set_items_size);
ADD_PROPERTY(PropertyInfo(Variant::INT, "items_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_items_size", "get_items_size");
ClassDB::bind_method(D_METHOD("get_loot_db_chance", "index"), &LootDataBase::get_loot_db_chance);
ClassDB::bind_method(D_METHOD("set_loot_db_chance", "index", "value"), &LootDataBase::set_loot_db_chance);
ClassDB::bind_method(D_METHOD("get_loot_db", "index"), &LootDataBase::get_loot_db);
ClassDB::bind_method(D_METHOD("set_loot_db", "index", "value"), &LootDataBase::set_loot_db);
ClassDB::bind_method(D_METHOD("get_item_chance", "index"), &LootDataBase::get_item_chance);
ClassDB::bind_method(D_METHOD("set_item_chance", "index", "value"), &LootDataBase::set_item_chance);
ClassDB::bind_method(D_METHOD("get_item", "index"), &LootDataBase::get_item);
ClassDB::bind_method(D_METHOD("set_item", "index", "value"), &LootDataBase::set_item);
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::ARRAY, "arr"), "_get_loot"));
ClassDB::bind_method(D_METHOD("get_loot"), &LootDataBase::get_loot);
ClassDB::bind_method(D_METHOD("get_chance"), &LootDataBase::get_chance);
ClassDB::bind_method(D_METHOD("set_chance", "value"), &LootDataBase::set_chance);
ADD_PROPERTY(PropertyInfo(Variant::INT, "chance"), "set_chance", "get_chance");
ClassDB::bind_method(D_METHOD("_get_loot"), &LootDataBase::_get_loot);
}

View File

@ -25,24 +25,66 @@ SOFTWARE.
#include "core/array.h"
#include "core/vector.h"
#include "../items/item_template.h"
class LootDataBase : public Resource {
GDCLASS(LootDataBase, Resource);
public:
float get_chance() const;
void set_chance(const float value);
int get_loot_db_size() const;
void set_loot_db_size(const float value);
void get_loot(Array into);
int get_items_size() const;
void set_items_size(const int value);
float get_loot_db_chance(const int index) const;
void set_loot_db_chance(const int index, const float value);
Ref<LootDataBase> get_loot_db(const int index) const;
void set_loot_db(const int index, const Ref<LootDataBase> &value);
float get_item_chance(const int index) const;
void set_item_chance(const int index, const float value);
Ref<ItemTemplate> get_item(const int index) const;
void set_item(const int index, const Ref<ItemTemplate> &value);
Array get_loot();
Array _get_loot();
LootDataBase();
~LootDataBase();
protected:
struct LootDBLDF {
float chance;
Ref<LootDataBase> loot_db;
LootDBLDF() {
chance = 0;
}
};
struct LootDBItem {
float chance;
Ref<ItemTemplate> item;
LootDBItem() {
chance = 0;
}
};
protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
static void _bind_methods();
private:
float _chance;
Vector<LootDBLDF> _loot_dbs;
Vector<LootDBItem> _items;
};
#endif

View File

@ -1,81 +0,0 @@
/*
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.
*/
#include "loot_data_container.h"
int LootDataContainter::get_num_entries() const {
return _entries.size();
}
Ref<LootDataBase> LootDataContainter::get_entry(int index) const {
ERR_FAIL_INDEX_V(index, _entries.size(), Ref<LootDataBase>(NULL));
return _entries.get(index);
}
void LootDataContainter::set_entry(const int index, const Ref<LootDataBase> ldb) {
ERR_FAIL_INDEX(index, _entries.size());
_entries.set(index, ldb);
}
void LootDataContainter::_get_loot(Array into) {
for (int i = 0; i < _entries.size(); ++i) {
if (_entries[i].is_valid()) {
_entries.get(i)->get_loot(into);
}
}
}
Vector<Variant> LootDataContainter::get_entries() {
Vector<Variant> r;
for (int i = 0; i < _entries.size(); i++) {
r.push_back(_entries[i].get_ref_ptr());
}
return r;
}
void LootDataContainter::set_entries(const Vector<Variant> &entries) {
_entries.clear();
for (int i = 0; i < entries.size(); i++) {
Ref<LootDataBase> entry = Ref<LootDataBase>(entries[i]);
_entries.push_back(entry);
}
}
LootDataContainter::LootDataContainter() {
}
LootDataContainter::~LootDataContainter() {
_entries.clear();
}
void LootDataContainter::_bind_methods() {
ClassDB::bind_method(D_METHOD("_get_loot", "into"), &LootDataContainter::_get_loot);
ClassDB::bind_method(D_METHOD("get_num_entries"), &LootDataContainter::get_num_entries);
ClassDB::bind_method(D_METHOD("get_entry", "index"), &LootDataContainter::get_entry);
ClassDB::bind_method(D_METHOD("set_entry", "index", "ldb"), &LootDataContainter::set_entry);
ClassDB::bind_method(D_METHOD("get_entries"), &LootDataContainter::get_entries);
ClassDB::bind_method(D_METHOD("set_entries", "auras"), &LootDataContainter::set_entries);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entries", PROPERTY_HINT_NONE, "17/17:LootDataBase", PROPERTY_USAGE_DEFAULT, "LootDataBase"), "set_entries", "get_entries");
}

View File

@ -1,53 +0,0 @@
/*
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.
*/
#ifndef LOOT_DATA_CONTAINER_H
#define LOOT_DATA_CONTAINER_H
#include "core/vector.h"
#include "loot_data_base.h"
class LootDataContainter : public LootDataBase {
GDCLASS(LootDataContainter, LootDataBase);
public:
int get_num_entries() const;
Ref<LootDataBase> get_entry(const int index) const;
void set_entry(const int index, const Ref<LootDataBase> ldb);
void _get_loot(Array into);
Vector<Variant> get_entries();
void set_entries(const Vector<Variant> &auras);
LootDataContainter();
~LootDataContainter();
protected:
static void _bind_methods();
private:
Vector<Ref<LootDataBase> > _entries;
};
#endif

View File

@ -1,53 +0,0 @@
/*
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.
*/
#include "loot_data_item.h"
Ref<ItemTemplate> LootDataItem::get_item() const {
return _item;
}
void LootDataItem::set_item(const Ref<ItemTemplate> item) {
_item = item;
}
void LootDataItem::_get_loot(Array into) {
if (_item.is_valid()) {
Math::randomize();
float val = Math::randf() * 100.0;
if (val < get_chance()) {
into.append(_item);
}
}
}
LootDataItem::LootDataItem() {
}
void LootDataItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("_get_loot", "into"), &LootDataItem::_get_loot);
ClassDB::bind_method(D_METHOD("get_item"), &LootDataItem::get_item);
ClassDB::bind_method(D_METHOD("set_item", "value"), &LootDataItem::set_item);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "ItemTemplate"), "set_item", "get_item");
}

View File

@ -1,48 +0,0 @@
/*
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.
*/
#ifndef LOOT_DATA_ITEM_H
#define LOOT_DATA_ITEM_H
#include "core/math/math_funcs.h"
#include "loot_data_base.h"
class LootDataItem : public LootDataBase {
GDCLASS(LootDataItem, LootDataBase);
public:
Ref<ItemTemplate> get_item() const;
void set_item(const Ref<ItemTemplate> item);
void _get_loot(Array into);
LootDataItem();
protected:
static void _bind_methods();
private:
Ref<ItemTemplate> _item;
};
#endif

View File

@ -112,8 +112,6 @@ SOFTWARE.
#include "utility/entity_create_info.h"
#include "data/loot/loot_data_base.h"
#include "data/loot/loot_data_container.h"
#include "data/loot/loot_data_item.h"
#include "data/spells/spell_effect_visual.h"
#include "data/spells/spell_effect_visual_simple.h"
@ -239,8 +237,6 @@ void register_entity_spell_system_types() {
ClassDB::register_class<CategoryCooldown>();
ClassDB::register_class<LootDataBase>();
ClassDB::register_class<LootDataItem>();
ClassDB::register_class<LootDataContainter>();
ClassDB::register_class<SpellEffectVisual>();
ClassDB::register_class<SpellEffectVisualSimple>();