rimworld-blax-zones/Source/BlaxZones/Zone_Delivery.cs

154 lines
5.8 KiB
C#

using System.Text;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxZones
{
/// <summary>
/// A delivery zone: a landing pad for drop pods (design: "Delivery zone"). Saved inside
/// MapComponent_BlaxZones, never in ZoneManager (design: "Zone store, safe to remove from a save").
///
/// This file holds the data model, saving and the cache notifications (integrator). The tab and
/// copy/paste gizmos are in Zone_Delivery.Tab.cs (package Tab), the expand designator in
/// Zone_Delivery.Designators.cs (package Designators).
/// </summary>
public partial class Zone_Delivery : Zone
{
/// <summary>Amber at the vanilla zone opacity (ZoneColorUtility.ZoneOpacity, 0.09).</summary>
public static readonly Color ZoneColor = new Color(1f, 0.70f, 0.10f, 0.09f);
/// <summary>The designator icon tint (defaultIconColor), amber at full opacity.</summary>
public static readonly Color IconColor = new Color(1f, 0.70f, 0.10f, 1f);
/// <summary>What may land here. Always a child of DeliveryFilters.Parent.</summary>
public ThingFilter filter;
/// <summary>Higher priority zones are tried first; ties go to the lower zone ID.</summary>
public StoragePriority priority = StoragePriority.Normal;
/// <summary>Orbital trade purchases, sale silver and ZonesApi callers (Blax Slaves payouts) may land here.</summary>
public bool acceptOrbital = true;
/// <summary>Item rewards from quests may land here (1.1).</summary>
public bool acceptRewards = true;
/// <summary>Goods bought from trader caravans and Hospitality guests may appear here (1.1).</summary>
public bool acceptCaravan = true;
/// <summary>Trader caravans and tribute collectors camp next to this zone.</summary>
public bool campHere;
protected override Color NextZoneColor => ZoneColor;
/// <summary>The map, or null while the zone is loaded but not yet registered (zoneManager unset).</summary>
public Map MapOrNull => zoneManager?.map;
public Zone_Delivery()
{
}
public Zone_Delivery(ZoneManager zoneManager) : base("BlaxZones_DeliveryZone".Translate(), zoneManager)
{
filter = DeliveryFilters.NewDefaultFilter();
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Deep.Look(ref filter, "filter");
Scribe_Values.Look(ref priority, "priority", StoragePriority.Normal);
Scribe_Values.Look(ref acceptOrbital, "acceptOrbital", true);
Scribe_Values.Look(ref acceptRewards, "acceptRewards", true);
Scribe_Values.Look(ref acceptCaravan, "acceptCaravan", true);
Scribe_Values.Look(ref campHere, "campHere", false);
if (Scribe.mode == LoadSaveMode.PostLoadInit)
{
filter ??= DeliveryFilters.NewDefaultFilter();
if (priority == StoragePriority.Unstored)
priority = StoragePriority.Normal;
}
}
/// <summary>Whether this zone takes deliveries from the source (its checkbox).</summary>
public bool AcceptsSource(DeliverySource source)
{
switch (source)
{
case DeliverySource.Orbital:
return acceptOrbital;
case DeliverySource.Rewards:
return acceptRewards;
case DeliverySource.Caravan:
return acceptCaravan;
default:
return false;
}
}
/// <summary>The zone's own filter plus the never-routed refusals. Not the cell rules.</summary>
public bool Accepts(Thing thing)
{
if (DeliveryFilters.NeverRouted(thing))
return false;
return filter == null || filter.Allows(thing);
}
/// <summary>Copies the player's choices (filter, priority, sources, camp) from another zone. Never the cells.</summary>
public void CopySettingsFrom(Zone_Delivery other)
{
if (other == null || other == this)
return;
filter ??= new ThingFilter();
if (other.filter != null)
filter.CopyAllowancesFrom(other.filter);
priority = other.priority;
acceptOrbital = other.acceptOrbital;
acceptRewards = other.acceptRewards;
acceptCaravan = other.acceptCaravan;
campHere = other.campHere;
}
public override void AddCell(IntVec3 c)
{
base.AddCell(c);
MapComponent_BlaxZones.Of(MapOrNull)?.Notify_ZoneCellsChanged(this);
}
public override void RemoveCell(IntVec3 c)
{
Map map = MapOrNull;
base.RemoveCell(c);
MapComponent_BlaxZones.Of(map)?.Notify_ZoneCellsChanged(this);
}
public override void PostDeregister()
{
Map map = MapOrNull;
base.PostDeregister();
MapComponent_BlaxZones.Of(map)?.Notify_ZoneRemoved(this);
}
public override string GetInspectString()
{
var sb = new StringBuilder(base.GetInspectString() ?? "");
try
{
MapComponent_BlaxZones comp = MapComponent_BlaxZones.Of(MapOrNull);
string rules = comp?.InspectLineFor(this);
if (!rules.NullOrEmpty())
{
if (sb.Length > 0)
sb.AppendLine();
sb.Append(rules);
}
}
catch (System.Exception e)
{
ZonesLog.Once("Delivery zone inspect string", e);
}
return sb.ToString();
}
}
}