diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index 27e4d9b26..ad4e3656c 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -100,7 +100,6 @@ function test_ship_convoy_capacity() u:add_order('NACH W') u:add_item('jewel', 39) - u.name = 'Xolgrim' u.ship.number = 2 u.number = 2 u:set_skill('sailing', 2, true) @@ -146,7 +145,7 @@ function test_ship_convoy_skill() local r3 = region.create(3, 0, 'ocean') local f = faction.create("human") local u = unit.create(f, r1, 1) - + u:set_skill('sailing', 2, true) u.ship = ship.create(r1, 'boat') assert_equal(1, u.ship.number) @@ -155,9 +154,9 @@ function test_ship_convoy_skill() assert_equal(r2, u.region) u.ship.number = 2 - u:set_skill('sailing', 2, true) + u:set_skill('sailing', 4, true) process_orders() - assert_equal(r2, u.region) + assert_equal(r2, u.region) -- not enough captains u.number = 2 u:set_skill('sailing', 2, true) diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 67b98d37c..ca2da27b9 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -332,7 +332,7 @@ int shipspeed(const ship * sh, const unit * u) bonus = ShipSpeedBonus(u); if (bonus > 0 && sh->type->range_max > sh->type->range) { - int crew = crew_skill(sh, NULL); + int crew = crew_skill(sh); int crew_bonus = (crew / sh->type->sumskill / 2) - 1; if (crew_bonus > 0) { int sbonus = sh->type->range_max - sh->type->range; @@ -387,31 +387,41 @@ int enoughsailors(const ship * sh, int crew_skill) return crew_skill >= sh->type->sumskill * sh->number; } -int crew_skill(const ship *sh, int *o_captains) { - int n = 0, captains = 0; +int crew_skill(const ship *sh) { + int n = 0; unit *u; for (u = sh->region->units; u; u = u->next) { if (u->ship == sh) { int es = effskill(u, SK_SAILING, NULL); - if (es >= sh->type->cptskill) { - captains += u->number; - } if (es >= sh->type->minskill) { n += es * u->number; } } } - if (o_captains) { - *o_captains = captains; - } return n; } -bool ship_crewed(const ship *sh) -{ - int num_caps, crew = crew_skill(sh, &num_caps); - return num_caps >= sh->number && enoughsailors(sh, crew); +bool ship_crewed(const ship *sh) { + unit *u; + int capskill = -1, sumskill = 0; + for (u = sh->region->units; u; u = u->next) { + if (u->ship == sh) { + int es = effskill(u, SK_SAILING, NULL); + if (capskill < 0) { + if (u->number >= sh->number) { + capskill = es; + } + else { + capskill = 0; + } + } + if (es >= sh->type->minskill) { + sumskill += es * u->number; + } + } + } + return (capskill >= ship_captain_minskill(sh)) && (sumskill >= sh->type->sumskill * sh->number); } int ship_capacity(const ship * sh) diff --git a/src/kernel/ship.h b/src/kernel/ship.h index b82a69140..34230f55b 100644 --- a/src/kernel/ship.h +++ b/src/kernel/ship.h @@ -118,7 +118,7 @@ extern "C" { int shipspeed(const struct ship *sh, const struct unit *u); bool ship_crewed(const struct ship *sh); - int crew_skill(const struct ship *sh, int *num_captains); + int crew_skill(const struct ship *sh); int ship_captain_minskill(const struct ship *sh); int ship_damage_percent(const struct ship *sh); diff --git a/src/kernel/ship.test.c b/src/kernel/ship.test.c index 18d8a1754..062e7cd20 100644 --- a/src/kernel/ship.test.c +++ b/src/kernel/ship.test.c @@ -641,21 +641,21 @@ static void test_crew_skill(CuTest *tc) { CuAssertIntEquals(tc, 1, stype->minskill); r = test_create_ocean(0, 0); sh = test_create_ship(r, stype); - CuAssertIntEquals(tc, 0, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 0, crew_skill(sh)); u = test_create_unit(test_create_faction(NULL), r); set_level(u, SK_SAILING, 1); - CuAssertIntEquals(tc, 0, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 0, crew_skill(sh)); u_set_ship(u, sh); set_level(u, SK_SAILING, 1); - CuAssertIntEquals(tc, 1, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 1, crew_skill(sh)); set_number(u, 10); - CuAssertIntEquals(tc, 10, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 10, crew_skill(sh)); stype->minskill = 2; - CuAssertIntEquals(tc, 0, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 0, crew_skill(sh)); set_level(u, SK_SAILING, 2); - CuAssertIntEquals(tc, 20, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 20, crew_skill(sh)); set_level(u, SK_SAILING, 3); - CuAssertIntEquals(tc, 30, crew_skill(sh, NULL)); + CuAssertIntEquals(tc, 30, crew_skill(sh)); test_teardown(); }