use of org.terasology.world.chunks.ChunkProvider in project Terasology by MovingBlocks.
the class ReadWriteStorageManager method isSavingNecessary.
private boolean isSavingNecessary() {
ChunkProvider chunkProvider = CoreRegistry.get(ChunkProvider.class);
int unloadedChunkCount = unloadedAndUnsavedChunkMap.size();
int loadedChunkCount = chunkProvider.getAllChunks().size();
double totalChunkCount = unloadedChunkCount + loadedChunkCount;
double percentageUnloaded = 100.0 * unloadedChunkCount / totalChunkCount;
if (percentageUnloaded >= config.getSystem().getMaxUnloadedChunksPercentageTillSave()) {
return true;
}
long currentTime = System.currentTimeMillis();
if (nextAutoSave == null) {
scheduleNextAutoSave();
return false;
}
return currentTime >= nextAutoSave;
}
use of org.terasology.world.chunks.ChunkProvider in project Terasology by MovingBlocks.
the class CreateWorldEntity method step.
@Override
public boolean step() {
EntityManager entityManager = context.get(EntityManager.class);
ChunkProvider chunkProvider = context.get(ChunkProvider.class);
Iterator<EntityRef> worldEntityIterator = entityManager.getEntitiesWith(WorldComponent.class).iterator();
if (worldEntityIterator.hasNext()) {
EntityRef worldEntity = worldEntityIterator.next();
chunkProvider.setWorldEntity(worldEntity);
// get the world generator config from the world entity
// replace the world generator values from the components in the world entity
WorldGenerator worldGenerator = context.get(WorldGenerator.class);
WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
Map<String, Component> params = worldConfigurator.getProperties();
for (Map.Entry<String, Component> entry : params.entrySet()) {
Class<? extends Component> clazz = entry.getValue().getClass();
Component comp = worldEntity.getComponent(clazz);
if (comp != null) {
worldConfigurator.setProperty(entry.getKey(), comp);
}
}
} else {
EntityRef worldEntity = entityManager.create();
worldEntity.addComponent(new WorldComponent());
NetworkComponent networkComponent = new NetworkComponent();
networkComponent.replicateMode = NetworkComponent.ReplicateMode.ALWAYS;
worldEntity.addComponent(networkComponent);
chunkProvider.setWorldEntity(worldEntity);
// transfer all world generation parameters from Config to WorldEntity
WorldGenerator worldGenerator = context.get(WorldGenerator.class);
SimpleUri generatorUri = worldGenerator.getUri();
Config config = context.get(Config.class);
// get the map of properties from the world generator.
// Replace its values with values from the config set by the UI.
// Also set all the components to the world entity.
WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
Map<String, Component> params = worldConfigurator.getProperties();
for (Map.Entry<String, Component> entry : params.entrySet()) {
Class<? extends Component> clazz = entry.getValue().getClass();
Component comp = config.getModuleConfig(generatorUri, entry.getKey(), clazz);
if (comp != null) {
worldEntity.addComponent(comp);
worldConfigurator.setProperty(entry.getKey(), comp);
} else {
worldEntity.addComponent(entry.getValue());
}
}
}
return true;
}
use of org.terasology.world.chunks.ChunkProvider in project Terasology by MovingBlocks.
the class StorageManagerTest method testEntitySurvivesStorageInChunkStore.
@Test
public void testEntitySurvivesStorageInChunkStore() throws Exception {
Chunk chunk = new ChunkImpl(CHUNK_POS, blockManager, biomeManager);
chunk.setBlock(0, 0, 0, testBlock);
chunk.markReady();
ChunkProvider chunkProvider = mock(ChunkProvider.class);
when(chunkProvider.getAllChunks()).thenReturn(Arrays.asList(chunk));
CoreRegistry.put(ChunkProvider.class, chunkProvider);
EntityRef entity = entityManager.create();
long id = entity.getId();
LocationComponent locationComponent = new LocationComponent();
Vector3f positionInChunk = new Vector3f(chunk.getAABB().getMin());
positionInChunk.x += 1;
positionInChunk.y += 1;
positionInChunk.z += 1;
locationComponent.setWorldPosition(positionInChunk);
entity.addComponent(locationComponent);
esm.waitForCompletionOfPreviousSaveAndStartSaving();
esm.finishSavingAndShutdown();
EntitySystemSetupUtil.addReflectionBasedLibraries(context);
EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
EngineEntityManager newEntityManager = context.get(EngineEntityManager.class);
StorageManager newSM = new ReadWriteStorageManager(savePath, moduleEnvironment, newEntityManager, blockManager, biomeManager, false);
newSM.loadGlobalStore();
ChunkStore restored = newSM.loadChunkStore(CHUNK_POS);
restored.restoreEntities();
EntityRef ref = newEntityManager.getEntity(id);
assertTrue(ref.exists());
assertTrue(ref.isActive());
}
use of org.terasology.world.chunks.ChunkProvider in project Terasology by MovingBlocks.
the class ReadWriteStorageManager method createSaveTransaction.
private SaveTransaction createSaveTransaction() {
SaveTransactionBuilder saveTransactionBuilder = new SaveTransactionBuilder(privateEntityManager, entitySetDeltaRecorder, isStoreChunksInZips(), getStoragePathProvider(), worldDirectoryWriteLock);
ChunkProvider chunkProvider = CoreRegistry.get(ChunkProvider.class);
NetworkSystem networkSystem = CoreRegistry.get(NetworkSystem.class);
addChunksToSaveTransaction(saveTransactionBuilder, chunkProvider);
addPlayersToSaveTransaction(saveTransactionBuilder, networkSystem);
addGlobalStoreBuilderToSaveTransaction(saveTransactionBuilder);
addGameManifestToSaveTransaction(saveTransactionBuilder);
return saveTransactionBuilder.build();
}
use of org.terasology.world.chunks.ChunkProvider in project Terasology by MovingBlocks.
the class StateIngame method dispose.
@Override
public void dispose(boolean shuttingDown) {
ChunkProvider chunkProvider = context.get(ChunkProvider.class);
chunkProvider.dispose();
boolean save = networkSystem.getMode().isAuthority();
if (save) {
storageManager.waitForCompletionOfPreviousSaveAndStartSaving();
}
networkSystem.shutdown();
// TODO: Shutdown background threads
eventSystem.process();
GameThread.processWaitingProcesses();
nuiManager.clear();
context.get(AudioManager.class).stopAllSounds();
if (worldRenderer != null) {
worldRenderer.dispose();
worldRenderer = null;
}
componentSystemManager.shutdown();
context.get(PhysicsEngine.class).dispose();
entityManager.clear();
if (storageManager != null) {
storageManager.finishSavingAndShutdown();
}
ModuleEnvironment oldEnvironment = context.get(ModuleManager.class).getEnvironment();
context.get(ModuleManager.class).loadEnvironment(Collections.<Module>emptySet(), true);
if (!shuttingDown) {
context.get(EnvironmentSwitchHandler.class).handleSwitchToEmptyEnvironment(context);
}
if (oldEnvironment != null) {
oldEnvironment.close();
}
console.dispose();
GameThread.clearWaitingProcesses();
/*
* Clear the binding as otherwise the complete ingame state would be
* referenced.
*/
nuiManager.getHUD().clearVisibleBinding();
}
Aggregations