First commit

This commit is contained in:
Greg Burri 2023-09-08 23:45:14 +02:00
commit 1d890b728a
24 changed files with 1814 additions and 0 deletions

View file

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{141E1804-F644-48F8-A3D8-BEFEEE66ECBA}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>IngameScript</RootNamespace>
<AssemblyName>AutoPilot</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
<LangVersion>6</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="netstandard" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<Import Project="MDK/MDK.options.props" />
<Import Project="MDK/MDK.paths.props" Condition="exists('MDK/MDK.paths.props')" />
<ItemGroup>
<Compile Include="MDK\Bootstrapper.cs" />
<AdditionalFiles Include="MDK\MDK.options.props">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AdditionalFiles>
<AdditionalFiles Include="MDK\MDK.paths.props">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AdditionalFiles>
<Compile Include="Program.cs" />
<AdditionalFiles Include="Instructions.readme" />
<AdditionalFiles Include="thumb.png" />
<AdditionalFiles Include="MDK\whitelist.cache" />
</ItemGroup>
<Import Project="..\SECommon\SECommon.projitems" Label="Shared" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<Copy SourceFiles="MDK\MDK.options.props" DestinationFolder="$(TargetDir)\MDK" />
<Copy SourceFiles="MDK\MDK.paths.props" DestinationFolder="$(TargetDir)\MDK" />
</Target>
</Project>

View file

@ -0,0 +1,27 @@
using Malware.MDKUtilities;
namespace IngameScript.MDK
{
public class TestBootstrapper
{
// All the files in this folder, as well as all files containing the file ".debug.", will be excluded
// from the build process. You can use this to create utilites for testing your scripts directly in
// Visual Studio.
static TestBootstrapper()
{
// Initialize the MDK utility framework
MDKUtilityFramework.Load();
}
public static void Main()
{
// In order for your program to actually run, you will need to provide a mockup of all the facilities
// your script uses from the game, since they're not available outside of the game.
// Create and configure the desired program.
var program = MDKFactory.CreateProgram<Program>();
MDKFactory.Run(program);
}
}
}

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!--
Contains basic settings that should be included in code repositories
-->
<MDKVersion>1.5.17</MDKVersion>
<MDKTrimTypes>
<Enabled>no</Enabled>
</MDKTrimTypes>
<MDKMinify>
<Level>None</Level>
</MDKMinify>
<MDKIgnore>
<Folder>mdk</Folder>
</MDKIgnore>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!--
You can safely exclude this file from code repositories and use different
user settings per machine.
-->
<MDKVersion>1.5.17</MDKVersion>
<MDKUseGameBinPath>no</MDKUseGameBinPath>
<MDKGameBinPath>G:\SteamLibrary\SteamApps\common\SpaceEngineers\Bin64</MDKGameBinPath>
<MDKInstallPath>c:\users\lyssmetal\appdata\local\microsoft\visualstudio\17.0_6c02a30d\extensions\bnnd2422.thc</MDKInstallPath>
<MDKOutputPath>C:\Users\LyssMetal\AppData\Roaming\SpaceEngineers\IngameScripts\local</MDKOutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Collections.Immutable">
<HintPath>$(MDKGameBinPath)\System.Collections.Immutable.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="Sandbox.Common">
<HintPath>$(MDKGameBinPath)\Sandbox.Common.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Game">
<HintPath>$(MDKGameBinPath)\Sandbox.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Graphics">
<HintPath>$(MDKGameBinPath)\Sandbox.Graphics.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.Game">
<HintPath>$(MDKGameBinPath)\SpaceEngineers.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.ObjectBuilders">
<HintPath>$(MDKGameBinPath)\SpaceEngineers.ObjectBuilders.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage">
<HintPath>$(MDKGameBinPath)\VRage.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Audio">
<HintPath>$(MDKGameBinPath)\VRage.Audio.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Game">
<HintPath>$(MDKGameBinPath)\VRage.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Input">
<HintPath>$(MDKGameBinPath)\VRage.Input.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Library">
<HintPath>$(MDKGameBinPath)\VRage.Library.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Math">
<HintPath>$(MDKGameBinPath)\VRage.Math.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Render">
<HintPath>$(MDKGameBinPath)\VRage.Render.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Render11">
<HintPath>$(MDKGameBinPath)\VRage.Render11.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Scripting">
<HintPath>$(MDKGameBinPath)\VRage.Scripting.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="MDKUtilities">
<HintPath>$(MDKInstallPath)\MDKUtilities.dll</HintPath>
<Private>True</Private>
</Reference>
<Analyzer Include="$(MDKInstallPath)\Analyzers\MDKAnalyzer.dll" />
</ItemGroup>
</Project>

