imancity Posted June 8, 2016 Share Posted June 8, 2016 I've been digging through the Magic API and can't figure it out... Quote Link to comment Share on other sites More sharing options...
Chris Posted June 8, 2016 Share Posted June 8, 2016 http://osbot.org/forum/topic/94684-snippet-autocast-api/ 1 Quote Link to comment Share on other sites More sharing options...
imancity Posted June 8, 2016 Author Share Posted June 8, 2016 http://osbot.org/forum/topic/94684-snippet-autocast-api/ Ohh sweet so there's no built-in one. So I added the Autocast class but it's giving me errors for the botre stuff, any ideas? Quote Link to comment Share on other sites More sharing options...
Acerd Posted June 8, 2016 Share Posted June 8, 2016 (edited) Here's my own autocaster I made: Autocaster.java import org.osbot.rs07.api.ui.Tab; import org.osbot.rs07.script.MethodProvider; public class Autocaster { private static final int AUTOCAST_CONFIG = 108; private CachedWidget openPanel = new CachedWidget(593, 25); private CachedWidget panel = new CachedWidget(548, 61); //private CachedWidget cancel = new CachedWidget(201, 0, 0); private CachedWidget spellWidget; private MethodProvider api; public Autocaster(MethodProvider api) { this.api = api; } public boolean isAutocasting() { return api.getConfigs().get(AUTOCAST_CONFIG) != 0; } public boolean isAutocasting(Items spell) { return api.getConfigs().get(AUTOCAST_CONFIG) == spell.getConfigValue(); } public void openPanel(Items spell) throws InterruptedException { if (api.getTabs().getOpen() != Tab.ATTACK) { TabHotkey.COMBAT.openTab(api); } else { if (openPanel.getWidget(api.getWidgets()) != null) { if (openPanel.getWidget(api.getWidgets()).interact()) { new CSleep(() -> isSpellVisible(spell), 2_500).sleep(); } } } } public void selectSpell(Items spell) { if (isSpellVisible(spell)) { if (spellWidget.getWidget(api.getWidgets()).interact()) { new CSleep(() -> isAutocasting(), 2_500).sleep(); } } } public void autoCastSpell(Items spell) throws InterruptedException { if (!isSpellVisible(spell)) openPanel(spell); else { selectSpell(spell); } } public boolean isPanelOpen() { return panel.getWidget(api.getWidgets()) != null; } public boolean isSpellVisible(Items spell) { spellWidget = new CachedWidget(spell.getRoot(), spell.getChild(), spell.getChild2()); return spellWidget.getWidget(api.getWidgets()) != null; } } CachedWidget.java import org.osbot.rs07.api.Widgets; import org.osbot.rs07.api.ui.RS2Widget; public class CachedWidget { private Integer parentID; private Integer childID; private Integer child2ID; private String text; private RS2Widget widget; public CachedWidget(final int parentID, final int childID, final int child2ID) { this.parentID = parentID; this.childID = childID; this.child2ID = child2ID; } public CachedWidget(final int parentID, final int childID) { this.parentID = parentID; this.childID = childID; } public CachedWidget(final String text) { this.text = text; } public RS2Widget getWidget(final Widgets widgets) { if (widget == null) cacheWidget(widgets); return widget; } private void cacheWidget(final Widgets widgets) { RS2Widget widget; if (text != null) widget = widgets.getWidgetContainingText(text); else if (child2ID != null) widget = widgets.get(parentID, childID, child2ID); else widget = widgets.get(parentID, childID); this.widget = widget; } } Items.java import org.osbot.rs07.api.ui.MagicSpell; import org.osbot.rs07.api.ui.RS2Widget; import org.osbot.rs07.api.ui.Spells.NormalSpells; import org.osbot.rs07.script.MethodProvider; public enum Items { AIR_STRIKE("Wind strike", NormalSpells.WIND_STRIKE, 201, 0, 1, 3), WATER_STRIKE("Water strike", NormalSpells.WATER_STRIKE, 201, 0, 2, 5), EARTH_STRIKE("Earth strike", NormalSpells.EARTH_STRIKE, 201, 0, 3, 7), FIRE_STRIKE("Fire strike", NormalSpells.FIRE_STRIKE, 201, 0, 4, 9), AIR_BOLT("Wind bolt", NormalSpells.WIND_BOLT, 201, 0, 5, 11), WATER_BOLT("Water bolt", NormalSpells.WATER_BOLT, 201, 0, 6, 13), EARTH_BOLT("Earth bolt", NormalSpells.EARTH_BOLT, 201, 0, 7, 15), FIRE_BOLT("Fire bolt", NormalSpells.FIRE_BOLT, 201, 0, 8, 17), AIR_BLAST("Wind blast", NormalSpells.WIND_BLAST, 201, 0, 9, 19), WATER_BLAST("Water blast", NormalSpells.WATER_BLAST, 201, 0, 10, 21), EARTH_BLAST("Earth blast", NormalSpells.EARTH_BLAST, 201, 0, 11, 23), FIRE_BLAST("Fire blast", NormalSpells.FIRE_BLAST, 201, 0, 12, 25), WIND_WAVE("Wind wave", NormalSpells.WIND_WAVE, 201, 0, 13, 27), WATER_WAVE("Water wave", NormalSpells.WATER_WAVE, 201, 0, 14, 29), EARTH_WAVE("Earth wave", NormalSpells.EARTH_WAVE, 201, 0, 15, 31), FIRE_WAVE("Fire wave", NormalSpells.FIRE_WAVE, 201, 0, 16, 33), IBAN_BLAST("Iban blast", NormalSpells.IBAN_BLAST), CRUMBLE_UNDEAD("Crumble undead", NormalSpells.CRUMBLE_UNDEAD, 201, 0, 0, 35), MAGIC_DART("Magic dart", NormalSpells.MAGIC_DART, 201, 0, 0, 37), CLAWS_OF_GUTHIX("Claws of Guthix", NormalSpells.CLAWS_OF_GUTHIX), SARADOMIN_STRIKE("Saradomin strike", NormalSpells.SARADOMIN_STRIKE), FLAMESS_OF_ZAMORAK("Flames of Zamorak", NormalSpells.FLAMES_OF_ZAMORAK), CONFUSE("Confuse", NormalSpells.CONFUSE), WEAKEN("Weaken", NormalSpells.WEAKEN), CURSE("Curse", NormalSpells.CURSE), STUN("Stun", NormalSpells.STUN); Items(String name, MagicSpell spell, int root, int child, int child2, int configValue) { this.name = name; this.magicSpell = spell; this.root = root; this.child = child; this.child2 = child2; this.configValue = configValue; } Items(String name, MagicSpell spell) { this.name = name; this.magicSpell = spell; } private MagicSpell magicSpell; private String name; private int root, child, child2, configValue; @Override public String toString() { return name; } public MagicSpell getMagicSpell() { return magicSpell; } public int getRoot() { return root; } public int getChild() { return child; } public int getChild2() { return child2; } public int getConfigValue() { return configValue; } public RS2Widget getWidget(MethodProvider api) { return api.getWidgets().get(getRoot(), getChild(), getChild2()); } } TabHotkey.java (@FrostBug) import java.awt.event.KeyEvent; import org.osbot.rs07.api.ui.Tab; import org.osbot.rs07.script.MethodProvider; public enum TabHotkey { COMBAT(Tab.ATTACK, 0, 1224), SKILLS(Tab.SKILLS, 1, 1224), QUEST(Tab.QUEST, 2, 1224), INVENTORY(Tab.INVENTORY, 3, 1224), EQUIPMENT(Tab.EQUIPMENT, 4, 1224), PRAYER(Tab.PRAYER, 5, 1224), MAGIC(Tab.MAGIC, 0, 1225), CLAN(Tab.CLANCHAT, 1, 1225), FRIENDS(Tab.FRIENDS, 2, 1225), IGNORE(Tab.IGNORES, 3, 1225), SETTINGS(Tab.SETTINGS, 4, 1225), EMOTES(Tab.EMOTES, 5, 1225), LOGOUT(Tab.LOGOUT, 1, 1226); private final Tab tab; private final int index; private final int register; private final static int[] KEYCODES = { -1, KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4, KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8, KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12, KeyEvent.VK_ESCAPE }; private TabHotkey(Tab tab, int index, int register) { this.tab = tab; this.index = index; this.register = register; } public Tab getTab() { return tab; } public int getHotkey(MethodProvider parent) { int config = parent.getConfigs().get(this.register); int kcIndex = (config >> (this.index * 5)) & 0b11111; return KEYCODES[kcIndex]; } public boolean openTab(MethodProvider parent) throws InterruptedException { if(!isAssigned(parent)) return false; int hkey = getHotkey(parent); parent.getKeyboard().pressKey(hkey); try { MethodProvider.sleep(MethodProvider.random(20, 50)); } finally { parent.getKeyboard().releaseKey(hkey); } return true; } public boolean isAssigned(MethodProvider parent) { return getHotkey(parent) != -1; } public static TabHotkey forTab(Tab tab) { for(TabHotkey thk : values()) { if(thk.getTab() == tab) { return thk; } } return null; } } CSleep: (@Explv) import java.util.function.BooleanSupplier; import org.osbot.rs07.utility.ConditionalSleep; public final class CSleep extends ConditionalSleep { private final BooleanSupplier condition; public CSleep(final BooleanSupplier condition, int timeout) { super(timeout); this.condition = condition; } @Override public boolean condition() throws InterruptedException { return condition.getAsBoolean(); } } Usage: private Autocaster caster = new Autocaster(this); private Items spell; //onloop if (!auto.isAutocasting()) { if (getMagic().canCast(NornalSpells.AIR_BLAST)) auto.autoCastSpell(spell); else stop(); } Edited June 8, 2016 by Acerd 1 Quote Link to comment Share on other sites More sharing options...
imancity Posted June 8, 2016 Author Share Posted June 8, 2016 Here's my own autocaster I made: Autocaster.java import org.osbot.rs07.api.ui.Tab; import org.osbot.rs07.script.MethodProvider; public class Autocaster { private static final int AUTOCAST_CONFIG = 108; private CachedWidget openPanel = new CachedWidget(593, 25); private CachedWidget panel = new CachedWidget(548, 61); //private CachedWidget cancel = new CachedWidget(201, 0, 0); private CachedWidget spellWidget; private MethodProvider api; public Autocaster(MethodProvider api) { this.api = api; } public boolean isAutocasting() { return api.getConfigs().get(AUTOCAST_CONFIG) != 0; } public boolean isAutocasting(Items spell) { return api.getConfigs().get(AUTOCAST_CONFIG) == spell.getConfigValue(); } public void openPanel(Items spell) throws InterruptedException { if (api.getTabs().getOpen() != Tab.ATTACK) { TabHotkey.COMBAT.openTab(api); } else { if (openPanel.getWidget(api.getWidgets()) != null) { if (openPanel.getWidget(api.getWidgets()).interact()) { new CSleep(() -> isSpellVisible(spell), 2_500).sleep(); } } } } public void selectSpell(Items spell) { if (isSpellVisible(spell)) { if (spellWidget.getWidget(api.getWidgets()).interact()) { new CSleep(() -> isAutocasting(), 2_500).sleep(); } } } public void autoCastSpell(Items spell) throws InterruptedException { if (!isSpellVisible(spell)) openPanel(spell); else { selectSpell(spell); } } public boolean isPanelOpen() { return panel.getWidget(api.getWidgets()) != null; } public boolean isSpellVisible(Items spell) { spellWidget = new CachedWidget(spell.getRoot(), spell.getChild(), spell.getChild2()); return spellWidget.getWidget(api.getWidgets()) != null; } } CachedWidget.java import org.osbot.rs07.api.Widgets; import org.osbot.rs07.api.ui.RS2Widget; public class CachedWidget { private Integer parentID; private Integer childID; private Integer child2ID; private String text; private RS2Widget widget; public CachedWidget(final int parentID, final int childID, final int child2ID) { this.parentID = parentID; this.childID = childID; this.child2ID = child2ID; } public CachedWidget(final int parentID, final int childID) { this.parentID = parentID; this.childID = childID; } public CachedWidget(final String text) { this.text = text; } public RS2Widget getWidget(final Widgets widgets) { if (widget == null) cacheWidget(widgets); return widget; } private void cacheWidget(final Widgets widgets) { RS2Widget widget; if (text != null) widget = widgets.getWidgetContainingText(text); else if (child2ID != null) widget = widgets.get(parentID, childID, child2ID); else widget = widgets.get(parentID, childID); this.widget = widget; } } Items.java import org.osbot.rs07.api.ui.MagicSpell; import org.osbot.rs07.api.ui.RS2Widget; import org.osbot.rs07.api.ui.Spells.NormalSpells; import org.osbot.rs07.script.MethodProvider; public enum Items { AIR_STRIKE("Wind strike", NormalSpells.WIND_STRIKE, 201, 0, 1, 3), WATER_STRIKE("Water strike", NormalSpells.WATER_STRIKE, 201, 0, 2, 5), EARTH_STRIKE("Earth strike", NormalSpells.EARTH_STRIKE, 201, 0, 3, 7), FIRE_STRIKE("Fire strike", NormalSpells.FIRE_STRIKE, 201, 0, 4, 9), AIR_BOLT("Wind bolt", NormalSpells.WIND_BOLT, 201, 0, 5, 11), WATER_BOLT("Water bolt", NormalSpells.WATER_BOLT, 201, 0, 6, 13), EARTH_BOLT("Earth bolt", NormalSpells.EARTH_BOLT, 201, 0, 7, 15), FIRE_BOLT("Fire bolt", NormalSpells.FIRE_BOLT, 201, 0, 8, 17), AIR_BLAST("Wind blast", NormalSpells.WIND_BLAST, 201, 0, 9, 19), WATER_BLAST("Water blast", NormalSpells.WATER_BLAST, 201, 0, 10, 21), EARTH_BLAST("Earth blast", NormalSpells.EARTH_BLAST, 201, 0, 11, 23), FIRE_BLAST("Fire blast", NormalSpells.FIRE_BLAST, 201, 0, 12, 25), WIND_WAVE("Wind wave", NormalSpells.WIND_WAVE, 201, 0, 13, 27), WATER_WAVE("Water wave", NormalSpells.WATER_WAVE, 201, 0, 14, 29), EARTH_WAVE("Earth wave", NormalSpells.EARTH_WAVE, 201, 0, 15, 31), FIRE_WAVE("Fire wave", NormalSpells.FIRE_WAVE, 201, 0, 16, 33), IBAN_BLAST("Iban blast", NormalSpells.IBAN_BLAST), CRUMBLE_UNDEAD("Crumble undead", NormalSpells.CRUMBLE_UNDEAD, 201, 0, 0, 35), MAGIC_DART("Magic dart", NormalSpells.MAGIC_DART, 201, 0, 0, 37), CLAWS_OF_GUTHIX("Claws of Guthix", NormalSpells.CLAWS_OF_GUTHIX), SARADOMIN_STRIKE("Saradomin strike", NormalSpells.SARADOMIN_STRIKE), FLAMESS_OF_ZAMORAK("Flames of Zamorak", NormalSpells.FLAMES_OF_ZAMORAK), CONFUSE("Confuse", NormalSpells.CONFUSE), WEAKEN("Weaken", NormalSpells.WEAKEN), CURSE("Curse", NormalSpells.CURSE), STUN("Stun", NormalSpells.STUN); Items(String name, MagicSpell spell, int root, int child, int child2, int configValue) { this.name = name; this.magicSpell = spell; this.root = root; this.child = child; this.child2 = child2; this.configValue = configValue; } Items(String name, MagicSpell spell) { this.name = name; this.magicSpell = spell; } private MagicSpell magicSpell; private String name; private int root, child, child2, configValue; @Override public String toString() { return name; } public MagicSpell getMagicSpell() { return magicSpell; } public int getRoot() { return root; } public int getChild() { return child; } public int getChild2() { return child2; } public int getConfigValue() { return configValue; } public RS2Widget getWidget(MethodProvider api) { return api.getWidgets().get(getRoot(), getChild(), getChild2()); } } TabHotkey.java (@FrostBug) import java.awt.event.KeyEvent; import org.osbot.rs07.api.ui.Tab; import org.osbot.rs07.script.MethodProvider; public enum TabHotkey { COMBAT(Tab.ATTACK, 0, 1224), SKILLS(Tab.SKILLS, 1, 1224), QUEST(Tab.QUEST, 2, 1224), INVENTORY(Tab.INVENTORY, 3, 1224), EQUIPMENT(Tab.EQUIPMENT, 4, 1224), PRAYER(Tab.PRAYER, 5, 1224), MAGIC(Tab.MAGIC, 0, 1225), CLAN(Tab.CLANCHAT, 1, 1225), FRIENDS(Tab.FRIENDS, 2, 1225), IGNORE(Tab.IGNORES, 3, 1225), SETTINGS(Tab.SETTINGS, 4, 1225), EMOTES(Tab.EMOTES, 5, 1225), LOGOUT(Tab.LOGOUT, 1, 1226); private final Tab tab; private final int index; private final int register; private final static int[] KEYCODES = { -1, KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4, KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8, KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12, KeyEvent.VK_ESCAPE }; private TabHotkey(Tab tab, int index, int register) { this.tab = tab; this.index = index; this.register = register; } public Tab getTab() { return tab; } public int getHotkey(MethodProvider parent) { int config = parent.getConfigs().get(this.register); int kcIndex = (config >> (this.index * 5)) & 0b11111; return KEYCODES[kcIndex]; } public boolean openTab(MethodProvider parent) throws InterruptedException { if(!isAssigned(parent)) return false; int hkey = getHotkey(parent); parent.getKeyboard().pressKey(hkey); try { MethodProvider.sleep(MethodProvider.random(20, 50)); } finally { parent.getKeyboard().releaseKey(hkey); } return true; } public boolean isAssigned(MethodProvider parent) { return getHotkey(parent) != -1; } public static TabHotkey forTab(Tab tab) { for(TabHotkey thk : values()) { if(thk.getTab() == tab) { return thk; } } return null; } } CSleep: (@Explv) import java.util.function.BooleanSupplier; import org.osbot.rs07.utility.ConditionalSleep; public final class CSleep extends ConditionalSleep { private final BooleanSupplier condition; public CSleep(final BooleanSupplier condition, int timeout) { super(timeout); this.condition = condition; } @Override public boolean condition() throws InterruptedException { return condition.getAsBoolean(); } } Usage: private Autocaster caster = new Autocaster(this); private Items spell; //onloop if (!auto.isAutocasting()) { if (getMagic().canCast(NornalSpells.AIR_BLAST)) auto.autoCastSpell(spell); else stop(); } Amazing! Thanks that helps solve the other problem where I didn't have botre's package so it wasn't reading the TabHotKey. Hey Acerd, any idea why its giving me these errors? The rest of the class files I made are fine. Quote Link to comment Share on other sites More sharing options...
Acerd Posted June 8, 2016 Share Posted June 8, 2016 cause "auto" is not the name of your Autocaster instance, its "caster". so !caster.isAutocasting() 1 Quote Link to comment Share on other sites More sharing options...
imancity Posted June 8, 2016 Author Share Posted June 8, 2016 cause "auto" is not the name of your Autocaster instance, its "caster". so !caster.isAutocasting() I realized that and fixed it, but then it gives an error for the autocastSpell and isAutocasting things. When I compile it says can't read the autocaster in private autocaster caster = new Autocaster(this). It doesn't show red underline errors but once compiled it does and then it doesn't actually work. Sorry if its a completely stupid question. Quote Link to comment Share on other sites More sharing options...