Jump to content

[Snippet] Autocast API


Botre

Recommended Posts

Currently only supports the normal spellbook.

 

 

Examples

package org.botre.magic;

import org.osbot.rs07.api.ui.MagicSpell;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;

//Script manifest...
public class ExampleScript extends Script {

	private Autocast auto = new Autocast(this);
	
	@Override
	public int onLoop() throws InterruptedException {

		/*
		 * These are radom non-dependant examples.
		 */
		
		// Autocasts the fireblast spell in non-defensive mode
		auto.autocast(AutocastSpell.FIRE_BLAST, false);
		
		// Autocast the wind bolt spell in defensive mode
		auto.autocast(AutocastSpell.WIND_BOLT, true);
		
		// Stop the script immediately if in defensive mode.
		if(auto.isDefensiveMode()) {
			stop();
		}
		
		// Get the difference between the level required to autocast the...
		// spell you are autocasting and your static magic level (a bit arbitrary).
		MagicSpell spell = auto.getAutocastSpell();
		if(spell != null) {
			int difference = Math.abs(spell.getRequiredLevel() - getSkills().getStatic(Skill.MAGIC));
		}
		
		return 500;
	}

}

Autocast

package org.botre.magic;

import java.awt.Point;

import org.botre.tab.TabHotkey;
import org.osbot.rs07.api.ui.MagicSpell;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.input.mouse.WidgetDestination;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.ConditionalLoop;
import org.osbot.rs07.utility.ConditionalSleep;

public class Autocast {

	public static final int AUTOCAST_SPELL_CONFIG = 108;
	public static final int AUTOCAST_MODE_CONFIG = 439; // 0 = regular, 256 = defensive
	public static final Point DEFENSIVE_AUTOCAST_BUTTON_POSITION = new Point(650, 280);
	public static final Point REGULAR_AUTOCAST_BUTTON_POSITION = new Point(651, 333);
	
	private MethodProvider ctx;
	
	public Autocast(MethodProvider ctx) {
		this.ctx = ctx;
	}
	
	public boolean isAutocasting() {
		return ctx.getConfigs().get(AUTOCAST_SPELL_CONFIG) != 0;
	}
	
	public boolean isAutocasting(AutocastSpell auto) {
		return ctx.getConfigs().get(AUTOCAST_SPELL_CONFIG) == auto.getConfigValue();
	}
	
	public boolean isAutocasting(AutocastSpell auto, boolean defensive) {
		return ctx.getConfigs().get(AUTOCAST_SPELL_CONFIG) == auto.getConfigValue() && defensive == isDefensiveMode();
	}
	
	public boolean isRegularMode() {
		return ctx.getConfigs().get(AUTOCAST_MODE_CONFIG) == 0;
	}
	
	public boolean isDefensiveMode() {
		return ctx.getConfigs().get(AUTOCAST_MODE_CONFIG) == 256;
	}
	
	public MagicSpell getAutocastSpell() {
		AutocastSpell auto = AutocastSpell.forConfigValue(ctx.getConfigs().get(AUTOCAST_SPELL_CONFIG));
		if(auto == null) return null;
		return auto.getSpell();
	}
	
	public boolean isAutocastPanelOpen() {
		RS2Widget panel = ctx.getWidgets().get(201, 0);
		return panel != null && panel.isVisible();
	}
	
	@SuppressWarnings("unchecked")
	public boolean openAutocastPanel(boolean defensive) {
		new ConditionalLoop(ctx.getBot(), 10) {
			@Override
			public boolean condition() {
				return !isAutocastPanelOpen();
			};
			@Override
			public int loop() {
				if(TabHotkey.COMBAT.openTab(ctx)) {
					RS2Widget button = ctx.getWidgets().singleFilter(ctx.getWidgets().getAll(), w -> {
						return w != null && w.isVisible() && w.getMessage() != null && w.getMessage().equals("Spell") && w.getPosition().equals(defensive ? DEFENSIVE_AUTOCAST_BUTTON_POSITION : REGULAR_AUTOCAST_BUTTON_POSITION);
					});
					if(button != null) {
						WidgetDestination destination = new WidgetDestination(ctx.getBot(), button);
						if(ctx.getMouse().click(destination)) {
							new ConditionalSleep(2400, 200) {
								@Override
								public boolean condition() throws InterruptedException {
									return !button.isVisible();
								}
							}.sleep();
						}
					}
				}
				return MethodProvider.random(50, 150);
			}
		}.start();
		return isAutocastPanelOpen();
	}

