use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.
the class PacketInitializeRemoteWorld method process.
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
String initializationString = in.readUTF();
ByteArrayInputStream bais = new ByteArrayInputStream(initializationString.getBytes("UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(bais, "UTF-8"));
info = new WorldInfoImplementation(reader);
if (processor instanceof ClientPacketsContext) {
processor.logger().info("Received World initialization packet");
ClientPacketsContext cpp = (ClientPacketsContext) processor;
OnlineContentTranslator contentTranslator = cpp.getContentTranslator();
if (contentTranslator == null) {
processor.logger().error("Can't initialize a world without a ContentTranslator initialized first!");
return;
}
// TODO should we expose this to the interface ?
Client client = (Client) cpp.getContext();
Fence fence = client.getGameWindow().queueSynchronousTask(new Runnable() {
@Override
public void run() {
WorldClientRemote world;
try {
world = new WorldClientRemote(client, info, contentTranslator, cpp.getConnection());
client.changeWorld(world);
cpp.getConnection().handleSystemRequest("world/ok");
} catch (WorldLoadingException e) {
client.exitToMainMenu(e.getMessage());
}
}
});
fence.traverse();
}
}
use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.
the class ChunkRenderDataHolder method setData.
public void setData(ChunkMeshDataSections newData) {
if (newData == null)
throw new NullPointerException("setData() requires non-null ata");
oneUploadAtATime.acquireUninterruptibly();
noDrawDeleteConflicts.acquireUninterruptibly();
// Meh that's a waste of time then
if (isDestroyed) {
noDrawDeleteConflicts.release();
oneUploadAtATime.release();
// <-- Free the data
newData.notNeeded();
return;
}
// No verticesObject already created; create one, fill it and then change the bails
if (verticesObject == null) {
VertexBuffer wip = worldRenderer.getRenderingInterface().newVertexBuffer();
Fence fence = wip.uploadData(newData.dataToUpload);
// We unlock while waiting for the upload
noDrawDeleteConflicts.release();
fence.traverse();
// Then we lock again
noDrawDeleteConflicts.acquireUninterruptibly();
verticesObject = wip;
currentData = newData;
// And we're good !
} else // Already a VerticesObject present hum, we create another one then delete the old one
{
VertexBuffer wip = worldRenderer.getRenderingInterface().newVertexBuffer();
Fence fence = wip.uploadData(newData.dataToUpload);
// We unlock while waiting for the upload
noDrawDeleteConflicts.release();
fence.traverse();
// Then we lock again
noDrawDeleteConflicts.acquireUninterruptibly();
// We delete the OLD one
verticesObject.destroy();
// We swap the new one in
verticesObject = wip;
currentData = newData;
}
newData.consumed();
noDrawDeleteConflicts.release();
oneUploadAtATime.release();
}
use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.
the class MultithreadedOfflineWorldConverter method stepThreeSpreadLightning.
protected void stepThreeSpreadLightning(WorldTool csWorld) {
verbose("Entering step three: spreading light");
csWorld.setLightning(true);
WorldSize size = csWorld.getWorldInfo().getSize();
int maxHeightPossible = 256;
int done = 0;
int todo = (size.sizeInChunks) * (size.sizeInChunks);
double completion = 0.0;
long lastPercentageShow = System.currentTimeMillis();
Set<ChunkHolder> registeredCS_Holders = new HashSet<ChunkHolder>();
Set<Heightmap> registeredCS_Summaries = new HashSet<Heightmap>();
int chunksAquired = 0;
WorldUser worldUser = this;
int waveSize = this.threadsCount * 32;
int wave = 0;
CompoundFence waveFence = new CompoundFence();
for (int chunkX = 0; chunkX < size.sizeInChunks; chunkX++) {
for (int chunkZ = 0; chunkZ < size.sizeInChunks; chunkZ++) {
wave++;
CompoundFence loadRelevantData = new CompoundFence();
Heightmap sum = csWorld.getRegionsSummariesHolder().aquireHeightmapChunkCoordinates(worldUser, chunkX, chunkZ);
registeredCS_Summaries.add(sum);
loadRelevantData.add(sum.waitForLoading());
// Loads 3x3 arround relevant chunks
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
for (int chunkY = 0; chunkY <= maxHeightPossible / 32; chunkY++) {
ChunkHolder chunkHolder = csWorld.aquireChunkHolder(worldUser, chunkX + i, chunkY, chunkZ + j);
if (chunkHolder != null) {
loadRelevantData.add(chunkHolder.waitForLoading());
if (registeredCS_Holders.add(chunkHolder))
chunksAquired++;
}
}
}
}
assert chunksAquired == registeredCS_Holders.size();
// Wait for everything to actually load
loadRelevantData.traverse();
// Spreads lightning, from top to botton
for (int chunkY = maxHeightPossible / 32; chunkY >= 0; chunkY--) {
CubicChunk chunk = csWorld.getChunk(chunkX, chunkY, chunkZ);
Fence fence = chunk.lightBaker.requestLightningUpdate();
// TaskLightChunk task = new TaskLightChunk(chunk, true);
// workers.scheduleTask(task);
waveFence.add(fence);
}
if (wave >= waveSize) {
waveFence.traverse();
waveFence.clear();
while (true) {
if (workers.size() > 0) {
// endless tasks
try {
Thread.sleep(50L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
break;
}
wave = 0;
// Show progress
done += waveSize;
if (Math.floor(((double) done / (double) todo) * 100) > completion) {
completion = Math.floor(((double) done / (double) todo) * 100);
if (completion >= 100.0 || (System.currentTimeMillis() - lastPercentageShow > 5000)) {
verbose(completion + "% ... using " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + "/" + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "Mb ");
lastPercentageShow = System.currentTimeMillis();
}
}
if (registeredCS_Holders.size() > targetChunksToKeepInRam) {
for (ChunkHolder holder : registeredCS_Holders) {
holder.unregisterUser(worldUser);
chunksAquired--;
}
for (Heightmap summary : registeredCS_Summaries) summary.unregisterUser(worldUser);
registeredCS_Summaries.clear();
registeredCS_Holders.clear();
csWorld.unloadUselessData().traverse();
// verbose("Done.");
}
}
}
}
waveFence.traverse();
wave = 0;
// Terminate
for (ChunkHolder holder : registeredCS_Holders) {
holder.unregisterUser(worldUser);
chunksAquired--;
}
for (Heightmap summary : registeredCS_Summaries) summary.unregisterUser(worldUser);
registeredCS_Summaries.clear();
registeredCS_Holders.clear();
csWorld.unloadUselessData().traverse();
/*csWorld.saveEverything();
for (ChunkHolder holder : registeredCS_Holders)
holder.unregisterUser(worldUser);
csWorld.unloadUselessData().traverse();*/
}
use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.
the class TaskBuildHeightmap method task.
@Override
protected boolean task(TaskExecutor taskExecutor) {
ConverterWorkerThread cwt = (ConverterWorkerThread) taskExecutor;
// We wait on a bunch of stuff to load everytime
CompoundFence loadRelevantData = new CompoundFence();
HeightmapImplementation summary = csWorld.getRegionsSummariesHolder().aquireHeightmap(cwt, regionX, regionZ);
loadRelevantData.add(summary.waitForLoading());
// Aquires the chunks we want to make the summaries of.
for (int innerCX = 0; innerCX < 8; innerCX++) for (int innerCZ = 0; innerCZ < 8; innerCZ++) for (int chunkY = 0; chunkY < OfflineWorldConverter.mcWorldHeight / 32; chunkY++) {
ChunkHolder holder = csWorld.aquireChunkHolder(cwt, regionX * 8 + innerCX, chunkY, regionZ * 8 + innerCZ);
if (holder != null) {
loadRelevantData.add(holder.waitForLoading());
if (cwt.registeredCS_Holders.add(holder))
cwt.chunksAquired++;
}
}
// Wait until all of that crap loads
loadRelevantData.traverse();
// Descend from top
for (int i = 0; i < 256; i++) for (int j = 0; j < 256; j++) {
for (int h = OfflineWorldConverter.mcWorldHeight; h > 0; h--) {
CellData data = csWorld.peekSafely(regionX * 256 + i, h, regionZ * 256 + j);
if (!data.getVoxel().isAir()) {
Voxel vox = data.getVoxel();
if (vox.getDefinition().isSolid() || vox.getDefinition().isLiquid()) {
summary.setTopCell(data);
break;
}
}
}
}
Fence waitForSummarySave = summary.saveSummary();
// cwt.converter().verbose("Waiting for summary saving...");
waitForSummarySave.traverse();
// cwt.converter().verbose("Done.");
// We don't need the summary anymore
summary.unregisterUser(cwt);
return true;
}
use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.
the class Ingame method destroy.
@Override
public void destroy() {
// Logout sequence: Save the player entity
if (world instanceof WorldMaster) {
Player player = getPlayer();
PlayerLogoutEvent playerDisconnectionEvent = new PlayerLogoutEvent(player);
world.getPluginManager().fireEvent(playerDisconnectionEvent);
if (this.playerEntity != null) {
SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + getPlayer().getName().toLowerCase() + ".csf");
playerEntityFile.write(this.playerEntity);
}
}
// Stop the game logic and save
if (world instanceof WorldMaster) {
// TODO: Stop simulation
Fence fence = ((WorldMaster) world).stopLogic();
// exitButton.text = "#{world.saving}";
fence.traverse();
fence = world.saveEverything();
// exitButton.text = "#{world.saving}";
fence.traverse();
}
// Disables plugins
world.getPluginManager().disablePlugins();
this.world.getWorldRenderer().destroy();
}
Aggregations