Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/main/java/io/luna/game/event/impl/WalkingEvent.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.luna.game.event.impl;

import io.luna.game.model.mob.Player;
import io.luna.game.model.mob.WalkingQueue.Step;
import io.luna.math.Vector2;

import java.util.Deque;

Expand All @@ -15,7 +15,7 @@ public final class WalkingEvent extends PlayerEvent implements ControllableEvent
/**
* The walking path.
*/
private final Deque<Step> path;
private final Deque<Vector2> path;

/**
* If the player is running.
Expand All @@ -41,7 +41,7 @@ public final class WalkingEvent extends PlayerEvent implements ControllableEvent
* @param pathSize The path size.
* @param opcode The opcode.
*/
public WalkingEvent(Player player, Deque<Step> path, boolean running, int pathSize, int opcode) {
public WalkingEvent(Player player, Deque<Vector2> path, boolean running, int pathSize, int opcode) {
super(player);
this.path = path;
this.running = running;
Expand All @@ -52,7 +52,7 @@ public WalkingEvent(Player player, Deque<Step> path, boolean running, int pathSi
/**
* @return The walking path.
*/
public Deque<Step> getPath() {
public Deque<Vector2> getPath() {
return path;
}

Expand Down
37 changes: 21 additions & 16 deletions src/main/java/io/luna/game/model/Direction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.luna.game.model;

import com.google.common.collect.ImmutableList;
import io.luna.game.model.mob.WalkingQueue.Step;
import io.luna.math.Vector2;

import java.util.Set;

Expand All @@ -14,15 +14,15 @@
* @author Graham
*/
public enum Direction {
NONE(-1, new Step(0, 0)),
NORTH_WEST(0, new Step(-1, 1)),
NORTH(1, new Step(0, 1)),
NORTH_EAST(2, new Step(1, 1)),
WEST(3, new Step(-1, 0)),
EAST(4, new Step(1, 0)),
SOUTH_WEST(5, new Step(-1, -1)),
SOUTH(6, new Step(0, -1)),
SOUTH_EAST(7, new Step(1, -1));
NONE(-1, new Vector2(0, 0)),
NORTH_WEST(0, new Vector2(-1, 1)),
NORTH(1, new Vector2(0, 1)),
NORTH_EAST(2, new Vector2(1, 1)),
WEST(3, new Vector2(-1, 0)),
EAST(4, new Vector2(1, 0)),
SOUTH_WEST(5, new Vector2(-1, -1)),
SOUTH(6, new Vector2(0, -1)),
SOUTH_EAST(7, new Vector2(1, -1));
// todo cleanup, documentation
/**
* A list of directions representing all possible directions of the NPC view cone, in order.
Expand Down Expand Up @@ -58,19 +58,19 @@ public enum Direction {
* The direction identifier.
*/
private final int id;
private final Step translate;
private final Vector2 translate;

/**
* Creates a new {@link Direction}.
*
* @param id The direction identifier.
*/
Direction(int id, Step translate) {
Direction(int id, Vector2 translate) {
this.id = id;
this.translate = translate;
}

public Step getTranslation() {
public Vector2 getTranslation() {
return translate;
}

Expand Down Expand Up @@ -141,11 +141,13 @@ public Direction opposite() {
*
* @param currentX The current x coordinate.
* @param currentY The current y coordinate.
* @param nextX The next x coordinate.
* @param nextY The next y coordinate.
* @param nextX The next x coordinate.
* @param nextY The next y coordinate.
* @return The direction between the current and next coordinates.
* @deprecated Use {@link Vector2#directionTowards(Vector2)}.
*/
@SuppressWarnings("Duplicates")
@Deprecated
public static Direction between(int currentX, int currentY, int nextX, int nextY) {
int deltaX = Integer.signum(nextX - currentX);
int deltaY = Integer.signum(nextY - currentY);
Expand Down Expand Up @@ -184,8 +186,11 @@ public static Direction between(int currentX, int currentY, int nextX, int nextY
* @param current The current step.
* @param next The next step.
* @return The direction between the current and next steps.
*
* @deprecated Use {@link Vector2#directionTowards(Vector2)}.
*/
public static Direction between(Step current, Step next) {
@Deprecated
public static Direction between(Vector2 current, Vector2 next) {
return between(current.getX(), current.getY(), next.getX(), next.getY());
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/luna/game/model/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.Range;
import io.luna.game.model.chunk.Chunk;
import io.luna.game.model.mob.WalkingQueue.Step;
import io.luna.math.Vector2;

import java.util.Comparator;
import java.util.Objects;
Expand Down Expand Up @@ -222,7 +222,7 @@ public Position translate(int amountX, int amountY) {
* @return The translated position.
*/
public Position translate(int amount, Direction direction) {
Step translation = direction.getTranslation();
Vector2 translation = direction.getTranslation();
return translate(amount * translation.getX(), amount * translation.getY(), 0);
}

Expand Down
115 changes: 20 additions & 95 deletions src/main/java/io/luna/game/model/mob/WalkingQueue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.luna.game.model.mob;

import com.google.common.base.MoreObjects;
import com.google.common.util.concurrent.ListenableFuture;
import io.luna.game.model.Direction;
import io.luna.game.model.Entity;
Expand All @@ -12,6 +11,7 @@
import io.luna.game.model.path.EuclideanHeuristic;
import io.luna.game.model.path.PathfindingAlgorithm;
import io.luna.game.model.path.SimplePathfindingAlgorithm;
import io.luna.math.Vector2;
import io.luna.util.RandomUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -31,90 +31,15 @@ public final class WalkingQueue {
*/
private static final Logger logger = LogManager.getLogger();

/**
* A model representing a step in the walking queue.
*/
public static final class Step {

/**
* The x coordinate.
*/
private final int x;

/**
* The y coordinate.
*/
private final int y;

/**
* Creates a new {@link Step}.
*
* @param x The x coordinate.
* @param y The y coordinate.
*/
public Step(int x, int y) {
this.x = x;
this.y = y;
}

/**
* Creates a new {@link Step}.
*
* @param position The position.
*/
public Step(Position position) {
this(position.getX(), position.getY());
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Step) {
Step other = (Step) obj;
return x == other.x && y == other.y;
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("x", x)
.add("y", y)
.toString();
}

/**
* @return The x coordinate.
*/
public int getX() {
return x;
}

/**
* @return The y coordinate.
*/
public int getY() {
return y;
}
}

/**
* A deque of current steps.
*/
private final Deque<Step> current = new ArrayDeque<>();
private final Deque<Vector2> current = new ArrayDeque<>();

/**
* A deque of previous steps.
*/
private final Deque<Step> previous = new ArrayDeque<>();
private final Deque<Vector2> previous = new ArrayDeque<>();

/**
* The collision manager.
Expand Down Expand Up @@ -182,13 +107,13 @@ public void process() {
if(mob instanceof Npc && mob.asNpc().isStationary()) {
return;
}
Step currentStep = new Step(mob.getPosition());
Vector2 currentStep = new Vector2(mob.getPosition());

Direction walkingDirection = Direction.NONE;
Direction runningDirection = Direction.NONE;

boolean restoreEnergy = true;
Step nextStep = current.poll();
Vector2 nextStep = current.poll();
if (nextStep != null) {
walkingDirection = Direction.between(currentStep, nextStep);
boolean blocked = false;// !collisionManager.traversable(mob.getPosition(), EntityType.NPC, walkingDirection);
Expand Down Expand Up @@ -254,7 +179,7 @@ public void walk(Position destination) {
* @param target The destination target.
*/
public void walkUntilReached(Entity target) {
Deque<Step> newPath = new ArrayDeque<>();
Deque<Vector2> newPath = new ArrayDeque<>();
Deque<Position> path = pathfindingAlgorithm.find(mob.getPosition(), target.getPosition());
Position lastPosition = mob.getPosition();
for (; ; ) {
Expand All @@ -264,7 +189,7 @@ public void walkUntilReached(Entity target) {
if (nextPosition == null || reached) {
break;
}
newPath.add(new Step(nextPosition));
newPath.add(new Vector2(nextPosition));
lastPosition = nextPosition;
}
addPath(newPath);
Expand All @@ -280,7 +205,7 @@ public void lazyWalk(Position destination) {
logger.warn("Existing lazy walk request in progress.");
return;
}
ListenableFuture<Deque<Step>> pathResult = mob.getService().submit(() -> findPath(destination));
ListenableFuture<Deque<Vector2>> pathResult = mob.getService().submit(() -> findPath(destination));
pathResult.addListener(() -> {
try {
addPath(pathResult.get());
Expand Down Expand Up @@ -360,14 +285,14 @@ public void walkBehind(Mob target) {
*
* @param path The path to walk.
*/
public void addPath(Deque<Step> path) {
public void addPath(Deque<Vector2> path) {
int size = path.size();
if (size == 1) {
addFirst(path.poll());
} else if (size > 1) {
addFirst(path.poll());
for (; ; ) {
Step nextStep = path.poll();
Vector2 nextStep = path.poll();
if (nextStep == null) {
break;
}
Expand All @@ -389,11 +314,11 @@ public void clear() {
*
* @param step The step to add.
*/
private void addFirst(Step step) {
private void addFirst(Vector2 step) {
current.clear();
Queue<Step> backtrack = new ArrayDeque<>();
Deque<Vector2> backtrack = new ArrayDeque<>();
for (; ; ) {
Step prev = previous.pollLast();
Vector2 prev = previous.pollLast();
if (prev == null) {
break;
}
Expand All @@ -413,10 +338,10 @@ private void addFirst(Step step) {
*
* @param next The step to add.
*/
private void add(Step next) {
Step last = current.peekLast();
void add(Vector2 next) {
Vector2 last = current.peekLast();
if (last == null) {
last = new Step(mob.getPosition());
last = new Vector2(mob.getPosition());
}

int nextX = next.getX();
Expand All @@ -438,7 +363,7 @@ private void add(Step next) {
} else if (deltaY > 0) {
deltaY--;
}
current.add(new Step(nextX - deltaX, nextY - deltaY));
current.add(new Vector2(nextX - deltaX, nextY - deltaY));
}
}

Expand Down Expand Up @@ -496,16 +421,16 @@ private void incrementRunEnergy() {
* @param target The target.
* @return The path.
*/
private Deque<Step> findPath(Position target) {
private Deque<Vector2> findPath(Position target) {
Deque<Position> positionPath = pathfindingAlgorithm.find(mob.getPosition(), target);
Deque<Step> stepPath = new ArrayDeque<>(positionPath.size());
Deque<Vector2> stepPath = new ArrayDeque<>(positionPath.size());
for (; ; ) {
// TODO remove step class?
Position next = positionPath.poll();
if (next == null) {
break;
}
stepPath.add(new Step(next));
stepPath.add(new Vector2(next));
}
return stepPath;
}
Expand Down
Loading
Loading