Advertisement
Meridius_IX

Wandering Turret Aim Script

Dec 13th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. //Meridius_IX's Wandering Turret Aim Script
  2.  
  3. IMyBlockGroup turretGroup;
  4. Random rnd = new Random();
  5.  
  6. void Main(string argument){
  7.  
  8.     float maxElevation = 0.5f;
  9.     float maxAzimuth = 0.5f;
  10.     turretGroup = null;
  11.     List<IMyBlockGroup> blockGroups = new List<IMyBlockGroup>();
  12.     List<IMyLargeTurretBase> turretList = new List<IMyLargeTurretBase>();
  13.     GridTerminalSystem.GetBlockGroups(blockGroups);
  14.  
  15.     if(blockGroups.Count != 0){
  16.        
  17.         foreach(IMyBlockGroup group in blockGroups){
  18.            
  19.             if(argument.Contains(group.Name) == true){
  20.                
  21.                 turretGroup = group;
  22.                 break;
  23.                
  24.             }
  25.            
  26.         }
  27.        
  28.     }
  29.  
  30.     if(turretGroup != null){
  31.        
  32.         turretGroup.GetBlocksOfType<IMyLargeTurretBase>(turretList);
  33.        
  34.     }
  35.  
  36.     if(turretList.Count != 0){
  37.        
  38.        
  39.        
  40.         string [] argSplit = argument.Split('|');
  41.         if(argSplit.Length <= 3){
  42.            
  43.             Echo("Argument Format Invalid, Please Check.");
  44.             return;
  45.            
  46.         }else{
  47.            
  48.             float.TryParse(argSplit[2], out maxElevation);
  49.             float.TryParse(argSplit[3], out maxAzimuth);
  50.            
  51.         }
  52.        
  53.         foreach(IMyLargeTurretBase turret in turretList){
  54.            
  55.             float elevationWander = 0.1f;
  56.             float azimuthWander = 0.1f;
  57.            
  58.             if(turret.IsFunctional == false){
  59.                
  60.                 continue;
  61.                
  62.             }
  63.            
  64.             //Get/Init Custom Data
  65.             if(turret.CustomData.Contains("AimWander") == false){
  66.                
  67.                 turret.CustomData = "AimWander|";
  68.                 elevationWander = (float)RandomNumberBetween(0.005f, 0.015f);
  69.                 azimuthWander = (float)RandomNumberBetween(0.005f, 0.015f);
  70.                 turret.CustomData += elevationWander.ToString() + "|";
  71.                 turret.CustomData += azimuthWander.ToString();
  72.                
  73.             }else{
  74.                
  75.                 string [] dataSplit = turret.CustomData.Split('|');
  76.                 float.TryParse(dataSplit[1], out elevationWander);
  77.                 float.TryParse(dataSplit[2], out azimuthWander);
  78.                
  79.             }
  80.            
  81.             turret.Elevation += elevationWander;
  82.             Echo(turret.Elevation.ToString());
  83.             Echo(elevationWander.ToString());
  84.             Echo(maxElevation.ToString() + "\n");
  85.            
  86.             if(elevationWander > 0){
  87.                
  88.                 if(turret.Elevation > maxElevation){
  89.                    
  90.                     elevationWander = (float)RandomNumberBetween(-0.015f, -0.005f);
  91.                    
  92.                 }
  93.                
  94.             }else{
  95.                
  96.                 if(turret.Elevation < maxElevation * -1){
  97.                    
  98.                     elevationWander = (float)RandomNumberBetween(0.005f, 0.015f);
  99.                    
  100.                 }
  101.                
  102.             }
  103.            
  104.             turret.Azimuth += azimuthWander;
  105.            
  106.             if(azimuthWander > 0){
  107.                
  108.                 if(turret.Azimuth > maxAzimuth){
  109.                    
  110.                     azimuthWander = (float)RandomNumberBetween(-0.015f, -0.005f);
  111.                    
  112.                 }
  113.                
  114.             }else{
  115.                
  116.                 if(turret.Azimuth < maxAzimuth * -1){
  117.                    
  118.                     azimuthWander = (float)RandomNumberBetween(0.005f, 0.015f);
  119.                    
  120.                 }
  121.                
  122.             }
  123.            
  124.             if(argument.Contains("BroadsideOn")){
  125.                
  126.                 turret.ApplyAction("ShootOnce");
  127.                 turret.ApplyAction("Shoot_On");
  128.                
  129.             }
  130.            
  131.             if(argument.Contains("BroadsideOff")){
  132.                
  133.                 turret.ApplyAction("Shoot_Off");
  134.                
  135.             }
  136.            
  137.             if(argument.Contains("BroadsideToggle")){
  138.                
  139.                 turret.ApplyAction("Shoot");
  140.                
  141.             }
  142.            
  143.             turret.CustomData = "AimWander|";
  144.             turret.CustomData += elevationWander.ToString() + "|";
  145.             turret.CustomData += azimuthWander.ToString();
  146.            
  147.         }
  148.        
  149.     }
  150.  
  151. }
  152.  
  153. double RandomNumberBetween(float minValue, float maxValue){
  154.  
  155.     var next = rnd.NextDouble();
  156.     return minValue + (next * (maxValue - minValue));
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement