summaryrefslogtreecommitdiffstats
path: root/example
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2013-02-04 16:19:21 +0100
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-02-04 17:43:24 +0100
commitf7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4 (patch)
tree24a1d06ef633fe3a1bf4943846666f48aaf0c32a /example
parent5255be5a5457e287464606478df62300a44d8479 (diff)
downloadmanaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.tar.gz
manaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.tar.xz
manaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.zip
Moved attribute (re)calculation to the scripts
This introduces two callbacks: - on_update_derived_attribute -> Called to recalculate other derived attributes. - on_recalculate_base_attribute -> Called to recalculate a base attribute (only called for characters. However the function passed as callback can be useful for recalculating the derived attributes as well) Monsters no longer block recalculation of attributes except HP and Speed. I saw no sense to keep this. Fixed constant value in libmana-constants.lua Dropped bool type of the recalculation functions. It would be difficult to keep it while pushing all to the script engine and it was unused anyway. All in all this adds a LOT more flexibillity to projects since they can now adapt all attributes in the way they want.
Diffstat (limited to 'example')
-rw-r--r--example/scripts/attributes.lua74
-rw-r--r--example/scripts/main.lua1
2 files changed, 75 insertions, 0 deletions
diff --git a/example/scripts/attributes.lua b/example/scripts/attributes.lua
new file mode 100644
index 0000000..7009a73
--- /dev/null
+++ b/example/scripts/attributes.lua
@@ -0,0 +1,74 @@
+--[[
+
+ This file demonstrates how attributes are getting calculated and how they can
+ be linked to each other.
+
+ See http://doc.manasource.org/attributes.xml for more info.
+
+--]]
+
+local function recalculate_base_attribute(being, attribute)
+ local old_base = being_get_base_attribute(being, attribute)
+ local new_base = old_base
+ if attribute == ATTR_ACCURACY then
+ -- Provisional
+ new_base = being_get_modified_attribute(being, ATTR_DEX)
+ elseif attribute == ATTR_DEFENSE then
+ new_base = 0.3 * being_get_modified_attribute(being, ATTR_VIT)
+ elseif attribute == ATTR_DODGE then
+ -- Provisional
+ new_base = being_get_modified_attribute(being, ATTR_AGI)
+ elseif attribute == ATTR_MAGIC_DODGE then
+ -- TODO
+ new_base = 1
+ elseif attribute == ATTR_MAGIC_DEFENSE then
+ -- TODO
+ new_base = 0
+ elseif attribute == ATTR_BONUS_ASPD then
+ -- TODO
+ new_base = 0
+ elseif attribute == ATTR_HP_REGEN then
+ local hp_per_sec = being_get_modified_attribute(being, ATTR_VIT) * 0.05
+ new_base = hp_per_sec * TICKS_PER_HP_REGENERATION / 10
+ elseif attribute == ATTR_HP then
+ local hp = being_get_modified_attribute(being, ATTR_HP)
+ local max_hp = being_get_modified_attribute(being, ATTR_MAX_HP)
+
+ if hp > max_hp then
+ new_base = new_base - hp - max_hp
+ end
+ elseif attribute == ATTR_MAX_HP then
+ local vit = being_get_modified_attribute(being, ATTR_VIT)
+ new_base = (vit + 3) * (vit + 20) * 0.125
+ elseif attribute == ATTR_MOVE_SPEED_TPS then
+ -- Provisional
+ new_base = 3.0 + being_get_modified_attribute(being, ATTR_AGI) * 0.08
+ elseif attribute == ATTR_INV_CAPACITY then
+ -- Provisional
+ new_base = 2000 + being_get_modified_attribute(being, ATTR_STR) * 180
+ end
+
+ if new_base ~= old_base then
+ being_set_base_attribute(being, attribute, new_base)
+ end
+
+end
+
+local function update_derived_attributes(being, attribute)
+ if attribute == ATTR_STR then
+ recalculate_base_attribute(ATTR_INV_CAPACITY, being)
+ elseif attribute == ATTR_AGI then
+ recalculate_base_attribute(ATTR_DODGE, being)
+ elseif attribute == ATTR_VIT then
+ recalculate_base_attribute(ATTR_MAX_HP, being)
+ recalculate_base_attribute(ATTR_HP_REGEN, being)
+ recalculate_base_attribute(ATTR_DEFENSE, being)
+ elseif attribute == ATTR_INT then
+ -- unimplemented
+ elseif attribute == ATTR_WIL then
+ -- unimplemented
+ end
+end
+
+on_recalculate_base_attribute(recalculate_base_attribute)
+on_update_derived_attribute(update_derived_attributes)
diff --git a/example/scripts/main.lua b/example/scripts/main.lua
index 6d8207e..3418385 100644
--- a/example/scripts/main.lua
+++ b/example/scripts/main.lua
@@ -9,6 +9,7 @@
require "scripts/global_events"
require "scripts/special_actions"
require "scripts/crafting"
+require "scripts/attributes"
require "scripts/items/candy"
require "scripts/monster/testmonster"