From 3fd4884584c6984d8e5999f261b437ad0fc28e7e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 27 Nov 2015 15:23:48 +0100 Subject: [PATCH] putting all of the pieces together for the snow globe, with final use implementation still TBD Conflicts: scripts/eressea/xmasitems.lua src/laws.c --- res/core/de/strings.xml | 8 ++++ res/eressea/items.xml | 6 +++ scripts/eressea/xmasitems.lua | 79 +++++++++++++++++++++++++++++++++ scripts/tests/e2/e2features.lua | 12 +++++ src/helpers.c | 5 ++- src/laws.c | 25 +++++------ 6 files changed, 120 insertions(+), 15 deletions(-) create mode 100644 scripts/eressea/xmasitems.lua diff --git a/res/core/de/strings.xml b/res/core/de/strings.xml index 3186dcc42..0f16f91b1 100644 --- a/res/core/de/strings.xml +++ b/res/core/de/strings.xml @@ -1581,6 +1581,14 @@ Schneemann snowman + + Schneekugel + snow globe + + + Schneekugeln + snow globes + Schneemänner snowmen diff --git a/res/eressea/items.xml b/res/eressea/items.xml index 076cc023c..8b2df7add 100644 --- a/res/eressea/items.xml +++ b/res/eressea/items.xml @@ -22,6 +22,12 @@ + + + + + + diff --git a/scripts/eressea/xmasitems.lua b/scripts/eressea/xmasitems.lua new file mode 100644 index 000000000..6fe3512b8 --- /dev/null +++ b/scripts/eressea/xmasitems.lua @@ -0,0 +1,79 @@ +local function get_direction(locale, token) + local dir = eressea.locale.direction(locale, token) + if dir and dir>=0 then + return dir + end + return nil +end + +function use_snowglobe(u, amount, token) + local direction = get_direction(u.faction.locale, token) + if direction then + local r = u.region:next(direction) + if r and r.terrain=="ocean" then + r.terrain = "glacier" + end + else + return -4 + end + return 1 +end + +function use_snowman(u, amount) + if amount>0 and u.region.terrain == "glacier" then + local man = unit.create(u.faction, u.region) + man.race = "snowman" + man.number = amount + return amount + end + return -4 +end + +function use_xmastree(u, amount) + if u.region.herb~=nil then + -- TODO: else? + local trees = u.region:get_resource("tree") + u.region:set_key("xm06", true) + u.region:set_resource("tree", 10+trees) + local msg = message.create("usepotion") + msg:set_unit("unit", u) + msg:set_resource("potion", "xmastree") + msg:send_region(u.region) + return amount + end + return 0 +end + +local self = {} + +function self.update() + local turn = get_turn() + local season = get_season(turn) + if season == "calendar::winter" then + eressea.log.debug("it is " .. season .. ", the christmas trees do their magic") + local msg = message.create("xmastree_effect") + for r in regions() do + if r:get_key("xm06") then + trees = r:get_resource("tree") + if trees*0.1>=1 then + r:set_resource("tree", trees * 1.1) + msg:send_region(r) + end + if clear then + end + end + end + else + local prevseason = get_season(turn-1) + if prevseason == "calendar::winter" then + -- we celebrate knut and kick out the trees. + for r in regions() do + if r:get_key("xm06") then + r:set_key("xm06", false) + end + end + end + end +end + +return self diff --git a/scripts/tests/e2/e2features.lua b/scripts/tests/e2/e2features.lua index 8505bfd39..b9a705fa5 100644 --- a/scripts/tests/e2/e2features.lua +++ b/scripts/tests/e2/e2features.lua @@ -201,6 +201,18 @@ function test_no_uruk() assert_equal(f1.race, "orc") end +function test_snowglobe() + local r1 = region.create(0, 0, "glacier") + local r2 = region.create(1, 0, "ocean") + local f = faction.create("noreply@eressea.de", "human", "de") + local u = unit.create(f, r1, 1) + u:add_item("snowglobe", 1) + u:clear_orders() + u:add_order("BENUTZEN 1 Schneekugel Ost") + process_orders() + assert_equal("glacier", r2.terrain) +end + function test_snowman() local r = region.create(0, 0, "glacier") local f = faction.create("noreply@eressea.de", "human", "de") diff --git a/src/helpers.c b/src/helpers.c index a81a91679..c784e9901 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -19,6 +19,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include #include @@ -509,8 +510,8 @@ struct order *ord) if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); lua_pushinteger(L, amount); - - if (lua_pcall(L, 2, 1, 0) != 0) { + lua_pushstring(L, getstrtoken()); + if (lua_pcall(L, 3, 1, 0) != 0) { const char *error = lua_tostring(L, -1); log_error("use(%s) calling '%s': %s.\n", unitname(u), fname, error); lua_pop(L, 1); diff --git a/src/laws.c b/src/laws.c index f508c4b69..5c6d5a46e 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3442,10 +3442,13 @@ void update_long_order(unit * u) static int use_item(unit * u, const item_type * itype, int amount, struct order *ord) { int i; - int target = read_unitid(u->faction, u->region); + int target = -1; + + if (itype->useonother) { + target = read_unitid(u->faction, u->region); + } i = get_pooled(u, itype->rtype, GET_DEFAULT, amount); - if (amount > i) { /* TODO: message? eg. "not enough %, using only %" */ amount = i; @@ -3455,20 +3458,16 @@ static int use_item(unit * u, const item_type * itype, int amount, struct order } if (target == -1) { - int result; - if (itype->use == NULL) { - return EUNUSABLE; + if (itype->use) { + int result = itype->use(u, itype, amount, ord); + if (result > 0) { + use_pooled(u, itype->rtype, GET_DEFAULT, result); + } + return result; } - result = itype->use ? itype->use(u, itype, amount, ord) : EUNUSABLE; - if (result>0) { - use_pooled(u, itype->rtype, GET_DEFAULT, result); - } - return result; + return EUNUSABLE; } else { - if (itype->useonother == NULL) { - return EUNUSABLE; - } return itype->useonother(u, target, itype, amount, ord); } }