Jump to content

YaksPls [Potion support][Special Attack support]


Salty as fuck

Recommended Posts

There's also an edge case that the script runs into sometimes that I unfortunately am not entirely sure of the circumstances that create it:

 

Sometimes the script attempts to attack a Yak other than the Yak that is attacking the player. The script repeatedly tries to click on the new Yak, but is interrupted by "I'm already under attack". There must be something about how it is checking interaction because it will end up target-switching to the attacking Yak and hitting it after the player auto-retaliates against the attacking Yak.

 

My only guess as to why this happens is that it looks like the script doesn't wait very long (or the ConditionalSleep isn't quiet tuned to the correct actions) once selecting a new target. You can watch it click on new Yaks more than once sometimes when first starting to fight it. So my guess is that the script misclicks the second click, hitting a second Yak, thereby attempting to fight the second Yak while the first Yak is attacking the player from the first click.

 

Pushing the hypothesis even further, it looks like this happens most-consistently when the newly-selected Yak requires the player to run up to it first.

 

EDIT: Yep, easy to replicate if you Pause, walk away from the Yaks, and then hit Play again. The script will select to attack the new Yak more than once before fighting commences instead of waiting until the Player is fighting after the first selection.

 

Also, the script semi-frequently will try attacking Yaks that other players are attacking. I think this is largely a small race-condition though as one player reaches a Yak before you do. That issue is less obvious and less prevalent. 

Edited by jython
Link to comment
Share on other sites

There's also an edge case that the script runs into sometimes that I unfortunately am not entirely sure of the circumstances that create it:

 

Sometimes the script attempts to attack a Yak other than the Yak that is attacking the player. The script repeatedly tries to click on the new Yak, but is interrupted by "I'm already under attack". There must be something about how it is checking interaction because it will end up target-switching to the attacking Yak and hitting it after the player auto-retaliates against the attacking Yak.

 

My only guess as to why this happens is that it looks like the script doesn't wait very long (or the ConditionalSleep isn't quiet tuned to the correct actions) once selecting a new target. You can watch it click on new Yaks more than once sometimes when first starting to fight it. So my guess is that the script misclicks the second click, hitting a second Yak, thereby attempting to fight the second Yak while the first Yak is attacking the player from the first click.

 

Pushing the hypothesis even further, it looks like this happens most-consistently when the newly-selected Yak requires the player to run up to it first.

 

EDIT: Yep, easy to replicate if you Pause, walk away from the Yaks, and then hit Play again. The script will select to attack the new Yak more than once before fighting commences instead of waiting until the Player is fighting after the first selection.

 

Also, the script semi-frequently will try attacking Yaks that other players are attacking. I think this is largely a small race-condition though as one player reaches a Yak before you do. That issue is less obvious and less prevalent. 

I'm leaving and don't really have any time left to update stuff. Here's the source. My suggestion is just removing the pots only option and withdrawing potions based on how many checkboxes have been selected.

 

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.util.Utilities;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

@ScriptManifest(name = "YaksPls", author = "Reminiscence", version = 1.2, info = "Attacks yaks in Neitiznot.", logo = "http://i.imgur.com/JQNSSJT.png") 
public class YaksPls extends Script {

	boolean eating, potsOnly, usingPots, drinkingCombatPotions, drinkingAtt, drinkingStr, drinkingDef, drinkingSupAtt, drinkingSupStr, drinkingSupDef;
	private long startTime;

	String[] perc25 = {"Dragon dagger", "Dragon longsword", "Dragon mace"};
	String[] perc30 = {"Dragon halberd", "Dragon spear"};
	String[] perc40 = {"Magic longbow", "Magic composite bow"};
	String[] perc50 = {"Abyssal whip", "Granite maul", "Barrelchest anchor", "Armadyl godsword", "Saradomin godsword", "Zamorak godsword"};
	String[] perc55 = {"Dragon 2h sword", "Dragon scimitar", "Magic shortbow"};
	String[] perc65 = {"Dark bow", "Bandos godsword"};
	String[] perc100 = {"Dragon battleaxe", "Saradomin sword", "Seercull bow"};


	public enum State {
		Banking, walkingToBank, walkingToNpc, Eating, Fighting, Idle
	}

