Use areas to make script more robust

This commit is contained in:
2021-10-18 17:59:21 +02:00
parent bc6e82e409
commit 97d34e4415
2 changed files with 79 additions and 64 deletions

View File

@ -1,30 +1,24 @@
package io.reisub.dreambot.cagility; package io.reisub.dreambot.cagility;
import org.dreambot.api.methods.interactive.GameObjects; import org.dreambot.api.methods.interactive.GameObjects;
import org.dreambot.api.methods.interactive.Players;
import org.dreambot.api.methods.map.Area;
import org.dreambot.api.wrappers.interactive.GameObject; import org.dreambot.api.wrappers.interactive.GameObject;
public class Obstacle { public class Obstacle {
private final int id; private final int id;
private final boolean retry; private final Area[] areas;
private Obstacle next; private Obstacle next;
public Obstacle(int id, boolean canRetry) { public Obstacle(int id, Area... areas) {
this.id = id; this.id = id;
this.retry = canRetry; this.areas = areas;
}
public Obstacle(int id) {
this(id, false);
} }
public int getID() { public int getID() {
return this.id; return this.id;
} }
public boolean canRetry() {
return this.retry;
}
public GameObject getGameObject() { public GameObject getGameObject() {
return GameObjects.closest(getID()); return GameObjects.closest(getID());
} }
@ -36,4 +30,14 @@ public class Obstacle {
public void setNext(Obstacle next) { public void setNext(Obstacle next) {
this.next = next; this.next = next;
} }
public boolean isPlayerInArea() {
for (Area area : areas) {
if (area.contains(Players.localPlayer())) {
return true;
}
}
return false;
}
} }

View File

@ -3,19 +3,16 @@ package io.reisub.dreambot.cagility.tasks;
import io.reisub.dreambot.cagility.Obstacle; import io.reisub.dreambot.cagility.Obstacle;
import io.reisub.dreambot.util.Constants; import io.reisub.dreambot.util.Constants;
import io.reisub.dreambot.util.Util; import io.reisub.dreambot.util.Util;
import io.reisub.dreambot.util.event.ListenerManager;
import io.reisub.dreambot.util.event.health.HealthListener;
import org.dreambot.api.input.Mouse; import org.dreambot.api.input.Mouse;
import org.dreambot.api.methods.Calculations; import org.dreambot.api.methods.Calculations;
import org.dreambot.api.methods.MethodContext; import org.dreambot.api.methods.MethodContext;
import org.dreambot.api.methods.interactive.Players; import org.dreambot.api.methods.interactive.Players;
import org.dreambot.api.methods.item.GroundItems; import org.dreambot.api.methods.item.GroundItems;
import org.dreambot.api.methods.skills.Skill; import org.dreambot.api.methods.map.Area;
import org.dreambot.api.methods.map.Tile;
import org.dreambot.api.methods.walking.impl.Walking; import org.dreambot.api.methods.walking.impl.Walking;
import org.dreambot.api.script.ScriptManager; import org.dreambot.api.script.ScriptManager;
import org.dreambot.api.script.TaskNode; import org.dreambot.api.script.TaskNode;
import org.dreambot.api.script.event.impl.ExperienceEvent;
import org.dreambot.api.script.listener.ExperienceListener;
import org.dreambot.api.wrappers.interactive.GameObject; import org.dreambot.api.wrappers.interactive.GameObject;
import org.dreambot.api.wrappers.items.GroundItem; import org.dreambot.api.wrappers.items.GroundItem;
@ -26,20 +23,58 @@ import java.util.Map;
public class HandleObstacle extends TaskNode { public class HandleObstacle extends TaskNode {
public enum Course { public enum Course {
CANIFIS( CANIFIS(
new Obstacle(14843), new Obstacle(14843, new Area(new Tile(3520, 3510, 0), new Tile(3465, 3465, 0))),
new Obstacle(14844), new Obstacle(14844, new Area(
new Obstacle(14845), new Tile(3504, 3497, 2),
new Obstacle(14848), new Tile(3504, 3494, 2),
new Obstacle(14846), new Tile(3505, 3491, 2),
new Obstacle(14894), new Tile(3508, 3491, 2),
new Obstacle(14847), new Tile(3511, 3494, 2),
new Obstacle(14897) new Tile(3512, 3496, 2),
new Tile(3508, 3499, 2),
new Tile(3505, 3499, 2))),
new Obstacle(14845, new Area(
new Tile(3496, 3502, 2),
new Tile(3505, 3503, 2),
new Tile(3505, 3507, 2),
new Tile(3496, 3507, 2))),
new Obstacle(14848, new Area(
new Tile(3493, 3506, 2),
new Tile(3488, 3506, 2),
new Tile(3488, 3502, 2),
new Tile(3485, 3502, 2),
new Tile(3485, 3498, 2),
new Tile(3491, 3498, 2),
new Tile(3493, 3499, 2))),
new Obstacle(14846, new Area(
new Tile(3481, 3500, 3),
new Tile(3474, 3500, 3),
new Tile(3474, 3491, 3),
new Tile(3481, 3491, 3))),
new Obstacle(14894, new Area(
new Tile(3485, 3488, 2),
new Tile(3477, 3488, 2),
new Tile(3476, 3481, 2),
new Tile(3482, 3480, 2))),
new Obstacle(14847, new Area(
new Tile(3487, 3479, 3),
new Tile(3486, 3468, 3),
new Tile(3500, 3467, 3),
new Tile(3505, 3471, 3),
new Tile(3504, 3477, 3),
new Tile(3497, 3480, 3))),
new Obstacle(14897, new Area(
new Tile(3508, 3473, 2),
new Tile(3513, 3473, 2),
new Tile(3516, 3476, 2),
new Tile(3516, 3484, 2),
new Tile(3507, 3484, 2)))
); );
private final Obstacle firstObstacle; private final Obstacle[] obstacles;
Course(Obstacle... obstacles) { Course(Obstacle... obstacles) {
this.firstObstacle = obstacles[0]; this.obstacles = obstacles;
for (int i = 0; i < obstacles.length; i++) { for (int i = 0; i < obstacles.length; i++) {
if (i + 1 == obstacles.length) { if (i + 1 == obstacles.length) {
@ -49,32 +84,22 @@ public class HandleObstacle extends TaskNode {
} }
} }
} }
public Obstacle getCurrentObstacle() {
for (Obstacle o : obstacles) {
if (o.isPlayerInArea()) return o;
}
return null;
}
} }
private final Course course; private final Course course;
private final Map<Integer, Rectangle> hoverMap; private final Map<Integer, Rectangle> hoverMap;
private Obstacle lastObstacle = null;
private boolean failed, obstacleCompleted = false;
public HandleObstacle(Course course) { public HandleObstacle(Course course) {
this.course = course; this.course = course;
this.hoverMap = new HashMap<>(); this.hoverMap = new HashMap<>();
ListenerManager.getInstance().addListener(new HealthListener() {
@Override
public void onHealthDecreased(int oldValue, int newValue) {
failed = true;
}
});
ScriptManager.getScriptManager().addListener(new ExperienceListener() {
@Override
public void onGained(ExperienceEvent event) {
if (event.getSkill().equals(Skill.AGILITY)) {
obstacleCompleted = true;
}
}
});
} }
@Override @Override
@ -86,21 +111,11 @@ public class HandleObstacle extends TaskNode {
@Override @Override
public int execute() { public int execute() {
Obstacle current; Obstacle current = course.getCurrentObstacle();
if (current == null) {
if (lastObstacle == null) { MethodContext.logError("Player is in an unsupported area: " + Players.localPlayer().getTile().toString() + ". Stopping.");
current = course.firstObstacle; ScriptManager.getScriptManager().stop();
} else { return 0;
if (failed) {
if (lastObstacle.canRetry()) {
current = lastObstacle;
} else {
current = course.firstObstacle;
}
failed = false;
} else {
current = lastObstacle.getNext();
}
} }
final GameObject currentObject = current.getGameObject(); final GameObject currentObject = current.getGameObject();
@ -117,16 +132,12 @@ public class HandleObstacle extends TaskNode {
currentObject.interact(); currentObject.interact();
lastObstacle = current;
Rectangle hoverRect = hoverMap.get(current.getNext().getID()); Rectangle hoverRect = hoverMap.get(current.getNext().getID());
if (hoverRect != null) { if (hoverRect != null) {
Mouse.move(hoverRect); Mouse.move(hoverRect);
} }
MethodContext.sleepUntil(() -> failed || obstacleCompleted, Calculations.random(9000, 10000)); Util.sleepUntilMovingAndAnimating(Calculations.random(9000, 10000));
obstacleCompleted = false;
return Calculations.random(180, 350); return Calculations.random(180, 350);
} }