test target selection rules for drifting ships.

This commit is contained in:
Enno Rehling 2016-06-10 18:24:18 +02:00
parent 546ffe87b8
commit f08957b513
4 changed files with 42 additions and 15 deletions

View File

@ -783,9 +783,26 @@ static void msg_to_ship_inmates(ship *sh, unit **firstu, unit **lastu, message *
msg_release(msg); msg_release(msg);
} }
region * drift_target(ship *sh) {
int d, d_offset = rng_int() % MAXDIRECTIONS;
region *rnext = NULL;
for (d = 0; d != MAXDIRECTIONS; ++d) {
region *rn;
direction_t dir = (direction_t)((d + d_offset) % MAXDIRECTIONS);
rn = rconnect(sh->region, dir);
if (rn != NULL && fval(rn->terrain, SAIL_INTO) && check_ship_allowed(sh, rn) >= 0) {
rnext = rn;
if (!fval(rnext->terrain, SEA_REGION)) {
// prefer drifting towards non-ocean regions
break;
}
}
}
return rnext;
}
static void drifting_ships(region * r) static void drifting_ships(region * r)
{ {
direction_t d;
bool drift = config_get_int("rules.ship.drifting", 1) != 0; bool drift = config_get_int("rules.ship.drifting", 1) != 0;
double damage_drift = config_get_flt("rules.ship.damage_drift", 0.02); double damage_drift = config_get_flt("rules.ship.damage_drift", 0.02);
@ -796,7 +813,6 @@ static void drifting_ships(region * r)
region *rnext = NULL; region *rnext = NULL;
region_list *route = NULL; region_list *route = NULL;
unit *firstu = r->units, *lastu = NULL, *captain; unit *firstu = r->units, *lastu = NULL, *captain;
int d_offset;
direction_t dir = 0; direction_t dir = 0;
double ovl; double ovl;
@ -831,17 +847,7 @@ static void drifting_ships(region * r)
} else { } else {
/* Auswahl einer Richtung: Zuerst auf Land, dann /* Auswahl einer Richtung: Zuerst auf Land, dann
* zufällig. Falls unmögliches Resultat: vergiß es. */ * zufällig. Falls unmögliches Resultat: vergiß es. */
d_offset = rng_int () % MAXDIRECTIONS; rnext = drift_target(sh);
for (d = 0; d != MAXDIRECTIONS; ++d) {
region *rn;
dir = (direction_t)((d + d_offset) % MAXDIRECTIONS);
rn = rconnect(r, dir);
if (rn != NULL && fval(rn->terrain, SAIL_INTO) && check_ship_allowed(sh, rn) >= 0) {
rnext = rn;
if (!fval(rnext->terrain, SEA_REGION))
break;
}
}
} }
if (rnext != NULL) { if (rnext != NULL) {

View File

@ -83,6 +83,7 @@ extern "C" {
#define SA_NO_COAST -2 #define SA_NO_COAST -2
int check_ship_allowed(struct ship *sh, const struct region * r); int check_ship_allowed(struct ship *sh, const struct region * r);
struct region * drift_target(struct ship *sh);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -498,6 +498,24 @@ static void test_follow_ship_msg(CuTest * tc) {
test_cleanup(); test_cleanup();
} }
static void test_drifting_ships(CuTest *tc) {
ship *sh;
region *r1, *r2, *r3;
terrain_type *t_ocean, *t_plain;
ship_type *st_boat;
test_cleanup();
t_ocean = test_create_terrain("ocean", SEA_REGION|SAIL_INTO);
t_plain = test_create_terrain("plain", LAND_REGION|SAIL_INTO);
r1 = test_create_region(0, 0, t_ocean);
r2 = test_create_region(1, 0, t_ocean);
st_boat = test_create_shiptype("boat");
sh = test_create_ship(r1, st_boat);
CuAssertPtrEquals(tc, r2, drift_target(sh));
r3 = test_create_region(-1, 0, t_plain);
CuAssertPtrEquals(tc, r3, drift_target(sh));
test_cleanup();
}
CuSuite *get_move_suite(void) CuSuite *get_move_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -521,5 +539,6 @@ CuSuite *get_move_suite(void)
SUITE_ADD_TEST(suite, test_ship_ridiculous_overload_no_captain); SUITE_ADD_TEST(suite, test_ship_ridiculous_overload_no_captain);
SUITE_ADD_TEST(suite, test_ship_damage_overload); SUITE_ADD_TEST(suite, test_ship_damage_overload);
SUITE_ADD_TEST(suite, test_follow_ship_msg); SUITE_ADD_TEST(suite, test_follow_ship_msg);
SUITE_ADD_TEST(suite, test_drifting_ships);
return suite; return suite;
} }

View File

@ -196,9 +196,10 @@ ship_type * test_create_shiptype(const char * name)
free(stype->coasts); free(stype->coasts);
} }
stype->coasts = stype->coasts =
(terrain_type **)malloc(sizeof(terrain_type *) * 2); (terrain_type **)malloc(sizeof(terrain_type *) * 3);
stype->coasts[0] = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION | SAIL_INTO | FLY_INTO); stype->coasts[0] = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION | SAIL_INTO | FLY_INTO);
stype->coasts[1] = NULL; stype->coasts[1] = test_create_terrain("ocean", SEA_REGION | SWIM_INTO | SAIL_INTO | FLY_INTO);
stype->coasts[2] = NULL;
if (default_locale) { if (default_locale) {
locale_setstring(default_locale, name, name); locale_setstring(default_locale, name, name);
} }