Skip to content

Commit

Permalink
Bei GIB EINHEIT die Tarnung als eigene Partei löschen. (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
ennorehling authored Feb 10, 2024
1 parent 9527301 commit 83f4db9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/give.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/* attributes includes */
#include <attributes/racename.h>
#include <attributes/otherfaction.h>

/* kernel includes */
#include <kernel/attrib.h>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions src/give.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "contact.h"
#include "eressea.h"

#include "attributes/otherfaction.h"

#include <kernel/ally.h>
#include <kernel/config.h>
#include <kernel/faction.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 83f4db9

Please sign in to comment.