PsyOps

WeaponCalc

Nov 14th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore.Events;
  7. using BattleCore;
  8.  
  9. namespace Sentinel
  10. {
  11.     class WeaponCalc
  12.     {
  13.         private ushort[] bombStart, position;
  14.         private int[] bombVel, playerVel;
  15.         private int bombForce, intercept;
  16.         private double slope;
  17.         private int delay = 9750;
  18.  
  19.         //begins at cos 90 degrees and goes counterclockwise - light
  20.         private double[] xChange = new[] {  0.001, -0.156, -0.309, -0.454, -0.588, -0.707, -0.809, -0.891, -0.951, -0.988,
  21.                         -1, -0.988, -0.951, -0.891, -0.809, -0.707, -0.588, -0.454, -0.309, -0.156,
  22.                          0.001,  0.156,  0.309,  0.454,  0.588,  0.707,  0.848,  0.891,  0.951,  0.988,
  23.                          1,  0.988,  0.951,  0.891,  0.809,  0.707,  0.588,  0.454,  0.309,  0.156 };
  24.         //begins at sin 90 degrees and goes counterclockwise - light
  25.         private double[] yChange = new[] {  1,  0.988,  0.951,  0.891,  0.809,  0.707,  0.588,  0.454,  0.309,  0.156,
  26.                          0.001, -0.156, -0.309, -0.454, -0.588, -0.707, -0.809, -0.891, -0.951, -0.988,
  27.                         -1, -0.988, -0.951, -0.891, -0.809, -0.707, -0.588, -0.454, -0.309, -0.156,
  28.                          0.001,  0.156,  0.309,  0.454,  0.588,  0.707,  0.809,  0.891,  0.951,  0.988 };
  29.  
  30.         /// <summary>
  31.         /// This will configure your weapon for storing.
  32.         /// </summary>
  33.         public string Configure(ushort[] Target, PlayerPositionEvent p)
  34.         {
  35.             position = Target;
  36.  
  37.             int bombXVel, bombYVel;
  38.             int playerXVel, playerYVel;
  39.  
  40.             // If the player being tracked has fired a bomb
  41.             // Save the spot where the bomb was fired
  42.             bombStart = new ushort[2] { p.MapPositionX, p.MapPositionY };
  43.  
  44.             // Compute the velocity of the bomb
  45.             bombXVel = (int)(-xChange[p.ShipRotation] * 2000);
  46.             bombYVel = (int)(-yChange[p.ShipRotation] * 2000);
  47.  
  48.  
  49.             // Convert the player's velocity out of ss format
  50.             playerXVel = (int)(p.VelocityX);
  51.             if (playerXVel > (ushort.MaxValue / 2))
  52.                 playerXVel = playerXVel - ushort.MaxValue;
  53.  
  54.             playerYVel = (int)(p.VelocityY);
  55.             if (playerYVel > (ushort.MaxValue / 2))
  56.                 playerYVel = playerYVel - ushort.MaxValue;
  57.  
  58.  
  59.             // Add the reformatted velocity to the bomb velocity
  60.             bombXVel = bombXVel + playerXVel;
  61.             if (bombXVel > (ushort.MaxValue / 2))
  62.                 bombXVel = bombXVel - ushort.MaxValue;
  63.  
  64.             bombYVel = bombYVel + playerYVel;
  65.             if (bombYVel > (ushort.MaxValue / 2))
  66.                 bombYVel = bombYVel - ushort.MaxValue;
  67.  
  68.             bombForce = (int)(Math.Sqrt(bombXVel * bombXVel + bombYVel * bombYVel));
  69.             bombVel = new int[2] { bombXVel, bombYVel };
  70.             playerVel = new int[2] { playerXVel, playerYVel };
  71.  
  72.             string result = CalculateHit();
  73.  
  74.             if (result == "~miss")
  75.                 return result;
  76.             else return result;
  77.         }
  78.         private string CalculateHit()
  79.         {
  80.             string impact = "~miss";
  81.  
  82.             bool checkhit = false;
  83.             ushort bombXStart = bombStart[0];
  84.             ushort bombYStart = bombStart[1];
  85.             int bombXVel = bombVel[0];
  86.             int bombYVel = bombVel[1];
  87.             int playerXVel = playerVel[0];
  88.             int playerYVel = playerVel[1];
  89.             int x1 = position[0];
  90.             int y1 = position[1];
  91.             int x2 = position[2];
  92.             int y2 = position[3];
  93.  
  94.             // Calculate the Y-intercept of the bomb's trajectory
  95.             slope = (double)(bombYVel) / (double)(bombXVel);
  96.             intercept = (int)(bombYStart - (slope * bombXStart));
  97.  
  98.             if (bombXVel == 0)
  99.             {
  100.                 // Special consideration if the bomb's slope is technically infinity
  101.                 if ((bombXStart > x1) && (bombXStart < x2))
  102.  
  103.                     checkhit = true;
  104.             }
  105.             else if (slope < 0)
  106.             {
  107.                 if (((y1 - (slope * x1)) <= intercept) && (intercept <= (y2 - (slope * x2))))
  108.                     checkhit = true;
  109.             }
  110.             else
  111.             {
  112.                 if (((y2 - (slope * x1)) >= intercept) && (intercept >= (y1 - (slope * x2))))
  113.                     checkhit = true;
  114.             }
  115.             if (checkhit)
  116.             {
  117.                 bombStart = new ushort[2] { bombXStart, bombYStart };
  118.                 bombVel = new int[2] { bombXVel, bombYVel };
  119.                 playerVel = new int[2] { playerXVel, playerYVel };
  120.  
  121.                 // Determine if the bomb is going the correct way to hit the box
  122.                 if (slope < 0)
  123.                 {
  124.                     if ((bombStart[1] >= position[1]) && (bombVel[1] < 0))
  125.                     {
  126.                         if (intercept <= (position[3] - (slope * position[0])))
  127.                             //Hit on the west wall
  128.                             impact = "x1";
  129.                         else
  130.                             // Hit on the south wall
  131.                             impact = "y2";
  132.                     }
  133.                     else if ((bombStart[1] <= position[3]) && (bombVel[1] > 0))
  134.                     {
  135.                         if (intercept <= (position[1] - (slope * position[2])))
  136.                             //Hit on the north wall
  137.                             impact = "y1";
  138.                         else
  139.                             //Hit on the east wall
  140.                             impact = "x2";
  141.                     }
  142.                 }
  143.                 else
  144.                 {
  145.                     if ((bombStart[0] <= position[2]) && (bombVel[0] > 0))
  146.                     {
  147.                         if (intercept >= (position[1] - (slope * position[0])))
  148.                             // Hit on the west wall
  149.                             impact = "x1";
  150.                         else
  151.                             // Hit on the north wall
  152.                             impact = "y1";
  153.                     }
  154.                     else if ((bombStart[0] >= position[0]) && (bombVel[0] < 0))
  155.                     {
  156.                         if (intercept >= (position[3] - (slope * position[2])))
  157.                             // Hit on the south wall
  158.                             impact = "y2";
  159.                         else
  160.                             // Hit on the east wall
  161.                             impact = "x2";
  162.                     }
  163.                 }
  164.             }
  165.  
  166.             return impact;
  167.         }
  168.         /// <summary>
  169.         /// Calculate time until weapon impact.
  170.         /// </summary>
  171.         /// <param name="CHresult"></param>
  172.         /// Send whatever was sent from Calculate Hit only if it is not "~miss" (Make sure to check)
  173.         /// <returns>Time it will take for a weapon to hit.</returns>
  174.         public double timeHit(string CHresult)
  175.         {
  176.             int coord, distance = 0;
  177.  
  178.             // Calculate the other half of the coordinate on the target needed to figure out the distance between
  179.             //  the player and the point of impact
  180.             if (CHresult == "x1")
  181.             {
  182.                 coord = (int)(slope * position[0]) + intercept;
  183.                 distance = (int)(Math.Sqrt((bombStart[1] - coord) * (bombStart[1] - coord) + (bombStart[0] - position[0]) * (bombStart[0] - position[0])));
  184.             }
  185.             else if (CHresult == "x2")
  186.             {
  187.                 coord = (int)(slope * position[2]) + intercept;
  188.                 distance = (int)(Math.Sqrt((bombStart[1] - coord) * (bombStart[1] - coord) + (bombStart[0] - position[2]) * (bombStart[0] - position[2])));
  189.             }
  190.             else if (CHresult == "y1")
  191.             {
  192.                 coord = (int)((position[1] - intercept) / slope);
  193.                 distance = (int)(Math.Sqrt((bombStart[1] - position[1]) * (bombStart[1] - position[1]) + (bombStart[0] - coord) * (bombStart[0] - coord)));
  194.             }
  195.             else if (CHresult == "y2")
  196.             {
  197.                 coord = (int)((position[3] - intercept) / slope);
  198.                 distance = (int)(Math.Sqrt((bombStart[1] - position[3]) * (bombStart[1] - position[3]) + (bombStart[0] - coord) * (bombStart[0] - coord)));
  199.             }
  200.  
  201.             // Estimate the time until impact
  202.             double time = ((double)(distance) / (double)(bombForce));
  203.             if (bombForce > 3500)
  204.                 if (distance < 350)
  205.                     // Reduce the delay a substantial amount for close range and high velocity
  206.                     time = time * (delay - 1000);
  207.                 else
  208.                     // Reduce the delay a fair bit for high velocity
  209.                     time = time * (delay - 500);
  210.             else if (bombForce > 2000)
  211.                 if (distance < 350)
  212.                     // Reduce the delay a fair bit for close and medium velocity
  213.                     time = time * (delay - 500);
  214.                 else
  215.                     // Reduce the delay somewhat for just medium velocity
  216.                     time = time * (delay - 250);
  217.             else
  218.                 if (distance < 350)
  219.                     // Reduce the delay a bit for close range low speed
  220.                     time = time * (delay - 250);
  221.                 else
  222.                     time = time * delay;
  223.             return time;
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment