Skip to content

Commit 23d66e1

Browse files
Merge pull request #11 from konstructs/feature-tweak-forest-growth-to-grass
Tweak forest to work better with grass
2 parents 393a954 + 702e9b8 commit 23d66e1

6 files changed

Lines changed: 54 additions & 51 deletions

File tree

src/main/java/org/konstructs/forest/ForestConfig.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public class ForestConfig {
88
private final BlockTypeId thinLeaves;
99
private final BlockTypeId sapling;
1010
private final BlockTypeId growsOn;
11+
private final BlockTypeId seedsOn;
1112
private final int seedHeightDifference;
12-
private final int maxSeedHeight;
13-
private final int minSeedHeight;
1413
private final int maxGenerations;
1514
private final int minGenerations;
1615
private final int trunkRadi;
@@ -21,16 +20,16 @@ public class ForestConfig {
2120
private final int minGrowthDelay;
2221
private final int randomGrowthDelay;
2322
private final int maxSeedsPerGeneration;
23+
private final int seedEveryGeneration;
2424
private final int randomGrowth;
2525

2626
ForestConfig(String wood,
2727
String leaves,
2828
String thinLeaves,
2929
String sapling,
3030
String growsOn,
31+
String seedsOn,
3132
int seedHeightDifference,
32-
int maxSeedHeight,
33-
int minSeedHeight,
3433
int maxGenerations,
3534
int minGenerations,
3635
int trunkRadi,
@@ -41,15 +40,15 @@ public class ForestConfig {
4140
int minGrowthDelay,
4241
int randomGrowthDelay,
4342
int maxSeedsPerGeneration,
43+
int seedEveryGeneration,
4444
int randomGrowth) {
4545
this.wood = BlockTypeId.fromString(wood);
4646
this.leaves = BlockTypeId.fromString(leaves);
4747
this.thinLeaves = BlockTypeId.fromString(thinLeaves);
4848
this.sapling = BlockTypeId.fromString(sapling);
4949
this.growsOn = BlockTypeId.fromString(growsOn);
50+
this.seedsOn = BlockTypeId.fromString(seedsOn);
5051
this.seedHeightDifference = seedHeightDifference;
51-
this.maxSeedHeight = maxSeedHeight;
52-
this.minSeedHeight = minSeedHeight;
5352
this.maxGenerations = maxGenerations;
5453
this.minGenerations = minGenerations;
5554
this.trunkRadi = trunkRadi;
@@ -60,6 +59,7 @@ public class ForestConfig {
6059
this.minGrowthDelay = minGrowthDelay;
6160
this.randomGrowthDelay = randomGrowthDelay;
6261
this.maxSeedsPerGeneration = maxSeedsPerGeneration;
62+
this.seedEveryGeneration = seedEveryGeneration;
6363
this.randomGrowth = randomGrowth;
6464
}
6565

@@ -78,15 +78,12 @@ public BlockTypeId getSapling() {
7878
public BlockTypeId getGrowsOn() {
7979
return growsOn;
8080
}
81+
public BlockTypeId getSeedsOn() {
82+
return seedsOn;
83+
}
8184
public int getSeedHeightDifference() {
8285
return seedHeightDifference;
8386
}
84-
public int getMaxSeedHeight() {
85-
return maxSeedHeight;
86-
}
87-
public int getMinSeedHeight() {
88-
return minSeedHeight;
89-
}
9087
public int getMaxGenerations() {
9188
return maxGenerations;
9289
}
@@ -117,6 +114,9 @@ public int getRandomGrowthDelay() {
117114
public int getMaxSeedsPerGeneration() {
118115
return maxSeedsPerGeneration;
119116
}
117+
public int getSeedEveryGeneration() {
118+
return seedEveryGeneration;
119+
}
120120
public int getRandomGrowth() {
121121
return randomGrowth;
122122
}

src/main/java/org/konstructs/forest/ForestPlugin.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
public class ForestPlugin extends KonstructsActor {
1515
private final ForestConfig config;
1616
private final BlockTypeId growsOn;
17+
private final BlockTypeId seedsOn;
1718
private final BlockTypeId sapling;
1819
private final int randomGrowth;
1920
private final Random random = new Random();
@@ -23,22 +24,19 @@ public ForestPlugin(String name, ActorRef universe, ForestConfig config) {
2324
super(universe);
2425
this.config = config;
2526
this.growsOn = config.getGrowsOn();
27+
this.seedsOn = config.getSeedsOn();
2628
this.sapling = config.getSapling();
2729
this.randomGrowth = config.getRandomGrowth();
2830
}
2931

3032
void tryToSeed(Position pos) {
3133
Position start =
32-
pos.withY(Math.max(pos.getY() - config.getSeedHeightDifference(),
33-
config.getMinSeedHeight()));
34+
pos.withY(pos.getY() - config.getSeedHeightDifference());
3435
Position end =
3536
new Position(pos.getX() + 1,
36-
Math.min(pos.getY() + config.getSeedHeightDifference(),
37-
config.getMaxSeedHeight()),
37+
pos.getY() + config.getSeedHeightDifference(),
3838
pos.getZ() + 1);
39-
// Only run query if within the possible height band
40-
if(start.getY() < end.getY())
41-
boxQuery(new Box(start, end));
39+
boxQuery(new Box(start, end));
4240
}
4341

4442
void seeded(Position pos) {
@@ -58,7 +56,7 @@ void plant(Position pos) {
5856
public void onBoxQueryResult(BoxQueryResult result) {
5957
Map<Position, BlockTypeId> placed = result.getAsMap();
6058
for(Map.Entry<Position, BlockTypeId> p: placed.entrySet()) {
61-
if(p.getValue().equals(growsOn)) {
59+
if(p.getValue().equals(growsOn) || p.getValue().equals(seedsOn)) {
6260
Position pos = p.getKey().addY(1);
6361
BlockTypeId above = placed.get(pos);
6462
if(above != null && above.equals(BlockTypeId.VACUUM)) {
@@ -76,7 +74,7 @@ public void onBlockUpdateEvent(BlockUpdateEvent update) {
7674
if(after.equals(sapling)) {
7775
seeded(p.getKey());
7876
} else if(after.equals(growsOn) &&
79-
random.nextInt(1000) <= randomGrowth) {
77+
random.nextInt(10000) <= randomGrowth) {
8078
/* Try to seed a new tree */
8179
scheduleSelfOnce(new TryToSeedTree(p.getKey()),
8280
(int)((float)(config.getMinGrowthDelay() * 1000 +
@@ -116,9 +114,8 @@ public void onReceive(Object message) {
116114
@Config(key = "thin-leaves-block") String thinLeaves,
117115
@Config(key = "sapling-block") String sapling,
118116
@Config(key = "grows-on") String growsOn,
117+
@Config(key = "seeds-on") String seedsOn,
119118
@Config(key = "max-seed-height-difference") int seedHeightDifference,
120-
@Config(key = "max-seed-height") int maxSeedHeight,
121-
@Config(key = "min-seed-height") int minSeedHeight,
122119
@Config(key = "max-generations") int maxGenerations,
123120
@Config(key = "min-generations") int minGenerations,
124121
@Config(key = "trunk-radi") int trunkRadi,
@@ -129,6 +126,7 @@ public void onReceive(Object message) {
129126
@Config(key = "min-growth-delay") int minGrowthDelay,
130127
@Config(key = "random-growth-delay") int randomGrowthDelay,
131128
@Config(key = "max-seeds-per-generation") int maxSeedsPerGeneration,
129+
@Config(key = "seed-every-generation") int seedEveryGeneration,
132130
@Config(key = "random-growth") int randomGrowth
133131
) {
134132
Class currentClass = new Object() { }.getClass().getEnclosingClass();
@@ -139,9 +137,8 @@ public void onReceive(Object message) {
139137
thinLeaves,
140138
sapling,
141139
growsOn,
140+
seedsOn,
142141
seedHeightDifference,
143-
maxSeedHeight,
144-
minSeedHeight,
145142
maxGenerations,
146143
minGenerations,
147144
trunkRadi,
@@ -152,6 +149,7 @@ public void onReceive(Object message) {
152149
minGrowthDelay,
153150
randomGrowthDelay,
154151
maxSeedsPerGeneration,
152+
seedEveryGeneration,
155153
randomGrowth);
156154
return Props.create(currentClass, pluginName, universe, config);
157155
}

src/main/java/org/konstructs/forest/Tree.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ private void scheduleGrowth(int generation) {
6969
}
7070
}
7171

72-
private void seed() {
73-
/* Plant seeds */
74-
int seeds = r.nextInt(config.getMaxSeedsPerGeneration() + 1);
75-
for(int i = 0; i < seeds; i++) {
76-
Position p = position.add(new Position(nextRandomSeedDistance(),0, nextRandomSeedDistance()));
77-
getContext().parent().tell(new TryToSeedTree(p), getSelf());
72+
private void seed(int generation) {
73+
/* Only try to seed every n:th generation */
74+
if(generation % config.getSeedEveryGeneration() == 0) {
75+
/* Plant seeds */
76+
int seeds = r.nextInt(config.getMaxSeedsPerGeneration() + 1);
77+
for(int i = 0; i < seeds; i++) {
78+
Position p = position.add(new Position(nextRandomSeedDistance(),0, nextRandomSeedDistance()));
79+
getContext().parent().tell(new TryToSeedTree(p), getSelf());
80+
}
7881
}
7982
}
8083

@@ -84,7 +87,7 @@ private void grow(int generation) {
8487
state = SYSTEM.iterate(state);
8588
removeOldBlocks.putAll(machine.interpret(state, position));
8689
replaceBlocks(forestBlocks, removeOldBlocks);
87-
seed();
90+
seed(generation);
8891
scheduleGrowth(generation + 1);
8992
}
9093

src/main/resources/reference.conf

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ konstructs {
22
org/konstructs/forest/oak {
33
class = "org.konstructs.forest.ForestPlugin"
44
wood-block = org/konstructs/wood
5-
leaves-block = org/konstructs/leaves
5+
leaves-block = org/konstructs/forest/oak/leaves
66
thin-leaves-block = org/konstructs/leaves
77
sapling-block = org/konstructs/forest/oak/sapling
88
grows-on = org/konstructs/grass-dirt
9+
seeds-on = org/konstructs/grass/warm
910
max-seed-height-difference = 10
10-
max-seed-height = 48
11-
min-seed-height = 0
12-
max-generations = 9
11+
max-generations = 10
1312
min-generations = 5
14-
trunk-radi = 3
13+
trunk-radi = 2
1514
trunk-height = 10
1615
crown-radi = 15
1716
crown-height = 30
1817
initial-state = "bba[&[c][-c][--c][+c]]c"
19-
min-growth-delay = 300
18+
min-growth-delay = 180
2019
random-growth-delay = 300
2120
max-seeds-per-generation = 1
21+
seed-every-generation = 4
2222
random-growth = 2
2323
}
2424
org/konstructs/forest/beech {
@@ -28,19 +28,19 @@ konstructs {
2828
thin-leaves-block = org/konstructs/forest/beech/thin-leaves
2929
sapling-block = org/konstructs/forest/beech/sapling
3030
grows-on = org/konstructs/grass-dirt
31+
seeds-on = org/konstructs/grass-dirt
3132
max-seed-height-difference = 4
32-
max-seed-height = 48
33-
min-seed-height = 8
34-
max-generations = 5
33+
max-generations = 6
3534
min-generations = 3
36-
trunk-radi = 2
35+
trunk-radi = 1
3736
trunk-height = 5
38-
crown-radi = 8
37+
crown-radi = 5
3938
crown-height = 15
4039
initial-state = "a[&[c][-c][--c][+c]]c"
4140
min-growth-delay = 180
4241
random-growth-delay = 180
43-
max-seeds-per-generation = 2
42+
max-seeds-per-generation = 3
43+
seed-every-generation = 2
4444
random-growth = 1
4545
}
4646
org/konstructs/forest/birch {
@@ -50,27 +50,29 @@ konstructs {
5050
thin-leaves-block = org/konstructs/forest/birch/leaves
5151
sapling-block = org/konstructs/forest/birch/sapling
5252
grows-on = org/konstructs/grass/autumn
53-
max-seed-height-difference = 20
54-
max-seed-height = 128
55-
min-seed-height = 48
53+
seeds-on = org/konstructs/snow-dirt
54+
max-seed-height-difference = 30
5655
max-generations = 4
5756
min-generations = 2
5857
trunk-radi = 1
59-
trunk-height = 4
60-
crown-radi = 6
58+
trunk-height = 5
59+
crown-radi = 8
6160
crown-height = 15
6261
initial-state = "a[&[c][-c][--c][+c]]c"
6362
min-growth-delay = 180
6463
random-growth-delay = 180
65-
max-seeds-per-generation = 0
66-
random-growth = 5
64+
max-seeds-per-generation = 1
65+
seed-every-generation = 3
66+
random-growth = 6
6767
}
6868
org/konstructs/block-manager {
6969
blocks {
7070
org/konstructs/forest/oak/sapling {
7171
obstacle = false
7272
shape = "plant"
7373
}
74+
org/konstructs/forest/oak/leaves {
75+
}
7476
org/konstructs/forest/beech/sapling {
7577
obstacle = false
7678
shape = "plant"
-21 Bytes
Loading
3.47 KB
Loading

0 commit comments

Comments
 (0)