Advertisement
TheBat

DTM

Apr 11th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. package scripts;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.awt.Rectangle;
  6. import java.util.ArrayList;
  7.  
  8. import org.tribot.script.methods.Colour;
  9. import org.tribot.script.methods.General;
  10.  
  11. public class DTM {
  12. private final Color[] rgb;
  13. private final Point[] loc;
  14. private Rectangle box;
  15. private final String name;
  16. ArrayList<Rectangle> dtmLocations = new ArrayList<Rectangle>();
  17.  
  18. public DTM(final Point[] loc, final Color[] color, final String name) {
  19. this.loc = loc;
  20. this.rgb = color;
  21. this.name = name;
  22. box = createRectangleFromPoints(loc);
  23. }
  24.  
  25. public DTM(final Point[] loc, final Color[] color) {
  26. this(loc, color, "DTM");
  27. }
  28.  
  29. /**
  30. * Given a set of points create a rectangle that encases all of those points
  31. * @param points : set of points to create a rectangle with
  32. * @return : A rectangle that encloses all points
  33. */
  34. private Rectangle createRectangleFromPoints(final Point[] points) {
  35. Point minPoint = new Point(800, 600), maxPoint = new Point(0, 0);
  36. for(int i = 0; i < loc.length; i++) {
  37. if(points[i].x < minPoint.x) minPoint.x = points[i].x;
  38. if(points[i].x > maxPoint.x) maxPoint.x = points[i].x;
  39. if(points[i].y < minPoint.y) minPoint.y = points[i].y;
  40. if(points[i].y > maxPoint.y) maxPoint.y = points[i].y;
  41. }
  42. return new Rectangle(minPoint.x, minPoint.y, (maxPoint.x - minPoint.x) + 1, (maxPoint.y - minPoint.y) + 1);
  43. }
  44.  
  45. /**
  46. * Looks for the DTM in the original spot specified
  47. * @param tol : tolerance between each RGB value when checking if colors are the same
  48. * @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
  49. */
  50. public boolean findOriginalDTM(final int tol) {
  51. return checkAllPoints(loc[0], tol);
  52. }
  53.  
  54. /**
  55. * Finds all DTMs on the game screen stores and stores there location to the dtmLocations ArrayList.
  56. * @param tol : tolerance between each RGB value when checking if colors are the same
  57. * @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
  58. */
  59. public boolean findDTMS(final int tol) {
  60. return findDTMS(tol, new Rectangle(0, 0, 765, 503));
  61. }
  62.  
  63. /**
  64. * Searches for DTMs in in the rectangle passed and stores all DTMs found to the dtmLocations ArrayList.
  65. * @param tol : tolerance between each RGB value when checking if colors are the same
  66. * @param rec : Rectangle to search for the DTM to be in.
  67. * @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
  68. */
  69. public boolean findDTMS(final int tol, final Rectangle rec) {
  70. boolean foundDTM = false;
  71. dtmLocations.clear();
  72.  
  73. for(int y = rec.y; y < rec.y + rec.height; y++) {
  74. for(int x = rec.x; x < rec.x + rec.width; x++) {
  75. if(checkColor(Colour.getColour(x, y), rgb[0], tol)) {
  76. final Rectangle curbox = new Rectangle(x - (loc[0].x - box.x), y - (loc[0].y - box.y), box.width, box.height);
  77. if(checkAllPoints(new Point(x, y), tol)) {
  78. dtmLocations.add(curbox);
  79. foundDTM = true;
  80. }
  81. }
  82. }
  83. }
  84. return foundDTM;
  85. }
  86.  
  87. /**
  88. * Grabs the DTM specified by index in the dtms found array
  89. * @param i : index in the dtm array to grab
  90. * @return : the center point of the rectangle for the DTM or null if not found
  91. */
  92. public Point getDTM(final int i) {
  93. return dtmLocations.isEmpty() ? null : new Point(dtmLocations.get(i).x + (dtmLocations.get(i).width / 2), dtmLocations.get(i).y + (dtmLocations.get(i).height / 2));
  94. }
  95. /**
  96. * Returns the all of the dtms in the list
  97. * @return : the center point of the rectangle for the DTM or null if not found
  98. */
  99. public ArrayList<Rectangle> getList() {
  100. return dtmLocations;
  101. }
  102. /**
  103. *
  104. * @param gameS game screen colors array
  105. * @param locM location of point that matches the first color in the array list colorB
  106. * @return true if all colors match
  107. */
  108. private boolean checkAllPoints(Point locM, final int tol) {
  109. try {
  110. final Point locI = loc[0];
  111. for(int i = 0; i < loc.length; i++) {
  112. // we can come back and shorten this, but for now this is to make sure we make no mistakes
  113. final Color initialColor = rgb[i];
  114. final Point initialColorPoint = loc[i];
  115. final int xshift = initialColorPoint.x - locI.x;
  116. final int yshift = initialColorPoint.y - locI.y;
  117. final Point pointToCheck = new Point(locM.x + xshift, locM.y + yshift);
  118. final Color colorToCheck = Colour.getColour(pointToCheck);
  119.  
  120. if(!checkColor(initialColor, colorToCheck, tol)) return false;
  121. }
  122. return true;
  123. } catch (Exception e) {
  124. return false;
  125. }
  126. }
  127.  
  128. /**
  129. * Checks to see if the 2 colors passed match
  130. * @param c : Color 1
  131. * @param c2 : Color 2
  132. * @param tol : tolerance between each RGB value when checking if colors are the same
  133. * @return <tt>true</tt> if colors match; otherwise <tt>false</tt>
  134. */
  135. private boolean checkColor(final Color c, final Color c2, final int Tol) {
  136. return (checkColor(c.getRed(), c2.getRed(), Tol)
  137. && checkColor(c.getGreen(), c2.getGreen(), Tol) && checkColor(c.getBlue(),
  138. c2.getBlue(), Tol));
  139. }
  140.  
  141. /**
  142. * Checks to see if the 2 RGB values match
  143. * @param RGB1 : RGB value 1
  144. * @param RGB2 : RGB value 2
  145. * @param tol : tolerance between the RGB values when checking if they are the same
  146. * @return <tt>true</tt> if RGB values match; otherwise <tt>false</tt>
  147. */
  148. private boolean checkColor(final int RGB1, final int RGB2, final int Tol) {
  149. return Math.abs(RGB1 - RGB2) < Tol;
  150. }
  151.  
  152. public void drawDTMs(Graphics g) {
  153. g.setColor(Color.RED);
  154. for(int i = 0; i < dtmLocations.size(); i++) {
  155. drawX(g, dtmLocations.get(i));
  156. }
  157. }
  158.  
  159. private void drawX(Graphics g, Rectangle rec) {
  160. g.setColor(Color.RED);
  161. g.drawLine(rec.x, rec.y, rec.x + rec.width, rec.y + rec.height);
  162. g.drawLine(rec.x, rec.y + rec.height, rec.x + rec.width, rec.y);
  163. }
  164.  
  165. public void drawDTMSquares(Graphics g, final Color c1, boolean addname) throws Exception {
  166. for(int i = 0; i < dtmLocations.size(); i++) {
  167. Rectangle dtm = dtmLocations.get(i);
  168. g.setColor(c1);
  169. g.drawRect(dtm.x, dtm.y, dtm.width, dtm.height);
  170. if(!addname) return;
  171. g.setColor(Color.BLACK);
  172. g.fill3DRect(dtm.x + dtm.width - 33, dtm.y + dtm.height + 4, 41, 11, true);
  173. g.setColor(Color.WHITE);
  174. g.drawString(name + " " + i, dtm.x + dtm.width - 30, dtm.y + dtm.height + 13);
  175. }
  176. }
  177.  
  178. public void fancydraw(Graphics g, Color recColor, boolean addname, boolean addX) {
  179. try {
  180. if(dtmLocations.isEmpty()) return;
  181. g.setFont(g.getFont().deriveFont((float) 9));
  182. for(int i = 0; i < dtmLocations.size(); i++) {
  183. drawDTMSquares(g, recColor, addname);
  184. }
  185. if(addX) drawX(g, dtmLocations.get(0));
  186. } catch (Exception e) {
  187. General.println("Exception occured in paint");
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement