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).
This commit is contained in:
Enno Rehling 2014-10-16 07:34:09 +02:00
parent 8d6a25d15a
commit 7df47fd25c
9 changed files with 147 additions and 121 deletions

View File

@ -4,6 +4,7 @@
#include "json.h"
#include <kernel/faction.h>
#include <kernel/types.h>
#include <kernel/config.h>
#include <kernel/save.h>

View File

@ -57,9 +57,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/bsdstring.h>
#include <util/event.h>
#include <util/goodies.h>
#include <util/lists.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/parser.h>
#include <util/rng.h>

View File

@ -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.

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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

View File

@ -11,6 +11,42 @@
#include <stdlib.h>
#include <assert.h>
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;
}