PsyOps

Drone.Rotation

Jun 25th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // Need this for playerposition info
  7. using BattleCore.Events;
  8.  
  9. namespace Drone
  10. {
  11.     class Rotation
  12.     {
  13.         private byte _DefaultRotation;
  14.  
  15.         private ushort _DroneX;
  16.         private ushort _DroneY;
  17.  
  18.         /// <summary>
  19.         /// Set drone position
  20.         /// </summary>
  21.         /// <param name="x">x coord</param>
  22.         /// <param name="y">y coord</param>
  23.         /// <param name="rotation">rotation drone returns to when there are no targets.</param>
  24.         public void SetVariables(ushort x, ushort y, byte rotation)
  25.         {
  26.             _DroneX = x;
  27.             _DroneY = y;
  28.             _DefaultRotation = rotation;
  29.         }
  30.  
  31.         /// <summary>
  32.         /// This will increment 1 rotation toward the target.
  33.         /// </summary>
  34.         /// <param name="position">Player position packet.</param>
  35.         /// <param name="NewRotation">Rotation to target.</param>
  36.         public void UpdatedRotation(PlayerPositionEvent position, byte NewRotation)
  37.         {
  38.             if (position.ShipRotation != NewRotation)
  39.             {
  40.                 // Special circumstance if ActR == 19
  41.                 if ((position.ShipRotation == 39) && (NewRotation <= 19))
  42.                     position.ShipRotation = 0;
  43.                 // Special circumstance if ActR == 0
  44.                 else if (position.ShipRotation == 0)
  45.                 {
  46.                     if (NewRotation <= 20)
  47.                         position.ShipRotation++;
  48.                     else
  49.                         position.ShipRotation = 39;
  50.                 }
  51.                 else if (NewRotation > position.ShipRotation)
  52.                 {
  53.                     if (NewRotation - position.ShipRotation < 20)
  54.                         position.ShipRotation++;
  55.                     else
  56.                         position.ShipRotation--;
  57.                 }
  58.                 else
  59.                 {
  60.                     if (position.ShipRotation - NewRotation < 20)
  61.                         position.ShipRotation--;
  62.                     else
  63.                         position.ShipRotation++;
  64.                 }
  65.             }
  66.         }
  67.         /// <summary>
  68.         /// If no target is found in the area have drone return to default rotation.
  69.         /// </summary>
  70.         /// <param name="position">Player position packet.</param>
  71.         public void NoTarget(PlayerPositionEvent position)
  72.         {
  73.             UpdatedRotation(position, _DefaultRotation);
  74.         }
  75.         public byte RotationToTarget(ushort PlayerPosX, ushort PlayerPosY)
  76.         {
  77.             //setting info to variables (i can take this out later)
  78.             ushort play_x = PlayerPosX;
  79.             ushort play_y = PlayerPosY;
  80.             ushort bot_x = _DroneX;
  81.             ushort bot_y = _DroneY;
  82.  
  83.             // Formula to extract an angle
  84.             double angle = Math.Atan((double)(play_y - bot_y) / (double)(play_x - bot_x)) * (180.0 / 3.14159);
  85.             byte rot = 0;
  86.  
  87.             // with formula we get 0-90 degrees for each quadrant
  88.             // here we simply find out which quadrant it is and add the proper increment to make it rotation 0-39
  89.             if (play_x > bot_x)
  90.             {
  91.                 if (play_y > bot_y)// Lower Right
  92.                 {
  93.                     rot = (byte)Math.Floor((angle / 9) + 10);
  94.                 }
  95.                 else if (play_y < bot_y)// Top Right
  96.                 {
  97.                     rot = (byte)Math.Floor((90 + angle) / 9);
  98.                 }
  99.             }
  100.             else if (play_x < bot_x)
  101.             {
  102.                 if (play_y > bot_y)// Lower Left
  103.                 {
  104.                     rot = (byte)Math.Floor(((90 + angle) / 9) + 19);
  105.                 }
  106.                 else if (play_y < bot_y)// Top Left
  107.                 {
  108.                     rot = (byte)Math.Floor((angle / 9) + 29);
  109.                 }
  110.             }
  111.             return rot;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment