remove static variables optimizations, they create global state that is bad for testing

This commit is contained in:
Enno Rehling 2014-06-17 23:10:55 -07:00
parent 59b0f0f582
commit 3625ba6a95
11 changed files with 208 additions and 242 deletions

View file

@ -1971,19 +1971,21 @@ static void buy(unit * u, request ** buyorders, struct order *ord)
return; return;
} }
} else { } else {
/* ...oder in der Region muß es eine Burg geben. */ /* ...oder in der Region muß es eine Burg geben. */
building *b; building *b = 0;
static const struct building_type *bt_castle; if (r->buildings) {
if (!bt_castle) const struct building_type *bt_castle = bt_find("castle");
bt_castle = bt_find("castle");
for (b = r->buildings; b; b = b->next) { for (b = r->buildings; b; b = b->next) {
if (b->type == bt_castle && b->size >= 2) if (b->type == bt_castle && b->size >= 2) {
break; break;
} }
if (b == NULL) { }
cmistake(u, ord, 119, MSG_COMMERCE); }
return; if (b == NULL) {
} cmistake(u, ord, 119, MSG_COMMERCE);
return;
}
} }
/* Ein Händler kann nur 10 Güter pro Talentpunkt handeln. */ /* 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; return false;
} }
} else { } else {
/* ...oder in der Region muß es eine Burg geben. */ /* ...oder in der Region muß es eine Burg geben. */
building *b; building *b = 0;
static const struct building_type *bt_castle; if (r->buildings) {
if (!bt_castle) const struct building_type *bt_castle = bt_find("castle");
bt_castle = bt_find("castle"); for (b = r->buildings; b; b = b->next) {
for (b = r->buildings; b; b = b->next) { if (b->type == bt_castle && b->size >= 2) break;
if (b->type == bt_castle && b->size >= 2) }
break; }
} if (!b) {
if (b == NULL) { cmistake(u, ord, 119, MSG_COMMERCE);
cmistake(u, ord, 119, MSG_COMMERCE); return false;
return false; }
}
} }
/* Ein Händler kann nur 10 Güter pro Talentpunkt verkaufen. */ /* Ein Händler kann nur 10 Güter pro Talentpunkt verkaufen. */

View file

@ -278,34 +278,25 @@ void build_road(region * r, unit * u, int size, direction_t d)
if (r->terrain == newterrain(T_SWAMP)) { if (r->terrain == newterrain(T_SWAMP)) {
/* wenn kein Damm existiert */ /* wenn kein Damm existiert */
static const struct building_type *bt_dam; const struct building_type *bt_dam = bt_find("dam");
if (!bt_dam) if (!bt_dam || !buildingtype_exists(r, bt_dam, true)) {
bt_dam = bt_find("dam");
assert(bt_dam);
if (!buildingtype_exists(r, bt_dam, true)) {
cmistake(u, u->thisorder, 132, MSG_PRODUCE); cmistake(u, u->thisorder, 132, MSG_PRODUCE);
return; return;
} }
} else if (r->terrain == newterrain(T_DESERT)) { } else if (r->terrain == newterrain(T_DESERT)) {
static const struct building_type *bt_caravan; const struct building_type *bt_caravan = bt_find("caravan");
if (!bt_caravan)
bt_caravan = bt_find("caravan");
assert(bt_caravan);
/* wenn keine Karawanserei existiert */ /* 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); cmistake(u, u->thisorder, 133, MSG_PRODUCE);
return; return;
} }
} else if (r->terrain == newterrain(T_GLACIER)) { } else if (r->terrain == newterrain(T_GLACIER)) {
static const struct building_type *bt_tunnel; const struct building_type *bt_tunnel = bt_find("tunnel");
if (!bt_tunnel) /* wenn kein Tunnel existiert */
bt_tunnel = bt_find("tunnel"); if (!bt_tunnel || !buildingtype_exists(r, bt_tunnel, true)) {
assert(bt_tunnel); cmistake(u, u->thisorder, 131, MSG_PRODUCE);
/* wenn kein Tunnel existiert */ return;
if (!buildingtype_exists(r, bt_tunnel, true)) { }
cmistake(u, u->thisorder, 131, MSG_PRODUCE);
return;
}
} }
/* left kann man noch bauen */ /* left kann man noch bauen */

View file

