Skip to content

Commit

Permalink
Fixed stray loop and added probabilistic movement
Browse files Browse the repository at this point in the history
  • Loading branch information
casals committed Jun 22, 2019
1 parent 83986dc commit f06925c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
4 changes: 3 additions & 1 deletion assets/behaviors/common/doRandomMove.behavior
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
sequence : [
set_target_nearby_block,
{
set_target_nearby_block : { moveProbability: 65 }
},
move_to
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,41 @@

@BehaviorAction(name = "set_target_nearby_block")
public class SetTargetToNearbyBlockNode extends BaseAction {
private static final int RANDOM_BLOCK_ITERATIONS = 10;
private static final Logger logger = LoggerFactory.getLogger(SetTargetToNearbyBlockNode.class);
private transient Random random = new Random();
@In
private PathfinderSystem pathfinderSystem;

private int moveProbability = 100;

@Override
public BehaviorState modify(Actor actor, BehaviorState result) {
MinionMoveComponent moveComponent = actor.getComponent(MinionMoveComponent.class);
if (moveComponent.currentBlock != null) {
WalkableBlock target = findRandomNearbyBlock(moveComponent.currentBlock);
moveComponent.target = target.getBlockPosition().toVector3f();
actor.save(moveComponent);
} else {
return BehaviorState.FAILURE;
if (random.nextInt(100) > (99 - moveProbability)) {
MinionMoveComponent moveComponent = actor.getComponent(MinionMoveComponent.class);
if (moveComponent.currentBlock != null) {
WalkableBlock target = findRandomNearbyBlock(moveComponent.currentBlock);
moveComponent.target = target.getBlockPosition().toVector3f();
actor.save(moveComponent);
} else {
return BehaviorState.FAILURE;
}
}
return BehaviorState.SUCCESS;
}

private WalkableBlock findRandomNearbyBlock(WalkableBlock startBlock) {
WalkableBlock currentBlock = startBlock;
for (int i = 0; i < RANDOM_BLOCK_ITERATIONS; i++) {
WalkableBlock[] neighbors = currentBlock.neighbors;
List<WalkableBlock> existingNeighbors = Lists.newArrayList();
for (WalkableBlock neighbor : neighbors) {
if (neighbor != null) {
existingNeighbors.add(neighbor);
}
}
if (existingNeighbors.size() > 0) {
currentBlock = existingNeighbors.get(random.nextInt(existingNeighbors.size()));

WalkableBlock[] neighbors = currentBlock.neighbors;
List<WalkableBlock> existingNeighbors = Lists.newArrayList();
for (WalkableBlock neighbor : neighbors) {
if (neighbor != null) {
existingNeighbors.add(neighbor);
}
}
if (existingNeighbors.size() > 0) {
currentBlock = existingNeighbors.get(random.nextInt(existingNeighbors.size()));
}

logger.debug(String.format("Looking for a block: my block is %s, found destination %s", startBlock.getBlockPosition(), currentBlock.getBlockPosition()));
return currentBlock;
Expand Down

0 comments on commit f06925c

Please sign in to comment.