Advertisement
idiotonastic

sdagasdgas

Mar 2nd, 2021
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.91 KB | None | 0 0
  1. using Sandbox.Game.EntityComponents;
  2. using Sandbox.ModAPI.Ingame;
  3. using Sandbox.ModAPI.Interfaces;
  4. using SpaceEngineers.Game.ModAPI.Ingame;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using VRage;
  11. using VRage.Collections;
  12. using VRage.Game;
  13. using VRage.Game.Components;
  14. using VRage.Game.GUI.TextPanel;
  15. using VRage.Game.ModAPI.Ingame;
  16. using VRage.Game.ModAPI.Ingame.Utilities;
  17. using VRage.Game.ObjectBuilders.Definitions;
  18. using VRageMath;
  19.  
  20. namespace IngameScript
  21. {
  22.     partial class Program : MyGridProgram
  23.     {
  24.  
  25.  
  26.         List<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>();
  27.         List<IMyBatteryBlock> backupBatteries = new List<IMyBatteryBlock>();
  28.         List<IMyProjector> Projector = new List<IMyProjector>();
  29.         List<IMyShipWelder> Welders = new List<IMyShipWelder>();
  30.         List<IMyShipMergeBlock> Merge = new List<IMyShipMergeBlock>();
  31.  
  32.         // Battery variables
  33.         float storedPowerAll = 0;
  34.         float storedPowerAllMax = 0;
  35.  
  36.         DateTime balanceTime = DateTime.Now;
  37.  
  38.         // Command line parameters
  39.         string setting = "";
  40.         string message = "";
  41.         string action = "";
  42.         DateTime actionTime;
  43.  
  44.         HashSet<string> allWarnings = new HashSet<string>();
  45.         HashSet<string> currentWarnings = new HashSet<string>();
  46.  
  47.         // Script timing variables
  48.         int savedBlockCount = 0;
  49.         int execCounter = 1;
  50.         int weldcooldown = 0;
  51.         bool firstRun = true;
  52.         /// <summary>
  53.         /// Pre-Run preparations
  54.         /// </summary>
  55.         public Program()
  56.         {
  57.             // Set UpdateFrequency for starting the programmable block over and over again
  58.             Runtime.UpdateFrequency = UpdateFrequency.Update10;
  59.  
  60.         }
  61.  
  62.  
  63.         /// <summary>
  64.         /// Main method
  65.         /// </summary>
  66.         void Main(string arg)
  67.         {
  68.  
  69.             // First run
  70.             if (firstRun)
  71.             {
  72.                 Echo("Initializing.. ");
  73.                 if (execCounter == 1) WipeData(); Echo("Wiping Previous Memory.. ");
  74.                 if (execCounter == 2) GetBlocks();
  75.                 if (execCounter >= 3) Echo("Found: " + batteries.Count + " doner batteries"); Echo("Found: " + backupBatteries.Count + " backup batteries");  Echo("Found: " + Projector.Count + " Projectors"); Echo("Found: " + Welders.Count + " Welders"); Echo("Found: " + Merge.Count + " Merge Blocks");
  76.                 if (execCounter >= 8) ResetMerge(); Echo("\nResetting Merge Block...");
  77.                 if (execCounter >= 9)  Echo("\nStarting script..");
  78.  
  79.                 execCounter++;
  80.                 if (execCounter >= 15)
  81.                 {
  82.                     execCounter = 1;
  83.                     firstRun = false;
  84.                 }
  85.                
  86.                 return;
  87.             }
  88.  
  89.             // Store the parameter
  90.             if (arg != "")
  91.             {
  92.                 action = arg;
  93.                 execCounter = 0;
  94.                 message = "";
  95.                 actionTime = DateTime.Now;
  96.             }
  97.  
  98.             // Execute commandline argument
  99.             if (ExecuteArgument(action)) return;
  100.  
  101.             // Get all blocks, the script should use
  102.             var allBlocks = new List<IMyTerminalBlock>();
  103.             GridTerminalSystem.GetBlocks(allBlocks);
  104.             int blockCount = allBlocks.Count;
  105.            
  106.             if (execCounter == 0 || blockCount != savedBlockCount)
  107.             {
  108.                 GetBlocks();
  109.                 savedBlockCount = blockCount;
  110.  
  111.                 if (execCounter == 0)
  112.                 {
  113.                     execCounter++;
  114.                 }
  115.  
  116.                 return;
  117.             }
  118.             if (execCounter == 1)
  119.             {
  120.                 Echo("Keeping Gotham Charged");
  121.                 Echo("\n-..");
  122.                 projectorManager();
  123.             }
  124.             if (execCounter == 6)
  125.             {
  126.                 Echo("Keeping Gotham Charged");
  127.                 Echo("\n.-.");
  128.                 // Get state of doners
  129.                 if (IsDonerDead())
  130.                 {
  131.                     ReleaseMerge();
  132.                 }
  133.  
  134.             }
  135.  
  136.             // Update the script execution counter
  137.             if (execCounter >= 10)
  138.             {
  139.                 Echo("Keeping Gotham Charged");
  140.                 Echo("\n..-");
  141.  
  142.                 // Reset the counter
  143.                 execCounter = 0;
  144.  
  145.                 // Overwrite all warnings set with current warnings
  146.                 allWarnings = new HashSet<string>(currentWarnings);
  147.                 currentWarnings.Clear();
  148.                
  149.             }
  150.             else
  151.             {
  152.                 execCounter++;
  153.             }
  154.         }
  155.  
  156.         private void WipeData()
  157.         {
  158.             batteries.Clear();
  159.             Projector.Clear();
  160.             Welders.Clear();
  161.             Merge.Clear();
  162.         }
  163.  
  164.         private void projectorManager()
  165.         {
  166.             if (!(Projector.First().Enabled))
  167.             {
  168.                 Projector.First().Enabled = true;  // stop turning off my projector reload
  169.             }
  170.  
  171.             int x = 0;
  172.             for (int i = batteries.Count - 1; i >= 0; i--)
  173.             {
  174.                 if (batteries[i].IsFunctional)
  175.                 {
  176.                     x++;
  177.                 }
  178.             }
  179.             if (x >= batteries.Count)
  180.             {
  181.                 toggleWeld(false);
  182.             }
  183.             else
  184.             {
  185.                 toggleWeld(true);
  186.             }
  187.         }
  188.         private void toggleWeld(bool on, bool force = false)
  189.         {
  190.             if (weldcooldown <= 0 || force)
  191.             {
  192.                 for (int i = Welders.Count - 1; i >= 0; i--)
  193.                 {
  194.                     Welders[i].Enabled = on;
  195.                     weldcooldown = 100;
  196.                 }
  197.                 if (!force)
  198.                 {
  199.                     backupBat(false);
  200.                 }
  201.             } else
  202.             {
  203.                 weldcooldown--;
  204.             }
  205.         }
  206.         private void backupBat(bool setAuto)
  207.         {
  208.             for (int i = backupBatteries.Count - 1; i >= 0; i--)
  209.             {
  210.                 backupBatteries[i].ChargeMode =  setAuto ? ChargeMode.Auto : ChargeMode.Recharge;
  211.             }
  212.         }
  213.         private void ReleaseMerge()
  214.         {
  215.             if (Projector.First().BuildableBlocksCount.Equals(0))
  216.             {
  217.                 backupBat(true);
  218.                 Sleep(10);
  219.                 foreach (IMyShipMergeBlock e in Merge)
  220.                 {
  221.                     for (int i = Merge.Count - 1; i >= 0; i--)
  222.                     {
  223.                        
  224.                         Merge[i].Enabled = false;
  225.                         toggleWeld(true, true);
  226.                     }
  227.                 }
  228.                
  229.             }
  230.             firstRun = true;
  231.         }
  232.  
  233.         private void Sleep(int v)
  234.         {
  235.            for (int i = (v*16); i >= 0; i--)
  236.             {
  237.                 Echo("Potatoes");
  238.             }
  239.         }
  240.  
  241.         private void ResetMerge()
  242.         {
  243.             for (int i = Merge.Count - 1; i >= 0; i--)
  244.             {
  245.                 Merge[i].Enabled = true;
  246.             }
  247.         }
  248.  
  249.         private Boolean IsDonerDead()
  250.         {
  251.             int x = 0;
  252.             int y = 0;
  253.             foreach (IMyBatteryBlock e in batteries)
  254.             {
  255.                 if (e.ChargeMode == ChargeMode.Discharge)
  256.                 {
  257.                     x++;
  258.                     if (e.CurrentStoredPower.Equals(0f))
  259.                     {
  260.                         y++;
  261.                     }
  262.                 }
  263.             }
  264.             return y == x;
  265.         }
  266.  
  267.         bool ExecuteArgument(string arg)
  268.         {
  269.             bool validArgument = true;
  270.             bool status = true;
  271.  
  272.             if (arg != "msg")
  273.             {
  274.                 if (!arg.Contains(" on") && !arg.Contains(" off") && !arg.Contains(" toggle")) return false;
  275.                 if (arg.Contains(" off")) status = false;
  276.             }
  277.  
  278.             if (arg == "msg")
  279.             {
  280.             }
  281.             else
  282.             {
  283.                 validArgument = false;
  284.             }
  285.  
  286.             if (validArgument)
  287.             {
  288.                 TimeSpan timeSinceAction = DateTime.Now - actionTime;
  289.  
  290.                 if (message == "") message = setting + " temporarily " + (status ? "enabled" : "disabled") + "!\n";
  291.                 Echo(message);
  292.                 Echo("Continuing in " + Math.Ceiling(3 - timeSinceAction.TotalSeconds) + " seconds..");
  293.                 action = "msg";
  294.  
  295.                 if (timeSinceAction.TotalSeconds >= 3)
  296.                 {
  297.                     setting = "";
  298.                     message = "";
  299.                     action = "";
  300.                 }
  301.             }
  302.  
  303.             return validArgument;
  304.         }
  305.  
  306.  
  307.  
  308.         /// Gets all blocks that should be used by the script
  309.  
  310.         void GetBlocks()
  311.         {
  312.             List<IMyBatteryBlock> batteriesTemp = new List<IMyBatteryBlock>();
  313.             GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteriesTemp);
  314.             for (int i = batteriesTemp.Count - 1; i >= 0; i--)
  315.             {
  316.                 if (!batteriesTemp[i].ChargeMode.Equals(ChargeMode.Discharge))
  317.                 {
  318.                     if (batteriesTemp[i].CustomName.Contains("[BAT]"))
  319.                     {
  320.                         backupBatteries.Add(batteriesTemp[i]);
  321.                     }
  322.                     batteriesTemp.RemoveAtFast(i);
  323.  
  324.                 }
  325.                    
  326.             }
  327.             batteries = batteriesTemp;
  328.             List<IMyShipMergeBlock> MergeTemp = new List<IMyShipMergeBlock>();
  329.             GridTerminalSystem.GetBlocksOfType<IMyShipMergeBlock>(MergeTemp);
  330.             for (int i = MergeTemp.Count - 1; i >= 0; i--)
  331.             {
  332.                 if (!MergeTemp[i].CustomName.Contains("[BAT]"))
  333.                     MergeTemp.RemoveAtFast(i);
  334.             }
  335.             Merge = MergeTemp;
  336.             List<IMyProjector> ProjTemp = new List<IMyProjector>();
  337.             GridTerminalSystem.GetBlocksOfType<IMyProjector>(ProjTemp);
  338.             for (int i = ProjTemp.Count - 1; i >= 0; i--)
  339.             {
  340.                 if (!ProjTemp[i].CustomName.Contains("[BAT]"))
  341.                     ProjTemp.RemoveAtFast(i);
  342.             }
  343.             Projector = ProjTemp;
  344.             List<IMyShipWelder> weldTemp = new List<IMyShipWelder>();
  345.             GridTerminalSystem.GetBlocksOfType<IMyShipWelder>(weldTemp);
  346.             for (int i = weldTemp.Count - 1; i >= 0; i--)
  347.             {
  348.                 if (!weldTemp[i].CustomName.Contains("[BAT]"))
  349.                     weldTemp.RemoveAtFast(i);
  350.             }
  351.             Welders = weldTemp;
  352.  
  353.         }
  354.  
  355.  
  356.  
  357.  
  358.         /// Save method for recompiling the script or saving the world
  359.  
  360.         public void Save()
  361.         {
  362.    
  363.         }
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement