Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 6 additions & 187 deletions SafeHaven/SafeHaven/SafeHavenModSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2563,13 +2563,12 @@ internal sealed class SafeZoneConfig
Array.Empty<BlockTierEntry>(),
new[]
{
new BlockTierEntry("game:book-*", scoreStudy: 1)
new BlockTierEntry("game:book-*")
});

private readonly Dictionary<SafeZoneTier, ClusterBonusSettings> clusterBonuses;
private readonly SafeZoneConfigFile file;
private readonly List<DecorationScoreEntry> decorationScoreEntries;
private readonly List<StudyScoreEntry> studyScoreEntries;

public AssetLocation[] Tier0Patterns { get; }
public AssetLocation[] Tier1Patterns { get; }
Expand All @@ -2594,7 +2593,6 @@ private SafeZoneConfig(
Dictionary<SafeZoneTier, ClusterBonusSettings> clusterBonuses,
SafeZoneConfigFile file,
List<DecorationScoreEntry> decorationScores,
List<StudyScoreEntry> studyScores,
bool enableTieredRangeBonus,
bool enableClusterRangeBonus,
bool enableDecorativeStatBonus,
Expand All @@ -2610,7 +2608,6 @@ private SafeZoneConfig(
this.clusterBonuses = clusterBonuses;
this.file = file;
decorationScoreEntries = decorationScores ?? new List<DecorationScoreEntry>();
studyScoreEntries = studyScores ?? new List<StudyScoreEntry>();
EnableTieredRangeBonus = enableTieredRangeBonus;
EnableClusterRangeBonus = enableClusterRangeBonus;
EnableDecorativeStatBonus = enableDecorativeStatBonus;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<DecorationScoreEntry> decorationScores = BuildDecorationScoreEntries(defaults.TierDecorative, logger);
List<StudyScoreEntry> studyScores = BuildStudyScoreEntries(defaults.TierStudy, logger);

Dictionary<SafeZoneTier, ClusterBonusSettings> bonuses = new();
foreach (SafeZoneTier tier in ConfigurableTiers)
Expand All @@ -2788,7 +2760,6 @@ private static SafeZoneConfig BuildConfig(SafeZoneConfigFile file, BlockTierData
bonuses,
file,
decorationScores,
studyScores,
enableTiered,
enableCluster,
enableDecorative,
Expand Down Expand Up @@ -3020,38 +2991,6 @@ private static List<DecorationScoreEntry> BuildDecorationScoreEntries(IEnumerabl
return scores;
}

private static List<StudyScoreEntry> BuildStudyScoreEntries(IEnumerable<BlockTierEntry> entries, ILogger logger)
{
List<StudyScoreEntry> 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"));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4055,7 +3982,6 @@ private bool TryCollectCluster(BlockPos start, HashSet<BlockKey> processed, out
clusterInfo = SafeZoneClusterInfo.Empty;
clusterBonusEligibility = new List<bool>();
List<int> clusterDecorationScores = new();
List<int> clusterStudyScores = new();

if (!blockAccessor.IsValidPos(start))
{
Expand Down Expand Up @@ -4105,7 +4031,6 @@ private bool TryCollectCluster(BlockPos start, HashSet<BlockKey> processed, out
clusterBlocks.Add(currentCopy);
clusterTiers.Add(currentTier);
clusterDecorationScores.Add(decorationScore);
clusterStudyScores.Add(CountStudyItems(current));

foreach (BlockFacing facing in BlockFacing.ALLFACES)
{
Expand Down Expand Up @@ -4142,7 +4067,7 @@ private bool TryCollectCluster(BlockPos start, HashSet<BlockKey> 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)
{
Expand Down Expand Up @@ -4238,7 +4163,7 @@ private static bool IsClusterKeyBetter(BlockKey candidate, BlockKey current)
return candidate.Z < current.Z;
}

private static ClusterStats BuildClusterStats(List<BlockPos> clusterBlocks, List<SafeZoneTier> clusterTiers, List<int> clusterDecorationScores, List<int> clusterStudyScores, int probeLimit, List<bool> clusterBonusEligibility)
private static ClusterStats BuildClusterStats(List<BlockPos> clusterBlocks, List<SafeZoneTier> clusterTiers, List<int> clusterDecorationScores, int probeLimit, List<bool> clusterBonusEligibility)
{
ClusterStats stats = new();
if (clusterBlocks.Count == 0)
Expand All @@ -4265,8 +4190,7 @@ private static ClusterStats BuildClusterStats(List<BlockPos> 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;
Expand Down Expand Up @@ -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++;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<IInventory> 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<IInventory> 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);
Expand Down
16 changes: 8 additions & 8 deletions SafeHaven/SafeHaven/assets/safehaven/config/blocktier.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}