forked from github/server
refactored inside_building/building_is_active/active_building
This commit is contained in:
parent
dcca3f1424
commit
5326bbd9e4
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -6,10 +6,13 @@
|
|||
#include <kernel/building.h>
|
||||
#include <kernel/unit.h>
|
||||
|
||||
#include <util/language.h>
|
||||
|
||||
#include <CuTest.h>
|
||||
#include <tests.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
14
src/move.c
14
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) {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
#include <kernel/race.h>
|
||||
|
||||
#include <util/attrib.h>
|
||||
#include <util/language.h>
|
||||
|
||||
#include <CuTest.h>
|
||||
#include <tests.h>
|
||||
|
@ -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);
|
||||
|
|
14
src/study.c
14
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue