|
| 1 | +// Copyright 2022 The Terasology Foundation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package org.terasology.wildAnimals.system; |
| 5 | + |
| 6 | +import com.google.common.collect.Lists; |
| 7 | +import org.joml.Vector3f; |
| 8 | +import org.joml.Vector3i; |
| 9 | +import org.joml.Vector3ic; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.MethodOrderer; |
| 12 | +import org.junit.jupiter.api.Order; |
| 13 | +import org.junit.jupiter.api.Tag; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.TestInstance; |
| 16 | +import org.junit.jupiter.api.TestMethodOrder; |
| 17 | +import org.junit.jupiter.api.Timeout; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.terasology.engine.config.Config; |
| 22 | +import org.terasology.engine.config.SystemConfig; |
| 23 | +import org.terasology.engine.context.Context; |
| 24 | +import org.terasology.engine.core.GameEngine; |
| 25 | +import org.terasology.engine.core.PathManager; |
| 26 | +import org.terasology.engine.core.Time; |
| 27 | +import org.terasology.engine.core.bootstrap.EntitySystemSetupUtil; |
| 28 | +import org.terasology.engine.core.module.ModuleManager; |
| 29 | +import org.terasology.engine.core.subsystem.EngineSubsystem; |
| 30 | +import org.terasology.engine.entitySystem.entity.EntityManager; |
| 31 | +import org.terasology.engine.entitySystem.entity.EntityRef; |
| 32 | +import org.terasology.engine.entitySystem.entity.internal.EngineEntityManager; |
| 33 | +import org.terasology.engine.entitySystem.prefab.PrefabManager; |
| 34 | +import org.terasology.engine.integrationenvironment.MainLoop; |
| 35 | +import org.terasology.engine.integrationenvironment.jupiter.Dependencies; |
| 36 | +import org.terasology.engine.integrationenvironment.jupiter.IntegrationEnvironment; |
| 37 | +import org.terasology.engine.integrationenvironment.jupiter.MTEExtension; |
| 38 | +import org.terasology.engine.logic.location.LocationComponent; |
| 39 | +import org.terasology.engine.logic.players.LocalPlayer; |
| 40 | +import org.terasology.engine.persistence.StorageManager; |
| 41 | +import org.terasology.engine.persistence.internal.ReadWriteStorageManager; |
| 42 | +import org.terasology.engine.rendering.logic.SkeletalMeshComponent; |
| 43 | +import org.terasology.engine.world.WorldProvider; |
| 44 | +import org.terasology.engine.world.block.BlockManager; |
| 45 | +import org.terasology.engine.world.block.BlockRegion; |
| 46 | +import org.terasology.engine.world.chunks.Chunks; |
| 47 | +import org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager; |
| 48 | +import org.terasology.wildAnimals.AnimalSpawnConfig; |
| 49 | +import reactor.core.publisher.Flux; |
| 50 | + |
| 51 | +import java.io.IOException; |
| 52 | +import java.nio.file.Path; |
| 53 | +import java.util.HashSet; |
| 54 | +import java.util.Set; |
| 55 | +import java.util.concurrent.atomic.AtomicInteger; |
| 56 | +import java.util.function.Supplier; |
| 57 | +import java.util.stream.Collectors; |
| 58 | + |
| 59 | +import static com.google.common.truth.Truth.assertThat; |
| 60 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 61 | + |
| 62 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 63 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 64 | +@Tag("MteTest") |
| 65 | +@ExtendWith(MTEExtension.class) |
| 66 | +@Dependencies("WildAnimals") |
| 67 | +@IntegrationEnvironment(subsystem = WildAnimalsSpawnSystemTest.WriteSaveGames.class) |
| 68 | +@Timeout(3600) |
| 69 | +class WildAnimalsSpawnSystemTest { |
| 70 | + |
| 71 | + private static final Logger logger = LoggerFactory.getLogger(WildAnimalsSpawnSystemTest.class); |
| 72 | + |
| 73 | + Set<Vector3ic> inhabitedChunks = new HashSet<>(); |
| 74 | + |
| 75 | + @BeforeAll |
| 76 | + void configSpawn(WildAnimalsSpawnSystem spawnSystem, WorldProvider worldProvider) { |
| 77 | + var config = new AnimalSpawnConfig(); |
| 78 | + config.spawnChanceInPercent = 100; |
| 79 | + config.minFlockSize = 1; |
| 80 | + config.minGroundPerFlockAnimal = 1; |
| 81 | + spawnSystem.setConfig(config); |
| 82 | + |
| 83 | + // Spawn on any solid ground. |
| 84 | + spawnSystem.setSpawnCondition(pos -> { |
| 85 | + Vector3i below = new Vector3i(pos.x, pos.y - 1, pos.z); |
| 86 | + return worldProvider.getBlock(pos).isPenetrable() |
| 87 | + && !worldProvider.getBlock(below).isPenetrable(); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @Order(1) |
| 93 | + void testCanSave(EntityManager entities, StorageManager storage, LocalPlayer player, MainLoop main, |
| 94 | + WildAnimalsSpawnSystem spawnSystem, PrefabManager prefabs) { |
| 95 | + var character = player.getCharacterEntity(); |
| 96 | + var loc = character.getComponent(LocationComponent.class).getWorldPosition(new Vector3f()); |
| 97 | + main.runUntil(main.makeBlocksRelevant(new BlockRegion((int) loc.x, (int) loc.y, (int) loc.z) |
| 98 | + .expand(100, 0, 100))); |
| 99 | + |
| 100 | +// var sheepPrefab = prefabs.getPrefab("WildAnimals:sheep"); |
| 101 | +// spawnSystem.tryFlockAnimalSpawn(sheepPrefab, Chunks.toChunkPos(loc, new Vector3i())); |
| 102 | +// |
| 103 | +// main.runUntil(new Ticks(1)); |
| 104 | + |
| 105 | + assertThat(entities.getEntitiesWith(SkeletalMeshComponent.class)).isNotEmpty(); |
| 106 | + |
| 107 | + var animalPosition = Flux.fromIterable(entities.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) |
| 108 | + .map(e -> e.getComponent(LocationComponent.class).getWorldPosition(new Vector3f())) |
| 109 | + .map(v -> (Vector3ic) Chunks.toChunkPos(v, new Vector3i())) |
| 110 | + .collect(Collectors.toUnmodifiableSet()) |
| 111 | + .block(); |
| 112 | + inhabitedChunks.addAll(animalPosition); |
| 113 | + |
| 114 | + storage.waitForCompletionOfPreviousSaveAndStartSaving(); |
| 115 | + storage.finishSavingAndShutdown(); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @Order(2) |
| 120 | + void testCanLoad(ModuleManager modules, |
| 121 | + BlockManager blockManager, ExtraBlockDataManager extraDataManager, |
| 122 | + Context context, Config config, MainLoop main, Time time) throws IOException { |
| 123 | + // MTE does not include helpers for loading from a save file. |
| 124 | + // This is kludged together from StorageManagerTest internals. |
| 125 | + EntitySystemSetupUtil.addReflectionBasedLibraries(context); |
| 126 | + EntitySystemSetupUtil.addEntityManagementRelatedClasses(context); |
| 127 | + EngineEntityManager newEntityManager = context.getValue(EngineEntityManager.class); |
| 128 | + StorageManager newSM = new ReadWriteStorageManager(getSavePath(config), modules.getEnvironment(), newEntityManager, blockManager, |
| 129 | + extraDataManager, null, null, null); |
| 130 | + context.put(StorageManager.class, newSM); |
| 131 | + |
| 132 | + newSM.loadGlobalStore(); |
| 133 | + |
| 134 | + for (Vector3ic chunk : inhabitedChunks) { |
| 135 | + var restored = newSM.loadChunkStore(chunk); |
| 136 | + var dudes = restored.restoreEntities(); |
| 137 | + logger.warn("restored dudes in {} '{}'", chunk, dudes); |
| 138 | + } |
| 139 | + |
| 140 | +// var later = time.getGameTime() + 2; |
| 141 | +// main.runUntil(() -> time.getGameTime() > later); |
| 142 | + |
| 143 | + var animals = Lists.newArrayList(newEntityManager.getEntitiesWith(SkeletalMeshComponent.class)); |
| 144 | + assertThat(animals).isNotNull(); |
| 145 | + assertThat(animals).isNotEmpty(); |
| 146 | + for (EntityRef animal : animals) { |
| 147 | + var skeleton = animal.getComponent(SkeletalMeshComponent.class); |
| 148 | + assertThat(skeleton.boneEntities).isNotNull(); |
| 149 | + assertThat(skeleton.boneEntities).isNotEmpty(); |
| 150 | + skeleton.boneEntities.forEach((name, boneEnt) -> |
| 151 | + assertWithMessage("Bone \"%s\"", name).that(boneEnt).isNotEqualTo(EntityRef.NULL)); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // copied in from StorageManagerTest |
| 156 | + Path getSavePath(Config config) { |
| 157 | + // TODO: add a more direct way of inspecting StorageManager's path. |
| 158 | + // This way of getting the game title (and thus the path) is a bit brittle, |
| 159 | + // because world title and game title are not _required_ to be identical. |
| 160 | + String gameTitle = config.getWorldGeneration().getWorldTitle(); |
| 161 | + return PathManager.getInstance().getSavePath(gameTitle); |
| 162 | + } |
| 163 | + |
| 164 | + static class WriteSaveGames implements EngineSubsystem { |
| 165 | + @Override |
| 166 | + public String getName() { |
| 167 | + return getClass().getCanonicalName(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void initialise(GameEngine engine, Context rootContext) { |
| 172 | + rootContext.getValue(SystemConfig.class).writeSaveGamesEnabled.set(true); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + static class Ticks implements Supplier<Boolean> { |
| 177 | + private final AtomicInteger count; |
| 178 | + Ticks(int initial) { |
| 179 | + count = new AtomicInteger(initial); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public Boolean get() { |
| 184 | + return count.getAndDecrement() > 0; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments