Fehler in ship_crewed Berechnung

This commit is contained in:
Enno Rehling 2019-10-27 10:56:16 +01:00
parent c82b3b27dd
commit c8769a4412
2 changed files with 58 additions and 8 deletions

View File

@ -407,22 +407,19 @@ int crew_skill(const ship *sh) {
} }
bool ship_crewed(const ship *sh) { bool ship_crewed(const ship *sh) {
unit *u; unit *u, *cap = ship_owner(sh);
int capskill = -1, sumskill = 0; int capskill = -1, sumskill = 0;
for (u = sh->region->units; u; u = u->next) { for (u = sh->region->units; u; u = u->next) {
if (u->ship == sh) { if (u->ship == sh) {
int es = effskill(u, SK_SAILING, NULL); int es = effskill(u, SK_SAILING, NULL);
if (capskill < 0) { if (es > 0) {
if (u->number >= sh->number) { if (u == cap && u->number >= sh->number) {
capskill = es; capskill = es;
} }
else { if (es >= sh->type->minskill) {
capskill = 0; sumskill += es * u->number;
} }
} }
if (es >= sh->type->minskill) {
sumskill += es * u->number;
}
} }
} }
return (capskill >= ship_captain_minskill(sh)) && (sumskill >= sh->type->sumskill * sh->number); return (capskill >= ship_captain_minskill(sh)) && (sumskill >= sh->type->sumskill * sh->number);

View File

@ -30,6 +30,58 @@ static void test_register_ship(CuTest * tc)
test_teardown(); test_teardown();
} }
static void test_ship_crewed(CuTest * tc)
{
struct region *r;
struct faction *f;
struct ship *sh;
struct unit *u1, *u2;
struct ship_type *stype;
test_setup();
f = test_create_faction(NULL);
r = test_create_ocean(0, 0);
stype = test_create_shiptype("longboat");
stype->cptskill = 2;
stype->sumskill = 4;
sh = test_create_ship(r, stype);
CuAssertTrue(tc, !ship_crewed(sh));
u1 = test_create_unit(f, r);
set_level(u1, SK_SAILING, 4);
u_set_ship(u1, sh);
CuAssertTrue(tc, ship_crewed(sh));
u2 = test_create_unit(f, r);
set_level(u1, SK_SAILING, 2);
set_level(u1, SK_SAILING, 2);
u_set_ship(u2, sh);
CuAssertTrue(tc, ship_crewed(sh));
set_level(u1, SK_SAILING, 1);
set_level(u2, SK_SAILING, 2);
CuAssertTrue(tc, !ship_crewed(sh));
set_level(u1, SK_SAILING, 2);
set_level(u2, SK_SAILING, 1);
CuAssertTrue(tc, !ship_crewed(sh));
set_level(u1, SK_SAILING, 3);
set_level(u2, SK_SAILING, 1);
CuAssertTrue(tc, ship_crewed(sh));
stype->minskill = 2;
CuAssertTrue(tc, !ship_crewed(sh));
set_level(u1, SK_SAILING, 2);
set_level(u2, SK_SAILING, 2);
CuAssertTrue(tc, ship_crewed(sh));
sh->number = 2;
CuAssertTrue(tc, !ship_crewed(sh));
set_level(u1, SK_SAILING, 4);
set_level(u2, SK_SAILING, 4);
CuAssertTrue(tc, !ship_crewed(sh));
u1->number = 2;
set_level(u1, SK_SAILING, 2);
set_level(u2, SK_SAILING, 4);
CuAssertTrue(tc, ship_crewed(sh));
test_teardown();
}
static void test_ship_set_owner(CuTest * tc) static void test_ship_set_owner(CuTest * tc)
{ {
struct region *r; struct region *r;
@ -665,6 +717,7 @@ CuSuite *get_ship_suite(void)
SUITE_ADD_TEST(suite, test_register_ship); SUITE_ADD_TEST(suite, test_register_ship);
SUITE_ADD_TEST(suite, test_stype_defaults); SUITE_ADD_TEST(suite, test_stype_defaults);
SUITE_ADD_TEST(suite, test_ship_set_owner); SUITE_ADD_TEST(suite, test_ship_set_owner);
SUITE_ADD_TEST(suite, test_ship_crewed);
SUITE_ADD_TEST(suite, test_shipowner_resets_when_empty); SUITE_ADD_TEST(suite, test_shipowner_resets_when_empty);
SUITE_ADD_TEST(suite, test_shipowner_goes_to_next_when_empty); SUITE_ADD_TEST(suite, test_shipowner_goes_to_next_when_empty);
SUITE_ADD_TEST(suite, test_shipowner_goes_to_other_when_empty); SUITE_ADD_TEST(suite, test_shipowner_goes_to_other_when_empty);