summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2013-05-12 11:30:59 +0200
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-05-12 11:30:59 +0200
commitef78ed124fad1ac770183b7bcd816d416e4d11b0 (patch)
treed2e8c93bae9339c75c7165b5d0f5ca34e0583bf6
parent2ac89cae8f19dc2ca8e5211974df18359e3f9536 (diff)
downloadmanaserv-abilities.tar.gz
manaserv-abilities.tar.xz
manaserv-abilities.zip
Readded Entity:damage as lua functionabilities
-rw-r--r--example/scripts/abilities.lua13
-rw-r--r--example/scripts/damage.lua31
-rw-r--r--example/scripts/main.lua3
-rw-r--r--example/scripts/monster/basic_ai.lua8
-rw-r--r--example/scripts/monster/settings.lua6
5 files changed, 46 insertions, 15 deletions
diff --git a/example/scripts/abilities.lua b/example/scripts/abilities.lua
index ce617ee..0dee281 100644
--- a/example/scripts/abilities.lua
+++ b/example/scripts/abilities.lua
@@ -25,13 +25,12 @@ spell1:on_use(function(user, x, y, abilityId)
local affected_beings = get_beings_in_circle(target_x, target_y, TILESIZE)
for _, being in ipairs(affected_beings) do
if being ~= user then
- local old_hp = being:base_attribute(ATTR_HP)
- local new_hp = math.max(old_hp - 5, 0)
- being:set_base_attribute(ATTR_HP, new_hp)
- local diff = old_hp - new_hp
- if diff > 0 then
- being:add_hit_taken(diff)
- end
+ local damage = {
+ base = 10,
+ delta = 5,
+ chance_to_hit = user:modified_attribute(ATTR_STR),
+ }
+ being:damage(user, damage)
end
end
end)
diff --git a/example/scripts/damage.lua b/example/scripts/damage.lua
new file mode 100644
index 0000000..3527c7a
--- /dev/null
+++ b/example/scripts/damage.lua
@@ -0,0 +1,31 @@
+--[[
+
+ Offers damage functions
+
+--]]
+
+
+-- damage is a table with they keys:
+-- base, delta, chance_to_hit
+function Entity:damage(source, damage)
+ local hp_loss = math.random(damage.base, damage.base + damage.delta)
+ local dodge = self:modified_attribute(ATTR_DODGE)
+
+ if math.random(dodge) > math.random(damage.chance_to_hit) then
+ hp_loss = 0 -- attack missed
+ else
+ local defense = self:modified_attribute(ATTR_DEFENSE)
+ local randomness = hp_loss > 16 and math.random(hp_loss / 16) or 0
+ hp_loss = hp_loss * (1 - (0.0159375 * defense) / (1 + 0.017 * defense))
+ + randomness
+ end
+
+ if hp_loss > 0 then
+ local hp = self:modified_attribute(ATTR_HP)
+ hp_loss = math.min(hp, hp_loss)
+ self:add_hit_taken(hp_loss)
+ self:set_base_attribute(ATTR_HP, hp - hp_loss)
+ else
+ hp_loss = 0
+ end
+end
diff --git a/example/scripts/main.lua b/example/scripts/main.lua
index 8a4c8af..90b651a 100644
--- a/example/scripts/main.lua
+++ b/example/scripts/main.lua
@@ -5,6 +5,9 @@
--]]
+-- Global functions:
+require "scripts/damage"
+
-- At the moment the event handlers are split up over the following files:
require "scripts/global_events"
require "scripts/abilities"
diff --git a/example/scripts/monster/basic_ai.lua b/example/scripts/monster/basic_ai.lua
index 40fa851..0fc1b23 100644
--- a/example/scripts/monster/basic_ai.lua
+++ b/example/scripts/monster/basic_ai.lua
@@ -157,14 +157,8 @@ local function update(mob, tick)
end
local function mob_attack(mob, target, ability_id)
- local hp = target:base_attribute(ATTR_HP)
local config = mob_config[mob:name()]
- local dealt_damage = math.min(hp, config.damage)
- if dealt_damage > 0 then
- local v = hp - dealt_damage
- target:set_base_attribute(ATTR_HP, hp - dealt_damage)
- target:add_hit_taken(dealt_damage)
- end
+ target:damage(mob, config.damage)
end
local function mob_recharged(mob, ability_id)
diff --git a/example/scripts/monster/settings.lua b/example/scripts/monster/settings.lua
index b095a2d..f2b6741 100644
--- a/example/scripts/monster/settings.lua
+++ b/example/scripts/monster/settings.lua
@@ -17,7 +17,11 @@ return {
trackrange = 5 * TILESIZE,
attack_distance = TILESIZE,
ability_id = 2,
- damage = 1,
+ damage = {
+ base = 2,
+ delta = 5,
+ chance_to_hit = 20,
+ },
},
["Green Slime"] = {
strollrange = TILESIZE,