Jump to content

Different loops


Kramnik

Recommended Posts

Hello, I am working on my first basic script which is fighter. Mainly it is working and doying what I need most - changing combat style according to levels, but the thing is that the script tries to change combat stat every loop. Tried to change sleep time but it either does nothing or messes up whole loop, then the script only attacks new monster when its time to check if combat style is good for level.

 

import com.sun.xml.internal.bind.annotation.OverrideAnnotationOf;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.ui.RS2Widget;
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 org.osbot.rs07.utility.ConditionalSleep;

import java.util.Arrays;

@ScriptManifest(name = "First one", version = 1.0, info = "", author = "Kramnik", logo = "")
public class Main extends Script {
    String npcName;

    @Override
    public int onLoop() throws InterruptedException {

        if (npcName == null) {
            if (myPlayer().getInteracting() != null) {
                npcName = myPlayer().getInteracting().getName();
            } else {
                return 500;
            }
        }

        if (!myPlayer().isAnimating() && myPlayer().getInteracting() == null) {
            NPC next = getNpcs().closest(npc -> npc.getName().equals(npcName) && npc.isAttackable() && !npc.isUnderAttack() && npc.getInteracting() == null);

            if (next != null) {
                next.interact("Attack");
                new ConditionalSleep(5000) {
                    @Override
                    public boolean condition() throws InterruptedException {
                        return myPlayer().getInteracting() != null;
                    }
                }.sleep();

            }
        }



        int str = getSkills().getDynamic(Skill.STRENGTH);
        RS2Widget modeStr = getWidgets().get(593, 7);
        int atk = getSkills().getDynamic(Skill.ATTACK);
        RS2Widget modeAtk = getWidgets().get(593, 3);
        int def = getSkills().getDynamic(Skill.DEFENCE);
        RS2Widget modeDef = getWidgets().get(593, 15);

        int config = getConfigs().get(43);

            if (str < 20) {
                getTabs().open(Tab.ATTACK);
                sleep(1000);
                if (config != 1) {
                    modeStr.interact("Slash");
                    sleep(random(3000, 7000));
                }
                getTabs().open(Tab.INVENTORY);
            } else if (atk < 20) {
                getTabs().open(Tab.ATTACK);
                sleep(1000);
                if (config != 0) {
                    modeAtk.interact("Chop");
                    sleep(random(3000, 7000));
                }
                getTabs().open(Tab.INVENTORY);
            } else if (def < 20) {
                getTabs().open(Tab.ATTACK);
                sleep(1000);
                if (config != 3) {
                    modeDef.interact("Block");
                    sleep(random(3000, 7000));
                }
                getTabs().open(Tab.INVENTORY);
            } else {
                getTabs().open(Tab.ATTACK);
                sleep(1000);
                if (config != 1) {
                    modeStr.interact("Slash");
                    sleep(random(3000, 7000));
                }
                getTabs().open(Tab.INVENTORY);

                return 25000;

            }
            return 500;
        }
    }
}

 

  • Like 1
Link to comment
Share on other sites

Well, every loop, you are opening the combat tab and then opening the inventory tab, going back and forth between the two. You should set it so only on a level up message (the onMessage function) set a boolean for checking attack style, then setting that variable back to false after everything has been checked

 

Link to comment
Share on other sites

Try break your code down into separate routines and use a data structure for your attack styles:

if (defenceLevel < 20) {
	// need to train defence
	if (!isAttackStyleSelected(AttackStyle.DEFENCE)) {
		// need to change attack style
		if (setAttackStyle(AttackStyle.DEFENCE)) {
			// we are now training defence (yay)
		} else {
			// something f'd up?
			throw new InterruptedException("Something wen't wrong!");
		}
	}
}

via

static enum AttackStyle {
	
	ATTACK(0, "Slash"),
	STRENGTH(1, "Chop"),
	DEFENCE(3, "Block");
	
	private final int configValue;
	private final String action;
	
	private AttackStyle(int configValue, String action) {
		this.configValue = configValue;
		this.action = action;
	}
	
	public int getConfigValue() {
		return configValue;
	}
	
	public String getAction() {
		return action;
	}
	
	public static AttackStyle valueOf(int configValue) {
		AttackStyle value = null;
		for (AttackStyle next : values()) {
			if (next.configValue == configValue) {
				value = next;
				break;
			}
		}
		return value;
	}
}
	
private int getAttackStyleConfigValue() {
	return configs.get(43);
}
	
private boolean isAttackStyleSelected(AttackStyle attackStyle) {
	
	int configValue = getAttackStyleConfigValue();
	
	return attackStyle == AttackStyle.valueOf(configValue);
}
	
private boolean setAttackStyle(AttackStyle attackStyle) {
	
	boolean success = false;
	List<RS2Widget> styleButtons;
	ConditionalSleep conditionalSleep;
	
	if (tabs.open(Tab.ATTACK)) {

		styleButtons = widgets.containingActions(593, attackStyle.action);

		if (styleButtons != null && !styleButtons.isEmpty()) {

			if (styleButtons.get(0).interact(attackStyle.action)) {

				conditionalSleep = new ConditionalSleep(2500) { // 2.5 seconds

					@Override
					public boolean condition() throws InterruptedException {

						return getAttackStyleConfigValue() == attackStyle.configValue;
					}
				};

				success = conditionalSleep.sleep();
			}
		}
	}
	
	return success;
}

 

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