From 0fc71e97efffe7d35700d0146efa5b823b4f42cb Mon Sep 17 00:00:00 2001 From: gnembon Date: Sun, 7 Jun 2020 16:55:32 -0300 Subject: [PATCH] added mobcaps to API --- docs/scarpet/api/Auxiliary.md | 7 +++ .../carpet/mixins/SpawnHelperInnerMixin.java | 2 +- .../java/carpet/script/CarpetExpression.java | 45 +++++++++++++++++++ src/main/java/carpet/utils/SpawnReporter.java | 7 ++- 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/docs/scarpet/api/Auxiliary.md b/docs/scarpet/api/Auxiliary.md index d4de9940..d61bef0e 100644 --- a/docs/scarpet/api/Auxiliary.md +++ b/docs/scarpet/api/Auxiliary.md @@ -332,6 +332,13 @@ world-localized block, so not `block('stone')`, or a string representing a dimen ### `view_distance()` Returns the view distance of the server. +### `mob_counts()`, `mob_counts(category)` 1.16+ + +Returns either a map of mob categories with its respective counts and capacities (a.k.a. mobcaps) or just a tuple +of count and limit for a specific category. If a category was not spawning for whatever reason it may not be +returned from `mob_counts()`, but could be retrieved for `mob_counts(category)`. Returned counts is what spawning +algorithm has taken in to account last time mobs spawned. + ### `schedule(delay, function, args...)` Schedules a user defined function to run with a specified `delay` ticks of delay. Scheduled functions run at the end diff --git a/src/main/java/carpet/mixins/SpawnHelperInnerMixin.java b/src/main/java/carpet/mixins/SpawnHelperInnerMixin.java index f2f48e1c..30451bd6 100644 --- a/src/main/java/carpet/mixins/SpawnHelperInnerMixin.java +++ b/src/main/java/carpet/mixins/SpawnHelperInnerMixin.java @@ -29,7 +29,7 @@ public class SpawnHelperInnerMixin implements SpawnHelperInnerInterface private void changeMobCaps(SpawnGroup entityCategory, CallbackInfoReturnable cir) { int newCap = (int) ((double)entityCategory.getCapacity()*(Math.pow(2.0,(SpawnReporter.mobcap_exponent/4)))); - int i = newCap * spawningChunkCount / (int)Math.pow(17.0D, 2.0D); + int i = newCap * spawningChunkCount / SpawnReporter.MAGIC_NUMBER; cir.setReturnValue(groupToCount.getInt(entityCategory) < i); } diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 2df7e305..91a5c7b2 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -38,11 +38,13 @@ import carpet.script.value.ThreadValue; import carpet.script.value.Value; import carpet.utils.BlockInfo; import carpet.utils.Messenger; +import carpet.utils.SpawnReporter; import com.google.common.collect.Sets; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.LongSet; +import it.unimi.dsi.fastutil.objects.Object2IntMap; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; @@ -52,6 +54,7 @@ import net.minecraft.block.StructureBlock; import net.minecraft.block.entity.BlockEntity; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.FallingBlockEntity; +import net.minecraft.entity.SpawnGroup; import net.minecraft.entity.ai.pathing.NavigationType; import net.minecraft.item.BlockItem; import net.minecraft.item.Item; @@ -3233,6 +3236,47 @@ public class CarpetExpression }); } + private void API_GaemMonitoring() + { + // game processed snooper functions + this.expr.addLazyFunction("get_mob_counts", -1, (c, t, lv) -> + { + CarpetContext cc = (CarpetContext)c; + ServerWorld world = cc.s.getWorld(); + SpawnHelper.Info info = world.getChunkManager().getSpawnInfo(); + if (info == null) return LazyValue.NULL; + Object2IntMap mobcounts = info.getGroupToCount(); + int chunks = info.getSpawningChunkCount(); + Value retVal; + if (lv.size() == 0) + { + Map retDict = new HashMap<>(); + for (SpawnGroup category: mobcounts.keySet()) + { + int currentCap = (int)(category.getCapacity() * chunks / SpawnReporter.currentMagicNumber()); // MAGIC_NUMBER + retDict.put( + new StringValue(category.asString().toLowerCase(Locale.ROOT)), + ListValue.of( + new NumericValue(mobcounts.getInt(category)), + new NumericValue(currentCap)) + ); + } + retVal = MapValue.wrap(retDict); + } + else + { + String catString = lv.get(0).evalValue(c).getString(); + SpawnGroup cat = SpawnGroup.method_28307(catString.toLowerCase(Locale.ROOT)); + if (cat == null) throw new InternalExpressionException("Unreconized mob category: "+catString); + retVal = ListValue.of( + new NumericValue(mobcounts.getInt(cat)), + new NumericValue((int)(cat.getCapacity() * chunks / SpawnReporter.currentMagicNumber())) + ); + } + return (_c, _t) -> retVal; + }); + } + private Stat getStat(StatType type, Identifier id) { T key = type.getRegistry().get(id); @@ -3263,6 +3307,7 @@ public class CarpetExpression API_AuxiliaryAspects(); API_Scoreboard(); API_Interapperability(); + API_GaemMonitoring(); } public boolean fillAndScanCommand(ScriptHost host, int x, int y, int z) diff --git a/src/main/java/carpet/utils/SpawnReporter.java b/src/main/java/carpet/utils/SpawnReporter.java index 6fa72cf9..a7fa3fcd 100644 --- a/src/main/java/carpet/utils/SpawnReporter.java +++ b/src/main/java/carpet/utils/SpawnReporter.java @@ -82,6 +82,11 @@ public class SpawnReporter local_spawns.put(cat, local_spawns.get(cat)+1); } + public static final int MAGIC_NUMBER = (int)Math.pow(17.0D, 2.0D); + public static double currentMagicNumber() + { + return MAGIC_NUMBER / (Math.pow(2.0,(SpawnReporter.mobcap_exponent/4))); + } public static List printMobcapsForDimension(ServerWorld world, boolean multiline) { @@ -103,7 +108,7 @@ public class SpawnReporter for (SpawnGroup enumcreaturetype : SpawnGroup.values()) { int cur = dimCounts.getOrDefault(enumcreaturetype, -1); - int max = chunkcount * (int)((double)enumcreaturetype.getCapacity() * (Math.pow(2.0,(SpawnReporter.mobcap_exponent/4)))) / (int)Math.pow(17.0D, 2.0D); // from ServerChunkManager.CHUNKS_ELIGIBLE_FOR_SPAWNING + int max = chunkcount * (int)((double)enumcreaturetype.getCapacity() / currentMagicNumber()); // from ServerChunkManager.CHUNKS_ELIGIBLE_FOR_SPAWNING String color = Messenger.heatmap_color(cur, max); String mobColor = Messenger.creatureTypeColor(enumcreaturetype); if (multiline)