Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix slime chunks algorithm #8

Merged
merged 1 commit into from
Nov 13, 2024

Conversation

asquared31415
Copy link

The old algorithm was incorrectly using integer literals in two places instead of long literals. This caused it to incorrectly handle certain seeds. I also took the liberty of making the code exactly match the reference implementation on the wiki using hex literals and casts, just to be sure it was completely identical.

@wlhlm
Copy link
Member

wlhlm commented Nov 12, 2024

Hmm, it's better to look at the decompiled Minecraft source rather than the wiki. Here is the snippet, defined in net.minecraft.entity.monster.EntitySlime#getCanSpawnHere:

 public boolean getCanSpawnHere() {
    Chunk chunk = this.worldObj.getChunkFromBlockCoords(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posZ));
    // ...
    if (this.rand.nextInt(10) == 0 && chunk.getRandomWithSeed(987234911L).nextInt(10) == 0 && this.posY < 40.0D) {
        return super.getCanSpawnHere();
    }
    // ...
}

Here is net.minecraft.world.chunk.Chunk#getRandomWithSeed:

public Random getRandomWithSeed(long p_76617_1_) {
    return new Random(this.worldObj.getSeed() + (long)(this.xPosition * this.xPosition * 4987142) + (long)(this.xPosition * 5947611) + (long)(this.zPosition * this.zPosition) * 4392871L + (long)(this.zPosition * 389711) ^ p_76617_1_);
}

You can see the code actually casts the terms to long rather than int.

@asquared31415
Copy link
Author

asquared31415 commented Nov 13, 2024

Ah interesting, it turns out that that's actually an implicit cast to long anyway, the (int) were entirely redundant. Pushed an update making the casts explicit to be more clear and silence IDE warnings.

Copy link
Member

@wlhlm wlhlm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

@Dream-Master Dream-Master merged commit ec315c6 into GTNewHorizons:master Nov 13, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants