Skip to content

Commit

Permalink
HeightmapImpl takes firstUser in the constructor
Browse files Browse the repository at this point in the history
Also, moves the directional chunk generation to it
  • Loading branch information
Hugobros3 committed Jul 4, 2018
1 parent 7ac5cc0 commit 537c2ab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import io.xol.chunkstories.api.math.Math2;
import io.xol.chunkstories.api.player.Player;
import io.xol.chunkstories.api.server.RemotePlayer;
import io.xol.chunkstories.api.util.IterableIterator;
import io.xol.chunkstories.api.util.concurrency.Fence;
Expand Down Expand Up @@ -45,7 +47,7 @@
*/
public class HeightmapImplementation implements Heightmap
{
final WorldHeightmapsImplementation worldSummariesHolder;
final WorldHeightmapsImplementation heightmapsHolder;
public final WorldImplementation world;
private final int regionX;
private final int regionZ;
Expand Down Expand Up @@ -84,18 +86,40 @@ public class HeightmapImplementation implements Heightmap
/** The offsets in an array containing sequentially each mipmaps of a square texture of base size 128 */
public final static int[] minHeightMipmapOffsets = {0, 16384, 20480, 21504, 21760, 21824, 21840, 21844, 21845};

HeightmapImplementation(WorldHeightmapsImplementation worldSummariesHolder, int rx, int rz, int dirx, int dirz)
HeightmapImplementation(WorldHeightmapsImplementation heightmapsHolder, int rx, int rz, WorldUser firstUser)
{
this.worldSummariesHolder = worldSummariesHolder;
this.world = worldSummariesHolder.getWorld();
this.heightmapsHolder = heightmapsHolder;
this.world = heightmapsHolder.getWorld();
this.regionX = rx;
this.regionZ = rz;

this.registerUser(firstUser);

int dirX = 0;
int dirZ = 0;

if (firstUser instanceof Player) {
Player player = (Player)firstUser;

int playerRegionX = Math2.floor((player.getControlledEntity().getLocation().x()) / 256);
int playerRegionZ = Math2.floor((player.getControlledEntity().getLocation().z()) / 256);

if (regionX < playerRegionX)
dirX = -1;
if (regionX > playerRegionX)
dirX = 1;

if (regionZ < playerRegionZ)
dirZ = -1;
if (regionZ > playerRegionZ)
dirZ = 1;
}

if (world instanceof WorldMaster) {
handler = new File(world.getFolderPath() + "/summaries/" + rx + "." + rz + ".sum");

if(!handler.exists())
world.getGameContext().tasks().scheduleTask(new TaskGenerateWorldSlice(world, this, dirx, dirz));
world.getGameContext().tasks().scheduleTask(new TaskGenerateWorldSlice(world, this, dirX, dirZ));

loadFence = this.world.ioHandler.requestHeightmapLoad(this);
}
Expand Down Expand Up @@ -338,9 +362,9 @@ void unloadSummary()
if(loadFence instanceof SimpleFence)
((SimpleFence) loadFence).signal();

if (!worldSummariesHolder.removeSummary(this))
if (!heightmapsHolder.removeSummary(this))
{
System.out.println(this+" failed to be removed from the holder "+worldSummariesHolder);
System.out.println(this+" failed to be removed from the holder "+heightmapsHolder);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.concurrent.Semaphore;

import io.xol.chunkstories.api.Location;
import io.xol.chunkstories.api.math.Math2;
import io.xol.chunkstories.api.player.Player;
import io.xol.chunkstories.api.util.concurrency.Fence;
import io.xol.chunkstories.api.world.WorldUser;
import io.xol.chunkstories.api.world.cell.CellData;
Expand Down Expand Up @@ -49,28 +47,8 @@ long index(int x, int z)
}

@Override
public HeightmapImplementation aquireHeightmap(WorldUser worldUser, int regionX, int regionZ)
{
HeightmapImplementation summary;
int dirX = 0;
int dirZ = 0;

if (worldUser instanceof Player) {
Player player = (Player)worldUser;

int playerRegionX = Math2.floor((player.getControlledEntity().getLocation().x()) / 256);
int playerRegionZ = Math2.floor((player.getControlledEntity().getLocation().z()) / 256);

if (regionX < playerRegionX)
dirX = -1;
if (regionX > playerRegionX)
dirX = 1;

if (regionZ < playerRegionZ)
dirZ = -1;
if (regionZ > playerRegionZ)
dirZ = 1;
}
public HeightmapImplementation aquireHeightmap(WorldUser worldUser, int regionX, int regionZ) {
HeightmapImplementation heightmap;

regionX %= worldSizeInRegions;
regionZ %= worldSizeInRegions;
Expand All @@ -79,21 +57,19 @@ public HeightmapImplementation aquireHeightmap(WorldUser worldUser, int regionX,
if (regionZ < 0)
regionZ += worldSizeInRegions;

long i = index(regionX * 256, regionZ * 256);
long index = index(regionX * 256, regionZ * 256);

dontDeleteWhileCreating.acquireUninterruptibly();
if (summaries.containsKey(i))
summary = summaries.get(i);
else
{
summary = new HeightmapImplementation(this, regionX, regionZ, dirX, dirZ);
summaries.put(i, summary);
if (summaries.containsKey(index)) {
heightmap = summaries.get(index);
heightmap.registerUser(worldUser);
} else {
heightmap = new HeightmapImplementation(this, regionX, regionZ, worldUser);
summaries.put(index, heightmap);
}
dontDeleteWhileCreating.release();

//NOTE: WARNING: ETC: CONTROVERSIAL CHANGE HITLER.
//Change of spec. Returns the summary no matter what. No way of knowing if the add was redundant, that's on you.
return summary.registerUser(worldUser) ? summary : summary;

return heightmap;
}

@Override
Expand Down

0 comments on commit 537c2ab

Please sign in to comment.