	@SuppressWarnings("unchecked")
	public boolean closeAutocastPanel() {
		new ConditionalLoop(ctx.getBot(), 10) {
			@Override
			public boolean condition() {
				return isAutocastPanelOpen();
			};
			@Override
			public int loop() {
				RS2Widget cancel = ctx.getWidgets().singleFilter(ctx.getWidgets().getAll(), w -> {
					return w != null && w.isVisible() && w.getMessage() != null && w.getMessage().equals("Cancel");
				});
				if(cancel != null) {
					WidgetDestination destination = new WidgetDestination(ctx.getBot(), cancel);
					if(ctx.getMouse().click(destination)) {
						new ConditionalSleep(2400, 200) {
							@Override
							public boolean condition() throws InterruptedException {
								return !cancel.isVisible();
							}
						}.sleep();
					}
				}
				return MethodProvider.random(50, 150);
			}
		}.start();
		return !isAutocastPanelOpen();
	}
	
	public boolean autocast(AutocastSpell spell, boolean defensive) {
		new ConditionalLoop(ctx.getBot(), 10) {
			@Override
			public boolean condition() {
				return spell != null ? !isAutocasting(spell) : isAutocasting();
			};
			@SuppressWarnings("unchecked")
			@Override
			public int loop() {
				if(isAutocastPanelOpen()) {
					if(spell == null) {
						closeAutocastPanel();
					}
					else {
						if(defensive != isDefensiveMode()) {
							closeAutocastPanel();
						}
						else {
							RS2Widget button = ctx.getWidgets().singleFilter(ctx.getWidgets().getAll(), w -> {
								if(w != null && w.isVisible() && w.getInteractActions() != null) {
									String name = spell.getSpell().toString().replace("_", " ");
									for (String action : w.getInteractActions()) if(action != null && action.equalsIgnoreCase(name)) return true;
								}
								return false;
							});
							if(button != null) {
								WidgetDestination destination = new WidgetDestination(ctx.getBot(), button);
								if(ctx.getMouse().click(destination)) {
									new ConditionalSleep(2400, 200) {
										@Override
										public boolean condition() throws InterruptedException {
											return !button.isVisible();
										}
									}.sleep();
								}
							}
						}
					}
				}
				else {
					openAutocastPanel(defensive);
				}
				return MethodProvider.random(50, 150);
			}
			
		}.start();
		return spell != null ? isAutocasting(spell) : !isAutocasting();
	}
	
}

AutocastSpell

 

This is a MagicSpell wrapper storing the config value for a MagicSpell.

package org.botre.magic;

import org.osbot.rs07.api.ui.MagicSpell;
import org.osbot.rs07.api.ui.Spells.NormalSpells;

public enum AutocastSpell {

	WIND_STRIKE(NormalSpells.WIND_STRIKE, 3),
	WATER_STRIKE(NormalSpells.WATER_STRIKE, 5),
	EARTH_STRIKE(NormalSpells.EARTH_STRIKE, 7),
	FIRE_STRIKE(NormalSpells.FIRE_STRIKE, 9),
	WIND_BOLT(NormalSpells.WIND_BOLT, 11),
	WATER_BOLT(NormalSpells.WATER_BOLT, 13),
	EARTH_BOLT(NormalSpells.EARTH_BOLT, 15),
	FIRE_BOLT(NormalSpells.FIRE_BOLT, 17),
	WIND_BLAST(NormalSpells.WIND_BLAST, 19),
	WATER_BLAST(NormalSpells.WATER_BLAST, 21),
	EARTH_BLAST(NormalSpells.EARTH_BLAST, 23),
	FIRE_BLAST(NormalSpells.FIRE_BLAST, 25),
	WIND_WAVE(NormalSpells.WIND_WAVE, 27),
	WATER_WAVE(NormalSpells.WATER_WAVE, 29),
	EARTH_WAVE(NormalSpells.EARTH_WAVE, 31),
	FIRE_WAVE(NormalSpells.FIRE_WAVE, 33);
	
	private MagicSpell spell;
	private int configValue;
	
	private AutocastSpell(MagicSpell spell, int configValue) {
		this.spell = spell;
		this.configValue = configValue;
	}
	
	public MagicSpell getSpell() {
		return spell;
	}
	
	public int getConfigValue() {
		return configValue;
	}
	
	public static AutocastSpell forSpell(MagicSpell spell) {
		for (AutocastSpell a : values()) {
			if(a.spell == spell) return a;
		}
		return null;
	}
	
	public static AutocastSpell forConfigValue(int configValue) {
		for (AutocastSpell a : values()) {
			if(a.configValue == configValue) return a;
		}
		return null;
	}
	
}
  • Like 9
Link to comment
Share on other sites

  • 2 months later...
  • 10 months later...
  • 4 weeks later...

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