Paper icon indicating copy to clipboard operation
Paper copied to clipboard

Chunk isSectionEmpty API

Open aecsocket opened this issue 2 years ago • 5 comments

Is your feature request related to a problem?

Chunk doesn't have a isSectionEmpty(int) method to check if a 16x16x16 slice of that chunk is just filled with air

Describe the solution you'd like.

ChunkSnapshot class has access to isSectionEmpty(int): boolean, and internally it reads its empty field. This is populated by:

boolean[] sectionEmpty = new boolean[cs.length];
// ...
sectionEmpty[i] = cs[i].hasOnlyAir(); // Paper - fix sectionEmpty array not being filled

could this cs[i].hasOnlyAir() be exposed to the Chunk API as well? I suggest keeping it the same method signature as for ChunkSnapshot

Describe alternatives you've considered.

  • Making a ChunkSnapshot and reading the isSectionEmpty of that, but that's very slow relative to just reading a field from the underlying chunk handle; why make a whole snapshot for it?
  • Accessing internals, but that's obviously not really supported

Other

No response

aecsocket avatar Jun 03 '23 19:06 aecsocket

The API doesn't really have the concept of chunk sections... is it worth adding that just for this? Is there anywhere else in the API that could use chunk sections that would increase the use of such an API?

Could you describe a little more about your usecase for this API? Like what are you doing that couldn't be accomplished with more method calls to check if air blocks exist at all locations in a volume?

Machine-Maker avatar Jun 03 '23 19:06 Machine-Maker

My goal is to generate a physics collision shape for a chunk just-in-time for a rigid body, from an external physics engine, to be able to collide with the terrain. I want this process to be as fast as possible, since there might potentially be thousands of bodies intersecting hundreds or thousands of chunks. So I want to prune out chunk sections that don't have any blocks earlier on.

In my opinion this is more about keeping consistency between Chunk and ChunkSnapshot, rather than introducing the concept of segments into the API.

aecsocket avatar Jun 03 '23 19:06 aecsocket

I'm not opposed to it, mainly on the consistency basis. I think such a method should probably come with a helper method on Chunk & ChunkSnapshot to convert a y coordinate into a chunk section index, or maybe all section-related methods should just take a y coordinate to not really bother with section indices.

Machine-Maker avatar Jun 03 '23 19:06 Machine-Maker

That makes sense, although I couldn't come up with a good name for a method which takes a Y coordinate as opposed to a segment coordinate (isSectionAtYLevelEmpty?? bleh), so perhaps isSectionEmpty(Chunk.segmentIndex(yCoord)), as you describe having a helper method for this?

aecsocket avatar Jun 03 '23 19:06 aecsocket

so perhaps isSectionEmpty(Chunk.segmentIndex(yCoord)), as you describe having a helper method for this?

It can't be a static method because it has to have the world height, specifically the min height, so it'd have to be an instance method. But essentially that, yeah.

Machine-Maker avatar Jun 03 '23 20:06 Machine-Maker