2016-09-13 20:17:59 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "creport.h"
|
|
|
|
#include "move.h"
|
2017-03-01 14:59:20 +01:00
|
|
|
#include "spy.h"
|
2016-09-13 20:17:59 +02:00
|
|
|
#include "travelthru.h"
|
|
|
|
#include "keyword.h"
|
|
|
|
|
2017-03-01 19:18:00 +01:00
|
|
|
#include <kernel/ally.h>
|
2016-09-13 20:17:59 +02:00
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/race.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/spell.h>
|
|
|
|
#include <kernel/spellbook.h>
|
|
|
|
|
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/lists.h>
|
|
|
|
#include <util/message.h>
|
|
|
|
|
|
|
|
#include <stream.h>
|
|
|
|
#include <memstream.h>
|
|
|
|
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void test_cr_unit(CuTest *tc) {
|
|
|
|
stream strm;
|
|
|
|
char line[1024];
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
f = test_create_faction(0);
|
|
|
|
r = test_create_region(0, 0, 0);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
renumber_unit(u, 1234);
|
|
|
|
|
|
|
|
mstream_init(&strm);
|
|
|
|
cr_output_unit(&strm, r, f, u, seen_unit);
|
|
|
|
strm.api->rewind(strm.handle);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, line, "EINHEIT 1234");
|
|
|
|
mstream_done(&strm);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:18:09 +01:00
|
|
|
static void setup_resources(void) {
|
2017-03-05 15:51:04 +01:00
|
|
|
struct locale *lang;
|
|
|
|
|
|
|
|
test_setup();
|
2017-03-05 17:42:57 +01:00
|
|
|
init_resources();
|
2017-03-05 15:51:04 +01:00
|
|
|
lang = get_or_create_locale("de"); /* CR tags are translated from this */
|
|
|
|
locale_setstring(lang, "money", "Silber");
|
|
|
|
locale_setstring(lang, "money_p", "Silber");
|
|
|
|
locale_setstring(lang, "horse", "Pferd");
|
|
|
|
locale_setstring(lang, "horse_p", "Pferde");
|
|
|
|
locale_setstring(lang, "peasant", "Bauer");
|
|
|
|
locale_setstring(lang, "peasant_p", "Bauern");
|
2017-03-05 16:55:11 +01:00
|
|
|
locale_setstring(lang, "tree", "Blume");
|
|
|
|
locale_setstring(lang, "tree_p", "Blumen");
|
2017-03-05 15:51:04 +01:00
|
|
|
locale_setstring(lang, "sapling", "Schoessling");
|
|
|
|
locale_setstring(lang, "sapling_p", "Schoesslinge");
|
2017-03-10 23:18:09 +01:00
|
|
|
locale_setstring(lang, "mallornsapling", "Mallornschoessling");
|
|
|
|
locale_setstring(lang, "mallornsapling_p", "Mallornschoesslinge");
|
|
|
|
locale_setstring(lang, "mallorntree", "Mallorn");
|
|
|
|
locale_setstring(lang, "mallorntree_p", "Mallorn");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cr_resources(CuTest *tc) {
|
|
|
|
stream strm;
|
|
|
|
char line[1024];
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
|
|
|
|
setup_resources();
|
2017-03-05 15:26:04 +01:00
|
|
|
|
|
|
|
f = test_create_faction(0);
|
|
|
|
r = test_create_region(0, 0, 0);
|
2017-03-05 18:23:24 +01:00
|
|
|
r->land->horses = 1;
|
2017-03-05 15:26:04 +01:00
|
|
|
r->land->peasants = 200;
|
|
|
|
r->land->money = 300;
|
2017-03-05 15:51:04 +01:00
|
|
|
rsettrees(r, 0, 1);
|
|
|
|
rsettrees(r, 1, 2);
|
|
|
|
rsettrees(r, 2, 3);
|
2017-03-05 15:26:04 +01:00
|
|
|
|
|
|
|
mstream_init(&strm);
|
|
|
|
cr_output_resources(&strm, f, r, false);
|
|
|
|
strm.api->rewind(strm.handle);
|
2017-03-05 15:51:04 +01:00
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "3;Baeume", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "2;Schoesslinge", line);
|
|
|
|
|
2017-03-10 23:18:09 +01:00
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "\"Schoesslinge\";type", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "2;number", line);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "\"Blumen\";type", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "3;number", line);
|
|
|
|
|
2017-03-05 15:26:04 +01:00
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "\"Silber\";type", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "300;number", line);
|
2017-03-05 15:51:04 +01:00
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "\"Bauern\";type", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "200;number", line);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "\"Pferde\";type", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
2017-03-05 18:23:24 +01:00
|
|
|
CuAssertStrEquals(tc, "1;number", line);
|
2017-03-05 15:51:04 +01:00
|
|
|
|
2017-03-10 23:18:09 +01:00
|
|
|
mstream_done(&strm);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cr_mallorn(CuTest *tc) {
|
|
|
|
stream strm;
|
|
|
|
char line[1024];
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
|
|
|
|
setup_resources();
|
|
|
|
|
|
|
|
f = test_create_faction(0);
|
|
|
|
r = test_create_region(0, 0, 0);
|
|
|
|
r->land->horses = 1;
|
|
|
|
r->land->peasants = 200;
|
|
|
|
r->land->money = 300;
|
|
|
|
r->flags |= RF_MALLORN;
|
|
|
|
rsettrees(r, 0, 1);
|
|
|
|
rsettrees(r, 1, 2);
|
|
|
|
rsettrees(r, 2, 3);
|
|
|
|
|
|
|
|
mstream_init(&strm);
|
|
|
|
cr_output_resources(&strm, f, r, false);
|
|
|
|
strm.api->rewind(strm.handle);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "3;Baeume", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "2;Schoesslinge", line);
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "1;Mallorn", line);
|
|
|
|
|
2017-03-05 15:51:04 +01:00
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
2017-03-10 23:18:09 +01:00
|
|
|
CuAssertStrEquals(tc, "\"Mallornschoesslinge\";type", line);
|
2017-03-05 15:51:04 +01:00
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "2;number", line);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9));
|
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
2017-03-10 23:18:09 +01:00
|
|
|
CuAssertStrEquals(tc, "\"Mallorn\";type", line);
|
2017-03-05 15:51:04 +01:00
|
|
|
CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line)));
|
|
|
|
CuAssertStrEquals(tc, "3;number", line);
|
|
|
|
|
2017-03-05 15:26:04 +01:00
|
|
|
mstream_done(&strm);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2017-03-01 14:59:20 +01:00
|
|
|
static int cr_get_int(stream *strm, const char *match, int def)
|
|
|
|
{
|
|
|
|
char line[1024];
|
2017-03-07 19:30:52 +01:00
|
|
|
size_t len = strlen(match);
|
2017-03-01 14:59:20 +01:00
|
|
|
|
|
|
|
strm->api->rewind(strm->handle);
|
|
|
|
while (strm->api->readln(strm->handle, line, sizeof(line))==0) {
|
2017-03-07 19:30:52 +01:00
|
|
|
const char * pos = strstr(line, match);
|
|
|
|
if (pos && pos[len]=='\0') {
|
2017-03-01 14:59:20 +01:00
|
|
|
return atoi(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cr_factionstealth(CuTest *tc) {
|
|
|
|
stream strm;
|
2017-03-01 18:16:07 +01:00
|
|
|
faction *f1, *f2;
|
2017-03-01 14:59:20 +01:00
|
|
|
region *r;
|
|
|
|
unit *u;
|
2017-03-01 19:18:00 +01:00
|
|
|
ally *al;
|
2017-03-01 14:59:20 +01:00
|
|
|
|
|
|
|
test_setup();
|
|
|
|
f1 = test_create_faction(0);
|
|
|
|
f2 = test_create_faction(0);
|
|
|
|
r = test_create_region(0, 0, 0);
|
|
|
|
u = test_create_unit(f1, r);
|
|
|
|
|
2017-03-07 19:30:52 +01:00
|
|
|
/* report to ourselves */
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_init(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
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));
|
2017-03-07 19:30:52 +01:00
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1));
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_done(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
|
2017-03-07 19:30:52 +01:00
|
|
|
/* ... also when we are anonymous */
|
|
|
|
u->flags |= UFL_ANON_FACTION;
|
|
|
|
mstream_init(&strm);
|
|
|
|
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));
|
|
|
|
CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1));
|
|
|
|
u->flags &= ~UFL_ANON_FACTION;
|
|
|
|
mstream_done(&strm);
|
|
|
|
|
|
|
|
/* we see that our unit is cloaked */
|
2017-03-01 14:59:20 +01:00
|
|
|
set_factionstealth(u, f2);
|
|
|
|
CuAssertPtrNotNull(tc, u->attribs);
|
2017-03-07 19:30:52 +01:00
|
|
|
mstream_init(&strm);
|
|
|
|
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));
|
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1));
|
|
|
|
mstream_done(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
|
2017-03-07 19:30:52 +01:00
|
|
|
/* ... also when we are anonymous */
|
|
|
|
u->flags |= UFL_ANON_FACTION;
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_init(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
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));
|
2017-03-07 19:30:52 +01:00
|
|
|
CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1));
|
|
|
|
u->flags &= ~UFL_ANON_FACTION;
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_done(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
|
2017-03-07 19:30:52 +01:00
|
|
|
/* we can tell that someone is presenting as us */
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_init(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
cr_output_unit(&strm, u->region, f2, u, seen_unit);
|
2017-03-06 20:37:17 +01:00
|
|
|
CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Partei", -1));
|
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1));
|
2017-03-01 14:59:20 +01:00
|
|
|
CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Verraeter", -1));
|
2017-03-07 19:30:52 +01:00
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1));
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_done(&strm);
|
2017-03-01 14:59:20 +01:00
|
|
|
|
2017-03-07 19:30:52 +01:00
|
|
|
/* ... but not if they are anonymous */
|
|
|
|
u->flags |= UFL_ANON_FACTION;
|
|
|
|
mstream_init(&strm);
|
|
|
|
cr_output_unit(&strm, u->region, f2, u, seen_unit);
|
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Partei", -1));
|
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1));
|
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1));
|
|
|
|
CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1));
|
|
|
|
u->flags &= ~UFL_ANON_FACTION;
|
|
|
|
mstream_done(&strm);
|
|
|
|
|
|
|
|
/* we see the same thing as them when we are an ally */
|
2017-03-01 19:18:00 +01:00
|
|
|
al = ally_add(&f1->allies, f2);
|
|
|
|
al->status = HELP_FSTEALTH;
|
2017-03-06 20:37:17 +01:00
|
|
|
mstream_init(&strm);
|
2017-03-01 19:18:00 +01:00
|
|
|
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));
|
2017-03-06 20:37:17 +01:00
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1));
|
2017-03-07 19:30:52 +01:00
|
|
|
CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1));
|
|
|
|
mstream_done(&strm);
|
|
|
|
|
|
|
|
/* ... also when they are anonymous */
|
|
|
|
u->flags |= UFL_ANON_FACTION;
|
|
|
|
mstream_init(&strm);
|
2017-03-08 18:29:04 +01:00
|
|
|
cr_output_unit(&strm, u->region, f2, u, seen_unit);
|
2017-03-07 19:30:52 +01:00
|
|
|
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));
|
|
|
|
CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1));
|
|
|
|
u->flags &= ~UFL_ANON_FACTION;
|
2017-03-01 14:59:20 +01:00
|
|
|
mstream_done(&strm);
|
2017-03-06 20:37:17 +01:00
|
|
|
|
2017-03-01 14:59:20 +01:00
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2016-09-13 20:17:59 +02:00
|
|
|
CuSuite *get_creport_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_cr_unit);
|
2017-03-05 15:26:04 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_cr_resources);
|
2017-03-10 23:18:09 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_cr_mallorn);
|
2017-03-01 14:59:20 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_cr_factionstealth);
|
2016-09-13 20:17:59 +02:00
|
|
|
return suite;
|
|
|
|
}
|