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

Get WalkingQueue under test #244

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/main/java/io/luna/LunaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author lare96 <http://github.org/lare96>
*/
public final class LunaContext {
public class LunaContext {

/**
* The world.
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/io/luna/game/model/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,19 +608,23 @@ public double getRunEnergy() {
/**
* Sets the run energy percentage.
*
* @param newRunEnergy The value to set to.
* @param runEnergy The value to set to.
*/
public void setRunEnergy(double newRunEnergy, boolean update) {
if (newRunEnergy > 100.0) {
newRunEnergy = 100.0;
public void setRunEnergy(double runEnergy) {
if (runEnergy > 100.0) {
runEnergy = 100.0;
}

if (runEnergy != newRunEnergy) {
runEnergy = newRunEnergy;
if (update) {
queue(new UpdateRunEnergyMessageWriter((int) runEnergy));
}
if (runEnergy < 0) {
runEnergy = 0;
}

this.runEnergy = runEnergy;
}

/** Updates the client with the current run energy. */
public void updateRunEnergy() {
queue(new UpdateRunEnergyMessageWriter((int) runEnergy));
}

/**
Expand All @@ -635,9 +639,20 @@ public void increaseRunEnergy(double amount) {
} else if (newEnergy < 0.0) {
newEnergy = 0.0;
}
setRunEnergy(newEnergy, true);
setRunEnergy(newEnergy);
updateRunEnergy();
}

boolean hasEnoughEnergyToRun() {
return runEnergyAfterReduction() >= 0;
}

/** @return the remaining {@code runEnergy} after a running a single step.*/
double runEnergyAfterReduction() {
double energyReduction = 0.117 * 2 * Math
.pow(Math.E, 0.0027725887222397812376689284858327062723020005374410 * getWeight());
return getRunEnergy() - energyReduction;
}
/**
* @return The combined weight of the inventory and equipment.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/luna/game/model/mob/PlayerSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author lare96 <http://github.com/lare96>
*/
public final class PlayerSettings {
public class PlayerSettings {

/**
* An enumerated type whose elements represent brightness levels.
Expand Down
104 changes: 53 additions & 51 deletions src/main/java/io/luna/game/model/mob/WalkingQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author lare96 <http://github.org/lare96>
* @author Graham
*/
public final class WalkingQueue {
public class WalkingQueue {

// TODO Rewrite

Expand Down Expand Up @@ -94,12 +94,12 @@ public int getY() {
/**
* A deque of current steps.
*/
private final Deque<Step> current = new ArrayDeque<>();
private final Deque<Step> currentQueue = new ArrayDeque<>();

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

/**
* The mob.
Expand All @@ -115,7 +115,6 @@ public int getY() {
* If the current path is a running path.
*/
private boolean runningPath;

/**
* Create a new {@link WalkingQueue}.
*
Expand All @@ -131,45 +130,70 @@ public WalkingQueue(Mob mob) {
*/
public void process() {
// TODO clean up function
Step current = new Step(mob.getPosition());
Step currentStep = new Step(mob.getPosition());

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

boolean restoreEnergy = true;

Step next = this.current.poll();
if (next != null) {
previous.add(next);
walkingDirection = Direction.between(current, next);
current = next;
Step nextStep = this.currentQueue.poll();
if (nextStep != null) {
previousQueue.add(nextStep);
walkingDirection = Direction.between(currentStep, nextStep);
currentStep = nextStep;

if (mob.getType() == EntityType.PLAYER) {
Player player = mob.asPlr();
if (player.isRunning() || runningPath) {
next = decrementRunEnergy(player) ? this.current.poll() : null;
if (next != null) {
if (player.hasEnoughEnergyToRun()) {
useEnergy(player);
updateEnergy();
nextStep = this.currentQueue.poll();
} else {
nextStep = null;
}
if (nextStep != null) {
restoreEnergy = false;
previous.add(next);
runningDirection = Direction.between(current, next);
current = next;
previousQueue.add(nextStep);
runningDirection = Direction.between(currentStep, nextStep);
currentStep = nextStep;
}
}
}


Position newPosition = new Position(current.getX(), current.getY(), mob.getPosition().getZ());
Position newPosition = new Position(currentStep.getX(), currentStep.getY(), mob.getPosition().getZ());
mob.setPosition(newPosition);
}

if (restoreEnergy && mob.getType() == EntityType.PLAYER) {
incrementRunEnergy();
Player player = mob.asPlr();
incrementRunEnergy(player);
updateEnergy();
}

mob.setWalkingDirection(walkingDirection);
mob.setRunningDirection(runningDirection);
}

void updateEnergy(){
mob.asPlr().updateRunEnergy();
}

/**
* Depletes a player's energy. If the player doesn't have enough energy, the player will stop running.
*/
void useEnergy(Player player) {
double energyLeft = player.runEnergyAfterReduction();
if (player.hasEnoughEnergyToRun()) {
player.setRunEnergy(energyLeft);
} else {
player.setRunEnergy(0.0);
player.setRunning(false);
runningPath = false;
}
}

/**
* Walks to the specified offsets.
*
Expand Down Expand Up @@ -200,23 +224,22 @@ public void walk(Position firstPos, Position... otherPos) {
* @param step The step to add.
*/
public void addFirst(Step step) {
current.clear();
currentQueue.clear();
runningPath = false;

Queue<Step> backtrack = new ArrayDeque<>();
for (; ; ) {
Step prev = previous.pollLast();
Step prev = previousQueue.pollLast();
if (prev == null) {
break;
}
backtrack.add(prev);
if (prev.equals(step)) {
backtrack.forEach(this::add);
previous.clear();
previousQueue.clear();
return;
}
}
previous.clear();
previousQueue.clear();

add(step);
}
Expand All @@ -227,7 +250,7 @@ public void addFirst(Step step) {
* @param next The step to add.
*/
public void add(Step next) {
Step last = current.peekLast();
Step last = currentQueue.peekLast();
if (last == null) {
last = new Step(mob.getPosition());
}
Expand All @@ -251,43 +274,22 @@ public void add(Step next) {
} else if (deltaY > 0) {
deltaY--;
}
current.add(new Step(nextX - deltaX, nextY - deltaY));
currentQueue.add(new Step(nextX - deltaX, nextY - deltaY));
}
}

/**
* Clears the current and previous steps.
*/
public void clear() {
current.clear();
previous.clear();
}

/**
* A function that implements an algorithm to deplete run energy.
*
* @return {@code false} if the player can no longer run.
*/
private boolean decrementRunEnergy(Player player) {
double totalWeight = player.getWeight();
double energyReduction = 0.117 * 2 * Math
.pow(Math.E, 0.0027725887222397812376689284858327062723020005374410 * totalWeight);
double newValue = player.getRunEnergy() - energyReduction;
if (newValue <= 0.0) {
player.setRunEnergy(0.0, true);
player.setRunning(false);
runningPath = false;
return false;
}
player.setRunEnergy(newValue, true);
return true;
currentQueue.clear();
previousQueue.clear();
}

/**
* A function that implements an algorithm to restore run energy.
*/
private void incrementRunEnergy() {
Player player = mob.asPlr();
void incrementRunEnergy(Player player) {

double runEnergy = player.getRunEnergy();
if (runEnergy >= 100.0) {
Expand All @@ -300,7 +302,7 @@ private void incrementRunEnergy() {
double newValue = runEnergy + energyRestoration;
newValue = Math.min(newValue, 100.0);

player.setRunEnergy(newValue, true);
player.setRunEnergy(newValue);
}

/**
Expand All @@ -309,7 +311,7 @@ private void incrementRunEnergy() {
* @return The amount of remaining steps.
*/
public int getRemainingSteps() {
return current.size();
return currentQueue.size();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void load(Player player) {
player.getIgnores().addAll(ignores);
player.setUnbanDate(unbanDate);
player.setUnmuteDate(unmuteDate);
player.setRunEnergy(runEnergy, false);
player.setRunEnergy(runEnergy);
player.setWeight(weight, false);
player.getAttributes().load(attributes);
}
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/io/luna/game/model/mob/NullPlayerSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.luna.game.model.mob;

/** A {@link PlayerSettings} designed for testing purposes only. */
public class NullPlayerSettings extends PlayerSettings {

@Override
public void showRunning() {
return;
}

@Override
public void showAutoRetaliate() {
return;
}

@Override
public void showBrightnessLevel() {
return;
}

@Override
public void showMouseType() {
return;
}

@Override
public void showChatEffects() {
return;
}

@Override
public void showSplitPrivateChat() {
return;
}

@Override
public void showAcceptAid() {
return;
}

@Override
public void showMusicVolume() {
return;
}

@Override
public void showEffectsVolume() {
return;
}
}
20 changes: 20 additions & 0 deletions src/test/java/io/luna/game/model/mob/NullWalkingQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.luna.game.model.mob;

/**
* A {@link WalkingQueue} designed for testing purposes only.
*/
class NullWalkingQueue extends WalkingQueue {
/**
* Create a new {@link WalkingQueue}.
*
* @param mob The mob.
*/
NullWalkingQueue(Mob mob) {
super(mob);
}

@Override
void updateEnergy() {
return;
}
}
Loading