Search in sources :

Example 1 with ChunkProvider

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;
}
Also used : ChunkProvider(org.terasology.world.chunks.ChunkProvider)

Example 2 with ChunkProvider

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;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Config(org.terasology.config.Config) SimpleUri(org.terasology.engine.SimpleUri) EntityManager(org.terasology.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.network.NetworkComponent) ChunkProvider(org.terasology.world.chunks.ChunkProvider) WorldComponent(org.terasology.world.WorldComponent) NetworkComponent(org.terasology.network.NetworkComponent) WorldComponent(org.terasology.world.WorldComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 3 with ChunkProvider

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());
}
Also used : EngineEntityManager(org.terasology.entitySystem.entity.internal.EngineEntityManager) ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3f(org.terasology.math.geom.Vector3f) StorageManager(org.terasology.persistence.StorageManager) Chunk(org.terasology.world.chunks.Chunk) ChunkProvider(org.terasology.world.chunks.ChunkProvider) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Example 4 with ChunkProvider

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();
}
Also used : NetworkSystem(org.terasology.network.NetworkSystem) ChunkProvider(org.terasology.world.chunks.ChunkProvider)

Example 5 with ChunkProvider

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();
}
Also used : AudioManager(org.terasology.audio.AudioManager) ModuleEnvironment(org.terasology.module.ModuleEnvironment) PhysicsEngine(org.terasology.physics.engine.PhysicsEngine) ChunkProvider(org.terasology.world.chunks.ChunkProvider) ModuleManager(org.terasology.engine.module.ModuleManager) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Aggregations

ChunkProvider (org.terasology.world.chunks.ChunkProvider)7 Test (org.junit.Test)3 ChunkStore (org.terasology.persistence.ChunkStore)3 Chunk (org.terasology.world.chunks.Chunk)3 ChunkImpl (org.terasology.world.chunks.internal.ChunkImpl)3 EntityRef (org.terasology.entitySystem.entity.EntityRef)2 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)2 StorageManager (org.terasology.persistence.StorageManager)2 Map (java.util.Map)1 AudioManager (org.terasology.audio.AudioManager)1 Config (org.terasology.config.Config)1 SimpleUri (org.terasology.engine.SimpleUri)1 EnvironmentSwitchHandler (org.terasology.engine.bootstrap.EnvironmentSwitchHandler)1 ModuleManager (org.terasology.engine.module.ModuleManager)1 Component (org.terasology.entitySystem.Component)1 EntityManager (org.terasology.entitySystem.entity.EntityManager)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Vector3f (org.terasology.math.geom.Vector3f)1 Vector3i (org.terasology.math.geom.Vector3i)1 ModuleEnvironment (org.terasology.module.ModuleEnvironment)1