@ -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 /* 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). * name if it depends on the size (as for Eressea castles).
*/ */
const char *buildingtype(const building_type * btype, const building * b, const char *buildingtype(const building_type * btype, const building * b, int bsize)
int bsize)
{ {
const char *s = NULL; const char *s;
static bool init_generic = false; assert(btype);
static const struct building_type *bt_generic;
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; 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 #define BMAXHASH 7919
@ -526,50 +523,45 @@ static building *deleted_buildings;
*/ */
void remove_building(building ** blist, building * b) void remove_building(building ** blist, building * b)
{ {
unit *u; unit *u;
static const struct building_type *bt_caravan, *bt_dam, *bt_tunnel; const struct building_type *bt_caravan, *bt_dam, *bt_tunnel;
static bool init = false;
assert(bfindhash(b->no));
if (!init) {
init = true;
bt_caravan = bt_find("caravan"); bt_caravan = bt_find("caravan");
bt_dam = bt_find("dam"); bt_dam = bt_find("dam");
bt_tunnel = bt_find("tunnel"); 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); b->size = 0;
for (u = b->region->units; u; u = u->next) { update_lighthouse(b);
if (u->building == b) bunhash(b);
leave(u, true);
}
b->size = 0;
update_lighthouse(b);
bunhash(b);
/* Falls Karawanserei, Damm oder Tunnel einstürzen, wird die schon /* Falls Karawanserei, Damm oder Tunnel einstürzen, wird die schon
* gebaute Straße zur Hälfte vernichtet */ * gebaute Straße zur Hälfte vernichtet */
if (b->type == bt_caravan || b->type == bt_dam || b->type == bt_tunnel) { if (b->type == bt_caravan || b->type == bt_dam || b->type == bt_tunnel) {
region *r = b->region; region *r = b->region;
int d; int d;
for (d = 0; d != MAXDIRECTIONS; ++d) { for (d = 0; d != MAXDIRECTIONS; ++d) {
direction_t dir = (direction_t)d; direction_t dir = (direction_t)d;
if (rroad(r, dir) > 0) { if (rroad(r, dir) > 0) {
rsetroad(r, dir, rroad(r, dir) / 2); rsetroad(r, dir, rroad(r, dir) / 2);
} }
}
} }
}
/* Stattdessen nur aus Liste entfernen, aber im Speicher halten. */
/* Stattdessen nur aus Liste entfernen, aber im Speicher halten. */ while (*blist && *blist != b) {
while (*blist && *blist != b) { blist = &(*blist)->next;
blist = &(*blist)->next; }
} *blist = b->next;
*blist = b->next; b->region = NULL;
b->region = NULL; b->next = deleted_buildings;
b->next = deleted_buildings; deleted_buildings = b;
deleted_buildings = b;
} }
void free_building(building * b) void free_building(building * b)

View file

@ -1075,53 +1075,42 @@ static attrib_type at_lighthouse = {
*/ */
void update_lighthouse(building * lh) void update_lighthouse(building * lh)
{ {
static bool init_lighthouse = false; const struct building_type *bt_lighthouse = bt_find("lighthouse");
static const struct building_type *bt_lighthouse = 0; if (bt_lighthouse && lh->type == bt_lighthouse) {
region *r = lh->region;
int d = (int)log10(lh->size) + 1;
int x;
if (!init_lighthouse) { if (lh->size > 0) {
bt_lighthouse = bt_find("lighthouse"); r->flags |= RF_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 (!a) {
a = a_add(&r2->attribs, a_new(&at_lighthouse)); for (x = -d; x <= d; ++x) {
a->data.v = (void *)lh; 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) 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) int cmp_wage(const struct building *b, const building * a)
{ {
static const struct building_type *bt_castle; const struct building_type *bt_castle = bt_find("castle");
if (!bt_castle)
bt_castle = bt_find("castle");
if (b->type == bt_castle) { if (b->type == bt_castle) {
if (!a) if (!a)
return 1; return 1;

View file

@ -628,46 +628,45 @@ static bool is_freezing(const unit * u)
int check_ship_allowed(struct ship *sh, const region * r) int check_ship_allowed(struct ship *sh, const region * r)
{ {
int c = 0; int c = 0;
static const building_type *bt_harbour = NULL; const building_type *bt_harbour = NULL;
if (bt_harbour == NULL)
bt_harbour = bt_find("harbour"); bt_harbour = bt_find("harbour");
if (sh->region && r_insectstalled(r)) { if (sh->region && r_insectstalled(r)) {
/* insekten dürfen nicht hier rein. haben wir welche? */ /* insekten dürfen nicht hier rein. haben wir welche? */
unit *u; unit *u;
for (u = sh->region->units; u != NULL; u = u->next) { for (u = sh->region->units; u != NULL; u = u->next) {
if (u->ship != sh) if (u->ship != sh) {
continue; continue;
}
if (is_freezing(u)) {
unit *captain = ship_owner(sh); if (is_freezing(u)) {
if (captain) { unit *captain = ship_owner(sh);
ADDMSG(&captain->faction->msgs, msg_message("detectforbidden", if (captain) {
"unit region", u, r)); 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)) {
if (bt_harbour && buildingtype_exists(r, bt_harbour, true)) { return SA_HARBOUR;
return SA_HARBOUR; }
} if (fval(r->terrain, SEA_REGION)) {
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) {
return SA_COAST; return SA_COAST;
}
} }
} if (sh->type->coasts) {
return SA_NO_COAST; 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) static bool flying_ship(const ship * sh)

View file

@ -341,27 +341,23 @@ void
report_building(const struct building *b, const char **name, report_building(const struct building *b, const char **name,
const char **illusion) const char **illusion)
{ {
static int init; const struct building_type *bt_illusion;
static const struct building_type *bt_illusion;
if (name) { if (name) {
*name = buildingtype(b->type, b, b->size); *name = buildingtype(b->type, b, b->size);
}
if (illusion) {
*illusion = NULL;
if (!init) {
bt_illusion = bt_find("illusioncastle");
init = 1;
} }
if (bt_illusion && b->type == bt_illusion) { if (illusion) {
const attrib *a = a_findc(b->attribs, &at_icastle); *illusion = NULL;
if (a != NULL) {
icastle_data *icastle = (icastle_data *) a->data.v; bt_illusion = bt_find("illusioncastle");
*illusion = buildingtype(icastle->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);
}
}
} }
}
} }
int int
@ -463,16 +459,12 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
faction *fv = visible_faction(f, u); faction *fv = visible_faction(f, u);
char *bufp = buf; char *bufp = buf;
bool itemcloak = false; bool itemcloak = false;
static const curse_type *itemcloak_ct = 0; const curse_type *itemcloak_ct = 0;
static bool init = false;
int bytes; int bytes;
item result[MAX_INVENTORY]; item result[MAX_INVENTORY];
if (!init) { itemcloak_ct = ct_find("itemcloak");
init = true; if (itemcloak_ct) {
itemcloak_ct = ct_find("itemcloak");
}
if (itemcloak_ct != NULL) {
itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct)); itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct));
} }
@ -1504,14 +1496,11 @@ static void prepare_reports(void)
{ {
region *r; region *r;
faction *f; faction *f;
static const struct building_type *bt_lighthouse = NULL; const struct building_type *bt_lighthouse = bt_find("lighthouse");
if (bt_lighthouse == NULL) {
bt_lighthouse = bt_find("lighthouse");
}
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
if (f->seen) if (f->seen) seen_done(f->seen);
seen_done(f->seen); f->seen = seen_init();
f->seen = seen_init();
} }
for (r = regions; r; r = r->next) { for (r = regions; r; r = r->next) {

View file

@ -3256,14 +3256,11 @@ int renumber_cmd(unit * u, order * ord)
static building *age_building(building * b) static building *age_building(building * b)
{ {
static bool init = false; const struct building_type *bt_blessed;
static const building_type *bt_blessed; const struct curse_type *ct_astralblock;
static const curse_type *ct_astralblock;
if (!init) {
init = true;
bt_blessed = bt_find("blessedstonecircle"); bt_blessed = bt_find("blessedstonecircle");
ct_astralblock = ct_find("astralblock"); ct_astralblock = ct_find("astralblock");
}
/* blesses stone circles create an astral protection in the astral region /* blesses stone circles create an astral protection in the astral region
* above the shield, which prevents chaos suction and other spells. * above the shield, which prevents chaos suction and other spells.

View file

@ -32,11 +32,9 @@ static unsigned int get_markets(region * r, unit ** results, size_t size)
{ {
unsigned int n = 0; unsigned int n = 0;
building *b; building *b;
static const building_type *btype; const building_type *btype = bt_find("market");
if (!btype) if (!btype)
btype = bt_find("market"); return 0;
if (!btype)
return 0;
for (b = r->buildings; n < size && b; b = b->next) { for (b = r->buildings; n < size && b; b = b->next) {
if (b->type == btype && (b->flags & BLD_WORKING) if (b->type == btype && (b->flags & BLD_WORKING)
&& b->size >= b->type->maxsize) { && b->size >= b->type->maxsize) {

View file

@ -4391,24 +4391,22 @@ int sp_puttorest(castorder * co)
int sp_icastle(castorder * co) int sp_icastle(castorder * co)
{ {
building *b; building *b;
const building_type *type; const building_type *type;
attrib *a; attrib *a;
region *r = co_get_region(co); region *r = co_get_region(co);
unit *mage = co->magician.u; unit *mage = co->magician.u;
int cast_level = co->level; int cast_level = co->level;
float power = co->force; float power = co->force;
spellparameter *pa = co->par; spellparameter *pa = co->par;
icastle_data *data; icastle_data *data;
const char *bname; const char *bname;
message *msg; message *msg;
static const building_type *bt_illusion; const building_type *bt_illusion = bt_find("illusioncastle");
if (bt_illusion == NULL) if (!bt_illusion) {
bt_illusion = bt_find("illusioncastle"); return 0;
if (bt_illusion == NULL) { }
return 0;
}
if ((type = if ((type =
findbuildingtype(pa->param[0]->data.xs, mage->faction->locale)) == NULL) { findbuildingtype(pa->param[0]->data.xs, mage->faction->locale)) == NULL) {

View file

@ -1,8 +1,8 @@
-- new tests 2014-06-11 -- new tests 2014-06-11
--require "tests.settings" require "tests.settings"
--require "tests.config" require "tests.config"
--require "tests.locale" require "tests.locale"
--require "tests.regions" require "tests.regions"
require "tests.ships" require "tests.ships"

View file

@ -85,4 +85,18 @@ function test_sail_to_forbidden_shore()
assert_equal(ocean, u.region) assert_equal(ocean, u.region)
end 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