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<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;
- 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) GetBlocks();
- if (execCounter >= 2) Echo("Found: " + batteries.Count + " batteries");
- if (execCounter >= 10) 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)
- {
- // Get state of doners
- if (IsDonerDead())
- {
- ReleaseMerge();
- }
- }
- // Update the script execution counter
- if (execCounter >= 2)
- {
- // Reset the counter
- execCounter = 0;
- // Overwrite all warnings set with current warnings
- allWarnings = new HashSet<string>(currentWarnings);
- currentWarnings.Clear();
- ResetMerge();
- }
- else
- {
- execCounter++;
- }
- }
- private void ReleaseMerge()
- {
- foreach (IMyShipMergeBlock e in Merge)
- {
- e.Enabled = false;
- }
- }
- private void ResetMerge()
- {
- foreach (IMyShipMergeBlock e in Merge)
- {
- e.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);
- List<IMyShipMergeBlock> MergeTemp = new List<IMyShipMergeBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyShipMergeBlock>(MergeTemp);
- foreach (IMyShipMergeBlock e in MergeTemp)
- {
- if (!e.DisplayName.Contains("[Bat]"))
- {
- MergeTemp.Remove(e);
- }
- }
- Merge = MergeTemp;
- }
- /// Save method for recompiling the script or saving the world
- public void Save()
- {
- // Activate everything
- foreach (var battery in batteries)
- {
- battery.Enabled = true;
- battery.ChargeMode = ChargeMode.Auto;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement