is_building_type should be a quicker way to test for a building type than bt_find.

This commit is contained in:
Enno Rehling 2016-08-30 09:13:59 +01:00
parent fdc91c01a0
commit 5bb9a10a46
6 changed files with 23 additions and 21 deletions

View File

@ -158,9 +158,7 @@ const char *buildingtype(const building_type * btype, const building * b, int bs
s = btype->name(btype, b, bsize);
}
if (b && b->attribs) {
const struct building_type *bt_generic = bt_find("generic");
if (btype == bt_generic) {
if (is_building_type(btype, "generic")) {
const attrib *a = a_find(b->attribs, &at_building_generic_type);
if (a) {
s = (const char *)a->data.v;
@ -390,16 +388,9 @@ building *new_building(const struct building_type * btype, region * r,
{
building **bptr = &r->buildings;
building *b = (building *)calloc(1, sizeof(building));
static bool init_lighthouse = false;
static const struct building_type *bt_lighthouse = 0;
const char *bname = 0;
char buffer[32];
if (!init_lighthouse) {
bt_lighthouse = bt_find("lighthouse");
init_lighthouse = true;
}
b->no = newcontainerid();
bhash(b);
@ -409,9 +400,7 @@ building *new_building(const struct building_type * btype, region * r,
bptr = &(*bptr)->next;
*bptr = b;
if (b->type == bt_lighthouse) {
r->flags |= RF_LIGHTHOUSE;
}
update_lighthouse(b);
if (b->type->name) {
bname = LOC(lang, buildingtype(btype, b, 0));
}
@ -695,3 +684,8 @@ bool in_safe_building(unit *u1, unit *u2) {
}
return false;
}
bool is_building_type(const struct building_type *btype, const char *name) {
assert(btype);
return name && strcmp(btype->_name, name)==0;
}

View File

@ -163,6 +163,7 @@ extern "C" {
bool buildingtype_exists(const struct region *r,
const struct building_type *bt, bool working);
bool building_is_active(const struct building *b);
bool is_building_type(const struct building_type *btype, const char *name);
struct building *active_building(const struct unit *u, const struct building_type *btype);
extern const char *buildingname(const struct building *b);

View File

@ -480,6 +480,16 @@ static void test_safe_building(CuTest *tc) {
test_cleanup();
}
static void test_building_type(CuTest *tc) {
building_type *btype;
test_setup();
btype = test_create_buildingtype("house");
CuAssertIntEquals(tc, true, is_building_type(btype, "house"));
CuAssertIntEquals(tc, false, is_building_type(btype, "castle"));
CuAssertIntEquals(tc, false, is_building_type(NULL, "house"));
test_cleanup();
}
CuSuite *get_building_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -494,6 +504,7 @@ CuSuite *get_building_suite(void)
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_other_after_leave);
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_same_faction_after_leave);
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_empty_unit_after_leave);
SUITE_ADD_TEST(suite, test_building_type);
SUITE_ADD_TEST(suite, test_active_building);
SUITE_ADD_TEST(suite, test_buildingtype_exists);
SUITE_ADD_TEST(suite, test_safe_building);

View File

@ -899,7 +899,6 @@ default_wage(const region * r, const faction * f, const race * rc, int in_turn)
int esize = 0;
double wage;
attrib *a;
const building_type *artsculpture_type = bt_find("artsculpture");
const struct curse_type *ctype;
if (b != NULL) {
@ -935,7 +934,7 @@ default_wage(const region * r, const faction * f, const race * rc, int in_turn)
/* Artsculpture: Income +5 */
for (b = r->buildings; b; b = b->next) {
if (b->type == artsculpture_type) {
if (is_building_type(b->type, "artsculpture")) {
wage += 5;
}
}

View File

@ -25,8 +25,7 @@ static attrib_type at_lighthouse = {
*/
void update_lighthouse(building * lh)
{
const struct building_type *bt_lighthouse = bt_find("lighthouse");
if (bt_lighthouse && lh->type == bt_lighthouse) {
if (is_building_type(lh->type, "lighthouse")) {
region *r = lh->region;
int d = (int)log10(lh->size) + 1;
int x;

View File

@ -1754,11 +1754,9 @@ f_regionid(const region * r, const faction * f, char *buffer, size_t size)
static char *f_regionid_s(const region * r, const faction * f)
{
static int i = 0;
static char bufs[4][NAMESIZE + 20]; // FIXME: static return value
char *buf = bufs[(++i) % 4];
static char buf[NAMESIZE + 20]; // FIXME: static return value
f_regionid(r, f, buf, NAMESIZE + 20);
f_regionid(r, f, buf, sizeof(buf));
return buf;
}