From 58e62da5015bd6ca442d90508f054ea2c4e988de Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 26 Feb 2020 16:49:52 +0100 Subject: [PATCH] Spell's get_description can now replace some words with value of it's properties. e.g. "%%cast_cast_time " (not the whitespace at the end) will be replaced to the value stored in cast_cast_time. This value can scaled with class level like %#cast_cast_time, and with character level like %$cast_cast_time. Also made get_description overrideabe from scripts. And bound text_translation_key. --- data/spells/spell.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ data/spells/spell.h | 1 + 2 files changed, 51 insertions(+) diff --git a/data/spells/spell.cpp b/data/spells/spell.cpp index 35f0892..03fbdf2 100644 --- a/data/spells/spell.cpp +++ b/data/spells/spell.cpp @@ -723,6 +723,10 @@ String Spell::get_name_translated() const { } String Spell::get_description(const int class_level, const int character_level) { + return call("_get_description", class_level, character_level); +} + +String Spell::_get_description(const int class_level, const int character_level) { String str; if (_text_translation_key != "") { @@ -731,6 +735,45 @@ String Spell::get_description(const int class_level, const int character_level) str = _text_description; + int pos = str.find_char('%'); + + while (pos > 0) { + if (pos == str.size() - 1) + break; + + CharType o = str[pos + 1]; + + if (o == '#' || o == '$' || o == '%') { + int nsp = str.find_char(' ', pos + 1); + + if (pos < 0) + break; + + String prop = str.substr(pos + 2, nsp - pos - 2); + StringName psm = prop; + bool valid = false; + Variant value = get(psm, &valid); + + if (valid) { + if (o == '#') { + value = Variant::evaluate(Variant::OP_MULTIPLY, value, class_level / static_cast(EntityEnums::MAX_CLASS_LEVEL)); + + value = static_cast(value); + } + + if (o == '$') { + value = Variant::evaluate(Variant::OP_MULTIPLY, value, character_level / static_cast(EntityEnums::MAX_CHARACTER_LEVEL)); + + value = static_cast(value); + } + + str = str.replace(str.substr(pos, nsp - pos) + " ", value); + } + } + + pos = str.find_char('%', pos + 1); + } + return str; } @@ -1140,14 +1183,21 @@ void Spell::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "on_learn_auras", PROPERTY_HINT_NONE, "17/17:Aura", PROPERTY_USAGE_DEFAULT, "Aura"), "set_on_learn_auras", "get_on_learn_auras"); ADD_GROUP("Texts", "text"); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::STRING, "desc"), "_get_description", PropertyInfo(Variant::INT, "class_level"), PropertyInfo(Variant::INT, "character_level"))); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "text_name"), "set_name", "get_name"); + ClassDB::bind_method(D_METHOD("get_text_translation_key"), &Spell::get_text_translation_key); + ClassDB::bind_method(D_METHOD("set_text_translation_key", "value"), &Spell::set_text_description); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "text_translation_key"), "set_text_translation_key", "get_text_translation_key"); + ClassDB::bind_method(D_METHOD("get_text_description"), &Spell::get_text_description); ClassDB::bind_method(D_METHOD("set_text_description", "value"), &Spell::set_text_description); ADD_PROPERTY(PropertyInfo(Variant::STRING, "text_description", PROPERTY_HINT_MULTILINE_TEXT), "set_text_description", "get_text_description"); ClassDB::bind_method(D_METHOD("get_name_translated"), &Spell::get_name_translated); ClassDB::bind_method(D_METHOD("get_description", "class_level", "character_level"), &Spell::get_description); + ClassDB::bind_method(D_METHOD("_get_description", "class_level", "character_level"), &Spell::_get_description); ADD_GROUP("Scaling", "scale"); ClassDB::bind_method(D_METHOD("get_scale_with_level"), &Spell::get_scale_with_level); diff --git a/data/spells/spell.h b/data/spells/spell.h index 3c6b9b0..64238d8 100644 --- a/data/spells/spell.h +++ b/data/spells/spell.h @@ -319,6 +319,7 @@ public: String get_name_translated() const; String get_description(const int class_level, const int character_level); + String _get_description(const int class_level, const int character_level); Spell(); ~Spell();