From b25705ec609ca491035d3209680593e599f973ff Mon Sep 17 00:00:00 2001 From: Guillaume Mercier <10gui-gui10@live.ca> Date: Mon, 1 Apr 2024 20:27:08 -0400 Subject: [PATCH] Optimise random drop calls, and burn my brain Should resolve point 1 of https://github.com/GTNewHorizons/ThaumicBases/pull/30#pullrequestreview-1971014677 You can approximate the rates it used to generate by using the LODA calculator here: https://loda-lang.org/edit/?source=mov%20%241%2C%20%240%0Aseq%20%240%2C169%0Amov%20%242%2C%20%240%0Amov%20%240%2C%20%241%0Aseq%20%240%2C%20435%0Amul%20%240%2C%20100000%0Adiv%20%240%2C%20%242%0Amul%20%240%2C%202 - x is the value provided to the nextInt function. - y is the average number of bonus rounds (the values used in this commit from from there) The calculator makes integers, so divide numbers by 100000 to get the actual values. --- .../java/tb/common/block/BlockAshroom.java | 15 ++++---- .../tb/common/block/BlockRainbowCactus.java | 4 ++- src/main/java/tb/common/block/BlockSweed.java | 4 +-- .../java/tb/common/block/BlockTBPlant.java | 35 ++++++++++++++++--- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/main/java/tb/common/block/BlockAshroom.java b/src/main/java/tb/common/block/BlockAshroom.java index 1d93ff4..816e050 100644 --- a/src/main/java/tb/common/block/BlockAshroom.java +++ b/src/main/java/tb/common/block/BlockAshroom.java @@ -1,5 +1,6 @@ package tb.common.block; +import java.util.ArrayList; import java.util.Random; import net.minecraft.entity.player.EntityPlayer; @@ -42,14 +43,14 @@ protected boolean func_150109_e(World p_150109_1_, int p_150109_2_, int p_150109 @Override public void onBlockHarvested(World world, int x, int y, int z, int metadata, EntityPlayer player) { if (metadata >= this.growthStages - 1) { - for (int i = 0; i < 8 + world.rand.nextInt(32); ++i) // Nerf for the shrooms + // should be close enough to the rate of the old logic. + int dropCount = 8 + (int) Math.round(world.rand.nextDouble() * 11.76742); + ArrayList primals = Aspect.getPrimalAspects(); + for (int i = 0; i < dropCount; ++i) // Nerf for the shrooms { - Aspect primal = Aspect.getPrimalAspects() - .get( - world.rand.nextInt( - Aspect.getPrimalAspects() - .size())); - EntityAspectOrb orb = new EntityAspectOrb(world, x, y, z, primal, 1); + // We should probably cluster orbs of the same aspect instead of spawning them individually. + Aspect aspect = primals.get(world.rand.nextInt(primals.size())); + EntityAspectOrb orb = new EntityAspectOrb(world, x, y, z, aspect, 1); if (!world.isRemote) world.spawnEntityInWorld(orb); } } diff --git a/src/main/java/tb/common/block/BlockRainbowCactus.java b/src/main/java/tb/common/block/BlockRainbowCactus.java index f37d7ff..aa7bc98 100644 --- a/src/main/java/tb/common/block/BlockRainbowCactus.java +++ b/src/main/java/tb/common/block/BlockRainbowCactus.java @@ -55,7 +55,9 @@ public ArrayList getDrops(World world, int x, int y, int z, int metad * @param ret The output list to append to, 1 new stack per dye drop, merge them on your own time. */ public void addDyeDropsToOutput(Random rand, ArrayList ret) { - for (int i = 0; i < 3 + rand.nextInt(8); ++i) { + // Should be roughly equivalent to the old bonus rates + int dropCount = 3 + (int) Math.round(rand.nextDouble() * 4.91662); + for (int i = 0; i < dropCount; ++i) { ret.add(allowedDyes[rand.nextInt(allowedDyes.length)].copy()); } } diff --git a/src/main/java/tb/common/block/BlockSweed.java b/src/main/java/tb/common/block/BlockSweed.java index fe5011e..b41d0f6 100644 --- a/src/main/java/tb/common/block/BlockSweed.java +++ b/src/main/java/tb/common/block/BlockSweed.java @@ -51,8 +51,8 @@ public ArrayList getDrops(World world, int x, int y, int z, int metad if (metadata >= growthStages - 1) { if (world.rand.nextInt(growthStages) <= metadata) if (dropSeed != null) ret.add(dropSeed.copy()); - int sugarDropCount = 0; - for (int i = 0; i < 3 + fortune; ++i) if (world.rand.nextBoolean()) ++sugarDropCount; + // should preserve drop rates + int sugarDropCount = world.rand.nextInt(4 + fortune); if (sugarDropCount > 0) ret.add(new ItemStack(Items.sugar, sugarDropCount)); if (world.rand.nextBoolean()) ret.add(new ItemStack(Items.reeds)); diff --git a/src/main/java/tb/common/block/BlockTBPlant.java b/src/main/java/tb/common/block/BlockTBPlant.java index 4911d9d..7e9ae0b 100644 --- a/src/main/java/tb/common/block/BlockTBPlant.java +++ b/src/main/java/tb/common/block/BlockTBPlant.java @@ -159,13 +159,38 @@ public int getRenderType() { public ArrayList getDrops(World world, int x, int y, int z, int metadata, int fortune) { ArrayList ret = new ArrayList(); - if (metadata >= growthStages - 1) { - for (int i = 0; i < 1 + world.rand.nextInt(fortune + 1); ++i) // Change for the resource drop - { - if (world.rand.nextInt(growthStages) <= metadata) { - if (dropItem != null) ret.add(dropItem.copy()); + if (this.dropItem != null && metadata >= growthStages - 1) { + // You can approximate the fortune bonus by diving https://oeis.org/A000169 with https://oeis.org/A000435 + // I can't figure out a good efficient way to compute those without melting my brain so if you feel + // like you can approximate it well enough, feel free to do so. For now, I'm just capping it at 3 drops + // with a linear function since fortune levels above 3 aren't very common, and I don't want some poor lad + // crashing their pc with max int fortune int. + // + // Average bonuses for each fortune level: + // F1 = 0.5 -> 0.5 (stayed the same) + // F2 = 0.88888 -> 0.9 (slight buff) + // F3 = 1.21875 -> 1.3 (slight buff) + // The EIG doesn't use fortune levels, so it shouldn't be affected anyway. + int roundCount = 1; + if (fortune > 0) { + roundCount += (int) Math.min(3, Math.round(world.rand.nextDouble() * (1 + 0.8d * (fortune - 1)))); + } + int dropCount = 0; + if (growthStages <= metadata) { + // we can just skip the random calls if we are at max growth. + dropCount = roundCount; + } else { + for (int i = 0; i < roundCount; ++i) { + if (world.rand.nextInt(growthStages) <= metadata) { + dropCount++; + } } } + if (dropCount > 0) { + ItemStack drop = dropItem.copy(); + drop.stackSize = dropCount; + ret.add(drop); + } } if (dropSeed != null) ret.add(dropSeed.copy());