diff --git a/src/creport.test.c b/src/creport.test.c index 21a6c09b7..079220738 100644 --- a/src/creport.test.c +++ b/src/creport.test.c @@ -1,6 +1,7 @@ #include #include "creport.h" #include "move.h" +#include "spy.h" #include "travelthru.h" #include "keyword.h" @@ -48,9 +49,59 @@ static void test_cr_unit(CuTest *tc) { test_cleanup(); } +static int cr_get_int(stream *strm, const char *match, int def) +{ + char line[1024]; + + strm->api->rewind(strm->handle); + while (strm->api->readln(strm->handle, line, sizeof(line))==0) { + if (strstr(line, match)) { + return atoi(line); + } + } + return def; +} + +static void test_cr_factionstealth(CuTest *tc) { + stream strm; + faction *f1, *f2, *fr; + region *r; + unit *u; + + test_setup(); + mstream_init(&strm); + f1 = test_create_faction(0); + f2 = test_create_faction(0); + fr = test_create_faction(0); + r = test_create_region(0, 0, 0); + u = test_create_unit(f1, r); + + cr_output_unit(&strm, u->region, f1, u, seen_unit); + CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); + CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); + CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); + + set_factionstealth(u, f2); + CuAssertPtrNotNull(tc, u->attribs); + + cr_output_unit(&strm, u->region, f1, u, seen_unit); + CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); + CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); + CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); + + cr_output_unit(&strm, u->region, f2, u, seen_unit); + CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); + CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); + CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Verraeter", -1)); + + mstream_done(&strm); + test_cleanup(); +} + CuSuite *get_creport_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_cr_unit); + SUITE_ADD_TEST(suite, test_cr_factionstealth); return suite; } diff --git a/src/spy.c b/src/spy.c index d4082cad7..c9667f7ee 100644 --- a/src/spy.c +++ b/src/spy.c @@ -178,7 +178,7 @@ int spy_cmd(unit * u, struct order *ord) return 0; } -void set_factionstealth(unit * u, faction * f) +static bool can_set_factionstealth(const unit * u, const faction * f) { region *lastr = NULL; /* for all units mu of our faction, check all the units in the region @@ -194,7 +194,7 @@ void set_factionstealth(unit * u, faction * f) faction *fv = visible_faction(f, ru); if (fv == f) { if (cansee(f, lastr, ru, 0)) - break; + return true; } } ru = ru->next; @@ -204,13 +204,15 @@ void set_factionstealth(unit * u, faction * f) } mu = mu->nextF; } - if (mu != NULL) { - attrib *a = a_find(u->attribs, &at_otherfaction); - if (!a) - a = a_add(&u->attribs, make_otherfaction(f)); - else - a->data.v = f; - } + return true; +} + +void set_factionstealth(unit *u, faction *f) { + attrib *a = a_find(u->attribs, &at_otherfaction); + if (!a) + a = a_add(&u->attribs, make_otherfaction(f)); + else + a->data.v = f; } int setstealth_cmd(unit * u, struct order *ord) @@ -315,7 +317,7 @@ int setstealth_cmd(unit * u, struct order *ord) } else { struct faction *f = findfaction(nr); - if (f == NULL) { + if (f == NULL || !can_set_factionstealth(u, f)) { cmistake(u, ord, 66, MSG_EVENT); break; } diff --git a/src/spy.h b/src/spy.h index d3ee0c728..665595ef4 100644 --- a/src/spy.h +++ b/src/spy.h @@ -26,12 +26,15 @@ extern "C" { struct region; struct strlist; struct order; + struct faction; - extern int setstealth_cmd(struct unit *u, struct order *ord); - extern int spy_cmd(struct unit *u, struct order *ord); - extern int sabotage_cmd(struct unit *u, struct order *ord); - extern void spy_message(int spy, const struct unit *u, + int setstealth_cmd(struct unit *u, struct order *ord); + int spy_cmd(struct unit *u, struct order *ord); + int sabotage_cmd(struct unit *u, struct order *ord); + void spy_message(int spy, const struct unit *u, const struct unit *target); + void set_factionstealth(struct unit * u, struct faction * f); + #define OCEAN_SWIMMER_CHANCE 0.1 #define CANAL_SWIMMER_CHANCE 0.9 diff --git a/src/spy.test.c b/src/spy.test.c index ae1550689..0dfaada7a 100644 --- a/src/spy.test.c +++ b/src/spy.test.c @@ -50,14 +50,6 @@ static void test_simple_spy_message(CuTest *tc) { test_cleanup(); } -static void set_factionstealth(unit *u, faction *f) { - attrib *a = a_find(u->attribs, &at_otherfaction); - if (!a) - a = a_add(&u->attribs, make_otherfaction(f)); - else - a->data.v = f; -} - static void test_all_spy_message(CuTest *tc) { spy_fixture fix; item_type *itype;