	String s, food = "";
	String[] pots = { "Combat potion(", "Attack potion(", "Super attack(", "Strength potion(", "Super strength(", "Defence potion(", "Super defence("};



	private State getState() {
		Entity BankChest = objects.closest("Bank chest");

		if (npcs.closest("Yak") != null) {
			NPC yaks = npcs.getAll().stream().filter(n -> n.getName().equals("Yak") && !n.isUnderAttack()).findFirst().get();

			if (!combat.isFighting() && players.myPlayer().getHealthPercent() >= 50 && map.isWithinRange(yaks,  15) && yaks != null) {
				if ((eating && inventory.contains(food)) || !eating) {
					s = "Fighting";
					return State.Fighting;
				}
			}

			if (!inventory.contains(food) && eating && !map.isWithinRange(BankChest, 10)) {
				s = "Walking to bank";
				return State.walkingToBank;
			}

			if ((inventory.contains(food) && eating && !map.isWithinRange(yaks, 15)) 
					|| (potsOnly && !inventory.isEmpty() && !inventory.onlyContains("Vial") && !map.isWithinRange(yaks, 15))) {
				s = "Walking to npc";
				return State.walkingToNpc;
			}

			if ((!inventory.contains(food) && eating || potsOnly && inventory.onlyContains("Vial")) && map.isWithinRange(BankChest, 10)) {	
				s = "Banking";
				return State.Banking;
			}

			if (inventory.contains(food) && eating && players.myPlayer().getHealthPercent() <= 50) {
				s = "Eating";
				return State.Eating;
			}
		}

		return State.Idle;
	}



	GUI g = new GUI();
	public static boolean guiWait = true;

	@[member='Override']
	public void onStart() throws InterruptedException {

		g.setVisible(true);
		while (guiWait) { 
			sleep(400);
		}
		startTime = System.currentTimeMillis();
		experienceTracker.startAll();
	}

	@[member='Override']
	public void onExit() {
		log("food: " + food);
	}

	@[member='Override']
	public int onLoop() throws InterruptedException {

		if (npcs.closest("Yak") != null) {
			NPC yaks = npcs.getAll().stream().filter(n -> n.getName().equals("Yak") && n.getHealthPercent() > 0 && !n.isUnderAttack()).findFirst().get();		

			if (usingPots) {
				//regualar potions
				if (drinkingAtt && skills.getDynamic(Skill.ATTACK) <= (skills.getStatic(Skill.ATTACK) +2)) {
					inventory.interactWithNameThatContains("Drink", "Attack potion(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.ATTACK) > skills.getStatic(Skill.ATTACK);
						}
					}.sleep();
				}

				if (drinkingStr && skills.getDynamic(Skill.STRENGTH) <= (skills.getStatic(Skill.STRENGTH) +2)) {
					inventory.interactWithNameThatContains("Drink", "Strength potion(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.STRENGTH) > skills.getStatic(Skill.STRENGTH);
						}
					}.sleep();
				}

				if (drinkingDef && skills.getDynamic(Skill.DEFENCE) <= (skills.getStatic(Skill.DEFENCE) +2)) {
					inventory.interactWithNameThatContains("Drink", "Defence potion(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.DEFENCE) > skills.getStatic(Skill.DEFENCE);
						}
					}.sleep();
				}

