From 83f4db9511f9953db4e24e95ff145eb657b216f5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 10 Feb 2024 19:01:26 +0100 Subject: [PATCH] =?UTF-8?q?Bei=20GIB=20EINHEIT=20die=20Tarnung=20als=20eig?= =?UTF-8?q?ene=20Partei=20l=C3=B6schen.=20(#1055)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/give.c | 16 +++++++++++++--- src/give.test.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/give.c b/src/give.c index 2ead3600c..4b4b94ebc 100644 --- a/src/give.c +++ b/src/give.c @@ -9,6 +9,7 @@ /* attributes includes */ #include +#include /* kernel includes */ #include @@ -591,6 +592,17 @@ int give_unit_allowed(const unit * u) return 0; } +static void transfer_unit(unit *u, unit *u2) +{ + faction *f = get_otherfaction(u); + if (f == u2->faction) { + set_otherfaction(u, NULL); + } + u_setfaction(u, u2->faction); + u_freeorders(u); + u2->faction->newbies += u->number; +} + void give_unit(unit * u, unit * u2, order * ord) { int err, maxt = max_transfers(); @@ -714,9 +726,7 @@ void give_unit(unit * u, unit * u2, order * ord) return; } add_give_person(u, u2, u->number, ord, 0); - u_setfaction(u, u2->faction); - u_freeorders(u); - u2->faction->newbies += u->number; + transfer_unit(u, u2); } bool can_give_to(unit *u, unit *u2) { diff --git a/src/give.test.c b/src/give.test.c index 07d275489..b00acca26 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -3,6 +3,8 @@ #include "contact.h" #include "eressea.h" +#include "attributes/otherfaction.h" + #include #include #include @@ -129,6 +131,48 @@ static void test_give_unit(CuTest * tc) { test_teardown(); } +static void test_give_unit_stealth(CuTest * tc) { + struct give env = { 0 }; + + test_setup_ex(tc); + env.f1 = test_create_faction(); + env.f2 = test_create_faction(); + setup_give(&env); + set_otherfaction(env.src, env.dst->faction); + CuAssertPtrEquals(tc, env.dst->faction, get_otherfaction(env.src)); + CuAssertIntEquals(tc, 1, env.f1->num_units); + CuAssertIntEquals(tc, 1, env.f2->num_units); + join_group(env.src, "group"); + + config_set("rules.give.max_men", "0"); + give_unit(env.src, env.dst, NULL); + CuAssertPtrEquals(tc, env.f1, env.src->faction); + CuAssertIntEquals(tc, 0, env.f2->newbies); + + config_set("rules.give.max_men", "-1"); + give_unit(env.src, env.dst, NULL); + CuAssertPtrEquals(tc, env.f2, env.src->faction); + CuAssertPtrEquals(tc, NULL, get_group(env.src)); + CuAssertIntEquals(tc, 1, env.f2->newbies); + CuAssertPtrEquals(tc, NULL, env.f1->units); + CuAssertPtrEquals(tc, NULL, get_otherfaction(env.src)); + CuAssertPtrNotNull(tc, test_find_messagetype(env.f1->msgs, "give_person")); + CuAssertPtrNotNull(tc, test_find_messagetype(env.f2->msgs, "receive_person")); + + /* must be allied to transfer a unit */ + u_setfaction(env.src, env.f1); + ally_set(&env.f2->allies, env.f1, 0); + give_unit(env.src, env.dst, NULL); + CuAssertPtrEquals(tc, env.f1, env.src->faction); + + /* contact also works */ + contact_unit(env.dst, env.src); + give_unit(env.src, env.dst, NULL); + CuAssertPtrEquals(tc, env.f2, env.src->faction); + + test_teardown(); +} + static void test_give_unit_humans(CuTest * tc) { struct give env = { 0 }; race *rc; @@ -851,6 +895,7 @@ CuSuite *get_give_suite(void) SUITE_ADD_TEST(suite, test_give_men_hungry); SUITE_ADD_TEST(suite, test_give_men_cursed); SUITE_ADD_TEST(suite, test_give_unit); + SUITE_ADD_TEST(suite, test_give_unit_stealth); SUITE_ADD_TEST(suite, test_give_unit_humans); SUITE_ADD_TEST(suite, test_give_unit_cursed); SUITE_ADD_TEST(suite, test_give_unit_other_race);