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

feat(JOML): migrate logic.ai.* #4382

Merged
merged 3 commits into from
Jan 9, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.terasology.logic.ai;

import org.terasology.entitySystem.Component;
import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;

/**
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.terasology.logic.ai;

import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.terasology.engine.Time;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
Expand All @@ -28,8 +30,6 @@
import org.terasology.logic.characters.events.HorizontalCollisionEvent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3f;
import org.terasology.registry.In;
import org.terasology.utilities.random.FastRandom;
import org.terasology.utilities.random.Random;
Expand All @@ -40,8 +40,7 @@
*
*/
@RegisterSystem(RegisterMode.AUTHORITY)
public class HierarchicalAISystem extends BaseComponentSystem implements
UpdateSubscriberSystem {
public class HierarchicalAISystem extends BaseComponentSystem implements UpdateSubscriberSystem {

@In
private WorldProvider worldProvider;
Expand All @@ -62,20 +61,19 @@ public class HierarchicalAISystem extends BaseComponentSystem implements

@Override
public void update(float delta) {
Vector3f tmp = new Vector3f();
for (EntityRef entity : entityManager.getEntitiesWith(
HierarchicalAIComponent.class, CharacterMovementComponent.class,
LocationComponent.class)) {
LocationComponent location = entity
.getComponent(LocationComponent.class);
Vector3f worldPos = location.getWorldPosition();
HierarchicalAIComponent.class, CharacterMovementComponent.class, LocationComponent.class)) {
LocationComponent location = entity.getComponent(LocationComponent.class);
location.getWorldPosition(tmp);

// Skip this AI if not in a loaded chunk
if (!worldProvider.isBlockRelevant(worldPos)) {
if (!worldProvider.isBlockRelevant(tmp)) {
continue;
}

// goto Hierarchical system
loop(entity, location, worldPos);
loop(entity, location, tmp);
}
}

Expand All @@ -86,10 +84,8 @@ public void update(float delta) {
* @param location
* @param worldPos
*/
private void loop(EntityRef entity, LocationComponent location,
Vector3f worldPos) {
HierarchicalAIComponent ai = entity
.getComponent(HierarchicalAIComponent.class);
private void loop(EntityRef entity, LocationComponent location, Vector3fc worldPos) {
HierarchicalAIComponent ai = entity.getComponent(HierarchicalAIComponent.class);
long tempTime = time.getGameTimeInMs();
//TODO remove next
long lastAttack = 0;
Expand All @@ -111,11 +107,9 @@ private void loop(EntityRef entity, LocationComponent location,

// find player position
// TODO: shouldn't use local player, need some way to find nearest
// player
if (localPlayer != null) {
Vector3f dist = new Vector3f(worldPos);
dist.sub(localPlayer.getPosition());
double distanceToPlayer = dist.lengthSquared();
final Vector3f playerPosition = localPlayer.getPosition(new Vector3f());
double distanceToPlayer = worldPos.distanceSquared(playerPosition);

ai.inDanger = false;
if (ai.dieIfPlayerFar && distanceToPlayer > ai.dieDistance) {
Expand All @@ -126,19 +120,15 @@ private void loop(EntityRef entity, LocationComponent location,
if (tempTime - ai.lastChangeOfDangerAt > dangerChangeTime) {
dangerChangeTime = (long) (ai.dangerUpdateTime * random.nextDouble() * ai.hectic);
if (ai.hunter) {
if (distanceToPlayer > ai.playerdistance
&& distanceToPlayer < ai.playerSense) {
if (distanceToPlayer > ai.playerdistance && distanceToPlayer < ai.playerSense) {
// Head to player
Vector3f tempTarget = localPlayer.getPosition();
if (ai.forgiving != 0) {
ai.movementTarget.set(new Vector3f(
tempTarget.x + random.nextFloat(-ai.forgiving, ai.forgiving),
tempTarget.y + random.nextFloat(-ai.forgiving, ai.forgiving),
tempTarget.z + random.nextFloat(-ai.forgiving, ai.forgiving)
));
} else {
ai.movementTarget.set(tempTarget);
playerPosition.add(
random.nextFloat(-ai.forgiving, ai.forgiving),
random.nextFloat(-ai.forgiving, ai.forgiving),
random.nextFloat(-ai.forgiving, ai.forgiving));
}
ai.movementTarget.set(playerPosition);
ai.inDanger = true;
entity.saveComponent(ai);

Expand Down Expand Up @@ -205,22 +195,19 @@ private void loop(EntityRef entity, LocationComponent location,
}
}

Vector3f targetDirection = new Vector3f();
targetDirection.sub(ai.movementTarget, worldPos);
Vector3f targetDirection = ai.movementTarget.sub(worldPos, new Vector3f());
targetDirection.normalize();
drive.set(targetDirection);

float yaw = (float) Math.atan2(targetDirection.x, targetDirection.z);
entity.send(new CharacterMoveInputEvent(0, 0, yaw, JomlUtil.from(drive), false, false, time.getGameDeltaInMs()));
entity.send(new CharacterMoveInputEvent(0, 0, yaw, drive, false, false, false, time.getGameDeltaInMs()));
entity.saveComponent(location);
// System.out.print("\Destination set: " + targetDirection.x + ":" +targetDirection.z + "\n");
// System.out.print("\nI am: " + worldPos.x + ":" + worldPos.z + "\n");

ai.lastProgressedUpdateAt = time.getGameTimeInMs();
}

private void runAway(EntityRef entity, HierarchicalAIComponent ai) {
Vector3f tempTarget = localPlayer.getPosition();
Vector3f tempTarget = localPlayer.getPosition(new Vector3f());
if (ai.forgiving != 0) {
ai.movementTarget.set(new Vector3f(
-tempTarget.x + random.nextFloat(-ai.forgiving, ai.forgiving),
Expand All @@ -237,22 +224,22 @@ private void runAway(EntityRef entity, HierarchicalAIComponent ai) {
ai.inDanger = true;
}

private void randomWalk(Vector3f worldPos, HierarchicalAIComponent ai) {
private void randomWalk(Vector3fc worldPos, HierarchicalAIComponent ai) {
// if ai flies
if (ai.flying) {
float targetY = 0;
do {
targetY = worldPos.y + random.nextFloat(-100.0f, 100.0f);
targetY = worldPos.y() + random.nextFloat(-100.0f, 100.0f);
} while (targetY > ai.maxAltitude);
ai.movementTarget.set(
worldPos.x + random.nextFloat(-500.0f, 500.0f),
worldPos.x() + random.nextFloat(-500.0f, 500.0f),
targetY,
worldPos.z + random.nextFloat(-500.0f, 500.0f));
worldPos.z() + random.nextFloat(-500.0f, 500.0f));
} else {
ai.movementTarget.set(
worldPos.x + random.nextFloat(-500.0f, 500.0f),
worldPos.y,
worldPos.z + random.nextFloat(-500.0f, 500.0f));
worldPos.x() + random.nextFloat(-500.0f, 500.0f),
worldPos.y(),
worldPos.z() + random.nextFloat(-500.0f, 500.0f));
}
ai.lastChangeOfDirectionAt = time.getGameTimeInMs();
}
Expand All @@ -264,9 +251,7 @@ private boolean foodInFront() {

//TODO change eating thingy to use this
@ReceiveEvent(components = {HierarchicalAIComponent.class})
public void onBump(HorizontalCollisionEvent event, EntityRef entity) {
CharacterMovementComponent moveComp = entity
.getComponent(CharacterMovementComponent.class);
public void onBump(HorizontalCollisionEvent event, EntityRef entity, CharacterMovementComponent moveComp) {
if (moveComp != null && moveComp.grounded) {
moveComp.jump = true;
entity.saveComponent(moveComp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.terasology.logic.ai;

import org.terasology.entitySystem.Component;
import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;

/**
*/
Expand Down
16 changes: 7 additions & 9 deletions engine/src/main/java/org/terasology/logic/ai/SimpleAISystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.terasology.logic.ai;

import org.joml.Vector3f;
import org.terasology.engine.Time;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
Expand All @@ -28,8 +29,6 @@
import org.terasology.logic.characters.events.HorizontalCollisionEvent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3f;
import org.terasology.registry.In;
import org.terasology.utilities.random.FastRandom;
import org.terasology.utilities.random.Random;
Expand All @@ -54,7 +53,7 @@ public class SimpleAISystem extends BaseComponentSystem implements UpdateSubscri
public void update(float delta) {
for (EntityRef entity : entityManager.getEntitiesWith(SimpleAIComponent.class, CharacterMovementComponent.class, LocationComponent.class)) {
LocationComponent location = entity.getComponent(LocationComponent.class);
Vector3f worldPos = location.getWorldPosition();
Vector3f worldPos = location.getWorldPosition(new Vector3f());

// Skip this AI if not in a loaded chunk
if (!worldProvider.isBlockRelevant(worldPos)) {
Expand All @@ -65,13 +64,12 @@ public void update(float delta) {
Vector3f drive = new Vector3f();
// TODO: shouldn't use local player, need some way to find nearest player
if (localPlayer != null) {
Vector3f dist = new Vector3f(worldPos);
dist.sub(localPlayer.getPosition());
double distanceToPlayer = dist.lengthSquared();
final Vector3f playerPosition = localPlayer.getPosition(new Vector3f());
double distanceToPlayer = worldPos.distanceSquared(playerPosition);

if (distanceToPlayer > 6 && distanceToPlayer < 16) {
// Head to player
ai.movementTarget.set(localPlayer.getPosition());
ai.movementTarget.set(playerPosition);
ai.followingPlayer = true;
entity.saveComponent(ai);
} else {
Expand All @@ -90,10 +88,10 @@ public void update(float delta) {
drive.set(targetDirection);

float yaw = (float) Math.atan2(targetDirection.x, targetDirection.z);
location.getLocalRotation().set(new Vector3f(0, 1, 0), yaw);
location.getLocalRotation().set(0, 1, 0, yaw);
entity.saveComponent(location);
}
entity.send(new CharacterMoveInputEvent(0, 0, 0, JomlUtil.from(drive), false, false, time.getGameDeltaInMs()));
entity.send(new CharacterMoveInputEvent(0, 0, 0, drive, false, false, false, time.getGameDeltaInMs()));
}
}

Expand Down