Skip to content

Commit

Permalink
Add TargetSelector for MoveToNearestAction
Browse files Browse the repository at this point in the history
  • Loading branch information
behindcurtain3 committed Apr 28, 2024
1 parent 66603cb commit 68bea56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Pulsar4X/GameEngine/Engine/Orders/Actions/MoveToNearestAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ internal override Entity EntityCommanding
}

public EntityManager.FilterEntities Filter { get; protected set; }

public delegate Entity EntitySelector(Entity entity);
public EntitySelector? TargetSelector { get; protected set; }
public EntityFilter EntityFactionFilter { get; protected set; } = EntityFilter.Friendly | EntityFilter.Neutral | EntityFilter.Hostile;

private List<EntityCommand> _shipCommands = new List<EntityCommand>();
Expand Down Expand Up @@ -76,12 +79,16 @@ private void FindNearestAndSetupWarpCommands()

if(closestValidEntity == null) return;

// Get the colonies parent radius
closestValidEntity.TryGetDatablob<PositionDB>(out var closestEntityPositionDB);
var targetEntity = TargetSelector == null ? closestValidEntity : TargetSelector(closestValidEntity);

if(!targetEntity.TryGetDatablob<PositionDB>(out var targetEntityPositionDB))
{
return;
}

if(closestEntityPositionDB.Parent == null) return;
if(targetEntityPositionDB.Parent == null) return;

double targetSMA = OrbitMath.LowOrbitRadius(closestEntityPositionDB.Parent);
double targetSMA = OrbitMath.LowOrbitRadius(targetEntityPositionDB.Parent);

// Get all the ships we need to add the movement command to
var ships = fleetDB.Children.Where(c => c.HasDataBlob<ShipInfoDB>());
Expand All @@ -90,22 +97,22 @@ private void FindNearestAndSetupWarpCommands()
{
if(!ship.HasDataBlob<WarpAbilityDB>()) continue;
if(!ship.TryGetDatablob<PositionDB>(out var shipPositionDB)) continue;
if(shipPositionDB.Parent == closestEntityPositionDB.OwningEntity) continue;
if(shipPositionDB.Parent == targetEntityPositionDB.OwningEntity) continue;

var shipMass = ship.GetDataBlob<MassVolumeDB>().MassTotal;

(Vector3 position, DateTime _) = WarpMath.GetInterceptPosition
(
ship,
closestValidEntity.GetDataBlob<OrbitDB>(),
targetEntity.GetDataBlob<OrbitDB>(),
EntityCommanding.StarSysDateTime
);

Vector3 targetPos = Vector3.Normalise(position) * targetSMA;

// Create the movement order
var cargoLibrary = EntityCommanding.GetFactionOwner.GetDataBlob<FactionInfoDB>().Data.CargoGoods;
(WarpMoveCommand warpCommand, NewtonThrustCommand? thrustCommand) = WarpMoveCommand.CreateCommand(cargoLibrary, RequestingFactionGuid, ship, closestValidEntity, targetPos, EntityCommanding.StarSysDateTime, new Vector3() , shipMass);
(WarpMoveCommand warpCommand, NewtonThrustCommand? thrustCommand) = WarpMoveCommand.CreateCommand(cargoLibrary, RequestingFactionGuid, ship, targetEntity, targetPos, EntityCommanding.StarSysDateTime, new Vector3(), shipMass);
_shipCommands.Add(warpCommand);
}

Expand Down Expand Up @@ -156,7 +163,8 @@ public override EntityCommand Clone()
CreatedDate = this.CreatedDate,
ActionOnDate = this.ActionOnDate,
ActionedOnDate = this.ActionedOnDate,
IsRunning = this.IsRunning
IsRunning = this.IsRunning,
TargetSelector = this.TargetSelector
};

return command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ private static bool ColonyFilter(Entity entity)
return entity.HasDataBlob<ColonyInfoDB>();
}

private static Entity ColonySelector(Entity entity)
{
return entity.GetDataBlob<PositionDB>().Parent ?? entity;
}

public static MoveToNearestColonyAction CreateCommand(int factionId, Entity commandingEntity)
{
var command = MoveToNearestAction.CreateCommand<MoveToNearestColonyAction>(factionId, commandingEntity);
command.Filter = ColonyFilter;
command.TargetSelector = ColonySelector;
return command;
}
}
Expand Down

0 comments on commit 68bea56

Please sign in to comment.