Jump to content

Stage Based Scripting Framework


xlDino

Recommended Posts

All thoughts or improvements are welcome!

1. initStages(): This method initializes the stages of the bot. It creates an array of "Stage" objects and assigns it to the "stages" variable. In this example, there are two stages defined: StageOne and StageTwo.

2. nextStage(): This method determines the next stage that should be executed based on the current stage. It searches for the index of the current stage in the "stages" array and returns the stage at the next index. If there are no more stages, it returns null, indicating that the script has completed.

3. onStart(): calls the initStages() method to initialize the stages and assigns the first stage to the currentStage variable upon bot startup.

4. onLoop():  Inside this method, the current stage is checked, and if it exists, its logic is executed by calling the `execute()` method of the current stage. After executing the logic, the isCompleted() method of the current stage is checked to determine if the stage is completed. If the stage is completed, the next stage is transitioned to by calling the nextStage() method. If there are no more stages, the script restarts by re-initializing the stages and assigning the first stage to the currentStage variable. The method returns an integer value that represents the delay between iterations of the loop.


import java.awt.Graphics2D;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(name = "MyScript", author = "YourName", version = 1.0, info = "", logo = "")
public class Main extends Script {

    boolean debugMode = true;
    
    private Stage previousStage;
    private Stage currentStage;
    private Stage[] stages;

    private void debug(String message) {
    	if(debugMode){log("[DEBUG] " + message);}
    }
    
    
    private interface Stage {
        void execute() throws InterruptedException;

        boolean isCompleted();
    }

    private void initStages() {
        stages = new Stage[]{
                new StageOne(),
                new StageTwo()
        };
    }
    
    private Stage nextStage() {
        int currentIndex = -1;

        for (int i = 0; i < stages.length; i++) {
            if (stages[i] == currentStage) {
                currentIndex = i;
                break;
            }
        }

        if (currentIndex != -1 && currentIndex + 1 < stages.length) {
            return stages[currentIndex + 1];
        }

        return null; // No more stages, script completed
    }

    public void onStart() {
        // Initialize stages
        initStages();
        // Start with the first stage
        currentStage = stages[0];
    }

    public void onExit() {
        // Cleanup code
    }

    public void onPaint(Graphics2D g) {
        
    }

    public int onLoop() throws InterruptedException {
    	
        if (currentStage != null) {
            boolean stageChanged = currentStage != previousStage;
            if (stageChanged) {
                debug("[StageHandler] Current Stage: " + currentStage);
                previousStage = currentStage;
            }
            currentStage.execute();
            if (currentStage.isCompleted()) {
                currentStage = nextStage();
                previousStage = null; // Reset previousStage when transitioning to a new stage
            }
        }

        if (currentStage == null) {
        	debug("Stage is null! Restarting stages...");
            initStages();
            currentStage = stages[0];
            previousStage = null; // Reset previousStage when restarting stages
        }

        return random(500, 1000); // Set a delay between iterations
    }
    
    private class StageOne implements Stage {
        public void execute() throws InterruptedException {
           
        }

        public boolean isCompleted() {
            // Stage One completion condition
            return false;
        }
    }

    private class StageTwo implements Stage {
        public void execute() throws InterruptedException {
            // Stage Two execution logic
        }

        public boolean isCompleted() {
            // Stage Two completion condition
            return false;
        }
    }
}

An example StageOne that checks if the player is moving, and completes if they are:

    private class StageOne implements Stage {
        public void execute() throws InterruptedException {
            // Check if the player is moving
            if (!myPlayer().isMoving()) {
                debug("Player is not moving...");                            
            } else {
            	debug("Player is moving..."); 
            }
        }

        public boolean isCompleted() {
            // Completion condition: Player is moving
        	
            return myPlayer().isMoving();
        }
    }

 

Edited by xlDino
Link to comment
Share on other sites

1 hour ago, xlDino said:

All thoughts or improvements are welcome!

1. initStages(): This method initializes the stages of the bot. It creates an array of "Stage" objects and assigns it to the "stages" variable. In this example, there are two stages defined: StageOne and StageTwo.

2. nextStage(): This method determines the next stage that should be executed based on the current stage. It searches for the index of the current stage in the "stages" array and returns the stage at the next index. If there are no more stages, it returns null, indicating that the script has completed.

3. onStart(): calls the initStages() method to initialize the stages and assigns the first stage to the currentStage variable upon bot startup.

4. onLoop():  Inside this method, the current stage is checked, and if it exists, its logic is executed by calling the `execute()` method of the current stage. After executing the logic, the isCompleted() method of the current stage is checked to determine if the stage is completed. If the stage is completed, the next stage is transitioned to by calling the nextStage() method. If there are no more stages, the script restarts by re-initializing the stages and assigning the first stage to the currentStage variable. The method returns an integer value that represents the delay between iterations of the loop.


