From 796ec4419d4076528cd394cbc037fd2bba0df495 Mon Sep 17 00:00:00 2001 From: Al Winfy <20421383+Alwinfy@users.noreply.github.com> Date: Thu, 23 Jul 2020 11:57:06 -0500 Subject: [PATCH] Overhaul Rafflowsia to scale with flower variety (#3362) * Overhaul Rafflowsia to scale with flower variety * Document the table's generation * Serialize blocks over raw strings * Lexica! * Use Registry.getKey over getRegistryName --- .../subtile/generating/SubTileRafflowsia.java | 87 ++++++++++++++----- .../botania/data/BlockLootProvider.java | 4 +- .../resources/assets/botania/lang/en_us.json | 2 +- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/src/main/java/vazkii/botania/common/block/subtile/generating/SubTileRafflowsia.java b/src/main/java/vazkii/botania/common/block/subtile/generating/SubTileRafflowsia.java index 2f8244f13..e5584ed20 100644 --- a/src/main/java/vazkii/botania/common/block/subtile/generating/SubTileRafflowsia.java +++ b/src/main/java/vazkii/botania/common/block/subtile/generating/SubTileRafflowsia.java @@ -11,9 +11,12 @@ package vazkii.botania.common.block.subtile.generating; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; +import net.minecraft.nbt.StringNBT; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.registry.Registry; +import net.minecraftforge.common.util.Constants; import vazkii.botania.api.subtile.RadiusDescriptor; import vazkii.botania.api.subtile.TileEntityGeneratingFlower; @@ -22,20 +25,66 @@ import vazkii.botania.common.lib.ModTags; import javax.annotation.Nullable; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; + public class SubTileRafflowsia extends TileEntityGeneratingFlower { - public static final String TAG_LAST_FLOWER = "lastFlower"; + public static final String TAG_LAST_FLOWERS = "lastFlowers"; public static final String TAG_LAST_FLOWER_TIMES = "lastFlowerTimes"; + public static final String TAG_STREAK_LENGTH = "streakLength"; @Nullable - private Block lastFlower; - private int lastFlowerTimes; + private List lastFlowers = new LinkedList<>(); + private int streakLength = -1; + private int lastFlowerCount = 0; private static final int RANGE = 5; + // Below table generated from the function: + // f(x) = round(-401.45 + 7.03436 x + 16.0932 x^2 + 7.64878 * 1.25226^x, 100), + // where x is the number of unique flowers in the streak. + // Function created from a best-fit approximation on the sorted raw mana costs of production of each flower. + private static final int[] STREAK_OUTPUTS = { 300, 1100, 1900, 2700, 3500, 4400, 5300, 6300, 7300, 8300, 9400, 10500, 11600, 12800, 14000, 15200, 16500, 17900, 19200, 20700, 22200, 23800, 25400, 27100, 29000, 30900, 33000, 35200, 37700, 40300, 43200, 46500, 50200, 54300, 59100, 64600, 71100, 78600, 87600, 98400 }; + public SubTileRafflowsia() { super(ModSubtiles.RAFFLOWSIA); } + private int getMaxStreak() { + return STREAK_OUTPUTS.length - 1; + } + + private int getValueForStreak(int index) { + // special-case repeated first flowers + if (index != 0) { + lastFlowerCount = 0; + } + return STREAK_OUTPUTS[index] / ++lastFlowerCount; + } + + /** + * Processes a flower, placing it in the appropriate place in the history. + * + * @return the last time the flower showed up in history. + */ + private int processFlower(Block flower) { + for (ListIterator it = lastFlowers.listIterator(); it.hasNext();) { + int index = it.nextIndex(); + Block streakFlower = it.next(); + if (streakFlower == flower) { + it.remove(); + lastFlowers.add(0, streakFlower); + return index; + } + } + lastFlowers.add(0, flower); + if (lastFlowers.size() >= getMaxStreak()) { + lastFlowers.remove(lastFlowers.size() - 1); + } + return getMaxStreak(); + } + @Override public void tickFlower() { super.tickFlower(); @@ -50,17 +99,10 @@ public class SubTileRafflowsia extends TileEntityGeneratingFlower { BlockState state = getWorld().getBlockState(pos); if (state.isIn(ModTags.Blocks.SPECIAL_FLOWERS) && state.getBlock() != ModSubtiles.rafflowsia) { - if (state.getBlock() == lastFlower) { - lastFlowerTimes++; - } else { - lastFlower = state.getBlock(); - lastFlowerTimes = 1; - } - - float mod = 1F / lastFlowerTimes; + streakLength = Math.min(streakLength + 1, processFlower(state.getBlock())); getWorld().destroyBlock(pos, false); - addMana((int) (mana * mod)); + addMana(getValueForStreak(streakLength)); sync(); return; } @@ -74,21 +116,26 @@ public class SubTileRafflowsia extends TileEntityGeneratingFlower { public void writeToPacketNBT(CompoundNBT cmp) { super.writeToPacketNBT(cmp); - if (lastFlower != null) { - cmp.putString(TAG_LAST_FLOWER, Registry.BLOCK.getKey(lastFlower).toString()); + ListNBT flowerList = new ListNBT(); + for (Block flower : lastFlowers) { + flowerList.add(StringNBT.valueOf(Registry.BLOCK.getKey(flower).toString())); } - cmp.putInt(TAG_LAST_FLOWER_TIMES, lastFlowerTimes); + cmp.put(TAG_LAST_FLOWERS, flowerList); + cmp.putInt(TAG_LAST_FLOWER_TIMES, lastFlowerCount); + cmp.putInt(TAG_STREAK_LENGTH, streakLength); } @Override public void readFromPacketNBT(CompoundNBT cmp) { super.readFromPacketNBT(cmp); - ResourceLocation id = ResourceLocation.tryCreate(cmp.getString(TAG_LAST_FLOWER)); - if (id != null) { - lastFlower = Registry.BLOCK.getValue(id).orElse(null); + lastFlowers.clear(); + ListNBT flowerList = cmp.getList(TAG_LAST_FLOWERS, Constants.NBT.TAG_STRING); + for (int i = 0; i < flowerList.size(); i++) { + lastFlowers.add(Registry.BLOCK.getOrDefault(ResourceLocation.tryCreate(flowerList.getString(i)))); } - lastFlowerTimes = cmp.getInt(TAG_LAST_FLOWER_TIMES); + lastFlowerCount = cmp.getInt(TAG_LAST_FLOWER_TIMES); + streakLength = cmp.getInt(TAG_STREAK_LENGTH); } @Override @@ -103,7 +150,7 @@ public class SubTileRafflowsia extends TileEntityGeneratingFlower { @Override public int getMaxMana() { - return 9000; + return 100000; } } diff --git a/src/main/java/vazkii/botania/data/BlockLootProvider.java b/src/main/java/vazkii/botania/data/BlockLootProvider.java index de5ecc9ea..53eaf493a 100644 --- a/src/main/java/vazkii/botania/data/BlockLootProvider.java +++ b/src/main/java/vazkii/botania/data/BlockLootProvider.java @@ -107,8 +107,8 @@ public class BlockLootProvider implements IDataProvider { functionTable.put(ModSubtiles.hydroangeasFloating, b -> genCopyNbt(b, SubTileHydroangeas.TAG_COOLDOWN, TileEntityGeneratingFlower.TAG_PASSIVE_DECAY_TICKS)); functionTable.put(ModSubtiles.munchdew, b -> genCopyNbt(b, SubTileMunchdew.TAG_COOLDOWN)); functionTable.put(ModSubtiles.munchdewFloating, b -> genCopyNbt(b, SubTileMunchdew.TAG_COOLDOWN)); - functionTable.put(ModSubtiles.rafflowsia, b -> genCopyNbt(b, SubTileRafflowsia.TAG_LAST_FLOWER, SubTileRafflowsia.TAG_LAST_FLOWER_TIMES)); - functionTable.put(ModSubtiles.rafflowsiaFloating, b -> genCopyNbt(b, SubTileRafflowsia.TAG_LAST_FLOWER, SubTileRafflowsia.TAG_LAST_FLOWER_TIMES)); + functionTable.put(ModSubtiles.rafflowsia, b -> genCopyNbt(b, SubTileRafflowsia.TAG_LAST_FLOWERS, SubTileRafflowsia.TAG_LAST_FLOWER_TIMES)); + functionTable.put(ModSubtiles.rafflowsiaFloating, b -> genCopyNbt(b, SubTileRafflowsia.TAG_LAST_FLOWERS, SubTileRafflowsia.TAG_LAST_FLOWER_TIMES)); functionTable.put(ModSubtiles.spectrolus, b -> genCopyNbt(b, SubTileSpectrolus.TAG_NEXT_COLOR)); functionTable.put(ModSubtiles.spectrolusFloating, b -> genCopyNbt(b, SubTileSpectrolus.TAG_NEXT_COLOR)); functionTable.put(ModSubtiles.thermalily, b -> genCopyNbt(b, SubTileHydroangeas.TAG_COOLDOWN)); diff --git a/src/main/resources/assets/botania/lang/en_us.json b/src/main/resources/assets/botania/lang/en_us.json index a7e634b35..1b32dab13 100644 --- a/src/main/resources/assets/botania/lang/en_us.json +++ b/src/main/resources/assets/botania/lang/en_us.json @@ -2188,7 +2188,7 @@ "botania.entry.rafflowsia": "Rafflowsia", "botania.tagline.rafflowsia": "Mana from flowers", - "botania.page.rafflowsia0": "The $(item)Rafflowsia$(0) functions similarly to a $(l:generating_flowers/kekimurus)$(item)Kekimurus$(0)$(/l), but eats $(item)man-made flowers$(0) in the $(l:basics/apothecary)$(item)Petal Apothecary$(0)$(/l) instead. It'll consume any nearby placed flowers and synthesize $(thing)Mana$(0) from them.$(p)However, feeding it the same flower several times in a row yields diminishing returns.", + "botania.page.rafflowsia0": "The $(item)Rafflowsia$(0) functions similarly to a $(l:generating_flowers/kekimurus)$(item)Kekimurus$(0)$(/l), but eats $(item)man-made flowers$(0) in the $(l:basics/apothecary)$(item)Petal Apothecary$(0)$(/l) instead. It'll consume any nearby placed flowers and synthesize $(thing)Mana$(0) from them.$(p)While feeding it the same flower several times in a row yields diminishing returns, feeding it a large variety of them can yield ludicrous quantities of $(thing)Mana$(0).", "botania.page.rafflowsia1": "$(o)A New Dawn, you could say$().", "botania.entry.dandelifeon": "Dandelifeon",