using Sandbox.Game.EntityComponents; using Sandbox.ModAPI.Ingame; using Sandbox.ModAPI.Interfaces; using SpaceEngineers.Game.ModAPI.Ingame; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System; using VRage.Collections; using VRage.Game.Components; using VRage.Game.ModAPI.Ingame; using VRage.Game.ModAPI.Ingame.Utilities; using VRage.Game.ObjectBuilders.Definitions; using VRage.Game; using VRageMath; namespace IngameScript { partial class Program : MyGridProgram { // This file contains your actual script. // // You can either keep all your code here, or you can create separate // code files to make your program easier to navigate while coding. // // In order to add a new utility class, right-click on your project, // select 'New' then 'Add Item...'. Now find the 'Space Engineers' // category under 'Visual C# Items' on the left hand side, and select // 'Utility Class' in the main area. Name it in the box below, and // press OK. This utility class will be merged in with your code when // deploying your final script. // // You can also simply create a new utility class manually, you don't // have to use the template if you don't want to. Just do so the first // time to see what a utility class looks like. public Program() { // The constructor, called only once every session and // always before any other method is called. Use it to // initialize your script. // // The constructor is optional and can be removed if not // needed. // // It's recommended to set RuntimeInfo.UpdateFrequency // here, which will allow your script to run itself without a // timer block. Runtime.UpdateFrequency = UpdateFrequency.Update1; } public void Save() { // Called when the program needs to save its state. Use // this method to save your state to the Storage field // or some other means. // // This method is optional and can be removed if not // needed. } public void Off( List gravityGeneratorSpheres, List artificialMassBlocks) { foreach (var gravityGeneratorSphere in gravityGeneratorSpheres) { gravityGeneratorSphere.GravityAcceleration = 0.0f; } foreach (var artificialMassBlock in artificialMassBlocks) { artificialMassBlock.Enabled = false; } } public void On( List artificialMassBlocks) { foreach (var artificialMassBlock in artificialMassBlocks) { artificialMassBlock.Enabled = true; } } //public void Main(string argument, UpdateType updateSource) public void Main() { List gravityGeneratorSpheres = new List(); GridTerminalSystem.GetBlocksOfType(gravityGeneratorSpheres); List artificialMassBlocks = new List(); GridTerminalSystem.GetBlocksOfType(artificialMassBlocks); List shipControllers = new List(); GridTerminalSystem.GetBlocksOfType(shipControllers, x => x.IsMainCockpit == true); List textPanels = new List(); GridTerminalSystem.GetBlocksOfType(textPanels); if (shipControllers.Count > 0) { IMyShipController shipController = shipControllers[0]; Vector3D moveIndicator = shipController.MoveIndicator; Vector3D linearVelocity = Vector3D.TransformNormal( shipController.GetShipVelocities().LinearVelocity, MatrixD.Invert(shipController.WorldMatrix)); Quaternion quaternion = new Quaternion(); shipController.Orientation.GetQuaternion(out quaternion); quaternion = Quaternion.Inverse(quaternion); Matrix matrix = Matrix.CreateFromQuaternion(quaternion); moveIndicator = Vector3.TransformNormal(moveIndicator, matrix); linearVelocity = Vector3.TransformNormal(linearVelocity, matrix); if (linearVelocity.Length() >= 1.0) { if (moveIndicator.Length() > 0.001) moveIndicator.Normalize(); else moveIndicator = new Vector3D(0.0, 0.0, 0.0); if (linearVelocity.Length() > 0.001) linearVelocity.Normalize(); else linearVelocity = new Vector3D(0.0, 0.0, 0.0); Vector3D desiredDirection = moveIndicator; if (shipController.DampenersOverride) desiredDirection -= linearVelocity / 2.0; if (desiredDirection.Length() > 0.001) { desiredDirection.Normalize(); On(artificialMassBlocks); Vector3D centerOfArtificialMass = new Vector3D(0.0, 0.0, 0.0); foreach (var artificialMassBlock in artificialMassBlocks) { centerOfArtificialMass += artificialMassBlock.Position; } centerOfArtificialMass /= artificialMassBlocks.Count; foreach (var gravityGeneratorSphere in gravityGeneratorSpheres) { Vector3D view = gravityGeneratorSphere.Position; view = centerOfArtificialMass - view; view.Normalize(); double direction = Vector3D.Dot(view, desiredDirection); if (Math.Abs(direction) > 0.001) gravityGeneratorSphere.GravityAcceleration = (float)Math.Sign(direction) * 9.81f; else gravityGeneratorSphere.GravityAcceleration = 0.0f; } } else { Off(gravityGeneratorSpheres, artificialMassBlocks); } foreach (var textPanel in textPanels) { textPanel.WritePublicText((int)(moveIndicator.X * 100.0) + " "); textPanel.WritePublicText((int)(moveIndicator.Y * 100.0) + " ", true); textPanel.WritePublicText((int)(moveIndicator.Z * 100.0) + "\n", true); textPanel.WritePublicText((int)(linearVelocity.X * 100.0) + " ", true); textPanel.WritePublicText((int)(linearVelocity.Y * 100.0) + " ", true); textPanel.WritePublicText((int)(linearVelocity.Z * 100.0) + "\n", true); textPanel.WritePublicText((int)(desiredDirection.X * 100.0) + " ", true); textPanel.WritePublicText((int)(desiredDirection.Y * 100.0) + " ", true); textPanel.WritePublicText((int)(desiredDirection.Z * 100.0) + "\n", true); } } else { Off(gravityGeneratorSpheres, artificialMassBlocks); } } else { Off(gravityGeneratorSpheres, artificialMassBlocks); } } } }