Skip to content

Commit

Permalink
Add Geo Survey order and processor
Browse files Browse the repository at this point in the history
Can now geo survey things!
  • Loading branch information
behindcurtain3 committed Mar 26, 2024
1 parent 5ea1e8d commit 6f82b2b
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Pulsar4X/GameEngine/Atb/GeoSurveyAtb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Pulsar4X.Atb;
public class GeoSurveyAtb : IComponentDesignAttribute
{
[JsonProperty]
public int Speed { get; set; } = 1;
public uint Speed { get; set; } = 1;

public GeoSurveyAtb(int speed)
{
Speed = speed;
Speed = (uint)speed;
}

public void OnComponentInstallation(Entity parentEntity, ComponentInstance componentInstance)
Expand Down
2 changes: 1 addition & 1 deletion Pulsar4X/GameEngine/Datablobs/GeoSurveyAbilityDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Pulsar4X.Datablobs;
public class GeoSurveyAbilityDB : BaseDataBlob
{
[JsonProperty]
public int Speed { get; set; }
public uint Speed { get; set; }
}
2 changes: 1 addition & 1 deletion Pulsar4X/GameEngine/Engine/ManagerSubPulse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ internal void ProcessSystem(DateTime targetDateTime)
Performance.BeginInterval();
IsProcessing = true;

if (!SpinWait.SpinUntil(_entityManager.HaveAllListnersProcessed, TimeSpan.FromMilliseconds(250)))
if (!SpinWait.SpinUntil(_entityManager.HaveAllListnersProcessed, TimeSpan.FromMilliseconds(500)))
throw new Exception("timeout on listnerProcessing.");

_entityManager.RemoveTaggedEntitys();
Expand Down
103 changes: 103 additions & 0 deletions Pulsar4X/GameEngine/Engine/Orders/Actions/GeoSurveyOrder.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 GeoSurveyOrder : EntityCommand
{
public override ActionLaneTypes ActionLanes => ActionLaneTypes.Movement | ActionLaneTypes.InteractWithExternalEntity;

public override bool IsBlocking => true;

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

public override string Details => "";

public Entity Target { get; private set; }
public GeoSurveyableDB? TargetGeoSurveyDB { get; private set; } = null;
public DateTime? PreviousUpdate { get; private set; } = null;
public GeoSurveyProcessor? Processor { get; private set; } = null;

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

public GeoSurveyOrder() { }
public GeoSurveyOrder(Entity commandingEntity, Entity target)
{
_entityCommanding = commandingEntity;
Target = target;
if(Target.TryGetDatablob<GeoSurveyableDB>(out var geoSurveyableDB))
{
TargetGeoSurveyDB = geoSurveyableDB;
}
}

public override EntityCommand Clone()
{
var command = new GeoSurveyOrder(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 TargetGeoSurveyDB == null ? true : TargetGeoSurveyDB.IsSurveyComplete(EntityCommanding.FactionOwnerID);
}

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

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

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

return command;
}

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

uint pointsRequired = TargetGeoSurveyDB.PointsRequired;
uint currentValue = TargetGeoSurveyDB.GeoSurveyStatus[RequestingFactionGuid];

return (1f - ((float)currentValue / (float)pointsRequired)) * 100f;
}
}
60 changes: 60 additions & 0 deletions Pulsar4X/GameEngine/Engine/Processors/GeoSurveyProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Pulsar4X.Datablobs;
using Pulsar4X.Interfaces;

namespace Pulsar4X.Engine;

public class GeoSurveyProcessor : IInstanceProcessor
{
public Entity Fleet { get; internal set; }
public Entity Target { get; internal set; }

public GeoSurveyProcessor() {}

public GeoSurveyProcessor(Entity fleet, Entity target)
{
Fleet = fleet;
Target = target;
}

internal override void ProcessEntity(Entity entity, DateTime atDateTime)
{
// TODO: need to only get the survey points from ships that are at the survey location
uint totalSurveyPoints = GetSurveyPoints(Fleet);

if(Target.TryGetDatablob<GeoSurveyableDB>(out var geoSurveyableDB))
{
if(!geoSurveyableDB.GeoSurveyStatus.ContainsKey(Fleet.FactionOwnerID))
geoSurveyableDB.GeoSurveyStatus[Fleet.FactionOwnerID] = geoSurveyableDB.PointsRequired;

if(totalSurveyPoints > geoSurveyableDB.GeoSurveyStatus[Fleet.FactionOwnerID])
{
geoSurveyableDB.GeoSurveyStatus[Fleet.FactionOwnerID] = 0;
}
else
{
geoSurveyableDB.GeoSurveyStatus[Fleet.FactionOwnerID] -= totalSurveyPoints;
}
}
}

private uint GetSurveyPoints(Entity entity)
{
uint totalSurveyPoints = 0;

if(entity.TryGetDatablob<GeoSurveyAbilityDB>(out var geoSurveyAbilityDB))
{
totalSurveyPoints += geoSurveyAbilityDB.Speed;
}

if(entity.TryGetDatablob<FleetDB>(out var fleetDB))
{
foreach(var child in fleetDB.Children)
{
totalSurveyPoints += GetSurveyPoints(child);
}
}

return totalSurveyPoints;
}
}
12 changes: 9 additions & 3 deletions Pulsar4X/Pulsar4X.Client/EmpireManagement/FleetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private void DisplayTabs()
{
ImGui.BeginTooltip();
ImGui.Text("IsRunning: " + actions[i].IsRunning);
ImGui.Text("IsFinished(): " + actions[i].IsFinished());
ImGui.Text("IsFinished: " + actions[i].IsFinished());
ImGui.EndTooltip();
}
}
Expand Down Expand Up @@ -304,11 +304,17 @@ private void DisplayTabs()
case IssueOrderType.GeoSurvey:
foreach(var body in bodies)
{
if(!body.TryGetDatablob<GeoSurveyableDB>(out var geoSurveyableDB)) continue;
if(geoSurveyableDB.IsSurveyComplete(_uiState.Faction.Id)) continue;

var name = body.GetName(_uiState.Faction.Id);
if(ImGui.Button(name + "###geosurvey-button-" + name))
{
// var order = MoveToSystemBodyOrder.CreateCommand(_uiState.Faction.Id, SelectedFleet, body);
// _uiState.Game.OrderHandler.HandleOrder(order);
var order = MoveToSystemBodyOrder.CreateCommand(_uiState.Faction.Id, SelectedFleet, body);
_uiState.Game.OrderHandler.HandleOrder(order);

var order2 = GeoSurveyOrder.CreateCommand(_uiState.Faction.Id, SelectedFleet, body);
_uiState.Game.OrderHandler.HandleOrder(order2);
}
}
break;
Expand Down

0 comments on commit 6f82b2b

Please sign in to comment.