added a test: region owners can set PAY NOT <id> for empty buildings if they are in region_owner_pay_building.

This commit is contained in:
Enno Rehling 2014-12-17 21:31:02 +01:00
parent d7899a392a
commit 484e52d491
2 changed files with 314 additions and 281 deletions

View File

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -72,7 +72,7 @@ static building_type *bt_find_i(const char *name)
assert(name);
for (qi = 0, ql = buildingtypes; ql; ql_advance(&ql, &qi, 1)) {
building_type *btype = (building_type *) ql_get(ql, qi);
building_type *btype = (building_type *)ql_get(ql, qi);
if (strcmp(btype->_name, name) == 0)
return btype;
}
@ -297,7 +297,7 @@ const building_type *findbuildingtype(const char *name,
bn->lang = lang;
for (qi = 0, ql = buildingtypes; ql; ql_advance(&ql, &qi, 1)) {
building_type *btype = (building_type *) ql_get(ql, qi);
building_type *btype = (building_type *)ql_get(ql, qi);
const char *n = locale_string(lang, btype->_name);
type.v = (void *)btype;
@ -325,14 +325,14 @@ static int meropis_building_protection(building * b, unit * u)
void register_buildings(void)
{
register_function((pf_generic) & eressea_building_protection,
register_function((pf_generic)& eressea_building_protection,
"eressea_building_protection");
register_function((pf_generic) & meropis_building_protection,
register_function((pf_generic)& meropis_building_protection,
"meropis_building_protection");
register_function((pf_generic) & init_smithy, "init_smithy");
register_function((pf_generic) & castle_name, "castle_name");
register_function((pf_generic) & castle_name_2, "castle_name_2");
register_function((pf_generic) & fort_name, "fort_name");
register_function((pf_generic)& init_smithy, "init_smithy");
register_function((pf_generic)& castle_name, "castle_name");
register_function((pf_generic)& castle_name_2, "castle_name_2");
register_function((pf_generic)& fort_name, "fort_name");
}
void write_building_reference(const struct building *b, struct storage *store)
@ -350,7 +350,7 @@ int resolve_building(variant id, void *address)
result = -1;
}
}
*(building **) address = b;
*(building **)address = b;
return result;
}
@ -365,7 +365,7 @@ building *new_building(const struct building_type * btype, region * r,
const struct locale * lang)
{
building **bptr = &r->buildings;
building *b = (building *) calloc(1, sizeof(building));
building *b = (building *)calloc(1, sizeof(building));
static bool init_lighthouse = false;
static const struct building_type *bt_lighthouse = 0;
const char *bname = 0;
@ -549,7 +549,7 @@ static unit *building_owner_ex(const building * bld, const struct faction * last
for (u = bld->region->units; u; u = u->next) {
if (u->building == bld) {
if (u->number > 0) {
if (heir && last_owner && heir->faction!=last_owner && u->faction==last_owner) {
if (heir && last_owner && heir->faction != last_owner && u->faction == last_owner) {
heir = u;
break; /* we found someone from the same faction who is not dead. let's take this guy */
}
@ -579,16 +579,16 @@ unit *building_owner(const building * bld)
return NULL;
}
unit *owner = bld->_owner;
if (!owner || (owner->building!=bld || owner->number<=0)) {
unit * heir = building_owner_ex(bld, owner?owner->faction:0);
return (heir && heir->number>0) ? heir : 0;
if (!owner || (owner->building != bld || owner->number <= 0)) {
unit * heir = building_owner_ex(bld, owner ? owner->faction : 0);
return (heir && heir->number > 0) ? heir : 0;
}
return owner;
}
void building_update_owner(building * bld) {
unit * owner = bld->_owner;
bld->_owner = building_owner_ex(bld, owner?owner->faction:0);
bld->_owner = building_owner_ex(bld, owner ? owner->faction : 0);
}
const char *building_getname(const building * self)

View File

@ -13,12 +13,14 @@
#include <kernel/terrain.h>
#include <kernel/unit.h>
#include <util/base36.h>
#include <util/language.h>
#include <CuTest.h>
#include <tests.h>
#include <assert.h>
#include <stdio.h>
static void test_new_building_can_be_renamed(CuTest * tc)
{
@ -356,16 +358,23 @@ struct pay_fixture {
unit *u2;
};
static double level_taxes(const building * b, int level) {
return b->size*level*2.0;
}
static void setup_pay_cmd(struct pay_fixture *fix) {
faction *f;
region *r;
building *b;
building_type *btcastle;
test_create_world();
f = test_create_faction(NULL);
r = findregion(0, 0);
assert(r && f);
b = test_create_building(r, bt_get_or_create("lighthouse"));
btcastle = bt_get_or_create("castle");
btcastle->taxes = level_taxes;
b = test_create_building(r, btcastle);
assert(b);
fix->u1 = test_create_unit(f, r);
fix->u2 = test_create_unit(f, r);
@ -394,6 +403,30 @@ static void test_pay_cmd(CuTest *tc) {
test_cleanup();
}
static void test_pay_cmd_other_building(CuTest *tc) {
struct pay_fixture fix;
order *ord;
faction *f;
building *b;
char cmd[32];
test_cleanup();
setup_pay_cmd(&fix);
f = fix.u1->faction;
b = test_create_building(fix.u1->region, bt_get_or_create("lighthouse"));
set_param(&global.parameters, "rules.region_owners", "1");
set_param(&global.parameters, "rules.region_owner_pay_building", "lighthouse");
update_owners(b->region);
_snprintf(cmd, sizeof(cmd), "NOT %s", itoa36(b->no));
ord = create_order(K_PAY, f->locale, cmd);
assert(ord);
CuAssertPtrEquals(tc, fix.u1, building_owner(b));
CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord));
CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY);
test_cleanup();
}
static void test_pay_cmd_must_be_owner(CuTest *tc) {
struct pay_fixture fix;
order *ord;
@ -412,7 +445,6 @@ static void test_pay_cmd_must_be_owner(CuTest *tc) {
test_cleanup();
}
static void test_new_units(CuTest *tc) {
unit *u;
faction *f;
@ -581,6 +613,7 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_reserve_self);
SUITE_ADD_TEST(suite, test_reserve_cmd);
SUITE_ADD_TEST(suite, test_pay_cmd);
SUITE_ADD_TEST(suite, test_pay_cmd_other_building);
SUITE_ADD_TEST(suite, test_pay_cmd_must_be_owner);
SUITE_ADD_TEST(suite, test_new_units);
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);