Skip to content

Commit

Permalink
Work on JP surveys, add Move Towards command + processor
Browse files Browse the repository at this point in the history
Still a bit buggy, move towards command tanks the frame rate
  • Loading branch information
behindcurtain3 committed Mar 30, 2024
1 parent a1314b9 commit 7f61506
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Pulsar4X/GameEngine/Atb/GravSurveyAtb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ public GravSurveyAtb(int speed)

public void OnComponentInstallation(Entity parentEntity, ComponentInstance componentInstance)
{
if(parentEntity.TryGetDatablob<GravSurveyAbilityDB>(out var gravSurveyAbilityDB))
if(parentEntity.TryGetDatablob<JPSurveyAbilityDB>(out var gravSurveyAbilityDB))
{
gravSurveyAbilityDB.Speed += Speed;
}
else
{
parentEntity.SetDataBlob<GravSurveyAbilityDB>(new GravSurveyAbilityDB() { Speed = Speed });
parentEntity.SetDataBlob<JPSurveyAbilityDB>(new JPSurveyAbilityDB() { Speed = Speed });
}
}

public void OnComponentUninstallation(Entity parentEntity, ComponentInstance componentInstance)
{
if(parentEntity.TryGetDatablob<GravSurveyAbilityDB>(out var gravSurveyAbilityDB))
if(parentEntity.TryGetDatablob<JPSurveyAbilityDB>(out var gravSurveyAbilityDB))
{
if(Speed >= gravSurveyAbilityDB.Speed)
{
parentEntity.RemoveDataBlob<GravSurveyAbilityDB>();
parentEntity.RemoveDataBlob<JPSurveyAbilityDB>();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Pulsar4X.Datablobs;

public class GravSurveyAbilityDB : BaseDataBlob
public class JPSurveyAbilityDB : BaseDataBlob
{
[JsonProperty]
public uint Speed { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions Pulsar4X/GameEngine/Datablobs/MoveTowardsTargetDB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;

namespace Pulsar4X.Datablobs;

public class MoveTowardsTargetDB : BaseDataBlob
{
[JsonProperty]
public int TargetId { get; set; }

[JsonProperty]
public double DistanceOffset { get; set; } = 100; // 100 meters is probably close enough for a default

[JsonProperty]
public double Speed { get; set; }
}
15 changes: 15 additions & 0 deletions Pulsar4X/GameEngine/Engine/Factories/DefaultStartFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static class DefaultStartFactory
private static ShipDesign _spaceXStarShipDesign;
private static OrdnanceDesign _missile;
private static ComponentDesign _geoSurveyor;
private static ComponentDesign _jpSurveyor;


// this code is a test for multiple systems, worth mentioning it utterly failed, modularity is good when you have it huh.ç
Expand Down Expand Up @@ -489,6 +490,7 @@ public static ShipDesign DefaultShipDesign(Game game, Entity faction, FactionDat
(DefaultBFC(game, faction, factionDataStore), 1),
(ShipSmallCargo(game, faction, factionDataStore), 1),
(DefaultGeoSurveyor(game, faction, factionDataStore), 1),
(DefaultJPSurveyor(game, faction, factionDataStore), 1),
(DefaultFuelTank(game, faction, factionDataStore), 2),
(DefaultWarpDesign(game, faction, factionDataStore), 4),
(DefaultBatteryBank(game, faction, factionDataStore), 3),
Expand Down Expand Up @@ -1061,6 +1063,19 @@ public static ComponentDesign DefaultGeoSurveyor(Game game, Entity faction, Fact
factionDataStore.IncrementTechLevel(_geoSurveyor.TechID);
return _geoSurveyor;
}

public static ComponentDesign DefaultJPSurveyor(Game game, Entity faction, FactionDataStore factionDataStore)
{
if (_jpSurveyor != null)
return _jpSurveyor;
ComponentTemplateBlueprint template = factionDataStore.ComponentTemplates["gravitational-surveyor"];
ComponentDesigner design = new ComponentDesigner(template, factionDataStore, faction.GetDataBlob<FactionTechDB>());
design.ComponentDesignAttributes["Survey Speed"].SetValueFromInput(10);
design.Name = "Gravitational Surveyor";
_jpSurveyor = design.CreateDesign(faction);
factionDataStore.IncrementTechLevel(_jpSurveyor.TechID);
return _jpSurveyor;
}
}

}
103 changes: 103 additions & 0 deletions Pulsar4X/GameEngine/Engine/Orders/Actions/JPSurveyOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using Pulsar4X.Datablobs;
using Pulsar4X.Extensions;

namespace Pulsar4X.Engine.Orders;

public class JPSurveyOrder : EntityCommand
{
public override ActionLaneTypes ActionLanes => ActionLaneTypes.Movement | ActionLaneTypes.InteractWithExternalEntity;

public override bool IsBlocking => true;

public override string Name => $"Jump Point Survey {Target.GetOwnersName()} ({GetProgressPercent()}%)";

public override string Details => "";

public Entity Target { get; private set; }
public JPSurveyableDB? TargetSurveyDB { get; private set; } = null;
public DateTime? PreviousUpdate { get; private set; } = null;
public JPSurveyProcessor? Processor { get; private set; } = null;

private Entity _entityCommanding;
internal override Entity EntityCommanding
{
get { return _entityCommanding; }
}

public JPSurveyOrder() { }
public JPSurveyOrder(Entity commandingEntity, Entity target)
{
_entityCommanding = commandingEntity;
Target = target;
if(Target.TryGetDatablob<JPSurveyableDB>(out var jpSurveyableDB))
{
TargetSurveyDB = jpSurveyableDB;
}
}

public override EntityCommand Clone()
{
var command = new JPSurveyOrder(EntityCommanding, Target)
{
UseActionLanes = this.UseActionLanes,
RequestingFactionGuid = this.RequestingFactionGuid,
EntityCommandingGuid = this.EntityCommandingGuid,
CreatedDate = this.CreatedDate,
ActionOnDate = this.ActionOnDate,
ActionedOnDate = this.ActionedOnDate,
IsRunning = this.IsRunning
};

return command;
}

public override bool IsFinished()
{
return TargetSurveyDB == null ? true : TargetSurveyDB.IsSurveyComplete(EntityCommanding.FactionOwnerID);
}

internal override void Execute(DateTime atDateTime)
{
if(!IsRunning)
{
IsRunning = true;
PreviousUpdate = atDateTime;
Processor = new JPSurveyProcessor(EntityCommanding, Target);
}
else
{
if(PreviousUpdate != null && atDateTime - PreviousUpdate >= TimeSpan.FromDays(1))
{
Processor?.ProcessEntity(EntityCommanding, atDateTime);
PreviousUpdate = atDateTime;
}
}
}

internal override bool IsValidCommand(Game game)
{
return TargetSurveyDB != null;
}

public static JPSurveyOrder CreateCommand(int requestingFactionId, Entity fleet, Entity target)
{
var command = new JPSurveyOrder(fleet, target)
{
RequestingFactionGuid = requestingFactionId
};

return command;
}

private float GetProgressPercent()
{
if(TargetSurveyDB == null) return 0f;
if(!TargetSurveyDB.HasSurveyStarted(RequestingFactionGuid)) return 0f;

uint pointsRequired = TargetSurveyDB.PointsRequired;
uint currentValue = TargetSurveyDB.SurveyPointsRemaining[RequestingFactionGuid];

return (1f - ((float)currentValue / (float)pointsRequired)) * 100f;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Pulsar4X.Datablobs;

namespace Pulsar4X.Engine.Orders;

public class MoveFleetTowardsTargetOrder : EntityCommand
{
public override ActionLaneTypes ActionLanes => ActionLaneTypes.Movement;

public override bool IsBlocking => true;

public override string Name => "Move Fleet Towards Target";

public override string Details => "";

private Entity _entityCommanding;

internal override Entity EntityCommanding => _entityCommanding;

public Entity Target { get; set; }

List<EntityCommand> _shipCommands = new List<EntityCommand>();

public override EntityCommand Clone()
{
throw new NotImplementedException();
}

public override bool IsFinished()
{
if(!IsRunning) return false;

foreach(var command in _shipCommands)
{
if(!command.IsFinished())
return false;
}
return true;
}

internal override void Execute(DateTime atDateTime)
{
if(IsRunning) return;
if(!_entityCommanding.TryGetDatablob<FleetDB>(out var fleetDB)) return;
// Get all the ships we need to add the movement command to
var ships = fleetDB.Children.Where(c => c.HasDataBlob<ShipInfoDB>());

foreach(var ship in ships)
{
var shipCommand = MoveTowardsTargetOrder.CreateCommand(ship, Target);
ship.Manager.Game.OrderHandler.HandleOrder(shipCommand);
_shipCommands.Add(shipCommand);
}
IsRunning = true;
}

public static MoveFleetTowardsTargetOrder CreateCommand(Entity entity, Entity target)
{
var order = new MoveFleetTowardsTargetOrder()
{
RequestingFactionGuid = entity.FactionOwnerID,
EntityCommandingGuid = entity.Id,
_entityCommanding = entity,
Target = target,
};

return order;
}

internal override bool IsValidCommand(Game game)
{
return true;
}
}
49 changes: 35 additions & 14 deletions Pulsar4X/GameEngine/Engine/Orders/Actions/MoveToSystemBodyOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ private void FindColonyAndSetupWarpCommands()

if(targetPositionDB.OwningEntity == null) throw new NullReferenceException("targetPositionDB.OwningEntity cannot be null");

double targetSMA = OrbitMath.LowOrbitRadius(targetPositionDB.OwningEntity);
double targetSMA = 0;

if(Target.HasDataBlob<MassVolumeDB>())
targetSMA = OrbitMath.LowOrbitRadius(targetPositionDB.OwningEntity);

// Get all the ships we need to add the movement command to
var ships = fleetDB.Children.Where(c => c.HasDataBlob<ShipInfoDB>());
Target.TryGetDatablob<OrbitDB>(out var targetOrbitDB);

foreach(var ship in ships)
{
Expand All @@ -57,19 +61,36 @@ private void FindColonyAndSetupWarpCommands()

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

(Vector3 position, DateTime _) = WarpMath.GetInterceptPosition
(
ship,
targetPositionDB.OwningEntity.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, Target, targetPos, EntityCommanding.StarSysDateTime, new Vector3() , shipMass);
_shipCommands.Add(warpCommand);
if(targetOrbitDB == null)
{
var cargoLibrary = EntityCommanding.GetFactionOwner.GetDataBlob<FactionInfoDB>().Data.CargoGoods;
(WarpMoveCommand warpCommand, _) = WarpMoveCommand.CreateCommand(
cargoLibrary,
RequestingFactionGuid,
ship,
Target,
Vector3.Zero,
EntityCommanding.StarSysDateTime,
new Vector3(),
shipMass);
_shipCommands.Add(warpCommand);
}
else
{
(Vector3 position, DateTime _) = WarpMath.GetInterceptPosition
(
ship,
targetPositionDB.OwningEntity.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, Target, targetPos, EntityCommanding.StarSysDateTime, new Vector3() , shipMass);
_shipCommands.Add(warpCommand);
}
}

IsRunning = true;
Expand Down
Loading

0 comments on commit 7f61506

Please sign in to comment.