2015-01-18 17:40:54 +01:00
|
|
|
#include <platform.h>
|
|
|
|
|
|
|
|
#include <magic.h>
|
2016-03-09 23:20:05 +01:00
|
|
|
#include <kernel/config.h>
|
2015-01-18 17:40:54 +01:00
|
|
|
#include <kernel/types.h>
|
2017-04-17 20:11:44 +02:00
|
|
|
#include <kernel/race.h>
|
2015-01-18 17:40:54 +01:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/faction.h>
|
2015-07-07 13:38:14 +02:00
|
|
|
#include <kernel/ship.h>
|
2015-07-07 09:29:43 +02:00
|
|
|
#include <kernel/order.h>
|
2015-01-18 17:40:54 +01:00
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/messages.h>
|
|
|
|
#include <util/attrib.h>
|
2015-07-07 09:29:43 +02:00
|
|
|
#include <util/language.h>
|
2015-01-18 17:40:54 +01:00
|
|
|
#include <util/message.h>
|
|
|
|
#include <util/crmessage.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
|
|
|
#include <attributes/otherfaction.h>
|
|
|
|
|
|
|
|
#include "spy.h"
|
|
|
|
|
2015-07-07 09:29:43 +02:00
|
|
|
#include <assert.h>
|
2015-01-18 17:40:54 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <CuTest.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
region *r;
|
|
|
|
unit *spy;
|
|
|
|
unit *victim;
|
|
|
|
} spy_fixture;
|
|
|
|
|
|
|
|
static void setup_spy(spy_fixture *fix) {
|
2018-05-18 19:58:49 +02:00
|
|
|
mt_create_va(mt_new("spyreport", NULL),
|
|
|
|
"spy:unit", "target:unit", "status:int", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("spyreport_mage", NULL),
|
|
|
|
"spy:unit", "target:unit", "type:int", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("spyreport_faction", NULL),
|
|
|
|
"spy:unit", "target:unit", "faction:faction", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("spyreport_skills", NULL),
|
|
|
|
"spy:unit", "target:unit", "skills:string", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("spyreport_items", NULL),
|
|
|
|
"spy:unit", "target:unit", "items:items", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("destroy_ship_0", NULL),
|
|
|
|
"unit:unit", "ship:ship", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("destroy_ship_1", NULL),
|
|
|
|
"unit:unit", "ship:ship", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("destroy_ship_2", NULL),
|
|
|
|
"unit:unit", "ship:ship", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("destroy_ship_3", NULL),
|
|
|
|
"ship:ship", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("destroy_ship_4", NULL),
|
|
|
|
"ship:ship", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("sink_msg", NULL),
|
|
|
|
"ship:ship", "region:region", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("sink_lost_msg", NULL),
|
|
|
|
"unit:unit", "region:region", "dead:int", MT_NEW_END);
|
|
|
|
mt_create_va(mt_new("sink_saved_msg", NULL),
|
|
|
|
"unit:unit", "region:region", MT_NEW_END);
|
2018-01-13 08:51:40 +01:00
|
|
|
|
|
|
|
if (fix) {
|
|
|
|
fix->r = test_create_region(0, 0, NULL);
|
|
|
|
fix->spy = test_create_unit(test_create_faction(NULL), fix->r);
|
|
|
|
fix->victim = test_create_unit(test_create_faction(NULL), fix->r);
|
|
|
|
}
|
2015-01-18 17:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_simple_spy_message(CuTest *tc) {
|
|
|
|
spy_fixture fix;
|
|
|
|
|
2018-01-13 08:51:40 +01:00
|
|
|
test_setup();
|
2015-01-18 17:40:54 +01:00
|
|
|
setup_spy(&fix);
|
|
|
|
|
|
|
|
spy_message(0, fix.spy, fix.victim);
|
|
|
|
|
2015-08-17 19:35:07 +02:00
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
|
2015-01-18 17:40:54 +01:00
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2015-01-18 17:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_all_spy_message(CuTest *tc) {
|
|
|
|
spy_fixture fix;
|
2017-02-18 21:15:14 +01:00
|
|
|
item_type *itype;
|
2015-01-18 17:40:54 +01:00
|
|
|
|
2018-01-13 08:51:40 +01:00
|
|
|
test_setup();
|
2015-01-18 17:40:54 +01:00
|
|
|
setup_spy(&fix);
|
|
|
|
|
|
|
|
enable_skill(SK_MAGIC, true);
|
|
|
|
set_level(fix.victim, SK_MINING, 2);
|
|
|
|
set_level(fix.victim, SK_MAGIC, 2);
|
|
|
|
create_mage(fix.victim, M_DRAIG);
|
|
|
|
set_factionstealth(fix.victim, fix.spy->faction);
|
|
|
|
|
|
|
|
itype = it_get_or_create(rt_get_or_create("sword"));
|
2017-12-17 10:16:56 +01:00
|
|
|
new_weapontype(itype, 0, frac_zero, NULL, 0, 0, 0, SK_MELEE);
|
2015-01-18 17:40:54 +01:00
|
|
|
i_change(&fix.victim->items, itype, 1);
|
|
|
|
|
|
|
|
spy_message(99, fix.spy, fix.victim);
|
|
|
|
|
2015-08-17 19:35:07 +02:00
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_mage"));
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_skills"));
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_faction"));
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_items"));
|
2015-01-18 17:40:54 +01:00
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2015-01-18 17:40:54 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:38:14 +02:00
|
|
|
static void test_sabotage_self(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
2015-07-07 09:29:43 +02:00
|
|
|
|
2017-03-10 21:29:37 +01:00
|
|
|
test_setup();
|
2018-01-13 08:51:40 +01:00
|
|
|
setup_spy(NULL);
|
2018-01-14 09:38:26 +01:00
|
|
|
r = test_create_region(0, 0, NULL);
|
2015-07-07 09:29:43 +02:00
|
|
|
assert(r);
|
|
|
|
u = test_create_unit(test_create_faction(NULL), r);
|
2015-07-07 09:44:24 +02:00
|
|
|
assert(u && u->faction && u->region == r);
|
2015-07-07 09:29:43 +02:00
|
|
|
u->ship = test_create_ship(r, test_create_shiptype("boat"));
|
|
|
|
assert(u->ship);
|
2015-07-07 13:38:14 +02:00
|
|
|
ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
|
2015-07-07 09:29:43 +02:00
|
|
|
assert(ord);
|
|
|
|
CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord));
|
|
|
|
CuAssertPtrEquals(tc, 0, r->ships);
|
2018-01-12 21:15:21 +01:00
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "sink_msg"));
|
2015-10-13 15:45:38 +02:00
|
|
|
free_order(ord);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2015-07-07 09:29:43 +02:00
|
|
|
}
|
2015-01-18 17:40:54 +01:00
|
|
|
|
2015-07-07 13:38:14 +02:00
|
|
|
|
|
|
|
static void test_sabotage_other_fail(CuTest *tc) {
|
|
|
|
unit *u, *u2;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
2015-07-07 14:42:07 +02:00
|
|
|
message *msg;
|
2015-07-07 13:38:14 +02:00
|
|
|
|
2017-03-10 21:29:37 +01:00
|
|
|
test_setup();
|
2018-01-13 08:51:40 +01:00
|
|
|
setup_spy(NULL);
|
|
|
|
|
2018-01-14 09:38:26 +01:00
|
|
|
r = test_create_region(0, 0, NULL);
|
2015-07-07 13:38:14 +02:00
|
|
|
assert(r);
|
|
|
|
u = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
u2 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
assert(u && u2);
|
|
|
|
u2->ship = test_create_ship(r, test_create_shiptype("boat"));
|
|
|
|
assert(u2->ship);
|
|
|
|
u->ship = u2->ship;
|
|
|
|
ship_update_owner(u->ship);
|
|
|
|
assert(ship_owner(u->ship) == u);
|
|
|
|
ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
|
|
|
|
assert(ord);
|
|
|
|
CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord));
|
2015-07-07 14:42:07 +02:00
|
|
|
msg = test_get_last_message(u2->faction->msgs);
|
|
|
|
CuAssertStrEquals(tc, "destroy_ship_1", test_get_messagetype(msg));
|
|
|
|
msg = test_get_last_message(u->faction->msgs);
|
|
|
|
CuAssertStrEquals(tc, "destroy_ship_3", test_get_messagetype(msg));
|
2015-07-07 13:38:14 +02:00
|
|
|
CuAssertPtrNotNull(tc, r->ships);
|
2015-10-13 15:45:38 +02:00
|
|
|
free_order(ord);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2015-07-07 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:38:53 +01:00
|
|
|
static void test_setstealth_cmd(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
const struct locale *lang;
|
2017-04-17 20:11:44 +02:00
|
|
|
|
2016-12-03 23:38:53 +01:00
|
|
|
test_setup();
|
2018-01-14 09:38:26 +01:00
|
|
|
u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL));
|
2016-12-03 23:38:53 +01:00
|
|
|
lang = u->faction->locale;
|
2017-04-17 20:11:44 +02:00
|
|
|
u->flags = UFL_ANON_FACTION | UFL_SIEGE;
|
2016-12-03 23:38:53 +01:00
|
|
|
u->thisorder = create_order(K_SETSTEALTH, lang, "%s %s",
|
2017-04-17 20:11:44 +02:00
|
|
|
LOC(lang, parameters[P_FACTION]),
|
|
|
|
LOC(lang, parameters[P_NOT]));
|
2016-12-03 23:38:53 +01:00
|
|
|
setstealth_cmd(u, u->thisorder);
|
|
|
|
CuAssertIntEquals(tc, UFL_SIEGE, u->flags);
|
|
|
|
free_order(u->thisorder);
|
|
|
|
u->thisorder = create_order(K_SETSTEALTH, lang, "%s",
|
2017-04-17 20:11:44 +02:00
|
|
|
LOC(lang, parameters[P_FACTION]));
|
|
|
|
setstealth_cmd(u, u->thisorder);
|
|
|
|
CuAssertIntEquals(tc, UFL_SIEGE | UFL_ANON_FACTION, u->flags);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2017-04-17 20:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_setstealth_demon(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
struct locale *lang;
|
|
|
|
struct race *rc;
|
|
|
|
|
|
|
|
test_setup();
|
|
|
|
lang = test_create_locale();
|
|
|
|
rc = test_create_race("demon");
|
2018-01-14 09:38:26 +01:00
|
|
|
u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, NULL));
|
2017-04-17 20:11:44 +02:00
|
|
|
rc = test_create_race("dwarf");
|
|
|
|
init_races(lang);
|
|
|
|
u->thisorder = create_order(K_SETSTEALTH, lang, racename(lang, u, rc));
|
|
|
|
setstealth_cmd(u, u->thisorder);
|
|
|
|
CuAssertPtrEquals(tc, (void *)rc, (void *)u->irace);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2017-04-17 20:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_setstealth_demon_bad(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
struct locale *lang;
|
|
|
|
struct race *rc;
|
|
|
|
|
|
|
|
test_setup();
|
|
|
|
lang = test_create_locale();
|
|
|
|
rc = test_create_race("demon");
|
2018-01-14 09:38:26 +01:00
|
|
|
u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, NULL));
|
2018-05-10 22:00:23 +02:00
|
|
|
|
2017-04-17 20:11:44 +02:00
|
|
|
rc = test_create_race("smurf");
|
2018-05-10 22:00:23 +02:00
|
|
|
rc->flags &= ~RCF_PLAYABLE;
|
|
|
|
|
2017-04-17 20:11:44 +02:00
|
|
|
init_races(lang);
|
|
|
|
u->thisorder = create_order(K_SETSTEALTH, lang, racename(lang, u, rc));
|
2016-12-03 23:38:53 +01:00
|
|
|
setstealth_cmd(u, u->thisorder);
|
2017-04-17 20:11:44 +02:00
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)u->irace);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2016-12-03 23:38:53 +01:00
|
|
|
}
|
2015-07-07 13:38:14 +02:00
|
|
|
|
|
|
|
static void test_sabotage_other_success(CuTest *tc) {
|
|
|
|
unit *u, *u2;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
|
|
|
|
2017-03-10 21:29:37 +01:00
|
|
|
test_setup();
|
2018-01-13 08:51:40 +01:00
|
|
|
setup_spy(NULL);
|
2018-01-14 09:38:26 +01:00
|
|
|
r = test_create_region(0, 0, NULL);
|
2015-07-07 13:38:14 +02:00
|
|
|
assert(r);
|
|
|
|
u = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
u2 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
assert(u && u2);
|
|
|
|
u2->ship = test_create_ship(r, test_create_shiptype("boat"));
|
|
|
|
assert(u2->ship);
|
|
|
|
u->ship = u2->ship;
|
|
|
|
ship_update_owner(u->ship);
|
|
|
|
assert(ship_owner(u->ship) == u);
|
|
|
|
ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
|
|
|
|
assert(ord);
|
|
|
|
set_level(u2, SK_SPY, 1);
|
|
|
|
CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord));
|
|
|
|
CuAssertPtrEquals(tc, 0, r->ships);
|
2015-10-13 15:45:38 +02:00
|
|
|
free_order(ord);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2015-07-07 13:38:14 +02:00
|
|
|
}
|
2015-07-07 14:42:07 +02:00
|
|
|
|
2015-01-18 17:40:54 +01:00
|
|
|
CuSuite *get_spy_suite(void)
|
|
|
|
{
|
2015-07-07 09:44:24 +02:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_simple_spy_message);
|
|
|
|
SUITE_ADD_TEST(suite, test_all_spy_message);
|
|
|
|
SUITE_ADD_TEST(suite, test_sabotage_self);
|
2016-12-03 23:38:53 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_setstealth_cmd);
|
2017-04-17 20:11:44 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_setstealth_demon);
|
|
|
|
SUITE_ADD_TEST(suite, test_setstealth_demon_bad);
|
2015-07-07 13:38:14 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_sabotage_other_fail);
|
|
|
|
SUITE_ADD_TEST(suite, test_sabotage_other_success);
|
2015-07-07 09:44:24 +02:00
|
|
|
return suite;
|
2015-01-18 17:40:54 +01:00
|
|
|
}
|