-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track which spells are active on which blocks to make tile-by-tile in…
…teractions easier. Requireed by #412
- Loading branch information
Showing
6 changed files
with
264 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/minelittlepony/unicopia/server/world/chunk/Chunk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.minelittlepony.unicopia.server.world.chunk; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
|
||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap; | ||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Box; | ||
import net.minecraft.util.math.ChunkSectionPos; | ||
|
||
public class Chunk<T extends PositionalDataMap.Hotspot> { | ||
private final Long2ObjectMap<Section<T>> sections = new Long2ObjectOpenHashMap<>(); | ||
private final Map<T, Set<Section<T>>> entryToSections = new WeakHashMap<>(); | ||
|
||
Chunk(long pos) { } | ||
|
||
public synchronized Set<T> getState(BlockPos pos) { | ||
Section<T> section = sections.get(ChunkSectionPos.getSectionCoord(pos.getY())); | ||
return section == null ? Set.of() : section.getState(pos); | ||
} | ||
|
||
public synchronized boolean remove(T entry) { | ||
Set<Section<T>> sections = entryToSections.remove(entry); | ||
if (sections != null) { | ||
sections.forEach(section -> { | ||
if (section.remove(entry) && section.isEmpty()) { | ||
this.sections.remove(section.pos); | ||
} | ||
}); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public synchronized boolean update(T entry, Box entryBox) { | ||
Set<Section<T>> oldSections = entryToSections.get(entry); | ||
Set<Section<T>> newSections = getIntersectingSections(entryBox); | ||
if (oldSections != null) { | ||
oldSections.forEach(section -> { | ||
if (!newSections.contains(section) && section.remove(entry) && section.isEmpty()) { | ||
this.sections.remove(section.pos); | ||
} | ||
}); | ||
} | ||
newSections.forEach(chunk -> chunk.update(entry, entryBox)); | ||
entryToSections.put(entry, newSections); | ||
return true; | ||
} | ||
|
||
private Set<Section<T>> getIntersectingSections(Box entryBox) { | ||
Set<Section<T>> sections = new HashSet<>(); | ||
|
||
int minY = ChunkSectionPos.getSectionCoord(entryBox.minY); | ||
int maxY = ChunkSectionPos.getSectionCoord(entryBox.maxY); | ||
for (int y = minY; y <= maxY; y++) { | ||
sections.add(this.sections.computeIfAbsent(y, Section::new)); | ||
} | ||
|
||
return sections; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/minelittlepony/unicopia/server/world/chunk/PositionalDataMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.minelittlepony.unicopia.server.world.chunk; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
|
||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap; | ||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Box; | ||
import net.minecraft.util.math.ChunkPos; | ||
import net.minecraft.util.math.ChunkSectionPos; | ||
import net.minecraft.util.math.MathHelper; | ||
|
||
public class PositionalDataMap<T extends PositionalDataMap.Hotspot> { | ||
private final Long2ObjectMap<Chunk<T>> chunks = new Long2ObjectOpenHashMap<>(); | ||
private final Map<T, Set<Chunk<T>>> entryToChunks = new WeakHashMap<>(); | ||
|
||
public Set<T> getState(BlockPos pos) { | ||
Chunk<T> chunk = chunks.get(ChunkPos.toLong(pos)); | ||
return chunk == null ? Set.of() : chunk.getState(pos); | ||
} | ||
|
||
public void remove(T entry) { | ||
Set<Chunk<T>> chunks = entryToChunks.remove(entry); | ||
if (chunks != null) { | ||
chunks.forEach(chunk -> chunk.remove(entry)); | ||
} | ||
} | ||
|
||
public void update(T entry) { | ||
Box entryBox = new Box(entry.getCenter()).expand(MathHelper.ceil(entry.getRadius())); | ||
Set<Chunk<T>> oldChunks = entryToChunks.get(entry); | ||
Set<Chunk<T>> newChunks = getIntersectingChunks(entryBox); | ||
if (oldChunks != null) { | ||
oldChunks.forEach(chunk -> chunk.remove(entry)); | ||
} | ||
newChunks.forEach(chunk -> chunk.update(entry, entryBox)); | ||
entryToChunks.put(entry, newChunks); | ||
} | ||
|
||
private Set<Chunk<T>> getIntersectingChunks(Box entryBox) { | ||
int minX = ChunkSectionPos.getSectionCoord(entryBox.minX); | ||
int maxX = ChunkSectionPos.getSectionCoord(entryBox.maxX); | ||
int minZ = ChunkSectionPos.getSectionCoord(entryBox.minZ); | ||
int maxZ = ChunkSectionPos.getSectionCoord(entryBox.maxZ); | ||
|
||
Set<Chunk<T>> chunks = new HashSet<>(); | ||
for (int x = minX; x <= maxX; x++) { | ||
for (int z = minZ; z <= maxZ; z++) { | ||
chunks.add(this.chunks.computeIfAbsent(ChunkPos.toLong(x, z), Chunk::new)); | ||
} | ||
} | ||
return chunks; | ||
} | ||
|
||
public interface Hotspot { | ||
float getRadius(); | ||
|
||
BlockPos getCenter(); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/minelittlepony/unicopia/server/world/chunk/Section.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.minelittlepony.unicopia.server.world.chunk; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
|
||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Box; | ||
import net.minecraft.util.math.MathHelper; | ||
|
||
public class Section<T extends PositionalDataMap.Hotspot> { | ||
private final Set<T> entries = weakSet(); | ||
private Set<T>[] states; | ||
|
||
final long pos; | ||
|
||
public Section(long pos) { | ||
this.pos = pos; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return entries.isEmpty(); | ||
} | ||
|
||
public boolean remove(T entry) { | ||
if (entries.remove(entry)) { | ||
states = null; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean update(T entry, Box box) { | ||
entries.add(entry); | ||
states = null; | ||
return true; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Set<T> getState(BlockPos pos) { | ||
int localPos = toLocalIndex(pos); | ||
if (states == null) { | ||
states = new Set[16 * 16 * 16]; | ||
} | ||
Set<T> state = states[localPos]; | ||
return state == null ? (states[localPos] = calculateState(pos)) : state; | ||
} | ||
|
||
private Set<T> calculateState(BlockPos pos) { | ||
Set<T> state = weakSet(); | ||
|
||
for (T entry : entries) { | ||
BlockPos center = entry.getCenter(); | ||
int radius = MathHelper.ceil(entry.getRadius()); | ||
|
||
if (pos.equals(center) | ||
|| (isInRange(pos.getX(), center.getX(), radius) | ||
&& isInRange(pos.getZ(), center.getZ(), radius) | ||
&& isInRange(pos.getY(), center.getY(), radius) | ||
&& center.isWithinDistance(pos, radius))) { | ||
state.add(entry); | ||
} | ||
} | ||
|
||
return state; | ||
} | ||
|
||
static boolean isInRange(int value, int center, int radius) { | ||
return value >= center - radius && value <= center + radius; | ||
} | ||
|
||
static int toLocalIndex(BlockPos pos) { | ||
int x = pos.getX() % 16; | ||
int y = pos.getY() % 16; | ||
int z = pos.getZ() % 16; | ||
return x + (y * 16) + (z * 16 * 16); | ||
} | ||
|
||
static<T> Set<T> weakSet() { | ||
return Collections.newSetFromMap(new WeakHashMap<>()); | ||
} | ||
} |