Advertisement
Guest User

ACamera

a guest
Jul 5th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. package scripts.fc.api;
  2.  
  3. import org.tribot.api.General;
  4. import org.tribot.api.interfaces.Positionable;
  5. import org.tribot.api2007.Camera;
  6. import org.tribot.api2007.Camera.ROTATION_METHOD;
  7. import org.tribot.api2007.Player;
  8. import org.tribot.script.Script;
  9. /**
  10.  *
  11.  * @author Final Calibur & WastedBro (no specific order)
  12.  *
  13.  */
  14. public class ACamera
  15. {
  16.     //Local constants
  17.     private final int ROTATION_THRESHOLD    = 30;   //We will not change the rotation when turning to a tile if the current rotation is +- this amount from the optimal value
  18.     private final int ANGLE_THRESHOLD       = 15;   //Same as above, but for angle
  19.    
  20.     //Private instance fields
  21.     private RotationThread          rotationThread; //The thread that will handle camera rotation
  22.     private AngleThread             angleThread;    //The thread that will handle camera angle
  23.     private Script                  script;         //The script we are handling the camera for. This is so we can know when to shut off the threads.
  24.     private boolean                 runsWithoutScript;
  25.    
  26.     /**
  27.      * Default constructor.
  28.      */
  29.     public ACamera(Script s)
  30.     {
  31.         instantiateVars(s);
  32.     }
  33.    
  34.     public ACamera()
  35.     {
  36.         this.runsWithoutScript = true;
  37.        
  38.         instantiateVars(null);
  39.     }
  40.    
  41.     /**
  42.      *  This method instantiates all of our variables for us.
  43.      *  I decided to make this method because it makes instantiating the variables in the constructors less redundant.
  44.      *
  45.      *  @param s script we are controlling camera for
  46.      *  @param sleepTime sleep time between cycles
  47.      */
  48.     private void instantiateVars(Script s)
  49.     {
  50.         this.script = s;
  51.         this.rotationThread = new RotationThread();
  52.         this.rotationThread.setName("ACamera Rotation Thread");
  53.         this.angleThread = new AngleThread();
  54.         this.angleThread.setName("ACamera Angle Thread");
  55.         this.rotationThread.start();
  56.         this.angleThread.start();
  57.         Camera.setRotationMethod(ROTATION_METHOD.ONLY_KEYS);
  58.     }
  59.    
  60.     /**
  61.      * Sets the camera angle
  62.      * @param specified angle to set camera to
  63.      */
  64.     public void setCameraAngle(int angle)
  65.     {
  66.         synchronized(this.angleThread)
  67.         {
  68.             this.angleThread.sleepTime = 0;
  69.             this.angleThread.angle = angle;
  70.             this.angleThread.notify();
  71.         }
  72.     }
  73.    
  74.     /**
  75.      * Sets the camera angle
  76.      */
  77.     public void setCameraAngle(int angle, long sleepTime)
  78.     {
  79.         synchronized(this.angleThread)
  80.         {
  81.             this.angleThread.sleepTime = sleepTime;
  82.             this.angleThread.angle = angle;
  83.             this.angleThread.notify();
  84.         }
  85.     }
  86.    
  87.     /**
  88.      * Sets the camera rotation
  89.      * @param rotation specified rotation to set camera to
  90.      */
  91.     public void setCameraRotation(int rotation)
  92.     {
  93.         synchronized(this.rotationThread)
  94.         {
  95.             this.rotationThread.rotation = rotation;
  96.             this.rotationThread.notify();
  97.         }
  98.     }
  99.    
  100.     /**
  101.      *  Turns the camera to a specific tile.
  102.      *
  103.      *  @param tile to turn to
  104.      */
  105.     public void turnToTile(Positionable tile)
  106.     {  
  107.         int optimalAngle = adjustAngleToTile(tile);
  108.         int optimalRotation = Camera.getTileAngle(tile);
  109.        
  110.         if(Math.abs(optimalAngle - Camera.getCameraAngle()) > ANGLE_THRESHOLD)
  111.         {
  112.             setCameraAngle(optimalAngle + General.random(-12, 12), calculateSleepTime());
  113.         }
  114.        
  115.         if(Math.abs(optimalRotation - Camera.getCameraRotation()) > ROTATION_THRESHOLD)
  116.         {
  117.             setCameraRotation(optimalRotation + General.random(-30, 30));
  118.         }        
  119.     }
  120.    
  121.     private long calculateSleepTime()
  122.     {
  123.         int diff    = Math.abs(Camera.getCameraRotation() - rotationThread.rotation);
  124.         int minTime = (int)(diff * 2.0);
  125.         int maxTime = (int)(diff * 3.0);
  126.        
  127.         return (General.random(minTime, maxTime));
  128.     }
  129.    
  130.     /**
  131.      * Adjusts the angle of the camera so that it is optimal for the tile
  132.      * @param tile Tile to adjust angle for
  133.      * @return the optimal angle to view this tile at
  134.      */
  135.     public int adjustAngleToTile(Positionable tile)
  136.     {
  137.         //Distance from player to object - Used in calculating the optimal angle.
  138.         //Objects that are farther away require the camera to be turned to a lower angle to increase viewing distance.
  139.         int distance = Player.getPosition().distanceTo(tile);
  140.        
  141.         //The angle is calculated by taking the max height (100, optimal for very close objects),
  142.         //and subtracting an arbitrary number (I chose 6 degrees) for every tile that it is away.
  143.         int angle = 100 - (distance * 6);
  144.        
  145.         return angle;
  146.     }
  147.    
  148.     private class RotationThread extends Thread
  149.     {  
  150.         protected int rotation = Camera.getCameraRotation();
  151.        
  152.         @Override
  153.         public synchronized void run()
  154.         {
  155.             try
  156.             {  
  157.                 while(script != null || runsWithoutScript)
  158.                 {              
  159.                     Camera.setCameraRotation(rotation);
  160.                        
  161.                     getRotationThread().wait();
  162.                 }
  163.             }
  164.             catch(Exception e)
  165.             {
  166.                 General.println("Error initiating wait on angle thread.");
  167.             }
  168.         }
  169.        
  170.     }
  171.        
  172.     private class AngleThread extends Thread
  173.     {
  174.         protected int   angle       = Camera.getCameraAngle();
  175.         protected long  sleepTime   = 0;
  176.                
  177.         @Override
  178.         public synchronized void run()
  179.         {
  180.             try
  181.             {  
  182.                 while(script != null || runsWithoutScript)
  183.                 {
  184.                     sleep(sleepTime);
  185.                    
  186.                     Camera.setCameraAngle(angle);
  187.                        
  188.                     getAngleThread().wait();      
  189.                 }
  190.             }
  191.             catch(Exception e)
  192.             {
  193.                 General.println("Error initiating wait on angle thread.");
  194.             }
  195.         }
  196.        
  197.     }
  198.  
  199.     public RotationThread getRotationThread()
  200.     {
  201.         return rotationThread;
  202.     }
  203.  
  204.     public AngleThread getAngleThread()
  205.     {
  206.         return angleThread;
  207.     }
  208.  
  209.     public Script getScript() {
  210.         return script;
  211.     }
  212.    
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement