diff --git a/src/economy.c b/src/economy.c index 19223daa1..6d4e0a56e 100644 --- a/src/economy.c +++ b/src/economy.c @@ -2421,13 +2421,11 @@ static void breedtrees(unit * u, int raw) static void breedhorses(unit * u) { int n, c, breed = 0; - struct building *b = inside_building(u); - const struct building_type *btype = building_is_active(b) ? b->type : NULL; const struct resource_type *rhorse = get_resourcetype(R_HORSE); int horses, effsk; assert(rhorse && rhorse->itype); - if (btype != bt_find("stables")) { + if (!active_building(u, bt_find("stables"))) { cmistake(u, u->thisorder, 122, MSG_PRODUCE); return; } diff --git a/src/kernel/building.c b/src/kernel/building.c index d4a7fbbbd..98b4df0c6 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -642,10 +642,31 @@ region *building_getregion(const building * b) return b->region; } -bool building_is_active(const struct building *b) { - return b && fval(b, BLD_WORKING); +bool +buildingtype_exists(const region * r, const building_type * bt, bool working) +{ + building *b; + + for (b = rbuildings(r); b; b = b->next) { + if (b->type == bt && b->size >= bt->maxsize && (!working || fval(b, BLD_WORKING))) + return true; + } + + return false; } +bool building_is_active(const struct building *b) { + return b && fval(b, BLD_WORKING) && b->size >= b->type->maxsize; +} + +building *active_building(const unit *u, const struct building_type *btype) { + if (u->building && u->building->type == btype && building_is_active(u->building)) { + return inside_building(u); + } + return 0; +} + + void building_setregion(building * b, region * r) { building **blist = &b->region->buildings; diff --git a/src/kernel/building.h b/src/kernel/building.h index 78b234f3d..4d08ae647 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -166,7 +166,10 @@ extern "C" { extern void building_set_owner(struct unit * u); extern void building_update_owner(struct building * bld); + bool buildingtype_exists(const struct region *r, + const struct building_type *bt, bool working); bool building_is_active(const struct building *b); + struct building *active_building(const struct unit *u, const struct building_type *btype); #ifdef WDW_PYRAMID extern int wdw_pyramid_level(const struct building *b); diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 4241e893f..abbb335bb 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -6,10 +6,13 @@ #include #include +#include + #include #include #include #include +#include static void test_register_building(CuTest * tc) { @@ -382,16 +385,59 @@ static void test_btype_defaults(CuTest *tc) { test_cleanup(); } -static void test_active_building(CuTest *tc) { +static void test_building_type_exists(CuTest * tc) +{ + region *r; building *b; + building_type *btype, *btype2; test_cleanup(); - b = test_create_building(test_create_region(0, 0, 0), 0); + test_create_world(); + + btype2 = bt_get_or_create("lighthouse"); + btype = bt_get_or_create("castle"); + + r = findregion(-1, 0); + b = new_building(btype, r, default_locale); + + CuAssertPtrNotNull(tc, b); + CuAssertTrue(tc, !buildingtype_exists(r, NULL, false)); + CuAssertTrue(tc, buildingtype_exists(r, btype, false)); + CuAssertTrue(tc, !buildingtype_exists(r, btype2, false)); +} + +static void test_active_building(CuTest *tc) { + building *b; + region *r; + unit *u; + building_type *btype; + + test_cleanup(); + + btype = test_create_buildingtype("castle"); + assert(btype && btype->maxsize == -1); + b = test_create_building(r = test_create_region(0, 0, 0), btype); + u = test_create_unit(test_create_faction(0), r); CuAssertIntEquals(tc, false, building_is_active(b)); + CuAssertPtrEquals(tc, NULL, active_building(u, btype)); + b->flags |= BLD_WORKING; CuAssertIntEquals(tc, true, building_is_active(b)); + CuAssertPtrEquals(tc, NULL, active_building(u, btype)); + u_set_building(u, b); + CuAssertIntEquals(tc, true, building_is_active(b)); + CuAssertPtrNotNull(tc, active_building(u, btype) ); + btype->maxsize = 10; + b->size = btype->maxsize; + CuAssertIntEquals(tc, true, building_is_active(b)); + CuAssertPtrNotNull(tc, active_building(u, btype) ); + b->size = 9; + CuAssertIntEquals(tc, false, building_is_active(b)); + CuAssertPtrEquals(tc, NULL, active_building(u, btype)); + btype->maxsize = -1; b->flags &= ~BLD_WORKING; CuAssertIntEquals(tc, false, building_is_active(b)); + CuAssertPtrEquals(tc, NULL, active_building(u, btype)); test_cleanup(); } @@ -430,6 +476,7 @@ CuSuite *get_building_suite(void) 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_active_building); + SUITE_ADD_TEST(suite, test_building_type_exists); SUITE_ADD_TEST(suite, test_safe_building); return suite; } diff --git a/src/laws.c b/src/laws.c index 15c5404b4..54574cee9 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3546,9 +3546,7 @@ void monthly_healing(void) if (u->hp < umhp) { double maxheal = _max(u->number, umhp / 20.0); int addhp; - struct building *b = inside_building(u); - const struct building_type *btype = building_is_active(b) ? b->type : NULL; - if (btype == bt_find("inn")) { + if (active_building(u, bt_find("inn"))) { p *= 1.5; } /* pro punkt 5% höher */ diff --git a/src/market.c b/src/market.c index 68ef9767c..3d96c30e4 100644 --- a/src/market.c +++ b/src/market.c @@ -36,9 +36,9 @@ static unsigned int get_markets(region * r, unit ** results, size_t size) if (!btype) 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) { + if (b->type == btype && building_is_active(b)) { unit *u = building_owner(b); + /* I decided to omit check for inside_building(u) */ unsigned int i; for (i = 0; u && i != n; ++i) { /* only one market per faction */ diff --git a/src/move.c b/src/move.c index 00752c3cf..fa736ac79 100644 --- a/src/move.c +++ b/src/move.c @@ -1749,21 +1749,7 @@ unit *owner_buildingtyp(const region * r, const building_type * bt) return NULL; } -bool -buildingtype_exists(const region * r, const building_type * bt, bool working) -{ - building *b; - - for (b = rbuildings(r); b; b = b->next) { - if (b->type == bt && b->size >= bt->maxsize && (!working || fval(b, BLD_WORKING))) - return true; - } - - return false; -} - /* Prüft, ob Ablegen von einer Küste in eine der erlaubten Richtungen erfolgt. */ - bool can_takeoff(const ship * sh, const region * from, const region * to) { if (!fval(from->terrain, SEA_REGION) && sh->coast != NODIRECTION) { diff --git a/src/move.h b/src/move.h index c0b92d071..53eac0d48 100644 --- a/src/move.h +++ b/src/move.h @@ -68,8 +68,6 @@ extern "C" { struct region *to, struct region_list *route); int walkingcapacity(const struct unit *u); void follow_unit(struct unit *u); - bool buildingtype_exists(const struct region *r, - const struct building_type *bt, bool working); struct unit *owner_buildingtyp(const struct region *r, const struct building_type *bt); bool move_blocked(const struct unit *u, const struct region *src, diff --git a/src/move.test.c b/src/move.test.c index 5c24ca72e..95efa2c2c 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -16,7 +16,6 @@ #include #include -#include #include #include @@ -153,27 +152,6 @@ static void test_ship_has_harbormaster_ally(CuTest * tc) { test_cleanup(); } -static void test_building_type_exists(CuTest * tc) -{ - region *r; - building *b; - building_type *btype, *btype2; - - test_cleanup(); - test_create_world(); - - btype2 = bt_get_or_create("lighthouse"); - btype = bt_get_or_create("castle"); - - r = findregion(-1, 0); - b = new_building(btype, r, default_locale); - - CuAssertPtrNotNull(tc, b); - CuAssertTrue(tc, !buildingtype_exists(r, NULL, false)); - CuAssertTrue(tc, buildingtype_exists(r, btype, false)); - CuAssertTrue(tc, !buildingtype_exists(r, btype2, false)); -} - static void test_walkingcapacity(CuTest *tc) { region *r; unit *u; @@ -299,7 +277,6 @@ CuSuite *get_move_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_walkingcapacity); - SUITE_ADD_TEST(suite, test_building_type_exists); SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast); SUITE_ADD_TEST(suite, test_ship_allowed_without_harbormaster); SUITE_ADD_TEST(suite, test_ship_blocked_by_harbormaster); diff --git a/src/study.c b/src/study.c index b798277ca..bbc24c07f 100644 --- a/src/study.c +++ b/src/study.c @@ -175,13 +175,6 @@ static int study_days(unit * student, skill_t sk) return student->number * speed; } -static building *active_building(const unit *u, const struct building_type *btype) { - if (u->building && u->building->type == btype && building_is_active(u->building)) { - return inside_building(u); - } - return 0; -} - static int teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk, bool report, int *academy) @@ -546,8 +539,6 @@ int study_cmd(unit * u, order * ord) int maxalchemy = 0; int speed_rule = (study_rule_t)get_param_int(global.parameters, "study.speedup", 0); static int learn_newskills = -1; - struct building *b = inside_building(u); - const struct building_type *btype = building_is_active(b) ? b->type : NULL; if (learn_newskills < 0) { const char *str = get_param(global.parameters, "study.newskills"); @@ -610,10 +601,7 @@ int study_cmd(unit * u, order * ord) return 0; } /* Akademie: */ - b = inside_building(u); - btype = building_is_active(b) ? b->type : NULL; - - if (btype && btype == bt_find("academy")) { + if (active_building(u, bt_find("academy"))) { studycost = _max(50, studycost * 2); }