From 198f084f5dbb2cd9cec986b11a05518532fcc1a8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 27 Dec 2017 21:54:09 +0100 Subject: [PATCH] implement speedy ships, with unit tests and edge testing. --- scripts/tests/e2/ships.lua | 9 ++------- src/kernel/ship.c | 24 ++++++++++++++++++++---- src/kernel/ship.test.c | 15 +++++++++++++-- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index 625e62f64..ba410a56b 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -58,25 +58,20 @@ function test_speedy_ship_slow() u2:set_skill("sailing", 24) -- sumskill = 50 u1.name = "XXX" u1:add_order("NACH O O O O O O O O O O") - print(u1.region) process_orders() - print(u1.region) assert_equal(5, u1.region.x) end function test_speedy_ship_fast() local r1 = region.create(0, 0, 'ocean') local f = faction.create("human") - f.name="Vikings" local u1 = unit.create(f, r1, 1) - u1.name = "Hagar" for x = 1, 10 do region.create(x, 0, 'ocean') end u1.ship = ship.create(r1, "dragonship") - u1:set_skill("sailing", 64) -- cptskill = 2^6 + u1:set_skill("sailing", 54) -- cptskill = 2*3^3 u1:add_order("NACH O O O O O O O O O O") process_orders() - print(f, get_turn()) - assert_equal(10, u1.region.x) + assert_equal(8, u1.region.x) end diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 88eb5c39f..400e05a9d 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -288,12 +288,28 @@ const char *write_shipname(const ship * sh, char *ibuf, size_t size) static int ShipSpeedBonus(const unit * u) { - int level = config_get_int("movement.shipspeed.skillbonus", 0); - if (level > 0) { - ship *sh = u->ship; + const ship * sh = u->ship; + static int config; + static int bonus; + + if (config_changed(&config)) { + bonus = config_get_int("movement.shipspeed.skillbonus", 0); + } + if (bonus > 0) { int skl = effskill(u, SK_SAILING, 0); int minsk = (sh->type->cptskill + 1) / 2; - return (skl - minsk) / level; + return (skl - minsk) / bonus; + } + else if (sh->type->flags & SFL_SPEEDY) { + int base = 3; + int speed = 0; + int minsk = sh->type->cptskill * base; + int skl = effskill(u, SK_SAILING, 0); + while (skl >= minsk) { + ++speed; + minsk *= base; + } + return speed; } return 0; } diff --git a/src/kernel/ship.test.c b/src/kernel/ship.test.c index 280c2122a..f36ce28e2 100644 --- a/src/kernel/ship.test.c +++ b/src/kernel/ship.test.c @@ -438,7 +438,7 @@ static void test_shipspeed_speedy(CuTest *tc) { unit *cap, *crw; test_setup(); stype = test_create_shiptype("dragonship"); - stype->range = 2; + stype->range = 5; stype->range_max = -1; stype->flags |= SFL_SPEEDY; cap = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); @@ -449,7 +449,18 @@ static void test_shipspeed_speedy(CuTest *tc) { set_level(cap, SK_SAILING, stype->cptskill); set_level(crw, SK_SAILING, stype->sumskill - stype->cptskill); CuAssertPtrEquals(tc, cap, ship_owner(sh)); - CuAssertIntEquals(tc, 2, shipspeed(sh, cap)); + CuAssertIntEquals(tc, 5, shipspeed(sh, cap)); + + set_level(cap, SK_SAILING, stype->cptskill * 3 - 1); + CuAssertIntEquals(tc, 5, shipspeed(sh, cap)); + set_level(cap, SK_SAILING, stype->cptskill * 3); + CuAssertIntEquals(tc, 6, shipspeed(sh, cap)); + + set_level(cap, SK_SAILING, stype->cptskill * 3 * 3 - 1); + CuAssertIntEquals(tc, 6, shipspeed(sh, cap)); + set_level(cap, SK_SAILING, stype->cptskill * 3 * 3); + CuAssertIntEquals(tc, 7, shipspeed(sh, cap)); + test_teardown(); }