Implemented Chunk-Links Mapping

Implemented support for tracking the list of links in each chunk in a
dimension. This will be used for scheduling rift regeneration when
chunks load.
This commit is contained in:
SenseiKiwi 2014-07-10 18:21:10 -04:00
parent 782c6d5e50
commit 85ff28298e
2 changed files with 68 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package StevenDimDoors.mod_pocketDim.core;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import net.minecraft.world.ChunkCoordIntPair;
import StevenDimDoors.mod_pocketDim.util.Point4D; import StevenDimDoors.mod_pocketDim.util.Point4D;
import StevenDimDoors.mod_pocketDim.watcher.ClientLinkData; import StevenDimDoors.mod_pocketDim.watcher.ClientLinkData;
@ -15,7 +16,6 @@ public abstract class DimLink
protected DimLink(ClientLinkData link, DimLink parent) protected DimLink(ClientLinkData link, DimLink parent)
{ {
if (parent.link.point.getDimension() != link.point.getDimension()) if (parent.link.point.getDimension() != link.point.getDimension())
{ {
// Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails // Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails
@ -95,6 +95,11 @@ public abstract class DimLink
{ {
return tail.getLinkType(); return tail.getLinkType();
} }
public ChunkCoordIntPair getChunkCoordinates()
{
return new ChunkCoordIntPair(link.point.getX() >> 4, link.point.getZ() >> 4);
}
@Override @Override
public String toString() public String toString()

View file

@ -1,6 +1,7 @@
package StevenDimDoors.mod_pocketDim.core; package StevenDimDoors.mod_pocketDim.core;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@ -117,6 +118,8 @@ public abstract class NewDimData
} }
} }
private static int EXPECTED_LINKS_PER_CHUNK = 2;
protected static Random random = new Random(); protected static Random random = new Random();
protected int id; protected int id;
@ -163,6 +166,7 @@ public abstract class NewDimData
this.origin = null; this.origin = null;
this.dungeon = null; this.dungeon = null;
this.linkWatcher = linkWatcher; this.linkWatcher = linkWatcher;
this.chunkMapping = new HashMap<ChunkCoordIntPair, List<InnerDimLink>>();
this.modified = true; this.modified = true;
//Register with parent //Register with parent
@ -203,6 +207,7 @@ public abstract class NewDimData
this.linkWatcher = null; this.linkWatcher = null;
this.depth = 0; this.depth = 0;
this.root = root; this.root = root;
this.chunkMapping = null;
} }
@ -297,13 +302,26 @@ public abstract class NewDimData
public DimLink createLink(Point4D source, int linkType, int orientation) public DimLink createLink(Point4D source, int linkType, int orientation)
{ {
//Return an existing link if there is one to avoid creating multiple links starting at the same point. // Return an existing link if there is one to avoid creating multiple links starting at the same point.
InnerDimLink link = linkMapping.get(source); InnerDimLink link = linkMapping.get(source);
if (link == null) if (link == null)
{ {
link = new InnerDimLink(source, linkType, orientation); link = new InnerDimLink(source, linkType, orientation);
linkMapping.put(source, link); linkMapping.put(source, link);
linkList.add(link); linkList.add(link);
// If this code is running on the server side, add this link to chunkMapping.
if (linkType != LinkTypes.CLIENT_SIDE)
{
ChunkCoordIntPair chunk = link.getChunkCoordinates();
List<InnerDimLink> chunkLinks = chunkMapping.get(chunk);
if (chunkLinks == null)
{
chunkLinks = new ArrayList<InnerDimLink>(EXPECTED_LINKS_PER_CHUNK);
chunkMapping.put(chunk, chunkLinks);
}
chunkLinks.add(link);
}
} }
else else
{ {
@ -311,7 +329,7 @@ public abstract class NewDimData
} }
modified = true; modified = true;
//Link created! // Link created!
if (linkType != LinkTypes.CLIENT_SIDE) if (linkType != LinkTypes.CLIENT_SIDE)
{ {
linkWatcher.onCreated(link.link); linkWatcher.onCreated(link.link);
@ -331,8 +349,8 @@ public abstract class NewDimData
private DimLink createChildLink(Point4D source, InnerDimLink parent) private DimLink createChildLink(Point4D source, InnerDimLink parent)
{ {
//To avoid having multiple links at a single point, if we find an existing link then we overwrite // To avoid having multiple links at a single point, if we find an existing link then we overwrite
//its destination data instead of creating a new instance. // its destination data instead of creating a new instance.
InnerDimLink link = linkMapping.get(source); InnerDimLink link = linkMapping.get(source);
if (link == null) if (link == null)
@ -341,14 +359,28 @@ public abstract class NewDimData
linkMapping.put(source, link); linkMapping.put(source, link);
linkList.add(link); linkList.add(link);
//Link created! // If this code is running on the server side, add this link to chunkMapping.
// Granted, the client side code should never create child links anyway...
if (link.linkType() != LinkTypes.CLIENT_SIDE)
{
ChunkCoordIntPair chunk = link.getChunkCoordinates();
List<InnerDimLink> chunkLinks = chunkMapping.get(chunk);
if (chunkLinks == null)
{
chunkLinks = new ArrayList<InnerDimLink>(EXPECTED_LINKS_PER_CHUNK);
chunkMapping.put(chunk, chunkLinks);
}
chunkLinks.add(link);
}
// Link created!
linkWatcher.onCreated(link.link); linkWatcher.onCreated(link.link);
} }
else else
{ {
if (link.overwrite(parent, parent.link.orientation)) if (link.overwrite(parent, parent.link.orientation))
{ {
//Link created! // Link created!
linkWatcher.onCreated(link.link); linkWatcher.onCreated(link.link);
} }
} }
@ -366,7 +398,19 @@ public abstract class NewDimData
if (target != null) if (target != null)
{ {
linkList.remove(target); linkList.remove(target);
//Raise deletion event
// If this code is running on the server side, remove this link to chunkMapping.
if (link.linkType() != LinkTypes.CLIENT_SIDE)
{
ChunkCoordIntPair chunk = target.getChunkCoordinates();
List<InnerDimLink> chunkLinks = chunkMapping.get(chunk);
if (chunkLinks != null)
{
chunkLinks.remove(target);
}
}
// Raise deletion event
linkWatcher.onDeleted(target.link); linkWatcher.onDeleted(target.link);
target.clear(); target.clear();
modified = true; modified = true;
@ -388,7 +432,7 @@ public abstract class NewDimData
public DimLink getLink(Point3D location) public DimLink getLink(Point3D location)
{ {
return linkMapping.get(new Point4D(location.getX(),location.getY(),location.getZ(),this.id)); return linkMapping.get(new Point4D(location.getX(), location.getY(), location.getZ(), this.id));
} }
public DimLink getLink(Point4D location) public DimLink getLink(Point4D location)
@ -618,6 +662,16 @@ public abstract class NewDimData
return linkList.get(0); return linkList.get(0);
} }
public Iterable<? extends DimLink> getChunkLinks(int chunkX, int chunkZ)
{
List<InnerDimLink> chunkLinks = chunkMapping.get(new ChunkCoordIntPair(chunkX, chunkZ));
if (chunkLinks != null)
{
return chunkLinks;
}
return new ArrayList<InnerDimLink>(0);
}
public boolean isModified() public boolean isModified()
{ {
return modified; return modified;