Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed neighbor finding loop and added probabilistic movement #16

Merged
merged 4 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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