2015-10-12 11:54:59 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "piracy.h"
|
|
|
|
|
2015-10-12 19:40:20 +02:00
|
|
|
#include <kernel/config.h>
|
|
|
|
#include <kernel/region.h>
|
2015-10-12 11:54:59 +02:00
|
|
|
#include <kernel/unit.h>
|
2015-10-12 13:27:39 +02:00
|
|
|
#include <kernel/ship.h>
|
2015-10-12 16:30:56 +02:00
|
|
|
#include <kernel/terrain.h>
|
2015-10-12 11:54:59 +02:00
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/order.h>
|
2015-11-17 11:25:58 +01:00
|
|
|
#include <kernel/race.h>
|
2015-10-12 11:54:59 +02:00
|
|
|
|
2015-10-12 16:30:56 +02:00
|
|
|
#include <util/base36.h>
|
2015-10-12 11:54:59 +02:00
|
|
|
#include <util/language.h>
|
|
|
|
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-10-12 16:30:56 +02:00
|
|
|
static void setup_piracy(void) {
|
|
|
|
struct locale *lang;
|
2015-10-12 19:40:20 +02:00
|
|
|
ship_type *st_boat;
|
2015-10-12 16:30:56 +02:00
|
|
|
|
|
|
|
test_cleanup();
|
2015-11-22 10:33:31 +01:00
|
|
|
config_set("rules.ship.storms", "0");
|
2015-10-12 16:30:56 +02:00
|
|
|
lang = get_or_create_locale("de");
|
|
|
|
locale_setstring(lang, directions[D_EAST], "OSTEN");
|
|
|
|
init_directions(lang);
|
2016-06-10 20:55:27 +02:00
|
|
|
test_create_terrain("ocean", SEA_REGION);
|
2015-10-12 19:40:20 +02:00
|
|
|
st_boat = test_create_shiptype("boat");
|
|
|
|
st_boat->cargo = 1000;
|
2015-10-12 16:30:56 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 21:54:00 +01:00
|
|
|
static void setup_pirate(unit **pirate, int p_r_flags, int p_rc_flags, const char *p_shiptype,
|
|
|
|
order **ord, unit **victim, int v_r_flags, const char *v_shiptype) {
|
|
|
|
terrain_type *vterrain;
|
|
|
|
ship_type *st_boat = NULL;
|
|
|
|
race *rc;
|
|
|
|
faction *f;
|
|
|
|
|
|
|
|
setup_piracy();
|
|
|
|
vterrain = get_or_create_terrain("terrain1");
|
|
|
|
fset(vterrain, v_r_flags);
|
|
|
|
*victim = test_create_unit(test_create_faction(0), test_create_region(1, 0, vterrain));
|
|
|
|
assert(*victim);
|
|
|
|
|
|
|
|
if (v_shiptype) {
|
|
|
|
st_boat = st_get_or_create(v_shiptype);
|
|
|
|
u_set_ship(*victim, test_create_ship((*victim)->region, st_boat));
|
2016-03-11 11:52:36 +01:00
|
|
|
free(st_boat->coasts);
|
2015-12-01 21:54:00 +01:00
|
|
|
st_boat->coasts = (struct terrain_type **)calloc(2, sizeof(struct terrain_type *));
|
|
|
|
st_boat->coasts[0] = vterrain;
|
|
|
|
st_boat->coasts[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pirate = create_unit(test_create_region(0, 0, get_or_create_terrain("terrain2")), f = test_create_faction(0), 1, rc = rc_get_or_create("pirate"), 0, 0, 0);
|
|
|
|
fset(rc, p_rc_flags);
|
|
|
|
assert(f && *pirate);
|
|
|
|
|
|
|
|
if (p_shiptype) {
|
|
|
|
st_boat = st_get_or_create(p_shiptype);
|
|
|
|
u_set_ship(*pirate, test_create_ship((*pirate)->region, st_boat));
|
2016-03-11 11:52:36 +01:00
|
|
|
free(st_boat->coasts);
|
2015-12-01 21:54:00 +01:00
|
|
|
st_boat->coasts = (struct terrain_type **)calloc(2, sizeof(struct terrain_type *));
|
|
|
|
st_boat->coasts[0] = vterrain;
|
|
|
|
st_boat->coasts[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->locale = get_or_create_locale("de");
|
|
|
|
*ord = create_order(K_PIRACY, f->locale, "%s", itoa36((*victim)->faction->no));
|
|
|
|
assert(*ord);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:30:56 +02:00
|
|
|
static void test_piracy_cmd(CuTest * tc) {
|
2015-10-12 13:27:39 +02:00
|
|
|
faction *f;
|
2015-10-12 19:40:20 +02:00
|
|
|
region *r;
|
2015-10-12 13:27:39 +02:00
|
|
|
unit *u, *u2;
|
2015-10-12 11:54:59 +02:00
|
|
|
order *ord;
|
2015-10-12 16:30:56 +02:00
|
|
|
terrain_type *t_ocean;
|
2015-10-12 19:40:20 +02:00
|
|
|
ship_type *st_boat;
|
2015-10-12 16:30:56 +02:00
|
|
|
|
2015-12-01 21:54:00 +01:00
|
|
|
test_cleanup();
|
|
|
|
|
2015-10-12 16:30:56 +02:00
|
|
|
setup_piracy();
|
|
|
|
t_ocean = get_or_create_terrain("ocean");
|
2015-10-12 19:40:20 +02:00
|
|
|
st_boat = st_get_or_create("boat");
|
2015-10-12 16:30:56 +02:00
|
|
|
u2 = test_create_unit(test_create_faction(0), test_create_region(1, 0, t_ocean));
|
2015-10-12 19:40:20 +02:00
|
|
|
u_set_ship(u2, test_create_ship(u2->region, st_boat));
|
2015-10-12 16:30:56 +02:00
|
|
|
assert(u2);
|
2015-10-12 19:40:20 +02:00
|
|
|
u = test_create_unit(f = test_create_faction(0), r = test_create_region(0, 0, t_ocean));
|
|
|
|
set_level(u, SK_SAILING, st_boat->sumskill);
|
|
|
|
u_set_ship(u, test_create_ship(u->region, st_boat));
|
2015-10-12 16:30:56 +02:00
|
|
|
assert(f && u);
|
|
|
|
f->locale = get_or_create_locale("de");
|
|
|
|
ord = create_order(K_PIRACY, f->locale, "%s", itoa36(u2->faction->no));
|
|
|
|
assert(ord);
|
|
|
|
|
|
|
|
piracy_cmd(u, ord);
|
2015-10-12 19:40:20 +02:00
|
|
|
CuAssertPtrEquals(tc, 0, u->thisorder);
|
|
|
|
CuAssertTrue(tc, u->region != r);
|
|
|
|
CuAssertPtrEquals(tc, u2->region, u->region);
|
|
|
|
CuAssertPtrEquals(tc, u2->region, u->ship->region);
|
|
|
|
CuAssertPtrNotNullMsg(tc, "successful PIRACY sets attribute", r->attribs); // FIXME: this is testing implementation, not interface
|
|
|
|
CuAssertPtrNotNullMsg(tc, "successful PIRACY message", test_find_messagetype(f->msgs, "piratesawvictim"));
|
|
|
|
CuAssertPtrNotNullMsg(tc, "successful PIRACY movement", test_find_messagetype(f->msgs, "shipsail"));
|
2015-10-13 15:45:38 +02:00
|
|
|
free_order(ord);
|
2015-10-12 11:54:59 +02:00
|
|
|
|
|
|
|
test_cleanup();
|
2015-10-12 16:30:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_piracy_cmd_errors(CuTest * tc) {
|
2015-11-17 11:25:58 +01:00
|
|
|
race *r;
|
2015-10-12 16:30:56 +02:00
|
|
|
faction *f;
|
|
|
|
unit *u, *u2;
|
|
|
|
order *ord;
|
2015-10-12 19:40:20 +02:00
|
|
|
ship_type *st_boat;
|
2015-10-12 16:30:56 +02:00
|
|
|
|
2015-12-01 21:54:00 +01:00
|
|
|
test_cleanup();
|
|
|
|
|
2015-10-12 16:30:56 +02:00
|
|
|
setup_piracy();
|
2015-10-12 19:40:20 +02:00
|
|
|
st_boat = st_get_or_create("boat");
|
2015-11-17 11:25:58 +01:00
|
|
|
r = test_create_race("pirates");
|
|
|
|
u = test_create_unit(f = test_create_faction(r), test_create_region(0, 0, get_or_create_terrain("ocean")));
|
2015-10-12 13:27:39 +02:00
|
|
|
f->locale = get_or_create_locale("de");
|
|
|
|
ord = create_order(K_PIRACY, f->locale, "");
|
2015-10-12 16:30:56 +02:00
|
|
|
assert(u && ord);
|
|
|
|
|
2015-10-12 11:54:59 +02:00
|
|
|
piracy_cmd(u, ord);
|
2015-10-12 13:27:39 +02:00
|
|
|
CuAssertPtrNotNullMsg(tc, "must be on a ship for PIRACY", test_find_messagetype(f->msgs, "error144"));
|
|
|
|
|
2015-11-17 11:25:58 +01:00
|
|
|
test_clear_messages(f);
|
|
|
|
fset(r, RCF_SWIM);
|
|
|
|
piracy_cmd(u, ord);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "swimmers are pirates", 0, test_find_messagetype(f->msgs, "error144"));
|
|
|
|
CuAssertPtrEquals_Msg(tc, "swimmers are pirates", 0, test_find_messagetype(f->msgs, "error146"));
|
|
|
|
freset(r, RCF_SWIM);
|
|
|
|
fset(r, RCF_FLY);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "flyers are pirates", 0, test_find_messagetype(f->msgs, "error144"));
|
|
|
|
freset(r, RCF_FLY);
|
|
|
|
test_clear_messages(f);
|
|
|
|
|
2015-10-12 19:40:20 +02:00
|
|
|
u_set_ship(u, test_create_ship(u->region, st_boat));
|
2015-10-12 13:27:39 +02:00
|
|
|
|
|
|
|
u2 = test_create_unit(u->faction, u->region);
|
|
|
|
u_set_ship(u2, u->ship);
|
|
|
|
|
|
|
|
test_clear_messages(f);
|
|
|
|
piracy_cmd(u2, ord);
|
|
|
|
CuAssertPtrNotNullMsg(tc, "must be owner for PIRACY", test_find_messagetype(f->msgs, "error146"));
|
|
|
|
|
|
|
|
test_clear_messages(f);
|
|
|
|
piracy_cmd(u, ord);
|
|
|
|
CuAssertPtrNotNullMsg(tc, "must specify target for PIRACY", test_find_messagetype(f->msgs, "piratenovictim"));
|
2015-10-13 15:45:38 +02:00
|
|
|
free_order(ord);
|
2015-10-12 13:27:39 +02:00
|
|
|
|
2015-10-12 11:54:59 +02:00
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-12-01 21:54:00 +01:00
|
|
|
static void test_piracy_cmd_walking(CuTest * tc) {
|
|
|
|
unit *pirate, *victim;
|
|
|
|
order *ord;
|
|
|
|
region *r;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
|
|
|
|
setup_pirate(&pirate, 0, 0, NULL, &ord, &victim, SWIM_INTO | SEA_REGION, "boat");
|
|
|
|
/* fset(rc, RCF_SWIM); */
|
|
|
|
r = pirate->region;
|
|
|
|
|
|
|
|
piracy_cmd(pirate, ord);
|
|
|
|
CuAssertPtrEquals(tc, 0, pirate->thisorder);
|
|
|
|
CuAssertTrue(tc, pirate->region == r);
|
|
|
|
CuAssertPtrNotNullMsg(tc, "successful PIRACY message", test_find_messagetype(pirate->faction->msgs, "error144"));
|
|
|
|
free_order(ord);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_piracy_cmd_land_to_land(CuTest * tc) {
|
2016-08-20 19:10:53 +02:00
|
|
|
unit *u;
|
2015-12-01 21:54:00 +01:00
|
|
|
order *ord;
|
|
|
|
region *r;
|
2016-08-20 19:10:53 +02:00
|
|
|
faction *f;
|
|
|
|
int target;
|
|
|
|
const terrain_type *t_plain;
|
|
|
|
const ship_type *stype;
|
2015-12-01 21:54:00 +01:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
|
2016-08-20 19:10:53 +02:00
|
|
|
setup_piracy();
|
|
|
|
t_plain = get_or_create_terrain("plain");
|
|
|
|
stype = test_create_shiptype("boat");
|
|
|
|
|
|
|
|
// create a target:
|
|
|
|
r = test_create_region(0, 0, t_plain);
|
|
|
|
f = test_create_faction(0);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
u->ship = test_create_ship(r, stype);
|
|
|
|
target = f->no;
|
|
|
|
|
|
|
|
// create a pirate:
|
|
|
|
r = test_create_region(1, 0, t_plain);
|
|
|
|
f = test_create_faction(0);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
u->ship = test_create_ship(r, stype);
|
|
|
|
set_level(u, SK_SAILING, u->ship->type->sumskill);
|
|
|
|
ord = create_order(K_PIRACY, f->locale, "%s", itoa36(target));
|
2015-12-01 21:54:00 +01:00
|
|
|
|
2016-08-20 19:10:53 +02:00
|
|
|
piracy_cmd(u, ord);
|
|
|
|
CuAssertPtrEquals(tc, 0, u->thisorder);
|
|
|
|
CuAssertPtrEquals(tc, r, u->region);
|
|
|
|
// TODO check message
|
2015-12-01 21:54:00 +01:00
|
|
|
free_order(ord);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_piracy_cmd_swimmer(CuTest * tc) {
|
|
|
|
unit *pirate, *victim;
|
|
|
|
order *ord;
|
|
|
|
region *r;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
|
|
|
|
setup_pirate(&pirate, 0, RCF_SWIM, NULL, &ord, &victim, SWIM_INTO | SEA_REGION, "boat");
|
|
|
|
r = pirate->region;
|
|
|
|
|
|
|
|
piracy_cmd(pirate, ord);
|
|
|
|
CuAssertPtrEquals(tc, 0, pirate->thisorder);
|
|
|
|
CuAssertTrue(tc, pirate->region != r);
|
|
|
|
CuAssertPtrEquals(tc, victim->region, pirate->region);
|
|
|
|
CuAssertPtrNotNullMsg(tc, "successful PIRACY message", test_find_messagetype(pirate->faction->msgs, "piratesawvictim"));
|
|
|
|
CuAssertPtrNotNullMsg(tc, "successful PIRACY movement", test_find_messagetype(pirate->faction->msgs, "travel"));
|
|
|
|
free_order(ord);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
2015-10-12 11:54:59 +02:00
|
|
|
|
|
|
|
CuSuite *get_piracy_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
2015-10-12 13:27:39 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_piracy_cmd_errors);
|
2015-10-12 16:30:56 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_piracy_cmd);
|
2015-12-01 21:54:00 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_piracy_cmd_walking);
|
2016-08-20 19:10:53 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_piracy_cmd_land_to_land);
|
2015-12-01 21:54:00 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_piracy_cmd_swimmer);
|
2015-10-12 11:54:59 +02:00
|
|
|
return suite;
|
|
|
|
}
|