Merge pull request #416 from Elvish-Hunter/master

UtBS: teaching ability for Garak
This commit is contained in:
Elvish-Hunter 2015-07-18 20:28:50 +02:00
commit f77579813d
5 changed files with 94 additions and 1 deletions

View file

@ -516,6 +516,7 @@
{GARAK}
side=1
x,y=35,34
{ABILITY_TEACHING_EVENT}
[/unit]
{NAMED_GENERIC_UNIT 1 "Desert Fighter" 36 33 "Jorazan" ( _ "Jorazan")}

View file

@ -66,3 +66,13 @@ _" This unit is capable of basic healing and slowing dehydration."#enddef
#define SPECIAL_NOTES_UTBS_CURES
_" This unit is capable of healing those around it, slowing dehydration, and curing them of poison."#enddef
# This is the Teaching ability owned by Garak
#define ABILITY_TEACHING
[dummy]
id=teaching
name= _ "teaching"
description= _ "At the start of every turn, this unit redistributes its experience points to all the units of the same side adjacent to it. If no suitable unit is adjacent, its experience just goes back to zero."
[/dummy]
#enddef

View file

@ -36,7 +36,7 @@
#enddef
#define GARAK
type=Desert Captain
type=Desert Marshal
id=Garak
name= _ "Garak"
profile=portraits/garak.png
@ -50,6 +50,16 @@
range=ranged
increase_damage=1
[/effect]
[effect]
apply_to=new_ability
[abilities]
{ABILITY_TEACHING}
[/abilities]
[/effect]
[effect]
apply_to=max_experience
increase=850
[/effect]
[/object]
[/modifications]
{IS_HERO}

View file

@ -0,0 +1,71 @@
#textdomain wesnoth-utbs
# event for the teaching ability
#define ABILITY_TEACHING_EVENT
[event]
name=turn refresh
id=garak_teaching
first_time_only=no
[lua]
code=<<
local _ = wesnoth.textdomain "wesnoth-utbs"
local teachers = wesnoth.get_units { ability = "teaching", side = wesnoth.current.side }
for i, teacher in ipairs(teachers) do
local students = wesnoth.get_units { side = wesnoth.current.side, { "filter_adjacent", { id = teacher.id } } }
if #students > 0 then -- don't divide by zero
wesnoth.scroll_to_tile(teacher.x, teacher.y)
local function cf(u1, u2)
-- criteria: the unit with lower level comes first
-- if level is equal, the unit with less experience comes first
if u1.level < u2.level then return true end
if u1.level > u2.level then return false end
if u1.experience < u2.experience then return true end
return false
end
table.sort(students, cf)
-- now we need to divide Garak's xp between all the units
-- we can't just round the result of the division, because this will mean
-- either not redistributing all the XP points, or generating them out of thin air
-- so we perform both a division and a modulo
-- # (length) has a higher precedence than *, / and %
local div = math.floor(teacher.experience / #students)
local mod = teacher.experience % #students
for j, student in ipairs(students) do
-- the modulo operation is used to redistribute every remaining XP
-- so at the start of each turn Garak goes back to 0 XP
-- concrete example: we have 9 XP and 4 units
-- the first unit will receive 3 XP, while the other units 2
local xp_to_add = div
if j <= mod then
xp_to_add = xp_to_add + 1
end
-- having a message that tells you that the unit gained 0 XP is simply obnoxious
if xp_to_add > 0 then
student.experience = student.experience + xp_to_add
-- performance-wise, string concatenation is slower than string interpolation
-- but it's way more readable than two nested interpolations...
wesnoth.float_label(student.x, student.y, "<span color='cyan'>" .. tostring(_ "+%d XP"):format(xp_to_add) .. "</span>")
if student.experience >= student.max_experience then
-- advance unit, animate and fire events
wesnoth.advance_unit(student, true, true)
end
end
end
end
-- in case that Garak has no "students", his experience goes wasted instead of being redistributed...
teacher.experience = 0
end
>>
[/lua]
[/event]
#enddef

View file

@ -282,6 +282,7 @@ static int impl_unit_get(lua_State *L)
return_int_attrib("attacks_left", u.attacks_left());
return_tstring_attrib("name", u.name());
return_bool_attrib("canrecruit", u.can_recruit());
return_int_attrib("level", u.level());
return_vector_string_attrib("extra_recruit", u.recruits());
return_vector_string_attrib("advances_to", u.advances_to());