GIB alle Schiffe an andere Einheit.

This commit is contained in:
Enno Rehling 2019-10-08 04:52:41 +02:00
parent 4052cb71ff
commit b0e6ed430b
2 changed files with 75 additions and 25 deletions

View File

@ -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")

View File

@ -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);
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);
}
}
}
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 (u2->ship->type != u->ship->type) {
return msg_error(u, ord, 322);
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 {
transfer_ships(u1->ship, u2->ship, n);
transfer_units(u1->ship, u2->ship);
}
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);
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(u->ship, sh, n);
transfer_ships(u1->ship, sh, n);
}
else {
return msg_error(u, ord, 233);
u_set_ship(u2, u1->ship);
ship_set_owner(u2);
}
}
if (u->ship->number < n) {
n = u->ship->number;
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) {