Skip to content
Permalink
Browse files

Merge pull request #8 from konstructs/feature-add-new-grass-block

Add a new grass block
  • Loading branch information
petterarvidsson committed May 16, 2016
2 parents a72ef23 + 2734fa2 commit bcf7baaa307d5a86957077e8d2ca3b06aca46df1

This file was deleted.

@@ -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;
}
@@ -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;
@@ -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;
@@ -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();
@@ -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)) {
@@ -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());
}
}
@@ -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);
}

0 comments on commit bcf7baa

Please sign in to comment.
You can’t perform that action at this time.