Created the single level container

This commit is contained in:
Leonardo
2021-09-10 16:42:21 +02:00
parent a9aa630aff
commit feebc3564a
8 changed files with 65 additions and 44 deletions
@@ -3,15 +3,19 @@ package com.seibel.lod.objects;
import com.seibel.lod.util.LevelPosUtil;
import com.seibel.lod.util.LodUtil;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public interface LevelContainer
{
public static final char VERTICAL_DATA_DELIMITER = '\t';
public static final char DATA_DELIMITER = '\n';
public static final char DATA_DELIMITER = ',';
public static final ConcurrentMap<String,long[]> threadAddDataMap = new ConcurrentHashMap();
public static final ConcurrentMap<String,long[]> threadGetDataMap = new ConcurrentHashMap();
/**With this you can add data to the level container
*
* @param data actual data to add in a array of long format.
* @param detailLevel just to check that it's correct
* @param posX x position in the detail level
* @param posZ z position in the detail level
* @return true if correctly added, false otherwise
@@ -20,7 +24,6 @@ public interface LevelContainer
/**With this you can get data from the level container
*
* @param detailLevel just to check that it's correct
* @param posX x position in the detail level
* @param posZ z position in the detail level
* @return the data in long array format
@@ -28,7 +31,6 @@ public interface LevelContainer
public long[] getData(int posX, int posZ);
/**
* @param detailLevel just to check that it's correct
* @param posX x position in the detail level
* @param posZ z position in the detail level
* @return true only if the data exist
@@ -49,7 +51,6 @@ public interface LevelContainer
/**
*
* @param lowerLevelContainer lower level where we extract the data
* @param detailLevel detail level to update
* @param posX x position in the detail level to update
* @param posZ z position in the detail level to update
*/
@@ -446,7 +446,14 @@ public class LodDimension
LodRegion region = getRegion(regionPosX, regionPosZ);
if (region == null)
return false;
boolean nodeAdded = region.addData(detailLevel, posX, posZ, lodDataPoint, serverQuality);
;
if(!LevelContainer.threadAddDataMap.containsKey(Thread.currentThread().getName()) || (LevelContainer.threadAddDataMap.get(Thread.currentThread().getName()) == null))
{
LevelContainer.threadAddDataMap.put(Thread.currentThread().getName(), new long[10]);
}
long[] dataArray = LevelContainer.threadAddDataMap.get(Thread.currentThread().getName());
dataArray[0] = lodDataPoint;
boolean nodeAdded = region.addData(detailLevel, posX, posZ, dataArray, serverQuality);
// only save valid LODs to disk
if (!dontSave && fileHandler != null)
{
@@ -533,7 +540,7 @@ public class LodDimension
return 0;
}
return region.getData(detailLevel, posX, posZ);
return region.getData(detailLevel, posX, posZ)[0];
}
@@ -30,6 +30,8 @@ public class LodRegion
private LevelContainer[] dataContainer;
private DistanceGenerationMode generationMode;
public final int regionPosX;
public final int regionPosZ;
@@ -41,26 +43,32 @@ public class LodRegion
dataContainer = new LevelContainer[POSSIBLE_LOD];
}
public LodRegion(byte minDetailLevel, RegionPos regionPos, boolean twoDimension)
public LodRegion(byte minDetailLevel, RegionPos regionPos, DistanceGenerationMode generationMode)
{
this.minDetailLevel = minDetailLevel;
this.regionPosX = regionPos.x;
this.regionPosZ = regionPos.z;
this.generationMode = generationMode;
dataContainer = new LevelContainer[POSSIBLE_LOD];
//Initialize all the different matrices
for (byte lod = minDetailLevel; lod <= LodUtil.REGION_DETAIL_LEVEL; lod++)
{
if(twoDimension){
dataContainer[lod] = new SingleLevelContainer(lod);
/*if(twoDimension){
dataContainer[lod] = new SingleLevelContainer(lod);
}else{
dataContainer[lod] = new VerticalLevelContainer(lod);
}
}*/
}
}
public DistanceGenerationMode getGenerationMode()
{
return generationMode;
}
/**
* This method can be used to insert data into the LodRegion
*
@@ -287,6 +295,8 @@ public class LodRegion
if(detailLevel < minDetailLevel) return false;
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
posZ = LevelPosUtil.getRegionModule(detailLevel, posZ);
if(dataContainer == null || dataContainer[detailLevel] == null)
return false;
return dataContainer[detailLevel].doesItExist(posX, posZ);
}
@@ -361,12 +371,10 @@ public class LodRegion
{
if (detailLevel < minDetailLevel)
{
for (byte tempLod = (byte) (minDetailLevel - 1); tempLod >= minDetailLevel ; tempLod--)
for (byte tempLod = (byte) (minDetailLevel - 1); tempLod >= detailLevel ; tempLod--)
{
int size = (short) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - tempLod);
if(dataContainer[tempLod + 1] == null)
{
/* TODO this can become either Single or Vertical*/
dataContainer[tempLod + 1] = new SingleLevelContainer((byte) (tempLod + 1));
}
dataContainer[tempLod] = dataContainer[tempLod+1].expand();
@@ -9,23 +9,19 @@ import com.seibel.lod.util.LodUtil;
public class SingleLevelContainer implements LevelContainer
{
public final byte detailLevel;
public final int size;
public final long[][] data;
public SingleLevelContainer(byte detailLevel)
{
this.detailLevel = detailLevel;
int size = 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel);
size = 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel);
data = new long[size][size];
}
public SingleLevelContainer(byte detailLevel, long[][] data)
{
this.detailLevel = detailLevel;
this.data = data;
}
public boolean addData(long[] newData, int posX, int posZ){
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
posZ = LevelPosUtil.getRegionModule(detailLevel, posZ);
data[posX][posZ] = newData[0];
@@ -39,10 +35,18 @@ public class SingleLevelContainer implements LevelContainer
return true;
}
public long[] getData(int posX, int posZ){
if(!LevelContainer.threadGetDataMap.containsKey(Thread.currentThread().getName()) || (LevelContainer.threadGetDataMap.get(Thread.currentThread().getName()) == null))
{
LevelContainer.threadGetDataMap.put(Thread.currentThread().getName(), new long[1]);
}
long[] dataArray = LevelContainer.threadGetDataMap.get(Thread.currentThread().getName());
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
posZ = LevelPosUtil.getRegionModule(detailLevel, posZ);
//Improve this using a thread map to long[]
return new long[]{data[posX][posZ]};
dataArray[0] = data[posX][posZ];
return dataArray;
}
private long getSingleData(int posX, int posZ){
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
@@ -67,8 +71,8 @@ public class SingleLevelContainer implements LevelContainer
index = inputString.indexOf(DATA_DELIMITER, 0);
this.detailLevel = (byte) Integer.parseInt(inputString.substring(0, index));
int size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel);
detailLevel = (byte) Integer.parseInt(inputString.substring(0, index));
size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel);
this.data = new long[size][size];
for (int x = 0; x < size; x++)
@@ -162,12 +166,6 @@ public class SingleLevelContainer implements LevelContainer
}
public String toDataString()
{
return toString();
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
int size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel);
@@ -184,4 +182,12 @@ public class SingleLevelContainer implements LevelContainer
}
return stringBuilder.toString();
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(detailLevel);
return stringBuilder.toString();
}
}