Jump to content

Lag with my scripts


Mikkoz

Recommended Posts

Hello. I recently started making my own scripts due to usual bugs in other people's scripts.

However, I have noticed that every time I run my scripts, my client starts lagging FPS wise.

I'm not getting any errors in logger so what could cause this?

 

Typically caused by inefficient code. Without a snippet tho, we can't help much.

 

  • Like 2
Link to comment
Share on other sites

Hmm, well here's one of them it cuts yew at Catherby and banks it.

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


import java.awt.*;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;


@ScriptManifest(name = "YewCutter", author = "Mikkoz", version = 1.0, info = "Catherby Yew Chopper", logo = "")
public class MainClass extends Script {

    private enum State {
        CHOP, BANK
    }

    private static final Area YEW_AREA = new Area(2767, 3424, 2752, 3435);
    private static final Area BANK_AREA = new Area(2808, 3440, 2810, 3441);

    private long timeBegan;
    private int beginningXP;
    private int currentLevel;
    private int beginningLevel;
    private int itemsMade;
    private int costOfItem;
    private int beginningXp;
    private long timeTNL;


    private String ft(long duration) {
        String res = "";
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        long hours = TimeUnit.MILLISECONDS.toHours(duration) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
        if (days == 0) {
            res = (hours + ":" + minutes + ":" + seconds);
        } else {
            res = (days + ":" + hours + ":" + minutes + ":" + seconds);
        }
        return res;
    }

    final int[] XP_TABLE =
            {
                    0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154,
                    1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018,
                    5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833,
                    16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
                    41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721,
                    101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254,
                    224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428,
                    496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895,
                    1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068,
                    2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294,
                    4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614,
                    8771558, 9684577, 10692629, 11805606, 13034431, 200000000
            };

    private State getState() {
        if (inventory.isFull())
            return State.BANK;
        return State.CHOP;
    }


    public void AntiBan() throws InterruptedException {
        switch (random(1, 50)) {
            case 1:
                camera.getCamera().moveYaw(random(10, 50));
                break;
            case 2:
                camera.getCamera().movePitch(random(10, 50));
                break;
            case 3:
                getInventory().getTabs().open(Tab.SKILLS);
                getSkills().hoverSkill(Skill.MINING);
                sleep(random(2000, 4000));
                break;
            case 4:
                mouse.getMouse().moveOutsideScreen();
                sleep(random(5000, 12000));
                break;
            case 5:
                RS2Object yewtree = objects.closest(YEW_AREA, "Rocks");
                getCamera().toEntity(yewtree);
                break;
            case 6:
                getInventory().getTabs().open(Tab.FRIENDS);
                sleep(random(500, 2000));
                break;
            case 7:
                getMouse().moveRandomly();
                sleep(random(1000, 1500));
                break;
            case 8:
                getMouse().moveRandomly();
                sleep(random(2000, 2500));
                break;
            case 9:
                camera.getCamera().movePitch(35 + random(1, 30));
                break;
            case 10:
                camera.getCamera().moveYaw(50 + random(1, 125));
        }
        sleep(random(500, 1000));
        getInventory().getTabs().open(Tab.INVENTORY);
    }


    @Override
    public void onStart() {
        log("Welcome to Yew Cutter!");
        timeBegan = System.currentTimeMillis();
        beginningXP = skills.getExperience(Skill.WOODCUTTING);
        beginningLevel = skills.getStatic(Skill.WOODCUTTING);
        timeBegan = System.currentTimeMillis();
        beginningXp = skills.getExperience(Skill.WOODCUTTING);
        timeTNL = 0;
        costOfItem = 437;
    }

