124 lines
6.6 KiB
C#
124 lines
6.6 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxZones
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Package Caravan. Design: 1.1 "Caravan purchases appear in the zone". Goods bought from a trader
|
||
|
|
/// caravan or a Hospitality guest, and the silver from selling to them, appear in the best matching
|
||
|
|
/// delivery zone instead of at the pack animal's feet. Roofs don't matter: nothing falls.
|
||
|
|
/// </summary>
|
||
|
|
public static partial class DeliveryRouter
|
||
|
|
{
|
||
|
|
/// <summary>One stack a caravan placement put on the map or merged into, and the zone it went to.</summary>
|
||
|
|
public struct PlacedPiece
|
||
|
|
{
|
||
|
|
public Thing Thing;
|
||
|
|
public Zone_Delivery Zone;
|
||
|
|
/// <summary>Every fitting zone was full: the stack lies next to Zone (overflow), not in it.</summary>
|
||
|
|
public bool NextToZone;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Replaces the one GenPlace.TryPlaceThing call in Pawn_TraderTracker.GiveSoldThingToPlayer
|
||
|
|
/// (Pawn_TraderTracker.cs:194, GenPlace.cs:27), same signature. Bought pawns never get here: vanilla
|
||
|
|
/// returns early for them (:180-188). Sale silver does: when the player sells more than they buy, the
|
||
|
|
/// trader hands over silver through the same call (TradeDeal.cs:147, Tradeable.cs:472-478). With
|
||
|
|
/// "Route caravan purchases" on, tries the delivery zones (TryPlaceInZones) and records what it placed
|
||
|
|
/// for the trade's message (CaravanDeliveries). Whatever is left, and everything on an exception
|
||
|
|
/// (logged once), goes to the original call with the original arguments, so vanilla's destroy path
|
||
|
|
/// is reached only where vanilla would reach it. On a true result vanilla adds the thing to the lord's
|
||
|
|
/// extraForbiddenThings (:196), as before.
|
||
|
|
/// </summary>
|
||
|
|
public static bool PlaceFromCaravan(Thing thing, IntVec3 center, Map map, ThingPlaceMode mode, Action<Thing, int> placedAction,
|
||
|
|
Predicate<IntVec3> extraValidator, Rot4? rot, int squareRadius)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (BlaxZonesMod.S.routeCaravan && mode == ThingPlaceMode.Near)
|
||
|
|
{
|
||
|
|
bool done = TryPlaceInZones(thing, map, placedAction, extraValidator, rot, out List<PlacedPiece> pieces);
|
||
|
|
if (pieces.Count > 0)
|
||
|
|
CaravanDeliveries.Record(pieces);
|
||
|
|
if (done)
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
ZonesLog.Once("Caravan delivery", e);
|
||
|
|
// Already on the map (placed, or merged into a stack, before the failure): placing it again would fail.
|
||
|
|
if (thing != null && (thing.Spawned || thing.Destroyed))
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return GenPlace.TryPlaceThing(thing, center, map, mode, placedAction, extraValidator, rot, squareRadius);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Places the thing in a delivery zone with "Caravan purchases land here" ticked and a filter that
|
||
|
|
/// allows it, by priority, then zone ID. In each zone it picks a cell by the placement rule
|
||
|
|
/// (TryPickCellInZone with DeliveryRules.PlacementCellOk: free cells nearest the middle first, then a
|
||
|
|
/// matching stack) and places there with GenPlace in Near mode, limited to that zone's cells that
|
||
|
|
/// pass the placement rule. When every fitting zone is full, it places next to the top zone with
|
||
|
|
/// GenPlace in Near mode from the top zone's overflow center, with "Overflow lands next to the zone"
|
||
|
|
/// on and that cell reachable by the colony, as orbital overflow does.
|
||
|
|
///
|
||
|
|
/// True when the whole thing was placed. pieces holds every stack placed or merged into, each with
|
||
|
|
/// the zone it went to and whether it lies next to that zone (overflow), also when only part of a
|
||
|
|
/// large stack found room (the rest is still in thing for the caller). Throws only when
|
||
|
|
/// ZonesTestHooks.RouterThrows is set.
|
||
|
|
/// </summary>
|
||
|
|
public static bool TryPlaceInZones(Thing thing, Map map, Action<Thing, int> placedAction, Predicate<IntVec3> extraValidator,
|
||
|
|
Rot4? rot, out List<PlacedPiece> pieces)
|
||
|
|
{
|
||
|
|
if (ZonesTestHooks.RouterThrows)
|
||
|
|
throw new InvalidOperationException("ZonesTestHooks.RouterThrows");
|
||
|
|
pieces = new List<PlacedPiece>();
|
||
|
|
if (thing == null || thing.Spawned || thing.Destroyed || Refuses(map, thing) || DeliveryFilters.HoldsPawn(thing))
|
||
|
|
return false;
|
||
|
|
List<Zone_Delivery> candidates = Candidates(map, thing, DeliverySource.Caravan);
|
||
|
|
if (candidates.Count == 0)
|
||
|
|
return false;
|
||
|
|
|
||
|
|
// GenPlace reports each stack it placed or merged into; the zone and overflow flag of the current try go with it.
|
||
|
|
List<PlacedPiece> placed = pieces;
|
||
|
|
Zone_Delivery target = null;
|
||
|
|
bool nextTo = false;
|
||
|
|
void Record(Thing t, int count)
|
||
|
|
{
|
||
|
|
if (t != null && !placed.Exists(p => p.Thing == t))
|
||
|
|
placed.Add(new PlacedPiece { Thing = t, Zone = target, NextToZone = nextTo });
|
||
|
|
placedAction?.Invoke(t, count);
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < candidates.Count; i++)
|
||
|
|
{
|
||
|
|
Zone_Delivery candidate = candidates[i];
|
||
|
|
if (!TryPickCellInZone(candidate, thing, DeliveryRules.PlacementCellOk, null, out IntVec3 cell))
|
||
|
|
continue;
|
||
|
|
bool InZone(IntVec3 c) =>
|
||
|
|
map.zoneManager.ZoneAt(c) == candidate
|
||
|
|
&& DeliveryRules.PlacementCellOk(map, c)
|
||
|
|
&& (extraValidator == null || extraValidator(c));
|
||
|
|
target = candidate;
|
||
|
|
nextTo = false;
|
||
|
|
if (GenPlace.TryPlaceThing(thing, cell, map, ThingPlaceMode.Near, Record, InZone, rot) || thing.Destroyed)
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Every fitting zone is full: next to the top one, if the colony can reach it.
|
||
|
|
Zone_Delivery top = candidates[0];
|
||
|
|
if (!BlaxZonesMod.S.overflowNearZone)
|
||
|
|
return false;
|
||
|
|
IntVec3 center = OverflowCenter(top);
|
||
|
|
if (!center.IsValid || !center.InBounds(map) || !DeliveryRules.ColonyCanReach(map, center))
|
||
|
|
return false;
|
||
|
|
target = top;
|
||
|
|
nextTo = true;
|
||
|
|
return GenPlace.TryPlaceThing(thing, center, map, ThingPlaceMode.Near, Record, extraValidator, rot) || thing.Destroyed;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|