Cleanup cargo shuttle/telepad order code (#13591)

Co-authored-by: Eoin Mcloughlin <helloworld@eoinrul.es>
This commit is contained in:
eoineoineoin
2023-03-05 04:27:30 +00:00
committed by GitHub
parent 2276e74b1d
commit 6722adcd83
11 changed files with 167 additions and 205 deletions

View File

@@ -209,34 +209,27 @@ public sealed partial class CargoSystem
if (component == null || shuttle == null || component.Orders.Count == 0)
return orders;
var space = GetCargoSpace(shuttle);
if (space == 0) return orders;
var indices = component.Orders.Keys.ToList();
indices.Sort();
var amount = 0;
foreach (var index in indices)
var spaceRemaining = GetCargoSpace(shuttle);
for( var i = 0; i < component.Orders.Count && spaceRemaining > 0; i++)
{
var order = component.Orders[index];
if (!order.Approved) continue;
var cappedAmount = Math.Min(space - amount, order.Amount);
amount += cappedAmount;
DebugTools.Assert(amount <= space);
if (cappedAmount < order.Amount)
var order = component.Orders[i];
if(order.Approved)
{
var reducedOrder = new CargoOrderData(order.OrderIndex, order.ProductId, cappedAmount, order.Requester, order.Reason);
orders.Add(reducedOrder);
break;
var numToShip = order.OrderQuantity - order.NumDispatched;
if (numToShip > spaceRemaining)
{
// We won't be able to fit the whole oder on, so make one
// which represents the space we do have left:
var reducedOrder = new CargoOrderData(order.OrderId,
order.ProductId, spaceRemaining, order.Requester, order.Reason);
orders.Add(reducedOrder);
}
else
{
orders.Add(order);
}
spaceRemaining -= numToShip;
}
orders.Add(order);
if (amount == space) break;
}
return orders;
@@ -424,70 +417,18 @@ public sealed partial class CargoSystem
_sawmill.Info($"Retrieved cargo shuttle {ToPrettyString(shuttle.Owner)} from {ToPrettyString(orderDatabase.Owner)}");
}
private void AddCargoContents(CargoShuttleComponent component, StationCargoOrderDatabaseComponent orderDatabase)
private void AddCargoContents(CargoShuttleComponent shuttle, StationCargoOrderDatabaseComponent orderDatabase)
{
var xformQuery = GetEntityQuery<TransformComponent>();
var orders = GetProjectedOrders(orderDatabase, component);
var pads = GetCargoPallets(component);
DebugTools.Assert(orders.Sum(o => o.Amount) <= pads.Count);
for (var i = 0; i < pads.Count; i++)
var pads = GetCargoPallets(shuttle);
while (pads.Count > 0)
{
if (orders.Count == 0)
var coordinates = new EntityCoordinates(shuttle.Owner, xformQuery.GetComponent(_random.PickAndTake(pads).Owner).LocalPosition);
if(!FulfillOrder(orderDatabase, coordinates, shuttle.PrinterOutput))
{
break;
var order = orders[0];
var coordinates = new EntityCoordinates(component.Owner, xformQuery.GetComponent(_random.PickAndTake(pads).Owner).LocalPosition);
var item = Spawn(_protoMan.Index<CargoProductPrototype>(order.ProductId).Product, coordinates);
SpawnAndAttachOrderManifest(item, order, coordinates, component);
order.Amount--;
if (order.Amount == 0)
{
// Yes this is functioning as a stack, I was too lazy to re-jig the shuttle state event.
orders.RemoveSwap(0);
orderDatabase.Orders.Remove(order.OrderIndex);
}
else
{
orderDatabase.Orders[order.OrderIndex] = order;
}
}
}
/// <summary>
/// In this method we are printing and attaching order manifests to the orders.
/// </summary>
private void SpawnAndAttachOrderManifest(EntityUid item, CargoOrderData order, EntityCoordinates coordinates, CargoShuttleComponent component)
{
if (!_protoMan.TryIndex(order.ProductId, out CargoProductPrototype? prototype))
return;
// spawn a piece of paper.
var printed = EntityManager.SpawnEntity(component.PrinterOutput, coordinates);
if (!TryComp<PaperComponent>(printed, out var paper))
return;
// fill in the order data
var val = Loc.GetString("cargo-console-paper-print-name", ("orderNumber", order.PrintableOrderNumber));
MetaData(printed).EntityName = val;
_paperSystem.SetContent(printed, Loc.GetString(
"cargo-console-paper-print-text",
("orderNumber", order.PrintableOrderNumber),
("itemName", prototype.Name),
("requester", order.Requester),
("reason", order.Reason),
("approver", order.Approver ?? string.Empty)),
paper);
// attempt to attach the label
if (TryComp<PaperLabelComponent>(item, out var label))
{
_slots.TryInsert(item, label.LabelSlot, printed, null);
}
}