				//Super potions
				if (drinkingSupAtt && skills.getDynamic(Skill.ATTACK) <= (skills.getStatic(Skill.ATTACK) +2)) {
					inventory.interactWithNameThatContains("Drink", "Super attack(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.ATTACK) > skills.getStatic(Skill.ATTACK);
						}
					}.sleep();
				}

				if (drinkingSupStr && skills.getDynamic(Skill.STRENGTH) <= (skills.getStatic(Skill.STRENGTH) +2)) {
					inventory.interactWithNameThatContains("Drink", "Super strength(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.STRENGTH) > skills.getStatic(Skill.STRENGTH);
						}
					}.sleep();
				}

				if (drinkingSupDef && skills.getDynamic(Skill.DEFENCE) <= (skills.getStatic(Skill.DEFENCE) +2)) {
					inventory.interactWithNameThatContains("Drink", "Super defence(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.DEFENCE) > skills.getStatic(Skill.DEFENCE);
						}
					}.sleep();
				}

				//combat potion
				if (drinkingCombatPotions && skills.getDynamic(Skill.ATTACK) <= (skills.getStatic(Skill.ATTACK) +1)) {
					inventory.interactWithNameThatContains("Drink", "Combat potion(");
					sleep(1200);
					new ConditionalSleep(4000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return skills.getDynamic(Skill.DEFENCE) > skills.getStatic(Skill.DEFENCE);
						}
					}.sleep();
				}
			}

			if (!guiWait) 

				if (dialogues.isPendingContinuation()) {

					Utilities.takeScreenshot();
					dialogues.clickContinue();
					new ConditionalSleep(2000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return !dialogues.isPendingContinuation();
						}
					}.sleep();
				}

			switch (getState()) {
			case Banking:
				if (map.isWithinRange(objects.closest("Bank chest"), 10) && !bank.isOpen()) {
					bank.open();
					new ConditionalSleep(10000) {
						@[member='Override']
						public boolean condition() throws InterruptedException {
							s = "Opening bank";
							return bank.isOpen();
						}
					}.sleep();
				}

				if (bank.isOpen() && !potsOnly) {
					if (!inventory.contains(food)) {
						if (!inventory.isEmpty()) {
							bank.depositAll();
						}
						new ConditionalSleep(5000) {

							@[member='Override']
							public boolean condition() throws InterruptedException {
								return inventory.isEmpty();
							}
						}.sleep();
					}
				}

				if (!inventory.contains(food) && bank.isOpen() && !usingPots) {

					bank.withdraw(food, inventory.getEmptySlotCount());
					new ConditionalSleep(3500) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return inventory.contains(food);
						}
					}.sleep();
				} else if (!inventory.contains(food) && bank.isOpen() && usingPots && !potsOnly) {
					bank.depositAll();
					new ConditionalSleep(3000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return inventory.isEmpty();
						}
					}.sleep();

					if (drinkingAtt && bank.contains("Attack potion(4)"))
						bank.withdraw("Attack potion(4)", 2);

					if (drinkingCombatPotions && bank.contains("Combat potion(4)"))
						bank.withdraw("Combat potion(4)", 2);

					if (drinkingStr && bank.contains("Strength potion(4)"))
						bank.withdraw("Strength potion(4)", 2);

					if (drinkingDef && bank.contains("Defence potion(4)"))
						bank.withdraw("Defence potion(4)", 2);

					if (drinkingSupAtt && bank.contains("Super attack(4)"))
						bank.withdraw("Super attack(4)", 2);

					if (drinkingSupAtt && bank.contains("Super strength(4)"))
						bank.withdraw("Super strength(4)", 2);

					if (drinkingSupAtt && bank.contains("Defence attack(4)"))
						bank.withdraw("Defence attack(4)", 2);

					bank.withdraw(food, inventory.getEmptySlotCount());
					new ConditionalSleep(3500) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return inventory.contains(food);
						}
					}.sleep();

				} else if (!inventory.contains(food) && bank.isOpen() && usingPots && potsOnly) {
					bank.depositAll();
					new ConditionalSleep(3000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return inventory.isEmpty();
						}
					}.sleep();

					if (drinkingAtt && bank.contains("Attack potion(4)"))
						bank.withdraw("Attack potion(4)", 3);

					if (drinkingStr && bank.contains("Strength potion(4)"))
						bank.withdraw("Strength potion(4)", 3);

					if (drinkingDef && bank.contains("Defence potion(4)"))
						bank.withdraw("Defence potion(4)", 3);

					if (drinkingSupAtt && bank.contains("Super attack(4)"))
						bank.withdraw("Super attack(4)", 3);

					if (drinkingSupAtt && bank.contains("Super strength(4)"))
						bank.withdraw("Super strength(4)", 3);

					if (drinkingSupAtt && bank.contains("Defence attack(4)"))
						bank.withdraw("Defence attack(4)", 3);

					if (drinkingCombatPotions && bank.contains("Combat potion(4)"))
						bank.withdraw("Combat potion(4)", 3);
					
					new ConditionalSleep(3500) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return !inventory.isEmpty();
						}
					}.sleep();
				}

				if (eating && inventory.contains(food) && bank.isOpen() && !usingPots) {
					bank.close();

					new ConditionalSleep(5000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return !bank.isOpen();
						}
					}.sleep();
				}

				if (usingPots && bank.isOpen() && potsOnly && !inventory.isEmpty()) {
					bank.close();

					new ConditionalSleep(5000) {

						@[member='Override']
						public boolean condition() throws InterruptedException {
							return !bank.isOpen();
						}
					}.sleep();
				}

				break;

			case walkingToNpc:
				s = "Walking to yaks";
				walking.webWalk(new Position(2323, 3798, 0));
				break;

			case Eating:
				if (eating) {
					if (players.myPlayer().getHealthPercent() < 50 && inventory.contains(food)) {
						s = "Eating";
						inventory.getItem(food).interact("Eat");
						sleep(800);
					}
				}
				break;

			case Fighting:
				if (!combat.isFighting()) {
					if (yaks != null && yaks.getHealthPercent() > 0 && yaks.isAttackable()) {
						yaks.interact("Attack");
						new ConditionalSleep(5000) {
							
							@[member='Override']
							public boolean condition() throws InterruptedException {
								return combat.isFighting();
							}
						}.sleep();
					}
				}
				
				if (combat.getSpecialPercentage() >= 25 && !combat.isSpecialActivated()) {
					specAttack();
				}
				break;

			case Idle:
				sleep(600);
				break;

			case walkingToBank:
				walking.webWalk(new Position(2336, 3807, 0));
				break;

			default:
				break;
			}
		}
		return 0;
	}
	
	public void specAttack() {
		if (equipment.isWieldingWeaponThatContains(perc25)) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() >= 25) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}

		if (equipment.isWieldingWeaponThatContains(perc30)) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() >= 30) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}

		if (equipment.isWieldingWeaponThatContains(perc40)) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() >= 40) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}

		if (equipment.isWieldingWeaponThatContains(perc50)) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() >= 50) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}

		if (equipment.isWieldingWeaponThatContains(perc55)) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() >= 55) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}

		if (equipment.isWieldingWeaponThatContains(perc100)) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() == 100) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}

		if (equipment.isWieldingWeaponThatContains(perc65) ) {
			if (!combat.isSpecialActivated() && combat.getSpecialPercentage() >= 65) {
				combat.toggleSpecialAttack(true);
				return;
			}
		}
	}

	public boolean isAnimating() throws InterruptedException {
		int i = 0;
		while (i < 3) {
			if (myPlayer().isAnimating() || players.myPlayer().isMoving()) {
				return true;
			}
			i++;
		}
		return false;
	}

	private final Color color1 = new Color(0, 0, 0, 210);
	private final Color color2 = new Color(255, 153, 51);

	private final BasicStroke stroke1 = new BasicStroke(1);

	private final Font font1 = new Font("Arial", 1, 16);
	private final Font font2 = new Font("Arial", 0, 11);

	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);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void onPaint(Graphics2D g) {
		final long runTime = System.currentTimeMillis() - startTime;
		g.setColor(color1);
		g.fillRect(544, 208, 192, 256);
		g.setColor(color2);
		g.setStroke(stroke1);
		g.drawRect(544, 208, 192, 256);
		g.drawLine(560, 240, 720, 240);
		g.setFont(font1);
		g.drawString("YaksPls", 608, 224);
		g.setFont(font2);
		g.drawString("Health Exp: " + experienceTracker.getGainedXP(Skill.HITPOINTS) + " (" + experienceTracker.getGainedLevels(Skill.HITPOINTS) + ") H/r: " + experienceTracker.getGainedXPPerHour(Skill.HITPOINTS), 560, 256);
		g.drawString("Attack Exp: " + experienceTracker.getGainedXP(Skill.ATTACK) + " (" + experienceTracker.getGainedLevels(Skill.ATTACK) + ") H/r: " + experienceTracker.getGainedXPPerHour(Skill.ATTACK), 560, 272);
		g.drawString("Strength Exp: " + experienceTracker.getGainedXP(Skill.STRENGTH) + " (" + experienceTracker.getGainedLevels(Skill.STRENGTH) + ") H/r: " + experienceTracker.getGainedXPPerHour(Skill.STRENGTH), 560, 288);
		g.drawString("Defence Exp: " + experienceTracker.getGainedXP(Skill.DEFENCE) + " (" + experienceTracker.getGainedLevels(Skill.DEFENCE) + ") H/r: " + experienceTracker.getGainedXPPerHour(Skill.DEFENCE), 560, 304);
		g.drawString("State: " + s, 560, 416);
		g.drawString("Runtime: " + formatTime(runTime), 560, 432);
	}

	public class GUI extends JFrame {
		private static final long serialVersionUID = -7466782193912627641L;
		private JPanel contentPane;

		@SuppressWarnings({ "rawtypes", "unchecked" })
		public GUI() {
			setTitle("YaksPls");
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			setBounds(100, 100, 302, 178);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			contentPane.setLayout(null);

			JLabel lblFood = new JLabel("Food:");
			lblFood.setBounds(10, 11, 46, 14);
			contentPane.add(lblFood);

			final JComboBox comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"Not eating", "Pots only", "Tuna", "Bass", "Lobster", "Swordfish", "Monkfish"}));
			comboBox.setBounds(44, 8, 232, 20);
			contentPane.add(comboBox);



			JCheckBox chckbxAttackPotions = new JCheckBox("Attack potions");
			chckbxAttackPotions.setBounds(10, 32, 116, 23);
			contentPane.add(chckbxAttackPotions);

			JCheckBox chckbxStrengthPotions = new JCheckBox("Strength potions");
			chckbxStrengthPotions.setBounds(10, 58, 112, 23);
			contentPane.add(chckbxStrengthPotions);

			JCheckBox chckbxDefencePotions = new JCheckBox("Defence potions");
			chckbxDefencePotions.setBounds(10, 84, 112, 23);
			contentPane.add(chckbxDefencePotions);

			JCheckBox chckbxSuperAttackPotions = new JCheckBox("Super attack potions");
			chckbxSuperAttackPotions.setBounds(138, 32, 140, 23);
			contentPane.add(chckbxSuperAttackPotions);

			JCheckBox chckbxSuperStrengthPotions = new JCheckBox("Super strength potions");
			chckbxSuperStrengthPotions.setBounds(138, 58, 140, 23);
			contentPane.add(chckbxSuperStrengthPotions);

			JCheckBox chckbxSuperDefencePotions = new JCheckBox("Super defence potions");
			chckbxSuperDefencePotions.setBounds(138, 84, 140, 23);
			contentPane.add(chckbxSuperDefencePotions);

			JCheckBox chckbxCombatPotions = new JCheckBox("Combat potions");
			chckbxCombatPotions.setBounds(10, 110, 116, 23);
			contentPane.add(chckbxCombatPotions);

			JButton btnStart = new JButton("Start");
			btnStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (comboBox.getSelectedItem() == "Not eating") {
						eating = false;
						food = "None";
					} else {
						eating = true;
					}

					if (comboBox.getSelectedItem() == "Pots only") {
						potsOnly = true;
					} else {
						potsOnly = false;
					}

					if (chckbxAttackPotions.isSelected()) 
						drinkingAtt = true;
					usingPots = true;

					if (chckbxSuperAttackPotions.isSelected()) 
						drinkingSupAtt = true;
					usingPots = true;

					if (chckbxStrengthPotions.isSelected()) 
						drinkingStr = true;
					usingPots = true;

					if (chckbxSuperStrengthPotions.isSelected()) 
						drinkingSupStr = true;
					usingPots = true;

					if (chckbxDefencePotions.isSelected()) 
						drinkingDef = true;
					usingPots = true;

					if (chckbxSuperDefencePotions.isSelected()) 
						drinkingSupDef = true;
					usingPots = true;

					if (chckbxCombatPotions.isSelected()) 
						drinkingCombatPotions = true;
					usingPots = true;

					food = comboBox.getSelectedItem().toString();
					log("Eating: " + eating);
					log("Food: " + food);
					guiWait = false;
					g.dispose();
				}
			});
			btnStart.setBounds(138, 114, 98, 23);
			contentPane.add(btnStart);
		}
	}
}

Edited by Reminiscence
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...