diff --git a/src/main/java/org/konstructs/forest/ForestConfig.java b/src/main/java/org/konstructs/forest/ForestConfig.java index 038b85a..840c360 100644 --- a/src/main/java/org/konstructs/forest/ForestConfig.java +++ b/src/main/java/org/konstructs/forest/ForestConfig.java @@ -8,9 +8,8 @@ public class ForestConfig { private final BlockTypeId thinLeaves; private final BlockTypeId sapling; private final BlockTypeId growsOn; + private final BlockTypeId seedsOn; private final int seedHeightDifference; - private final int maxSeedHeight; - private final int minSeedHeight; private final int maxGenerations; private final int minGenerations; private final int trunkRadi; @@ -21,6 +20,7 @@ public class ForestConfig { private final int minGrowthDelay; private final int randomGrowthDelay; private final int maxSeedsPerGeneration; + private final int seedEveryGeneration; private final int randomGrowth; ForestConfig(String wood, @@ -28,9 +28,8 @@ public class ForestConfig { String thinLeaves, String sapling, String growsOn, + String seedsOn, int seedHeightDifference, - int maxSeedHeight, - int minSeedHeight, int maxGenerations, int minGenerations, int trunkRadi, @@ -41,15 +40,15 @@ public class ForestConfig { int minGrowthDelay, int randomGrowthDelay, int maxSeedsPerGeneration, + int seedEveryGeneration, int randomGrowth) { this.wood = BlockTypeId.fromString(wood); this.leaves = BlockTypeId.fromString(leaves); this.thinLeaves = BlockTypeId.fromString(thinLeaves); this.sapling = BlockTypeId.fromString(sapling); this.growsOn = BlockTypeId.fromString(growsOn); + this.seedsOn = BlockTypeId.fromString(seedsOn); this.seedHeightDifference = seedHeightDifference; - this.maxSeedHeight = maxSeedHeight; - this.minSeedHeight = minSeedHeight; this.maxGenerations = maxGenerations; this.minGenerations = minGenerations; this.trunkRadi = trunkRadi; @@ -60,6 +59,7 @@ public class ForestConfig { this.minGrowthDelay = minGrowthDelay; this.randomGrowthDelay = randomGrowthDelay; this.maxSeedsPerGeneration = maxSeedsPerGeneration; + this.seedEveryGeneration = seedEveryGeneration; this.randomGrowth = randomGrowth; } @@ -78,15 +78,12 @@ public BlockTypeId getSapling() { public BlockTypeId getGrowsOn() { return growsOn; } + public BlockTypeId getSeedsOn() { + return seedsOn; + } public int getSeedHeightDifference() { return seedHeightDifference; } - public int getMaxSeedHeight() { - return maxSeedHeight; - } - public int getMinSeedHeight() { - return minSeedHeight; - } public int getMaxGenerations() { return maxGenerations; } @@ -117,6 +114,9 @@ public int getRandomGrowthDelay() { public int getMaxSeedsPerGeneration() { return maxSeedsPerGeneration; } + public int getSeedEveryGeneration() { + return seedEveryGeneration; + } public int getRandomGrowth() { return randomGrowth; } diff --git a/src/main/java/org/konstructs/forest/ForestPlugin.java b/src/main/java/org/konstructs/forest/ForestPlugin.java index d7823bb..9e7efb4 100644 --- a/src/main/java/org/konstructs/forest/ForestPlugin.java +++ b/src/main/java/org/konstructs/forest/ForestPlugin.java @@ -14,6 +14,7 @@ public class ForestPlugin extends KonstructsActor { private final ForestConfig config; private final BlockTypeId growsOn; + private final BlockTypeId seedsOn; private final BlockTypeId sapling; private final int randomGrowth; private final Random random = new Random(); @@ -23,22 +24,19 @@ public ForestPlugin(String name, ActorRef universe, ForestConfig config) { super(universe); this.config = config; this.growsOn = config.getGrowsOn(); + this.seedsOn = config.getSeedsOn(); this.sapling = config.getSapling(); this.randomGrowth = config.getRandomGrowth(); } void tryToSeed(Position pos) { Position start = - pos.withY(Math.max(pos.getY() - config.getSeedHeightDifference(), - config.getMinSeedHeight())); + pos.withY(pos.getY() - config.getSeedHeightDifference()); Position end = new Position(pos.getX() + 1, - Math.min(pos.getY() + config.getSeedHeightDifference(), - config.getMaxSeedHeight()), + pos.getY() + config.getSeedHeightDifference(), pos.getZ() + 1); - // Only run query if within the possible height band - if(start.getY() < end.getY()) - boxQuery(new Box(start, end)); + boxQuery(new Box(start, end)); } void seeded(Position pos) { @@ -58,7 +56,7 @@ void plant(Position pos) { public void onBoxQueryResult(BoxQueryResult result) { Map placed = result.getAsMap(); for(Map.Entry p: placed.entrySet()) { - if(p.getValue().equals(growsOn)) { + if(p.getValue().equals(growsOn) || p.getValue().equals(seedsOn)) { Position pos = p.getKey().addY(1); BlockTypeId above = placed.get(pos); if(above != null && above.equals(BlockTypeId.VACUUM)) { @@ -76,7 +74,7 @@ public void onBlockUpdateEvent(BlockUpdateEvent update) { if(after.equals(sapling)) { seeded(p.getKey()); } else if(after.equals(growsOn) && - random.nextInt(1000) <= randomGrowth) { + random.nextInt(10000) <= randomGrowth) { /* Try to seed a new tree */ scheduleSelfOnce(new TryToSeedTree(p.getKey()), (int)((float)(config.getMinGrowthDelay() * 1000 + @@ -116,9 +114,8 @@ public void onReceive(Object message) { @Config(key = "thin-leaves-block") String thinLeaves, @Config(key = "sapling-block") String sapling, @Config(key = "grows-on") String growsOn, + @Config(key = "seeds-on") String seedsOn, @Config(key = "max-seed-height-difference") int seedHeightDifference, - @Config(key = "max-seed-height") int maxSeedHeight, - @Config(key = "min-seed-height") int minSeedHeight, @Config(key = "max-generations") int maxGenerations, @Config(key = "min-generations") int minGenerations, @Config(key = "trunk-radi") int trunkRadi, @@ -129,6 +126,7 @@ public void onReceive(Object message) { @Config(key = "min-growth-delay") int minGrowthDelay, @Config(key = "random-growth-delay") int randomGrowthDelay, @Config(key = "max-seeds-per-generation") int maxSeedsPerGeneration, + @Config(key = "seed-every-generation") int seedEveryGeneration, @Config(key = "random-growth") int randomGrowth ) { Class currentClass = new Object() { }.getClass().getEnclosingClass(); @@ -139,9 +137,8 @@ public void onReceive(Object message) { thinLeaves, sapling, growsOn, + seedsOn, seedHeightDifference, - maxSeedHeight, - minSeedHeight, maxGenerations, minGenerations, trunkRadi, @@ -152,6 +149,7 @@ public void onReceive(Object message) { minGrowthDelay, randomGrowthDelay, maxSeedsPerGeneration, + seedEveryGeneration, randomGrowth); return Props.create(currentClass, pluginName, universe, config); } diff --git a/src/main/java/org/konstructs/forest/Tree.java b/src/main/java/org/konstructs/forest/Tree.java index 4139685..b358bb5 100644 --- a/src/main/java/org/konstructs/forest/Tree.java +++ b/src/main/java/org/konstructs/forest/Tree.java @@ -69,12 +69,15 @@ private void scheduleGrowth(int generation) { } } - private void seed() { - /* Plant seeds */ - int seeds = r.nextInt(config.getMaxSeedsPerGeneration() + 1); - for(int i = 0; i < seeds; i++) { - Position p = position.add(new Position(nextRandomSeedDistance(),0, nextRandomSeedDistance())); - getContext().parent().tell(new TryToSeedTree(p), getSelf()); + private void seed(int generation) { + /* Only try to seed every n:th generation */ + if(generation % config.getSeedEveryGeneration() == 0) { + /* Plant seeds */ + int seeds = r.nextInt(config.getMaxSeedsPerGeneration() + 1); + for(int i = 0; i < seeds; i++) { + Position p = position.add(new Position(nextRandomSeedDistance(),0, nextRandomSeedDistance())); + getContext().parent().tell(new TryToSeedTree(p), getSelf()); + } } } @@ -84,7 +87,7 @@ private void grow(int generation) { state = SYSTEM.iterate(state); removeOldBlocks.putAll(machine.interpret(state, position)); replaceBlocks(forestBlocks, removeOldBlocks); - seed(); + seed(generation); scheduleGrowth(generation + 1); } diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 16126a4..d9cf65f 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -2,23 +2,23 @@ konstructs { org/konstructs/forest/oak { class = "org.konstructs.forest.ForestPlugin" wood-block = org/konstructs/wood - leaves-block = org/konstructs/leaves + leaves-block = org/konstructs/forest/oak/leaves thin-leaves-block = org/konstructs/leaves sapling-block = org/konstructs/forest/oak/sapling grows-on = org/konstructs/grass-dirt + seeds-on = org/konstructs/grass/warm max-seed-height-difference = 10 - max-seed-height = 48 - min-seed-height = 0 - max-generations = 9 + max-generations = 10 min-generations = 5 - trunk-radi = 3 + trunk-radi = 2 trunk-height = 10 crown-radi = 15 crown-height = 30 initial-state = "bba[&[c][-c][--c][+c]]c" - min-growth-delay = 300 + min-growth-delay = 180 random-growth-delay = 300 max-seeds-per-generation = 1 + seed-every-generation = 4 random-growth = 2 } org/konstructs/forest/beech { @@ -28,19 +28,19 @@ konstructs { thin-leaves-block = org/konstructs/forest/beech/thin-leaves sapling-block = org/konstructs/forest/beech/sapling grows-on = org/konstructs/grass-dirt + seeds-on = org/konstructs/grass-dirt max-seed-height-difference = 4 - max-seed-height = 48 - min-seed-height = 8 - max-generations = 5 + max-generations = 6 min-generations = 3 - trunk-radi = 2 + trunk-radi = 1 trunk-height = 5 - crown-radi = 8 + crown-radi = 5 crown-height = 15 initial-state = "a[&[c][-c][--c][+c]]c" min-growth-delay = 180 random-growth-delay = 180 - max-seeds-per-generation = 2 + max-seeds-per-generation = 3 + seed-every-generation = 2 random-growth = 1 } org/konstructs/forest/birch { @@ -50,20 +50,20 @@ konstructs { thin-leaves-block = org/konstructs/forest/birch/leaves sapling-block = org/konstructs/forest/birch/sapling grows-on = org/konstructs/grass/autumn - max-seed-height-difference = 20 - max-seed-height = 128 - min-seed-height = 48 + seeds-on = org/konstructs/snow-dirt + max-seed-height-difference = 30 max-generations = 4 min-generations = 2 trunk-radi = 1 - trunk-height = 4 - crown-radi = 6 + trunk-height = 5 + crown-radi = 8 crown-height = 15 initial-state = "a[&[c][-c][--c][+c]]c" min-growth-delay = 180 random-growth-delay = 180 - max-seeds-per-generation = 0 - random-growth = 5 + max-seeds-per-generation = 1 + seed-every-generation = 3 + random-growth = 6 } org/konstructs/block-manager { blocks { @@ -71,6 +71,8 @@ konstructs { obstacle = false shape = "plant" } + org/konstructs/forest/oak/leaves { + } org/konstructs/forest/beech/sapling { obstacle = false shape = "plant" diff --git a/src/main/resources/textures/org/konstructs/forest/birch/leaves.png b/src/main/resources/textures/org/konstructs/forest/birch/leaves.png index 26a5810..b0290fd 100644 Binary files a/src/main/resources/textures/org/konstructs/forest/birch/leaves.png and b/src/main/resources/textures/org/konstructs/forest/birch/leaves.png differ diff --git a/src/main/resources/textures/org/konstructs/forest/oak/leaves.png b/src/main/resources/textures/org/konstructs/forest/oak/leaves.png new file mode 100644 index 0000000..5968228 Binary files /dev/null and b/src/main/resources/textures/org/konstructs/forest/oak/leaves.png differ