Skip to content

Commit

Permalink
Merge pull request #8 from konstructs/feature-add-new-grass-block
Browse files Browse the repository at this point in the history
Add a new grass block
  • Loading branch information
petterarvidsson committed May 16, 2016
2 parents a72ef23 + 2734fa2 commit bcf7baa
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 168 deletions.
71 changes: 0 additions & 71 deletions src/main/java/org/konstructs/flowers/CanAFlowerGrowHere.java

This file was deleted.

42 changes: 15 additions & 27 deletions src/main/java/org/konstructs/flowers/FlowersConfig.java
@@ -1,59 +1,50 @@
package org.konstructs.flowers;

import java.util.List;
import java.util.ArrayList;

import konstructs.api.BlockTypeId;

public class FlowersConfig {
private final BlockTypeId flower;
private final BlockTypeId growsOn;
private final List<BlockTypeId> growsOn;
private final int seedHeightDifference;
private final int maxSeedHeight;
private final int minSeedHeight;
private final int radi;
private final int minSeedDelay;
private final int randomSeedDelay;
private final int maxSeeds;
private final int minSeeds;
private final float seedProbability;
private final int randomGrowth;

FlowersConfig(String flower,
String growsOn,
List<String> growsOn,
int seedHeightDifference,
int maxSeedHeight,
int minSeedHeight,
int radi,
int minSeedDelay,
int randomSeedDelay,
int maxSeeds,
int minSeeds,
float seedProbability,
int randomGrowth) {
this.flower = BlockTypeId.fromString(flower);
this.growsOn = BlockTypeId.fromString(growsOn);
this.growsOn = new ArrayList<>();
for(String t: growsOn) {
this.growsOn.add(BlockTypeId.fromString(t));
}
this.seedHeightDifference = seedHeightDifference;
this.maxSeedHeight = maxSeedHeight;
this.minSeedHeight = minSeedHeight;
this.radi = radi;
this.minSeedDelay = minSeedDelay;
this.randomSeedDelay = randomSeedDelay;
this.maxSeeds = maxSeeds;
this.minSeeds = minSeeds;
this.seedProbability = seedProbability;
this.randomGrowth = randomGrowth;
}

public BlockTypeId getFlower() {
return flower;
}
public BlockTypeId getGrowsOn() {
public List<BlockTypeId> getGrowsOn() {
return growsOn;
}
public int getSeedHeightDifference() {
return seedHeightDifference;
}
public int getMaxSeedHeight() {
return maxSeedHeight;
}
public int getMinSeedHeight() {
return minSeedHeight;
}
public int getRadi() {
return radi;
}
Expand All @@ -63,11 +54,8 @@ public int getMinSeedDelay() {
public int getRandomSeedDelay() {
return randomSeedDelay;
}
public int getMinSeeds() {
return minSeeds;
}
public int getMaxSeeds() {
return maxSeeds;
public float getSeedProbability() {
return seedProbability;
}
public int getRandomGrowth() {
return randomGrowth;
Expand Down
50 changes: 30 additions & 20 deletions src/main/java/org/konstructs/flowers/FlowersPlugin.java
Expand Up @@ -2,9 +2,12 @@

import java.util.Map;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;

import akka.actor.ActorRef;
import akka.actor.Props;

import konstructs.plugin.KonstructsActor;
import konstructs.plugin.PluginConstructor;
import konstructs.plugin.Config;
Expand All @@ -13,7 +16,7 @@

public class FlowersPlugin extends KonstructsActor {
private final FlowersConfig config;
private final BlockTypeId growsOn;
private final List<BlockTypeId> growsOn;
private final BlockTypeId flower;
private final int randomGrowth;
private final Random random = new Random();
Expand Down Expand Up @@ -41,27 +44,34 @@ public FlowersPlugin(String name, ActorRef universe, FlowersConfig config) {

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));
}

void seed(Position pos) {
getContext().actorOf(CanAFlowerGrowHere.props(getUniverse(), pos, config, speed));
void seed(Position position) {
replaceVacuumBlock(position, Block.create(flower));
/* Plant seeds */
if(random.nextFloat() < config.getSeedProbability()){
Position p = position
.addX(random.nextInt(config.getRadi() + 1))
.addZ(random.nextInt(config.getRadi() + 1));
int msec = (int)((float)(config.getMinSeedDelay() * 1000 +
random.nextInt(config.getRandomSeedDelay()) * 1000) / speed);
scheduleOnce(new TryToSeed(p), msec, getSelf());
}
}

@Override
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(growsOn.contains(p.getValue())) {
Position pos = p.getKey().addY(1);
BlockTypeId above = placed.get(pos);
if(above != null && above.equals(BlockTypeId.VACUUM)) {
Expand All @@ -75,8 +85,8 @@ public void onBoxQueryResult(BoxQueryResult result) {
@Override
public void onBlockUpdateEvent(BlockUpdateEvent event) {
for(Map.Entry<Position, BlockUpdate> p: event.getUpdatedBlocks().entrySet()) {
if(p.getValue().getAfter().getType().equals(growsOn) &&
random.nextInt(1000) <= randomGrowth) {
if(growsOn.contains(p.getValue().getAfter().getType()) &&
random.nextInt(10000) <= randomGrowth) {
tryToSeed(p.getKey());
}
}
Expand All @@ -103,29 +113,29 @@ public void onReceive(Object message) {
String pluginName,
ActorRef universe,
@Config(key = "flower-block") String flower,
@Config(key = "grows-on") String growsOn,
@Config(key = "grows-on") com.typesafe.config.Config growsOn,
@Config(key = "max-seed-height-difference") int seedHeightDifference,
@Config(key = "max-seed-height") int maxSeedHeight,
@Config(key = "min-seed-height") int minSeedHeight,
@Config(key = "seed-radi") int radi,
@Config(key = "min-seed-delay") int minSeedDelay,
@Config(key = "random-seed-delay") int randomSeedDelay,
@Config(key = "max-seeds") int maxSeeds,
@Config(key = "min-seeds") int minSeeds,
@Config(key = "seed-probability") int seedProbability,
@Config(key = "random-growth") int randomGrowth
) {
Class currentClass = new Object() { }.getClass().getEnclosingClass();
List<String> growsOnTypes = new ArrayList<>();
for(String k: growsOn.root().keySet()) {
String type = growsOn.getString(k);
if(type != null)
growsOnTypes.add(type);
}
FlowersConfig config =
new FlowersConfig(flower,
growsOn,
growsOnTypes,
seedHeightDifference,
maxSeedHeight,
minSeedHeight,
radi,
minSeedDelay,
randomSeedDelay,
maxSeeds,
minSeeds,
(float)seedProbability / 100.0f,
randomGrowth);
return Props.create(currentClass, pluginName, universe, config);
}
Expand Down

0 comments on commit bcf7baa

Please sign in to comment.