From efde79405c913798f8d4115c7d36b8e184e7fb2e Mon Sep 17 00:00:00 2001 From: Interzoneism <113020222+Interzoneism@users.noreply.github.com> Date: Sat, 1 Nov 2025 22:47:13 +0100 Subject: [PATCH] Simplify tier study block counting --- SafeHaven/SafeHaven/SafeHavenModSystem.cs | 193 +----------------- .../assets/safehaven/config/blocktier.json | 16 +- 2 files changed, 14 insertions(+), 195 deletions(-) diff --git a/SafeHaven/SafeHaven/SafeHavenModSystem.cs b/SafeHaven/SafeHaven/SafeHavenModSystem.cs index e64193d..77a4273 100644 --- a/SafeHaven/SafeHaven/SafeHavenModSystem.cs +++ b/SafeHaven/SafeHaven/SafeHavenModSystem.cs @@ -2563,13 +2563,12 @@ internal sealed class SafeZoneConfig Array.Empty(), new[] { - new BlockTierEntry("game:book-*", scoreStudy: 1) + new BlockTierEntry("game:book-*") }); private readonly Dictionary clusterBonuses; private readonly SafeZoneConfigFile file; private readonly List decorationScoreEntries; - private readonly List studyScoreEntries; public AssetLocation[] Tier0Patterns { get; } public AssetLocation[] Tier1Patterns { get; } @@ -2594,7 +2593,6 @@ private SafeZoneConfig( Dictionary clusterBonuses, SafeZoneConfigFile file, List decorationScores, - List studyScores, bool enableTieredRangeBonus, bool enableClusterRangeBonus, bool enableDecorativeStatBonus, @@ -2610,7 +2608,6 @@ private SafeZoneConfig( this.clusterBonuses = clusterBonuses; this.file = file; decorationScoreEntries = decorationScores ?? new List(); - studyScoreEntries = studyScores ?? new List(); EnableTieredRangeBonus = enableTieredRangeBonus; EnableClusterRangeBonus = enableClusterRangeBonus; EnableDecorativeStatBonus = enableDecorativeStatBonus; @@ -2671,30 +2668,6 @@ public int GetDecorativeScore(AssetLocation code) return bestScore; } - public int GetStudyScore(AssetLocation code) - { - if (studyScoreEntries.Count == 0) - { - return 0; - } - - int bestScore = 0; - foreach (StudyScoreEntry entry in studyScoreEntries) - { - if (!WildcardUtil.Match(entry.Pattern, code)) - { - continue; - } - - if (entry.Score > bestScore) - { - bestScore = entry.Score; - } - } - - return bestScore; - } - public SafeZoneSettingsSnapshot GetSettingsSnapshot() { return new SafeZoneSettingsSnapshot( @@ -2763,7 +2736,6 @@ private static SafeZoneConfig BuildConfig(SafeZoneConfigFile file, BlockTierData AssetLocation[] tierDecorative = ParsePatternsOrDefault(file, SafeZoneTier.TierDecorative, defaults, logger); AssetLocation[] tierFunctional = ParsePatternsOrDefault(file, SafeZoneTier.TierFunctional, defaults, logger); List decorationScores = BuildDecorationScoreEntries(defaults.TierDecorative, logger); - List studyScores = BuildStudyScoreEntries(defaults.TierStudy, logger); Dictionary bonuses = new(); foreach (SafeZoneTier tier in ConfigurableTiers) @@ -2788,7 +2760,6 @@ private static SafeZoneConfig BuildConfig(SafeZoneConfigFile file, BlockTierData bonuses, file, decorationScores, - studyScores, enableTiered, enableCluster, enableDecorative, @@ -3020,38 +2991,6 @@ private static List BuildDecorationScoreEntries(IEnumerabl return scores; } - private static List BuildStudyScoreEntries(IEnumerable entries, ILogger logger) - { - List scores = new(); - if (entries == null) - { - return scores; - } - - foreach (BlockTierEntry entry in entries) - { - if (entry == null || entry.ScoreStudy <= 0) - { - continue; - } - - try - { - AssetLocation pattern = AssetLocation.Create(entry.Code); - scores.Add(new StudyScoreEntry(pattern, entry.ScoreStudy)); - } - catch (Exception ex) - { - logger.Warning( - "[SafeHaven] Ignored invalid study score entry '{0}': {1}", - entry.Code, - ex.Message); - } - } - - return scores; - } - private static BlockTierData LoadDefaultBlockTiers(ICoreServerAPI api, ILogger logger) { IAsset? asset = api.Assets.TryGet(new AssetLocation("safehaven", "config/blocktier.json")); @@ -3168,18 +3107,6 @@ public DecorationScoreEntry(AssetLocation pattern, int score) public int Score { get; } } - private readonly struct StudyScoreEntry - { - public StudyScoreEntry(AssetLocation pattern, int score) - { - Pattern = pattern; - Score = score; - } - - public AssetLocation Pattern { get; } - public int Score { get; } - } - private static string GetTierKey(SafeZoneTier tier) { return tier switch @@ -4055,7 +3982,6 @@ private bool TryCollectCluster(BlockPos start, HashSet processed, out clusterInfo = SafeZoneClusterInfo.Empty; clusterBonusEligibility = new List(); List clusterDecorationScores = new(); - List clusterStudyScores = new(); if (!blockAccessor.IsValidPos(start)) { @@ -4105,7 +4031,6 @@ private bool TryCollectCluster(BlockPos start, HashSet processed, out clusterBlocks.Add(currentCopy); clusterTiers.Add(currentTier); clusterDecorationScores.Add(decorationScore); - clusterStudyScores.Add(CountStudyItems(current)); foreach (BlockFacing facing in BlockFacing.ALLFACES) { @@ -4142,7 +4067,7 @@ private bool TryCollectCluster(BlockPos start, HashSet processed, out return false; } - ClusterStats stats = BuildClusterStats(clusterBlocks, clusterTiers, clusterDecorationScores, clusterStudyScores, ClusterProbeLimit, clusterBonusEligibility); + ClusterStats stats = BuildClusterStats(clusterBlocks, clusterTiers, clusterDecorationScores, ClusterProbeLimit, clusterBonusEligibility); if (clusterBonusEligibility.Count < clusterBlocks.Count) { @@ -4238,7 +4163,7 @@ private static bool IsClusterKeyBetter(BlockKey candidate, BlockKey current) return candidate.Z < current.Z; } - private static ClusterStats BuildClusterStats(List clusterBlocks, List clusterTiers, List clusterDecorationScores, List clusterStudyScores, int probeLimit, List clusterBonusEligibility) + private static ClusterStats BuildClusterStats(List clusterBlocks, List clusterTiers, List clusterDecorationScores, int probeLimit, List clusterBonusEligibility) { ClusterStats stats = new(); if (clusterBlocks.Count == 0) @@ -4265,8 +4190,7 @@ private static ClusterStats BuildClusterStats(List clusterBlocks, List } clusterBonusEligibility.Add(eligibleForBonus); int decorationScore = (i < clusterDecorationScores.Count) ? clusterDecorationScores[i] : 0; - int studyScore = (i < clusterStudyScores.Count) ? clusterStudyScores[i] : 0; - stats.Register(tier, eligibleForBonus, decorationScore, studyScore); + stats.Register(tier, eligibleForBonus, decorationScore); } return stats; @@ -4443,7 +4367,7 @@ private struct ClusterStats public int DecorativeScoreTotal { get; private set; } - public void Register(SafeZoneTier tier, bool eligibleForBonus, int decorativeScore, int studyCount) + public void Register(SafeZoneTier tier, bool eligibleForBonus, int decorativeScore) { Total++; @@ -4491,13 +4415,9 @@ public void Register(SafeZoneTier tier, bool eligibleForBonus, int decorativeSco TierFunctionalCount++; break; case SafeZoneTier.TierStudy: + TierStudyCount++; break; } - - if (studyCount > 0) - { - TierStudyCount += studyCount; - } } public int GetEligibleCount(SafeZoneTier tier) @@ -4615,107 +4535,6 @@ private SafeZoneTier ResolveTier(Block block, BlockPos? position, out int decora return SafeZoneTier.TierMisc; } - private int CountStudyItems(BlockPos position) - { - BlockEntity? entity = blockAccessor.GetBlockEntity(position); - if (entity == null) - { - return 0; - } - - HashSet processed = new(); - int total = 0; - - if (entity is IBlockEntityContainer container) - { - total += CountStudyItems(container.Inventory, processed); - } - - if (entity is BlockEntityItemPile itemPile) - { - total += CountStudyItems(itemPile.inventory, processed); - } - - return total; - } - - private int CountStudyItems(IInventory? inventory, HashSet processed) - { - if (inventory == null || !processed.Add(inventory)) - { - return 0; - } - - int total = 0; - foreach (ItemSlot slot in inventory) - { - ItemStack? stack = slot?.Itemstack; - if (stack == null || stack.StackSize <= 0) - { - continue; - } - - total += CountStudyItems(stack); - } - - return total; - } - - private int CountStudyItems(ItemStack? stack) - { - if (stack == null || stack.StackSize <= 0) - { - return 0; - } - - CollectibleObject? collectible = stack.Collectible; - if (collectible == null || collectible is Block) - { - return 0; - } - - AssetLocation? code = collectible.Code; - if (code == null) - { - return 0; - } - - int score = config.GetStudyScore(code); - if (score <= 0 && !IsBookLike(collectible)) - { - return 0; - } - - if (score <= 0) - { - score = 1; - } - - return stack.StackSize * score; - } - - private static bool IsBookLike(CollectibleObject collectible) - { - if (collectible is Block) - { - return false; - } - - AssetLocation? code = collectible.Code; - if (code == null) - { - return false; - } - - string path = code.Path ?? string.Empty; - if (path.IndexOf("book", StringComparison.OrdinalIgnoreCase) >= 0) - { - return true; - } - - return collectible.Attributes?["bookshelveable"]?.AsBool(false) == true; - } - private bool HasGroundStorageContents(BlockPos position) { BlockEntity? entity = blockAccessor.GetBlockEntity(position); diff --git a/SafeHaven/SafeHaven/assets/safehaven/config/blocktier.json b/SafeHaven/SafeHaven/assets/safehaven/config/blocktier.json index f2fc4cc..b79a78d 100644 --- a/SafeHaven/SafeHaven/assets/safehaven/config/blocktier.json +++ b/SafeHaven/SafeHaven/assets/safehaven/config/blocktier.json @@ -141,12 +141,12 @@ { "code": "game:helvehammerbase-*", "scoreFunctional": 0 } ], // Learning, invention, and technology-themed builds. "tierStudy": [ - { "code": "game:bookshelf-*", "scoreStudy": 1 }, - { "code": "game:scrollrack-*", "scoreStudy": 1 }, - { "code": "game:clutter-abacus*", "scoreStudy": 1 }, - { "code": "game:clutter-globe*", "scoreStudy": 1 }, - { "code": "game:clutter-astrolabe", "scoreStudy": 1 }, - { "code": "game:clutter-drafting-table-*", "scoreStudy": 1 }, - { "code": "game:book-*", "scoreStudy": 1 } - ] // Books and study materials contribute to scholarly clusters. + { "code": "game:bookshelf-*" }, + { "code": "game:scrollrack-*" }, + { "code": "game:clutter-abacus*" }, + { "code": "game:clutter-globe*" }, + { "code": "game:clutter-astrolabe" }, + { "code": "game:clutter-drafting-table-*" }, + { "code": "game:book-*" } + ] // Books and study materials contribute to scholarly clusters, each counting once. }