    @Override
    public void onExit() {
        log("Thanks for using Yew Cutter!");
    }


    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
            case CHOP:
                RS2Object yewtree = objects.closest(YEW_AREA, 7419);
                if (!yewtree.isVisible() && !yewtree.isOnScreen()) {
                    walking.webWalk(YEW_AREA);
                }
                if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
                    camera.getCamera().objects.closest("Yew");
                    sleep(random(500, 1000));
                    yewtree.interact("Chop down");
                    getMouse().moveRandomly();
                    AntiBan();
                    if (myPlayer().isMoving() && myPlayer().isAnimating()) {
                        sleep(random(3000, 6000));
                    }
                } else {
                    break;
                }
                break;
            case BANK:

                if (inventory.isFull()) {
                    if (!myPosition().equals(BANK_AREA)) {
                        walking.getLocalWalker().walk((random(2768, 2772)), (random(3429, 3434)));
                        sleep(random(1000, 2000));
                        walking.webWalk(BANK_AREA);
                        sleep(random(500, 1000));
                    }
                }
                    camera.getCamera().objects.closest("Bank booth");
                    sleep(random(500, 1000));
                    objects.closest("Bank booth").interact("Bank");
                    sleep(random(1000, 2500));
                    if (bank.isOpen()) {
                        bank.depositAllExcept("Dragon axe");
                        itemsMade += 27;
                        bank.close();
                        sleep(random(500, 1000));
                    }
                    if (!getInventory().isFull()) ;
                    return random(250, 500);
        }
        return random(2000, 4000);
                }


    @Override
    public void onPaint(Graphics2D g)
    {
        Graphics2D gr = g;
        long timeRan = System.currentTimeMillis() - this.timeBegan;
        int currentXp = skills.getExperience(Skill.WOODCUTTING);
        int xpGained;
        int levelsGained = currentLevel - beginningLevel;
        int gpGained = itemsMade * costOfItem;
        int totalGpGained = gpGained / 1000;
        currentXp = skills.getExperience(Skill.WOODCUTTING);
        currentLevel = skills.getStatic(Skill.WOODCUTTING);
        xpGained = currentXp - beginningXp;
        gpGained = xpGained / 175 * costOfItem;
        int xpPerHour = (int) (xpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D));
        double nextLevelXp = XP_TABLE[currentLevel + 1];
        double xpTillNextLevel = nextLevelXp - currentXp;
        if (xpGained >= 1)
        {
            timeTNL = (long) ((xpTillNextLevel / xpPerHour) * 3600000);
        }
        DecimalFormat df = new DecimalFormat("#");
        g.drawString("Runtime: " + ft(timeRan), 330, 319);
        g.drawString("XP Gained: " + xpGained, 168, 319);
        g.drawString("Current LVL:" + currentLevel, 15, 300);
        g.drawString("Levels Gained: " + levelsGained, 15, 319);
        g.drawString("Profit Made:  " + df.format(gpGained), 330, 300);
        g.drawString("Time to Level: " + ft(timeTNL), 168, 300);




    }


}

The lag is kind of like every 1 seconds my client freezes.

Link to comment
Share on other sites

Hmm, well here's one of them it cuts yew at Catherby and banks it.

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


import java.awt.*;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;


@ScriptManifest(name = "YewCutter", author = "Mikkoz", version = 1.0, info = "Catherby Yew Chopper", logo = "")
public class MainClass extends Script {

    private enum State {
        CHOP, BANK
    }

    private static final Area YEW_AREA = new Area(2767, 3424, 2752, 3435);
    private static final Area BANK_AREA = new Area(2808, 3440, 2810, 3441);

    private long timeBegan;
    private int beginningXP;
    private int currentLevel;
    private int beginningLevel;
    private int itemsMade;
    private int costOfItem;
    private int beginningXp;
    private long timeTNL;


    private String ft(long duration) {
        String res = "";
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        long hours = TimeUnit.MILLISECONDS.toHours(duration) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
        if (days == 0) {
            res = (hours + ":" + minutes + ":" + seconds);
        } else {
            res = (days + ":" + hours + ":" + minutes + ":" + seconds);
        }
        return res;
    }

    final int[] XP_TABLE =
            {
                    0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154,
                    1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018,
                    5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833,
                    16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
                    41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721,
                    101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254,
                    224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428,
                    496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895,
                    1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068,
                    2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294,
                    4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614,
                    8771558, 9684577, 10692629, 11805606, 13034431, 200000000
            };

    private State getState() {
        if (inventory.isFull())
            return State.BANK;
        return State.CHOP;
    }


    public void AntiBan() throws InterruptedException {
        switch (random(1, 50)) {
            case 1:
                camera.getCamera().moveYaw(random(10, 50));
                break;
            case 2:
                camera.getCamera().movePitch(random(10, 50));
                break;
            case 3:
                getInventory().getTabs().open(Tab.SKILLS);
                getSkills().hoverSkill(Skill.MINING);
                sleep(random(2000, 4000));
                break;
            case 4:
                mouse.getMouse().moveOutsideScreen();
                sleep(random(5000, 12000));
                break;
            case 5:
                RS2Object yewtree = objects.closest(YEW_AREA, "Rocks");
                getCamera().toEntity(yewtree);
                break;
            case 6:
                getInventory().getTabs().open(Tab.FRIENDS);
                sleep(random(500, 2000));
                break;
            case 7:
                getMouse().moveRandomly();
                sleep(random(1000, 1500));
                break;
            case 8:
                getMouse().moveRandomly();
                sleep(random(2000, 2500));
                break;
            case 9:
                camera.getCamera().movePitch(35 + random(1, 30));
                break;
            case 10:
                camera.getCamera().moveYaw(50 + random(1, 125));
        }
        sleep(random(500, 1000));
        getInventory().getTabs().open(Tab.INVENTORY);
    }


    @Override
    public void onStart() {
        log("Welcome to Yew Cutter!");
        timeBegan = System.currentTimeMillis();
        beginningXP = skills.getExperience(Skill.WOODCUTTING);
        beginningLevel = skills.getStatic(Skill.WOODCUTTING);
        timeBegan = System.currentTimeMillis();
        beginningXp = skills.getExperience(Skill.WOODCUTTING);
        timeTNL = 0;
        costOfItem = 437;
    }

