131 lines
5.5 KiB
C#
131 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxZones
|
|
{
|
|
/// <summary>
|
|
/// Package Caravan. One message per trade naming the delivery zone the caravan goods went to (design:
|
|
/// 1.1 "Caravan purchases appear in the zone" > Message). The TradeDeal.TryExecute prefix opens a batch,
|
|
/// DeliveryRouter.PlaceFromCaravan records each placed stack with its own zone, and the postfix (or, if
|
|
/// the trade threw, the finalizer) sends one message for the batch. Outside a trade, a mod calling
|
|
/// GiveSoldThingToPlayer directly, each call sends its own message. The text says "goods from the
|
|
/// trade", since sale silver comes through the same call as purchases.
|
|
///
|
|
/// A batch lives for one TryExecute call on this thread and is never saved.
|
|
/// </summary>
|
|
public static class CaravanDeliveries
|
|
{
|
|
public sealed class Batch
|
|
{
|
|
internal readonly Batch previous;
|
|
internal readonly List<Zone_Delivery> zones = new List<Zone_Delivery>();
|
|
/// <summary>Zones that got at least one stack inside them; the others only got overflow next to them.</summary>
|
|
internal readonly HashSet<Zone_Delivery> inside = new HashSet<Zone_Delivery>();
|
|
internal readonly List<Thing> things = new List<Thing>();
|
|
internal bool ended;
|
|
|
|
internal Batch(Batch previous)
|
|
{
|
|
this.previous = previous;
|
|
}
|
|
|
|
/// <summary>Distinct zones goods went to (in or next to) in this batch, in the order they first got some. Read by tests.</summary>
|
|
public IReadOnlyList<Zone_Delivery> Zones => zones;
|
|
|
|
/// <summary>True when the batch went to one zone and every stack of it lies next to that zone (overflow).</summary>
|
|
public bool OnlyNextToZone => zones.Count == 1 && !inside.Contains(zones[0]);
|
|
|
|
internal void Add(DeliveryRouter.PlacedPiece piece)
|
|
{
|
|
if (piece.Zone == null)
|
|
return;
|
|
if (!zones.Contains(piece.Zone))
|
|
zones.Add(piece.Zone);
|
|
if (!piece.NextToZone)
|
|
inside.Add(piece.Zone);
|
|
if (piece.Thing != null && !things.Contains(piece.Thing))
|
|
things.Add(piece.Thing);
|
|
}
|
|
}
|
|
|
|
[ThreadStatic] private static Batch current;
|
|
|
|
/// <summary>The open batch of the trade running on this thread, or null.</summary>
|
|
public static Batch Current => current;
|
|
|
|
/// <summary>Opens a batch for one trade; the previous one (none outside a nested call) comes back at End.</summary>
|
|
public static Batch Begin()
|
|
{
|
|
var batch = new Batch(current);
|
|
current = batch;
|
|
return batch;
|
|
}
|
|
|
|
/// <summary>Closes the batch and sends its message. A second call for the same batch does nothing.</summary>
|
|
public static void End(Batch batch)
|
|
{
|
|
if (batch == null || batch.ended)
|
|
return;
|
|
batch.ended = true;
|
|
if (current == batch)
|
|
current = batch.previous;
|
|
Send(batch);
|
|
}
|
|
|
|
/// <summary>Adds placed stacks, each under its own zone, to the open batch, or sends a message at once when no trade is open.</summary>
|
|
public static void Record(List<DeliveryRouter.PlacedPiece> pieces)
|
|
{
|
|
if (pieces == null || pieces.Count == 0)
|
|
return;
|
|
Batch batch = current;
|
|
bool alone = batch == null || batch.ended;
|
|
if (alone)
|
|
batch = new Batch(null);
|
|
for (int i = 0; i < pieces.Count; i++)
|
|
batch.Add(pieces[i]);
|
|
if (alone)
|
|
Send(batch);
|
|
}
|
|
|
|
/// <summary>
|
|
/// "Goods from the trade were delivered to Delivery zone 1.", "... delivered next to Delivery zone 1,
|
|
/// which is full." when nextToZone (one zone, every stack overflowed), or "... delivered to 2 delivery
|
|
/// zones." Null for no zone.
|
|
/// </summary>
|
|
public static string MessageText(IReadOnlyList<Zone_Delivery> zones, bool nextToZone = false)
|
|
{
|
|
if (zones == null || zones.Count == 0)
|
|
return null;
|
|
if (zones.Count == 1)
|
|
return (nextToZone ? "BlaxZones_CaravanGoodsNextToZone" : "BlaxZones_CaravanGoodsInZone").Translate(zones[0].label.Named("ZONE"));
|
|
return "BlaxZones_CaravanGoodsInZones".Translate(zones.Count.Named("COUNT"));
|
|
}
|
|
|
|
private static void Send(Batch batch)
|
|
{
|
|
try
|
|
{
|
|
if (batch.zones.Count == 0 || !BlaxZonesMod.S.caravanMessage)
|
|
return;
|
|
var targets = new List<Thing>();
|
|
for (int i = 0; i < batch.things.Count; i++)
|
|
{
|
|
Thing t = batch.things[i];
|
|
if (t != null && t.Spawned && !targets.Contains(t))
|
|
targets.Add(t);
|
|
}
|
|
string text = MessageText(batch.zones, batch.OnlyNextToZone);
|
|
if (text.NullOrEmpty())
|
|
return;
|
|
Messages.Message(text, targets.Count > 0 ? new LookTargets(targets) : LookTargets.Invalid, MessageTypeDefOf.NeutralEvent, historical: true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ZonesLog.Once("Caravan delivery message", e);
|
|
}
|
|
}
|
|
}
|
|
}
|