putting all of the pieces together for the snow globe, with final use implementation still TBD

Conflicts:
	scripts/eressea/xmasitems.lua
	src/laws.c
This commit is contained in:
Enno Rehling 2015-11-27 15:23:48 +01:00
parent 81888132e4
commit 3fd4884584
6 changed files with 120 additions and 15 deletions

View File

@ -1581,6 +1581,14 @@
<text locale="de">Schneemann</text>
<text locale="en">snowman</text>
</string>
<string name="snowglobe">
<text locale="de">Schneekugel</text>
<text locale="en">snow globe</text>
</string>
<string name="snowglobe_p">
<text locale="de">Schneekugeln</text>
<text locale="en">snow globes</text>
</string>
<string name="snowman_p">
<text locale="de">Schneemänner</text>
<text locale="en">snowmen</text>

View File

@ -22,6 +22,12 @@
</item>
</resource>
<resource name="snowglobe">
<item notlost="yes" weight="1">
<function name="use" value="lua_useitem"/>
</item>
</resource>
<resource name="ring_of_levitation" appearance="ring">
<item notlost="yes" weight="0" cursed="true">
<function name="use" value="lua_useitem"/>

View File

@ -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

View File

@ -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")

View File

@ -19,6 +19,7 @@ without prior permission by the authors of Eressea.
#include <util/bsdstring.h>
#include <util/functions.h>
#include <util/log.h>
#include <util/parser.h>
#include <util/resolve.h>
#include <kernel/config.h>
@ -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);

View File

@ -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;
}
result = itype->use ? itype->use(u, itype, amount, ord) : 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;
}
else {
if (itype->useonother == NULL) {
return EUNUSABLE;
}
else {
return itype->useonother(u, target, itype, amount, ord);
}
}