new bindings for allies.

script to fix damage from bug 2608.
This commit is contained in:
Enno Rehling 2019-09-15 13:08:50 +02:00
parent fef57d3fcf
commit fa3abe8c32
3 changed files with 125 additions and 3 deletions

76
scripts/fix-death.lua Normal file
View File

@ -0,0 +1,76 @@
require 'config'
eressea.read_game('1137.dat')
local dead = {"cwz", "rama"}
local function list_items(f)
local items = {}
for u in f.units do
local r = u.region
for name in u.items do
local count = u:get_item(name)
if not items[r.id] then
items[r.id] = {}
end
if not items[r.id][name] then
items[r.id][name] = count
else
items[r.id][name] = items[r.id][name] + count
end
end
end
return items
end
gifts = {}
info = {}
for _, no in ipairs(dead) do
f = get_faction(no)
gifts[f.id] = list_items(f)
local allies = {}
for fno, as in pairs(f.allies) do
local f2 = get_faction(fno)
if f2:get_ally(f, 'give') then
allies[fno] = as
end
end
info[f.id] = {
['name'] = f.name,
['race'] = f.race,
['allies'] = allies
}
end
eressea.free_game()
eressea.read_game('1138.dat')
newf = {}
for fid, rlist in pairs(gifts) do
local name = "Erben von " . info[fid].name
local race = info[fid].race
local f = faction.create(race, "noreply@eressea.de")
f.name = name
f.age = 10
f.lastturn = 1130
table.insert(newf, f)
for rid, items in pairs(rlist) do
local r = get_region_by_id(rid)
local u = unit.create(f, r, 1)
for name, count in pairs(items) do
u:add_item(name, count)
end
end
for fno, as in pairs(info[fid].allies) do
local f2 = get_faction(fno)
for _, s in ipairs(as) do
f:set_ally(f2, s)
end
f2:set_ally(f, "give")
end
end
eressea.write_game('1138.new.dat')

View File

@ -2,7 +2,7 @@ require "lunit"
module("tests.e2.allies", package.seeall, lunit.testcase) module("tests.e2.allies", package.seeall, lunit.testcase)
function test_get_set_ally() function skip_test_get_set_ally()
local f1 = faction.create("human") local f1 = faction.create("human")
local f2 = faction.create("human") local f2 = faction.create("human")
@ -13,3 +13,18 @@ function test_get_set_ally()
f1:set_ally(f2, "give", true) f1:set_ally(f2, "give", true)
assert_equal(true, f1:get_ally(f2, "give")) assert_equal(true, f1:get_ally(f2, "give"))
end end
function test_get_allies()
local f1 = faction.create("human")
local f2 = faction.create("human")
local allies = f1.allies
assert_equal('table', type(allies))
assert_equal(0, #allies)
f1:set_ally(f2, "give", true)
allies = f1.allies
assert_not_nil(1, table.getn(allies))
assert_equal('table', type(allies[f2.id]))
assert_equal(1, #allies[f2.id])
assert_equal("give", allies[f2.id][1])
end

View File

@ -492,8 +492,38 @@ static int tolua_faction_set_info(lua_State * L)
return 0; return 0;
} }
static int tolua_faction_set_ally(lua_State * L) /* TODO: this is probably useful elsewhere */
{ static const char *status_names[] = {
"money", "fight", "observe", "give", "guard", "stealth", "travel", NULL
};
static int cb_ally_push(struct allies *af, struct faction *f, int status, void *udata) {
struct lua_State *L = (struct lua_State *)udata;
int len = 1;
int i;
lua_pushnumber(L, f->no);
lua_newtable(L);
for (i = 0; status_names[i]; ++i) {
int flag = 1 << i;
if (status & flag) {
lua_pushstring(L, status_names[i]);
lua_rawseti(L, -2, len++);
}
}
lua_rawset(L, -3);
return 0;
}
static int tolua_faction_get_allies(lua_State * L) {
faction *f = (faction *)tolua_tousertype(L, 1, NULL);
lua_newtable(L);
allies_walk(f->allies, cb_ally_push, L);
return 1;
}
static int tolua_faction_set_ally(lua_State * L) {
faction *f1 = (faction *)tolua_tousertype(L, 1, NULL); faction *f1 = (faction *)tolua_tousertype(L, 1, NULL);
faction *f2 = (faction *)tolua_tousertype(L, 2, NULL); faction *f2 = (faction *)tolua_tousertype(L, 2, NULL);
const char *status = tolua_tostring(L, 3, NULL); const char *status = tolua_tostring(L, 3, NULL);
@ -618,6 +648,7 @@ void tolua_faction_open(lua_State * L)
tolua_variable(L, TOLUA_CAST "alliance", tolua_faction_get_alliances, tolua_variable(L, TOLUA_CAST "alliance", tolua_faction_get_alliances,
tolua_faction_set_alliance); tolua_faction_set_alliance);
tolua_variable(L, TOLUA_CAST "allies", tolua_faction_get_allies, NULL);
tolua_function(L, TOLUA_CAST "set_ally", tolua_faction_set_ally); tolua_function(L, TOLUA_CAST "set_ally", tolua_faction_set_ally);
tolua_function(L, TOLUA_CAST "get_ally", tolua_faction_get_ally); tolua_function(L, TOLUA_CAST "get_ally", tolua_faction_get_ally);