using System; using System.Collections.Generic; using RimWorld; using Verse; namespace BlaxZones { /// /// 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. /// public static class CaravanDeliveries { public sealed class Batch { internal readonly Batch previous; internal readonly List zones = new List(); /// Zones that got at least one stack inside them; the others only got overflow next to them. internal readonly HashSet inside = new HashSet(); internal readonly List things = new List(); internal bool ended; internal Batch(Batch previous) { this.previous = previous; } /// Distinct zones goods went to (in or next to) in this batch, in the order they first got some. Read by tests. public IReadOnlyList Zones => zones; /// True when the batch went to one zone and every stack of it lies next to that zone (overflow). 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; /// The open batch of the trade running on this thread, or null. public static Batch Current => current; /// Opens a batch for one trade; the previous one (none outside a nested call) comes back at End. public static Batch Begin() { var batch = new Batch(current); current = batch; return batch; } /// Closes the batch and sends its message. A second call for the same batch does nothing. public static void End(Batch batch) { if (batch == null || batch.ended) return; batch.ended = true; if (current == batch) current = batch.previous; Send(batch); } /// Adds placed stacks, each under its own zone, to the open batch, or sends a message at once when no trade is open. public static void Record(List 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); } /// /// "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. /// public static string MessageText(IReadOnlyList 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(); 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); } } } }