Skip to content

Commit

Permalink
Count fluids by their type to avoid unnecessary reads in Entity#updat…
Browse files Browse the repository at this point in the history
…eFluidHeightAndDoFluidPushing

We can take fluid counts in a chunk section into account to
avoid unnecessary searches as we were only checking for non
empty blocks.
  • Loading branch information
wuangg committed Oct 5, 2024
1 parent c22538c commit 5fea434
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ca.spottedleaf.moonrise.fabric.mixin.collisions;

import ca.spottedleaf.moonrise.patches.block_counting.BlockCountingChunkSection;
import ca.spottedleaf.moonrise.patches.getblock.GetBlockLevel;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -107,6 +109,20 @@ public boolean updateFluidHeightAndDoFluidPushing(final TagKey<Fluid> fluid, fin
continue;
}

final BlockCountingChunkSection blockCountingSection = (BlockCountingChunkSection)section;
final boolean hasFluids;
if (fluid == FluidTags.WATER) {
hasFluids = blockCountingSection.moonrise$hasWaterFluids();
} else if (fluid == FluidTags.LAVA) {
hasFluids = blockCountingSection.moonrise$hasLavaFluids();
} else {
hasFluids = blockCountingSection.moonrise$hasFluids();
}

if (!hasFluids) {
// also empty
continue;
}
final PalettedContainer<BlockState> blocks = section.states;

final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) : 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.spottedleaf.moonrise.neoforge.mixin.collisions;

import ca.spottedleaf.moonrise.neoforge.patches.collisions.FluidPushCalculation;
import ca.spottedleaf.moonrise.patches.block_counting.BlockCountingChunkSection;
import ca.spottedleaf.moonrise.patches.getblock.GetBlockLevel;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceArrayMap;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceMap;
Expand Down Expand Up @@ -101,6 +102,11 @@ public void updateFluidHeightAndDoFluidPushing() {
continue;
}

if (!((BlockCountingChunkSection)section).moonrise$hasFluids()) {
// also empty
continue;
}

final PalettedContainer<BlockState> blocks = section.states;

final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import net.minecraft.util.BitStorage;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.Palette;
Expand Down Expand Up @@ -46,7 +47,6 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {

@Shadow
public abstract boolean maybeHas(Predicate<BlockState> predicate);

@Unique
private static final ShortArrayList FULL_LIST = new ShortArrayList(16*16*16);
static {
Expand All @@ -64,6 +64,15 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
@Unique
private short specialCollidingBlocks;

@Unique
private short fluids;

@Unique
private short lavaFluids;

@Unique
private short waterFluids;

@Unique
private final ShortList tickingBlocks = new ShortList();

Expand All @@ -77,6 +86,21 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
return this.tickingBlocks;
}

@Override
public final boolean moonrise$hasFluids() {
return this.fluids != 0;
}

@Override
public final boolean moonrise$hasLavaFluids() {
return this.lavaFluids != 0;
}

@Override
public final boolean moonrise$hasWaterFluids() {
return this.waterFluids != 0;
}

/**
* @reason Callback used to update block counts on block change.
* @author Spottedleaf
Expand Down Expand Up @@ -122,6 +146,45 @@ private void updateBlockCallback(final int x, final int y, final int z, final Bl
tickingBlocks.add(position);
}
}

final FluidState oldFluid = oldState.getFluidState();
final FluidState newFluid = newState.getFluidState();
final boolean hasOldFluid = !oldFluid.isEmpty();
final boolean hasNewFluid = !newFluid.isEmpty();
if (hasOldFluid != hasNewFluid) {
if (hasOldFluid) {
--this.fluids;
} else {
++this.fluids;
}
}
if (hasOldFluid || hasNewFluid) {
boolean fluidChecked = false;
final boolean isOldWater = oldFluid.is(FluidTags.WATER);
final boolean isNewWater = newFluid.is(FluidTags.WATER);

if (isOldWater != isNewWater) {
if (isOldWater) {
--this.waterFluids;
} else {
++this.waterFluids;
}
fluidChecked = true;
}

if (!fluidChecked) {
final boolean isOldLava = oldFluid.is(FluidTags.LAVA);
final boolean isNewLava = newFluid.is(FluidTags.LAVA);

if (isOldLava != isNewLava) {
if (isOldLava) {
--this.lavaFluids;
} else {
++this.lavaFluids;
}
}
}
}
}

/**
Expand Down Expand Up @@ -150,6 +213,9 @@ public void recalcBlockCounts() {
this.tickingBlockCount = (short)0;
this.tickingFluidCount = (short)0;
this.specialCollidingBlocks = (short)0;
this.fluids = (short)0;
this.lavaFluids = (short)0;
this.waterFluids = (short)0;
this.tickingBlocks.clear();

if (this.maybeHas((final BlockState state) -> !state.isAir())) {
Expand Down Expand Up @@ -204,6 +270,13 @@ public void recalcBlockCounts() {
if (fluid.isRandomlyTicking()) {
this.tickingFluidCount += (short)paletteCount;
}
this.fluids += (short)paletteCount;

if (fluid.is(FluidTags.WATER)) {
this.waterFluids += (short)paletteCount;
} else if (fluid.is(FluidTags.LAVA)) {
this.lavaFluids += (short)paletteCount;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ public interface BlockCountingChunkSection {

public ShortList moonrise$getTickingBlockList();

public boolean moonrise$hasFluids();

public boolean moonrise$hasLavaFluids();

public boolean moonrise$hasWaterFluids();

}

0 comments on commit 5fea434

Please sign in to comment.