Jump to content
sellout_89

Code Review (Complete Noob)

Recommended Posts

Hey just wanted to get people opinions on a very basic script. Are there better ways to structure the code, better ways to go about what this script is attempting(Red chins)?

 

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;

@ScriptManifest(author = "Sellout", name = "Red chin", info = "Basic red chin script", version = 0.1, logo = "")
public final class RedChin extends Script  {
    private long startTime;

    Area top = new Area(2496, 2907, 2496, 2907);
    Area left = new Area(2495, 2906, 2495, 2906);
    Area right = new Area(2497, 2906, 2497, 2906);
    Area bottom = new Area(2496, 2905, 2496, 2905);

    Area[] trapPositions = {top, left, right, bottom};

    @Override
    public final int onLoop() throws InterruptedException {
        resetTraps();
        resetDecayed();
        collectChins();
        layTraps();
        return random(500, 1250);
    }

    @Override
    public final void onStart() {
        log("This will be printed to the logger when the script starts");
        startTime = System.currentTimeMillis();
    }

    @Override
    public void onPaint(final Graphics2D g) {
        Point mP = getMouse().getPosition();
        final long runTime = System.currentTimeMillis() - startTime;

        g.setColor(Color.RED);
        g.drawLine(mP.x - 5, mP.y + 5, mP.x + 5, mP.y - 5);
        g.drawLine(mP.x + 5, mP.y + 5, mP.x - 5, mP.y - 5);

        g.drawString(formatTime(runTime), 5, 336);
    }

    @Override
    public final void onExit() {
        log("This will be printed to the logger when the script exits");
    }

    public final void conditionalSleep(int time){
        new ConditionalSleep(time) {
            @Override
            public boolean condition() {
                return false;
            }
        }.sleep();
    }

    public final String formatTime(final long ms){
        long s = ms / 1000, m = s / 60, h = m / 60;
        s %= 60; m %= 60; h %= 24;
        return String.format("%02d:%02d:%02d", h, m, s);
    }

    public void layTraps(){
        for (Area position:trapPositions) {
            RS2Object trapTwo = getObjects().closest(position, "Shaking box", "Box trap");
            GroundItem trap = getGroundItems().closest(position, "Box trap");
            if(trapTwo == null && trap == null){
                getWalking().webWalk(position);
                conditionalSleep(random(1250, 1500));
                getInventory().interact("Lay", "Box trap");
                conditionalSleep(random(4500, 4750));
            }
        }
    }

    public void resetTraps(){
        for (Area position:trapPositions) {
            RS2Object trap = getObjects().closest(position, "Box trap");
            if(trap != null && trap.getId() == 9385) {
                trap.interact("Reset");
                conditionalSleep(random(8000, 8200));
            }
        }
    }

    public void collectChins(){
        for (Area position:trapPositions) {
            RS2Object trap = getObjects().closest(position,"Shaking box");
            if(trap != null && trap.getId() == 9383){
                trap.interact("Check");
                conditionalSleep(random(2250, 2500));
            }
        }
    }

    public void resetDecayed(){
        for (Area position:trapPositions) {
            GroundItem trap = getGroundItems().closest(position, "Box trap");
            if(trap != null) {
                trap.interact("Lay");
                conditionalSleep(random(7750, 8000));
            }
        }
    }

}

 

Edited by sellout_89
Link to comment
Share on other sites

public final void conditionalSleep(int time){
        new ConditionalSleep(time) {
            @Override
            public boolean condition() {
                return false;
            }
        }.sleep();
    }

You don't use conditional sleeps correctly. Any time you call this, all you do is sleep (do nothing) for <time> milliseconds, if you want that functionality use sleep(<time in MS>). Conditional sleeps until its "inner condition function" ( aka abstract condition method) returns true. For example I may want in a red chins script to sleep until the condition that a box trap that I've placed has been triggered by a chin resolves to true.

Furthermore on the matter of whether the box trap is yours, your script needs to register if your character has placed the trap (trap is removed from inventory and placed on the ground). If your character did, store the trap's position in some data structure. When your script runs the part of your code that checks triggered traps, first check whether said trap is yours. Only interact with the trap if the trap's position is stored your data structure, then remove the trap's registration.

 

 

Link to comment
Share on other sites

1 hour ago, PayPalMeRSGP said:

 

Furthermore on the matter of whether the box trap is yours, your script needs to register if your character has placed the trap (trap is removed from inventory and placed on the ground). If your character did, store the trap's position in some data structure. When your script runs the part of your code that checks triggered traps, first check whether said trap is yours. Only interact with the trap if the trap's position is stored your data structure, then remove the trap's registration.

 

 

 

You can't forget about all the cases though. This is what makes it challenging. Actually knowing it is your trap is different than saying if your trap is located on a square you specified. For instance, if you get your spot crashed while you are laying a trap, how will you render which one is your trap and which one is not, because a trap is placed regardless? Or handling moving locations if the spot becomes a nulled tile. Or if the trap vanishes. Or if your trap failed while placing a trap. There are so many of these different scenarios it is comical. 

  • Like 1
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.

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