Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Basic Node Framework /w Example Node


    Notorious
     Share

    Recommended Posts

    This is a simple implementation of a node design for your scripts. I will briefly explain each piece, though please don't hesitate to ask if you have more questions.

     

    Creating The Node Object:

     

     

    So to start this framework, we need to create a Node object. This class will be abstract, and contain two abstract methods, accept and execute.

     

    The basics of the implementation would be as follows: (NOTE: WE WILL NOT BE USING THIS FOR THE REST! USE THE ONE BELOW!)

    public abstract class Node {
        //This is optional, but can be used to help define which node should execute if two or more are accepted.
        public int priority () {
            return 0;
        }
        //This will determine whether or not we should execute this node.
        public abstract boolean accept();
    
        //This will contain the action logic you wish to preform if this node is accepted.
        public abstract int execute();
    }
    

    Though this is very simple implementation, and we do not have access to a method context object; so lets fix that by adding a MethodContext variable, and passing it as a argument in the constructor.

    public abstract class Node {
        private MethodContext context;
    
        public Node(MethodContext context){
            this.context = context;
        }
        public int priority () {
            return 0;
        }
        public abstract boolean accept();
    
        //This can return whatever you like.
        public abstract int execute();
    
        //We can now use this inside of any class which extends node.
        public MethodContext getContext(){
            return context;
        }
    }
    

     

     

    Creating A Node:

     

     

    Well now we have a Node object which we can extend from, so lets make a class which extends from Node which we can use in our script! Nodes are used for the logic of your scripts.This is just a example, and can be used for any type of script!

    public class CutTreeNode extends Node {
    
        //More Java 8 stuff you should learn more about
        private Optional<GameObject> object;
    
        //Since we define Node with a constructor accepting a MethodContext,
        // we must do the same from each class which extends from it.
        public CutTreeNode(MethodContext context) {
            super(context);  //Pass the arguments to Node class. We need this.
        }
    
        @Override
        public int priority() {
            return 4;   //We set the level of priority for this node. Higher == More Priority
        }
    
        //We must extend this method since we declared it as abstract.
        @Override
        public boolean accept() {
            if(!object.isPresent() || object.filter(g -> !g.exists()).isPresent()){
                object = Optional.ofNullable(getContext().getGameObjects().closest("Tree"));
            }
            return getContext().getLocalPlayer().isStandingStill() && object.filter(GameObject::exists).isPresent();
        }
        
        //We must extend this method since we declared it as abstract.
        @Override
        public int execute() {
            //This is equivalent to object != null using Java 8 Optional
            object.ifPresent(g -> {
                //'g' is the GameObject
                g.interact("Cut down");
            });
            return (int)Calculations.nextGaussianRandom(400, 200); //How long do I want to wait after executing this node
        }
    }
    

    Basic tree cutting example. Will find a game object tree, and cut it if standing still.

     

     

    Setting Up The Framework:

     

     

    So now we have the Node object we can use, so now we need to setup up our Script onLoop method to use these Nodes properly, and we can do that using:

    @ScriptManifest(name = "Node Example", author = "Notorious", version = 0.420D, description = "Nodes", category = Category.MISC)
    public class Script extends AbstractScript {
        //This holds the open possible nodes to use.
        private final List<Node> open = new ArrayList<>();
        //This holds the nodes you have created to iterate through.
        private final List<Node> cache = new ArrayList<>();
    
        @Override
        public void onStart() {
            //This is where we add the nodes to itterate, since we made our nodes take a MethodContext, we can pass it
            // by using the keyword this from our Script class.
            cache.add(new CutTreeNode(this));
        }
    
        @Override
        public int onLoop() {
            int delay = 420; //Default return value
            if(!cache.isEmpty()){ //Check if empty just to be safe.
                open.clear();
                //If the node accept method is true, add node to open list. Java8 ftw :D
                open.addAll(cache.stream().filter(Node::accept).collect(Collectors.toList()));
                if(!open.isEmpty()){
                    //Find the best node in the open list
                    delay = getSuitableOpenNode().execute(); //This will preform the action, and since I return int, I can use it to delay.
                }
            }
            return delay;
        }
    
        public Node getSuitableOpenNode () {
            Node node = null;
            if(!open.isEmpty()) {
                node = open.get(0); //Set to the first item
                if(open.size() > 1) { //If more than one item, iterate through the list
                    for (Node possible : open) {
                        if (node.priority() < possible.priority()) { //Find the highest priority node
                            node = possible;
                        }
                    }
                }
            }
            return node;
        }
        //This is the list of all your nodes you have added
        public List<Node> getNodeCache() {
            return cache;
        }
    }
    

     

     

     

    So it may not be the prettiest, or the best setup, though I feel it's a good starting point for most when starting to use Node frameworks. If you have any issues/question/or really anything, feel free to let me know.

     

    NOTE: Most of this code was written free hand outside a IDE, so forgive me for any errors, I will fix them as I find them!

    Link to comment
    Share on other sites

    So convenient! Thanks, makes scripting the motherload mine a bit easier and way cleaner. I might need some help though, banking works and mining status says it works it just doesn't even try to do anything, I might pm you when I get home to my code if that's okay. Do I have to declare the object in the accept method?

    Edited by lawdeedaw
    Link to comment
    Share on other sites

    • 2 weeks later...

    This is a simple implementation of a node design for your scripts. I will briefly explain each piece, though please don't hesitate to ask if you have more questions.

     

    Creating The Node Object:

     

     

    So to start this framework, we need to create a Node object. This class will be abstract, and contain two abstract methods, accept and execute.

     

    The basics of the implementation would be as follows: (NOTE: WE WILL NOT BE USING THIS FOR THE REST! USE THE ONE BELOW!)

    public abstract class Node {
        //This is optional, but can be used to help define which node should execute if two or more are accepted.
        public int priority () {
            return 0;
        }
        //This will determine whether or not we should execute this node.
        public abstract boolean accept();
    
        //This will contain the action logic you wish to preform if this node is accepted.
        public abstract int execute();
    }
    

    Though this is very simple implementation, and we do not have access to a method context object; so lets fix that by adding a MethodContext variable, and passing it as a argument in the constructor.

    public abstract class Node {
        private MethodContext context;
    
        public Node(MethodContext context){
            this.context = context;
        }
        public int priority () {
            return 0;
        }
        public abstract boolean accept();
    
        //This can return whatever you like.
        public abstract int execute();
    
        //We can now use this inside of any class which extends node.
        public MethodContext getContext(){
            return context;
        }
    }
    

     

     

    Creating A Node:

     

     

    Well now we have a Node object which we can extend from, so lets make a class which extends from Node which we can use in our script! Nodes are used for the logic of your scripts.This is just a example, and can be used for any type of script!

    public class CutTreeNode extends Node {
    
        //More Java 8 stuff you should learn more about
        private Optional<GameObject> object;
    
        //Since we define Node with a constructor accepting a MethodContext,
        // we must do the same from each class which extends from it.
        public CutTreeNode(MethodContext context) {
            super(context);  //Pass the arguments to Node class. We need this.
        }
    
        @Override
        public int priority() {
            return 4;   //We set the level of priority for this node. Higher == More Priority
        }
    
        //We must extend this method since we declared it as abstract.
        @Override
        public boolean accept() {
            if(!object.isPresent() || object.filter(g -> !g.exists()).isPresent()){
                object = Optional.ofNullable(getContext().getGameObjects().closest("Tree"));
            }
            return getContext().getLocalPlayer().isStandingStill() && object.filter(GameObject::exists).isPresent();
        }
        
        //We must extend this method since we declared it as abstract.
        @Override
        public int execute() {
            //This is equivalent to object != null using Java 8 Optional
            object.ifPresent(g -> {
                //'g' is the GameObject
                g.interact("Cut down");
            });
            return (int)Calculations.nextGaussianRandom(400, 200); //How long do I want to wait after executing this node
        }
    }
    

    Basic tree cutting example. Will find a game object tree, and cut it if standing still.

     

     

    Setting Up The Framework:

     

     

    So now we have the Node object we can use, so now we need to setup up our Script onLoop method to use these Nodes properly, and we can do that using:

    @ScriptManifest(name = "Node Example", author = "Notorious", version = 0.420D, description = "Nodes", category = Category.MISC)
    public class Script extends AbstractScript {
        //This holds the open possible nodes to use.
        private final List<Node> open = new ArrayList<>();
        //This holds the nodes you have created to iterate through.
        private final List<Node> cache = new ArrayList<>();
    
        @Override
        public void onStart() {
            //This is where we add the nodes to itterate, since we made our nodes take a MethodContext, we can pass it
            // by using the keyword this from our Script class.
            cache.add(new CutTreeNode(this));
        }
    
        @Override
        public int onLoop() {
            int delay = 420; //Default return value
            if(!cache.isEmpty()){ //Check if empty just to be safe.
                open.clear();
                //If the node accept method is true, add node to open list. Java8 ftw 
                open.addAll(cache.stream().filter(Node::accept).collect(Collectors.toList()));
                if(!open.isEmpty()){
                    //Find the best node in the open list
                    delay = getSuitableOpenNode().execute(); //This will preform the action, and since I return int, I can use it to delay.
                }
            }
            return delay;
        }
    
        public Node getSuitableOpenNode () {
            Node node = null;
            if(!open.isEmpty()) {
                node = open.get(0); //Set to the first item
                if(open.size() > 1) { //If more than one item, iterate through the list
                    for (Node possible : open) {
                        if (node.priority() < possible.priority()) { //Find the highest priority node
                            node = possible;
                        }
                    }
                }
            }
            return node;
        }
        //This is the list of all your nodes you have added
        public List<Node> getNodeCache() {
            return cache;
        }
    }
    

     

     

     

    So it may not be the prettiest, or the best setup, though I feel it's a good starting point for most when starting to use Node frameworks. If you have any issues/question/or really anything, feel free to let me know.

     

    NOTE: Most of this code was written free hand outside a IDE, so forgive me for any errors, I will fix them as I find them!

    Ahem...Why does it return 420??

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.