From 48becd31f4e672cc1e797a4a3e348c33c8f946ff Mon Sep 17 00:00:00 2001 From: Petter Arvidsson Date: Mon, 21 Nov 2016 11:44:00 +0100 Subject: [PATCH] Add possibility to leave saplings --- build.gradle | 2 +- .../java/org/konstructs/forest/CanATreeGrowHere.java | 10 +++++++--- src/main/java/org/konstructs/forest/ForestConfig.java | 11 +++++++++-- src/main/resources/reference.conf | 3 +++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 2e0e1e5..4433b57 100644 --- a/build.gradle +++ b/build.gradle @@ -12,5 +12,5 @@ compileJava { } dependencies { - compile group: 'org.konstructs', name: 'konstructs-server-api', version: '0.1.6' + compile group: 'org.konstructs', name: 'konstructs-server-api', version: '0.1.19' } diff --git a/src/main/java/org/konstructs/forest/CanATreeGrowHere.java b/src/main/java/org/konstructs/forest/CanATreeGrowHere.java index 61d1777..0245631 100644 --- a/src/main/java/org/konstructs/forest/CanATreeGrowHere.java +++ b/src/main/java/org/konstructs/forest/CanATreeGrowHere.java @@ -1,6 +1,7 @@ package org.konstructs.forest; import java.util.Map; +import java.util.Random; import akka.actor.ActorRef; import akka.actor.Props; @@ -10,6 +11,7 @@ import konstructs.api.messages.BoxQueryResult; class CanATreeGrowHere extends KonstructsActor { + private final Random random = new Random(); private final Position position; private final ForestConfig config; private final BlockTypeId leaves; @@ -50,12 +52,14 @@ private void queryForCrown() { private void canGrow() { getContext().parent().tell(new PlantTree(position), getSelf()); - getContext().stop(getSelf()); /* We are done, let's die*/ + getContext().stop(getSelf()); /* We are done, let's die */ } private void canNotGrow() { - replaceWithVacuum(saplingFilter, position); /* Remove the sapling */ - getContext().stop(getSelf()); /* We are done, let's die*/ + if(random.nextInt(10000) > config.getLeaveSaplingProbability()) { + replaceWithVacuum(saplingFilter, position); /* Remove the sapling */ + } + getContext().stop(getSelf()); /* We are done, let's die */ } @Override diff --git a/src/main/java/org/konstructs/forest/ForestConfig.java b/src/main/java/org/konstructs/forest/ForestConfig.java index c591986..49d87a3 100644 --- a/src/main/java/org/konstructs/forest/ForestConfig.java +++ b/src/main/java/org/konstructs/forest/ForestConfig.java @@ -24,6 +24,7 @@ public class ForestConfig { private final int seedEveryGeneration; private final int randomGrowth; private final int leafDecayDelay; + private final int leaveSaplingProbability; ForestConfig(Config config) { this(config.getString("wood-block"), @@ -45,7 +46,8 @@ public class ForestConfig { config.getInt("max-seeds-per-generation"), config.getInt("seed-every-generation"), config.getInt("random-growth"), - config.getInt("leaf-decay-delay")); + config.getInt("leaf-decay-delay"), + config.getInt("leave-sapling-probability")); } ForestConfig(String wood, @@ -67,7 +69,8 @@ public class ForestConfig { int maxSeedsPerGeneration, int seedEveryGeneration, int randomGrowth, - int leafDecayDelay) { + int leafDecayDelay, + int leaveSaplingProbability) { this.wood = BlockTypeId.fromString(wood); this.leaves = BlockTypeId.fromString(leaves); this.thinLeaves = BlockTypeId.fromString(thinLeaves); @@ -88,6 +91,7 @@ public class ForestConfig { this.seedEveryGeneration = seedEveryGeneration; this.randomGrowth = randomGrowth; this.leafDecayDelay = leafDecayDelay; + this.leaveSaplingProbability = leaveSaplingProbability; } public BlockTypeId getWood() { @@ -150,4 +154,7 @@ public int getRandomGrowth() { public int getLeafDecayDelay() { return leafDecayDelay; } + public int getLeaveSaplingProbability() { + return leaveSaplingProbability; + } } diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 6e59e45..7f73396 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -21,6 +21,7 @@ konstructs { seed-every-generation = 4 random-growth = 2 leaf-decay-delay = 5000 + leave-sapling-probability = 3000 } org/konstructs/forest/beech { class = "org.konstructs.forest.ForestPlugin" @@ -44,6 +45,7 @@ konstructs { seed-every-generation = 2 random-growth = 1 leaf-decay-delay = 5000 + leave-sapling-probability = 1000 } org/konstructs/forest/birch { class = "org.konstructs.forest.ForestPlugin" @@ -67,6 +69,7 @@ konstructs { seed-every-generation = 3 random-growth = 6 leaf-decay-delay = 5000 + leave-sapling-probability = 2000 } org/konstructs/block-manager { classes {