From f2c02232cdae3085e5178bf0db9b8d878b2e3e6a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 15 May 2016 23:21:39 +0200 Subject: [PATCH] http://bugs.eressea.de/view.php?id=2207 check that there are any regions that we can seed new factions into don't run into endless loop --- scripts/eressea/autoseed.lua | 42 ++++++++++++++++++++++-------------- src/bind_unit.c | 20 +++++++---------- src/bindings.c | 2 +- src/kernel/save.c | 2 +- src/util/log.c | 1 + 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/scripts/eressea/autoseed.lua b/scripts/eressea/autoseed.lua index e5cd2e478..5bdbf6c37 100644 --- a/scripts/eressea/autoseed.lua +++ b/scripts/eressea/autoseed.lua @@ -47,8 +47,11 @@ local function read_players() end local function seed(r, email, race, lang) + assert(r) local f = faction.create(email, race, lang) + assert(f) local u = unit.create(f, r) + assert(u) equip_unit(u, "new_faction") equip_unit(u, "first_unit") equip_unit(u, "first_" .. race, 7) -- disable old callbacks @@ -69,31 +72,38 @@ end function autoseed.init() -- local newbs = {} - local num_seeded = per_region + local num_seeded = 0 local start = nil eressea.log.info('autoseed new players') players = read_players() + if players then + print('autoseed ' .. #players .. ' new players') + end if players and #players >= per_region then local sel eressea.log.info(#players .. ' new players') sel = select_regions(regions(), peasants, trees) - for _, p in ipairs(players) do - if num_seeded == per_region then - while not start or start.units() do - local index = 1 + (rng_int() % #sel) - start = sel[index] + if #sel == 0 then + eressea.log.error("autoseed could not select regions for new factions") + else + for _, p in ipairs(players) do + if num_seeded == per_region then + while not start or start.units() do + local index = 1 + (rng_int() % #sel) + start = sel[index] + end + num_seeded = 0 + end + local dupe = get_faction_by_email(p.email) + if dupe then + eressea.log.warning("seed: duplicate email " .. p.email .. " already used by " .. tostring(dupe)) + else + local f = seed(start, p.email, p.race or "human", p.lang or "de") + num_seeded = num_seeded + 1 + print("new faction ".. tostring(f) .. " starts in ".. tostring(start)) + -- table.insert(newbs, f) end - num_seeded = 0 - end - local dupe = get_faction_by_email(p.email) - if dupe then - eressea.log.warning("seed: duplicate email " .. p.email .. " already used by " .. tostring(dupe)) - else - local f = seed(start, p.email, p.race or "human", p.lang or "de") - num_seeded = num_seeded + 1 - print("new faction ".. tostring(f) .. " starts in ".. tostring(start)) - -- table.insert(newbs, f) end end end diff --git a/src/bind_unit.c b/src/bind_unit.c index 29c2d287a..353bd4667 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -889,19 +889,15 @@ static int tolua_unit_create(lua_State * L) { faction *f = (faction *)tolua_tousertype(L, 1, 0); region *r = (region *)tolua_tousertype(L, 2, 0); + const char *rcname = tolua_tostring(L, 4, NULL); int num = (int)tolua_tonumber(L, 3, 1); - if (f && r) { - const race *rc = f->race; - const char *rcname = tolua_tostring(L, 4, NULL); - if (rcname) - rc = rc_find(rcname); - if (rc) { - unit *u = create_unit(r, f, num, rc, 0, NULL, NULL); - tolua_pushusertype(L, u, TOLUA_CAST "unit"); - return 1; - } - } - return 0; + const race *rc; + assert(f && r); + rc = rcname ? rc_find(rcname) : f->race; + assert(rc); + unit *u = create_unit(r, f, num, rc, 0, NULL, NULL); + tolua_pushusertype(L, u, TOLUA_CAST "unit"); + return 1; } static int tolua_unit_tostring(lua_State * L) diff --git a/src/bindings.c b/src/bindings.c index b2db9d761..b95b1f9f0 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -448,7 +448,7 @@ static int tolua_equipunit(lua_State * L) unit *u = (unit *)tolua_tousertype(L, 1, 0); const char *eqname = tolua_tostring(L, 2, 0); int mask = (int)tolua_tonumber(L, 3, EQUIP_ALL); - assert(mask > 0); + assert(u && mask > 0); equip_unit_mask(u, get_equipment(eqname), mask); return 0; } diff --git a/src/kernel/save.c b/src/kernel/save.c index 12afb6909..fd82bed96 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -174,7 +174,7 @@ static unit *unitorders(FILE * F, int enc, struct faction *f) if (s[0] != '@') { char token[128]; const char *stok = s; - stok = parse_token(&stok, token, sizeof(token)); + stok = parse_token(&stok, token, 64); // FIXME: use sizeof, but parse_token overwrites the buffer if (stok) { bool quit = false; diff --git a/src/util/log.c b/src/util/log.c index 6d8d5a7f4..0532e42ab 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -176,6 +176,7 @@ static void log_stdio(void *data, int level, const char *module, const char *for if (format[len - 1] != '\n') { fputc('\n', out); } + fflush(out); } log_t *log_to_file(int flags, FILE *out) {