From 5bb9a10a46c84635af1baea57ac804a17bca0c01 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 30 Aug 2016 09:13:59 +0100 Subject: [PATCH] is_building_type should be a quicker way to test for a building type than bt_find. --- src/kernel/building.c | 20 +++++++------------- src/kernel/building.h | 1 + src/kernel/building.test.c | 11 +++++++++++ src/kernel/config.c | 3 +-- src/lighthouse.c | 3 +-- src/reports.c | 6 ++---- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/kernel/building.c b/src/kernel/building.c index 7e537c70d..ca9fbe9ce 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -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; +} diff --git a/src/kernel/building.h b/src/kernel/building.h index 020de077b..587197276 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -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); diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 1c5ff1fe2..bda3e7e90 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -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); diff --git a/src/kernel/config.c b/src/kernel/config.c index feaf76aa8..12e16719c 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -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; } } diff --git a/src/lighthouse.c b/src/lighthouse.c index 6192db142..4230268d5 100644 --- a/src/lighthouse.c +++ b/src/lighthouse.c @@ -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; diff --git a/src/reports.c b/src/reports.c index 679b1be42..e498d0f23 100644 --- a/src/reports.c +++ b/src/reports.c @@ -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; }