From 3625ba6a95bd830eee6117b4c4ebfcc26c26d35f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 17 Jun 2014 23:10:55 -0700 Subject: [PATCH] remove static variables optimizations, they create global state that is bad for testing --- src/economy.c | 53 +++++++++++----------- src/kernel/build.c | 29 +++++------- src/kernel/building.c | 100 +++++++++++++++++++----------------------- src/kernel/config.c | 79 ++++++++++++++------------------- src/kernel/move.c | 67 ++++++++++++++-------------- src/kernel/reports.c | 53 +++++++++------------- src/laws.c | 9 ++-- src/market.c | 6 +-- src/spells/spells.c | 32 +++++++------- tests/init.lua | 8 ++-- tests/ships.lua | 14 ++++++ 11 files changed, 208 insertions(+), 242 deletions(-) diff --git a/src/economy.c b/src/economy.c index fd1f87e41..241788c92 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1971,19 +1971,21 @@ static void buy(unit * u, request ** buyorders, struct order *ord) return; } } else { - /* ...oder in der Region muß es eine Burg geben. */ - building *b; - static const struct building_type *bt_castle; - if (!bt_castle) - bt_castle = bt_find("castle"); - for (b = r->buildings; b; b = b->next) { - if (b->type == bt_castle && b->size >= 2) - break; - } - if (b == NULL) { - cmistake(u, ord, 119, MSG_COMMERCE); - return; - } + /* ...oder in der Region muß es eine Burg geben. */ + building *b = 0; + if (r->buildings) { + const struct building_type *bt_castle = bt_find("castle"); + + for (b = r->buildings; b; b = b->next) { + if (b->type == bt_castle && b->size >= 2) { + break; + } + } + } + if (b == NULL) { + cmistake(u, ord, 119, MSG_COMMERCE); + return; + } } /* Ein Händler kann nur 10 Güter pro Talentpunkt handeln. */ @@ -2275,19 +2277,18 @@ static bool sell(unit * u, request ** sellorders, struct order *ord) return false; } } else { - /* ...oder in der Region muß es eine Burg geben. */ - building *b; - static const struct building_type *bt_castle; - if (!bt_castle) - bt_castle = bt_find("castle"); - for (b = r->buildings; b; b = b->next) { - if (b->type == bt_castle && b->size >= 2) - break; - } - if (b == NULL) { - cmistake(u, ord, 119, MSG_COMMERCE); - return false; - } + /* ...oder in der Region muß es eine Burg geben. */ + building *b = 0; + if (r->buildings) { + const struct building_type *bt_castle = bt_find("castle"); + for (b = r->buildings; b; b = b->next) { + if (b->type == bt_castle && b->size >= 2) break; + } + } + if (!b) { + cmistake(u, ord, 119, MSG_COMMERCE); + return false; + } } /* Ein Händler kann nur 10 Güter pro Talentpunkt verkaufen. */ diff --git a/src/kernel/build.c b/src/kernel/build.c index c1afdc02c..43af9fbad 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -278,34 +278,25 @@ void build_road(region * r, unit * u, int size, direction_t d) if (r->terrain == newterrain(T_SWAMP)) { /* wenn kein Damm existiert */ - static const struct building_type *bt_dam; - if (!bt_dam) - bt_dam = bt_find("dam"); - assert(bt_dam); - if (!buildingtype_exists(r, bt_dam, true)) { + const struct building_type *bt_dam = bt_find("dam"); + if (!bt_dam || !buildingtype_exists(r, bt_dam, true)) { cmistake(u, u->thisorder, 132, MSG_PRODUCE); return; } } else if (r->terrain == newterrain(T_DESERT)) { - static const struct building_type *bt_caravan; - if (!bt_caravan) - bt_caravan = bt_find("caravan"); - assert(bt_caravan); + const struct building_type *bt_caravan = bt_find("caravan"); /* wenn keine Karawanserei existiert */ - if (!buildingtype_exists(r, bt_caravan, true)) { + if (!bt_caravan || !buildingtype_exists(r, bt_caravan, true)) { cmistake(u, u->thisorder, 133, MSG_PRODUCE); return; } } else if (r->terrain == newterrain(T_GLACIER)) { - static const struct building_type *bt_tunnel; - if (!bt_tunnel) - bt_tunnel = bt_find("tunnel"); - assert(bt_tunnel); - /* wenn kein Tunnel existiert */ - if (!buildingtype_exists(r, bt_tunnel, true)) { - cmistake(u, u->thisorder, 131, MSG_PRODUCE); - return; - } + const struct building_type *bt_tunnel = bt_find("tunnel"); + /* wenn kein Tunnel existiert */ + if (!bt_tunnel || !buildingtype_exists(r, bt_tunnel, true)) { + cmistake(u, u->thisorder, 131, MSG_PRODUCE); + return; + } } /* left kann man noch bauen */ diff --git a/src/kernel/building.c b/src/kernel/building.c index 8188cbb7a..132f46349 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -203,29 +203,26 @@ attrib_type at_building_generic_type = { /* Returns the (internal) name for a building of given size and type. Especially, returns the correct * name if it depends on the size (as for Eressea castles). */ -const char *buildingtype(const building_type * btype, const building * b, - int bsize) +const char *buildingtype(const building_type * btype, const building * b, int bsize) { - const char *s = NULL; - static bool init_generic = false; - static const struct building_type *bt_generic; + const char *s; + assert(btype); - if (!init_generic) { - init_generic = true; - bt_generic = bt_find("generic"); - } - - if (btype == bt_generic) { - const attrib *a = a_find(b->attribs, &at_building_generic_type); - if (a) - s = (const char *)a->data.v; - } - - if (btype->name) - s = btype->name(btype, b, bsize); - if (s == NULL) s = btype->_name; - return s; + if (btype->name) { + s = btype->name(btype, b, bsize); + } + if (b && b->attribs) { + const struct building_type *bt_generic = bt_find("generic"); + + if (btype == bt_generic) { + const attrib *a = a_find(b->attribs, &at_building_generic_type); + if (a) { + s = (const char *)a->data.v; + } + } + } + return s; } #define BMAXHASH 7919 @@ -526,50 +523,45 @@ static building *deleted_buildings; */ void remove_building(building ** blist, building * b) { - unit *u; - static const struct building_type *bt_caravan, *bt_dam, *bt_tunnel; - static bool init = false; + unit *u; + const struct building_type *bt_caravan, *bt_dam, *bt_tunnel; + + assert(bfindhash(b->no)); - if (!init) { - init = true; bt_caravan = bt_find("caravan"); bt_dam = bt_find("dam"); bt_tunnel = bt_find("tunnel"); - } - assert(bfindhash(b->no)); + handle_event(b->attribs, "destroy", b); + for (u = b->region->units; u; u = u->next) { + if (u->building == b) leave(u, true); + } - handle_event(b->attribs, "destroy", b); - for (u = b->region->units; u; u = u->next) { - if (u->building == b) - leave(u, true); - } - - b->size = 0; - update_lighthouse(b); - bunhash(b); + b->size = 0; + update_lighthouse(b); + bunhash(b); /* Falls Karawanserei, Damm oder Tunnel einstürzen, wird die schon * gebaute Straße zur Hälfte vernichtet */ - if (b->type == bt_caravan || b->type == bt_dam || b->type == bt_tunnel) { - region *r = b->region; - int d; - for (d = 0; d != MAXDIRECTIONS; ++d) { - direction_t dir = (direction_t)d; - if (rroad(r, dir) > 0) { - rsetroad(r, dir, rroad(r, dir) / 2); - } + if (b->type == bt_caravan || b->type == bt_dam || b->type == bt_tunnel) { + region *r = b->region; + int d; + for (d = 0; d != MAXDIRECTIONS; ++d) { + direction_t dir = (direction_t)d; + if (rroad(r, dir) > 0) { + rsetroad(r, dir, rroad(r, dir) / 2); + } + } } - } - - /* Stattdessen nur aus Liste entfernen, aber im Speicher halten. */ - while (*blist && *blist != b) { - blist = &(*blist)->next; - } - *blist = b->next; - b->region = NULL; - b->next = deleted_buildings; - deleted_buildings = b; + + /* Stattdessen nur aus Liste entfernen, aber im Speicher halten. */ + while (*blist && *blist != b) { + blist = &(*blist)->next; + } + *blist = b->next; + b->region = NULL; + b->next = deleted_buildings; + deleted_buildings = b; } void free_building(building * b) diff --git a/src/kernel/config.c b/src/kernel/config.c index 85e83bf6e..8df261479 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1075,53 +1075,42 @@ static attrib_type at_lighthouse = { */ void update_lighthouse(building * lh) { - static bool init_lighthouse = false; - static const struct building_type *bt_lighthouse = 0; + const struct building_type *bt_lighthouse = bt_find("lighthouse"); + if (bt_lighthouse && lh->type == bt_lighthouse) { + region *r = lh->region; + int d = (int)log10(lh->size) + 1; + int x; - if (!init_lighthouse) { - bt_lighthouse = bt_find("lighthouse"); - if (bt_lighthouse == NULL) - return; - init_lighthouse = true; - } - - if (lh->type == bt_lighthouse) { - region *r = lh->region; - int d = (int)log10(lh->size) + 1; - int x; - - if (lh->size > 0) { - r->flags |= RF_LIGHTHOUSE; - } - - for (x = -d; x <= d; ++x) { - int y; - for (y = -d; y <= d; ++y) { - attrib *a; - region *r2; - int px = r->x + x, py = r->y + y; - pnormalize(&px, &py, rplane(r)); - r2 = findregion(px, py); - if (r2 == NULL) - continue; - if (!fval(r2->terrain, SEA_REGION)) - continue; - if (distance(r, r2) > d) - continue; - a = a_find(r2->attribs, &at_lighthouse); - while (a && a->type == &at_lighthouse) { - building *b = (building *) a->data.v; - if (b == lh) - break; - a = a->next; + if (lh->size > 0) { + r->flags |= RF_LIGHTHOUSE; } - if (!a) { - a = a_add(&r2->attribs, a_new(&at_lighthouse)); - a->data.v = (void *)lh; + + for (x = -d; x <= d; ++x) { + int y; + for (y = -d; y <= d; ++y) { + attrib *a; + region *r2; + int px = r->x + x, py = r->y + y; + pnormalize(&px, &py, rplane(r)); + r2 = findregion(px, py); + if (!r2 || !fval(r2->terrain, SEA_REGION)) + continue; + if (distance(r, r2) > d) + continue; + a = a_find(r2->attribs, &at_lighthouse); + while (a && a->type == &at_lighthouse) { + building *b = (building *) a->data.v; + if (b == lh) + break; + a = a->next; + } + if (!a) { + a = a_add(&r2->attribs, a_new(&at_lighthouse)); + a->data.v = (void *)lh; + } + } } - } } - } } int count_faction(const faction * f, int flags) @@ -2418,9 +2407,7 @@ static const int wagetable[7][4] = { int cmp_wage(const struct building *b, const building * a) { - static const struct building_type *bt_castle; - if (!bt_castle) - bt_castle = bt_find("castle"); + const struct building_type *bt_castle = bt_find("castle"); if (b->type == bt_castle) { if (!a) return 1; diff --git a/src/kernel/move.c b/src/kernel/move.c index 8c28ac5bb..b6c8ef1d7 100644 --- a/src/kernel/move.c +++ b/src/kernel/move.c @@ -628,46 +628,45 @@ static bool is_freezing(const unit * u) int check_ship_allowed(struct ship *sh, const region * r) { - int c = 0; - static const building_type *bt_harbour = NULL; - - if (bt_harbour == NULL) + int c = 0; + const building_type *bt_harbour = NULL; bt_harbour = bt_find("harbour"); - if (sh->region && r_insectstalled(r)) { - /* insekten dürfen nicht hier rein. haben wir welche? */ - unit *u; - - for (u = sh->region->units; u != NULL; u = u->next) { - if (u->ship != sh) - continue; - - if (is_freezing(u)) { - unit *captain = ship_owner(sh); - if (captain) { - ADDMSG(&captain->faction->msgs, msg_message("detectforbidden", - "unit region", u, r)); + if (sh->region && r_insectstalled(r)) { + /* insekten dürfen nicht hier rein. haben wir welche? */ + unit *u; + + for (u = sh->region->units; u != NULL; u = u->next) { + if (u->ship != sh) { + continue; + } + + if (is_freezing(u)) { + unit *captain = ship_owner(sh); + if (captain) { + ADDMSG(&captain->faction->msgs, + msg_message("detectforbidden", "unit region", u, r)); + } + + return SA_NO_INSECT; + } } - - return SA_NO_INSECT; - } } - } - - if (bt_harbour && buildingtype_exists(r, bt_harbour, true)) { - return SA_HARBOUR; - } - if (fval(r->terrain, SEA_REGION)) { - return SA_COAST; - } - if (sh->type->coasts) { - for (c = 0; sh->type->coasts[c] != NULL; ++c) { - if (sh->type->coasts[c] == r->terrain) { + + if (bt_harbour && buildingtype_exists(r, bt_harbour, true)) { + return SA_HARBOUR; + } + if (fval(r->terrain, SEA_REGION)) { return SA_COAST; - } } - } - return SA_NO_COAST; + if (sh->type->coasts) { + for (c = 0; sh->type->coasts[c] != NULL; ++c) { + if (sh->type->coasts[c] == r->terrain) { + return SA_COAST; + } + } + } + return SA_NO_COAST; } static bool flying_ship(const ship * sh) diff --git a/src/kernel/reports.c b/src/kernel/reports.c index 58d11a0b0..1c2129047 100644 --- a/src/kernel/reports.c +++ b/src/kernel/reports.c @@ -341,27 +341,23 @@ void report_building(const struct building *b, const char **name, const char **illusion) { - static int init; - static const struct building_type *bt_illusion; + const struct building_type *bt_illusion; - if (name) { - *name = buildingtype(b->type, b, b->size); - } - if (illusion) { - *illusion = NULL; - - if (!init) { - bt_illusion = bt_find("illusioncastle"); - init = 1; + if (name) { + *name = buildingtype(b->type, b, b->size); } - if (bt_illusion && b->type == bt_illusion) { - const attrib *a = a_findc(b->attribs, &at_icastle); - if (a != NULL) { - icastle_data *icastle = (icastle_data *) a->data.v; - *illusion = buildingtype(icastle->type, b, b->size); - } + if (illusion) { + *illusion = NULL; + + bt_illusion = bt_find("illusioncastle"); + if (bt_illusion && b->type == bt_illusion) { + const attrib *a = a_findc(b->attribs, &at_icastle); + if (a != NULL) { + icastle_data *icastle = (icastle_data *) a->data.v; + *illusion = buildingtype(icastle->type, b, b->size); + } + } } - } } int @@ -463,16 +459,12 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf, faction *fv = visible_faction(f, u); char *bufp = buf; bool itemcloak = false; - static const curse_type *itemcloak_ct = 0; - static bool init = false; + const curse_type *itemcloak_ct = 0; int bytes; item result[MAX_INVENTORY]; - if (!init) { - init = true; - itemcloak_ct = ct_find("itemcloak"); - } - if (itemcloak_ct != NULL) { + itemcloak_ct = ct_find("itemcloak"); + if (itemcloak_ct) { itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct)); } @@ -1504,14 +1496,11 @@ static void prepare_reports(void) { region *r; faction *f; - static const struct building_type *bt_lighthouse = NULL; - if (bt_lighthouse == NULL) { - bt_lighthouse = bt_find("lighthouse"); - } + const struct building_type *bt_lighthouse = bt_find("lighthouse"); + for (f = factions; f; f = f->next) { - if (f->seen) - seen_done(f->seen); - f->seen = seen_init(); + if (f->seen) seen_done(f->seen); + f->seen = seen_init(); } for (r = regions; r; r = r->next) { diff --git a/src/laws.c b/src/laws.c index 88d3131a4..a3d41d058 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3256,14 +3256,11 @@ int renumber_cmd(unit * u, order * ord) static building *age_building(building * b) { - static bool init = false; - static const building_type *bt_blessed; - static const curse_type *ct_astralblock; - if (!init) { - init = true; + const struct building_type *bt_blessed; + const struct curse_type *ct_astralblock; + bt_blessed = bt_find("blessedstonecircle"); ct_astralblock = ct_find("astralblock"); - } /* blesses stone circles create an astral protection in the astral region * above the shield, which prevents chaos suction and other spells. diff --git a/src/market.c b/src/market.c index d80d8bdad..d860079cb 100644 --- a/src/market.c +++ b/src/market.c @@ -32,11 +32,9 @@ static unsigned int get_markets(region * r, unit ** results, size_t size) { unsigned int n = 0; building *b; - static const building_type *btype; + const building_type *btype = bt_find("market"); if (!btype) - btype = bt_find("market"); - if (!btype) - return 0; + return 0; for (b = r->buildings; n < size && b; b = b->next) { if (b->type == btype && (b->flags & BLD_WORKING) && b->size >= b->type->maxsize) { diff --git a/src/spells/spells.c b/src/spells/spells.c index d67ddf2d4..daa4f7a81 100644 --- a/src/spells/spells.c +++ b/src/spells/spells.c @@ -4391,24 +4391,22 @@ int sp_puttorest(castorder * co) int sp_icastle(castorder * co) { - building *b; - const building_type *type; - attrib *a; - region *r = co_get_region(co); - unit *mage = co->magician.u; - int cast_level = co->level; - float power = co->force; - spellparameter *pa = co->par; - icastle_data *data; - const char *bname; - message *msg; - static const building_type *bt_illusion; + building *b; + const building_type *type; + attrib *a; + region *r = co_get_region(co); + unit *mage = co->magician.u; + int cast_level = co->level; + float power = co->force; + spellparameter *pa = co->par; + icastle_data *data; + const char *bname; + message *msg; + const building_type *bt_illusion = bt_find("illusioncastle"); - if (bt_illusion == NULL) - bt_illusion = bt_find("illusioncastle"); - if (bt_illusion == NULL) { - return 0; - } + if (!bt_illusion) { + return 0; + } if ((type = findbuildingtype(pa->param[0]->data.xs, mage->faction->locale)) == NULL) { diff --git a/tests/init.lua b/tests/init.lua index b2d3ba1b2..e75241ead 100644 --- a/tests/init.lua +++ b/tests/init.lua @@ -1,8 +1,8 @@ -- new tests 2014-06-11 ---require "tests.settings" ---require "tests.config" ---require "tests.locale" ---require "tests.regions" +require "tests.settings" +require "tests.config" +require "tests.locale" +require "tests.regions" require "tests.ships" diff --git a/tests/ships.lua b/tests/ships.lua index 8367d8652..6bc92983f 100644 --- a/tests/ships.lua +++ b/tests/ships.lua @@ -85,4 +85,18 @@ function test_sail_to_forbidden_shore() assert_equal(ocean, u.region) end +function test_sail_into_harbour() + local ocean = region.create(1, 0, "ocean") + local shore = region.create(0, 0, "glacier") + local f = faction.create("noreply@eressea.de", "human", "de") + local u = unit.create(f, ocean, 1) + u.name = "Sailor" + u.ship = ship.create(ocean, "boat") + u:set_skill("sailing", 10) + u:add_order("NACH W") + local b = building.create(shore, "harbour") + assert_not_nil(b) + process_orders() + assert_equal(shore, u.region) +end