    @Override
    public void onExit() {
        log("Thanks for using Yew Cutter!");
    }


    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
            case CHOP:
                RS2Object yewtree = objects.closest(YEW_AREA, 7419);
                if (!yewtree.isVisible() && !yewtree.isOnScreen()) {
                    walking.webWalk(YEW_AREA);
                }
                if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
                    camera.getCamera().objects.closest("Yew");
                    sleep(random(500, 1000));
                    yewtree.interact("Chop down");
                    getMouse().moveRandomly();
                    AntiBan();
                    if (myPlayer().isMoving() && myPlayer().isAnimating()) {
                        sleep(random(3000, 6000));
                    }
                } else {
                    break;
                }
                break;
            case BANK:

                if (inventory.isFull()) {
                    if (!myPosition().equals(BANK_AREA)) {
                        walking.getLocalWalker().walk((random(2768, 2772)), (random(3429, 3434)));
                        sleep(random(1000, 2000));
                        walking.webWalk(BANK_AREA);
                        sleep(random(500, 1000));
                    }
                }
                    camera.getCamera().objects.closest("Bank booth");
                    sleep(random(500, 1000));
                    objects.closest("Bank booth").interact("Bank");
                    sleep(random(1000, 2500));
                    if (bank.isOpen()) {
                        bank.depositAllExcept("Dragon axe");
                        itemsMade += 27;
                        bank.close();
                        sleep(random(500, 1000));
                    }
                    if (!getInventory().isFull()) ;
                    return random(250, 500);
        }
        return random(2000, 4000);
                }


    @Override
    public void onPaint(Graphics2D g)
    {
        Graphics2D gr = g;
        long timeRan = System.currentTimeMillis() - this.timeBegan;
        int currentXp = skills.getExperience(Skill.WOODCUTTING);
        int xpGained;
        int levelsGained = currentLevel - beginningLevel;
        int gpGained = itemsMade * costOfItem;
        int totalGpGained = gpGained / 1000;
        currentXp = skills.getExperience(Skill.WOODCUTTING);
        currentLevel = skills.getStatic(Skill.WOODCUTTING);
        xpGained = currentXp - beginningXp;
        gpGained = xpGained / 175 * costOfItem;
        int xpPerHour = (int) (xpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D));
        double nextLevelXp = XP_TABLE[currentLevel + 1];
        double xpTillNextLevel = nextLevelXp - currentXp;
        if (xpGained >= 1)
        {
            timeTNL = (long) ((xpTillNextLevel / xpPerHour) * 3600000);
        }
        DecimalFormat df = new DecimalFormat("#");
        g.drawString("Runtime: " + ft(timeRan), 330, 319);
        g.drawString("XP Gained: " + xpGained, 168, 319);
        g.drawString("Current LVL:" + currentLevel, 15, 300);
        g.drawString("Levels Gained: " + levelsGained, 15, 319);
        g.drawString("Profit Made:  " + df.format(gpGained), 330, 300);
        g.drawString("Time to Level: " + ft(timeTNL), 168, 300);




    }


}

The lag is kind of like every 1 seconds my client freezes.

 

My guess is that it's throwing a ton of NullPointerException's due to your complete lack of null checks. Check the client logger for details on where they're thrown.

 

Link to comment
Share on other sites

My guess is that it's throwing a ton of NullPointerException's due to your complete lack of null checks. Check the client logger for details on where they're thrown.

 

Thank you very much! I added some null checks and it seems to be working now (uses about 10% CPU!)(talking about another script)

However I'm still getting the FPS spike every 1 sec?

Edited by Mikkoz
Link to comment
Share on other sites

Thank you very much! I added some null checks and it seems to be working now (uses about 10% CPU!)(talking about another script)

However I'm still getting the FPS spike every 1 sec?

I have this too... I'm actually starting to think it's just OSBot. Regardless of what script I use (other ppls or my own), I observe periodic lags that last only briefly.

Link to comment
Share on other sites

Thank you very much! I added some null checks and it seems to be working now (uses about 10% CPU!)(talking about another script)

However I'm still getting the FPS spike every 1 sec?

 

 

I have this too... I'm actually starting to think it's just OSBot. Regardless of what script I use (other ppls or my own), I observe periodic lags that last only briefly.

 

 

This is caused by too much "Memory Pressure" to the JVM heap. Read up on it if your interested, but basically every time you fill the available heap (which happens often), a "Garbage Collection" (aka GC) routine is ran to clear up some memory. This is most likely the lag, as it takes over the thread execution while it clears space in the memory. The larger the heap, the longer to clean it up as well (500mb or less is best size for heap, defaults to 500mb).

Edited by Lemons
  • Like 1
Link to comment
Share on other sites

This is caused by too much "Memory Pressure" to the JVM heap. Read up on it if your interested, but basically every time you fill the available heap (which happens often), a "Garbage Collection" (aka GC) routine is ran to clear up some memory. This is most likely the lag, as it takes over the thread execution while it clears space in the memory. The larger the heap, the longer to clean it up as well (500mb or less is best size for heap, defaults to 500mb).

 

This crossed my mind, but I haven't bothered to profile it yet (mostly because I don't really care about the lag)

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