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

@ -1972,14 +1972,16 @@ static void buy(unit * u, request ** buyorders, struct order *ord)
} }
} 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) { if (b == NULL) {
cmistake(u, ord, 119, MSG_COMMERCE); cmistake(u, ord, 119, MSG_COMMERCE);
return; return;
@ -2276,15 +2278,14 @@ static bool sell(unit * u, request ** sellorders, struct order *ord)
} }
} 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) { }
if (!b) {
cmistake(u, ord, 119, MSG_COMMERCE); cmistake(u, ord, 119, MSG_COMMERCE);
return false; return false;
} }

View file

@ -278,31 +278,22 @@ 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)
bt_tunnel = bt_find("tunnel");
assert(bt_tunnel);
/* wenn kein Tunnel existiert */ /* wenn kein Tunnel existiert */
if (!buildingtype_exists(r, bt_tunnel, true)) { if (!bt_tunnel || !buildingtype_exists(r, bt_tunnel, true)) {
cmistake(u, u->thisorder, 131, MSG_PRODUCE); cmistake(u, u->thisorder, 131, MSG_PRODUCE);
return; return;
} }

View file

@ -203,28 +203,25 @@ 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) { s = btype->_name;
init_generic = true; if (btype->name) {
bt_generic = bt_find("generic"); s = btype->name(btype, b, bsize);
} }
if (b && b->attribs) {
const struct building_type *bt_generic = bt_find("generic");
if (btype == bt_generic) { if (btype == bt_generic) {
const attrib *a = a_find(b->attribs, &at_building_generic_type); const attrib *a = a_find(b->attribs, &at_building_generic_type);
if (a) if (a) {
s = (const char *)a->data.v; s = (const char *)a->data.v;
} }
}
if (btype->name) }
s = btype->name(btype, b, bsize);
if (s == NULL)
s = btype->_name;
return s; return s;
} }
@ -527,22 +524,17 @@ 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;
if (!init) {
init = true;
bt_caravan = bt_find("caravan");
bt_dam = bt_find("dam");
bt_tunnel = bt_find("tunnel");
}
assert(bfindhash(b->no)); assert(bfindhash(b->no));
bt_caravan = bt_find("caravan");
bt_dam = bt_find("dam");
bt_tunnel = bt_find("tunnel");
handle_event(b->attribs, "destroy", b); handle_event(b->attribs, "destroy", b);
for (u = b->region->units; u; u = u->next) { for (u = b->region->units; u; u = u->next) {
if (u->building == b) if (u->building == b) leave(u, true);
leave(u, true);
} }
b->size = 0; b->size = 0;

View file

@ -1075,17 +1075,8 @@ 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) {
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; region *r = lh->region;
int d = (int)log10(lh->size) + 1; int d = (int)log10(lh->size) + 1;
int x; int x;
@ -1102,9 +1093,7 @@ void update_lighthouse(building * lh)
int px = r->x + x, py = r->y + y; int px = r->x + x, py = r->y + y;
pnormalize(&px, &py, rplane(r)); pnormalize(&px, &py, rplane(r));
r2 = findregion(px, py); r2 = findregion(px, py);
if (r2 == NULL) if (!r2 || !fval(r2->terrain, SEA_REGION))
continue;
if (!fval(r2->terrain, SEA_REGION))
continue; continue;
if (distance(r, r2) > d) if (distance(r, r2) > d)
continue; continue;
@ -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

@ -629,9 +629,7 @@ 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)) {
@ -639,14 +637,15 @@ int check_ship_allowed(struct ship *sh, const region * r)
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)) { if (is_freezing(u)) {
unit *captain = ship_owner(sh); unit *captain = ship_owner(sh);
if (captain) { if (captain) {
ADDMSG(&captain->faction->msgs, msg_message("detectforbidden", ADDMSG(&captain->faction->msgs,
"unit region", u, r)); msg_message("detectforbidden", "unit region", u, r));
} }
return SA_NO_INSECT; return SA_NO_INSECT;

View file

@ -341,8 +341,7 @@ 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);
@ -350,10 +349,7 @@ report_building(const struct building *b, const char **name,
if (illusion) { if (illusion) {
*illusion = NULL; *illusion = NULL;
if (!init) {
bt_illusion = bt_find("illusioncastle"); bt_illusion = bt_find("illusioncastle");
init = 1;
}
if (bt_illusion && b->type == bt_illusion) { if (bt_illusion && b->type == bt_illusion) {
const attrib *a = a_findc(b->attribs, &at_icastle); const attrib *a = a_findc(b->attribs, &at_icastle);
if (a != NULL) { if (a != NULL) {
@ -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) {
init = true;
itemcloak_ct = ct_find("itemcloak"); itemcloak_ct = ct_find("itemcloak");
} if (itemcloak_ct) {
if (itemcloak_ct != NULL) {
itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct)); itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct));
} }
@ -1504,13 +1496,10 @@ 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();
} }

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,9 +32,7 @@ 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)
btype = bt_find("market");
if (!btype) if (!btype)
return 0; return 0;
for (b = r->buildings; n < size && b; b = b->next) { for (b = r->buildings; n < size && b; b = b->next) {

View file

@ -4402,11 +4402,9 @@ int sp_icastle(castorder * co)
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");
if (bt_illusion == NULL) {
return 0; return 0;
} }

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