Compare commits

...
Author SHA1 Message Date
admin 584cb8a1a7 use raw pointers 2025-07-08 20:35:48 -07:00
@@ -81,8 +81,8 @@ struct PreCachedAPDs {
static constexpr int NUM_BATTALION_TYPES =
static_cast<int>(BattalionTypeId::BattalionTypeId_MAX) + 1;
std::array<std::shared_ptr<ActionPointDistances>, NUM_BATTALION_TYPES> regular;
std::array<std::shared_ptr<ActionPointDistances>, NUM_BATTALION_TYPES> braving;
std::array<ActionPointDistances *, NUM_BATTALION_TYPES> regular;
std::array<ActionPointDistances *, NUM_BATTALION_TYPES> braving;
std::array<BattalionTypeSPtr, NUM_BATTALION_TYPES> battalionTypes;
PreCachedAPDs(
@@ -92,13 +92,17 @@ struct PreCachedAPDs {
const MapId &mapId) {
ActionPoints braveWaterCost = settings.Backing().brave_water_action_point_cost();
// Initialize arrays to avoid incremental allocation
regular.fill(nullptr);
braving.fill(nullptr);
for (int battTypeId = 0; battTypeId < NUM_BATTALION_TYPES; ++battTypeId) {
auto battType = settings.GetBattalionType(static_cast<BattalionTypeId>(battTypeId));
battalionTypes[battTypeId] = std::move(battType);
auto regularApd =
apdCache->Get(gameState->hex_map(), mapId, battalionTypes[battTypeId], false);
regular[battTypeId] = std::move(regularApd);
regular[battTypeId] = regularApd.get();
if (battalionTypes[battTypeId]->allowsBraveWater) {
auto bravingApd = apdCache->Get(
@@ -107,18 +111,14 @@ struct PreCachedAPDs {
battalionTypes[battTypeId],
true,
braveWaterCost);
braving[battTypeId] = std::move(bravingApd);
braving[battTypeId] = bravingApd.get();
}
}
}
const std::shared_ptr<ActionPointDistances> &GetRegular(int battTypeId) const {
return regular[battTypeId];
}
ActionPointDistances *GetRegular(int battTypeId) const { return regular[battTypeId]; }
const std::shared_ptr<ActionPointDistances> &GetBraving(int battTypeId) const {
return braving[battTypeId];
}
ActionPointDistances *GetBraving(int battTypeId) const { return braving[battTypeId]; }
const BattalionTypeSPtr &GetBattalionType(int battTypeId) const {
return battalionTypes[battTypeId];
@@ -147,8 +147,8 @@ struct EffectiveDistanceCache {
DIST_T GetOrCompute(
const Unit *unit,
const Coords &target,
const std::shared_ptr<ActionPointDistances> &notBravingApd,
const std::shared_ptr<ActionPointDistances> &bravingApd,
ActionPointDistances *notBravingApd,
ActionPointDistances *bravingApd,
const HexMap *hexMap) const {
CacheKey key{unit->unit_id(), target};
auto it = cache.find(key);
@@ -157,7 +157,12 @@ struct EffectiveDistanceCache {
CoordsSet targetSet(hexMap);
targetSet.Add(target);
DIST_T result = EffectiveDistance(unit, notBravingApd, bravingApd, targetSet);
// Create shared_ptr wrappers for the EffectiveDistance call
std::shared_ptr<ActionPointDistances> notBravingPtr(
notBravingApd,
[](ActionPointDistances *) {});
std::shared_ptr<ActionPointDistances> bravingPtr(bravingApd, [](ActionPointDistances *) {});
DIST_T result = EffectiveDistance(unit, notBravingPtr, bravingPtr, targetSet);
cache[key] = result;
return result;
}
@@ -395,8 +400,12 @@ auto AttackerUnitsScore(
occupants,
gameState->hex_map(),
cachedAPDs.GetBattalionType(battTypeId),
cachedAPDs.GetRegular(battTypeId),
cachedAPDs.GetBraving(battTypeId),
std::shared_ptr<ActionPointDistances>(
cachedAPDs.GetRegular(battTypeId),
[](ActionPointDistances *) {}),
std::shared_ptr<ActionPointDistances>(
cachedAPDs.GetBraving(battTypeId),
[](ActionPointDistances *) {}),
isLateGame);
auto uv = UnitValue(
@@ -410,7 +419,9 @@ auto AttackerUnitsScore(
roundsRemaining,
attackLocationsForAttacker,
locationsCausingDanger,
cachedAPDs.GetRegular(battTypeId),
std::shared_ptr<ActionPointDistances>(
cachedAPDs.GetRegular(battTypeId),
[](ActionPointDistances *) {}),
settings);
attackerUnitsValue += distanceMultiplier * uv;
@@ -434,7 +445,9 @@ auto AttackerUnitsScore(
roundsRemaining,
attackLocationsForDefender,
locationsCausingDangerForAttacker,
cachedAPDs.GetRegular(battTypeId),
std::shared_ptr<ActionPointDistances>(
cachedAPDs.GetRegular(battTypeId),
[](ActionPointDistances *) {}),
settings);
double distanceMultiplier = 1.0;