Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Sandbox.Game.EntityComponents;
- using Sandbox.ModAPI.Ingame;
- using Sandbox.ModAPI.Interfaces;
- using SpaceEngineers.Game.ModAPI.Ingame;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using VRage;
- using VRage.Collections;
- using VRage.Game;
- using VRage.Game.Components;
- using VRage.Game.GUI.TextPanel;
- using VRage.Game.ModAPI.Ingame;
- using VRage.Game.ModAPI.Ingame.Utilities;
- using VRage.Game.ObjectBuilders.Definitions;
- using VRageMath;
- namespace IngameScript
- {
- partial class Program : MyGridProgram
- {
- List<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>();
- List<IMyBatteryBlock> backupBatteries = new List<IMyBatteryBlock>();
- List<IMyProjector> Projector = new List<IMyProjector>();
- List<IMyShipWelder> Welders = new List<IMyShipWelder>();
- List<IMyShipMergeBlock> Merge = new List<IMyShipMergeBlock>();
- // Battery variables
- float storedPowerAll = 0;
- float storedPowerAllMax = 0;
- DateTime balanceTime = DateTime.Now;
- // Command line parameters
- string setting = "";
- string message = "";
- string action = "";
- DateTime actionTime;
- HashSet<string> allWarnings = new HashSet<string>();
- HashSet<string> currentWarnings = new HashSet<string>();
- // Script timing variables
- int savedBlockCount = 0;
- int execCounter = 1;
- int weldcooldown = 0;
- bool firstRun = true;
- /// <summary>
- /// Pre-Run preparations
- /// </summary>
- public Program()
- {
- // Set UpdateFrequency for starting the programmable block over and over again
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- }
- /// <summary>
- /// Main method
- /// </summary>
- void Main(string arg)
- {
- // First run
- if (firstRun)
- {
- Echo("Initializing.. ");
- if (execCounter == 1) WipeData(); Echo("Wiping Previous Memory.. ");
- if (execCounter == 2) GetBlocks();
- 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");
- if (execCounter >= 8) ResetMerge(); Echo("\nResetting Merge Block...");
- if (execCounter >= 9) Echo("\nStarting script..");
- execCounter++;
- if (execCounter >= 15)
- {
- execCounter = 1;
- firstRun = false;
- }
- return;
- }
- // Store the parameter
- if (arg != "")
- {
- action = arg;
- execCounter = 0;
- message = "";
- actionTime = DateTime.Now;
- }
- // Execute commandline argument
- if (ExecuteArgument(action)) return;
- // Get all blocks, the script should use
- var allBlocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocks(allBlocks);
- int blockCount = allBlocks.Count;
- if (execCounter == 0 || blockCount != savedBlockCount)
- {
- GetBlocks();
- savedBlockCount = blockCount;
- if (execCounter == 0)
- {
- execCounter++;
- }
- return;
- }
- if (execCounter == 1)
- {
- Echo("Keeping Gotham Charged");
- Echo("\n-..");
- projectorManager();
- }
- if (execCounter == 6)
- {
- Echo("Keeping Gotham Charged");
- Echo("\n.-.");
- // Get state of doners
- if (IsDonerDead())
- {
- ReleaseMerge();
- }
- }
- // Update the script execution counter
- if (execCounter >= 10)
- {
- Echo("Keeping Gotham Charged");
- Echo("\n..-");
- // Reset the counter
- execCounter = 0;
- // Overwrite all warnings set with current warnings
- allWarnings = new HashSet<string>(currentWarnings);
- currentWarnings.Clear();
- }
- else
- {
- execCounter++;
- }
- }
- private void WipeData()
- {
- batteries.Clear();
- Projector.Clear();
- Welders.Clear();
- Merge.Clear();
- }
- private void projectorManager()
- {
- if (!(Projector.First().Enabled))
- {
- Projector.First().Enabled = true; // stop turning off my projector reload
- }
- int x = 0;
- for (int i = batteries.Count - 1; i >= 0; i--)
- {
- if (batteries[i].IsFunctional)
- {
- x++;
- }
- }
- if (x >= batteries.Count)
- {
- toggleWeld(false);
- }
- else
- {
- toggleWeld(true);
- }
- }
- private void toggleWeld(bool on, bool force = false)
- {
- if (weldcooldown <= 0 || force)
- {
- for (int i = Welders.Count - 1; i >= 0; i--)
- {
- Welders[i].Enabled = on;
- weldcooldown = 100;
- }
- if (!force)
- {
- backupBat(false);
- }
- } else
- {
- weldcooldown--;
- }
- }
- private void backupBat(bool setAuto)
- {
- for (int i = backupBatteries.Count - 1; i >= 0; i--)
- {
- backupBatteries[i].ChargeMode = setAuto ? ChargeMode.Auto : ChargeMode.Recharge;
- }
- }
- private void ReleaseMerge()
- {
- if (Projector.First().BuildableBlocksCount.Equals(0))
- {
- backupBat(true);
- Sleep(10);
- foreach (IMyShipMergeBlock e in Merge)
- {
- for (int i = Merge.Count - 1; i >= 0; i--)
- {
- Merge[i].Enabled = false;
- toggleWeld(true, true);
- }
- }
- }
- firstRun = true;
- }
- private void Sleep(int v)
- {
- for (int i = (v*16); i >= 0; i--)
- {
- Echo("Potatoes");
- }
- }
- private void ResetMerge()
- {
- for (int i = Merge.Count - 1; i >= 0; i--)
- {
- Merge[i].Enabled = true;
- }
- }
- private Boolean IsDonerDead()
- {
- int x = 0;
- int y = 0;
- foreach (IMyBatteryBlock e in batteries)
- {
- if (e.ChargeMode == ChargeMode.Discharge)
- {
- x++;
- if (e.CurrentStoredPower.Equals(0f))
- {
- y++;
- }
- }
- }
- return y == x;
- }
- bool ExecuteArgument(string arg)
- {
- bool validArgument = true;
- bool status = true;
- if (arg != "msg")
- {
- if (!arg.Contains(" on") && !arg.Contains(" off") && !arg.Contains(" toggle")) return false;
- if (arg.Contains(" off")) status = false;
- }
- if (arg == "msg")
- {
- }
- else
- {
- validArgument = false;
- }
- if (validArgument)
- {
- TimeSpan timeSinceAction = DateTime.Now - actionTime;
- if (message == "") message = setting + " temporarily " + (status ? "enabled" : "disabled") + "!\n";
- Echo(message);
- Echo("Continuing in " + Math.Ceiling(3 - timeSinceAction.TotalSeconds) + " seconds..");
- action = "msg";
- if (timeSinceAction.TotalSeconds >= 3)
- {
- setting = "";
- message = "";
- action = "";
- }
- }
- return validArgument;
- }
- /// Gets all blocks that should be used by the script
- void GetBlocks()
- {
- List<IMyBatteryBlock> batteriesTemp = new List<IMyBatteryBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteriesTemp);
- for (int i = batteriesTemp.Count - 1; i >= 0; i--)
- {
- if (!batteriesTemp[i].ChargeMode.Equals(ChargeMode.Discharge))
- {
- if (batteriesTemp[i].CustomName.Contains("[BAT]"))
- {
- backupBatteries.Add(batteriesTemp[i]);
- }
- batteriesTemp.RemoveAtFast(i);
- }
- }
- batteries = batteriesTemp;
- List<IMyShipMergeBlock> MergeTemp = new List<IMyShipMergeBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyShipMergeBlock>(MergeTemp);
- for (int i = MergeTemp.Count - 1; i >= 0; i--)
- {
- if (!MergeTemp[i].CustomName.Contains("[BAT]"))
- MergeTemp.RemoveAtFast(i);
- }
- Merge = MergeTemp;
- List<IMyProjector> ProjTemp = new List<IMyProjector>();
- GridTerminalSystem.GetBlocksOfType<IMyProjector>(ProjTemp);
- for (int i = ProjTemp.Count - 1; i >= 0; i--)
- {
- if (!ProjTemp[i].CustomName.Contains("[BAT]"))
- ProjTemp.RemoveAtFast(i);
- }
- Projector = ProjTemp;
- List<IMyShipWelder> weldTemp = new List<IMyShipWelder>();
- GridTerminalSystem.GetBlocksOfType<IMyShipWelder>(weldTemp);
- for (int i = weldTemp.Count - 1; i >= 0; i--)
- {
- if (!weldTemp[i].CustomName.Contains("[BAT]"))
- weldTemp.RemoveAtFast(i);
- }
- Welders = weldTemp;
- }
- /// Save method for recompiling the script or saving the world
- public void Save()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement