Skip to content

Commit

Permalink
Adjusted logistics drone speed calculation to match game a little better
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsemar committed Oct 17, 2021
1 parent fbef2f4 commit 73b7d6b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 35 deletions.
25 changes: 14 additions & 11 deletions Logistics/LogisticsNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using PersonalLogistics.Shipping;
using PersonalLogistics.StationStorage;
using PersonalLogistics.Util;
using UnityEngine;
using static PersonalLogistics.Log;
using Object = System.Object;

Expand Down Expand Up @@ -46,6 +47,7 @@ public class StationInfo
public readonly List<StationProductInfo> LocalImports = new List<StationProductInfo>();
public int stationId;
public HashSet<int> ItemTypes = new HashSet<int>();
public Vector3 localPosition;

private static Dictionary<int, Dictionary<int, StationInfo>> pool =
new Dictionary<int, Dictionary<int, StationInfo>>();
Expand Down Expand Up @@ -133,6 +135,7 @@ public static StationInfo Build(StationComponent station, PlanetData planet)
stationInfo.stationId = station.id;
stationInfo.PlanetInfo = new PlanetInfo
{ lastLocation = planet.uPosition, Name = planet.displayName, PlanetId = planet.id };
stationInfo.localPosition = station.shipDockPos;
return stationInfo;
}

Expand All @@ -159,8 +162,8 @@ public static class LogisticsNetwork
public static readonly Dictionary<int, List<StationInfo>> byPlanet = new Dictionary<int, List<StationInfo>>();
public static readonly Dictionary<int, int> byItem = new Dictionary<int, int>();
public static readonly Dictionary<int, ByItemSummary> byItemSummary = new Dictionary<int, ByItemSummary>();
public static bool IsInitted = false;
public static bool IsRunning = false;
public static bool IsInitted;
public static bool IsRunning;
public static bool IsFirstLoadComplete;
private static Timer _timer;

Expand Down Expand Up @@ -191,7 +194,7 @@ private static void CollectStationInfos(Object source, ElapsedEventArgs e)
{
if (IsRunning)
{
logger.LogWarning($"Collect already running");
logger.LogWarning("Collect already running");
return;
}

Expand Down Expand Up @@ -299,13 +302,13 @@ public static int ItemAvailableCount(int itemId)
return byItem[itemId];
}

public static (double distance, int itemsRemoved, StationInfo stationInfo) RemoveItem(VectorLF3 playerUPosition, int itemId, int itemCount)
public static (double distance, int itemsRemoved, StationInfo stationInfo) RemoveItem(VectorLF3 playerUPosition, Vector3 playerLocalPosition, int itemId, int itemCount)
{
var stationsWithItem = stations.FindAll(s => s.HasItem(itemId));
stationsWithItem.Sort((s1, s2) =>
{
var s1Distance = s1.PlanetInfo.lastLocation.Distance(playerUPosition);
var s2Distance = s2.PlanetInfo.lastLocation.Distance(playerUPosition);
var s1Distance = StationStorageManager.GetDistance(playerUPosition, playerLocalPosition, s1);
var s2Distance = StationStorageManager.GetDistance(playerUPosition, playerLocalPosition, s2);
return s1Distance.CompareTo(s2Distance);
});
var removedItemCount = 0;
Expand All @@ -323,7 +326,7 @@ public static (double distance, int itemsRemoved, StationInfo stationInfo) Remov
removedItemCount += removedCount;
if (removedCount > 0 && distance < 0)
{
distance = playerUPosition.Distance(stationInfo.PlanetInfo.lastLocation);
distance = StationStorageManager.GetDistance(playerUPosition, playerLocalPosition, stationInfo);
stationPayingCost = stationInfo;
}
}
Expand All @@ -348,7 +351,7 @@ public static int AddItem(VectorLF3 playerUPosition, int itemId, int itemCount)
var stationAddedAmount = itemCount - addedItemCount;
var addedCount = StationStorageManager.AddToStation(stationInfo, itemId, stationAddedAmount);
addedItemCount += addedCount;
var stationProducts = string.Join(", ", stationInfo.Products.Select(s => s.ItemName));
var stationProducts = string.Join(", ", stationInfo.Products.Select(s => s.ItemName));
Debug(
$"Added {addedCount} of {ItemUtil.GetItemName(itemId)} to station {stationInfo.stationId} {stationProducts} on {stationInfo.PlanetName}");
}
Expand Down Expand Up @@ -400,13 +403,13 @@ public static string ShortItemSummary(int itemId)

var stringBuilder = new StringBuilder($"Total items: {byItem[itemId]}\r\n");
stringBuilder.Append($"Supplied: {byItemSummary[itemId].SuppliedItems}\r\n");

var stationsWithItem = stations.FindAll(s => s.SuppliedItems.Contains(itemId));

if (stationsWithItem.Count > 0)
{
long closest =
(long) stationsWithItem.Select(st => st.PlanetInfo.lastLocation.Distance(GameMain.mainPlayer.uPosition)).Min();
(long) stationsWithItem.Select(st => StationStorageManager.GetDistance(GameMain.mainPlayer.uPosition, GameMain.mainPlayer.position, st)).Min();
var calculateArrivalTime = ShippingManager.CalculateArrivalTime(closest);
var secondsAway = (int)(calculateArrivalTime - DateTime.Now).TotalSeconds;
stringBuilder.Append($"Closest {closest} meters (approx {secondsAway} seconds)");
Expand Down
12 changes: 12 additions & 0 deletions Logistics/StationStorageManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using JetBrains.Annotations;
using PersonalLogistics.Logistics;
using UnityEngine;
using static PersonalLogistics.Log;

namespace PersonalLogistics.StationStorage
Expand Down Expand Up @@ -104,5 +105,16 @@ public static long RemoveEnergyFromStation(StationInfo stationInfo, long energy)
stationComponent.energy -= energyRemoved;
return energyRemoved;
}

public static double GetDistance(VectorLF3 playerUPosition, Vector3 playerLocalPosition, StationInfo stationInfo)
{
var uDistance = playerUPosition.Distance(stationInfo.PlanetInfo.lastLocation);
if (uDistance < 600)
{
return Vector3.Distance(playerLocalPosition, stationInfo.localPosition);
}

return uDistance;
}
}
}
4 changes: 4 additions & 0 deletions PlayerInventory/InventoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ public bool RemoveItemImmediately(int itemId, int count)
{
int cnt = count;
_player.package.TakeTailItems(ref itemId, ref cnt);
if (PluginConfig.sortInventory.Value)
{
_player.package.Sort();
}
return cnt == count;
}

Expand Down
2 changes: 1 addition & 1 deletion PlayerInventory/PersonalLogisticManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private bool ProcessLoadTask(ItemRequest itemRequest)
return false;
}

if (ShippingManager.AddRequest(_player.uPosition, itemRequest))
if (ShippingManager.AddRequest(_player.uPosition, _player.position, itemRequest))
{
itemRequest.State = RequestState.WaitingForShipping;
}
Expand Down
26 changes: 14 additions & 12 deletions Shipping/ShippingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using PersonalLogistics.PlayerInventory;
using PersonalLogistics.StationStorage;
using PersonalLogistics.Util;
using UnityEngine;
using static PersonalLogistics.Log;
using static PersonalLogistics.Util.Constant;

Expand All @@ -18,7 +19,7 @@ public class ShippingManager
private readonly Dictionary<Guid, ItemRequest> _requestByGuid = new Dictionary<Guid, ItemRequest>();
private readonly Dictionary<Guid, Cost> _costs = new Dictionary<Guid, Cost>();
private readonly TimeSpan _minAge = TimeSpan.FromSeconds(15);
private static DateTime _lastPopup = DateTime.Now;
private static DateTime _lastPopup = DateTime.Now.Subtract(TimeSpan.FromSeconds(100));

private static ShippingManager _instance;

Expand Down Expand Up @@ -195,7 +196,8 @@ private void ProcessImpl()
var energyToUse = cost.energyCost * ratio;
GameMain.mainPlayer.mecha.MarkEnergyChange(Mecha.EC_DRONE, -energyToUse);
GameMain.mainPlayer.mecha.UseEnergy(energyToUse);
LogPopup($"Personal logistics using {energyToUse} ({ratio}%) of mecha energy");
var ratioInt = (int)(ratio * 100);
LogPopup($"Personal logistics using {energyToUse} ({ratioInt}%) of mecha energy");
cost.energyCost -= (long) energyToUse;
}
}
Expand Down Expand Up @@ -277,7 +279,7 @@ public int RemoveItemsFromBuffer(int itemId, int count)
return removed;
}

public static bool AddRequest(VectorLF3 playerPosition, ItemRequest itemRequest)
public static bool AddRequest(VectorLF3 playerPosition, Vector3 position, ItemRequest itemRequest)
{
if (_instance == null)
{
Expand All @@ -286,17 +288,17 @@ public static bool AddRequest(VectorLF3 playerPosition, ItemRequest itemRequest)
return false;
}

return _instance.AddRequestImpl(playerPosition, itemRequest);
return _instance.AddRequestImpl(playerPosition, position, itemRequest);
}

