Skip to content

Commit

Permalink
bug 3054: allied defenders were not defending (#1098)
Browse files Browse the repository at this point in the history
* test: bug 3040, allies join to help their own faction

* failing test for bug 3054

* fix for bug 3054
allies were not counted as friends
  • Loading branch information
ennorehling authored Nov 24, 2024
1 parent 88e186a commit f589390
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/battle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3625,7 +3625,7 @@ static void join_ally(battle *b, const side *s, unit *u, fighter **cp)
}
}

static void join_allies(battle * b)
void join_allies(battle * b)
{
region *r = b->region;
unit *u;
Expand Down Expand Up @@ -3684,7 +3684,7 @@ static void join_allies(battle * b)
}
}

for (si = 0; si != num_sides; ++si) {
for (num_sides = arrlen(b->sides), si = 0; si != num_sides; ++si) {
side* s = b->sides[si];
size_t sei;
faction *f = s->bf->faction;
Expand Down Expand Up @@ -4119,7 +4119,6 @@ void force_leave(region *r, battle *b) {
}
}


static void do_battle(region * r) {
battle *b = NULL;
ship *sh;
Expand Down
1 change: 1 addition & 0 deletions src/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ void remove_troop(troop dt); /* not the same as the badly named rmtroop */
bool is_attacker(const fighter* fig);
struct battle* make_battle(struct region* r);
bool start_battle(struct region* r, struct battle** bp);
void join_allies(struct battle *b);
void free_battle(struct battle* b);
void init_tactics(struct battle* b);
struct fighter* make_fighter(struct battle* b, struct unit* u,
Expand Down
57 changes: 57 additions & 0 deletions src/battle.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "kernel/building.h"
#include "kernel/faction.h"
#include "kernel/curse.h"
#include "kernel/group.h"
#include "kernel/item.h"
#include "kernel/order.h"
#include "kernel/race.h"
Expand Down Expand Up @@ -1261,6 +1262,61 @@ static void test_start_battle(CuTest* tc) {
test_teardown();
}

static fighter *test_find_fighter(const battle *b, const unit *u)
{
size_t si, num_sides = arrlen(b->sides);

for (si = 0; si != num_sides; ++si) {
side *s = b->sides[si];
if (s->bf->faction == u->faction) {
fighter *fig;
for (fig = s->fighters; fig; fig = fig->next) {
if (fig->unit == u) {
return fig;
}
}
}
}
return NULL;
}

static void test_join_allies(CuTest *tc) {
battle *b = NULL;
unit *u1, *u2, *u3;
region *r;
fighter *f1, *f2, *f3;

test_setup();
r = test_create_plain(0, 0);
u1 = test_create_unit(test_create_faction(), r);
u2 = test_create_unit(test_create_faction(), r);
u3 = test_create_unit(u2->faction, r);
join_group(u3, "Fools");

unit_setstatus(u1, ST_FIGHT);
unit_setstatus(u2, ST_FLEE);
unit_setstatus(u3, ST_FIGHT);
unit_addorder(u1, create_order(K_ATTACK, u1->faction->locale, itoa36(u2->no)));
CuAssertTrue(tc, start_battle(r, &b));
CuAssertPtrNotNull(tc, f1 = test_find_fighter(b, u1));
CuAssertPtrNotNull(tc, f2 = test_find_fighter(b, u2));
CuAssertIntEquals(tc, E_ENEMY|E_ATTACKING, get_relation(f1->side, f2->side));
CuAssertIntEquals(tc, E_ENEMY, get_relation(f2->side, f1->side));
CuAssertTrue(tc, f1->side != f2->side);
CuAssertPtrEquals(tc, NULL, test_find_fighter(b, u3));
join_allies(b);
CuAssertPtrNotNull(tc, f3 = test_find_fighter(b, u3));
CuAssertTrue(tc, f3->side != f2->side);
CuAssertTrue(tc, f3->side != f1->side);
CuAssertIntEquals(tc, E_FRIEND, get_relation(f3->side, f2->side));
CuAssertIntEquals(tc, E_FRIEND, get_relation(f2->side, f3->side));
CuAssertIntEquals(tc, E_ENEMY, get_relation(f3->side, f1->side));
CuAssertIntEquals(tc, E_ENEMY, get_relation(f1->side, f3->side));

free_battle(b);
test_teardown();
}

static void test_battle_leaders(CuTest* tc) {
region* r;
faction* f;
Expand Down Expand Up @@ -1484,6 +1540,7 @@ CuSuite *get_battle_suite(void)
DISABLE_TEST(suite, test_drain_exp);
SUITE_ADD_TEST(suite, test_make_battle);
SUITE_ADD_TEST(suite, test_start_battle);
SUITE_ADD_TEST(suite, test_join_allies);
SUITE_ADD_TEST(suite, test_battle_leaders);
SUITE_ADD_TEST(suite, test_get_tactics);
SUITE_ADD_TEST(suite, test_get_unitrow);
Expand Down

0 comments on commit f589390

Please sign in to comment.