summaryrefslogtreecommitdiff
path: root/example/scripts
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2013-06-08 18:31:30 +0200
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-08-26 22:56:47 +0200
commit1e5a15c0a5e24fb4b358fff75a7082d65496e1f9 (patch)
tree741756128a3587768fa02dbbdd9ba64820afe6cb /example/scripts
parent44ee071d7ece5a2023f79307f36e8a244c9e7b3a (diff)
downloadmanaserv-1e5a15c0a5e24fb4b358fff75a7082d65496e1f9.tar.gz
manaserv-1e5a15c0a5e24fb4b358fff75a7082d65496e1f9.tar.bz2
manaserv-1e5a15c0a5e24fb4b358fff75a7082d65496e1f9.tar.xz
manaserv-1e5a15c0a5e24fb4b358fff75a7082d65496e1f9.zip
Readded level handling
Things done: Wrote a entity:give_experience function (lua side only). Renamed characterpoints to attributepoints (no db update. Did not want to do one for a simple rename). Temponary introduced a ATTR_LEVEL constant. TODO: dehardcode this. Script binds for settings the correction and attribute points.
Diffstat (limited to 'example/scripts')
-rw-r--r--example/scripts/abilities.lua7
-rw-r--r--example/scripts/attributes.lua66
-rw-r--r--example/scripts/damage.lua22
-rw-r--r--example/scripts/monster/basic_ai.lua22
-rw-r--r--example/scripts/monster/settings.lua10
5 files changed, 123 insertions, 4 deletions
diff --git a/example/scripts/abilities.lua b/example/scripts/abilities.lua
index 0dee2810..deed0c52 100644
--- a/example/scripts/abilities.lua
+++ b/example/scripts/abilities.lua
@@ -31,7 +31,14 @@ spell1:on_use(function(user, x, y, abilityId)
chance_to_hit = user:modified_attribute(ATTR_STR),
}
being:damage(user, damage)
+ being:say("OUCH")
end
end
end)
--spell1:on_recharged(function(ch) ch:say("Hoooooooo...") end)
+
+local debugspell = get_ability_info("debug_debug")
+debugspell:on_use(function(user)
+ WARN("USED")
+ user:give_experience(10)
+end)
diff --git a/example/scripts/attributes.lua b/example/scripts/attributes.lua
index 37d642d9..3e9b97e0 100644
--- a/example/scripts/attributes.lua
+++ b/example/scripts/attributes.lua
@@ -7,6 +7,9 @@
--]]
+local ATTR_EXP = 22
+local ATTR_LEVEL = 23
+
local function recalculate_base_attribute(being, attribute)
local old_base = being:base_attribute(attribute)
local new_base = old_base
@@ -49,6 +52,10 @@ local function recalculate_base_attribute(being, attribute)
elseif attribute == ATTR_ABILITY_COOLDOWN then
-- Provisional
new_base = 100 - being:modified_attribute(ATTR_WIL)
+ elseif attribute == ATTR_LEVEL then
+ -- Provisional
+ --new_base = 100 - 100 * math.pow(0.99999, being:base_attribute(ATTR_EXP))
+ new_base = being:base_attribute(ATTR_EXP) / 20
end
if new_base ~= old_base then
@@ -70,8 +77,67 @@ local function update_derived_attributes(being, attribute)
-- unimplemented
elseif attribute == ATTR_WIL then
recalculate_base_attribute(being, ATTR_ABILITY_COOLDOWN)
+ elseif attribute == ATTR_EXP then
+ recalculate_base_attribute(being, ATTR_LEVEL)
end
end
on_recalculate_base_attribute(recalculate_base_attribute)
on_update_derived_attribute(update_derived_attributes)
+
+function Entity:level()
+ return math.floor(self:base_attribute(ATTR_LEVEL))
+end
+
+function Entity:give_experience(experience)
+ local old_experience = self:base_attribute(ATTR_EXP)
+ local old_level = self:level()
+ self:set_base_attribute(ATTR_EXP, old_experience + experience)
+ if self:level() > old_level then
+ self:say("LEVELUP!!! " .. self:level())
+ self:set_attribute_points(self:attribute_points() + 1)
+ self:set_correction_points(self:correction_points() + 1)
+ end
+end
+
+local mobs_config = require "scripts/monster/settings"
+
+local exp_receiver = {}
+
+-- Give EXP for monster kills
+local function monster_damaged(mob, source, damage)
+
+ local receiver = exp_receiver[mob] or { chars = {}, total = 0 }
+ exp_receiver[mob] = receiver
+
+ if source and source:type() == TYPE_CHARACTER then
+ mob:change_anger(source, damage)
+
+ local current_damage = receiver.chars[source]
+ if not current_damage then
+ on_remove(source, function(removed_being)
+ receiver[removed_being] = nil
+ end)
+ on_death(source, function(removed_being)
+ receiver[removed_being] = nil
+ end)
+ end
+
+ current_damage = (current_damage or 0) + damage
+ receiver.chars[source] = current_damage
+ receiver.total = receiver.total + damage
+ end
+
+ if mob:base_attribute(ATTR_HP) == 0 then
+ local mob_config = mobs_config[mob:name()]
+ local experience = mob_config.experience or 0
+ for char, damage in pairs(receiver.chars) do
+ local gained_exp = damage / receiver.total * experience
+ char:give_experience(gained_exp)
+ end
+ end
+end
+
+for _, monsterclass in pairs(get_monster_classes()) do
+ monsterclass:on_damaged(monster_damaged)
+end
diff --git a/example/scripts/damage.lua b/example/scripts/damage.lua
index d031deee..8ad014f4 100644
--- a/example/scripts/damage.lua
+++ b/example/scripts/damage.lua
@@ -4,6 +4,11 @@
--]]
+local monster_class_damage_functions = {}
+
+function MonsterClass:on_damaged(func)
+ monster_class_damage_functions[self] = func
+end
-- damage is a table with these keys:
-- base, delta, chance_to_hit
@@ -11,20 +16,33 @@ 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
+ if dodge > 0 and math.random(dodge) > math.random(damage.chance_to_hit) or
+ damage.chance_to_hit == 0
+ then
hp_loss = 0 -- attack missed
+ self:say("HAHA 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
+ self:say("UH NO")
end
if hp_loss > 0 then
- local hp = self:modified_attribute(ATTR_HP)
+ local hp = self:base_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)
+ self:say("I GOT DAMAGED " .. hp - hp_loss)
+
+ if self:type() == TYPE_MONSTER then
+ local class = get_monster_class(self:monster_id())
+ local callback = monster_class_damage_functions[class]
+ if callback then
+ return callback(self, source, hp_loss)
+ end
+ end
else
hp_loss = 0
end
diff --git a/example/scripts/monster/basic_ai.lua b/example/scripts/monster/basic_ai.lua
index 17ffa74a..c75ca300 100644
--- a/example/scripts/monster/basic_ai.lua
+++ b/example/scripts/monster/basic_ai.lua
@@ -12,6 +12,24 @@ local TARGET_SEARCH_DELAY = 10
local mob_stati = {}
local angerlist = {}
+local function create_mob_status()
+ return {
+ angerlist = {},
+ }
+end
+
+function Entity:change_anger(target, amount)
+ local mob_status = mob_stati[self]
+ if not mob_status then
+ mob_status = create_mob_status()
+ mob_stati[self] = mob_status
+ end
+
+ local anger = mob_status.angerlist[target] or 0
+ mob_status.angerlist[target] = anger + amount
+ mob_stati[self].update_target_timer = 0 -- Enforce looking for new target
+end
+
local mob_config = require "scripts/monster/settings"
local function calculate_position_priority(x1, y1, x2, y2, anger, range)
@@ -43,7 +61,7 @@ local function update_attack_ai(mob, tick)
if being:type() == TYPE_CHARACTER
and being:action() ~= ACTION_DEAD
then
- local anger = angerlist[being] or 0
+ local anger = mob_status.angerlist[being] or 0
if anger == 0 and config.aggressive then
anger = 1
end
@@ -130,7 +148,7 @@ end
local function update(mob, tick)
local mob_status = mob_stati[mob]
if not mob_status then
- mob_status = {}
+ mob_status = create_mob_status()
mob_stati[mob] = mob_status
on_remove(mob, remove_mob)
end
diff --git a/example/scripts/monster/settings.lua b/example/scripts/monster/settings.lua
index f2b6741b..d89da569 100644
--- a/example/scripts/monster/settings.lua
+++ b/example/scripts/monster/settings.lua
@@ -4,12 +4,20 @@ return {
aggressive = false,
trackrange = 5 * TILESIZE,
attack_distance = TILESIZE,
+ experience = 10,
+ ability_id = 2,
+ damage = {
+ base = 0,
+ delta = 1,
+ chance_to_hit = 2,
+ },
},
["Scorpion"] = {
strollrange = 2 * TILESIZE,
aggressive = false,
trackrange = 5 * TILESIZE,
attack_distance = TILESIZE,
+ experience = 10,
},
["Red Scorpion"] = {
strollrange = TILESIZE,
@@ -17,6 +25,7 @@ return {
trackrange = 5 * TILESIZE,
attack_distance = TILESIZE,
ability_id = 2,
+ experience = 10,
damage = {
base = 2,
delta = 5,
@@ -28,5 +37,6 @@ return {
aggressive = true,
trackrange = 5 * TILESIZE,
attack_distance = TILESIZE,
+ experience = 10,
},
}