All pastes #350811 Raw Edit

Mine

public text v1 · immutable
·published 2007-02-11 19:06 UTC
rendered paste body
import impsoft.nexus.bots.color.constants.Areas;
import impsoft.nexus.bots.color.methods.ColorScript;
import impsoft.nexus.bots.color.scriptobjects.RGB;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;

/*rsObject, revolutionizing finding objects in ibot
 * V1.0
 * TODO: add more comparison methods.
 * allow multiple data inputs to be averaged into one object.-DONE!
 * simple object-creating script.
 * 
 * By LardMaster
 */

public class rsObject {
	private ColorScript methods;

	String name;

	Rectangle minDimensions, maxDimensions;

	int numSources = 0;

	TreeSet<RGBit> data = new TreeSet<RGBit>(new Comparator<RGBit>() {
		public int compare(RGBit o1, RGBit o2) {
			return (int) ((o1.getPercent() - o2.getPercent()) / Math.abs(o1
					.getPercent()
					- o2.getPercent()));
		}
	});

	public rsObject(ColorScript methods, String name, RGB[][] colorMap,
			Rectangle minDimensions, Rectangle maxDimensions) {
		add(colorMap);
		this.minDimensions = minDimensions;
		this.maxDimensions = maxDimensions;
		this.methods = methods;
		this.name = name;
	}

	public rsObject(ColorScript father, String name, Rectangle outline,
			Rectangle minDimensions, Rectangle maxDimensions) {
		this(father, name, LardMethods.rectOnColorMap(convertShort(father.getColorArray2d()), outline),
				minDimensions, maxDimensions);
	}

	public double getPercent(RGB r, RGB[][] colorMap) {
		int count = 0;
		for (RGB[] i : colorMap)
			for (RGB j : i)
				if (j.equals(r))
					count++;
		return count / (colorMap.length * colorMap[0].length);
	}

	private void add(RGBit colorData) {
		data.add(colorData);
	}

	public void add(RGB[][] colorMap) {
		RGBit duplicate;
		for (RGB[] i : colorMap)
			for (RGB j : i) {
				boolean copy = false;
				for (RGBit b : data)
					if (b.getColor().equals(j)) {
						copy = true;
						duplicate = b;
						if (copy) {
							data.remove(duplicate);
							double percent = (duplicate.getPercent()
									* numSources + getPercent(j, colorMap))
									/ (numSources + 1);
							add(new RGBit(j, percent));
						} else
							add(new RGBit(j, getPercent(j, colorMap)));
					}
			}
		numSources++;
	}

	public double matchByPercent(rsObject o2, int numColors) {
		int totalOff = 0;
		RGBit match = null;
		if (o2.data.equals(this.data))
			return 100;
		int i = 0;
		for (RGBit r : this.data) {
			if (i >= numColors)
				break;
			match = null;
			for (RGBit r2 : o2.data)
				if (r2.getColor().equals(r.getColor()))
					match = r2;
			if (match == null) {
				totalOff += 100;
				continue;
			}
			totalOff += match.percentOff(r);
		}
		return totalOff / numColors;
	}

	public double matchByPercent(RGB[][] o2, int numColors) {
		return matchByPercent(new rsObject(methods, "temp", o2, null, null),
				numColors);
	}

	public double matchByPercent(Rectangle r, int numColors) {
		return matchByPercent(LardMethods.rectOnColorMap(convertShort(methods.getColorArray2d()), r), numColors);
	}

	public boolean clickOne(String action) throws InterruptedException {
		methods.mouseMove(findOne());
		if (!methods.getTopText().contains(this.name)) {
			methods.log("Could not Find " + this.name);
			return false;
		}
		if (methods.getTopText().contains(action)) {
			methods.mouseClickLeft(methods.theBot.mouse_x,
					methods.theBot.mouse_y);
			return true;
		} else {
			methods.mouseClickRight(methods.theBot.mouse_x,
					methods.theBot.mouse_y);
			if (!methods.getMenu(Areas.GAME, methods.random(300, 400))
					.selectContains(action)) {
				methods.log("no such action: " + action);
				return false;
			}
			return true;
		}
	}

