Jump to content

Multi-path recorder


Cory

Recommended Posts

Wrote it in like 20 min as I needed it to record a lot of paths.

 

SPvVQIc.png

 

 

Source

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.mouse.MinimapTileDestination;
import org.osbot.script.rs2.map.Position;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: Cory
 * Date: 29/08/13
 * Time: 09:08
 */
@ScriptManifest(name = "Path Finder", author = "Novum", version = 1D, info = "Novum Path Finder")
public class PathRecorder extends Script {

	private final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Paths");
	private final DefaultTreeModel model = new DefaultTreeModel(rootNode);
	private final JTree tree = new JTree(model);
	private DefaultMutableTreeNode lastAdded = null;

	private Position position = null;
	private boolean started;

	public void onStart() {
		new Gui().init();
	}

	public int onLoop() throws InterruptedException {
		if(started) {
			if(tree.getSelectionPaths() == null)
				return 100;
			if(position == null || distance(position) >= 10) {
				DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Position("+myX()+", "+myY()+", "+myZ()+")");
				for(TreePath path : tree.getSelectionPaths()) {
					DefaultMutableTreeNode last = (DefaultMutableTreeNode)path.getLastPathComponent();
					model.insertNodeInto(newNode, last, last.getChildCount());
					tree.expandPath(new TreePath(last.getPath()));
				}
				position = myPosition();
			}
		}
		return 0;
	}

	public void onPaint(Graphics g) {
		for(int i = 0; i < rootNode.getChildCount(); i++) {
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode) rootNode.getChildAt(i);
			MinimapTileDestination mMTD = null;
			for(int c = 0; c < parent.getChildCount(); c++) {
				DefaultMutableTreeNode childAt = (DefaultMutableTreeNode) parent.getChildAt(c);
				String nodeName = childAt.toString().substring(9);
				String[] args = nodeName.substring(0, nodeName.length()-1).split(", ");
				MinimapTileDestination mMTD2 = new MinimapTileDestination(bot, new Position(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2])));
				if(mMTD != null && mMTD2 != null) {
					g.setColor(Color.BLACK);
					if(mMTD2.getBoundingBox() != null && mMTD.getBoundingBox() != null)
						g.drawLine(
								mMTD.getBoundingBox().x, mMTD.getBoundingBox().y,
								mMTD2.getBoundingBox().x, mMTD2.getBoundingBox().y
						);
				}
				if(mMTD2 != null && mMTD2.getBoundingBox() != null) {
					g.setColor(Color.RED);
					((Graphics2D)g).draw(mMTD2.getBoundingBox());
				}
				mMTD = mMTD2;
			}
		}
	}

	private class Gui extends JFrame {

		public void init() {
			setTitle("Multi-Path Generator");
			setBounds(100, 100, 295, 300);
			setAlwaysOnTop(true);
			JPanel contentPane = new JPanel();
			setContentPane(contentPane);
			contentPane.setLayout(new BorderLayout(0, 0));

			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			contentPane.add(scrollPane, BorderLayout.CENTER);

			scrollPane.setViewportView(tree);

			JToolBar toolBar_1 = new JToolBar();
			toolBar_1.setFloatable(false);
			contentPane.add(toolBar_1, BorderLayout.NORTH);

			JToolBar toolBar = new JToolBar();
			toolBar.setFloatable(false);
			contentPane.add(toolBar, BorderLayout.SOUTH);

			JButton btnAdd = new JButton("Add");
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String name = JOptionPane.showInputDialog(Gui.this, "Name: ");
					if(name != null && name.length() >= 0) {
						DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
						model.insertNodeInto(newNode, rootNode, rootNode.getChildCount());
						tree.expandPath(new TreePath(rootNode.getPath()));
						tree.setSelectionPath(new TreePath(newNode));
						lastAdded = newNode;
						position = null;
					}
				}
			});
			toolBar.add(btnAdd);

			JButton btnClear = new JButton("Clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(tree.getSelectionPaths() == null)
						return;
					for(TreePath path : tree.getSelectionPaths()) {
						DefaultMutableTreeNode last = (DefaultMutableTreeNode)path.getLastPathComponent();
						for(int c = last.getChildCount()-1; c >= 0; c--) {
							model.removeNodeFromParent((DefaultMutableTreeNode)last.getChildAt(c));
						}
					}
				}
			});
			toolBar.add(btnClear);

			JButton btnRemove = new JButton("Remove");
			btnRemove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(tree.getSelectionPaths() == null)
						return;
					for(TreePath path : tree.getSelectionPaths()) {
						DefaultMutableTreeNode last = (DefaultMutableTreeNode)path.getLastPathComponent();
						if(last != rootNode)
							model.removeNodeFromParent(last);
					}
				}
			});
			toolBar.add(btnRemove);

			JButton btnGet = new JButton("get");
			btnGet.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					JFrame frame = new JFrame("Code");
					frame.setSize(400, 200);
					frame.setLayout(new BorderLayout());
					JTextArea codeBox = new JTextArea();

					for(int c = 0; c < rootNode.getChildCount(); c++) {
						DefaultMutableTreeNode child = (DefaultMutableTreeNode)rootNode.getChildAt(c);
						StringBuilder builder = new StringBuilder("Position[] "+child.toString()+" = new Position[]{\n");
						for(int sc = 0; sc < child.getChildCount(); sc++) {
							DefaultMutableTreeNode subchild = (DefaultMutableTreeNode)child.getChildAt(sc);

							builder.append("    new "+subchild.toString()+",\n");
						}
						builder.append("};\n\n");
						codeBox.append(builder.toString());
					}
					frame.add(new JScrollPane(codeBox));
					frame.setVisible(true);
				}
			});
			toolBar.add(btnGet);

			JButton btnAddPosition = new JButton("Add Current");
			btnAddPosition.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(lastAdded == null)
						return;
					DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Position("+myX()+", "+myY()+", "+myZ()+")");
					model.insertNodeInto(newNode, lastAdded, lastAdded.getChildCount());
					tree.expandPath(new TreePath(lastAdded.getPath()));
				}
			});
			toolBar_1.add(btnAddPosition);

			JButton btnStart = new JButton("Start");
			btnStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TreePath path = tree.getSelectionPath();
					if(path == null || path.getLastPathComponent() == rootNode) {
						String name = JOptionPane.showInputDialog(Gui.this, "Name: ");
						if(name != null && name.length() >= 0) {
							DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
							model.insertNodeInto(newNode, rootNode, rootNode.getChildCount());
							tree.expandPath(new TreePath(rootNode.getPath()));
							tree.setSelectionPath(new TreePath(newNode));
							lastAdded = newNode;
						}
					}
					started = true;
				}
			});
			toolBar_1.add(btnStart);

			JButton btnStop = new JButton("Stop");
			btnStop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					started = false;
					position = null;
				}
			});
			toolBar_1.add(btnStop);

			setVisible(true);
		}
	}
}

 

Download

https://www.dropbox.com/s/umh4xdkjui3ave4/PathRecorder.jar

 

 

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

 

Looks great although I very much like the concept of Dunnker's more.

 

Great release. biggrin.png

 

The reason i wrote this is for accuracy, as the path maker/generators atm use newer maps than we actually have, or not a reliable pixel>tile ratio. 

 

Yes, this is perfect for my needs. Thank you.

Link to comment
Share on other sites

  • 1 year later...
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...