Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.40 KB | None | 0 0
  1. //  This script mines ore from asteroids.
  2. //  before running this script, make sure to prepare as follows:
  3. //  +enter bookmark for mining site and bookmark for station in the configuration section below.
  4. //  +in the Overview create a preset which includes asteroids and rats and enter the name of that preset in the configuration section below at 'OverviewPreset'. The bot will make sure this preset is loaded when it needs to use the overview.
  5. //  +set Overview to sort by distance with the nearest entry at the top.
  6. //  +in the Inventory select the 'List' view.
  7. //  +set the UI language to english.
  8. //  +use a ship with an ore hold.
  9. //  +put some light drones into your ships' drone bay. The bot will make use of them to attack rats when HP are too low (configurable) or it gets jammed.
  10. //  +enable the info panel 'System info'. The bot will use the button in there to access bookmarks and asteroid belts.
  11. //  +arrange windows to not occlude modules or info panels.
  12. //  +in the ship UI, disable "Display Passive Modules" and disable "Display Empty Slots" and enable "Display Module Tooltips". The bot uses the module tooltips to automatically identify the properties of the modules.
  13.  
  14. using Parse = Sanderling.Parse;
  15.  
  16. //  begin of configuration section ->
  17.  
  18. //  The bot uses the bookmarks from the menu which is opened from the button in the 'System info' panel.
  19. //  Bookmark to station where ore should be transfered to item hangar.
  20. string StationOrPosBookmark = "Hangar";
  21. //  Bookmark to place with asteroids.
  22. string MiningSiteBookmark = "Mine";
  23.  
  24. //if  Use a Station
  25. bool UseStation = false;
  26.  
  27. // Container name to push it on a pos or POSContainer =  "Item hangar" in station
  28. string DestinationContainerName ="Without";
  29.  
  30. Random genNbr=new Random();
  31. int nextNbr;
  32.  
  33. //  The bot loads this preset to the active tab.
  34. string OverviewPreset = "mining";
  35.  
  36. var ActivateHardener = true;
  37.  
  38. //  bot will start fighting (and stop mining) when hitpoints are lower.
  39. var DefenseEnterHitpointThresholdPercent = 85;
  40. var DefenseExitHitpointThresholdPercent = 90;
  41.  
  42. var EmergencyWarpOutHitpointPercent = 60;
  43.  
  44. var FightAllRats = true;    //  when this is set to true, the bot will attack rats independent of shield hp.
  45.  
  46. //  <- end of configuration section
  47.  
  48. Func<object> BotStopActivity = () => new object();
  49.  
  50. Func<object> NextActivity = MainStep;
  51.  
  52. for(;;)
  53. {
  54.     MemoryUpdate();
  55.  
  56.     Host.Log(
  57.         "ore hold fill: " + OreHoldFillPercent + "%" +
  58.         ", mining range: " + MiningRange +
  59.         ", mining modules (inactive): " + SetModuleMiner?.Length + "(" + SetModuleMinerInactive?.Length + ")" +
  60.         ", shield.hp: " + ShieldHpPercent + "%" +
  61.         ", EWO: " + EmergencyWarpOutEnabled.ToString() +
  62.         ", JLA: " + JammedLastAge +
  63.         ", overview.rats: " + ListRatOverviewEntry?.Length +
  64.         ", overview.roids: " + ListAsteroidOverviewEntry?.Length +
  65.         ", offload count: " + OffloadCount +
  66.         ", nextAct: " + NextActivity?.Method?.Name);
  67.  
  68.     CloseModalUIElement();
  69.    
  70.     if(EmergencyWarpOutEnabled)
  71.         if(!(Measurement?.IsDocked ?? false))
  72.         {
  73.             InitiateDockToOrWarpToBookmark(StationOrPosBookmark);
  74.             continue;
  75.         }
  76.    
  77.     NextActivity = NextActivity?.Invoke() as Func<object>;
  78.  
  79.     if(BotStopActivity == NextActivity)
  80.         break;
  81.    
  82.     if(null == NextActivity)
  83.         NextActivity = MainStep;
  84.    
  85.     Host.Delay(1111);
  86. }
  87.  
  88. //  seconds since ship was jammed.
  89. long? JammedLastAge => Jammed ? 0 : (Host.GetTimeContinuousMilli() - JammedLastTime) / 1000;
  90.  
  91. int?    ShieldHpPercent => ShipUi?.HitpointsAndEnergy?.Shield / 10;
  92.  
  93. bool    DefenseExit =>
  94.     (Measurement?.IsDocked ?? false) ||
  95.     !(0 < ListRatOverviewEntry?.Length) ||
  96.     (DefenseExitHitpointThresholdPercent < ShieldHpPercent && !(JammedLastAge < 40) &&
  97.     !(FightAllRats && 0 < ListRatOverviewEntry?.Length));
  98.  
  99. bool    DefenseEnter =>
  100.     !DefenseExit    ||
  101.     !(DefenseEnterHitpointThresholdPercent < ShieldHpPercent) || JammedLastAge < 10;
  102.  
  103. bool    OreHoldFilledForOffload => 97 < OreHoldFillPercent;
  104.  
  105. Int64?  JammedLastTime = null;
  106. bool    EmergencyWarpOutEnabled = false;
  107. int? LastCheckOreHoldFillPercent = null;
  108.  
  109. int OffloadCount = 0;
  110.  
  111.  
  112.  
  113. Func<object>    MainStep()
  114. {
  115.        var    DestinationContainer = WindowInventory?.LeftTreeListEntry?.SelectMany(entry => new[]{entry}.Concat(entry.EnumerateChildNodeTransitive()))?.FirstOrDefault(entry => string.Equals(entry?.Text, DestinationContainerName, StringComparison.InvariantCultureIgnoreCase));
  116.        
  117.        if(UseStation){
  118.         if(Measurement?.IsDocked ?? false)
  119.         {
  120.             UnloadToHangar(DestinationContainerName);
  121.    
  122.             if (EmergencyWarpOutEnabled)
  123.                 return BotStopActivity;
  124.    
  125.             Undock();
  126.         }
  127.        }
  128.        else{
  129.         if(DestinationContainer != null)
  130.         {
  131.             UnloadToHangar(DestinationContainerName);
  132.    
  133.             if (EmergencyWarpOutEnabled)
  134.                 return BotStopActivity;
  135.         }
  136.     }
  137.    
  138.     if(DefenseEnter)
  139.     {
  140.         Host.Log("enter defense.");
  141.         return DefenseStep;
  142.     }
  143.  
  144.     EnsureOverviewTypeSelectionLoaded();
  145.  
  146.     EnsureWindowInventoryOpenOreHold();
  147.  
  148.     if(ReadyForManeuver)
  149.     {
  150.         DroneEnsureInBay();
  151.  
  152.         if(OreHoldFilledForOffload)
  153.         {
  154.             if(ReadyForManeuver)
  155.             InitiateDockToOrWarpToBookmark(StationOrPosBookmark);
  156.             return MainStep;
  157.         }
  158.        
  159.         if(!(0 < ListAsteroidOverviewEntry?.Length)){
  160.             nextNbr = genNbr.Next(1,34);
  161.             InitiateDockToOrWarpToBookmark(MiningSiteBookmark + nextNbr);
  162.             }
  163.     }
  164.  
  165.     ModuleMeasureAllTooltip();
  166.  
  167.     if(ActivateHardener)
  168.         ActivateHardenerExecute();
  169.  
  170.     return InBeltMineStep;
  171. }
  172.  
  173. void CloseModalUIElement()
  174. {
  175.     var ButtonClose =
  176.         ModalUIElement?.ButtonText?.FirstOrDefault(button => (button?.Text).RegexMatchSuccessIgnoreCase("close|no|ok"));
  177.        
  178.     Sanderling.MouseClickLeft(ButtonClose);
  179. }
  180.  
  181. void DroneLaunch()
  182. {
  183.     Host.Log("launch drones.");
  184.     Sanderling.MouseClickRight(DronesInBayListEntry);
  185.     Sanderling.MouseClickLeft(Menu?.FirstOrDefault()?.EntryFirstMatchingRegexPattern("launch", RegexOptions.IgnoreCase));
  186.     CloseModalUIElement();
  187. }
  188.  
  189. void DroneEnsureInBay()
  190. {
  191.     if(0 == DronesInSpaceCount)
  192.         return;
  193.        
  194.     while(DronesInSpaceCount != 0)
  195.     DroneReturnToBay();
  196.    
  197.     Host.Delay(4444);
  198. }
  199.  
  200. void DroneReturnToBay()
  201. {
  202.     Host.Log("return drones to bay.");
  203.     Sanderling.MouseClickRight(DronesInSpaceListEntry);
  204.     Sanderling.MouseClickLeft(Menu?.FirstOrDefault()?.EntryFirstMatchingRegexPattern("return.*bay", RegexOptions.IgnoreCase));
  205. }
  206.  
  207. Func<object>    DefenseStep()
  208. {
  209.     if(DefenseExit)
  210.     {
  211.         Host.Log("exit defense.");
  212.         return null;
  213.     }
  214.  
  215.     if (!(0 < DronesInSpaceCount))
  216.         DroneLaunch();
  217.  
  218.     EnsureOverviewTypeSelectionLoaded();
  219.  
  220.     var SetRatName =
  221.         ListRatOverviewEntry?.Select(entry => Regex.Split(entry?.Name ?? "", @"\s+")?.FirstOrDefault())
  222.         ?.Distinct()
  223.         ?.ToArray();
  224.    
  225.     var SetRatTarget = Measurement?.Target?.Where(target =>SetRatName?.Any(ratName => target?.TextRow?.Any(row => row.RegexMatchSuccessIgnoreCase(ratName)) ?? false) ?? false);
  226.    
  227.     var RatTargetNext = SetRatTarget?.OrderBy(target => target?.DistanceMax ?? int.MaxValue)?.FirstOrDefault();
  228.    
  229.     if(null == RatTargetNext)
  230.     {
  231.         Host.Log("no rat targeted.");
  232.         Sanderling.MouseClickRight(ListRatOverviewEntry?.FirstOrDefault());
  233.         Sanderling.MouseClickLeft(MenuEntryLockTarget);
  234.     }
  235.     else
  236.     {
  237.         Host.Log("rat targeted. sending drones.");
  238.         Sanderling.MouseClickLeft(RatTargetNext);
  239.         Sanderling.MouseClickRight(DronesInSpaceListEntry);
  240.         Sanderling.MouseClickLeft(Menu?.FirstOrDefault()?.EntryFirstMatchingRegexPattern("engage", RegexOptions.IgnoreCase));
  241.     }
  242.    
  243.     return DefenseStep;
  244. }
  245.  
  246. int selectionTarget;
  247. int TargetRoidCount;
  248. Sanderling.Parse.IShipUiTarget Target;
  249. bool NotHaveModule;
  250.  
  251.  
  252. Func<object> InBeltMineStep()
  253. {
  254.     if (DefenseEnter)
  255.     {
  256.         Host.Log("enter defense.");
  257.         return DefenseStep;
  258.     }
  259.        
  260.     var AsteroidOverviewEntry = ListAsteroidOverviewEntry?.FirstOrDefault();
  261.    
  262.     if(null == AsteroidOverviewEntry)
  263.     {
  264.         Host.Log("no asteroid available");
  265.         return null;
  266.     }
  267.    
  268.     if(OreHoldFilledForOffload)
  269.         return null;
  270.        
  271.     if(!(0 < SetModuleMinerInactive?.Length))
  272.         return InBeltMineStep;     
  273.    
  274.     Target = null;
  275.     NotHaveModule = true;
  276.    
  277.     EnsureWindowInventoryOpenOreHold();
  278.  
  279.     EnsureOverviewTypeSelectionLoaded();
  280.  
  281.     if(DronesInSpaceCount <=  0/* && !(null == AsteroidOverviewEntry)*/)
  282.     DroneLaunch();
  283.    
  284.     var SetTargetAsteroidInRange = SetTargetAsteroid?.Where(target => target?.DistanceMax <= MiningRange)?.ToArray();
  285.    
  286.     /*if(AsteroidOverviewEntry != null)*/
  287.         if(!(AsteroidOverviewEntry.DistanceMax < MiningRange))
  288.         {
  289.             Host.Log("out of range, approaching");
  290.             /*if(AsteroidOverviewEntry != null)*/
  291.                 ClickMenuEntryOnMenuRoot(AsteroidOverviewEntry, "approach");
  292.         }
  293. //      Host.Delay(10000);
  294.     Host.Log(SetModuleMiner?.Count()+ "SetModuleMiner?.Count()");
  295.     Host.Log((TargetRoidCount) + "TargetRoidCount");
  296.    
  297.     /*if(AsteroidOverviewEntry != null)*/
  298.     if(TargetRoidCount < SetModuleMiner?.Count())
  299.     {
  300.         for(int i = TargetRoidCount; i<SetModuleMiner?.Count(); ++i){
  301.         selectionTarget = genNbr.Next(0,9);
  302.         while(TargetedByMe(ListAsteroidOverviewEntry[selectionTarget]))
  303.             selectionTarget = genNbr.Next(0,9);
  304.        
  305.             if(/*(AsteroidOverviewEntry != null) &&*/ !(TargetedByMe(ListAsteroidOverviewEntry[selectionTarget])) && (TargetRoidCount < SetModuleMiner?.Count()))
  306.             {
  307.                 ClickMenuEntryOnMenuRoot(ListAsteroidOverviewEntry[selectionTarget], "^lock");
  308.                 Host.Log("locking");
  309.                 TargetRoidCount = Sanderling?.MemoryMeasurementParsed?.Value?.Target?.Count(target => target?.TextRow?.Any(textRow => textRow?.RegexMatchSuccessIgnoreCase(@"Asteroid\s*\(") ?? false) ?? false) ?? 0;
  310. //              return InBeltMineStep;
  311.                 Host.Log(SetModuleMiner?.Count()+ "SetModuleMiner?.Count()");
  312.                 Host.Log((TargetRoidCount) + "TargetRoidCount");
  313.             }
  314.         }
  315.     }
  316.  
  317.     Host.Log("targeted asteroids in range: " + SetTargetAsteroidInRange?.Length);
  318.    
  319.     if(0 < SetTargetAsteroidInRange?.Length)
  320.     {
  321.         var TargetAsteroidInputFocus    =
  322.             SetTargetAsteroidInRange?.FirstOrDefault(target => target?.IsSelected ?? false);
  323.  
  324.         if(null == TargetAsteroidInputFocus)//
  325.             Sanderling.MouseClickLeft(SetTargetAsteroid?.FirstOrDefault()); /// Click sur l'asteroid
  326.    
  327.         foreach (var Module in SetModuleMinerInactive)
  328.         {
  329. //          Host.Delay(2222);
  330. //          Target = SetTargetAsteroid.FirstOrDefault();
  331.                
  332.             for(int t = 0; (t < TargetRoidCount) && NotHaveModule; ++t){
  333.                 Target = SetTargetAsteroid[t];
  334.                 if((Target?.Assigned?.Count() == 0))
  335.                     NotHaveModule = false;
  336.             }
  337.            
  338.             if(Target.DistanceMax > MiningRange)
  339.             {
  340.                 /*if(AsteroidOverviewEntry != null)*/
  341.                     ClickMenuEntryOnMenuRoot(Target, "Unlock Target");
  342.             }
  343.        
  344.             if(Target?.Assigned?.Count() == 0){
  345.                 Sanderling.MouseClickLeft(Target);
  346.                 //Host.Log(Target.DistanceMax + ": Target.DistanceMax" + MiningRange + " : MiningRange");
  347.                 ModuleToggle(Module);  
  348.                 return InBeltMineStep;
  349.             }
  350.             NotHaveModule = true;          
  351.         }
  352.         return InBeltMineStep;
  353.     }
  354.    
  355.     Host.Log("next asteroid: " + AsteroidOverviewEntry?.Name + " , Distance: " + AsteroidOverviewEntry?.DistanceMax);
  356.  
  357.     return InBeltMineStep;
  358. }
  359.  
  360. Sanderling.Parse.IMemoryMeasurement Measurement =>
  361.     Sanderling?.MemoryMeasurementParsed?.Value;
  362.  
  363. IWindow ModalUIElement =>
  364.     Measurement?.EnumerateReferencedUIElementTransitive()?.OfType<IWindow>()?.Where(window => window?.isModal ?? false)
  365.     ?.OrderByDescending(window => window?.InTreeIndex ?? int.MinValue)
  366.     ?.FirstOrDefault();
  367.    
  368. Sanderling.Interface.MemoryStruct.IMenu[] Menu => Measurement?.Menu;
  369.  
  370. IShipUi ShipUi => Measurement?.ShipUi;
  371.  
  372. bool Jammed => ShipUi?.EWarElement?.Any(EwarElement => (EwarElement?.EWarType).RegexMatchSuccess("electronic")) ?? false;
  373.  
  374. Sanderling.Interface.MemoryStruct.IMenuEntry MenuEntryLockTarget =>
  375.     Menu?.FirstOrDefault()?.Entry?.FirstOrDefault(entry => entry.Text.RegexMatchSuccessIgnoreCase("^lock"));
  376.  
  377. Sanderling.Parse.IWindowOverview    WindowOverview  =>
  378.     Measurement?.WindowOverview?.FirstOrDefault();
  379.  
  380. Sanderling.Parse.IWindowInventory   WindowInventory =>
  381.     Measurement?.WindowInventory?.FirstOrDefault();
  382.  
  383. IWindowDroneView    WindowDrones    =>
  384.     Measurement?.WindowDroneView?.FirstOrDefault();
  385.  
  386. ITreeViewEntry InventoryActiveShipOreHold =>
  387.     WindowInventory?.ActiveShipEntry?.TreeEntryFromCargoSpaceType(ShipCargoSpaceTypeEnum.OreHold);
  388.  
  389. IInventoryCapacityGauge OreHoldCapacityMilli =>
  390.     (InventoryActiveShipOreHold?.IsSelected ?? false) ? WindowInventory?.SelectedRightInventoryCapacityMilli : null;
  391.  
  392. int? OreHoldFillPercent => (int?)((OreHoldCapacityMilli?.Used * 100) / OreHoldCapacityMilli?.Max);
  393.  
  394. Tab OverviewPresetTabActive =>
  395.     WindowOverview?.PresetTab
  396.     ?.OrderByDescending(tab => tab?.LabelColorOpacityMilli ?? 0)
  397.     ?.FirstOrDefault();
  398.  
  399. string OverviewTypeSelectionName =>
  400.     WindowOverview?.Caption?.RegexMatchIfSuccess(@"\(([^\)]*)\)")?.Groups?[1]?.Value;
  401.  
  402. Parse.IOverviewEntry[] ListRatOverviewEntry => WindowOverview?.ListView?.Entry?.Where(entry =>
  403.         (entry?.MainIconIsRed ?? false) && (entry?.IsAttackingMe ?? false))
  404.         ?.OrderBy(entry => entry?.DistanceMax ?? int.MaxValue)
  405.         ?.ToArray();
  406.  
  407. Parse.IOverviewEntry[] ListAsteroidOverviewEntry =>
  408.     WindowOverview?.ListView?.Entry
  409.     ?.Where(entry => null != OreTypeFromAsteroidName(entry?.Name))
  410.     ?.OrderBy(entry => entry.DistanceMax ?? int.MaxValue)
  411.     ?.ToArray();
  412.    
  413.  
  414. DroneViewEntryGroup DronesInBayListEntry =>
  415.     WindowDrones?.ListView?.Entry?.OfType<DroneViewEntryGroup>()?.FirstOrDefault(Entry => null != Entry?.Caption?.Text?.RegexMatchIfSuccess(@"Drones in bay", RegexOptions.IgnoreCase));
  416.  
  417. DroneViewEntryGroup DronesInSpaceListEntry =>
  418.     WindowDrones?.ListView?.Entry?.OfType<DroneViewEntryGroup>()?.FirstOrDefault(Entry => null != Entry?.Caption?.Text?.RegexMatchIfSuccess(@"Drones in Local Space", RegexOptions.IgnoreCase));
  419.  
  420. int?    DronesInSpaceCount => DronesInSpaceListEntry?.Caption?.Text?.AsDroneLabel()?.Status?.TryParseInt();
  421.  
  422. bool ReadyForManeuverNot =>
  423.     Measurement?.ShipUi?.Indication?.Any(indication =>
  424.         (indication?.Text).RegexMatchSuccessIgnoreCase("warp|docking")) ?? false;
  425.  
  426. bool ReadyForManeuver => !ReadyForManeuverNot && !(Measurement?.IsDocked ?? true);
  427.  
  428. Sanderling.Parse.IShipUiTarget[] SetTargetAsteroid =>
  429.     Measurement?.Target?.Where(target =>
  430.         target?.TextRow?.Any(textRow => textRow.RegexMatchSuccessIgnoreCase("asteroid")) ?? false)?.ToArray();
  431.  
  432. Sanderling.Interface.MemoryStruct.IListEntry    WindowInventoryItem =>
  433.     WindowInventory?.SelectedRightInventory?.ListView?.Entry?.FirstOrDefault();
  434.  
  435. Sanderling.Accumulation.IShipUiModule[] SetModuleMiner =>
  436.     Sanderling.MemoryMeasurementAccu?.Value?.ShipUiModule?.Where(module => module?.TooltipLast?.Value?.IsMiner ?? false)?.ToArray();
  437.  
  438. Sanderling.Accumulation.IShipUiModule[] SetModuleMinerInactive   =>
  439.     SetModuleMiner?.Where(module => !(module?.RampActive ?? false))?.ToArray();
  440.  
  441. int?    MiningRange => SetModuleMiner?.Select(module =>
  442.     module?.TooltipLast?.Value?.RangeOptimal ?? module?.TooltipLast?.Value?.RangeMax ?? module?.TooltipLast?.Value?.RangeWithin ?? 0)?.DefaultIfEmpty(0)?.Min();;
  443.  
  444. bool ContainsLeftIconWithNameMatchingRegexPattern(Parse.IOverviewEntry overviewEntry, string regexPattern) => overviewEntry?.SetSprite?.Any(Sprite => (Sprite?.Name).RegexMatchSuccessIgnoreCase(regexPattern)) ?? false;
  445.  
  446. bool TargetedByMe(Parse.IOverviewEntry overviewEntry) =>ContainsLeftIconWithNameMatchingRegexPattern(overviewEntry, "targetedByMe");
  447.  
  448. //  extract the ore type from the name as seen in overview. "Asteroid (Plagioclase)"
  449. string OreTypeFromAsteroidName(string AsteroidName) =>
  450.     AsteroidName.ValueFromRegexMatchGroupAtIndex(@"Asteroid \(([^\)]+)", 0);
  451.  
  452. void ClickMenuEntryOnMenuRoot(IUIElement MenuRoot, string MenuEntryRegexPattern)
  453. {
  454.     Sanderling.MouseClickRight(MenuRoot);
  455.    
  456.     var Menu = Measurement?.Menu?.FirstOrDefault();
  457.    
  458.     var MenuEntry = Menu?.EntryFirstMatchingRegexPattern(MenuEntryRegexPattern, RegexOptions.IgnoreCase);
  459.    
  460.     Sanderling.MouseClickLeft(MenuEntry);
  461. }
  462.  
  463. void EnsureWindowInventoryOpen()
  464. {
  465.     if (null != WindowInventory)
  466.         return;
  467.  
  468.     Host.Log("open Inventory.");
  469.     Sanderling.MouseClickLeft(Measurement?.Neocom?.InventoryButton);
  470. }
  471.  
  472. void EnsureWindowInventoryOpenOreHold()
  473. {
  474.     EnsureWindowInventoryOpen();
  475.    
  476.     if(!(InventoryActiveShipOreHold?.IsSelected ?? false))
  477.         Sanderling.MouseClickLeft(InventoryActiveShipOreHold);
  478. }
  479.  
  480. void UnloadToHangar(string DestinationContainerName)
  481.  {
  482.      Host.Log("unload to hangar.");
  483.  
  484.      EnsureWindowInventoryOpenOreHold();
  485.  
  486.      for ( ; ; )
  487.      {
  488.          var    OreHoldItem = WindowInventory?.SelectedRightInventory?.ListView?.Entry?.FirstOrDefault();
  489.          var    DestinationContainer =
  490.              WindowInventory?.LeftTreeListEntry?.SelectMany(entry => new[]{entry}.Concat(entry.EnumerateChildNodeTransitive()))
  491.              ?.FirstOrDefault(entry => string.Equals(entry?.Text, DestinationContainerName, StringComparison.InvariantCultureIgnoreCase));
  492.  
  493.          if (null == DestinationContainer)
  494.             Host.Log("error: Inventory entry labeled '" + DestinationContainerName + "' not found");
  495.  
  496.          if(null == OreHoldItem)
  497.              break;    //    0 items in OreHold
  498.          Sanderling.MouseDragAndDrop(OreHoldItem, DestinationContainer);
  499.      }
  500.  }
  501.  
  502. bool InitiateDockToOrWarpToBookmark(string Bookmark)
  503. {
  504.     Host.Log("dock to or warp to bookmark: '" + Bookmark + "'");
  505.    
  506.     var ListSurroundingsButton = Measurement?.InfoPanelCurrentSystem?.ListSurroundingsButton;
  507.    
  508.     Sanderling.MouseClickRight(ListSurroundingsButton);
  509.    
  510.     var BookmarkMenuEntry = Measurement?.Menu?.FirstOrDefault()?.EntryFirstMatchingRegexPattern("^" + Bookmark + "$", RegexOptions.IgnoreCase);
  511.  
  512.     if(null == BookmarkMenuEntry)
  513.     {
  514.         Host.Log("menu entry not found for bookmark: '" + Bookmark + "'");
  515.         return true;
  516.     }
  517.  
  518.     Sanderling.MouseClickLeft(BookmarkMenuEntry);
  519.  
  520.     var Menu = Measurement?.Menu?.ElementAtOrDefault(1);
  521.     var DockMenuEntry = Menu?.EntryFirstMatchingRegexPattern("dock",RegexOptions.IgnoreCase);
  522.     var WarpMenuEntry = Menu?.EntryFirstMatchingRegexPattern(@"warp.*within\s*0",RegexOptions.IgnoreCase);
  523.     var ApproachEntry = Menu?.EntryFirstMatchingRegexPattern(@"approach",RegexOptions.IgnoreCase);
  524.  
  525.     var MenuEntry = DockMenuEntry ?? WarpMenuEntry;
  526.    
  527.     if(null == MenuEntry)
  528.     {
  529.         if(null != ApproachEntry)
  530.         {
  531.             Host.Log("found menu entry '" + ApproachEntry.Text + "'. Assuming we are already there.");
  532.             return false;
  533.         }
  534.  
  535.         Host.Log("no suitable menu entry found");
  536.         return true;
  537.     }
  538.    
  539.     Host.Log("initiating " + MenuEntry.Text);
  540.     Sanderling.MouseClickLeft(MenuEntry);
  541.  
  542.     return false;
  543. }
  544.  
  545. void Undock()
  546. {
  547.     Sanderling.MouseClickLeft(Measurement?.WindowStation?.FirstOrDefault()?.UndockButton);
  548.    
  549.     Host.Log("waiting for undocking to complete.");
  550.     while(Measurement?.IsDocked ?? true)     Host.Delay(1111);
  551.  
  552.     Host.Delay(4444);
  553.     Sanderling.InvalidateMeasurement();
  554. }
  555.  
  556. void ModuleMeasureAllTooltip()
  557. {
  558.     Host.Log("measure tooltips of all modules.");
  559.  
  560.     for (;;)
  561.     {
  562.         var NextModule = Sanderling.MemoryMeasurementAccu?.Value?.ShipUiModule?.FirstOrDefault(m => null == m?.TooltipLast);
  563.  
  564.         if(null == NextModule)
  565.             break;
  566.  
  567.         Host.Log("measure module.");
  568.         //  take multiple measurements of module tooltip to reduce risk to keep bad read tooltip.
  569.         Sanderling.MouseMove(NextModule);
  570.         Sanderling.WaitForMeasurement();
  571.         Sanderling.MouseMove(NextModule);
  572.     }
  573. }
  574.  
  575. void ActivateHardenerExecute()
  576. {
  577.     var SubsetModuleHardener =
  578.         Sanderling.MemoryMeasurementAccu?.Value?.ShipUiModule
  579.         ?.Where(module => module?.TooltipLast?.Value?.IsHardener ?? false);
  580.  
  581.     var SubsetModuleToToggle =
  582.         SubsetModuleHardener
  583.         ?.Where(module => !(module?.RampActive ?? false));
  584.  
  585.     foreach (var Module in SubsetModuleToToggle.EmptyIfNull())
  586.         ModuleToggle(Module);
  587. }
  588.  
  589. void ModuleToggle(Sanderling.Accumulation.IShipUiModule Module)
  590. {
  591.     var ToggleKey = Module?.TooltipLast?.Value?.ToggleKey;
  592.  
  593.     Host.Log("toggle module using " + (null == ToggleKey ? "mouse" : Module?.TooltipLast?.Value?.ToggleKeyTextLabel?.Text));
  594.  
  595.     if(null == ToggleKey)
  596.         Sanderling.MouseClickLeft(Module);
  597.     else
  598.         Sanderling.KeyboardPressCombined(ToggleKey);
  599. }
  600.  
  601. void EnsureOverviewTypeSelectionLoaded()
  602. {
  603.     if(null == OverviewPresetTabActive || null == WindowOverview || null == OverviewPreset)
  604.         return;
  605.  
  606.     if(string.Equals(OverviewTypeSelectionName, OverviewPreset, StringComparison.OrdinalIgnoreCase))
  607.         return;
  608.  
  609.     Host.Log("loading preset '" + OverviewPreset + "' to overview (current selection is '" + OverviewTypeSelectionName + "').");
  610.     Sanderling.MouseClickRight(OverviewPresetTabActive);
  611.     Sanderling.MouseClickLeft(Menu?.FirstOrDefault()?.EntryFirstMatchingRegexPattern("load.*preset", RegexOptions.IgnoreCase));
  612.     var PresetMenuEntry = Menu?.ElementAtOrDefault(1)?.EntryFirstMatchingRegexPattern(@"^\s*" + Regex.Escape(OverviewPreset) + @"\s*$", RegexOptions.IgnoreCase);
  613.  
  614.     if(null == PresetMenuEntry)
  615.     {
  616.         Host.Log("error: menu entry '" + OverviewPreset + "' not found");
  617.         return;
  618.     }
  619.  
  620.     Sanderling.MouseClickLeft(PresetMenuEntry);
  621. }
  622.  
  623. void MemoryUpdate()
  624. {
  625.     EmergencyWarpOutUpdate();
  626.     JammedLastTimeUpdate();
  627.     OffloadCountUpdate();
  628. }
  629.  
  630. void JammedLastTimeUpdate()
  631. {
  632.     if(Jammed)
  633.         JammedLastTime  = Host.GetTimeContinuousMilli();
  634. }
  635.  
  636. bool MeasurementEmergencyWarpOutEnter =>
  637.     !(Measurement?.IsDocked ?? false) && !(EmergencyWarpOutHitpointPercent < ShieldHpPercent);
  638.  
  639. void EmergencyWarpOutUpdate()
  640. {
  641.     if (!MeasurementEmergencyWarpOutEnter)
  642.         return;
  643.  
  644.     //  measure multiple times to avoid being scared off by noise from a single measurement.
  645.     Sanderling.InvalidateMeasurement();
  646.  
  647.     if (!MeasurementEmergencyWarpOutEnter)
  648.         return;
  649.  
  650.     EmergencyWarpOutEnabled = true;
  651. }
  652.  
  653. void OffloadCountUpdate()
  654. {
  655.     var OreHoldFillPercentSynced    = OreHoldFillPercent;
  656.  
  657.     if(!OreHoldFillPercentSynced.HasValue)
  658.         return;
  659.  
  660.     if(0 == OreHoldFillPercentSynced && OreHoldFillPercentSynced < LastCheckOreHoldFillPercent)
  661.         ++OffloadCount;
  662.  
  663.     LastCheckOreHoldFillPercent = OreHoldFillPercentSynced;
  664. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement