Skip to content

Commit

Permalink
Optimize spatial lookup collection entries, move GlowSquidLuminance t…
Browse files Browse the repository at this point in the history
…o implementation.
  • Loading branch information
LambdAurora committed Nov 18, 2024
1 parent e58249e commit c58b6f3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ record Type(Identifier id, MapCodec<? extends EntityLuminance> codec) {
);
public static final Type ENDERMAN = registerSimple("enderman", EndermanLuminance.INSTANCE);
public static final Type FALLING_BLOCK = registerSimple("falling_block", FallingBlockLuminance.INSTANCE);
public static final Type GLOW_SQUID = registerSimple("glow_squid", GlowSquidLuminance.INSTANCE);
public static final Type ITEM = register("item", ItemDerivedEntityLuminance.CODEC);
public static final Type ITEM_ENTITY = registerSimple("item_entity", ItemEntityLuminance.INSTANCE);
public static final Type ITEM_FRAME = registerSimple("item_frame", ItemFrameLuminance.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import net.minecraft.core.BlockPos;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -25,23 +26,21 @@ public Stream<SpatialLookupCollectionEntry> split() {
record Data(LongList position, ByteList luminance) {}
var cellKeyToData = new Int2ObjectOpenHashMap<Data>();

for (var record : this.entries) {
int cellKey = DynamicLightingEngine.hashAt(record.x(), record.y(), record.z());
for (var entry : this.entries) {
int cellKey = DynamicLightingEngine.hashAt(entry.x(), entry.y(), entry.z());

var data = cellKeyToData.computeIfAbsent(cellKey, k -> new Data(new LongArrayList(), new ByteArrayList()));

data.position.add(record.x());
data.position.add(record.y());
data.position.add(record.z());
data.luminance.add((byte) record.luminance());
data.position.add(BlockPos.asLong(entry.x(), entry.y(), entry.z()));
data.luminance.add((byte) entry.luminance());
}

return cellKeyToData.int2ObjectEntrySet()
.stream()
.map(entry -> new SpatialLookupCollectionEntry(
entry.getIntKey(),
entry.getValue().position().toLongArray(),
entry.getValue().luminance().toByteArray()
entry.getValue().position.toLongArray(),
entry.getValue().luminance.toByteArray()
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public record SpatialLookupCollectionEntry(int cellKey, long[] positions, byte[]
public double getDynamicLightLevel(@NotNull BlockPos pos) {
double maxLightLevel = 0.;

for (int i = 0; i < this.luminance.length; i++) {
int posIndex = i * 3;
double x = this.positions[posIndex] + 0.5;
double y = this.positions[posIndex + 1] + 0.5;
double z = this.positions[posIndex + 2] + 0.5;
for (int i = 0; i < this.positions.length; i++) {
long packed = this.positions[i];
double x = BlockPos.unpackLongX(packed) + 0.5;
double y = BlockPos.unpackLongY(packed) + 0.5;
double z = BlockPos.unpackLongZ(packed) + 0.5;
byte luminance = this.luminance[i];

double lightLevel = SpatialLookupEntry.lightAtPos(x, y, z, pos, luminance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public final class EntityLightSources extends LightSourceLoader<EntityLightSourc
public static final EntityLuminance.Type CREEPER = EntityLuminance.Type.registerSimple(
LambDynLightsConstants.id("creeper"), CreeperLuminance.INSTANCE
);
public static final EntityLuminance.Type GLOW_SQUID =EntityLuminance.Type.registerSimple(
LambDynLightsConstants.id("glow_squid"), GlowSquidLuminance.INSTANCE
);
public static final EntityLuminance.Type MAGMA_CUBE = EntityLuminance.Type.registerSimple(
LambDynLightsConstants.id("magma_cube"), MagmaCubeLuminance.INSTANCE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* see the LICENSE file.
*/

package dev.lambdaurora.lambdynlights.api.entity.luminance;
package dev.lambdaurora.lambdynlights.resource.entity.luminance;

import dev.lambdaurora.lambdynlights.api.entity.luminance.EntityLuminance;
import dev.lambdaurora.lambdynlights.api.item.ItemLightSourceManager;
import dev.lambdaurora.lambdynlights.resource.entity.EntityLightSources;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.GlowSquid;
Expand All @@ -30,7 +32,7 @@ private GlowSquidLuminance() {}

@Override
public @NotNull Type type() {
return Type.GLOW_SQUID;
return EntityLightSources.GLOW_SQUID;
}

@Override
Expand Down

0 comments on commit c58b6f3

Please sign in to comment.