refactored inside_building/building_is_active/active_building

This commit is contained in:
Steffen Mecke 2015-11-16 02:13:48 +01:00
parent dcca3f1424
commit 5326bbd9e4
10 changed files with 80 additions and 64 deletions

View File

@ -2421,13 +2421,11 @@ static void breedtrees(unit * u, int raw)
static void breedhorses(unit * u) static void breedhorses(unit * u)
{ {
int n, c, breed = 0; 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); const struct resource_type *rhorse = get_resourcetype(R_HORSE);
int horses, effsk; int horses, effsk;
assert(rhorse && rhorse->itype); assert(rhorse && rhorse->itype);
if (btype != bt_find("stables")) { if (!active_building(u, bt_find("stables"))) {
cmistake(u, u->thisorder, 122, MSG_PRODUCE); cmistake(u, u->thisorder, 122, MSG_PRODUCE);
return; return;
} }

View File

@ -642,10 +642,31 @@ region *building_getregion(const building * b)
return b->region; return b->region;
} }
bool building_is_active(const struct building *b) { bool
return b && fval(b, BLD_WORKING); 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) void building_setregion(building * b, region * r)
{ {
building **blist = &b->region->buildings; building **blist = &b->region->buildings;

View File

@ -166,7 +166,10 @@ extern "C" {
extern void building_set_owner(struct unit * u); extern void building_set_owner(struct unit * u);
extern void building_update_owner(struct building * bld); 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); bool building_is_active(const struct building *b);
struct building *active_building(const struct unit *u, const struct building_type *btype);
#ifdef WDW_PYRAMID #ifdef WDW_PYRAMID
extern int wdw_pyramid_level(const struct building *b); extern int wdw_pyramid_level(const struct building *b);

View File

@ -6,10 +6,13 @@
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <util/language.h>
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
static void test_register_building(CuTest * tc) static void test_register_building(CuTest * tc)
{ {
@ -382,16 +385,59 @@ static void test_btype_defaults(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_active_building(CuTest *tc) { static void test_building_type_exists(CuTest * tc)
{
region *r;
building *b; building *b;
building_type *btype, *btype2;
test_cleanup(); 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)); CuAssertIntEquals(tc, false, building_is_active(b));
CuAssertPtrEquals(tc, NULL, active_building(u, btype));
b->flags |= BLD_WORKING; b->flags |= BLD_WORKING;
CuAssertIntEquals(tc, true, building_is_active(b)); 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; b->flags &= ~BLD_WORKING;
CuAssertIntEquals(tc, false, building_is_active(b)); CuAssertIntEquals(tc, false, building_is_active(b));
CuAssertPtrEquals(tc, NULL, active_building(u, btype));
test_cleanup(); 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_same_faction_after_leave);
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_empty_unit_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_active_building);
SUITE_ADD_TEST(suite, test_building_type_exists);
SUITE_ADD_TEST(suite, test_safe_building); SUITE_ADD_TEST(suite, test_safe_building);
return suite; return suite;
} }

View File

@ -3546,9 +3546,7 @@ void monthly_healing(void)
if (u->hp < umhp) { if (u->hp < umhp) {
double maxheal = _max(u->number, umhp / 20.0); double maxheal = _max(u->number, umhp / 20.0);
int addhp; int addhp;
struct building *b = inside_building(u); if (active_building(u, bt_find("inn"))) {
const struct building_type *btype = building_is_active(b) ? b->type : NULL;
if (btype == bt_find("inn")) {
p *= 1.5; p *= 1.5;
} }
/* pro punkt 5% höher */ /* pro punkt 5% höher */

View File

@ -36,9 +36,9 @@ static unsigned int get_markets(region * r, unit ** results, size_t size)
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) {
if (b->type == btype && (b->flags & BLD_WORKING) if (b->type == btype && building_is_active(b)) {
&& b->size >= b->type->maxsize) {
unit *u = building_owner(b); unit *u = building_owner(b);
/* I decided to omit check for inside_building(u) */
unsigned int i; unsigned int i;
for (i = 0; u && i != n; ++i) { for (i = 0; u && i != n; ++i) {
/* only one market per faction */ /* only one market per faction */

View File

@ -1749,21 +1749,7 @@ unit *owner_buildingtyp(const region * r, const building_type * bt)
return NULL; 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. */ /* 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) bool can_takeoff(const ship * sh, const region * from, const region * to)
{ {
if (!fval(from->terrain, SEA_REGION) && sh->coast != NODIRECTION) { if (!fval(from->terrain, SEA_REGION) && sh->coast != NODIRECTION) {

View File

@ -68,8 +68,6 @@ extern "C" {
struct region *to, struct region_list *route); struct region *to, struct region_list *route);
int walkingcapacity(const struct unit *u); int walkingcapacity(const struct unit *u);
void follow_unit(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, struct unit *owner_buildingtyp(const struct region *r,
const struct building_type *bt); const struct building_type *bt);
bool move_blocked(const struct unit *u, const struct region *src, bool move_blocked(const struct unit *u, const struct region *src,

View File

@ -16,7 +16,6 @@
#include <kernel/race.h> #include <kernel/race.h>
#include <util/attrib.h> #include <util/attrib.h>
#include <util/language.h>
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
@ -153,27 +152,6 @@ static void test_ship_has_harbormaster_ally(CuTest * tc) {
test_cleanup(); 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) { static void test_walkingcapacity(CuTest *tc) {
region *r; region *r;
unit *u; unit *u;
@ -299,7 +277,6 @@ CuSuite *get_move_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_walkingcapacity); 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_not_allowed_in_coast);
SUITE_ADD_TEST(suite, test_ship_allowed_without_harbormaster); SUITE_ADD_TEST(suite, test_ship_allowed_without_harbormaster);
SUITE_ADD_TEST(suite, test_ship_blocked_by_harbormaster); SUITE_ADD_TEST(suite, test_ship_blocked_by_harbormaster);

View File

@ -175,13 +175,6 @@ static int study_days(unit * student, skill_t sk)
return student->number * speed; 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 static int
teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk, teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
bool report, int *academy) bool report, int *academy)
@ -546,8 +539,6 @@ int study_cmd(unit * u, order * ord)
int maxalchemy = 0; int maxalchemy = 0;
int speed_rule = (study_rule_t)get_param_int(global.parameters, "study.speedup", 0); int speed_rule = (study_rule_t)get_param_int(global.parameters, "study.speedup", 0);
static int learn_newskills = -1; 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) { if (learn_newskills < 0) {
const char *str = get_param(global.parameters, "study.newskills"); const char *str = get_param(global.parameters, "study.newskills");
@ -610,10 +601,7 @@ int study_cmd(unit * u, order * ord)
return 0; return 0;
} }
/* Akademie: */ /* Akademie: */
b = inside_building(u); if (active_building(u, bt_find("academy"))) {
btype = building_is_active(b) ? b->type : NULL;
if (btype && btype == bt_find("academy")) {
studycost = _max(50, studycost * 2); studycost = _max(50, studycost * 2);
} }