Some code I toyed around with yesterday to test out a few things:
Core:
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import java.util.Comparator;
import java.util.function.Predicate;
import java.util.stream.Stream;
@ScriptManifest(author = "Botre", info = "", logo = "", name = "Experiment", version = 0.0)
public class ExperimentalScript extends Script {
private ScriptClock clock = new ScriptClock(1.6);
private String name = "Man";
private String action = "Pickpocket";
private Predicate<NPC> validityPredicate = o -> o != null && o.exists() && o.getName().equals(name) && o.hasAction(action) && getMap().canReach(o.getPosition());
private Comparator<Entity> distanceComparator = EntityComparator.create(this, EntityComparator.Type.DISTANCE_FROM_PLAYER);
private Target<NPC> target = new Target<>(validityPredicate, () -> getNpcStream().filter(validityPredicate).min(distanceComparator));
@Override
public int onLoop() throws InterruptedException {
if (target.valid() || target.find()) {
target.get().interact(action);
}
return clock.tick();
}
private Stream<RS2Object> getObjectStream() {
return getObjects().getAll().stream();
}
private Stream<NPC> getNpcStream() {
return getNpcs().getAll().stream();
}
}
Target:
import java.util.Optional;
import java.util.function.Predicate;
/**
* Created by Bjorn on 19/07/2015.
*/
public class Target<T> {
private T target;
private TargetSearcher<T> searcher;
private Predicate<T> validity;
public Target(Predicate<T> validity, TargetSearcher<T> searcher) {
this.validity = validity;
this.searcher = searcher;
}
/**
* @return The current target.
*/
public T get() {
return target;
}
/**
* @return Whether the current target is valid.
*/
public boolean valid() {
if (validity.test(target)) {
return true;
} else {
target = null;
return false;
}
}
/**
* Searches for and assigns a new target by making a call to the search() method.
*
* @return Whether a new target was find.
*/
public boolean find() {
Optional<T> optional = searcher.search();
if (optional.isPresent()) {
target = optional.get();
return true;
} else {
return false;
}
}
}
Target searcher:
import java.util.Optional;
/**
* Created by Bjorn on 21/07/2015.
*/
public interface TargetSearcher<T> {
/**
* Searches for a new target.
*
* @return The new target.
*/
Optional<T> search();
}
Script clock:
/**
* Created by Bjorn on 19/07/2015.
*/
public class ScriptClock {
private double loopsPerSecond ;
private int millisecondsPerLoop;
private long systemMilliseconds;
private long deltaMilliseconds;
private int sleepMilliseconds;
public ScriptClock(double loopsPerSecond) {
this.loopsPerSecond = loopsPerSecond;
millisecondsPerLoop = (int) ((1 / loopsPerSecond) * 1000);
systemMilliseconds = System.currentTimeMillis();
}
public int tick() {
deltaMilliseconds = System.currentTimeMillis() - systemMilliseconds;
systemMilliseconds = System.currentTimeMillis();
sleepMilliseconds = (int) (millisecondsPerLoop - deltaMilliseconds);
return sleepMilliseconds > 0 ? sleepMilliseconds : 0;
}
}
Entity comparator:
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.MethodProvider;
import java.util.Comparator;
/**
* Created by Bjorn on 20/07/2015.
*/
public final class EntityComparator {
public enum Type {
DISTANCE_FROM_PLAYER,
}
public static Comparator<Entity> create(MethodProvider mp, Type type) {
switch(type) {
case DISTANCE_FROM_PLAYER: return (a, b) -> mp.getMap().distance(a.getPosition()) - mp.getMap().distance(b.getPosition());
}
return null;
}
}