diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 28e95b941..95c689ded 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -494,6 +494,10 @@ attrib_type at_alias = { NO_READ }; +/** remember old unit.no (for the creport, mostly) + * if alias is positive, then this unit was a TEMP + * if alias is negative, then this unit has been RENUMBERed + */ int ualias(const unit * u) { attrib *a = a_find(u->attribs, &at_alias); @@ -1716,7 +1720,7 @@ void renumber_unit(unit *u, int no) { uunhash(u); if (!ualias(u)) { attrib *a = a_add(&u->attribs, a_new(&at_alias)); - a->data.i = -u->no; // TODO: why is the alias negative? confusing! + a->data.i = -u->no; } u->no = no; uhash(u); diff --git a/src/renumber.c b/src/renumber.c index 259dfcc2b..de2b5a490 100644 --- a/src/renumber.c +++ b/src/renumber.c @@ -36,7 +36,6 @@ void renumber_factions(void) attrib *a = a_find(f->attribs, &at_number); int want; struct renum **rn; - faction *old; if (!a) continue; @@ -45,12 +44,6 @@ void renumber_factions(void) ADDMSG(&f->msgs, msg_message("renumber_twice", "id", want)); continue; } - old = findfaction(want); - if (old) { - a_remove(&f->attribs, a); - ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); - continue; - } if (!faction_id_is_unused(want)) { a_remove(&f->attribs, a); ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); @@ -151,7 +144,7 @@ int renumber_cmd(unit * u, order * ord) cmistake(u, ord, 114, MSG_EVENT); break; } - if (findship(i) || findbuilding(i)) { + if (findship(i)) { cmistake(u, ord, 115, MSG_EVENT); break; } @@ -180,7 +173,7 @@ int renumber_cmd(unit * u, order * ord) cmistake(u, ord, 114, MSG_EVENT); break; } - if (findship(i) || findbuilding(i)) { + if (findbuilding(i)) { cmistake(u, ord, 115, MSG_EVENT); break; } diff --git a/src/renumber.test.c b/src/renumber.test.c index 3fabaf4c1..9ab6b1e7b 100644 --- a/src/renumber.test.c +++ b/src/renumber.test.c @@ -29,6 +29,25 @@ static void test_renumber_faction(CuTest *tc) { test_cleanup(); } +static void test_renumber_faction_duplicate(CuTest *tc) { + unit *u; + faction *f, *f2; + int no; + const struct locale *lang; + + test_setup_ex(tc); + f2 = test_create_faction(0); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + no = f->no; + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s %s", LOC(lang, parameters[P_FACTION]), itoa36(f2->no)); + renumber_cmd(u, u->thisorder); + renumber_factions(); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "renumber_inuse")); + CuAssertIntEquals(tc, no, u->faction->no); + test_cleanup(); +} + static void test_renumber_building(CuTest *tc) { unit *u; int uno, no; @@ -46,6 +65,26 @@ static void test_renumber_building(CuTest *tc) { test_cleanup(); } +static void test_renumber_building_duplicate(CuTest *tc) { + unit *u; + faction *f; + int uno, no; + const struct locale *lang; + + test_setup_ex(tc); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + u->building = test_create_building(u->region, 0); + uno = u->building->no; + u->building = test_create_building(u->region, 0); + no = u->building->no; + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s %s", LOC(lang, parameters[P_BUILDING]), itoa36(uno)); + renumber_cmd(u, u->thisorder); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error115")); + CuAssertIntEquals(tc, no, u->building->no); + test_cleanup(); +} + static void test_renumber_ship(CuTest *tc) { unit *u; int uno, no; @@ -63,6 +102,26 @@ static void test_renumber_ship(CuTest *tc) { test_cleanup(); } +static void test_renumber_ship_duplicate(CuTest *tc) { + unit *u; + faction *f; + int uno, no; + const struct locale *lang; + + test_setup_ex(tc); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + u->ship = test_create_ship(u->region, 0); + uno = u->ship->no; + u->ship = test_create_ship(u->region, 0); + no = u->ship->no; + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s %s", LOC(lang, parameters[P_SHIP]), itoa36(uno)); + renumber_cmd(u, u->thisorder); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error115")); + CuAssertIntEquals(tc, no, u->ship->no); + test_cleanup(); +} + static void test_renumber_unit(CuTest *tc) { unit *u; int uno, no; @@ -76,7 +135,62 @@ static void test_renumber_unit(CuTest *tc) { u->thisorder = create_order(K_NUMBER, lang, "%s %s", LOC(lang, parameters[P_UNIT]), itoa36(uno)); renumber_cmd(u, u->thisorder); CuAssertIntEquals(tc, uno, u->no); - CuAssertIntEquals(tc, -no, ualias(u)); // TODO: why is ualias negative? + CuAssertIntEquals(tc, -no, ualias(u)); + test_cleanup(); +} + +static void test_renumber_unit_duplicate(CuTest *tc) { + unit *u, *u2; + faction *f; + int no; + const struct locale *lang; + + test_setup_ex(tc); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + no = u->no; + u2 = test_create_unit(f, u->region); + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s %s", LOC(lang, parameters[P_UNIT]), itoa36(u2->no)); + renumber_cmd(u, u->thisorder); + CuAssertIntEquals(tc, no, u->no); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error115")); + CuAssertIntEquals(tc, 0, ualias(u)); + test_cleanup(); +} + +static void test_renumber_unit_limit(CuTest *tc) { + unit *u; + faction *f; + int no; + const struct locale *lang; + + test_setup_ex(tc); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + no = u->no; + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s 10000", LOC(lang, parameters[P_UNIT])); + renumber_cmd(u, u->thisorder); + CuAssertIntEquals(tc, no, u->no); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error114")); + CuAssertIntEquals(tc, 0, ualias(u)); + test_cleanup(); +} + +static void test_renumber_unit_invalid(CuTest *tc) { + unit *u; + faction *f; + int no; + const struct locale *lang; + + test_setup_ex(tc); + u = test_create_unit(f = test_create_faction(0), test_create_region(0, 0, 0)); + no = u->no; + lang = f->locale; + u->thisorder = create_order(K_NUMBER, lang, "%s TEMP", LOC(lang, parameters[P_UNIT])); + renumber_cmd(u, u->thisorder); + CuAssertIntEquals(tc, no, u->no); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error116")); + CuAssertIntEquals(tc, 0, ualias(u)); test_cleanup(); } @@ -84,8 +198,14 @@ CuSuite *get_renumber_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_renumber_unit); + SUITE_ADD_TEST(suite, test_renumber_unit_limit); + SUITE_ADD_TEST(suite, test_renumber_unit_duplicate); + SUITE_ADD_TEST(suite, test_renumber_unit_invalid); SUITE_ADD_TEST(suite, test_renumber_building); + SUITE_ADD_TEST(suite, test_renumber_building_duplicate); SUITE_ADD_TEST(suite, test_renumber_ship); + SUITE_ADD_TEST(suite, test_renumber_ship_duplicate); SUITE_ADD_TEST(suite, test_renumber_faction); + SUITE_ADD_TEST(suite, test_renumber_faction_duplicate); return suite; }