From 7df47fd25c3b44096a08f429a3a4a9e0449a67d4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 16 Oct 2014 07:34:09 +0200 Subject: [PATCH] remove bad configuration caching (TODO: cache inside get_param). move remove_empty_* functions out of config.c add basic tests for remove_empty_units (no special units yet). --- src/bind_eressea.c | 1 + src/economy.c | 2 +- src/kernel/config.c | 118 +---------------------------------------- src/kernel/config.h | 4 -- src/kernel/faction.c | 54 +++++++++++++++++++ src/kernel/faction.h | 2 + src/kernel/unit.c | 47 ++++++++++++++++ src/kernel/unit.h | 2 + src/kernel/unit.test.c | 38 +++++++++++++ 9 files changed, 147 insertions(+), 121 deletions(-) diff --git a/src/bind_eressea.c b/src/bind_eressea.c index d6f1d1eb5..664808b17 100755 --- a/src/bind_eressea.c +++ b/src/bind_eressea.c @@ -4,6 +4,7 @@ #include "json.h" +#include #include #include #include diff --git a/src/economy.c b/src/economy.c index 965df6478..bb24fd5e5 100644 --- a/src/economy.c +++ b/src/economy.c @@ -57,9 +57,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include +#include #include #include diff --git a/src/kernel/config.c b/src/kernel/config.c index 7bffa0bbf..6f0004708 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -102,7 +102,6 @@ struct settings global = { bool lomem = false; FILE *logfile; -FILE *updatelog; bool battledebug = false; int turn = -1; @@ -122,17 +121,6 @@ bool IsImmune(const faction * f) return !fval(f, FFL_NPC) && f->age < NewbieImmunity(); } -static int MaxAge(void) -{ - static int value = -1; - static int gamecookie = -1; - if (value < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - value = get_param_int(global.parameters, "MaxAge", 0); - } - return value; -} - static int ally_flag(const char *s, int help_mask) { if ((help_mask & HELP_MONEY) && strcmp(s, "money") == 0) @@ -2069,100 +2057,6 @@ char *_strdup(const char *s) } #endif -void remove_empty_factions(void) -{ - faction **fp, *f3; - - for (fp = &factions; *fp;) { - faction *f = *fp; - /* monster (0) werden nicht entfernt. alive kann beim readgame - * () auf 0 gesetzt werden, wenn monsters keine einheiten mehr - * haben. */ - if ((f->units == NULL || f->alive == 0) && !is_monsters(f)) { - ursprung *ur = f->ursprung; - while (ur && ur->id != 0) - ur = ur->next; - if (verbosity >= 2) - log_printf(stdout, "\t%s\n", factionname(f)); - - /* Einfach in eine Datei schreiben und später vermailen */ - - if (updatelog) - fprintf(updatelog, "dropout %s\n", itoa36(f->no)); - - for (f3 = factions; f3; f3 = f3->next) { - ally *sf; - group *g; - ally **sfp = &f3->allies; - while (*sfp) { - sf = *sfp; - if (sf->faction == f || sf->faction == NULL) { - *sfp = sf->next; - free(sf); - } - else - sfp = &(*sfp)->next; - } - for (g = f3->groups; g; g = g->next) { - sfp = &g->allies; - while (*sfp) { - sf = *sfp; - if (sf->faction == f || sf->faction == NULL) { - *sfp = sf->next; - free(sf); - } - else - sfp = &(*sfp)->next; - } - } - } - - *fp = f->next; - funhash(f); - free_faction(f); - free(f); - } - else - fp = &(*fp)->next; - } -} - -void remove_empty_units_in_region(region * r) -{ - unit **up = &r->units; - - while (*up) { - unit *u = *up; - - if (u->number) { - faction *f = u->faction; - if (f == NULL || !f->alive) { - set_number(u, 0); - } - if (MaxAge() > 0) { - if ((!fval(f, FFL_NOTIMEOUT) && f->age > MaxAge())) { - set_number(u, 0); - } - } - } - if ((u->number == 0 && u_race(u) != get_race(RC_SPELL)) || (u->age <= 0 - && u_race(u) == get_race(RC_SPELL))) { - remove_unit(up, u); - } - if (*up == u) - up = &u->next; - } -} - -void remove_empty_units(void) -{ - region *r; - - for (r = regions; r; r = r->next) { - remove_empty_units_in_region(r); - } -} - bool faction_id_is_unused(int id) { return findfaction(id) == NULL; @@ -2715,20 +2609,12 @@ int entertainmoney(const region * r) int rule_give(void) { - static int value = -1; - if (value < 0) { - value = get_param_int(global.parameters, "rules.give", GIVE_DEFAULT); - } - return value; + return get_param_int(global.parameters, "rules.give", GIVE_DEFAULT); } int markets_module(void) { - static int value = -1; - if (value < 0) { - value = get_param_int(global.parameters, "modules.markets", 0); - } - return value; + return get_param_int(global.parameters, "modules.markets", 0); } /** releases all memory associated with the game state. diff --git a/src/kernel/config.h b/src/kernel/config.h index 06474dd65..5f3513a72 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -107,10 +107,6 @@ extern "C" { #define i2b(i) ((bool)((i)?(true):(false))) - void remove_empty_units_in_region(struct region *r); - void remove_empty_units(void); - void remove_empty_factions(void); - typedef struct strlist { struct strlist *next; char *s; diff --git a/src/kernel/faction.c b/src/kernel/faction.c index b46848e3c..d3e3bc014 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -601,3 +601,57 @@ int skill_limit(faction * f, skill_t sk) return m; } +void remove_empty_factions(void) +{ + faction **fp, *f3; + + for (fp = &factions; *fp;) { + faction *f = *fp; + /* monster (0) werden nicht entfernt. alive kann beim readgame + * () auf 0 gesetzt werden, wenn monsters keine einheiten mehr + * haben. */ + if ((f->units == NULL || f->alive == 0) && !is_monsters(f)) { + ursprung *ur = f->ursprung; + while (ur && ur->id != 0) + ur = ur->next; + if (verbosity >= 2) + log_printf(stdout, "\t%s\n", factionname(f)); + + /* Einfach in eine Datei schreiben und später vermailen */ + + for (f3 = factions; f3; f3 = f3->next) { + ally *sf; + group *g; + ally **sfp = &f3->allies; + while (*sfp) { + sf = *sfp; + if (sf->faction == f || sf->faction == NULL) { + *sfp = sf->next; + free(sf); + } + else + sfp = &(*sfp)->next; + } + for (g = f3->groups; g; g = g->next) { + sfp = &g->allies; + while (*sfp) { + sf = *sfp; + if (sf->faction == f || sf->faction == NULL) { + *sfp = sf->next; + free(sf); + } + else + sfp = &(*sfp)->next; + } + } + } + + *fp = f->next; + funhash(f); + free_faction(f); + free(f); + } + else + fp = &(*fp)->next; + } +} diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 52f3c271a..f1db53b87 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #define H_KRNL_FACTION #include "skill.h" +#include "types.h" #ifdef __cplusplus extern "C" { @@ -136,6 +137,7 @@ void destroyfaction(faction * f); extern void renumber_faction(faction * f, int no); void free_faction(struct faction *f); + void remove_empty_factions(void); #ifdef SMART_INTERVALS extern void update_interval(struct faction *f, struct region *r); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 09c821b70..dca02aabf 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1786,3 +1786,50 @@ int effskill(const unit * u, skill_t sk) return eff_skill(u, sk, u->region); } +static int MaxAge(void) +{ + static int value = -1; + static int gamecookie = -1; + if (value < 0 || gamecookie != global.cookie) { + gamecookie = global.cookie; + value = get_param_int(global.parameters, "MaxAge", 0); + } + return value; +} + +void remove_empty_units_in_region(region * r) +{ + unit **up = &r->units; + int max_age = MaxAge(); + + while (*up) { + unit *u = *up; + + if (u->number) { + faction *f = u->faction; + if (f == NULL || !f->alive) { + set_number(u, 0); + } + if (max_age > 0) { + if ((!fval(f, FFL_NOTIMEOUT) && f->age > max_age)) { + set_number(u, 0); + } + } + } + if ((u->number == 0 && u_race(u) != get_race(RC_SPELL)) || (u->age <= 0 + && u_race(u) == get_race(RC_SPELL))) { + remove_unit(up, u); + } + if (*up == u) + up = &u->next; + } +} + +void remove_empty_units(void) +{ + region *r; + + for (r = regions; r; r = r->next) { + remove_empty_units_in_region(r); + } +} diff --git a/src/kernel/unit.h b/src/kernel/unit.h index de90db3b2..a3db732dd 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -238,6 +238,8 @@ extern "C" { struct spellbook * unit_get_spellbook(const struct unit * u); void unit_add_spell(struct unit * u, struct sc_mage * m, struct spell * sp, int level); + void remove_empty_units_in_region(struct region * r); + void remove_empty_units(void); extern struct attrib_type at_creator; #ifdef __cplusplus diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 6f3618e73..3a341cce2 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -11,6 +11,42 @@ #include #include +static void test_remove_empty_units(CuTest *tc) { + unit *u; + int uid; + + test_cleanup(); + test_create_world(); + + u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); + uid = u->no; + remove_empty_units(); + CuAssertPtrNotNull(tc, findunit(uid)); + u->number = 0; + remove_empty_units(); + CuAssertPtrEquals(tc, 0, findunit(uid)); + test_cleanup(); +} + +static void test_remove_empty_units_in_region(CuTest *tc) { + unit *u; + int uid; + + test_cleanup(); + test_create_world(); + + u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); + uid = u->no; + remove_empty_units_in_region(u->region); + CuAssertPtrNotNull(tc, findunit(uid)); + u->number = 0; + remove_empty_units_in_region(u->region); + CuAssertPtrEquals(tc, 0, findunit(uid)); + CuAssertPtrEquals(tc, 0, u->region); + CuAssertPtrEquals(tc, 0, u->faction); + test_cleanup(); +} + static void test_scale_number(CuTest *tc) { unit *u; const struct potion_type *ptype; @@ -37,5 +73,7 @@ CuSuite *get_unit_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_scale_number); + SUITE_ADD_TEST(suite, test_remove_empty_units); + SUITE_ADD_TEST(suite, test_remove_empty_units_in_region); return suite; }