133 lines
6.1 KiB
C#
133 lines
6.1 KiB
C#
|
|
using System;
|
||
|
|
using HarmonyLib;
|
||
|
|
using UnityEngine;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxZones
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Global settings. Every feature has a toggle; zone storage has none, it is the save format.
|
||
|
|
/// Per-zone choices live on the zone (Zone_Delivery), not here.
|
||
|
|
/// </summary>
|
||
|
|
public class BlaxZonesSettings : ModSettings
|
||
|
|
{
|
||
|
|
/// <summary>The delivery zone designators show in the Zone menu. Off hides them; existing zones stay and still route.</summary>
|
||
|
|
public bool deliveryZones = true;
|
||
|
|
/// <summary>What the player buys from orbital traders, and the silver from selling, lands in delivery zones.</summary>
|
||
|
|
public bool routeOrbital = true;
|
||
|
|
/// <summary>When every zone cell is taken, pods land next to the top zone. Off: overflow goes to the vanilla spot.</summary>
|
||
|
|
public bool overflowNearZone = true;
|
||
|
|
/// <summary>How far from the top zone overflow pods may land. Never below 5: the vanilla search can hang below it.</summary>
|
||
|
|
public int overflowRadius = DefaultOverflowRadius;
|
||
|
|
/// <summary>Trader caravans and tribute collectors camp next to a zone with "Caravans camp here" ticked.</summary>
|
||
|
|
public bool campAtZones = true;
|
||
|
|
|
||
|
|
public const int DefaultOverflowRadius = 16;
|
||
|
|
public const int MinOverflowRadius = 5;
|
||
|
|
public const int MaxOverflowRadius = 30;
|
||
|
|
|
||
|
|
/// <summary>The overflow radius as the router must use it, clamped to 5..30 whatever the file says.</summary>
|
||
|
|
public int OverflowRadiusClamped => Mathf.Clamp(overflowRadius, MinOverflowRadius, MaxOverflowRadius);
|
||
|
|
|
||
|
|
public override void ExposeData()
|
||
|
|
{
|
||
|
|
base.ExposeData();
|
||
|
|
Scribe_Values.Look(ref deliveryZones, "deliveryZones", true);
|
||
|
|
Scribe_Values.Look(ref routeOrbital, "routeOrbital", true);
|
||
|
|
Scribe_Values.Look(ref overflowNearZone, "overflowNearZone", true);
|
||
|
|
Scribe_Values.Look(ref overflowRadius, "overflowRadius", DefaultOverflowRadius);
|
||
|
|
Scribe_Values.Look(ref campAtZones, "campAtZones", true);
|
||
|
|
if (Scribe.mode == LoadSaveMode.PostLoadInit || Scribe.mode == LoadSaveMode.LoadingVars)
|
||
|
|
overflowRadius = Mathf.Clamp(overflowRadius, MinOverflowRadius, MaxOverflowRadius);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Reset()
|
||
|
|
{
|
||
|
|
var d = new BlaxZonesSettings();
|
||
|
|
deliveryZones = d.deliveryZones;
|
||
|
|
routeOrbital = d.routeOrbital;
|
||
|
|
overflowNearZone = d.overflowNearZone;
|
||
|
|
overflowRadius = d.overflowRadius;
|
||
|
|
campAtZones = d.campAtZones;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class BlaxZonesMod : Mod
|
||
|
|
{
|
||
|
|
public static BlaxZonesSettings Settings;
|
||
|
|
|
||
|
|
/// <summary>The settings, or defaults before the mod class exists, so nothing reads null while loading.</summary>
|
||
|
|
public static BlaxZonesSettings S => Settings ?? (fallback ??= new BlaxZonesSettings());
|
||
|
|
private static BlaxZonesSettings fallback;
|
||
|
|
|
||
|
|
public BlaxZonesMod(ModContentPack content) : base(content)
|
||
|
|
{
|
||
|
|
Settings = GetSettings<BlaxZonesSettings>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public override string SettingsCategory() => "Blax Zones";
|
||
|
|
|
||
|
|
public override void DoSettingsWindowContents(Rect inRect)
|
||
|
|
{
|
||
|
|
BlaxZonesSettings s = Settings;
|
||
|
|
var l = new Listing_Standard();
|
||
|
|
l.maxOneColumn = true;
|
||
|
|
l.Begin(inRect);
|
||
|
|
|
||
|
|
l.CheckboxLabeled("BlaxZones_SettingDeliveryZones".Translate(), ref s.deliveryZones, "BlaxZones_SettingDeliveryZonesTip".Translate());
|
||
|
|
l.CheckboxLabeled("BlaxZones_SettingRouteOrbital".Translate(), ref s.routeOrbital, "BlaxZones_SettingRouteOrbitalTip".Translate());
|
||
|
|
l.CheckboxLabeled("BlaxZones_SettingOverflowNearZone".Translate(), ref s.overflowNearZone, "BlaxZones_SettingOverflowNearZoneTip".Translate());
|
||
|
|
if (s.overflowNearZone)
|
||
|
|
{
|
||
|
|
s.overflowRadius = Mathf.RoundToInt(l.SliderLabeled("BlaxZones_SettingOverflowRadius".Translate(s.OverflowRadiusClamped),
|
||
|
|
s.OverflowRadiusClamped, BlaxZonesSettings.MinOverflowRadius, BlaxZonesSettings.MaxOverflowRadius, 0.6f,
|
||
|
|
"BlaxZones_SettingOverflowRadiusTip".Translate()));
|
||
|
|
s.overflowRadius = Mathf.Clamp(s.overflowRadius, BlaxZonesSettings.MinOverflowRadius, BlaxZonesSettings.MaxOverflowRadius);
|
||
|
|
}
|
||
|
|
l.GapLine();
|
||
|
|
|
||
|
|
l.CheckboxLabeled("BlaxZones_SettingCampAtZones".Translate(), ref s.campAtZones, "BlaxZones_SettingCampAtZonesTip".Translate());
|
||
|
|
|
||
|
|
l.Gap();
|
||
|
|
if (l.ButtonText("BlaxZones_SettingsReset".Translate()))
|
||
|
|
s.Reset();
|
||
|
|
l.End();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Applies the Harmony patches after every Mod class exists (so the Harmony mod has set up
|
||
|
|
/// its 1.7 backend), one class at a time so a single broken target can't take the rest down.
|
||
|
|
/// </summary>
|
||
|
|
[StaticConstructorOnStartup]
|
||
|
|
public static class BlaxZonesStartup
|
||
|
|
{
|
||
|
|
public const string HarmonyId = "blax.zones";
|
||
|
|
public static readonly Harmony Harmony = new Harmony(HarmonyId);
|
||
|
|
|
||
|
|
/// <summary>Patch classes that failed to apply, by type name. Read by the smoke test.</summary>
|
||
|
|
public static readonly System.Collections.Generic.List<string> FailedPatches = new System.Collections.Generic.List<string>();
|
||
|
|
|
||
|
|
static BlaxZonesStartup()
|
||
|
|
{
|
||
|
|
int ok = 0;
|
||
|
|
foreach (Type type in typeof(BlaxZonesStartup).Assembly.GetTypes())
|
||
|
|
{
|
||
|
|
if (!type.IsDefined(typeof(HarmonyPatch), false))
|
||
|
|
continue;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
Harmony.CreateClassProcessor(type).Patch();
|
||
|
|
ok++;
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
FailedPatches.Add(type.Name);
|
||
|
|
Log.Error("[Blax Zones] Patch " + type.Name + " failed: " + e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Log.Message("[Blax Zones] " + ok + " patches applied" + (FailedPatches.Count > 0 ? ", " + FailedPatches.Count + " failed" : ""));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|