	public Rectangle findOne(int numColors) {
		Rectangle searchBounds = Areas.GAME;
		int xinc = (int) (minDimensions.getWidth() + this.maxDimensions
				.getWidth()) / 4;
		int yinc = (int) (minDimensions.getHeight() + this.maxDimensions
				.getHeight()) / 4;
		Rectangle minDimensions = this.minDimensions;
		Rectangle maxDimensions = this.maxDimensions;
		int widthStep = (int) (maxDimensions.getWidth() - minDimensions
				.getWidth()) / 8;
		int heightStep = (int) (maxDimensions.getHeight() - minDimensions
				.getHeight()) / 8;
		Rectangle result = null;
		int maxcount = 0;
		for (double i = 0; i < searchBounds.getWidth()
				- minDimensions.getWidth(); i = i + xinc)
			for (double b = 0; b < searchBounds.getHeight()
					- minDimensions.getHeight(); b = b + yinc)
				for (int width = (int) minDimensions.getWidth(); width < maxDimensions
						.getWidth(); width = width + widthStep)
					for (int height = (int) minDimensions.getHeight(); height > (int) maxDimensions
							.getHeight(); height = height + heightStep) {
						Rectangle h = new Rectangle((int) i, (int) b, width,
								height);
						if (matchByPercent(h, numColors) > maxcount) {
							maxcount = (int) matchByPercent(h, numColors);
							result = h;
						}
					}
		return result;
	}
	public ArrayList<Rectangle> getLocationsArray(int numcolors, double minPercent) {
			ArrayList<Rectangle> result = new ArrayList<Rectangle>();
			Rectangle best;
			Rectangle searchBounds = Areas.GAME;
			int xinc = (int) (minDimensions.getWidth() + this.maxDimensions
					.getWidth()) / 4;
			int yinc = (int) (minDimensions.getHeight() + this.maxDimensions
					.getHeight()) / 4;
			Rectangle minDimensions = this.minDimensions;
			Rectangle maxDimensions = this.maxDimensions;
			int widthStep = (int) (maxDimensions.getWidth() - minDimensions
					.getWidth()) / 8;
			int heightStep = (int) (maxDimensions.getHeight() - minDimensions
					.getHeight()) / 8;
			while (true) {
				best = null;
				int maxcount = 0;
				for (double i = searchBounds.getMinX(); i < searchBounds.getMaxX()
						- minDimensions.getWidth(); i = i + xinc)
					for (double b = searchBounds.getMinY(); b < searchBounds
							.getMaxY()
							- minDimensions.getHeight(); b = b + yinc)
						for (int width = (int) minDimensions.getWidth(); width < maxDimensions
								.getWidth(); width = width + widthStep)
							for (int height = (int) minDimensions.getHeight(); height > (int) maxDimensions
									.getHeight(); height = height + heightStep) {
								Rectangle h = new Rectangle((int) i, (int) b,
										width, height);
								if (matchByOrder(h,numcolors)>minPercent && LardMethods.tooMuchOverlap(h, result, 20)) {

								}
							}
				if (best == null)
					break;
				result.add(best);
			}
			return result;
		}
	private double matchByOrder(rsObject o2, int numcolors) {
		RGBit[] ours=(RGBit[])this.data.toArray();
		RGBit[] thiers=(RGBit[])o2.data.toArray();
	    double count=0;
		for(int i=0; i<numcolors; i++) {
			if(ours[i].equals(thiers[i])) {
				count++;
			} else {
				boolean found=false;
				for(RGBit r:o2.data) {
					if(r.getColor().equals(ours[i].getColor()))
						found=true;
				}
				if(found) {
					int thierIndex=-1;
					for(int b=0; b<thiers.length; b++) {
						if(thiers[b].getColor().equals(ours[i].getColor())) {
							thierIndex=b;
							break;
						}
					}
					if(thierIndex!=-1) {
						count++;
					}
				}
			}
		}
		return count;
	}
	private double matchByOrder(Rectangle r, int numcolors) {
		return matchByOrder(new rsObject(this.methods,"temp",r,null,null), numcolors);
	}
	public Rectangle findOne() {
		return findOne(4);
	}
	static RGB[][] convertShort(short[][][] colormap) {
		RGB[][] rgbmap = new RGB[colormap.length][colormap[0].length];
		for (int i = 0; i < colormap.length; i++)
			for (int j = 0; j < colormap[0].length; j++)
				rgbmap[i][j] = new RGB(colormap[i][j][0], colormap[i][j][1],
						colormap[i][j][2]);
		return rgbmap;
	}


}