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

Keep a radius of cached regions loaded #2967

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

UPSman123
Copy link

This implements an enhancement from #324. I created the ChunkLoader
class. This handles loading chunks around the player and render view
entity in a given radius. The chunks are loaded from disk using the
CachedWorld.tryLoadFromDisk function. If a chunk has not yet been
added to the cache (in memory or on disk) then this will not cache it.

Curently the class is initialized from the Baritone constructor. I don't know if this is okay or if it should be done elsewhere.
I couldn't find a good place for it because it needs to be registered with the GameEventHandler.

I also noticed that the ICachedWorld interface does not include the tryLoadFromDisk function even though it is public.
I don't know if this is intentional or not. In this commit I decided to just cast the ICachedWorld to a CachedWorld.

This implements an enhancement from cabaletta#324. I created the ChunkLoader
class. This handles loading chunks around the player and render view
entity in a given radius. The chunks are loaded from disk using the
CachedWorld.tryLoadFromDisk function. If a chunk has not yet been
added to the cache (in memory or on disk) then this will not cache it.

/**
* Loads an area around the player and renderViewEntity to the cache.
* The
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't think you meant to leave that here.

// Don't do anything if the world doesn't exist. Like in the main menu.
if (worldData == null) return;
loadAroundChunk(player.chunkCoordX, player.chunkCoordZ);
// Only load around the renderViewEntity if it's in a different chunk.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe also don't do anything if the position didn't change since last tick?
Don't know whether the overhead of reloading every tick is more than the overhead of checking this.

@@ -120,6 +121,9 @@
this.worldProvider = new WorldProvider();
this.selectionManager = new SelectionManager(this);
this.commandManager = new CommandManager(this);

ChunkLoader chunkLoader = new ChunkLoader(this, 2); // load 3 by 3 area of chunks around the player.
Copy link
Collaborator

Choose a reason for hiding this comment

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

#324 suggested loading 3 by 3 regions around the player, not chunks.
Loading 3 by 3 chunks changes almost nothing, it now just loads the next region 16 blocks before you reach the region border instead of when you reach it.

if (worldData == null) return;
loadAroundChunk(player.chunkCoordX, player.chunkCoordZ);
// Only load around the renderViewEntity if it's in a different chunk.
if (player.chunkCoordX != renderViewEntity.chunkCoordX || player.chunkCoordZ != renderViewEntity.chunkCoordZ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should compare region coords here because you can only load entire regions at once.

Previously the ChunkLoader would load chunks around the player but this
was a misinterpretation. Now the renamed RedionLoader class loads
regions around the player.
@UPSman123
Copy link
Author

Thanks for the code review.
I was a bit confused about the difference between regions and chunks but I properly looked into it and I get it now. I changed it to work with regions instead.
I also added a check to see if the player moved to a different region. This won't prevent regions around the renderViewEntity from being loaded if it is in a different region but in that case it's only a few hashmap lookups and shouldn't influence performance significantly. I could add code to check for the renderViewEntity as well but I feel like it will just make things more messy without any real gain.

Copy link
Collaborator

@ZacSharp ZacSharp left a comment

Choose a reason for hiding this comment

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

Just two oversights and some formatting

Comment on lines 125 to 126
RegionLoader chunkLoader = new RegionLoader(this, 2); // load 3 by 3 area of chunks around the player.
this.gameEventHandler.registerEventListener(chunkLoader);
Copy link
Collaborator

Choose a reason for hiding this comment

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

you forgot to change the variable and the comment

Comment on lines 54 to 57
public void onTick(TickEvent event) {this.loadRegions();}

@Override
public void onRenderPass(RenderEvent event) {this.loadRegions();}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the function body should be on a new line, but I'm not sure about that

EntityPlayerSP player = mc.player;
Entity renderViewEntity = mc.getRenderViewEntity();
// Don't do anything before the player is loaded.
if (player == null || renderViewEntity == null) return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

always use

if (cond) {
    something();
}

rather than

if (cond) something();

}

private void loadAroundRegion(int regionX, int regionZ) {
System.out.println("loading around region x: " + regionX + " z: " + regionZ);
Copy link
Collaborator

Choose a reason for hiding this comment

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

And remove this massive logspam. If you are flying around with freecam this is 9 lines per frame and tick.

@UPSman123
Copy link
Author

I fixed it. Thanks again for looking it over.

Copy link
Collaborator

@ZacSharp ZacSharp left a comment

Choose a reason for hiding this comment

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

Looks good to me.

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