using HarmonyLib; namespace IdleSpectator; /// Era + meta genesis patches from mutation discovery. [HarmonyPatch] public static class MetaEventPatches { [HarmonyPatch(typeof(WorldAgeManager), nameof(WorldAgeManager.startNextAge))] [HarmonyPostfix] public static void PostfixStartNextAge() { MetaInterestFeed.EmitEra(ReadCurrentEraId()); } /// /// Ongoing quake hold. Age change is already covered by /// (called from WorldAgeManager.update when progress completes). /// [HarmonyPatch(typeof(Earthquake), nameof(Earthquake.startQuake))] [HarmonyPostfix] public static void PostfixStartQuake(WorldTile pTile) { if (!Earthquake.isQuakeActive()) { return; } MetaInterestFeed.EmitEarthquake(pTile); } [HarmonyPatch(typeof(CultureManager), nameof(CultureManager.newCulture))] [HarmonyPostfix] public static void PostfixNewCulture(Culture __result) { EmitNew("new_culture", "Culture", 68f, __result, "culture"); } [HarmonyPatch(typeof(ReligionManager), nameof(ReligionManager.newReligion))] [HarmonyPostfix] public static void PostfixNewReligion(Religion __result) { EmitNew("new_religion", "Culture", 72f, __result, "religion"); } [HarmonyPatch(typeof(LanguageManager), nameof(LanguageManager.newLanguage))] [HarmonyPostfix] public static void PostfixNewLanguage(Language __result) { EmitNew("new_language", "Culture", 60f, __result, "language"); } [HarmonyPatch(typeof(ClanManager), nameof(ClanManager.newClan))] [HarmonyPostfix] public static void PostfixNewClan(Clan __result) { EmitNew("new_clan", "Politics", 70f, __result, "clan"); Actor clanFounder = TryReadActor(__result); if (clanFounder != null) { string clanKey = ""; try { clanKey = __result != null ? __result.getID().ToString() : ""; } catch { clanKey = ""; } LifeSagaMemory.RecordFounding(clanFounder, "clan", clanKey, "new_clan"); } } [HarmonyPatch(typeof(ArmyManager), nameof(ArmyManager.newArmy))] [HarmonyPostfix] public static void PostfixNewArmy(Army __result) { EmitNew("new_army", "Politics", 66f, __result, "army"); } [HarmonyPatch(typeof(AllianceManager), nameof(AllianceManager.newAlliance))] [HarmonyPostfix] public static void PostfixNewAlliance(Alliance __result) { EmitNew("new_alliance", "Politics", 78f, __result, "alliance"); } [HarmonyPatch(typeof(CityManager), nameof(CityManager.newCity))] [HarmonyPostfix] public static void PostfixNewCity(City __result) { // Outcome tip (decision intent like build_civ_city_here is Layer B). EmitNew("new_city", "Politics", 86f, __result, "city"); Actor cityFounder = TryReadActor(__result); if (cityFounder != null) { string cityKey = ""; try { cityKey = __result != null ? __result.getID().ToString() : ""; } catch { cityKey = ""; } LifeSagaMemory.RecordFounding(cityFounder, "city", cityKey, "new_city"); } } [HarmonyPatch(typeof(KingdomManager), nameof(KingdomManager.makeNewCivKingdom))] [HarmonyPostfix] public static void PostfixNewCivKingdom(Kingdom __result, Actor pActor) { if (__result == null) { return; } Actor anchor = pActor != null && pActor.isAlive() ? pActor : TryReadActor(__result); if (anchor == null) { return; } // Outcome tip (try_to_start_new_civilization decision intent is Layer B). MetaInterestFeed.EmitMeta( "new_kingdom", "Politics", 92f, anchor, EventReason.MetaNew(anchor, "kingdom")); string kingdomKey = ""; try { kingdomKey = __result.getID().ToString(); } catch { kingdomKey = ""; } LifeSagaMemory.RecordFounding(anchor, "kingdom", kingdomKey, "new_kingdom"); } private static void EmitNew( string eventId, string category, float strength, object meta, string kind) { Actor anchor = TryReadActor(meta); if (anchor == null) { return; } string label = EventReason.MetaNew(anchor, kind); MetaInterestFeed.EmitMeta(eventId, category, strength, anchor, label); } private static Actor TryReadActor(object meta) { if (meta == null) { return null; } try { foreach (string name in new[] { "founder", "creator", "leader", "king", "actor", "unit" }) { object value = meta.GetType().GetField(name)?.GetValue(meta) ?? meta.GetType().GetProperty(name)?.GetValue(meta, null); if (value is Actor actor && actor.isAlive()) { return actor; } } } catch { // ignore } return null; } private static string ReadCurrentEraId() { try { object world = World.world; object ageManager = world?.GetType().GetField("era_manager")?.GetValue(world) ?? world?.GetType().GetProperty("era_manager")?.GetValue(world, null) ?? world?.GetType().GetField("age_manager")?.GetValue(world) ?? MapBox.instance?.GetType().GetField("era_manager")?.GetValue(MapBox.instance); if (ageManager == null) { return "age_unknown"; } object asset = ageManager.GetType().GetMethod("getCurrentAge")?.Invoke(ageManager, null) ?? ageManager.GetType().GetProperty("current_age")?.GetValue(ageManager, null) ?? ageManager.GetType().GetField("current_age")?.GetValue(ageManager); if (asset != null) { object id = asset.GetType().GetField("id")?.GetValue(asset) ?? asset.GetType().GetProperty("id")?.GetValue(asset, null); if (id != null) { return id.ToString(); } } } catch { // ignore } return "age_unknown"; } }