Skip to content

Commit

Permalink
Optimise random drop calls, and burn my brain
Browse files Browse the repository at this point in the history
Should resolve point 1 of #30 (review)

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.
  • Loading branch information
C0bra5 committed Apr 2, 2024
1 parent a3eb425 commit b25705e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/main/java/tb/common/block/BlockAshroom.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tb.common.block;

import java.util.ArrayList;
import java.util.Random;

import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -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<Aspect> 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);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/tb/common/block/BlockRainbowCactus.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public ArrayList<ItemStack> 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<ItemStack> 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());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tb/common/block/BlockSweed.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public ArrayList<ItemStack> 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));
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/tb/common/block/BlockTBPlant.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,38 @@ public int getRenderType() {
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) {
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();

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());

Expand Down

0 comments on commit b25705e

Please sign in to comment.