import java.awt.Graphics2D;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(name = "MyScript", author = "YourName", version = 1.0, info = "", logo = "")
public class Main extends Script {

    boolean debugMode = true;
    
    private Stage previousStage;
    private Stage currentStage;
    private Stage[] stages;

    private void debug(String message) {
    	if(debugMode){log("[DEBUG] " + message);}
    }
    
    
    private interface Stage {
        void execute() throws InterruptedException;

        boolean isCompleted();
    }

    private void initStages() {
        stages = new Stage[]{
                new StageOne(),
                new StageTwo()
        };
    }
    
    private Stage nextStage() {
        int currentIndex = -1;

        for (int i = 0; i < stages.length; i++) {
            if (stages[i] == currentStage) {
                currentIndex = i;
                break;
            }
        }

        if (currentIndex != -1 && currentIndex + 1 < stages.length) {
            return stages[currentIndex + 1];
        }

        return null; // No more stages, script completed
    }

    public void onStart() {
        // Initialize stages
        initStages();
        // Start with the first stage
        currentStage = stages[0];
    }

    public void onExit() {
        // Cleanup code
    }

    public void onPaint(Graphics2D g) {
        
    }

    public int onLoop() throws InterruptedException {
    	
        if (currentStage != null) {
            boolean stageChanged = currentStage != previousStage;
            if (stageChanged) {
                debug("[StageHandler] Current Stage: " + currentStage);
                previousStage = currentStage;
            }
            currentStage.execute();
            if (currentStage.isCompleted()) {
                currentStage = nextStage();
                previousStage = null; // Reset previousStage when transitioning to a new stage
            }
        }

        if (currentStage == null) {
        	debug("Stage is null! Restarting stages...");
            initStages();
            currentStage = stages[0];
            previousStage = null; // Reset previousStage when restarting stages
        }

        return random(500, 1000); // Set a delay between iterations
    }
    
    private class StageOne implements Stage {
        public void execute() throws InterruptedException {
           
        }

        public boolean isCompleted() {
            // Stage One completion condition
            return false;
        }
    }

    private class StageTwo implements Stage {
        public void execute() throws InterruptedException {
            // Stage Two execution logic
        }

        public boolean isCompleted() {
            // Stage Two completion condition
            return false;
        }
    }
}

An example StageOne that checks if the player is moving, and completes if they are:

    private class StageOne implements Stage {
        public void execute() throws InterruptedException {
            // Check if the player is moving
            if (!myPlayer().isMoving()) {
                debug("Player is not moving...");                            
            } else {
            	debug("Player is moving..."); 
            }
        }

        public boolean isCompleted() {
            // Completion condition: Player is moving
        	
            return myPlayer().isMoving();
        }
    }

 

 

If you were looking to make an agility script with this you could use an iterator to iterate through the obstacles

alternatively you could use a switch statement

using code example from Spacecats sand crab killer:

Original Source- 

 

public State getState() {
    /*
    if (this.startScript == 0) {
        return State.INITIALIZING;
    }
     */
    if (resetSpot.contains(myPosition())){
        return State.FIGHTING;
    }
    if (!this.getInventory().contains(this.food)) {
        return State.BANKING;
    }
    if (this.myPlayer().isHitBarVisible() || this.myPlayer().isUnderAttack()) {
        return State.FIGHTING;
    }
    if (SpaceCats_CrabKiller.timer > SpaceCats_CrabKiller.waitingDelay) {
        return State.RESET;
    }
    return State.WAITING;
}
public int onLoop() throws InterruptedException {

switch(this.getState()){

case stage1:

    // stage 1 stuff here

    break;

case stage2;

    /stage 2 stuff here

    break;

 

return 200;

}

}

 

Link to comment
Share on other sites

  • 5 months later...

This works under the assumption that Stage N will be guaranteed to setup the conditions that Stage N + 1 can solve. This will work for some activities but not others. 

EX: Fletching Logs -> bow (u)s (Works)

Stage 1: Open Bank, Deposit (bow (u)s if needed)

Stage 2: Withdraw Items. (Assumes that the bank is open)

Stage 3: Close Bank

Stage 4: use knife on Log, wait until no more logs. 

 

Ex: Rooftop Agility (May not work)

Stage 1 - N: Do some agility obstacle, Finish at the end.

What if at Stage 3 you fall off the rooftop. Stage 4 which expects you to still be on the rooftop now no longer works. 

 

You can solve these issues by...

  • Add boolean canRunStage() to Stage interface, implement in all your stages. 
  • If canRunStage returns false, loop through your stage, get the first one stage where canRunStage returns true. Start your stages from there. 
  • It may be better to have your Stages be in a circular LinkedList instead since you are going to iterate through them again starting at Stage1.
Edited by PayPalMeRSGP
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...