Merge pull request #343 from ennorehling/feature/bug-2143-monsters-attack-buildings

bug 2143: monsters attack buildings
This commit is contained in:
Enno Rehling 2015-11-02 16:40:32 +01:00
commit 01078460bb
12 changed files with 111 additions and 63 deletions

View File

@ -1101,7 +1101,7 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
for (; mod->flags != 0; ++mod) { for (; mod->flags != 0; ++mod) {
if (mod->flags & RMF_REQUIREDBUILDING) { if (mod->flags & RMF_REQUIREDBUILDING) {
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
if (mod->btype && mod->btype != btype) { if (mod->btype && mod->btype != btype) {
cmistake(u, u->thisorder, 104, MSG_PRODUCE); cmistake(u, u->thisorder, 104, MSG_PRODUCE);
return; return;
@ -1157,7 +1157,7 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
} }
else { else {
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
if (rdata->modifiers) { if (rdata->modifiers) {
resource_mod *mod = rdata->modifiers; resource_mod *mod = rdata->modifiers;
@ -1213,7 +1213,7 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
if (rdata->modifiers) { if (rdata->modifiers) {
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
resource_mod *mod = rdata->modifiers; resource_mod *mod = rdata->modifiers;
for (; mod->flags != 0; ++mod) { for (; mod->flags != 0; ++mod) {
@ -2422,7 +2422,7 @@ static void breedhorses(unit * u)
{ {
int n, c, breed = 0; int n, c, breed = 0;
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; 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;

View File

@ -448,9 +448,10 @@ int build(unit * u, const construction * ctype, int completed, int want)
return EBUILDINGREQ; return EBUILDINGREQ;
} }
b = inside_building(u); b = inside_building(u);
if (b == NULL) if (!b || !building_is_active(b)) {
return EBUILDINGREQ; return EBUILDINGREQ;
} }
}
if (type->skill != NOSKILL) { if (type->skill != NOSKILL) {
int effsk; int effsk;
@ -462,7 +463,7 @@ int build(unit * u, const construction * ctype, int completed, int want)
return ENEEDSKILL; return ENEEDSKILL;
effsk = basesk; effsk = basesk;
if (inside_building(u)) { if (building_is_active(u->building) && inside_building(u)) {
effsk = skillmod(u->building->type->attribs, u, u->region, type->skill, effsk = skillmod(u->building->type->attribs, u, u->region, type->skill,
effsk, SMF_PRODUCTION); effsk, SMF_PRODUCTION);
} }
@ -558,7 +559,7 @@ int build(unit * u, const construction * ctype, int completed, int want)
int need, prebuilt; int need, prebuilt;
int canuse = get_pooled(u, rtype, GET_DEFAULT, INT_MAX); int canuse = get_pooled(u, rtype, GET_DEFAULT, INT_MAX);
if (inside_building(u)) { if (building_is_active(u->building) && inside_building(u)) {
canuse = matmod(u->building->type->attribs, u, rtype, canuse); canuse = matmod(u->building->type->attribs, u, rtype, canuse);
} }
@ -597,8 +598,9 @@ int build(unit * u, const construction * ctype, int completed, int want)
required(completed + n, type->reqsize, type->materials[c].number); required(completed + n, type->reqsize, type->materials[c].number);
int multi = 1; int multi = 1;
int canuse = 100; /* normalization */ int canuse = 100; /* normalization */
if (inside_building(u)) if (building_is_active(u->building) && inside_building(u)) {
canuse = matmod(u->building->type->attribs, u, rtype, canuse); canuse = matmod(u->building->type->attribs, u, rtype, canuse);
}
if (canuse < 0) if (canuse < 0)
return canuse; /* pass errors to caller */ return canuse; /* pass errors to caller */
canuse = matmod(type->attribs, u, rtype, canuse); canuse = matmod(type->attribs, u, rtype, canuse);

View File

@ -115,7 +115,7 @@ building_type *bt_get_or_create(const char *name)
btype->_name = _strdup(name); btype->_name = _strdup(name);
btype->auraregen = 1.0; btype->auraregen = 1.0;
btype->maxsize = -1; btype->maxsize = -1;
btype->capacity = -1; btype->capacity = 1;
btype->maxcapacity = -1; btype->maxcapacity = -1;
bt_register(btype); bt_register(btype);
} }
@ -642,6 +642,10 @@ region *building_getregion(const building * b)
return b->region; return b->region;
} }
bool building_is_active(const struct building *b) {
return b && fval(b, BLD_WORKING);
}
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

@ -78,6 +78,7 @@ extern "C" {
} building_type; } building_type;
extern struct quicklist *buildingtypes; extern struct quicklist *buildingtypes;
extern struct attrib_type at_building_action;
building_type *bt_get_or_create(const char *name); building_type *bt_get_or_create(const char *name);
const building_type *bt_find(const char *name); const building_type *bt_find(const char *name);
@ -163,7 +164,7 @@ 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);
extern struct attrib_type at_building_action; bool building_is_active(const struct building *b);
#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

@ -373,7 +373,7 @@ static void test_btype_defaults(CuTest *tc) {
CuAssertTrue(tc, !btype->taxes); CuAssertTrue(tc, !btype->taxes);
CuAssertDblEquals(tc, 1.0, btype->auraregen, 0.0); CuAssertDblEquals(tc, 1.0, btype->auraregen, 0.0);
CuAssertIntEquals(tc, -1, btype->maxsize); CuAssertIntEquals(tc, -1, btype->maxsize);
CuAssertIntEquals(tc, -1, btype->capacity); CuAssertIntEquals(tc, 1, btype->capacity);
CuAssertIntEquals(tc, -1, btype->maxcapacity); CuAssertIntEquals(tc, -1, btype->maxcapacity);
CuAssertIntEquals(tc, 0, btype->magres); CuAssertIntEquals(tc, 0, btype->magres);
CuAssertIntEquals(tc, 0, btype->magresbonus); CuAssertIntEquals(tc, 0, btype->magresbonus);
@ -382,6 +382,19 @@ static void test_btype_defaults(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_active_building(CuTest *tc) {
building *b;
test_cleanup();
b = test_create_building(test_create_region(0,0,0), 0);
CuAssertIntEquals(tc, false, building_is_active(b));
b->flags |= BLD_WORKING;
CuAssertIntEquals(tc, true, building_is_active(b));
b->flags &= ~BLD_WORKING;
CuAssertIntEquals(tc, false, building_is_active(b));
test_cleanup();
}
CuSuite *get_building_suite(void) CuSuite *get_building_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -396,5 +409,6 @@ CuSuite *get_building_suite(void)
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_other_after_leave); 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_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);
return suite; return suite;
} }

View File

@ -1072,11 +1072,7 @@ void transfermen(unit * u, unit * dst, int n)
struct building *inside_building(const struct unit *u) struct building *inside_building(const struct unit *u)
{ {
if (u->building == NULL) if (!u->building) {
return NULL;
if (!fval(u->building, BLD_WORKING)) {
/* Unterhalt nicht bezahlt */
return NULL; return NULL;
} }
else if (u->building->size < u->building->type->maxsize) { else if (u->building->size < u->building->type->maxsize) {

View File

@ -183,36 +183,36 @@ extern "C" {
void make_zombie(unit * u); void make_zombie(unit * u);
/* see resolve.h */ /* see resolve.h */
extern int resolve_unit(variant data, void *address); int resolve_unit(variant data, void *address);
extern void write_unit_reference(const struct unit *u, struct storage *store); void write_unit_reference(const struct unit *u, struct storage *store);
extern variant read_unit_reference(struct storage *store); variant read_unit_reference(struct storage *store);
extern bool leave(struct unit *u, bool force); bool leave(struct unit *u, bool force);
extern bool can_leave(struct unit *u); bool can_leave(struct unit *u);
extern void u_set_building(struct unit * u, struct building * b); void u_set_building(struct unit * u, struct building * b);
extern void u_set_ship(struct unit * u, struct ship * sh); void u_set_ship(struct unit * u, struct ship * sh);
extern void leave_ship(struct unit * u); void leave_ship(struct unit * u);
extern void leave_building(struct unit * u); void leave_building(struct unit * u);
extern void set_leftship(struct unit *u, struct ship *sh); void set_leftship(struct unit *u, struct ship *sh);
extern struct ship *leftship(const struct unit *); struct ship *leftship(const struct unit *);
extern bool can_survive(const struct unit *u, const struct region *r); bool can_survive(const struct unit *u, const struct region *r);
extern void move_unit(struct unit *u, struct region *target, void move_unit(struct unit *u, struct region *target,
struct unit **ulist); struct unit **ulist);
extern struct building *inside_building(const struct unit *u); struct building *inside_building(const struct unit *u);
/* cleanup code for this module */ /* cleanup code for this module */
extern void free_units(void); void free_units(void);
extern struct faction *dfindhash(int no); struct faction *dfindhash(int no);
extern void u_setfaction(struct unit *u, struct faction *f); void u_setfaction(struct unit *u, struct faction *f);
extern void set_number(struct unit *u, int count); void set_number(struct unit *u, int count);
extern bool learn_skill(struct unit *u, skill_t sk, double chance); bool learn_skill(struct unit *u, skill_t sk, double chance);
extern int invisible(const struct unit *target, const struct unit *viewer); int invisible(const struct unit *target, const struct unit *viewer);
extern void free_unit(struct unit *u); void free_unit(struct unit *u);
extern void name_unit(struct unit *u); extern void name_unit(struct unit *u);
extern struct unit *create_unit(struct region *r1, struct faction *f, extern struct unit *create_unit(struct region *r1, struct faction *f,

View File

@ -2,6 +2,7 @@
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/curse.h> #include <kernel/curse.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/building.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/order.h> #include <kernel/order.h>
#include <kernel/race.h> #include <kernel/race.h>
@ -376,6 +377,31 @@ static void test_produceexp(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_inside_building(CuTest *tc) {
unit *u;
building *b;
test_cleanup();
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
b = test_create_building(u->region, 0);
b->size = 1;
scale_number(u, 1);
CuAssertPtrEquals(tc, 0, inside_building(u));
u->building = b;
CuAssertPtrEquals(tc, b, inside_building(u));
scale_number(u, 2);
CuAssertPtrEquals(tc, 0, inside_building(u));
b->size = 2;
CuAssertPtrEquals(tc, b, inside_building(u));
u = test_create_unit(u->faction, u->region);
u->building = b;
CuAssertPtrEquals(tc, 0, inside_building(u));
b->size = 3;
CuAssertPtrEquals(tc, b, inside_building(u));
test_cleanup();
}
CuSuite *get_unit_suite(void) CuSuite *get_unit_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -394,6 +420,7 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_skill_hunger); SUITE_ADD_TEST(suite, test_skill_hunger);
SUITE_ADD_TEST(suite, test_skill_familiar); SUITE_ADD_TEST(suite, test_skill_familiar);
SUITE_ADD_TEST(suite, test_age_familiar); SUITE_ADD_TEST(suite, test_age_familiar);
SUITE_ADD_TEST(suite, test_inside_building);
SUITE_ADD_TEST(suite, test_produceexp); SUITE_ADD_TEST(suite, test_produceexp);
return suite; return suite;
} }

View File

@ -3545,7 +3545,7 @@ void monthly_healing(void)
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); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
if (btype == bt_find("inn")) { if (btype == bt_find("inn")) {
p *= 1.5; p *= 1.5;
} }

View File

@ -1025,7 +1025,7 @@ spellpower(region * r, unit * u, const spell * sp, int cast_level, struct order
else { else {
/* Bonus durch Magieturm und gesegneten Steinkreis */ /* Bonus durch Magieturm und gesegneten Steinkreis */
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
if (btype && btype->flags & BTF_MAGIC) ++force; if (btype && btype->flags & BTF_MAGIC) ++force;
} }
@ -1170,7 +1170,7 @@ double magic_resistance(unit * target)
/* Bonus durch Gebäude */ /* Bonus durch Gebäude */
{ {
struct building *b = inside_building(target); struct building *b = inside_building(target);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
/* gesegneter Steinkreis gibt 30% dazu */ /* gesegneter Steinkreis gibt 30% dazu */
if (btype) if (btype)
@ -1284,7 +1284,7 @@ bool fumble(region * r, unit * u, const spell * sp, int cast_grade)
int fumble_chance, rnd = 0; int fumble_chance, rnd = 0;
int effsk = effskill(u, SK_MAGIC, r); int effsk = effskill(u, SK_MAGIC, r);
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
int fumble_enabled = get_param_int(global.parameters, "magic.fumble.enable", 1); int fumble_enabled = get_param_int(global.parameters, "magic.fumble.enable", 1);
sc_mage * mage; sc_mage * mage;
@ -1468,7 +1468,7 @@ void regenerate_aura(void)
auramax = max_spellpoints(r, u); auramax = max_spellpoints(r, u);
if (aura < auramax) { if (aura < auramax) {
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = building_is_active(b) ? b->type : NULL;
reg_aura = regeneration(u); reg_aura = regeneration(u);
/* Magierturm erhöht die Regeneration um 75% */ /* Magierturm erhöht die Regeneration um 75% */

View File

@ -769,13 +769,14 @@ static order *plan_dragon(unit * u)
void plan_monsters(faction * f) void plan_monsters(faction * f)
{ {
region *r; region *r;
double attack_chance = monster_attack_chance();
assert(f); assert(f);
f->lastorders = turn; f->lastorders = turn;
for (r = regions; r; r = r->next) { for (r = regions; r; r = r->next) {
unit *u; unit *u;
double attack_chance = monster_attack_chance(); double rchance = attack_chance;
bool attacking = false; bool attacking = false;
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
@ -786,25 +787,23 @@ void plan_monsters(faction * f)
if (!is_monsters(u->faction)) if (!is_monsters(u->faction))
continue; continue;
if (attack_chance > 0.0) { /* Befehle müssen jede Runde neu gegeben werden: */
if (chance(attack_chance)) free_orders(&u->orders);
attacking = true;
attack_chance = 0.0;
}
if (u->status > ST_BEHIND) {
setstatus(u, ST_FIGHT);
/* all monsters fight */
}
if (skill_enabled(SK_PERCEPTION)) { if (skill_enabled(SK_PERCEPTION)) {
/* Monster bekommen jede Runde ein paar Tage Wahrnehmung dazu */ /* Monster bekommen jede Runde ein paar Tage Wahrnehmung dazu */
/* TODO: this only works for playerrace */ /* TODO: this only works for playerrace */
produceexp(u, SK_PERCEPTION, u->number); produceexp(u, SK_PERCEPTION, u->number);
} }
/* Befehle müssen jede Runde neu gegeben werden: */ if (rchance > 0.0) {
free_orders(&u->orders); if (chance(rchance))
attacking = true;
rchance = 0.0;
}
if (u->status > ST_BEHIND) {
setstatus(u, ST_FIGHT);
/* all monsters fight */
}
if (attacking && is_guard(u, GUARD_TAX)) { if (attacking && is_guard(u, GUARD_TAX)) {
monster_attacks(u); monster_attacks(u);
} }

View File

@ -175,6 +175,13 @@ 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)
@ -205,8 +212,7 @@ bool report, int *academy)
n = _min(n, nteaching); n = _min(n, nteaching);
if (n != 0) { if (n != 0) {
struct building *b = inside_building(teacher); const struct building_type *btype = bt_find("academy");
const struct building_type *btype = b ? b->type : NULL;
int index = 0; int index = 0;
if (teach == NULL) { if (teach == NULL) {
@ -229,8 +235,7 @@ bool report, int *academy)
/* Solange Akademien groessenbeschraenkt sind, sollte Lehrer und /* Solange Akademien groessenbeschraenkt sind, sollte Lehrer und
* Student auch in unterschiedlichen Gebaeuden stehen duerfen */ * Student auch in unterschiedlichen Gebaeuden stehen duerfen */
if (btype == bt_find("academy") if (active_building(teacher, btype) && active_building(student, btype)) {
&& student->building && student->building->type == bt_find("academy")) {
int j = study_cost(student, sk); int j = study_cost(student, sk);
j = _max(50, j * 2); j = _max(50, j * 2);
/* kann Einheit das zahlen? */ /* kann Einheit das zahlen? */
@ -542,7 +547,7 @@ int study_cmd(unit * u, order * ord)
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); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; 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");
@ -606,7 +611,7 @@ int study_cmd(unit * u, order * ord)
} }
/* Akademie: */ /* Akademie: */
b = inside_building(u); b = inside_building(u);
btype = b ? b->type : NULL; btype = building_is_active(b) ? b->type : NULL;
if (btype && btype == bt_find("academy")) { if (btype && btype == bt_find("academy")) {
studycost = _max(50, studycost * 2); studycost = _max(50, studycost * 2);