Refactor castledistance (#597)

* move the attacker-to-castle distance calculation out

* wrong extension into filesystem utils


Former-commit-id: f69d46119f88a644df92dd250bf02d7307656f97
This commit is contained in:
2021-04-19 07:38:00 -07:00
committed by GitHub
parent 7c60683f2e
commit cce16c2da5
5 changed files with 52 additions and 31 deletions
+5
View File
@@ -49,6 +49,11 @@ cc_binary(
cc_binary(
name = "random_battle_runner",
srcs = ["RandomBattleRunner.cpp"],
data = [
"//src/main/resources/net/eagle0/shardok:battalion_types",
"//src/main/resources/net/eagle0/shardok:settings",
"//src/main/resources/net/eagle0/shardok/maps",
],
deps = [
"//src/main/cpp/net/eagle0/common:byte_vector",
"//src/main/cpp/net/eagle0/common:filesystem_utils",
@@ -118,7 +118,7 @@ auto main(int argc, char** argv) -> int {
auto randomUnits = LoadUnits();
printf("Map file directory is %s\n", FilesystemUtils::MapFilesDirectory().c_str());
fileNames = FilesystemUtils::FilesInDirectory(FilesystemUtils::MapFilesDirectory(), "e0m");
fileNames = FilesystemUtils::FilesInDirectory(FilesystemUtils::MapFilesDirectory(), ".e0m");
for (auto & fileName : fileNames) { fileName.resize(fileName.size() - 4); }
@@ -133,7 +133,7 @@ auto ContextFreeUnitValue(const Unit &unit) -> ScoreValue {
return battalionValue + heroValue;
}
auto DefenderDistanceBuf(
auto DefenderDistanceBufTooManyCastles(
const Unit &unit,
const HexMap &hexMap,
const vector<Unit> &attackerUnits,
@@ -147,7 +147,7 @@ auto DefenderDistanceBuf(
int with;
WithoutAndWith(int a, int b) : without(a), with(b) {}
};
} __attribute__((aligned(8)));
vector<WithoutAndWith> pointCosts{};
pointCosts.reserve(attackerUnits.size());
@@ -192,6 +192,37 @@ auto DefenderDistanceBuf(
return buf;
}
auto AttackerDistanceToCastlesDebuf(
const Unit &unit,
const vector<Coords> &allCastleCoords,
const shared_ptr<ActionPointDistances> &actionPointDistancesWithoutBraving,
const shared_ptr<ActionPointDistances> &actionPointDistancesWithBraving) -> double {
if (common::Contains(allCastleCoords, unit.location())) return 1.0;
// This should be something like:
// 1) skip castles that we already occupy
// 2) skip me if I occupy a castle
// 3) Get within attack range of an occupied castle
// 4) do a progressive backoff, similar to the defender debuf, for other castles
int pointCostToDesiredTargetWithBraving = 0;
int pointCostToDesiredTargetWithoutBraving = 0;
CostsWithoutAndWithBraving(
actionPointDistancesWithBraving,
actionPointDistancesWithoutBraving,
unit.location(),
allCastleCoords,
pointCostToDesiredTargetWithoutBraving,
pointCostToDesiredTargetWithBraving);
if (pointCostToDesiredTargetWithoutBraving == 999) {
return 0.8 - pointCostToDesiredTargetWithBraving * kPointDistancePenalty;
} else {
return 1.0 - pointCostToDesiredTargetWithoutBraving * kPointDistancePenalty;
}
}
auto UnitValue(
const Unit &unit,
bool isAttacker,
@@ -275,30 +306,15 @@ auto UnitValue(
if (isAttacker) {
// If the attacker could capture all castles,
if (attackerCanCaptureCastles) {
if (!terrain.modifier().has_castle()) {
int pointCostToDesiredTargetWithBraving = 0;
int pointCostToDesiredTargetWithoutBraving = 0;
CostsWithoutAndWithBraving(
actionPointDistancesWithBraving,
actionPointDistancesWithoutBraving,
location,
allCastleCoords,
pointCostToDesiredTargetWithoutBraving,
pointCostToDesiredTargetWithBraving);
if (pointCostToDesiredTargetWithoutBraving == 999) {
distanceMultiplier =
0.8 - pointCostToDesiredTargetWithBraving * kPointDistancePenalty;
} else {
distanceMultiplier = 1.0 - pointCostToDesiredTargetWithoutBraving *
kPointDistancePenalty;
}
}
distanceMultiplier = AttackerDistanceToCastlesDebuf(
unit,
allCastleCoords,
actionPointDistancesWithoutBraving,
actionPointDistancesWithBraving);
}
} else { // defender; optimize for enemy being furthest away from being able to attack
if (!attackerCanCaptureCastles) {
distanceMultiplier = DefenderDistanceBuf(
distanceMultiplier = DefenderDistanceBufTooManyCastles(
unit,
map,
attackerUnits,
@@ -24,7 +24,7 @@ struct AttackLocations;
auto ContextFreeUnitValue(const Unit &unit) -> ScoreValue;
auto DefenderDistanceBuf(
auto DefenderDistanceBufTooManyCastles(
const Unit &unit,
const HexMap &hexMap,
const vector<Unit> &attackerUnits,
@@ -107,7 +107,7 @@ TEST_F(AIUnitScoreCalculatorTests, defender_getsBigBonusForClosestAttackerDistan
Unit attacker2 = attacker;
*attacker2.mutable_location() = MakeCoords(5, 5);
auto fartherBuf = DefenderDistanceBuf(
auto fartherBuf = DefenderDistanceBufTooManyCastles(
defender,
map,
{attacker1, attacker2},
@@ -117,7 +117,7 @@ TEST_F(AIUnitScoreCalculatorTests, defender_getsBigBonusForClosestAttackerDistan
*attacker1.mutable_location() = MakeCoords(1, 2);
auto closerBuf = DefenderDistanceBuf(
auto closerBuf = DefenderDistanceBufTooManyCastles(
defender,
map,
{attacker1, attacker2},
@@ -137,7 +137,7 @@ TEST_F(AIUnitScoreCalculatorTests, defender_getsSmallBonusForNextClosestAttacker
Unit attacker2 = attacker;
*attacker2.mutable_location() = MakeCoords(5, 5);
auto fartherBuf = DefenderDistanceBuf(
auto fartherBuf = DefenderDistanceBufTooManyCastles(
defender,
map,
{attacker1, attacker2},
@@ -147,7 +147,7 @@ TEST_F(AIUnitScoreCalculatorTests, defender_getsSmallBonusForNextClosestAttacker
*attacker2.mutable_location() = MakeCoords(4, 4);
auto closerBuf = DefenderDistanceBuf(
auto closerBuf = DefenderDistanceBufTooManyCastles(
defender,
map,
{attacker1, attacker2},
@@ -167,7 +167,7 @@ TEST_F(AIUnitScoreCalculatorTests, defender_neverImprovesWithMoreAttackers) {
Unit attacker2 = attacker;
*attacker2.mutable_location() = MakeCoords(5, 5);
auto moreAttackers = DefenderDistanceBuf(
auto moreAttackers = DefenderDistanceBufTooManyCastles(
defender,
map,
{attacker1, attacker2},
@@ -175,7 +175,7 @@ TEST_F(AIUnitScoreCalculatorTests, defender_neverImprovesWithMoreAttackers) {
actionPointDistancesWithBraving,
GetGameSettingsGetter());
auto fewerAttackers = DefenderDistanceBuf(
auto fewerAttackers = DefenderDistanceBufTooManyCastles(
defender,
map,
{attacker1},