From 93cdec9ddc28af5c780d6b790541abdd8ed4002d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 4 Nov 2014 07:57:29 +0100 Subject: [PATCH 1/9] fix json test (null-terminate read) --- src/json.test.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/json.test.c b/src/json.test.c index 73524f2e8..5b3f9813e 100644 --- a/src/json.test.c +++ b/src/json.test.c @@ -35,12 +35,14 @@ static void test_export_no_regions(CuTest * tc) { char buf[1024]; stream out = { 0 }; int err; + size_t len; mstream_init(&out); err = json_export(&out, EXPORT_REGIONS); CuAssertIntEquals(tc, 0, err); out.api->rewind(out.handle); - out.api->read(out.handle, buf, sizeof(buf)); + len = out.api->read(out.handle, buf, sizeof(buf)); + buf[len] = '\0'; CuAssertStrEquals(tc, "{}", strip(buf)); mstream_done(&out); } From 93e20ac7a928d7c96a001eb5de90b3c9a20c4d42 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 4 Nov 2014 08:06:46 +0100 Subject: [PATCH 2/9] clean up data files after tests are finished. --- scripts/eressea/tests/common.lua | 1 + scripts/eressea/tests/orders.lua | 1 + src/kernel/group.test.c | 1 + 3 files changed, 3 insertions(+) diff --git a/scripts/eressea/tests/common.lua b/scripts/eressea/tests/common.lua index 8c8664992..365b87195 100644 --- a/scripts/eressea/tests/common.lua +++ b/scripts/eressea/tests/common.lua @@ -45,6 +45,7 @@ function test_flags() eressea.write_game("test.dat") eressea.free_game() eressea.read_game("test.dat") + os.remove('test.dat') f = get_faction(no) assert_equal(flags, f.flags) end diff --git a/scripts/eressea/tests/orders.lua b/scripts/eressea/tests/orders.lua index ce6ba9949..af1ba8863 100644 --- a/scripts/eressea/tests/orders.lua +++ b/scripts/eressea/tests/orders.lua @@ -105,6 +105,7 @@ function test_process_quit() eressea.write_game('test.dat') eressea.free_game() eressea.read_game('test.dat') + os.remove('test.dat') assert_equal(nil, _G.get_faction(fno)) end diff --git a/src/kernel/group.test.c b/src/kernel/group.test.c index 1272fc493..46d794d2d 100644 --- a/src/kernel/group.test.c +++ b/src/kernel/group.test.c @@ -50,6 +50,7 @@ static void test_group_readwrite(CuTest * tc) CuAssertPtrEquals(tc, 0, f->groups->allies->next); CuAssertPtrEquals(tc, f, f->groups->allies->faction); CuAssertIntEquals(tc, HELP_GIVE, f->groups->allies->status); + remove("test.dat"); test_cleanup(); } From 61f675fad82743e8fb413eceb869e6a9540127d7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 4 Nov 2014 08:14:53 +0100 Subject: [PATCH 3/9] critbit submodule update --- critbit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/critbit b/critbit index 2a7af5e23..c267f95e4 160000 --- a/critbit +++ b/critbit @@ -1 +1 @@ -Subproject commit 2a7af5e2347217ea4efcf7fb3f0bc9c2681d1a17 +Subproject commit c267f95e4170c40809effa61af418fbe63d43e48 From 143fab9653258367b8c23da24e1829974da10561 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 6 Nov 2014 07:15:53 +0100 Subject: [PATCH 4/9] Some changes after talking to Don. moved locale change into its own module. made monsters get a report by default. reoved old creation.c code, it was dead. --- scripts/eressea/e2/init.lua | 1 + scripts/eressea/locales.lua | 20 +++++++++++ scripts/run-turn.lua | 15 -------- src/CMakeLists.txt | 1 - src/creation.c | 71 ------------------------------------- src/creation.h | 30 ---------------- src/kernel/faction.c | 1 - 7 files changed, 21 insertions(+), 118 deletions(-) create mode 100644 scripts/eressea/locales.lua delete mode 100644 src/creation.c delete mode 100644 src/creation.h diff --git a/scripts/eressea/e2/init.lua b/scripts/eressea/e2/init.lua index 4705631dd..a23aa3216 100644 --- a/scripts/eressea/e2/init.lua +++ b/scripts/eressea/e2/init.lua @@ -12,5 +12,6 @@ return { require('eressea.tunnels'), require('eressea.ponnuki'), require('eressea.astral'), + require('eressea.locales'), require('eressea.ents') } diff --git a/scripts/eressea/locales.lua b/scripts/eressea/locales.lua new file mode 100644 index 000000000..c3a9cdd83 --- /dev/null +++ b/scripts/eressea/locales.lua @@ -0,0 +1,20 @@ +local function change_locales(localechange) + for loc, flist in pairs(localechange) do + for index, name in pairs(flist) do + f = get_faction(atoi36(name)) + if f ~= nil and f.locale ~= loc then + print("LOCALECHANGE ", f, f.locale, loc) + f.locale = loc + end + end + end +end + +local package = {} + +function pacakge.update() + local localechange = { de = { 'ii' } } + change_locales(localechange) +end + +return package diff --git a/scripts/run-turn.lua b/scripts/run-turn.lua index f938c8514..c9033afe3 100644 --- a/scripts/run-turn.lua +++ b/scripts/run-turn.lua @@ -26,18 +26,6 @@ function callbacks(rules, name, ...) end end -local function change_locales(localechange) - for loc, flist in pairs(localechange) do - for index, name in pairs(flist) do - f = get_faction(atoi36(name)) - if f ~= nil and f.locale ~= loc then - print("LOCALECHANGE ", f, f.locale, loc) - f.locale = loc - end - end - end -end - local function dbupdate() update_scores() dbname = config.dbname or 'eressea.db' @@ -153,9 +141,6 @@ function process(rules, orders) process_orders() callbacks(rules, 'update') - local localechange = { de = { 'ii' } } - change_locales(localechange) - write_files(config.locales) file = '' .. get_turn() .. '.dat' diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 402e4dc58..3ac447096 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -77,7 +77,6 @@ set (ERESSEA_SRC keyword.c skill.c json.c - creation.c creport.c economy.c give.c diff --git a/src/creation.c b/src/creation.c deleted file mode 100644 index 7cf540f30..000000000 --- a/src/creation.c +++ /dev/null @@ -1,71 +0,0 @@ -/* -Copyright (c) 1998-2014, Enno Rehling - Katja Zedel - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -**/ - -#include -#include -#include "creation.h" -#include "monster.h" -#include "alchemy.h" - -/* kernel includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* util includes */ -#include -#include - -/* libc includes */ -#include -#include -#include -#include -#include -#include -#include -#include - -faction *createmonsters(int no) -{ - faction *f = findfaction(no); - - if (f) { - puts("* Fehler! Die Monster Partei gibt es schon."); - return f; - } - f = (faction *) calloc(1, sizeof(faction)); - f->no = no; - /* alles ist auf null gesetzt, ausser dem folgenden. achtung - partei - * no 0 muss keine orders einreichen! */ - - f->email = _strdup("monsters@eressea.de"); - f->name = _strdup("Monster"); - f->alive = 1; - f->options = (char)(1 << O_REPORT); - addlist(&factions, f); - fhash(f); - return f; -} diff --git a/src/creation.h b/src/creation.h deleted file mode 100644 index 47146d303..000000000 --- a/src/creation.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -Copyright (c) 1998-2010, Enno Rehling - Katja Zedel - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -**/ - -#ifndef H_GC_CREATION -#define H_GC_CREATION -#ifdef __cplusplus -extern "C" { -#endif - - struct faction *createmonsters(int no); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 2f58b994b..1515e9c65 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -169,7 +169,6 @@ faction *get_or_create_monsters(void) } renumber_faction(f, 666); faction_setname(f, "Monster"); - f->options = 0; } if (f) { fset(f, FFL_NPC | FFL_NOIDLEOUT); From 49493fa630e9e03b1639717c9391a4937a8befa4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 6 Nov 2014 07:53:15 +0100 Subject: [PATCH 5/9] fix typo in last commit. package is a standard lua module, not a good variable name. log name of script. --- scripts/eressea/locales.lua | 6 +++--- src/main.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/eressea/locales.lua b/scripts/eressea/locales.lua index c3a9cdd83..aa895ddb9 100644 --- a/scripts/eressea/locales.lua +++ b/scripts/eressea/locales.lua @@ -10,11 +10,11 @@ local function change_locales(localechange) end end -local package = {} +local pkg = {} -function pacakge.update() +function pkg.update() local localechange = { de = { 'ii' } } change_locales(localechange) end -return package +return pkg diff --git a/src/main.c b/src/main.c index 10cb2c002..7f74c9ea4 100644 --- a/src/main.c +++ b/src/main.c @@ -268,7 +268,7 @@ int main(int argc, char **argv) bind_monsters(L); err = eressea_run(L, luafile); if (err) { - log_error("server execution failed with code %d\n", err); + log_error("script %s failed with code %d\n", luafile, err); return err; } #ifdef MSPACES From c8f2f33eab70f6863dc530901631175181bc87e0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 7 Nov 2014 21:15:46 +0100 Subject: [PATCH 6/9] clean up some dealings with monsters and NPCs --- src/kernel/faction.c | 33 +++++++-------------------------- src/kernel/faction.h | 2 +- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 1515e9c65..b61db7297 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -139,43 +139,24 @@ void set_show_item(faction * f, const struct item_type *itype) a->data.v = (void *)itype; } -faction *get_monsters(void) { - faction *f; - - for (f = factions; f; f = f->next) { - if ((f->flags & FFL_NPC) && !(f->flags & FFL_DEFENDER)) { - return f; - } - } - return 0; -} - faction *get_or_create_monsters(void) { - faction *f = get_monsters(); + faction *f = findfaction(666); if (!f) { - /* shit! */ - f = findfaction(666); - } - if (!f) { - const race *rc = rc_find("dragon"); - + const race *rc = rc_get_or_create("dragon"); const char *email = get_param(global.parameters, "monster.email"); - if (email) { - f = addfaction(email, NULL, rc, NULL, 0); - } - else { - f = addfaction("noreply@eressea.de", NULL, rc, NULL, 0); - } + f = addfaction(email ? email : "noreply@eressea.de", NULL, rc, NULL, 0); renumber_faction(f, 666); faction_setname(f, "Monster"); - } - if (f) { fset(f, FFL_NPC | FFL_NOIDLEOUT); } return f; } +faction *get_monsters(void) { + return get_or_create_monsters(); +} + const unit *random_unit_in_faction(const faction * f) { unit *u; diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 25d8f20b5..91c98546a 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -54,7 +54,7 @@ extern "C" { #define FFL_SAVEMASK (FFL_DEFENDER|FFL_NEWID|FFL_GM|FFL_NPC|FFL_DBENTRY|FFL_NOIDLEOUT) -#define is_monsters(f) ((f)->flags&FFL_NPC) +#define is_monsters(f) (f && f==get_monsters()) typedef struct faction { struct faction *next; From d1f7fb571c7ab39059b2f3592e154c8a5ce88c75 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 7 Nov 2014 21:28:58 +0100 Subject: [PATCH 7/9] fix test for get_monsters(), nobody needs the function to ever return NULL. --- src/kernel/faction.test.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 34adf3ea0..644fb83e0 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -91,20 +91,18 @@ static void test_addfaction(CuTest *tc) { CuAssertIntEquals(tc, M_GRAY, f->magiegebiet); CuAssertIntEquals(tc, turn, f->lastorders); CuAssertPtrEquals(tc, f, findfaction(f->no)); + test_cleanup(); } static void test_get_monsters(CuTest *tc) { faction *f; - CuAssertPtrEquals(tc, NULL, get_monsters()); - f = get_or_create_monsters(); + + free_gamedata(); + CuAssertPtrNotNull(tc, (f = get_monsters())); CuAssertPtrEquals(tc, f, get_monsters()); CuAssertIntEquals(tc, 666, f->no); CuAssertStrEquals(tc, "Monster", f->name); - free_gamedata(); - CuAssertPtrEquals(tc, NULL, get_monsters()); - f = get_or_create_monsters(); - CuAssertPtrEquals(tc, f, get_monsters()); - CuAssertIntEquals(tc, 666, f->no); + test_cleanup(); } CuSuite *get_faction_suite(void) From e9084d967894ca7948fd5954845c6659379303c0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 7 Nov 2014 22:58:29 +0100 Subject: [PATCH 8/9] when free_gamedata, also unhash all factions. speed up is_monsters(). --- src/kernel/faction.c | 8 ++++++-- src/kernel/faction.h | 2 +- src/kernel/unit.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/kernel/faction.c b/src/kernel/faction.c index b61db7297..8204afd67 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -68,6 +68,7 @@ faction *factions; */ void free_faction(faction * f) { + funhash(f); if (f->msgs) free_messagelist(f->msgs); while (f->battles) { @@ -113,9 +114,12 @@ void funhash(faction * f) { int index = f->no % FMAXHASH; faction **fp = factionhash + index; - while (*fp && (*fp) != f) + while (*fp && (*fp) != f) { fp = &(*fp)->nexthash; - *fp = f->nexthash; + } + if (*fp == f) { + *fp = f->nexthash; + } } static faction *ffindhash(int no) diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 91c98546a..0ec42fe5c 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -54,7 +54,7 @@ extern "C" { #define FFL_SAVEMASK (FFL_DEFENDER|FFL_NEWID|FFL_GM|FFL_NPC|FFL_DBENTRY|FFL_NOIDLEOUT) -#define is_monsters(f) (f && f==get_monsters()) +#define is_monsters(f) (f && fval(f, FFL_NPC) && f==get_monsters()) typedef struct faction { struct faction *next; diff --git a/src/kernel/unit.c b/src/kernel/unit.c index affd4471d..87648994b 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -733,7 +733,7 @@ void set_level(unit * u, skill_t sk, int value) { skill *sv = u->skills; - assert(sk != SK_MAGIC || !u->faction || is_monsters(u->faction) || u->number == 1); + assert(sk != SK_MAGIC || !u->faction || u->number == 1 || is_monsters(u->faction)); if (!skill_enabled(sk)) return; From 85a5a433bb9e188340532a8312bcb0f6eb930378 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 7 Nov 2014 23:16:00 +0100 Subject: [PATCH 9/9] writing reports should not depend on the monster faction, this is silly. --- src/reports.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/reports.c b/src/reports.c index 29159d0c2..54e072a46 100644 --- a/src/reports.c +++ b/src/reports.c @@ -2337,14 +2337,13 @@ static void eval_int36(struct opstack **stack, const void *userdata) static void log_orders(const struct message *msg) { - faction *f = get_monsters(); char buffer[4096]; int i; for (i = 0; i != msg->type->nparameters; ++i) { if (msg->type->types[i]->copy == &var_copy_order) { const char *section = nr_section(msg); - nr_render(msg, f ? f->locale : default_locale, buffer, sizeof(buffer), f); + nr_render(msg, default_locale, buffer, sizeof(buffer), NULL); log_debug("MESSAGE [%s]: %s\n", section, buffer); break; }