283
AutoPilot/Program.cs Normal file
View file

@ -0,0 +1,283 @@
/*
* API index: https://github.com/malware-dev/MDK-SE/wiki/Api-Index
* Vector transformation: https://github.com/malware-dev/MDK-SE/wiki/Vector-Transformations-with-World-Matrices
* How to get rotation/position: https://forum.keenswh.com/threads/how-do-i-get-the-world-position-and-rotation-of-a-ship.7363867/
*
*/
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.Collections.Immutable;
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;
using VRageMath.Spatial;
namespace IngameScript
{
partial class Program : MyGridProgram
{
const int CONSOLE_NB_LINES = 9;
const float MAX_SPEED = 0.15f; // In rad per second.
const int NB_OF_GYRO = 1000; // To limit the number of gyros set to overdrive.
enum State
{
NORMAL, // Normal state: Manual commands.
AUTO_STABILIZATION,
AUTO_DOCKING,
}
State state = State.NORMAL;
readonly Output output;
IMyRemoteControl remoteController;
IMyCubeGrid grid;
readonly List<IMyGyro> gyros = new List<IMyGyro>();
IMyBroadcastListener connnectorMinerPositionListener;
MatrixD connectorMinerPosition;
public Program()
{
this.grid = this.Me.CubeGrid;
var cockpits = new List<IMyCockpit>();
this.GridTerminalSystem.GetBlocksOfType(cockpits);
IMyCockpit cockpit = null;
foreach (var c in cockpits)
{
if (c.CubeGrid == this.grid)
{
cockpit = c;
break;
}
}
if (cockpit == null)
{
this.Echo("Can't find a cockpit");
return;
}
this.output = new Output(cockpit, CONSOLE_NB_LINES);
this.output.Print("Intializing navigation control...");
var remoteControls = new List<IMyRemoteControl>();
this.GridTerminalSystem.GetBlocksOfType(remoteControls);
foreach (var rc in remoteControls)
{
if (rc.CubeGrid == this.grid)
{
this.remoteController = rc;
break;
}
}
this.GridTerminalSystem.GetBlocksOfType(
this.gyros,
(IMyGyro gyro) => gyro.CubeGrid == this.grid
);
if (this.remoteController == null)
{
this.output.Print("Can't find a remote controller");
return;
}
this.connnectorMinerPositionListener = this.IGC.RegisterBroadcastListener("POSITION_CONNECTOR_MINER");
/*
var sensor = this.GridTerminalSystem.GetBlockWithName("Sensor") as IMySensorBlock;
var entities = new List<MyDetectedEntityInfo>();
sensor.DetectedEntities(entities);
foreach (var entity in entities)
{
entity.
this.output.Print(entity.Name);
}*/
this.Runtime.UpdateFrequency = UpdateFrequency.Update10;
this.output.Print("Navigation control initializing complete");
}
public void Save()
{
}
void UpdateStateAutoStabilization()
{
//var pos = this.remoteController.Position;
var gravity = this.remoteController.GetNaturalGravity().Normalized();
if (!Vector3D.IsZero(gravity))
{
var worldMatrix = this.remoteController.WorldMatrix;
var up = worldMatrix.Up;
var forward = worldMatrix.Forward;
var left = worldMatrix.Left;
//this.Print(gravity.ToString());
//gravity.ToString();
this.output.Display(
$"Gravity: {gravity.ToString("f2")}\n" +
$"Up: {up.ToString("f2")}\n" +
$"Foward: {forward.ToString("f2")}\n" +
$"Left: {left.ToString("f2")}\n",
1
);
var alpha = (float)Math.Asin(up.Dot(gravity));
var beta = (float)Math.Asin(left.Dot(gravity));
this.output.Display(
$"Alpha: {alpha}\n" +
$"Beta: {beta}\n" +
$"Is under control: {this.remoteController.IsUnderControl}",
2
);
for (int i = 0; i < NB_OF_GYRO && i < this.gyros.Count; i++)
{
var gyro = this.gyros[i];
gyro.Pitch = MathHelper.Clamp(-alpha, -MAX_SPEED, MAX_SPEED);
gyro.Roll = MathHelper.Clamp(beta, -MAX_SPEED, MAX_SPEED);
}
}
}
void UpdateStateAutoDocking()
{
if (this.connnectorMinerPositionListener.HasPendingMessage)
this.connectorMinerPosition = this.connnectorMinerPositionListener.AcceptMessage().As<MatrixD>();
if (this.connectorMinerPosition.IsValid())
{
this.output.Display($"Connector position:\n{this.connectorMinerPosition}", 3);
}
else
{
this.output.Display($"NOPE", 3);
}
}
void UpdateState()
{
switch (this.state)
{
case State.AUTO_STABILIZATION:
this.UpdateStateAutoStabilization();
break;
case State.AUTO_DOCKING:
this.UpdateStateAutoDocking();
break;
case State.NORMAL:
break; // Nothing.
}
}
void UpdateStateMachine(State newState)
{
if (this.state == newState)
return;
if (newState == State.NORMAL)
{
switch (this.state)
{
case State.AUTO_STABILIZATION:
foreach (var gyro in this.gyros)
gyro.GyroOverride = false;
this.state = State.NORMAL;
this.output.Print("Auto stabilization disabled");
break;
case State.AUTO_DOCKING:
this.state = State.NORMAL;
this.output.Print("Auto docking disabled");
break;
case State.NORMAL:
break; // Nothing.
}
}
else
{
this.UpdateStateMachine(State.NORMAL);
switch (newState)
{
case State.AUTO_STABILIZATION:
for (int i = 0; i < NB_OF_GYRO && i < this.gyros.Count; i++)
{
var gyro = this.gyros[i];
gyro.GyroOverride = true;
gyro.Yaw = 0f;
gyro.Pitch = 0f;
gyro.Roll = 0f;
}
this.state = State.AUTO_STABILIZATION;
this.output.Print("Auto stabilization enabled");
break;
case State.AUTO_DOCKING:
this.state = State.AUTO_DOCKING;
this.output.Print("Auto docking enabled");
break;
case State.NORMAL:
break; // Nothing.
}
}
}
public void Main(string argument, UpdateType updateSource)
{
if ((updateSource & UpdateType.Update10) != 0)
{
this.UpdateState();
}
else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0)
{
switch (argument)
{
case "SWITCH_AUTO_STABILIZATION":
if (this.state != State.AUTO_STABILIZATION)
this.UpdateStateMachine(State.AUTO_STABILIZATION);
else
this.UpdateStateMachine(State.NORMAL);
break;
case "SWITCH_AUTO_DOCKING":
if (this.state != State.AUTO_DOCKING)
this.UpdateStateMachine(State.AUTO_DOCKING);
else
this.UpdateStateMachine(State.NORMAL);
break;
default:
this.output.Print($"Uknown command: {argument}");
break;
}
}
}
}
}

BIN
AutoPilot/thumb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 385 KiB