Don't like ads? PRO users don't see any ads ;-)
Guest

MatrixRunespan.java

By: a guest on May 2nd, 2012  |  syntax: Java  |  size: 11.93 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Polygon;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.powerbot.concurrent.Task;
  10. import org.powerbot.concurrent.strategy.Strategy;
  11. import org.powerbot.game.api.Manifest;
  12. import org.powerbot.game.api.methods.Walking;
  13. import org.powerbot.game.api.methods.interactive.NPCs;
  14. import org.powerbot.game.api.methods.node.SceneEntities;
  15. import org.powerbot.game.api.methods.tab.Skills;
  16. import org.powerbot.game.api.methods.widget.Camera;
  17. import org.powerbot.game.api.util.Filter;
  18. import org.powerbot.game.api.wrappers.Entity;
  19. import org.powerbot.game.api.wrappers.Locatable;
  20. import org.powerbot.game.api.wrappers.Tile;
  21. import org.powerbot.game.api.wrappers.interactive.NPC;
  22. import org.powerbot.game.api.wrappers.node.SceneObject;
  23. import org.powerbot.game.bot.event.MessageEvent;
  24. import org.powerbot.game.bot.event.listener.MessageListener;
  25. import org.powerbot.game.bot.event.listener.PaintListener;
  26.  
  27.  
  28. @Manifest(authors = { "AlexDkk", "Dragonkk" }, name = "MatrixRunespan", version = 1.02, website = "http://wwww.matrixrsps.com"
  29. , description = "Train rc at best xp rate available!")
  30. public final class MatrixRunespan extends DragonkkScript implements MessageListener, PaintListener {
  31.  
  32.         private static final int FLOATING_ESSENCE = 15402;
  33.         private static final int ESSENCE = 24227;
  34.        
  35.         public static enum CREATURES {
  36.                 Air_essling(15403, 1, 9.5, false)
  37.                 ,Mind_essling(15404, 1, 10, false)
  38.                 ,Water_essling(15405, 5, 12.5, false)
  39.                 ,Earth_essling(15406, 9, 14.5, false)
  40.                 ,Fire_essling(15407, 14, 17.5, false)
  41.                 ,Body_esshound(15408, 20, 23.5, false)
  42.                 ,Cosmic_esshound(15409, 27, 26.5, true)
  43.                 ,Chaos_esshound(15410, 35, 30.66, true)
  44.                 ,Astral_esshound(15411, 40, 35.5, true)
  45.                 ,Nature_esshound(15412, 44, 43.5, true)
  46.                 ,Law_esshound(15413, 54, 54, true)
  47.                 ,Death_esswraith(15414, 66, 60, true)
  48.                 ,Blood_esswraith(15415, 77, 73, true)
  49.                 ,Soul_esswraith(15416, 90, 107, true)
  50.                 ;
  51.                
  52.                 private int id, level;
  53.                 private double experience;
  54.                 private boolean member;
  55.                
  56.                 private CREATURES(int id, int level, double experience, boolean member) {
  57.                         this.id = id;
  58.                         this.level = level;
  59.                         this.experience = experience;
  60.                         this.member = member;
  61.                 }
  62.         }
  63.        
  64.         public static enum NODES {
  65.                 Cyclone(70455, 1,false, 19)
  66.                 ,Mind_storm(70456, 1,false, 20)
  67.                 ,Water_pool(70457, 5,false, 25.5)
  68.                 ,Rock_fragment(70458, 9,false, 28.6)
  69.                 ,Fireball(70459, 14,false, 34.8)
  70.                 ,Vine(70460, 17,false, 32.5)
  71.                 ,Fleshy_growth(70461, 20,false, 46.2)
  72.                 ,Fire_storm(70462, 27, false, 22, 42)
  73.                 ,Chaotic_cloud(70463, 35,true, 61.5)
  74.                 ,Nebula(70464, 40, true, 64, 85.5)
  75.                 ,Shifter(70465, 44, true, 86.8)
  76.                 ,Jumper(70466, 54, true, 107)
  77.                 ,Skulls(70467, 66, true, 120)
  78.                 ,Blood_pool(70468, 77, true,146)
  79.                 ,Bloody_skulls(70469, 83, true, 144, 176)
  80.                 ,Living_soul(70470, 90, true, 213)
  81.                 ,Undead_soul(70471, 95, true, 144, 255.5)
  82.                 ;
  83.                
  84.                 private int id, level;
  85.                 private double[] experience;
  86.                 private boolean member;
  87.                
  88.                 private NODES(int id, int level, boolean member, double... experience) {
  89.                         this.id = id;
  90.                         this.level = level;
  91.                         this.member = member;
  92.                         this.experience = experience;
  93.                 }
  94.                
  95.                 private double getAverageExperience() {
  96.                         int exp = 0;
  97.                         for(double xp : experience)
  98.                                 exp += xp;
  99.                         return exp / experience.length;
  100.                 }
  101.         }
  102.        
  103.         private Entity currentEntity;
  104.         private int startExperience, killedCreatures, killedNodes;
  105.         private long startTime;
  106.         private boolean gatherRunes;
  107.         private boolean memberWorld;
  108.         private Map<Integer, Integer> npcEmotes;
  109.        
  110.        
  111.         public static CREATURES getCreature(int id) {
  112.                 for(CREATURES creature : CREATURES.values())
  113.                         if(creature.id == id)
  114.                                 return creature;
  115.                 return null;
  116.         }
  117.        
  118.         public static NODES getNode(int id) {
  119.                 for(NODES node : NODES.values())
  120.                         if(node.id == id)
  121.                                 return node;
  122.                 return null;
  123.         }
  124.        
  125.        
  126.         public boolean acceptDistance(Tile tile) {
  127.                 return tile.canReach();
  128.         }
  129.         public Entity getBestAvailableResource() {
  130.                
  131.                 final int currentRCLevel = Skills.getLevel(Skills.RUNECRAFTING);
  132.                 NPC[] possibleCreatures = NPCs.getLoaded(new Filter<NPC>() {
  133.  
  134.                         @Override
  135.                         public boolean accept(NPC paramT) {
  136.                                 if(!paramT.validate() || !acceptDistance(paramT.getLocation()))
  137.                                         return false;
  138.                                 CREATURES creature = getCreature(paramT.getId());
  139.                                 if(creature != null && creature.level <= currentRCLevel && !(!memberWorld && !creature.member)) {
  140.                                         Integer emote = npcEmotes.get(paramT.getId());
  141.                                         if(emote != null) {
  142.                                                 if(paramT.getAnimation() != -1 && paramT.getAnimation() != emote)
  143.                                                         return false;
  144.                                         }
  145.                                         return true;
  146.                                 }
  147.                                 return false;
  148.                         }
  149.                        
  150.                 });
  151.                
  152.                 SceneObject[] possibleNodes = gatherRunes ? new SceneObject[0] : SceneEntities.getLoaded(new Filter<SceneObject>() {
  153.  
  154.                         @Override
  155.                         public boolean accept(SceneObject paramT) {
  156.                                 if(!paramT.validate() || !acceptDistance(paramT.getLocation()))
  157.                                         return false;
  158.                                 NODES node = getNode(paramT.getId());
  159.                                 return node != null && node.level <= currentRCLevel && !(!memberWorld && !node.member);
  160.                         }
  161.                        
  162.                 });
  163.                
  164.                 double experience = 0;
  165.                 List<Entity> entities = new ArrayList<Entity>();
  166.                 for(NPC n : possibleCreatures) {
  167.                         double xp = getCreature(n.getId()).experience;
  168.                         if(xp > experience) {
  169.                                 entities.clear();
  170.                                 entities.add(n);
  171.                                 experience = xp;
  172.                         }else if (xp == experience)
  173.                                 entities.add(n);
  174.                 }
  175.                 for(SceneObject o : possibleNodes) {
  176.                         double xp = getNode(o.getId()).getAverageExperience();
  177.                         if(xp > experience) {
  178.                                 entities.clear();
  179.                                 entities.add(o);
  180.                                 experience = xp;
  181.                         }else if (xp == experience)
  182.                                 entities.add(o);
  183.                 }
  184.                 if(entities.isEmpty())
  185.                         return null;
  186.                 Entity resource = null;
  187.                 int distance = Integer.MAX_VALUE;
  188.                 Tile myLoc = getMyPlayer().getLocation();
  189.                 for(Entity possiblity : entities) {
  190.                         int dis = Calc.distanceBetween(myLoc, ((Locatable) possiblity).getLocation());
  191.                         if(dis < distance) {
  192.                                 distance = dis;
  193.                                 resource = possiblity;
  194.                         }
  195.                 }
  196.                 return resource;
  197.                
  198.         }
  199.        
  200.         public boolean needsEntityChange(Entity nextEntity) {
  201.                 if(currentEntity == null
  202.                                 || currentEntity.getClass() != nextEntity.getClass())
  203.                         return true;
  204.                 if(nextEntity instanceof SceneObject) {
  205.                         SceneObject nextObject = (SceneObject) nextEntity;
  206.  
  207.                         SceneObject currentObject = (SceneObject) currentEntity;
  208.                         return nextObject.getId() != currentObject.getId();
  209.                 }else if(nextEntity instanceof NPC) {
  210.                         NPC nextNPC = (NPC) nextEntity;
  211.                         NPC currentNPC = (NPC) currentEntity;
  212.                         return nextNPC.getId() != currentNPC.getId();
  213.                 }
  214.                 return true;
  215.                
  216.         }
  217.        
  218.         public boolean setNextEntity(Entity nextEntity) {
  219.                 currentEntity = nextEntity;
  220.                 if(!nextEntity.isOnScreen())
  221.                         Camera.turnTo((Locatable) nextEntity, 15);
  222.                 if(!nextEntity.isOnScreen())
  223.                         Walking.walk(((Locatable) nextEntity).getLocation());
  224.                 return nextEntity.interact("Siphon");
  225.         }
  226.        
  227.         public int getEarnedExperience() {
  228.                 return Skills.getExperience(Skills.RUNECRAFTING) - startExperience;
  229.         }
  230.        
  231.         @Override
  232.         protected void setup() {
  233.                 npcEmotes = new HashMap<Integer, Integer>();
  234.                 super.setup();
  235.                 provide(new Strategy(this, new Task() {
  236.  
  237.                         @Override
  238.                         public void run() {
  239.                                 setEmote();
  240.                         }
  241.                        
  242.                 }));
  243.         }
  244.        
  245.         @Override
  246.         public boolean onStart() {
  247.                 startTime = System.currentTimeMillis();
  248.                 startExperience = Skills.getExperience(Skills.RUNECRAFTING);
  249.                 memberWorld = GameEx.isAtMemberWorld();
  250.                 return true;
  251.         }
  252.  
  253.         public int getRunesCount() {
  254.                 return InventoryEx.getQuantity(ESSENCE);
  255.         }
  256.        
  257.         @Override
  258.         public int loop() {
  259.                 //widget closer does it
  260.         /*      WidgetChild component = Widgets.get(1218, 73);
  261.                 if(component.isOnScreen()) {
  262.                         component.click(true);
  263.                         return 600;
  264.                 }*/
  265.                 if(getRunesCount() == 0) {
  266.                         if(!getMyPlayer().isMoving() && getMyPlayer().getAnimation() == -1) {
  267.                                 NPC n = NPCs.getNearest(FLOATING_ESSENCE);
  268.                                 if(!n.isOnScreen())
  269.                                         Camera.turnTo(n, 15);
  270.                                 n.interact("Collect");
  271.                                 gatherRunes = true;
  272.                         }
  273.                         return 600;
  274.                 }else if (gatherRunes && getRunesCount() >= 100)
  275.                         gatherRunes = false;
  276.                 if (currentEntity instanceof NPC) {
  277.                         NPC n = (NPC) currentEntity;
  278.                         Integer emote = npcEmotes.get(n.getId());
  279.                         if(emote != null) {
  280.                                 if(n.getAnimation() != -1 && n.getAnimation() != emote)
  281.                                         currentEntity = null;
  282.                         }
  283.                 }
  284.                 if(currentEntity != null && getMyPlayer().getAnimation() != 16596 && !getMyPlayer().isMoving()) {
  285.                         if(!currentEntity.isOnScreen())
  286.                                 currentEntity = null;
  287.                         else {
  288.                                 sleep(3600);
  289.                                 if(getMyPlayer().getAnimation() != 16596 && !getMyPlayer().isMoving())
  290.                                         currentEntity = null;
  291.                         }
  292.                 }
  293.                 Entity nextEntity = getBestAvailableResource();
  294.                 if(nextEntity != null && needsEntityChange(nextEntity))
  295.                         if(!setNextEntity(nextEntity))
  296.                                 currentEntity = null;
  297.                         return 600;
  298.         }
  299.        
  300.         public void setEmote() {
  301.                 if (currentEntity instanceof NPC) {
  302.                         NPC n = (NPC) currentEntity;
  303.                         if(!npcEmotes.containsKey(n.getId())) {
  304.                                 int emote = n.getAnimation();
  305.                                 if(emote != -1)  {
  306.                                         npcEmotes.put(n.getId(), emote);
  307.                                 }
  308.                         }
  309.                 }
  310.        
  311.         }
  312.  
  313.         @Override
  314.         public void messageReceived(MessageEvent paramMessageEvent) {
  315.                 String message = paramMessageEvent.getMessage();
  316.                 if(message.equals("You pick up the essence left by the creature."))
  317.                         killedCreatures++;
  318.                 else if(message.equals("The node you were siphoning from has been depleted of energy."))
  319.                         killedNodes++;
  320.         /*      switch(paramMessageEvent.getMessage()) {
  321.                 case "You pick up the essence left by the creature.":
  322.                         killedCreatures++;
  323.                 break;
  324.                 case "The node you were siphoning from has been depleted of energy.":
  325.                         killedNodes++;
  326.                 break;
  327.                        
  328.                 }*/
  329.         }
  330.  
  331.         private static String getFormatedTime(long seconds) {
  332.                 long minutes = seconds / 60;
  333.                 long hours = minutes / 60;
  334.                 minutes -= hours * 60;
  335.                 seconds -= (hours * 60 * 60) + (minutes * 60);
  336.                 String minutesString = (minutes < 10 ? "0" : "") + minutes;
  337.                 String secondsString = (seconds < 10 ? "0" : "") + seconds;
  338.                 return hours+":"+minutesString+":"+secondsString;
  339.         }
  340.        
  341.         private static void drawString(Graphics g, String s, Color c, int x, int y) {
  342.                 g.setColor(Color.BLACK);
  343.                 g.drawString(s, x + 1, y + 1);
  344.                 g.setColor(c);
  345.                 g.drawString(s, x, y);
  346.                
  347.         }
  348.        
  349.         public String getName() {
  350.                 try {
  351.                         Entity e = currentEntity;
  352.                         if(e == null || !e.validate())
  353.                                 return "Nothing";
  354.                         else if(e instanceof NPC) {
  355.                                 NPC n = (NPC) e;
  356.                                 return n.getName();
  357.                         }else if(e instanceof SceneObject) {
  358.                                 SceneObject o = (SceneObject) e;
  359.                                 return o.getDefinition().getName();
  360.                         }
  361.                 }catch(Throwable e) { //getname gives errors sometimes
  362.                         currentEntity = null;
  363.                 }
  364.                 return "Unknown";
  365.         }
  366.         @Override
  367.         public void onRepaint(Graphics g) {
  368.                 int x = 350;
  369.                 int y = 210;
  370.                 g.setColor(new Color(64, 64, 64, 128));
  371.                 g.fillRoundRect(x - 10, y - 20, 170, 110 + 30, 20, 20);
  372.                 drawString(g, "Matrix Runespan version 1.02", Color.CYAN, x, y);
  373.                 drawString(g, "by Dragonkk", Color.CYAN, x, y + 10);
  374.                 int seconds = (int)((System.currentTimeMillis() - startTime) / 1000);
  375.                 drawString(g, "Running for " + getFormatedTime(seconds), Color.WHITE, x, y + 30);
  376.                 drawString(g, "Siphoning: " + getName(), Color.WHITE, x, y + 50);
  377.                 drawString(g, "Creatures killed: " + killedCreatures, Color.YELLOW, x, y + 70);
  378.                 drawString(g, "Nodes killed: " + killedNodes, Color.YELLOW, x, y + 90);
  379.                 double totalXp = getEarnedExperience();
  380.                 drawString(g, "Experience: " + totalXp + " [" + (int)(totalXp / seconds * 3600) + "/h]", Color.GREEN, x, y + 110);
  381.                 Entity e = currentEntity;
  382.                 if(e != null && e.validate()) {
  383.                         if(e instanceof NPC) {
  384.                                 g.setColor(Color.GREEN);
  385.                                 NPC n = (NPC) e;
  386.                                 Polygon[] bounds = n.getBounds();
  387.                                 for(Polygon p : bounds)
  388.                                         g.drawPolygon(p);
  389.                         }else if(e instanceof SceneObject) {
  390.                                 g.setColor(Color.RED);
  391.                                 SceneObject o = (SceneObject) e;
  392.                                 Polygon[] bounds = o.getBounds();
  393.                                 for(Polygon p : bounds)
  394.                                         g.drawPolygon(p);
  395.                         }
  396.                 }
  397.                 g.setColor(new Color(0, 255, 255, 50));
  398.         }
  399.  
  400. }