rimworld-blax-laws/Source/BlaxLaws/Court/CourtPlace.cs

150 lines
7.0 KiB
C#

using System.Collections.Generic;
using RimWorld;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// One stored court place (design: "Court place"). Only the building reference and the tick it was set
/// are saved, never a map: the map is read from the building, so a place that is uninstalled into a crate
/// and installed on another map follows it (RimWorld/MinifyUtility.cs:15-23 keeps the same instance and
/// ThingID; RimWorld/Blueprint_Install.cs:99-112). A destroyed building saves as "null"
/// (Verse/Scribe_References.cs:60-78) and the entry is dropped after the load.
/// </summary>
public sealed class CourtPlaceEntry : IExposable
{
public Building building;
/// <summary>When the player set this place. The newest place on a map wins.</summary>
public int setTick;
public CourtPlaceEntry()
{
}
public CourtPlaceEntry(Building building, int setTick)
{
this.building = building;
this.setTick = setTick;
}
public void ExposeData()
{
Scribe_References.Look(ref building, "building");
Scribe_Values.Look(ref setTick, "setTick");
}
}
/// <summary>
/// The court place of each map: the public API that the switch on a building, the Laws tab and the later
/// court features (sessions, "Court on its own", public punishments) read and write. The state lives in
/// GameComponent_BlaxLaws; every read goes through Valid, so a deconstructed, destroyed, claimed or
/// crated place reads as no place at once, with no Destroy or DeSpawn hook.
/// </summary>
public static class CourtPlace
{
// Per-def cache of the kind rule. Defs, not game state.
private static readonly Dictionary<ThingDef, bool> eligibleDefs = new Dictionary<ThingDef, bool>();
private static DesignationCategoryDef furnitureCategory;
private static bool furnitureLooked;
/// <summary>
/// The building can be a court place right now: spawned, not destroyed and the player's
/// (Verse/Thing.cs:180-209, 390). A failed check reads as no place.
/// </summary>
public static bool Valid(Building b) =>
b != null && !b.Destroyed && b.Spawned && b.Faction != null && b.Faction == Faction.OfPlayer;
/// <summary>Valid and of a kind that can hold court. The switch, the picker and Set all use this.</summary>
public static bool CanBe(Building b) => Valid(b) && EligibleDef(b.def);
/// <summary>
/// The kind rule, per def: tables (Verse/ThingDef.cs:1008, or any eating surface), every gathering spot
/// (CompGatherSpot: the gather tables, the campfire, Royalty's instruments, Ideology's autobong), the
/// party spot (it has no CompGatherSpot, Core Buildings_Misc.xml:1188-1215), Royalty's thrones
/// (Building_Throne), Ideology's lectern and ritual spot (RimWorld/ThingDefOf.cs:687, 813, null without
/// Ideology), and other minifiable furniture (designation category Furniture). Impassable defs are refused
/// except thrones, so walls never qualify; no vanilla throne is impassable (Verse/BuildableDef.cs:13).
/// </summary>
public static bool EligibleDef(ThingDef d)
{
if (d == null)
return false;
if (eligibleDefs.TryGetValue(d, out bool known))
return known;
bool result = ComputeEligible(d);
eligibleDefs[d] = result;
return result;
}
private static bool ComputeEligible(ThingDef d)
{
if (d.category != ThingCategory.Building || d.building == null)
return false;
bool throne = d.thingClass != null && typeof(Building_Throne).IsAssignableFrom(d.thingClass);
if (d.passability == Traversability.Impassable && !throne)
return false;
if (throne)
return true;
if (d.IsTable || d.surfaceType == SurfaceType.Eat || d.HasComp(typeof(CompGatherSpot)))
return true;
if (d == ThingDefOf.PartySpot || d == ThingDefOf.RitualSpot || d == ThingDefOf.Lectern)
return true;
if (!furnitureLooked)
{
furnitureCategory = DefDatabase<DesignationCategoryDef>.GetNamedSilentFail("Furniture");
furnitureLooked = true;
}
return d.Minifiable && furnitureCategory != null && d.designationCategory == furnitureCategory;
}
/// <summary>The map's court place, or null when it has none or the place fails Valid.</summary>
public static Building For(Map map) => GameComponent_BlaxLaws.Instance?.CourtPlaceOn(map);
/// <summary>The building is its map's court place.</summary>
public static bool IsPlace(Building b) => b != null && b.Spawned && For(b.Map) == b;
/// <summary>
/// Makes the building its map's court place and drops the map's former one, crated ones included.
/// False when CanBe refuses it.
/// </summary>
public static bool Set(Building b)
{
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
if (comp == null || !CanBe(b))
return false;
comp.SetCourtPlace(b);
return true;
}
/// <summary>The map has no court place afterwards. Works with an empty lawbook.</summary>
public static void Clear(Map map) => GameComponent_BlaxLaws.Instance?.ClearCourtPlace(map);
/// <summary>
/// The court room, read when used and never stored (Room.ID changes on every region rebuild,
/// Verse/Room.cs:43, 597-600): vanilla's room stats rule, Gizmo_RoomStats.GetRoomToShowStatsFor
/// (Verse/Gizmo_RoomStats.cs:89-132), which handles passable and impassable buildings and returns null
/// for an unspawned or fogged building, a fogged room and a room without a role (outdoors, huge).
/// </summary>
public static Room RoomOf(Building b) => b != null && b.Spawned ? Gizmo_RoomStats.GetRoomToShowStatsFor(b) : null;
/// <summary>The room is a court room: not null and a proper room (Verse/Room.cs:536).</summary>
public static bool IsProperRoom(Room room) => room != null && room.ProperRoom;
/// <summary>
/// The picker's parameters: buildings only, the court place kind rule as validator, and
/// mapObjectTargetsMustBeAutoAttackable off, since it refuses buildings that aren't targetable
/// (RimWorld/TargetingParameters.cs:55, 277-281).
/// </summary>
public static TargetingParameters TargetParams() => new TargetingParameters
{
canTargetPawns = false,
canTargetBuildings = true,
canTargetItems = false,
canTargetLocations = false,
mapObjectTargetsMustBeAutoAttackable = false,
validator = t => t.Thing is Building b && CanBe(b),
};
}
}