diff --git a/src/laws.c b/src/laws.c index 49b683b72..27ba72ca0 100644 --- a/src/laws.c +++ b/src/laws.c @@ -1721,12 +1721,11 @@ static int rename_cmd(unit * u, order * ord, char **s, const char *s2) return 0; } -static bool try_rename(unit *u, building *b, order *ord) { +static bool try_rename(unit *u, building *b, order *ord, bool foreign) { unit *owner = b ? building_owner(b) : NULL; - bool foreign = !(owner && owner->faction == u->faction); if (!b) { - cmistake(u, ord, u->building ? 6 : 145, MSG_EVENT); + cmistake(u, ord, foreign ? 6 : 145, MSG_EVENT); return false; } @@ -1753,8 +1752,7 @@ static bool try_rename(unit *u, building *b, order *ord) { "building region", b, u->region)); } } - } - if (owner && owner->faction != u->faction) { + } else if (u != owner) { cmistake(u, ord, 148, MSG_PRODUCE); return false; } @@ -1764,8 +1762,9 @@ static bool try_rename(unit *u, building *b, order *ord) { int rename_building(unit * u, order * ord, building * b, const char *name) { + /* FIXME nobody uses this! */ assert(name); - if (!try_rename(u, b, ord)) { + if (!try_rename(u, b, ord, false)) { return -1; } return rename_cmd(u, ord, &b->name, name); @@ -1806,7 +1805,7 @@ int name_cmd(struct unit *u, struct order *ord) if (foreign) { b = getbuilding(u->region); } - if (try_rename(u, b, ord)) { + if (try_rename(u, b, ord, foreign)) { s = &b->name; } break; diff --git a/src/laws.test.c b/src/laws.test.c index 4e0dd043c..8fe590fb6 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1165,6 +1165,10 @@ static void test_name_building(CuTest *tc) { building_setname(u->building, "Home"); building_set_owner(ux); name_cmd(u, ord); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error148")); + test_clear_messages(f); + building_set_owner(u); + name_cmd(u, ord); CuAssertPtrEquals(tc, NULL, test_find_messagetype(f->msgs, "error148")); CuAssertStrEquals(tc, "Hodor", u->building->name); test_clear_messages(f); @@ -1328,13 +1332,14 @@ static void test_name_cmd(CuTest *tc) { name_cmd(u, ord); CuAssertStrEquals(tc, "Hodor", u->ship->name); free_order(ord); - + ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", LOC(f->locale, parameters[P_BUILDING])); u_set_building(u, test_create_building(u->region, NULL)); + building_set_owner(u); name_cmd(u, ord); CuAssertStrEquals(tc, "Hodor", u->building->name); free_order(ord); - + ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", LOC(f->locale, parameters[P_REGION])); name_cmd(u, ord); CuAssertStrEquals(tc, "Hodor", u->region->land->name); @@ -1366,6 +1371,28 @@ static void test_name_foreign_cmd(CuTest *tc) { test_teardown(); } +static void test_name_foreign_owned_cmd(CuTest *tc) { + building *b; + faction *f, *f2; + region *r; + unit *u, *u2; + + test_setup(); + u = test_create_unit(f = test_create_faction(), r = test_create_plain(0, 0)); + u2 = test_create_unit(f2 = test_create_faction(), r); + b = test_create_building(u->region, NULL); + u2->building = b; + building_set_owner(u2); + + u->thisorder = create_order(K_NAME, f->locale, "%s %s %s Hodor", + LOC(f->locale, parameters[P_FOREIGN]), + LOC(f->locale, parameters[P_BUILDING]), + itoa36(b->no)); + name_cmd(u, u->thisorder); + CuAssertStrEquals(tc, "Hodor", b->name); + test_teardown(); +} + static void test_name_cmd_2274(CuTest *tc) { unit *u1, *u2, *u3; faction *f; @@ -2451,6 +2478,7 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_ally_cmd); SUITE_ADD_TEST(suite, test_name_cmd); SUITE_ADD_TEST(suite, test_name_foreign_cmd); + SUITE_ADD_TEST(suite, test_name_foreign_owned_cmd); SUITE_ADD_TEST(suite, test_banner_cmd); SUITE_ADD_TEST(suite, test_email_cmd); SUITE_ADD_TEST(suite, test_name_cmd_2274);