From cd2624de8acfa430f44ee5d4ecd78a27a1e77bd3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 6 May 2018 15:49:34 +0200 Subject: [PATCH] stop reallocation spell parameters. fix involuntary tree growth in test, message check. --- scripts/tests/e2/init.lua | 2 +- scripts/tests/e2/items.lua | 1 + scripts/tests/e2/spells.lua | 33 +++++++++++++++++++++++++++++++++ scripts/tests/spells.lua | 2 +- src/bind_faction.c | 1 + src/bindings.c | 29 ++++++++++++++++------------- src/laws.c | 7 ------- src/laws.h | 1 - src/magic.c | 32 +++++++++++--------------------- 9 files changed, 64 insertions(+), 44 deletions(-) diff --git a/scripts/tests/e2/init.lua b/scripts/tests/e2/init.lua index 9dd507671..e9c14ec61 100644 --- a/scripts/tests/e2/init.lua +++ b/scripts/tests/e2/init.lua @@ -1,6 +1,6 @@ +require 'tests.e2.spells' require 'tests.e2.e2features' require 'tests.e2.insects' -require 'tests.e2.spells' require 'tests.e2.buildings' require 'tests.e2.production' require 'tests.e2.adamantium' diff --git a/scripts/tests/e2/items.lua b/scripts/tests/e2/items.lua index e444a9c73..456262342 100644 --- a/scripts/tests/e2/items.lua +++ b/scripts/tests/e2/items.lua @@ -9,6 +9,7 @@ function setup() eressea.settings.set("rules.ship.storms", "0") eressea.settings.set("rules.encounters", "0") eressea.settings.set("magic.regeneration.enable", "0") + eressea.settings.set("rules.grow.formula", "0") eressea.settings.set("study.random_progress", "0") end diff --git a/scripts/tests/e2/spells.lua b/scripts/tests/e2/spells.lua index deeb2798c..53c86d413 100644 --- a/scripts/tests/e2/spells.lua +++ b/scripts/tests/e2/spells.lua @@ -12,6 +12,39 @@ function setup() eressea.settings.set("magic.fumble.enable", "0") end +function disable_pull_astral() + local r = region.create(0, 0, "plain") + local f = faction.create("human") + local u1 = unit.create(f, r, 1) + local u2 = unit.create(f, r, 1) + u1.magic = "gray" + u1:set_skill("magic", 6) + u1.aura = 1 + u1:add_spell("pull_astral") + u1:clear_orders() + u1:add_order("ZAUBERE Astraler~Ruf " .. itoa36(u2.id)) + -- process_orders() + -- assert_equal(1, f:count_msg_type('error194'), 'no target region') + -- assert_equal(1, f:count_msg_type('error209'), 'bad arguments') + u1:clear_orders() + + u1:add_order("ZAUBERE Astraler~Ruf 1 0 " .. itoa36(u2.id)) + process_orders() + assert_equal(1, f:count_msg_type('error194'), 'no target region') + assert_equal(0, f:count_msg_type('error209'), 'bad arguments') + assert_equal(1, f:count_msg_type('spellregionresists'), 'no such region') + + u1:add_order("ZAUBERE Astraler~Ruf 0 0 " .. itoa36(u2.id)) + process_orders() + assert_equal(0, f:count_msg_type('error194'), 'no target region') + assert_equal(0, f:count_msg_type('error209'), 'bad arguments') + assert_equal(0, f:count_msg_type('spellregionresists'), 'no such region') + for k, v in ipairs(f.messages) do + print(v) + end + assert_equal(u1, u2) +end + function test_shapeshift() local r = region.create(42, 0, "plain") local f = faction.create("demon", "noreply@eressea.de", "de") diff --git a/scripts/tests/spells.lua b/scripts/tests/spells.lua index de56472c3..28efb936a 100644 --- a/scripts/tests/spells.lua +++ b/scripts/tests/spells.lua @@ -21,7 +21,7 @@ function test_create_bogus() u:clear_orders() u:add_order("ZAUBERE 'Erschaffe Katastrophe'") process_orders() - assert_equal(f.messages[3], 'error173') -- HACKity HACK + assert_equal(1, f:count_msg_type('error173'), 'spell not found') end function test_create_roi() diff --git a/src/bind_faction.c b/src/bind_faction.c index c0875aae2..ac797093d 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -39,6 +39,7 @@ without prior permission by the authors of Eressea. #include #include +#include #include typedef struct helpmode { diff --git a/src/bindings.c b/src/bindings.c index f9f51ded3..3fda9b844 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -476,22 +476,17 @@ static int tolua_write_reports(lua_State * L) return 1; } -static int tolua_process_orders(lua_State * L) -{ - UNUSED_ARG(L); -#if 0 - order * ord = parse_order("@GIB xmis ALLES Gurgelkraut", default_locale); - assert(ord); - free_order(ord); - return 0; -#endif - processorders(); - return 0; -} - static int tolua_turn_begin(lua_State * L) { + faction *f; UNUSED_ARG(L); + for (f = factions; f; f = f->next) { + if (f->msgs) { + free_messagelist(f->msgs->begin); + free(f->msgs); + f->msgs = NULL; + } + } turn_begin(); return 0; } @@ -510,6 +505,14 @@ static int tolua_turn_end(lua_State * L) return 0; } +static int tolua_process_orders(lua_State * L) +{ + UNUSED_ARG(L); + tolua_turn_begin(L); + tolua_turn_process(L); + return tolua_turn_end(L); +} + static int tolua_write_passwords(lua_State * L) { int result = writepasswd(); diff --git a/src/laws.c b/src/laws.c index b24fa5b3b..9eb3795c6 100644 --- a/src/laws.c +++ b/src/laws.c @@ -4140,13 +4140,6 @@ void turn_end(void) update_spells(); } -void processorders(void) -{ - turn_begin(); - turn_process(); - turn_end(); -} - void update_subscriptions(void) { FILE *F; diff --git a/src/laws.h b/src/laws.h index 56ed9623b..ae2c712ca 100755 --- a/src/laws.h +++ b/src/laws.h @@ -53,7 +53,6 @@ extern "C" { int enter_building(struct unit *u, struct order *ord, int id, bool report); int enter_ship(struct unit *u, struct order *ord, int id, bool report); - void processorders(void); void turn_begin(void); void turn_process(void); void turn_end(void); diff --git a/src/magic.c b/src/magic.c index a00a29527..4c2df4023 100644 --- a/src/magic.c +++ b/src/magic.c @@ -1822,7 +1822,7 @@ addparam_region(const char *const param[], spllprm ** spobjp, const unit * u, order * ord) { assert(param[0]); - if (param[1] == 0) { + if (param[1] == NULL) { /* Fehler: Zielregion vergessen */ cmistake(u, ord, 194, MSG_MAGIC); return -1; @@ -2014,13 +2014,13 @@ static spellparameter *add_spellparameter(region * target_r, unit * u, /* im Endeffekt waren es evtl. nur p parameter (wegen TEMP) */ par->length = p; - if (fail || par->length < minlen) { + if (par->length < minlen) { cmistake(u, ord, 209, MSG_MAGIC); free_spellparameter(par); return NULL; } - return par; + return fail ? NULL : par; } struct unit * co_get_caster(const struct castorder * co) { @@ -2412,6 +2412,7 @@ static bool is_moving_ship(ship * sh) return false; } +#define MAX_PARAMETERS 8 static castorder *cast_cmd(unit * u, order * ord) { char token[128]; @@ -2612,33 +2613,22 @@ static castorder *cast_cmd(unit * u, order * ord) } /* Weitere Argumente zusammenbasteln */ if (sp->parameter) { - char **params = (char**)malloc(2 * sizeof(char *)); - int p = 0, size = 2; + char *params[MAX_PARAMETERS]; + int i, p = 0; for (;;) { s = gettoken(token, sizeof(token)); - if (!s || *s == 0) + if (!s || *s == 0) { break; - if (p + 1 >= size) { - char ** tmp; - tmp = (char**)realloc(params, sizeof(char *) * size * 2); - if (tmp) { - size *= 2; - params = tmp; - } - else { - log_error("error allocationg %d bytes: %s", size * 2, strerror(errno)); - break; - } } + assert(p + 1 < MAX_PARAMETERS); params[p++] = str_strdup(s); } - params[p] = 0; args = add_spellparameter(target_r, caster, sp->parameter, (const char *const *)params, p, ord); - for (p = 0; params[p]; ++p) - free(params[p]); - free(params); + for (i = 0; i != p; ++i) { + free(params[i]); + } if (args == NULL) { /* Syntax war falsch */ return 0;