Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from konstructs/feature-add-leaf-decay
Add support for leaf decay
- Loading branch information
Showing
5 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.konstructs.forest; | ||
|
||
import java.util.Set; | ||
import java.util.Map; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import scala.concurrent.duration.Duration; | ||
import scala.concurrent.duration.FiniteDuration; | ||
|
||
import akka.actor.ActorRef; | ||
import akka.actor.Props; | ||
import akka.actor.Cancellable; | ||
|
||
import konstructs.plugin.KonstructsActor; | ||
import konstructs.api.*; | ||
import konstructs.api.messages.*; | ||
|
||
public class LeafDecay extends KonstructsActor { | ||
private static class Trigger {} | ||
private final Random random = new Random(); | ||
private final Position radi; | ||
private final ActorRef leafRemover; | ||
private final BlockTypeId leavesId; | ||
private final BlockTypeId thinLeavesId; | ||
private final ForestConfig config; | ||
private Set<Position> leaves; | ||
private float speed = GlobalConfig.DEFAULT_SIMULATION_SPEED; | ||
private Cancellable scheduled; | ||
|
||
LeafDecay(ActorRef universe, ForestConfig config) { | ||
super(universe); | ||
this.radi = new Position(config.getCrownRadi(), config.getCrownRadi(), config.getCrownRadi()); | ||
this.leaves = new HashSet<>(); | ||
this.leafRemover = getContext().actorOf(LeafRemover.props(getUniverse(), config)); | ||
this.leavesId = config.getLeaves(); | ||
this.thinLeavesId = config.getThinLeaves(); | ||
this.config = config; | ||
this.scheduled = schedule(); | ||
} | ||
|
||
private Cancellable schedule() { | ||
FiniteDuration duration = | ||
Duration.create((long)((float)config.getLeafDecayDelay() / speed), | ||
TimeUnit.MILLISECONDS); | ||
return getContext().system().scheduler().schedule(duration, duration, getSelf(), new Trigger(), | ||
getContext().system().dispatcher(), getSelf()); | ||
} | ||
|
||
@Override | ||
public void onBoxQueryResult(BoxQueryResult result) { | ||
Map<Position, BlockTypeId> placed = result.getAsMap(); | ||
for(Map.Entry<Position, BlockTypeId> p: placed.entrySet()) { | ||
BlockTypeId type = p.getValue(); | ||
if(type.equals(leavesId) || type.equals(thinLeavesId)) | ||
leaves.add(p.getKey()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onGlobalConfig(GlobalConfig config) { | ||
speed = config.getSimulationSpeed(); | ||
scheduled.cancel(); | ||
scheduled = schedule(); | ||
} | ||
|
||
@Override | ||
public void onReceive(Object message) { | ||
if(message instanceof Trigger && leaves.size() > 0) { | ||
int current = 0; | ||
int find = random.nextInt(leaves.size()); | ||
for(Iterator<Position> i = leaves.iterator(); i.hasNext();) { | ||
Position p = i.next(); | ||
if(current == find) { /* Random leaf found, try to remove it */ | ||
getUniverse().tell(new BoxQuery(Box.createAround(p, radi)), leafRemover); | ||
i.remove(); | ||
return; | ||
} | ||
current++; | ||
} | ||
} else { | ||
super.onReceive(message); // Handle konstructs messages | ||
} | ||
} | ||
|
||
public static Props props(ActorRef universe, ForestConfig config) { | ||
return Props.create(LeafDecay.class, universe, config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.konstructs.forest; | ||
|
||
import java.util.Arrays; | ||
|
||
import akka.actor.ActorRef; | ||
import akka.actor.Props; | ||
import konstructs.plugin.KonstructsActor; | ||
import konstructs.api.*; | ||
import konstructs.api.messages.*; | ||
|
||
public class LeafRemover extends KonstructsActor { | ||
private final BlockFilter leavesFilter; | ||
private final BlockTypeId wood; | ||
|
||
LeafRemover(ActorRef universe, ForestConfig config) { | ||
super(universe); | ||
this.wood = config.getWood(); | ||
this.leavesFilter = BlockFilterFactory | ||
.withBlockTypeId(config.getLeaves()) | ||
.or(BlockFilterFactory.withBlockTypeId(config.getThinLeaves())); | ||
} | ||
|
||
private Position getCenter(Box box) { | ||
Position size = box.getSize(); | ||
return box | ||
.getFrom() | ||
.addX(size.getX() / 2) | ||
.addY(size.getY() / 2) | ||
.addZ(size.getZ() / 2); | ||
} | ||
|
||
private boolean findWood(BoxQueryResult result) { | ||
return Arrays.asList(result.getBlocks()).contains(wood); | ||
} | ||
|
||
@Override | ||
public void onBoxQueryResult(BoxQueryResult result) { | ||
if(!findWood(result)) { | ||
replaceWithVacuum(leavesFilter, getCenter(result.getBox())); | ||
} | ||
} | ||
|
||
public static Props props(ActorRef universe, ForestConfig config) { | ||
return Props.create(LeafRemover.class, universe, config); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters