diff --git a/core/src/test/java/art/arcane/iris/core/content/ContentServicesTest.java b/core/src/test/java/art/arcane/iris/core/content/ContentServicesTest.java new file mode 100644 index 000000000..2251510cd --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/content/ContentServicesTest.java @@ -0,0 +1,133 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.content; + +import art.arcane.iris.builtin.loot.LootTable; +import art.arcane.iris.builtin.spawn.EntityDefinition; +import art.arcane.iris.builtin.spawn.SpawnMarker; +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.modules.ModuleResourceFixture; +import art.arcane.iris.engine.UsableBlockPlatform; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.EngineGeneration; +import art.arcane.iris.engine.framework.SeedManager; +import art.arcane.iris.engine.loot.LootModule; +import art.arcane.iris.engine.loot.LootResources; +import art.arcane.iris.engine.object.TestSettings; +import art.arcane.iris.engine.object.world.IrisWorld; +import art.arcane.iris.engine.spawn.SpawnModule; +import art.arcane.iris.engine.spawn.SpawnResources; +import art.arcane.iris.spi.PlatformWorld; +import com.volmit.item.ItemRegistry; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.List; +import java.util.OptionalInt; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** The answers of the loot and spawn modules reach adapters that cannot name IrisApi as host-typed values. */ +public class ContentServicesTest { + private Engine engine; + + @BeforeClass + public static void bindPlatform() { + UsableBlockPlatform.bind(); + } + + @AfterClass + public static void unbindPlatform() { + UsableBlockPlatform.unbind(); + } + + @Before + public void buildEngine() { + engine = mock(Engine.class); + IrisData data = mock(IrisData.class); + ModuleResourceFixture fixture = ModuleResourceFixture.serve(data); + ModuleResourceFixture.serveModules(data, new SpawnModule(), new LootModule()); + ItemRegistry items = mock(ItemRegistry.class); + when(items.maxDurability(anyString())).thenReturn(OptionalInt.of(0)); + when(data.getPackResources().items()).thenReturn(items); + when(engine.getData()).thenReturn(data); + IrisWorld engineWorld = mock(IrisWorld.class); + when(engineWorld.platformWorld()).thenReturn(mock(PlatformWorld.class)); + when(engine.getWorld()).thenReturn(engineWorld); + SeedManager seeds = mock(SeedManager.class); + when(seeds.getEntity()).thenReturn(11L); + when(seeds.getLoot()).thenReturn(5L); + when(engine.getSeedManager()).thenReturn(seeds); + when(engine.getMinHeight()).thenReturn(-64); + when(engine.getGeneration()).thenReturn(new EngineGeneration(engine)); + fixture.resource(SpawnResources.ENTITIES, "guard", TestSettings.of(EntityDefinition.class, """ + {"type": "minecraft:zombie", "customName": "Guard", "baby": true, "glowing": true, + "helmet": {"type": "iron_helmet", "enchantments": [{"enchantment": "protection", "minLevel": 3, "maxLevel": 3}]}, + "loot": {"tables": ["guard/drops"]}, + "rawCommands": [{"commands": ["say hi {x}"], "delay": 4, "repeat": true, "repeatDelay": 20}]}""")); + fixture.resource(SpawnResources.MARKERS, "camp", TestSettings.of(SpawnMarker.class, + "{\"spawners\": [], \"emptyAbove\": false, \"removeOnChange\": false}")); + fixture.resource(LootResources.TABLES, "guard/drops", TestSettings.of(LootTable.class, + "{\"name\": \"Drops\", \"minPicked\": 1, \"maxPicked\": 1, \"loot\": [{\"type\": \"bread\", \"minAmount\": 3, \"maxAmount\": 3}]}")); + } + + @Test + public void anEntityKeepsEveryDecisionTheModuleMade() { + ContentEntity guard = ContentServices.entity(engine, "guard").orElseThrow(); + + assertEquals("minecraft:zombie", guard.type()); + assertEquals("Guard", guard.customName()); + assertTrue(guard.flags().baby()); + assertTrue(guard.flags().glowing()); + assertFalse(guard.isSpecial()); + assertEquals("iron_helmet", guard.equipment().helmet().item()); + assertEquals(List.of(new ContentItem.Enchantment("protection", 3)), guard.equipment().helmet().enchantments()); + assertEquals(List.of("guard/drops"), guard.lootTables()); + assertEquals(List.of(new ContentEntity.Command(List.of("say hi {x}"), 4, true, 20)), guard.commands()); + assertTrue(ContentServices.entity(engine, "nobody").isEmpty()); + } + + @Test + public void aMarkerRuleIsTheModulesAnswer() { + ContentSpawns.MarkerRule rule = ContentServices.markerRule(engine, "camp").orElseThrow(); + + assertEquals(List.of(), rule.spawners()); + assertFalse(rule.emptyAbove()); + assertFalse(rule.removeOnChange()); + assertTrue(ContentServices.markerRule(engine, "unknown").isEmpty()); + } + + @Test + public void aTablesItemsAreDrawnByTheModuleAtTheSpot() { + List items = ContentServices.tableItems(engine, 10, 64, -3, "guard/drops"); + + assertEquals(1, items.size()); + assertEquals("bread", items.getFirst().item()); + assertEquals(3, items.getFirst().amount()); + assertEquals(items, ContentServices.tableItems(engine, 10, 64, -3, "guard/drops")); + assertTrue(ContentServices.tableItems(engine, 10, 64, -3, "missing").isEmpty()); + } +} diff --git a/core/src/test/java/art/arcane/iris/engine/spawn/AmbientEffectingTest.java b/core/src/test/java/art/arcane/iris/engine/spawn/AmbientEffectingTest.java new file mode 100644 index 000000000..c3c681d4e --- /dev/null +++ b/core/src/test/java/art/arcane/iris/engine/spawn/AmbientEffectingTest.java @@ -0,0 +1,141 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.spawn; + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.modules.BuiltInPack; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.EngineGeneration; +import art.arcane.iris.engine.object.TestSettings; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.engine.object.world.IrisWorld; +import art.arcane.iris.builtin.spawn.EffectSettings; +import art.arcane.iris.spi.PlatformWorld; +import com.volmit.service.AmbientEffects; +import com.volmit.world.BlockPosition; +import com.volmit.world.PackWorld; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** The ambient effects due around a player: the region's, then the biome's, each when its interval and chance allow, fully decided. */ +public class AmbientEffectingTest { + private static final BlockPosition AT = new BlockPosition(10, 70, -20); + private static final String SOUND_AND_POTION = """ + {"effects": [{"sound": "ambient.cave", "soundDistance": 4, "minPitch": 0.5, "maxPitch": 0.5, "volume": 2, + "potionEffect": "luck", "potionStrength": 1, "potionTicksMin": 100, "potionTicksMax": 100, + "interval": 1, "chance": 1}]}"""; + private static final String PARTICLES_AND_COMMANDS = """ + {"effects": [{"particleEffect": "flame", "particleDistance": 1, "particleAway": 5, "particleDistanceWidth": 0, + "particleCount": 2, "particleAltX": 0.5, "randomAltX": false, "randomAltZ": false, "extra": 0.25, + "commandRegistry": {"rawCommands": [{"commands": ["say hi {x}"], "delay": 3}, + {"commands": ["say never"], "weather": "DOWNFALL"}], + "commandOffsetX": 2, "commandRandomAltX": false, "commandAllRandomLocations": false}, + "interval": 1, "chance": 1}]}"""; + + private Engine engine; + private IrisRegion region; + private IrisBiome biome; + private PackWorld world; + + @Before + public void buildWorld() { + engine = mock(Engine.class); + when(engine.getData()).thenReturn(mock(IrisData.class)); + IrisWorld engineWorld = mock(IrisWorld.class); + PlatformWorld platformWorld = mock(PlatformWorld.class); + when(engineWorld.platformWorld()).thenReturn(platformWorld); + when(engine.getWorld()).thenReturn(engineWorld); + region = mock(IrisRegion.class); + biome = mock(IrisBiome.class); + when(region.getModuleSettings()).thenReturn(ModuleSettingsOf.none()); + when(biome.getModuleSettings()).thenReturn(ModuleSettingsOf.none()); + when(engine.getRegion(10, -20)).thenReturn(region); + when(engine.getBiome(10, 70, -20)).thenReturn(biome); + world = new EngineGeneration(engine).world(); + } + + @Test + public void anAreaWithoutEffectsHasNothingDue() { + assertEquals(List.of(), new AmbientEffecting().due(world, AT)); + } + + @Test + public void aSoundAndAPotionComeWithTheirDecidedValues() throws Exception { + when(biome.getModuleSettings()).thenReturn(BuiltInPack.settings(TestSettings.of(EffectSettings.class, SOUND_AND_POTION))); + Thread.sleep(5); + + List due = new AmbientEffecting().due(world, AT); + + assertEquals(1, due.size()); + AmbientEffects.Sound sound = due.getFirst().sound(); + assertEquals("ambient.cave", sound.key()); + assertTrue(Math.abs(sound.offsetX()) <= 4 && Math.abs(sound.offsetY()) <= 4 && Math.abs(sound.offsetZ()) <= 4); + assertEquals(2F, sound.volume(), 0); + assertEquals(0.5F, sound.pitch(), 0); + assertEquals(new AmbientEffects.Potion("luck", 1, 100), due.getFirst().potion()); + assertNull(due.getFirst().particles()); + assertTrue(due.getFirst().commands().isEmpty()); + } + + @Test + public void particlesAndOnlyTheCommandsThatMayRunNowComeWithTheirOffsets() throws Exception { + when(region.getModuleSettings()).thenReturn(BuiltInPack.settings(TestSettings.of(EffectSettings.class, PARTICLES_AND_COMMANDS))); + Thread.sleep(5); + + AmbientEffects.Effect effect = new AmbientEffecting().due(world, AT).getFirst(); + + assertEquals("flame", effect.particles().key()); + assertEquals(5, effect.particles().forward(), 0); + assertEquals(0, effect.particles().sideways(), 0); + assertEquals(0.5, effect.particles().altX(), 0); + assertEquals(0.25, effect.particles().extra(), 0); + assertEquals(2, effect.particles().count()); + assertEquals(1, effect.commands().size()); + assertEquals(List.of("say hi {x}"), effect.commands().getFirst().command().commands()); + assertEquals(3, effect.commands().getFirst().command().delay()); + assertEquals(2, effect.commands().getFirst().offsetX(), 0); + assertNull(effect.sound()); + assertNull(effect.potion()); + } + + @Test + public void anEffectPlaysOnlyOncePerInterval() { + when(biome.getModuleSettings()).thenReturn(BuiltInPack.settings(TestSettings.of(EffectSettings.class, + SOUND_AND_POTION.replace("\"interval\": 1", "\"interval\": 600000")))); + AmbientEffecting effecting = new AmbientEffecting(); + + assertEquals(1, effecting.due(world, AT).size()); + assertEquals(0, effecting.due(world, AT).size()); + } + + private static final class ModuleSettingsOf { + private static art.arcane.iris.core.modules.ModuleSettings none() { + return art.arcane.iris.core.modules.ModuleSettings.NONE; + } + } +} diff --git a/core/src/test/java/art/arcane/iris/engine/spawn/SpawnResourcesTest.java b/core/src/test/java/art/arcane/iris/engine/spawn/SpawnResourcesTest.java index 5b3d39ad9..b97b5f2a2 100644 --- a/core/src/test/java/art/arcane/iris/engine/spawn/SpawnResourcesTest.java +++ b/core/src/test/java/art/arcane/iris/engine/spawn/SpawnResourcesTest.java @@ -19,6 +19,9 @@ package art.arcane.iris.engine.spawn; import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.project.ModuleResourceSchemas; +import art.arcane.iris.core.project.SchemaTestPlatform; +import art.arcane.volmlib.util.json.JSONObject; import art.arcane.iris.builtin.spawn.EntityDefinition; import art.arcane.iris.builtin.spawn.SpawnMarker; import art.arcane.iris.builtin.spawn.Spawner; @@ -35,6 +38,7 @@ import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; /** Spawners, entities and markers are resource kinds of module {@code iris:spawn}, read from the folders packs have always had. */ public class SpawnResourcesTest { @@ -84,6 +88,25 @@ public class SpawnResourcesTest { } } + @Test + public void studioDescribesSpawnersEntitiesAndMarkers() throws Exception { + Path pack = spawnPack("studio"); + IrisData data = IrisData.openValidation(pack.toFile()); + try (SchemaTestPlatform ignored = SchemaTestPlatform.bind(temporaryFolder.getRoot())) { + List entries = ModuleResourceSchemas.entries(data, true); + + for (String schema : List.of("spawners", "entities", "markers")) { + assertTrue(schema, entries.stream().anyMatch(entry -> entry.getString("url").equals("./.iris/schema/" + schema + "-schema.json"))); + assertTrue(schema, Files.exists(pack.resolve(".iris/schema/" + schema + "-schema.json"))); + } + JSONObject entity = new JSONObject(Files.readString(pack.resolve(".iris/schema/entities-schema.json"))); + assertTrue(entity.toString().contains("passengers")); + assertTrue(entity.toString().contains("tables")); + } finally { + data.close(); + } + } + private Path spawnPack(String name) throws Exception { File pack = temporaryFolder.newFolder(name); write(pack.toPath(), "dimensions/main.json", "{\"regions\":[\"region\"]}"); diff --git a/core/src/test/java/art/arcane/iris/engine/spawn/SpawnerGateTest.java b/core/src/test/java/art/arcane/iris/engine/spawn/SpawnerGateTest.java new file mode 100644 index 000000000..4184581c5 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/engine/spawn/SpawnerGateTest.java @@ -0,0 +1,113 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.spawn; + +import art.arcane.iris.builtin.spawn.Spawner; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.TestSettings; +import art.arcane.iris.engine.object.runtime.IrisEngineData; +import art.arcane.iris.engine.object.world.IrisWorld; +import art.arcane.iris.engine.object.world.biome.InferredType; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.spi.PlatformWorld; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** Whether a spawner may fire: the biome kind its group wants, the world's time and weather, and its rates in the engine's data. */ +public class SpawnerGateTest { + private Engine engine; + private PlatformWorld world; + + @Before + public void buildEngine() { + engine = mock(Engine.class); + IrisWorld engineWorld = mock(IrisWorld.class); + world = mock(PlatformWorld.class); + when(engineWorld.platformWorld()).thenReturn(world); + when(engine.getWorld()).thenReturn(engineWorld); + when(engine.getEngineData()).thenReturn(new IrisEngineData()); + } + + @Test + public void theGroupDecidesWhichBiomesSuit() { + Spawner normal = spawner("{\"group\": \"NORMAL\"}"); + Spawner underwater = spawner("{\"group\": \"UNDERWATER\"}"); + Spawner beach = spawner("{\"group\": \"BEACH\"}"); + Spawner cave = spawner("{\"group\": \"CAVE\"}"); + + assertTrue(SpawnerGate.suits(normal, biome(InferredType.LAND))); + assertFalse(SpawnerGate.suits(normal, biome(InferredType.SEA))); + assertTrue(SpawnerGate.suits(underwater, biome(InferredType.SEA))); + assertFalse(SpawnerGate.suits(underwater, biome(InferredType.LAND))); + assertTrue(SpawnerGate.suits(beach, biome(InferredType.SHORE))); + assertFalse(SpawnerGate.suits(beach, biome(InferredType.CAVE))); + assertTrue(SpawnerGate.suits(cave, biome(InferredType.SEA))); + } + + @Test + public void timeAndWeatherMustMatch() { + Spawner daytimeRain = spawner("{\"timeBlock\": {\"startHour\": 8, \"endHour\": 18}, \"weather\": \"DOWNFALL\"}"); + when(world.getTime()).thenReturn(6000L); + when(world.isStorming()).thenReturn(true); + assertTrue(SpawnerGate.suits(daytimeRain, world)); + + when(world.isStorming()).thenReturn(false); + assertFalse(SpawnerGate.suits(daytimeRain, world)); + + when(world.isStorming()).thenReturn(true); + when(world.getTime()).thenReturn(20000L); + assertFalse(SpawnerGate.suits(daytimeRain, world)); + } + + @Test + public void aSpawnerWithoutRatesAlwaysMayFire() { + Spawner free = spawner("{}"); + + SpawnerGate.spawned("free", free, engine, 3, 4); + + assertTrue(SpawnerGate.canSpawn("free", free, engine, 3, 4)); + } + + @Test + public void aRateHoldsTheSpawnerBackForItsIntervalPerWorldAndPerChunk() { + Spawner limited = spawner("{\"maximumRate\": {\"amount\": 1, \"per\": {\"minutes\": 10}}, \"maximumRatePerChunk\": {\"amount\": 1, \"per\": {\"minutes\": 10}}}"); + + assertTrue(SpawnerGate.canSpawn("limited", limited, engine, 3, 4)); + SpawnerGate.spawned("limited", limited, engine, 3, 4); + + assertFalse(SpawnerGate.canSpawn("limited", limited, engine, 3, 4)); + assertFalse(SpawnerGate.canSpawn("limited", limited, engine, 9, 9)); + assertTrue(SpawnerGate.canSpawn("other", limited, engine, 9, 9)); + } + + private static Spawner spawner(String json) { + return TestSettings.of(Spawner.class, json); + } + + private static IrisBiome biome(InferredType type) { + IrisBiome biome = mock(IrisBiome.class); + when(biome.getInferredType()).thenReturn(type); + return biome; + } +}