Skip to content

Commit

Permalink
fix: Fix calculating ship cargo
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
octfx committed Aug 31, 2024
1 parent 90a4574 commit 95881ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
32 changes: 24 additions & 8 deletions Loader/ShipLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ StandardisedItemPort BuildPort(Part part)
var stdPort = new StandardisedItemPort
{
PortName = part.name,
Size = part.ItemPort.maxsize,
Size = part.ItemPort.maxSize,
Types = BuildPortTypes(part),
Flags = BuildPortFlags(part),
};
Expand Down Expand Up @@ -527,12 +527,13 @@ StandardisedPortSummary BuildPortSummary(List<StandardisedPart> parts)
portSummary.UtilityTurrets = FindItemPorts(parts, x => x.Category == "Utility turrets", true).Select(x => x.Item1).ToList();

// Other hardpoints
portSummary.InterdictionHardpoints = FindItemPorts(parts, x => x.Category == "EMP hardpoints" || x.Category == "QIG hardpoints", true).Select(x => x.Item1).ToList();
portSummary.InterdictionHardpoints = FindItemPorts(parts, x => x.Category is "EMP hardpoints" or "QIG hardpoints", true).Select(x => x.Item1).ToList();
portSummary.MissileRacks = FindItemPorts(parts, x => x.Category == "Missile racks", true).Select(x => x.Item1).ToList();
portSummary.PowerPlants = FindItemPorts(parts, x => x.Category == "Power plants", true).Select(x => x.Item1).ToList();
portSummary.Coolers = FindItemPorts(parts, x => x.Category == "Coolers", true).Select(x => x.Item1).ToList();
portSummary.Shields = FindItemPorts(parts, x => x.Category == "Shield generators", true).Select(x => x.Item1).ToList();
portSummary.CargoGrids = FindItemPorts(parts, x => x.Category == "Cargo grids", true).Select(x => x.Item1).ToList();
// portSummary.CargoGrids = FindItemPorts(parts, x => x.InstalledItem is { Type: "Cargo" or "CargoGrid" or "Container" } && !x.InstalledItem.ClassName.Contains("storage") && !x.InstalledItem.ClassName.Contains("personal"), true).Select(x => x.Item1).ToList();
portSummary.CargoGrids = FindItemPorts(parts, x => x.InstalledItem?.InventoryContainer != null, true).Select(x => x.Item1).ToList();
portSummary.Countermeasures = FindItemPorts(parts, x => x.Category == "Countermeasures", true).Select(x => x.Item1).ToList();
portSummary.MainThrusters = FindItemPorts(parts, x => x.Category == "Main thrusters", true).Select(x => x.Item1).ToList();
portSummary.RetroThrusters = FindItemPorts(parts, x => x.Category == "Retro thrusters", true).Select(x => x.Item1).ToList();
Expand All @@ -542,7 +543,7 @@ StandardisedPortSummary BuildPortSummary(List<StandardisedPart> parts)
portSummary.HydrogenFuelTanks = FindItemPorts(parts, x => x.Category == "Fuel tanks", true).Select(x => x.Item1).ToList();
portSummary.QuantumDrives = FindItemPorts(parts, x => x.Category == "Quantum drives", true).Select(x => x.Item1).ToList();
portSummary.QuantumFuelTanks = FindItemPorts(parts, x => x.Category == "Quantum fuel tanks", true).Select(x => x.Item1).ToList();
portSummary.Avionics = FindItemPorts(parts, x => x.Category == "Scanners" || x.Category == "Pings" || x.Category == "Radars" || x.Category == "Transponders", true).Select(x => x.Item1).ToList();
portSummary.Avionics = FindItemPorts(parts, x => x.Category is "Scanners" or "Pings" or "Radars" or "Transponders", true).Select(x => x.Item1).ToList();

return portSummary;
}
Expand All @@ -555,6 +556,24 @@ StandardisedShip BuildShipSummary(EntityClassDefinition entity, List<Standardise
inventorySize = _inventoryContainerSvc.GetInventoryContainer(entity.Components?.VehicleComponentParams?.inventoryContainerParams);
}

var cargo = (double)(portSummary.CargoGrids
.Where(x => x.InstalledItem?.InventoryContainer != null)
.Sum(x => x.InstalledItem.InventoryContainer.SCU));

if (entity.Components?.SEntityComponentDefaultLoadoutParams.loadout.SItemPortLoadoutManualParams.entries !=
null)
{
cargo = entity.Components?.SEntityComponentDefaultLoadoutParams.loadout.SItemPortLoadoutManualParams
.entries
.Where(x => x.entityClassName != null)
.Select(x => _entitySvc.GetByClassName(x.entityClassName))
.Select(x => x?.Components?.SCItemInventoryContainerComponentParams)
.Where(x => x != null)
.Select(x => _inventoryContainerSvc.GetInventoryContainer(x.containerParams))
.Where(x => x != null)
.Sum(x => (x.SCU == 0 && x.x > 0) ? (x.x * x.y * x.z) / 1.953125 : x.SCU) ?? 0;
}

var shipSummary = new StandardisedShip
{
ClassName = entity.ClassName,
Expand All @@ -571,10 +590,7 @@ StandardisedShip BuildShipSummary(EntityClassDefinition entity, List<Standardise
WeaponCrew = portSummary.MannedTurrets.Count + portSummary.RemoteTurrets.Count,
OperationsCrew = Math.Max(portSummary.MiningTurrets.Count, portSummary.UtilityTurrets.Count),
Mass = FindParts(parts, x => true).Sum((x) => x.Item1.Mass ?? 0),
Cargo = (int)(portSummary.CargoGrids
.Where(x => x.InstalledItem?.CargoGrid != null)
.Where(x => !x.InstalledItem.CargoGrid.MiningOnly)
.Sum(x => x.InstalledItem.CargoGrid.Capacity)),
Cargo = cargo,
Inventory = inventorySize,
// Insurance = insuranceSvc.GetInsurance(entity.ClassName)
};
Expand Down
4 changes: 2 additions & 2 deletions Loader/scdb.Xml/Vehicles/ItemPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace scdb.Xml.Vehicles
public class ItemPort
{
[XmlAttribute]
public int minsize;
public int minSize;

[XmlAttribute]
public int maxsize;
public int maxSize;

[XmlAttribute]
public string display_name;
Expand Down

0 comments on commit 95881ef

Please sign in to comment.