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
  • Binary tree framework with action controller


    jgs95

    Recommended Posts

    Hi all,

    I've written a binary tree framework that does the following:

    • Fluent style quickly and easily define branches
    • Ability to continuously execute a leaf until it is done processing, so the tree doesn't need to be traversed each game loop

    Main files to look at:

    • src/example/WoodcutterTreeBuilder
    • src/example/WoodcutterScript
    • src/example/actions
    • src/framework

    https://github.com/jgfighter95/dreambot_binary_tree/tree/master/src

    Snippet - define branches

    package example;
    
    import example.actions.BankAction;
    import example.actions.WoodcuttingAction;
    import framework.*;
    import org.dreambot.api.methods.interactive.Players;
    
    public class WoodcutterTreeBuilder extends TreeBuilder<WoodcutterContext> {
    	private ActionController controller;
    	
    	public WoodcutterTreeBuilder(WoodcutterContext context) {
    		super(context);
    	}
    	
    	@Override
    	public Node<WoodcutterContext> build(ActionController controller) {
    		this.controller = controller;
    		return inventoryIsFull();
    	}
    	
    	private Node<WoodcutterContext> inventoryIsFull() {
    		return new Branch<WoodcutterContext>()
    				.validate(() -> Inventory.isFull()))
    				.success(new Leaf<WoodcutterContext>(new BankAction(controller, context)))
    				.fail(new Leaf<WoodcutterContext>(new WoodcuttingAction(controller, context)));
    	}
    }

     

    Snippet define action (of a leaf)

    package example.actions;
    
    import example.WoodcutterContext;
    import framework.Action;
    import framework.ActionController;
    import org.dreambot.api.methods.MethodProvider;
    import org.dreambot.api.methods.container.impl.Inventory;
    import org.dreambot.api.methods.container.impl.bank.Bank;
    import org.dreambot.api.methods.interactive.Players;
    import org.dreambot.api.methods.walking.impl.Walking;
    
    public class BankAction extends Action<WoodcutterContext> {
    	public BankAction(ActionController actionController, WoodcutterContext context) {
    		super(actionController, context);
    	}
    	
    	@Override
    	public int onLoop() {
    		if (context.bankArea.contains(Players.localPlayer())) {
    			if (Bank.isOpen()) {
    				if (Inventory.count(item -> !item.getName().toLowerCase().contains("axe")) > 0)	{
    					MethodProvider.log("Depositing all items except axe");
    					Bank.depositAllExcept(item -> item.getName().toLowerCase().contains("axe"));
    				} else {
    					Bank.close();
    					MethodProvider.log("Closing bank");
    					// the action is now done, so tell controller to re-evaluate tree & get next action
    					actionController.nextNode();
    				}
    			} else {
    				MethodProvider.log("Opening bank");
    				Bank.open();
    			}
    		} else {
    			MethodProvider.log("Walking to bank");
    			if (Walking.shouldWalk()) {
    				Walking.walk(context.bankTile);
    			}
    			MethodProvider.sleepUntil(Walking::shouldWalk, Players.localPlayer()::isMoving, 3000, 500);
    		}
    		return 1000;
    	}
    	
    	@Override
    	public void onStart() {
    	
    	}
    }

     

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • 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.