Merge branch 'bug2274' into develop

This commit is contained in:
Enno Rehling 2017-01-25 21:15:35 +01:00
commit fb653d6dcf
9 changed files with 119 additions and 41 deletions

View File

@ -335,7 +335,18 @@ const building_type *findbuildingtype(const char *name,
return (const building_type *)type.v; return (const building_type *)type.v;
} }
static int building_protection(const building * b, const unit * u, building_bonus bonus) int cmp_castle_size(const building * b, const building * a)
{
if (!b || !b->type->protection || !building_owner(b)) {
return -1;
}
if (!a || !a->type->protection || !building_owner(a)) {
return 1;
}
return b->size - a->size;
}
int building_protection(const building * b, const unit * u, building_bonus bonus)
{ {
int i = 0; int i = 0;
int bsize = buildingeffsize(b, false); int bsize = buildingeffsize(b, false);

View File

@ -85,6 +85,8 @@ extern "C" {
extern struct quicklist *buildingtypes; extern struct quicklist *buildingtypes;
extern struct attrib_type at_building_action; extern struct attrib_type at_building_action;
int cmp_castle_size(const struct building * b, const struct building * a);
int building_protection(const struct building * b, const struct unit * u, building_bonus bonus);
building_type *bt_get_or_create(const char *name); building_type *bt_get_or_create(const char *name);
bool bt_changed(int *cache); bool bt_changed(int *cache);
const building_type *bt_find(const char *name); const building_type *bt_find(const char *name);

View File

@ -493,9 +493,32 @@ static void test_building_type(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_cmp_castle_size(CuTest *tc) {
region *r;
building *b1, *b2;
unit *u1, *u2;
test_setup();
r = test_create_region(0, 0, 0);
b1 = test_create_building(r, NULL);
b2 = test_create_building(r, NULL);
u1 = test_create_unit(test_create_faction(0), r);
u_set_building(u1, b1);
u2 = test_create_unit(test_create_faction(0), r);
u_set_building(u2, b2);
b1->size = 5;
b2->size = 10;
CuAssertTrue(tc, cmp_castle_size(b1, b2)<0);
CuAssertTrue(tc, cmp_castle_size(b2, b1)>0);
CuAssertTrue(tc, cmp_castle_size(b1, NULL)>0);
CuAssertTrue(tc, cmp_castle_size(NULL, b1)<0);
test_cleanup();
}
CuSuite *get_building_suite(void) CuSuite *get_building_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_cmp_castle_size);
SUITE_ADD_TEST(suite, test_register_building); SUITE_ADD_TEST(suite, test_register_building);
SUITE_ADD_TEST(suite, test_btype_defaults); SUITE_ADD_TEST(suite, test_btype_defaults);
SUITE_ADD_TEST(suite, test_building_set_owner); SUITE_ADD_TEST(suite, test_building_set_owner);

View File

@ -204,7 +204,7 @@ param_t findparam(const char *s, const struct locale * lang)
void **tokens = get_translations(lang, UT_PARAMS); void **tokens = get_translations(lang, UT_PARAMS);
critbit_tree *cb = (critbit_tree *)*tokens; critbit_tree *cb = (critbit_tree *)*tokens;
if (!cb) { if (!cb) {
log_error("no parameters defined in locale %s", locale_name(lang)); log_warning("no parameters defined in locale %s", locale_name(lang));
} }
else if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { else if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
cb_get_kv(match, &i, sizeof(int)); cb_get_kv(match, &i, sizeof(int));

View File

@ -1324,10 +1324,18 @@ struct message *msg)
struct faction *region_get_owner(const struct region *r) struct faction *region_get_owner(const struct region *r)
{ {
assert(rule_region_owners()); if (r->land) {
if (r->land && r->land->ownership) { if (rule_region_owners()) {
if (r->land->ownership) {
return r->land->ownership->owner; return r->land->ownership->owner;
} }
}
else {
building *b = largestbuilding(r, cmp_castle_size, false);
unit * u = b ? building_owner(b) : NULL;
return u ? u->faction : NULL;
}
}
return NULL; return NULL;
} }

View File

@ -1,6 +1,8 @@
#include <platform.h> #include <platform.h>
#include "region.h" #include "region.h"
#include "building.h"
#include "unit.h"
#include "terrain.h" #include "terrain.h"
#include "item.h" #include "item.h"
@ -31,9 +33,29 @@ void test_terraform(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_region_get_owner(CuTest *tc) {
region *r;
building *b1, *b2;
unit *u1, *u2;
test_setup();
r = test_create_region(0, 0, 0);
b1 = test_create_building(r, NULL);
b2 = test_create_building(r, NULL);
b1->size = 5;
b2->size = 10;
u1 = test_create_unit(test_create_faction(0), r);
u_set_building(u1, b1);
u2 = test_create_unit(test_create_faction(0), r);
u_set_building(u2, b2);
CuAssertPtrEquals(tc, u2->faction, region_get_owner(r));
test_cleanup();
}
CuSuite *get_region_suite(void) CuSuite *get_region_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_terraform); SUITE_ADD_TEST(suite, test_terraform);
SUITE_ADD_TEST(suite, test_region_get_owner);
return suite; return suite;
} }

View File

@ -1538,16 +1538,6 @@ int prefix_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static cmp_building_cb get_cmp_region_owner(void)
{
if (rule_region_owners()) {
return &cmp_current_owner;
}
else {
return &cmp_wage;
}
}
int display_cmd(unit * u, struct order *ord) int display_cmd(unit * u, struct order *ord)
{ {
char token[128]; char token[128];
@ -1597,15 +1587,7 @@ int display_cmd(unit * u, struct order *ord)
break; break;
case P_REGION: case P_REGION:
if (!u->building) { if (u->faction != region_get_owner(r)) {
cmistake(u, ord, 145, MSG_EVENT);
break;
}
if (building_owner(u->building) != u) {
cmistake(u, ord, 148, MSG_EVENT);
break;
}
if (u->building != largestbuilding(r, get_cmp_region_owner(), false)) {
cmistake(u, ord, 147, MSG_EVENT); cmistake(u, ord, 147, MSG_EVENT);
break; break;
} }
@ -1892,16 +1874,7 @@ int name_cmd(struct unit *u, struct order *ord)
break; break;
case P_REGION: case P_REGION:
if (!b) { if (u->faction != region_get_owner(r)) {
cmistake(u, ord, 145, MSG_EVENT);
break;
}
if (building_owner(b) != u) {
cmistake(u, ord, 148, MSG_EVENT);
break;
}
if (b != largestbuilding(r, get_cmp_region_owner(), false)) {
cmistake(u, ord, 147, MSG_EVENT); cmistake(u, ord, 147, MSG_EVENT);
break; break;
} }

View File

@ -229,6 +229,11 @@ static void test_display_cmd(CuTest *tc) {
CuAssertPtrEquals(tc, NULL, u->display); CuAssertPtrEquals(tc, NULL, u->display);
free_order(ord); free_order(ord);
ord = create_order(K_DISPLAY, f->locale, "%s Hodor", LOC(f->locale, parameters[P_REGION]));
CuAssertIntEquals(tc, 0, display_cmd(u, ord));
CuAssertPtrEquals(tc, NULL, r->display);
free_order(ord);
test_cleanup(); test_cleanup();
} }
@ -812,10 +817,7 @@ static void test_name_region(CuTest *tc) {
f = u->faction; f = u->faction;
ord = create_order(K_NAME, f->locale, "%s Hodor", LOC(f->locale, parameters[P_REGION])); ord = create_order(K_NAME, f->locale, "%s Hodor", LOC(f->locale, parameters[P_REGION]));
name_cmd(u, ord); u_set_building(u, test_create_building(u->region, 0));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error145"));
u->building = test_create_building(u->region, 0);
name_cmd(u, ord); name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->region->land->name); CuAssertStrEquals(tc, "Hodor", u->region->land->name);
free_order(ord); free_order(ord);
@ -1060,7 +1062,7 @@ static void test_name_cmd(CuTest *tc) {
free_order(ord); free_order(ord);
ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", LOC(f->locale, parameters[P_BUILDING])); ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", LOC(f->locale, parameters[P_BUILDING]));
u->building = test_create_building(u->region, 0); u_set_building(u, test_create_building(u->region, 0));
name_cmd(u, ord); name_cmd(u, ord);
CuAssertStrEquals(tc, "Hodor", u->building->name); CuAssertStrEquals(tc, "Hodor", u->building->name);
free_order(ord); free_order(ord);
@ -1073,6 +1075,37 @@ static void test_name_cmd(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_name_cmd_2274(CuTest *tc) {
unit *u1, *u2, *u3;
faction *f;
region *r;
test_setup();
r = test_create_region(0, 0, 0);
u1 = test_create_unit(test_create_faction(0), r);
u2 = test_create_unit(test_create_faction(0), r);
u3 = test_create_unit(u2->faction, r);
u_set_building(u1, test_create_building(r, NULL));
u1->building->size = 10;
u_set_building(u2, test_create_building(r, NULL));
u2->building->size = 20;
f = u2->faction;
u2->thisorder = create_order(K_NAME, f->locale, "%s Heimat", LOC(f->locale, parameters[P_REGION]));
name_cmd(u2, u2->thisorder);
CuAssertStrEquals(tc, "Heimat", r->land->name);
f = u3->faction;
u3->thisorder = create_order(K_NAME, f->locale, "%s Hodor", LOC(f->locale, parameters[P_REGION]));
name_cmd(u3, u3->thisorder);
CuAssertStrEquals(tc, "Hodor", r->land->name);
f = u1->faction;
u1->thisorder = create_order(K_NAME, f->locale, "%s notallowed", LOC(f->locale, parameters[P_REGION]));
name_cmd(u1, u1->thisorder);
CuAssertStrEquals(tc, "Hodor", r->land->name);
test_cleanup();
}
static void test_ally_cmd(CuTest *tc) { static void test_ally_cmd(CuTest *tc) {
unit *u; unit *u;
faction * f; faction * f;
@ -1481,6 +1514,7 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_nmr_warnings); SUITE_ADD_TEST(suite, test_nmr_warnings);
SUITE_ADD_TEST(suite, test_ally_cmd); SUITE_ADD_TEST(suite, test_ally_cmd);
SUITE_ADD_TEST(suite, test_name_cmd); SUITE_ADD_TEST(suite, test_name_cmd);
SUITE_ADD_TEST(suite, test_name_cmd_2274);
SUITE_ADD_TEST(suite, test_ally_cmd_errors); SUITE_ADD_TEST(suite, test_ally_cmd_errors);
SUITE_ADD_TEST(suite, test_long_order_normal); SUITE_ADD_TEST(suite, test_long_order_normal);
SUITE_ADD_TEST(suite, test_long_order_none); SUITE_ADD_TEST(suite, test_long_order_none);

View File

@ -225,8 +225,13 @@ building * test_create_building(region * r, const building_type * btype)
{ {
building * b; building * b;
assert(r); assert(r);
b = new_building(btype ? btype : test_create_buildingtype("castle"), r, default_locale); if (!btype) {
b->size = b->type->maxsize > 0 ? b->type->maxsize : 1; building_type *bt_castle = test_create_buildingtype("castle");
bt_castle->protection = building_protection;
btype = bt_castle;
}
b = new_building(btype, r, default_locale);
b->size = btype->maxsize > 0 ? btype->maxsize : 1;
return b; return b;
} }