private bool AddRequestImpl(VectorLF3 playerPosition, ItemRequest itemRequest)
private bool AddRequestImpl(VectorLF3 playerUPosition, Vector3 playerLocalPosition, ItemRequest itemRequest)
{
var shipCapacity = GameMain.history.logisticShipCarries;
var ramount = Math.Max(itemRequest.ItemCount, shipCapacity);
var actualRequestAmount = itemRequest.SkipBuffer ? itemRequest.ItemCount : ramount;
(double distance, int removed, var stationInfo) = LogisticsNetwork.RemoveItem(playerPosition, itemRequest.ItemId, actualRequestAmount);
(double distance, int removed, var stationInfo) = LogisticsNetwork.RemoveItem(playerUPosition, playerLocalPosition, itemRequest.ItemId, actualRequestAmount);
if (removed == 0)
{
{
return false;
}

Expand All @@ -307,7 +309,7 @@ private bool AddRequestImpl(VectorLF3 playerPosition, ItemRequest itemRequest)
if (!addToBuffer)
{
Warn($"Failed to add inbound items to storage buffer {itemRequest.ItemId} {itemRequest.State}");
LogisticsNetwork.AddItem(playerPosition, itemRequest.ItemId, removed);
LogisticsNetwork.AddItem(playerUPosition, itemRequest.ItemId, removed);
}

if (itemRequest.ItemId == DEBUG_ITEM_ID)
Expand Down Expand Up @@ -339,19 +341,19 @@ private Cost CalculateCost(double distance, StationInfo stationInfo)
public static DateTime CalculateArrivalTime(double oneWayDistance)
{
var distance = oneWayDistance * 2;
var sailSpeedModified = GameMain.history.logisticShipSailSpeedModified;
var droneSpeedModified = GameMain.history.logisticDroneSpeedModified;
var shipWarpSpeed = GameMain.history.logisticShipWarpDrive
? GameMain.history.logisticShipWarpSpeedModified
: sailSpeedModified;
: droneSpeedModified;
if (distance > 5000)
{
// d=rt
// t = d/r
var timeToArrival = distance / shipWarpSpeed;
return DateTime.Now.AddSeconds(timeToArrival).AddSeconds(5000 / sailSpeedModified);
return DateTime.Now.AddSeconds(timeToArrival).AddSeconds(5000 / droneSpeedModified);
}

return DateTime.Now.AddSeconds(distance / sailSpeedModified);
return DateTime.Now.AddSeconds(distance / droneSpeedModified);
}

public static bool ItemForTaskArrived(Guid requestGuid)
Expand Down
41 changes: 31 additions & 10 deletions package.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
param ($vertype = 'patch')

Set-StrictMode -Version Latest

write-host "version type $vertype"

Remove-Item .\tmp_release -Force -Recurse
New-Item .\tmp_release -ItemType "directory" -Force
New-Item .\tmp_release -ItemType "directory" -Force

$manifestContent = Get-Content -path .\manifest.json -Raw
$j = $manifestContent | ConvertFrom-Json

$old_vernum = $j.version_number
$sourceFileContent = Get-Content -path .\PersonalLogisticsPlugin.cs -Raw
$sourceFileContent -match '.*PluginVersion = "(\d+.\d+.\d+)".*'

$v = [version]$old_vernum
$old_vernum = $Matches[1]

$new_version = [version]::New($v.Major,$v.Minor,$v.Build+1)
$v = [version]$old_vernum
write-host "v = $v"

if ($vertype -eq "minor")
{
$new_version = [version]::New($v.Major, $v.Minor + 1, 0)
}
elseif ($vertype -eq "patch")
{
$new_version = [version]::New($v.Major, $v.Minor, $v.Build + 1)
}
elseif ($vertype -eq "major")
{
$new_version = [version]::New($v.Major + 1, 0, 0)
}
else
{
Write-Host "invalid vertype: should be (major, minor, patch), got $vertype"
exit
}

Write-Host "next version $new_version"
$new_version_string = "$([string]::Join(".", $new_version))";
$new_version_string

$sourceFileContent = Get-Content -path .\PersonalLogisticsPlugin.cs -Raw
$sourceFileContent -match '.*PluginVersion = "(\d+.\d+.\d+)".*'

$Matches[1]
$sourceFileContent -replace $Matches[1], $new_version_string | Set-Content -Path .\PersonalLogisticsPlugin.cs
$sourceFileContent -replace $old_vernum, $new_version_string | Set-Content -Path .\PersonalLogisticsPlugin.cs

Import-Module -Name ".\Invoke-MsBuild.psm1"
Invoke-MsBuild -Path ".\PersonalLogistics.sln"
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ Click the `Install with Mod Manager` link above.
#### v1.1.0
* Fixed longitude labeling for build ghost geo coords (both east and west were labeled 'E')
* Updated inventory checker to count players hand items (so more won't be requested if you pick up all foundation, for example)
* Adjusted incoming items position when using items from inventory for research
* Adjusted incoming items position when using items from inventory for research
* Adjusted logistics drone speed calculation to match game a little better
* Fixed missed sorting of player inventory

#### v1.0.9
Bugfix, fixed issue where incoming items would never be shown, oops.
Expand Down

0 comments on commit 73b7d6b

Please sign in to comment.