Created
December 3, 2017 19:34
-
-
Save JabDoesThings/7dd401c440add9a9515720a3ae3954e3 to your computer and use it in GitHub Desktop.
Planetary Orbit Script (Outdated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Orbit Script by Jab (40BlocksUnder) | |
string configName = "OrbitSettings"; | |
IMyTerminalBlock Block = null; | |
double px = 0; | |
double py = 0; | |
double pz = 0; | |
double radius = 0; | |
double factor = 0; | |
double limit = 3000; | |
double randomFactor = 0; | |
double p = 0; | |
double phi = 0; | |
double theta = 0; | |
double waypointRadius = 100; | |
char[] delimiter_colon = {':' }; | |
char[] delimiter_line = {'\n'}; | |
int m_lastX = 0; | |
int m_lastY = 1; | |
int m_lastZ = 2; | |
int m_p = 3; | |
int m_phi = 4; | |
int m_theta = 5; | |
int m_lastWX = 6; | |
int m_lastWY = 7; | |
int m_lastWZ = 8; | |
void Main(String arg) { | |
var blocks = new List<IMyTerminalBlock>(); | |
// Get the antenna | |
GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(blocks); | |
if (blocks.Count > 0) { | |
Block = blocks[0]; | |
Dictionary<string, string> config = Load_Settings(configName); | |
if (config == null || config.Count == 0) { | |
config = Load_Default_Settings(configName); | |
} | |
px = Convert.ToDouble(config["center-x"]); | |
py = Convert.ToDouble(config["center-y"]); | |
pz = Convert.ToDouble(config["center-z"]); | |
radius = Convert.ToDouble(config["orbit-radius"]); | |
factor = Convert.ToDouble(config["waypoint-step-degrees"]); | |
waypointRadius = Convert.ToDouble(config["waypoint-radius"]); | |
randomFactor = Convert.ToDouble(config["waypoint-randomness"]); | |
string memoryPanel = config["memory"]; | |
string[] memory = Load_Memory(memoryPanel, 32); | |
// Get coordinates (VRageMath.Vector3D, so pull it in the ugly way) | |
double x = Math.Round(Block.GetPosition().GetDim(0), 4); | |
double y = Math.Round(Block.GetPosition().GetDim(1), 4); | |
double z = Math.Round(Block.GetPosition().GetDim(2), 4); | |
if (arg.Equals("reset")) { | |
List<IMyTerminalBlock> list = new List<IMyTerminalBlock>(); | |
GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(list); | |
if (list.Count > 0) { | |
var remote = list[0] as IMyRemoteControl; | |
remote.ClearWaypoints(); | |
} | |
p = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2)); | |
theta = Math.Atan(y / z); | |
phi = Math.Atan((Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2))) / z); | |
memory[m_lastX ] = x.ToString(); | |
memory[m_lastY ] = y.ToString(); | |
memory[m_lastZ ] = z.ToString(); | |
memory[m_p ] = p.ToString(); | |
memory[m_phi ] = phi.ToString(); | |
memory[m_theta ] = theta.ToString(); | |
memory[m_lastWX] = x.ToString(); | |
memory[m_lastWY] = y.ToString(); | |
memory[m_lastWZ] = z.ToString(); | |
Save_Memory(memoryPanel, memory); | |
Block.SetCustomName("Reset."); | |
return; | |
} | |
double facAsRad = factor * (Math.PI / 180); | |
double lastX = Convert.ToDouble(memory[m_lastX ]); | |
double lastY = Convert.ToDouble(memory[m_lastY ]); | |
double lastZ = Convert.ToDouble(memory[m_lastZ ]); | |
double lastP = Convert.ToDouble(memory[m_p ]); | |
double lastPhi = Convert.ToDouble(memory[m_phi ]); | |
double lastTheta = Convert.ToDouble(memory[m_theta ]); | |
double lastWX = Convert.ToDouble(memory[m_lastWX]); | |
double lastWY = Convert.ToDouble(memory[m_lastWY]); | |
double lastWZ = Convert.ToDouble(memory[m_lastWZ]); | |
if (GetTargetDistance(lastX, lastY, lastZ, x, y, z) > limit) { | |
p = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2)); | |
theta = Math.Atan(y / z); | |
phi = Math.Atan((Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2))) / z); | |
memory[m_p ] = p.ToString(); | |
memory[m_phi ] = phi.ToString(); | |
memory[m_theta ] = theta.ToString(); | |
} else { | |
phi = lastPhi + facAsRad; | |
theta = lastTheta; | |
} | |
var wx = px + (radius * Math.Sin(phi) * Math.Cos(theta)); | |
var wy = py + (radius * Math.Sin(phi) * Math.Sin(theta)); | |
var wz = pz + (radius * Math.Cos(phi)); | |
if (randomFactor > 0) { | |
Random rnd = new Random(); | |
int rval = Convert.ToInt32(randomFactor); | |
wx += Convert.ToDouble(rnd.Next(rval) - (rval / 2)); | |
wy += Convert.ToDouble(rnd.Next(rval) - (rval / 2)); | |
wz += Convert.ToDouble(rnd.Next(rval) - (rval / 2)); | |
} | |
memory[m_lastX ] = x.ToString(); | |
memory[m_lastY ] = y.ToString(); | |
memory[m_lastZ ] = z.ToString(); | |
String wcoords = ""; | |
// If the distance between the set point and the | |
// current position isn't close enough, do not | |
// update the Remote Waypoints. | |
var dis = GetTargetDistance(lastWX, lastWY, lastWZ, x, y, z); | |
if (dis > waypointRadius) { | |
Block.SetCustomName("Distance from Waypoint: " + dis.ToString()); | |
} else { | |
Block.SetCustomName("Creating new Waypoint."); | |
List<IMyTerminalBlock> list = new List<IMyTerminalBlock>(); | |
GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(list); | |
if (list.Count > 0) { | |
var remote = list[0] as IMyRemoteControl; | |
remote.ClearWaypoints(); | |
Vector3D pos = new Vector3D(wx, wy, wz); | |
remote.AddWaypoint(pos, "NextPoint"); | |
remote.AddWaypoint(pos, "NextPoint"); | |
remote.SetAutoPilotEnabled(true); | |
} | |
memory[m_phi ] = phi.ToString(); | |
memory[m_theta ] = theta.ToString(); | |
memory[m_lastWX] = wx.ToString(); | |
memory[m_lastWY] = wy.ToString(); | |
memory[m_lastWZ] = wz.ToString(); | |
} | |
Save_Memory(memoryPanel, memory); | |
} | |
} | |
public string[] Load_Memory(string panel, int size) { | |
var lcd = GridTerminalSystem.GetBlockWithName(panel) as IMyTextPanel; | |
string data = lcd.GetPublicText(); | |
if (data != "") { | |
string[] ret = data.Split('\n'); | |
int length = ret.Length; | |
if (length > size) size = length; | |
string[] fullRet = new string[size]; | |
for (int index = 0; index < length; index++) { | |
fullRet[index] = ret[index]; | |
} | |
return fullRet; | |
} | |
return new string[size]; | |
} | |
public void Save_Memory(string panel, string[] memory) { | |
StringBuilder builder = new StringBuilder(); | |
foreach (string value in memory) { | |
if (value != null && value != "") { | |
builder.Append(value + "\n"); | |
} | |
} | |
var lcd = GridTerminalSystem.GetBlockWithName(panel) as IMyTextPanel; | |
lcd.WritePublicText(builder.ToString()); | |
lcd.ShowTextureOnScreen(); | |
lcd.ShowPublicTextOnScreen(); | |
} | |
public Dictionary<string, string> Load_Settings(string panel) { | |
Dictionary<string, string> config = new Dictionary<string, string>(); | |
var lcd = GridTerminalSystem.GetBlockWithName(panel) as IMyTextPanel; | |
if (lcd == null) return null; | |
string data = lcd.GetPublicText(); | |
if (data == "") return null; | |
string[] ret = data.Split(delimiter_line); | |
for (int index = 0; index < ret.Length; index++) { | |
string setting = ret[index]; | |
if (setting == "") continue; | |
string[] split = Array.ConvertAll(setting.Split(delimiter_colon), p => p.Trim()); | |
if (split.Length < 2) continue; | |
string settingName = split[0]; | |
string settingValue = split[1]; | |
if (settingName == "") continue; | |
if (settingName.StartsWith("#")) continue; | |
config[settingName] = settingValue; | |
} | |
return config; | |
} | |
public Dictionary<string, string> Load_Default_Settings(string panel) { | |
Dictionary<string, string> config = new Dictionary<string, string>(); | |
config.Add("# The coordinates to orbit around.", "#"); | |
config.Add("center-x", "0"); | |
config.Add("center-y", "0"); | |
config.Add("center-z", "0"); | |
config.Add("# The radius from the center to orbit.", "#"); | |
config.Add("orbit-radius", "25000"); | |
config.Add("# How many degrees between each new position.", "#"); | |
config.Add("waypoint-step-degrees", "5"); | |
config.Add("# Adds randomness for the waypoint. Good for multiple ships.", "#"); | |
config.Add("waypoint-randomness", "100"); | |
config.Add("# Radius of the area around the waypoint to move to the next.", "#"); | |
config.Add("waypoint-radius", "150"); | |
config.Add("# Memory cards (Text Panels). Will eventually drop to one.", "#"); | |
config.Add("memory", "memory"); | |
StringBuilder builder = new StringBuilder(); | |
var en = config.GetEnumerator(); | |
while (en.MoveNext()) { | |
var entry = en.Current; | |
var key = entry.Key; | |
var val = entry.Value; | |
// If this line is a comment. | |
if (key.StartsWith("#")) { | |
builder.Append(key + "\n"); | |
} else { | |
if (val == "") continue; | |
builder.Append(key + ": " + val.ToString() + "\n"); | |
} | |
} | |
string text = builder.ToString(); | |
var lcd = GridTerminalSystem.GetBlockWithName(panel) as IMyTextPanel; | |
lcd.WritePublicText(text); | |
lcd.ShowTextureOnScreen(); | |
lcd.ShowPublicTextOnScreen(); | |
return config; | |
} | |
public int GetTargetDistance(double oldx, double oldy, double oldz, double newx, double newy, double newz) { | |
double deltaX = newx - oldx; | |
double deltaY = newy - oldy; | |
double deltaZ = newz - oldz; | |
int distance = (int) Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ); | |
return distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment