Skip to content

Commit

Permalink
Merge PR #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Cervator committed Aug 11, 2019
2 parents a543403 + f373898 commit a44eeac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 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
7 changes: 4 additions & 3 deletions src/main/java/org/terasology/minion/move/MoveToAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ private boolean processDirect(Actor actor, MinionMoveComponent moveComponent) {
targetDirection.sub(moveComponent.target, worldPos);
Vector3f drive = new Vector3f();

// TODO review - is the yaw here being calculated properly?
float yaw = (float) Math.atan2(targetDirection.x, targetDirection.z);
float requestedYaw = 180f + yaw * TeraMath.RAD_TO_DEG;


if (targetDirection.x * targetDirection.x + targetDirection.z * targetDirection.z <= distance * distance) {
if((targetDirection.x < distance) && (targetDirection.y < distance) && (targetDirection.z < distance)) {
drive.set(0, 0, 0);
reachedTarget = true;
requestedYaw = 0f;
} else {
targetDirection.normalize();
drive.set(targetDirection);
}
float requestedYaw = 180f + yaw * TeraMath.RAD_TO_DEG;

CharacterMoveInputEvent wantedInput = new CharacterMoveInputEvent(0, 0, requestedYaw, drive, false, false, moveComponent.jumpMode, (long) (actor.getDelta() * 1000));
actor.getEntity().send(wantedInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,31 @@

@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++) {
for (int i = 0; i < random.nextInt(10) + 1; i++) {
WalkableBlock[] neighbors = currentBlock.neighbors;
List<WalkableBlock> existingNeighbors = Lists.newArrayList();
for (WalkableBlock neighbor : neighbors) {
Expand All @@ -66,7 +68,6 @@ private WalkableBlock findRandomNearbyBlock(WalkableBlock startBlock) {
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 a44eeac

Please sign in to comment.