Skip to content

Commit

Permalink
Merge pull request #11 from konstructs/feature-tweak-forest-growth-to…
Browse files Browse the repository at this point in the history
…-grass

Tweak forest to work better with grass
  • Loading branch information
petterarvidsson committed May 16, 2016
2 parents 393a954 + 702e9b8 commit 23d66e1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/konstructs/forest/ForestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,16 +20,16 @@ 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,
String leaves,
String thinLeaves,
String sapling,
String growsOn,
String seedsOn,
int seedHeightDifference,
int maxSeedHeight,
int minSeedHeight,
int maxGenerations,
int minGenerations,
int trunkRadi,
Expand All @@ -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;
Expand All @@ -60,6 +59,7 @@ public class ForestConfig {
this.minGrowthDelay = minGrowthDelay;
this.randomGrowthDelay = randomGrowthDelay;
this.maxSeedsPerGeneration = maxSeedsPerGeneration;
this.seedEveryGeneration = seedEveryGeneration;
this.randomGrowth = randomGrowth;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -117,6 +114,9 @@ public int getRandomGrowthDelay() {
public int getMaxSeedsPerGeneration() {
return maxSeedsPerGeneration;
}
public int getSeedEveryGeneration() {
return seedEveryGeneration;
}
public int getRandomGrowth() {
return randomGrowth;
}
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/konstructs/forest/ForestPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -58,7 +56,7 @@ void plant(Position pos) {
public void onBoxQueryResult(BoxQueryResult result) {
Map<Position, BlockTypeId> placed = result.getAsMap();
for(Map.Entry<Position, BlockTypeId> 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)) {
Expand All @@ -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 +
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -139,9 +137,8 @@ public void onReceive(Object message) {
thinLeaves,
sapling,
growsOn,
seedsOn,
seedHeightDifference,
maxSeedHeight,
minSeedHeight,
maxGenerations,
minGenerations,
trunkRadi,
Expand All @@ -152,6 +149,7 @@ public void onReceive(Object message) {
minGrowthDelay,
randomGrowthDelay,
maxSeedsPerGeneration,
seedEveryGeneration,
randomGrowth);
return Props.create(currentClass, pluginName, universe, config);
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/konstructs/forest/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand All @@ -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);
}

Expand Down
40 changes: 21 additions & 19 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -50,27 +50,29 @@ 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 {
org/konstructs/forest/oak/sapling {
obstacle = false
shape = "plant"
}
org/konstructs/forest/oak/leaves {
}
org/konstructs/forest/beech/sapling {
obstacle = false
shape = "plant"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 23d66e1

Please sign in to comment.