From b0e6ed430ba4aace08d147dc43df5453fb55ff9b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 8 Oct 2019 04:52:41 +0200 Subject: [PATCH] GIB alle Schiffe an andere Einheit. --- scripts/tests/e2/ships.lua | 33 +++++++++++++++++-- src/give.c | 67 +++++++++++++++++++++++++------------- 2 files changed, 75 insertions(+), 25 deletions(-) diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index 1e101b5b2..c71cf9eb3 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -191,7 +191,21 @@ function test_give_ship_merge() assert_equal(2, u2.ship.number) end -function test_give_ship_max() +function test_give_ship_only_same() + local r = region.create(1, 0, 'ocean') + local f = faction.create("human") + local u1 = unit.create(f, r, 1) + local u2 = unit.create(f, r, 1) + u2.ship = ship.create(r, 'boat') + u1.ship = ship.create(r, 'longboat') + u1.ship.number = 2 + u1:add_order("GIB " .. itoa36(u2.id) .. " 1 SCHIFF") + process_orders() + assert_equal(2, u1.ship.number) + assert_equal(1, u2.ship.number) +end + +function test_give_ship_scale() local r = region.create(1, 0, 'plain') local f = faction.create("human") local u1 = unit.create(f, r, 1) @@ -201,7 +215,7 @@ function test_give_ship_max() sh.damage = 9 sh.size = 12 u1.ship = sh - u1:add_order("GIB " .. itoa36(u2.id) .. " 4 SCHIFF") + u1:add_order("GIB " .. itoa36(u2.id) .. " 2 SCHIFF") process_orders() assert_equal(1, u1.ship.number) assert_equal(3, u1.ship.damage) @@ -211,6 +225,21 @@ function test_give_ship_max() assert_equal(8, u2.ship.size) end +function test_give_ship_all_ships() + local r = region.create(1, 0, 'plain') + local f = faction.create("human") + local u1 = unit.create(f, r, 1) + local u2 = unit.create(f, r, 1) + u1.ship = ship.create(r, 'boat') + u1.ship.number = 2 + u2.ship = ship.create(r, 'boat') + u2.ship.number = 1 + u1:add_order("GIB " .. itoa36(u2.id) .. " 2 SCHIFF") + process_orders() + assert_equal(3, u2.ship.number) + assert_equal(u2.ship, u1.ship) +end + function test_give_ship_self_only() local r = region.create(1, 0, 'plain') local f1 = faction.create("human") diff --git a/src/give.c b/src/give.c index 52b017448..582532fe1 100644 --- a/src/give.c +++ b/src/give.c @@ -295,40 +295,61 @@ bool rule_transfermen(void) static void transfer_ships(ship *s1, ship *s2, int n) { - assert(n < s1->number); + assert(n <= s1->number); s2->damage += s1->damage * n / s1->number; s2->size += s1->size * n / s1->number; s2->number += n; scale_ship(s1, s1->number - n); } -message * give_ship(unit *u, unit *u2, int n, order *ord) +static void transfer_units(ship *s1, ship *s2) { - assert(u->ship); - assert(n > 0 && n < u->ship->number); - if (u->faction != u2->faction) { - return msg_error(u, ord, 321); - } - if (u2->ship) { - if (u2->ship->type != u->ship->type) { - return msg_error(u, ord, 322); + region * r = s1->region; + unit *u; + for (u = r->units; u; u = u->next) { + if (u->ship == s1) { + leave_ship(u); + u_set_ship(u, s2); } - transfer_ships(u->ship, u2->ship, n); } - else{ - if (fval(u_race(u2), RCF_CANSAIL)) { - ship * sh = new_ship(u->ship->type, u->region, u->faction->locale); - scale_ship(sh, 0); - u_set_ship(u2, sh); - transfer_ships(u->ship, sh, n); +} + +message * give_ship(unit *u1, unit *u2, int n, order *ord) +{ + assert(u1->ship); + assert(n > 0 && n <= u1->ship->number); + if (u1->faction != u2->faction) { + return msg_error(u1, ord, 321); + } + /* TODO: when transferring all ships, unit must hop on the target ship */ + if (u2->ship) { + if (n < u1->ship->number) { + if (u2->ship->type != u1->ship->type) { + return msg_error(u1, ord, 322); + } + transfer_ships(u1->ship, u2->ship, n); } else { - return msg_error(u, ord, 233); + transfer_ships(u1->ship, u2->ship, n); + transfer_units(u1->ship, u2->ship); } - } - if (u->ship->number < n) { - n = u->ship->number; + else { + if (fval(u_race(u2), RCF_CANSAIL)) { + if (n < u1->ship->number) { + ship * sh = new_ship(u1->ship->type, u1->region, u1->faction->locale); + scale_ship(sh, 0); + u_set_ship(u2, sh); + transfer_ships(u1->ship, sh, n); + } + else { + u_set_ship(u2, u1->ship); + ship_set_owner(u2); + } + } + else { + return msg_error(u1, ord, 233); + } } return NULL; } @@ -860,8 +881,8 @@ void give_cmd(unit * u, order * ord) if (p == P_SHIP) { if (u->ship) { message * msg; - if (n >= u->ship->number) { - n = u->ship->number - 1; + if (n > u->ship->number) { + n = u->ship->number; } msg = give_ship(u, u2, n, ord); if (msg) {