Simplify BuildingInfo.IsCloseEnoughToBase adjacency checks.

This commit is contained in:
Paul Chote
2021-03-28 12:05:47 +01:00
committed by reaperrr
parent 1a9dfc0893
commit 19c7e14393

View File

@@ -181,10 +181,24 @@ namespace OpenRA.Mods.Common.Traits
return null; return null;
} }
bool ActorGrantsValidArea(Actor a, RequiresBuildableAreaInfo rba) static bool AnyGivesBuildableArea(IEnumerable<Actor> actors, Player p, bool allyBuildEnabled, RequiresBuildableAreaInfo rba)
{ {
return rba.AreaTypes.Overlaps(a.TraitsImplementing<GivesBuildableArea>() foreach (var a in actors)
.SelectMany(gba => gba.AreaTypes)); {
if (!a.IsInWorld)
continue;
if (a.Owner != p && (!allyBuildEnabled || a.Owner.RelationshipWith(p) != PlayerRelationship.Ally))
continue;
var overlaps = rba.AreaTypes.Overlaps(a.TraitsImplementing<GivesBuildableArea>()
.SelectMany(gba => gba.AreaTypes));
if (overlaps)
return true;
}
return false;
} }
public virtual bool IsCloseEnoughToBase(World world, Player p, ActorInfo ai, CPos topLeft) public virtual bool IsCloseEnoughToBase(World world, Player p, ActorInfo ai, CPos topLeft)
@@ -212,21 +226,17 @@ namespace OpenRA.Mods.Common.Traits
{ {
for (var x = scanStart.X; x < scanEnd.X; x++) for (var x = scanStart.X; x < scanEnd.X; x++)
{ {
var pos = new CPos(x, y); var c = new CPos(x, y);
var buildingAtPos = bi.GetBuildingAt(pos); if (AnyGivesBuildableArea(world.ActorMap.GetActorsAt(c), p, allyBuildEnabled, requiresBuildableArea))
if (buildingAtPos == null)
{ {
var unitsAtPos = world.ActorMap.GetActorsAt(pos).Where(a => a.IsInWorld nearnessCandidates.Add(c);
&& (a.Owner == p || (allyBuildEnabled && a.Owner.RelationshipWith(p) == PlayerRelationship.Ally)) continue;
&& ActorGrantsValidArea(a, requiresBuildableArea));
if (unitsAtPos.Any())
nearnessCandidates.Add(pos);
} }
else if (buildingAtPos.IsInWorld && ActorGrantsValidArea(buildingAtPos, requiresBuildableArea)
&& (buildingAtPos.Owner == p || (allyBuildEnabled && buildingAtPos.Owner.RelationshipWith(p) == PlayerRelationship.Ally))) // Building bibs and pathable footprint cells are not included in the ActorMap
nearnessCandidates.Add(pos); // TODO: Allow ActorMap to track these and finally remove the BuildingInfluence layer completely
if (AnyGivesBuildableArea(new[] { bi.GetBuildingAt(c) }, p, allyBuildEnabled, requiresBuildableArea))
nearnessCandidates.Add(c);
} }
} }