Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// Whip's Artificial Horizon Script v36 // - rev: 5/19/17
- _______________________________________________________________________
- ///DESCRIPTION///
- This code creates an Artificial Horizon indicator that takes into account your pitch and roll.
- Also displayed on the screen is a vector indicator of velocity, speed display, and bearing display.
- The colors of the display grid are configurable in the code.
- _______________________________________________________________________
- ///SETUP///
- 1.) Find the control seat, cockpit, remote control, or passenger seat that you wish to
- use as your horizon reference and add the name tag "Forward" somewhere in its name
- 2.) Find the Text Panel or LCD Panel that you want to display the Artificial Horizon on and add the tag
- "Horizon" somewhere in the name of the block
- - Change the font to MONOSPACED
- 3.) Make a program block and put this code in it
- 4.) Make a timer with actions
- - "Trigger Now" itself
- - "Start" itself
- - "Run with default argument" the program from step 1
- 5.) Start the timer
- 6.) Fly around and enjoy :)
- _______________________________________________________________________
- ///WHIP'S NOTES///
- I vastly improved the code's number -> pixel conversion system.
- If you notice any bugs, be sure to comment on the workshop page.
- Coded by Whiplash141
- */
- //-------------------------------------------------
- //CONFIGURABLE VARAIABLES
- //-------------------------------------------------
- const string controlSeatNameTag = "Forward";
- const string readoutScreenNameTag = "Horizon";
- const double maximumVelocity = 100; //supports up to 999 m/s max speed
- const int updatesPerSecond = 10;
- const bool enableOrientationIndicator = true; //specifies if the orientation axes are drawn with the artificial horizon
- const bool useVelocityMagnitude = false; //if the magnitude of the velocity should be accounted for in the velocity indicator
- bool pauseWhenAway = true; //whether or not the code should stop executing when no-one is piloting the ship
- bool ignoreBlocksOnConnectors = true;
- bool shipControllersOnSameGrid = false; //if the control seats are on same grid as the ship
- bool enableColorBlindMode = false; //this will change all colors to the colorblind default constants automatically
- //-------------------------------------------------
- //COLOR DEFAULTS: You can change these
- // Colors are defined with RGB color codes where
- // RGB values can range from (0,0,0) [black] to (7,7,7) [white].
- // You can also use the presets defined in the COLOR PRESETS section
- //-------------------------------------------------
- static char backgroundColor = CreateCustomColor(0, 0, 0);
- static char horizonLineColor = CreateCustomColor(0, 0, 0);
- static char groundColor = CreateCustomColor(1, 2, 1);
- static char skyColor = CreateCustomColor(1, 1, 2);
- static char textColor = CreateCustomColor(4, 0, 0);
- static char numberColor = CreateCustomColor(4, 4, 0);
- static char velocityIndicatorColor = CreateCustomColor(4, 4, 0);
- static char spaceOrientationColor = CreateCustomColor(0, 0, 4);
- static char planetaryOrientationColor = CreateCustomColor(4, 0, 0);
- //-------------------------------------------------
- //COLORBLIND OVERRIDES: These will overwrite the constants above
- // if colorblind mode is enabled
- //-------------------------------------------------
- void ColorBlindOverrides()
- {
- //these define the colorblind color constants
- backgroundColor = black;
- horizonLineColor = lightGray;
- groundColor = mediumGray;
- skyColor = black;
- textColor = yellow;
- numberColor = yellow;
- velocityIndicatorColor = cyan;
- spaceOrientationColor = green;
- planetaryOrientationColor = red;
- }
- //===================================================
- //DO NOT TOUCH ANYTHING BELOW THIS
- //===================================================
- //-------------------------------------------------
- //COLOR PRESETS: Do not change!!!
- const char red = '\uE200'; //No touchey
- const char medRed = '\uE1C0'; //No touchey
- const char darkRed = '\uE180'; //No touchey
- const char green = '\uE120'; //No touchey
- const char medGreen = '\uE118'; //No touchey
- const char darkGreen = '\uE110'; //No touchey
- const char blue = '\uE104'; //No touchey
- const char medBlue = '\uE103'; //No touchey
- const char darkBlue = '\uE102'; //No touchey
- const char yellow = '\uE220'; //No touchey
- const char medYellow = '\uE1D8'; //No touchey
- const char darkYellow = '\uE190'; //No touchey
- const char magenta = '\uE204'; //No touchey
- const char medMagenta = '\uE1C3'; //No touchey
- const char darkMagenta = '\uE182'; //No touchey
- const char cyan = '\uE124'; //No touchey
- const char medCyan = '\uE11B'; //No touchey
- const char darkCyan = '\uE112'; //No touchey
- const char white = '\uE2FF'; //No touchey
- const char lightGray = '\uE1DB'; //No touchey
- const char mediumGray = '\uE192'; //No touchey
- const char darkGray = '\uE149'; //No touchey
- const char black = '\uE100'; //No touchey
- //-------------------------------------------------
- const int horizontalMidpoint = 26; //DO NOT TOUCH
- const int verticalMidpoint = 24; //DO NOT TOUCH
- const int horizontalGridSize = horizontalMidpoint * 2 - 1;
- const int verticalGridSize = verticalMidpoint * 2 - 1;
- const int planeSymbolWidth = 10; //measured from center to wingtip
- int velocityRow = 0;
- int velocityColumn = 0;
- const double timeCycleMax = 1 / (double)updatesPerSecond;
- const double rad2deg = 180 / Math.PI;
- const double deg2rad = Math.PI / 180;
- const double horizontalVelocityIncrement = maximumVelocity / (double)horizontalMidpoint;
- const double verticalVelocityIncrement = maximumVelocity / (double)verticalMidpoint;
- const double pitchIncrement = 90 / (double)verticalMidpoint;
- double timeCurrentCycle = 0;
- double shipSpeed = 0;
- double shipAcceleration = 0;
- double rollAngle = 0;
- double pitchAngle = 0;
- double bearingAngle = 0;
- double altitude = 0;
- double lastSpeed = 0;
- bool isUpsideDown = false;
- bool inGravity = false;
- bool isBackwards = false;
- string headingAndVelocityPixelString = "";
- string altitudePixelString = "";
- Vector3D absoluteNorthVec = new Vector3D(0.342063708833718, -0.704407897782847, -0.621934025954579);
- //This is the rotation axis of the sun in vanilla
- //this was determined from the game files
- IMyShipController reference = null;
- Program()
- {
- if (enableColorBlindMode)
- {
- ColorBlindOverrides();
- }
- InitializeGrid();
- InitializeCharacterLists();
- }
- StringBuilder gridBuilder = new StringBuilder();
- List<string> emptyGrid = new List<string>();
- List<string> skyGrid = new List<string>();
- List<StringBuilder> horizonGrid = new List<StringBuilder>();
- void InitializeGrid()
- {
- for (int i = 1; i <= verticalGridSize; i++)
- {
- emptyGrid.Add(new String(backgroundColor, horizontalGridSize));
- }
- for (int i = 1; i <= verticalGridSize; i++)
- {
- skyGrid.Add(new String(skyColor, horizontalGridSize));
- }
- for (int i = 1; i <= verticalGridSize; i++)
- {
- horizonGrid.Add(new StringBuilder());
- }
- }
- static char CreateCustomColor(int r, int g, int b)
- {
- return (char)(0xE100 + (MathHelper.Clamp(r, 0, 7) << 6) + (MathHelper.Clamp(g, 0, 7) << 3) + MathHelper.Clamp(b, 0, 7));
- }
- void AssignGridDefaultValues(List<string> gridDefault)
- {
- for (int i = 0; i < horizonGrid.Count; i++)
- {
- var thisStringBuilder = horizonGrid[i];
- thisStringBuilder.Clear();
- thisStringBuilder.Append(gridDefault[i]);
- }
- }
- void Main()
- {
- timeCurrentCycle += Runtime.TimeSinceLastRun.TotalSeconds;
- if (timeCurrentCycle >= timeCycleMax)
- {
- Echo("WMI Artificial Horizon is online... " + RunningSymbol());
- bool isProperlySetup = CheckBlocks();
- if (isProperlySetup)
- {
- GetRollPitchAndHeading();
- GetVelocity();
- GetNumberPixelString();
- DrawGrid();
- }
- timeCurrentCycle = 0;
- }
- }
- //Whip's Get Subgrid Base Method
- IMyCubeGrid GetSubgridBase(IMyTerminalBlock thisBlock)
- {
- //small rotor head, large base
- var rotorHeads = new List<IMyMotorRotor>();
- GridTerminalSystem.GetBlocksOfType(rotorHeads, block => block.CubeGrid == thisBlock.CubeGrid && block.CubeGrid.GridSizeEnum.ToString() == "Small");
- foreach (var thisHead in rotorHeads)
- {
- if (thisHead.IsAttached && thisHead.Base.CubeGrid.GridSizeEnum.ToString() == "Large")
- return thisHead.Base.CubeGrid;
- }
- //large rotor base, small head
- var rotorBases = new List<IMyMotorStator>();
- GridTerminalSystem.GetBlocksOfType(rotorBases, block => block.CubeGrid == thisBlock.CubeGrid && block.CubeGrid.GridSizeEnum.ToString() == "Small");
- foreach (var thisBase in rotorBases)
- {
- if (thisBase.IsAttached && thisBase.TopGrid.GridSizeEnum.ToString() == "Large")
- return thisBase.TopGrid;
- }
- return null;
- }
- bool CheckBlocks()
- {
- var shipControllers = new List<IMyShipController>();
- GridTerminalSystem.GetBlocksOfType(shipControllers, shouldAddController);
- if (shipControllers.Count == 0)
- {
- Echo("Error: No ship controllers with name " + controlSeatNameTag + " detected");
- return false;
- }
- else
- {
- reference = shipControllers[0];
- return true;
- }
- }
- bool shouldAddController(IMyTerminalBlock block)
- {
- if (shipControllersOnSameGrid)
- return block.CustomName.Contains(controlSeatNameTag) && block.CubeGrid == Me.CubeGrid;
- else
- return block.CustomName.Contains(controlSeatNameTag);
- }
- T GetClosestBlockOfType<T>(string name = "") where T : class, IMyTerminalBlock
- {
- var allBlocks = new List<T>();
- if (name == "")
- GridTerminalSystem.GetBlocksOfType(allBlocks);
- else
- GridTerminalSystem.GetBlocksOfType(allBlocks, block => block.CustomName.Contains(name));
- if (allBlocks.Count == 0)
- {
- return null;
- }
- var closestBlock = allBlocks[0];
- var shortestDistance = Vector3D.DistanceSquared(Me.GetPosition(), closestBlock.GetPosition());
- allBlocks.Remove(closestBlock); //remove this block from the list
- foreach (T thisBlock in allBlocks)
- {
- var thisDistance = Vector3D.DistanceSquared(Me.GetPosition(), thisBlock.GetPosition());
- if (thisDistance < shortestDistance)
- {
- closestBlock = thisBlock;
- shortestDistance = thisDistance;
- }
- //otherwise move to next one
- }
- return closestBlock;
- }
- void AddToGrid(int row, int column, char color)
- {
- if (row - 1 >= 0 && row - 1 < horizonGrid.Count)
- {
- var thisRow = horizonGrid[row - 1]; //need to offset index down one
- if (column - 1 >= 0 && column - 1 < thisRow.Length)
- {
- thisRow[column - 1] = color;
- }
- }
- }
- void GetVelocity() //This method gets the relative position of the velocity vector on the screen
- {
- var velocityVec = reference.GetShipVelocities().LinearVelocity;
- shipSpeed = velocityVec.Length(); //raw speed of ship
- shipAcceleration = Math.Abs(shipSpeed - lastSpeed) / timeCycleMax;
- lastSpeed = shipSpeed;
- if (!useVelocityMagnitude && velocityVec.Length() > 0.1)
- {
- velocityVec = Vector3D.Normalize(velocityVec) * maximumVelocity;
- }
- var rightVelocity = VectorProjection(velocityVec, reference.WorldMatrix.Right).Length() * VectorCompareDirection(velocityVec, reference.WorldMatrix.Right);
- var upVelocity = VectorProjection(velocityVec, reference.WorldMatrix.Up).Length() * VectorCompareDirection(velocityVec, reference.WorldMatrix.Up);
- var forwardVelocity = VectorProjection(velocityVec, reference.WorldMatrix.Forward).Length() * VectorCompareDirection(velocityVec, reference.WorldMatrix.Forward);
- if (rightVelocity < 0)
- {
- velocityColumn = horizontalMidpoint - (int)Math.Round(-rightVelocity / horizontalVelocityIncrement);
- }
- else
- {
- velocityColumn = horizontalMidpoint + (int)Math.Round(rightVelocity / horizontalVelocityIncrement);
- }
- if (upVelocity < 0)
- {
- velocityRow = verticalMidpoint + (int)Math.Round(-upVelocity / verticalVelocityIncrement);
- }
- else
- {
- velocityRow = verticalMidpoint - (int)Math.Round(upVelocity / verticalVelocityIncrement);
- }
- if (forwardVelocity < 0)
- {
- isBackwards = true;
- }
- else
- {
- isBackwards = false;
- }
- if (!isBackwards) //draws prograde velocity
- {
- /*
- Looks like:
- o
- o
- o o o
- o o o o o o
- o o o
- */
- AddToGrid(velocityRow + 1, velocityColumn, velocityIndicatorColor);
- AddToGrid(velocityRow + 1, velocityColumn + 1, velocityIndicatorColor);
- AddToGrid(velocityRow + 1, velocityColumn - 1, velocityIndicatorColor);
- AddToGrid(velocityRow - 1, velocityColumn, velocityIndicatorColor);
- AddToGrid(velocityRow - 1, velocityColumn + 1, velocityIndicatorColor);
- AddToGrid(velocityRow - 1, velocityColumn - 1, velocityIndicatorColor);
- AddToGrid(velocityRow, velocityColumn + 1, velocityIndicatorColor);
- AddToGrid(velocityRow, velocityColumn - 1, velocityIndicatorColor);
- AddToGrid(velocityRow - 2, velocityColumn, velocityIndicatorColor);
- AddToGrid(velocityRow - 3, velocityColumn, velocityIndicatorColor);
- AddToGrid(velocityRow, velocityColumn + 2, velocityIndicatorColor);
- AddToGrid(velocityRow, velocityColumn + 3, velocityIndicatorColor);
- AddToGrid(velocityRow, velocityColumn - 2, velocityIndicatorColor);
- AddToGrid(velocityRow, velocityColumn - 3, velocityIndicatorColor);
- }
- else //draws retrograde velocity
- {
- /*
- Looks like:
- o o
- o o
- o
- o o
- o o
- */
- AddToGrid(velocityRow, velocityColumn, velocityIndicatorColor);
- AddToGrid(velocityRow - 1, velocityColumn + 1, velocityIndicatorColor);
- AddToGrid(velocityRow - 1, velocityColumn - 1, velocityIndicatorColor);
- AddToGrid(velocityRow + 1, velocityColumn + 1, velocityIndicatorColor);
- AddToGrid(velocityRow + 1, velocityColumn - 1, velocityIndicatorColor);
- AddToGrid(velocityRow - 2, velocityColumn + 2, velocityIndicatorColor);
- AddToGrid(velocityRow - 2, velocityColumn - 2, velocityIndicatorColor);
- AddToGrid(velocityRow + 2, velocityColumn + 2, velocityIndicatorColor);
- AddToGrid(velocityRow + 2, velocityColumn - 2, velocityIndicatorColor);
- }
- //Echo("Ship Velocity: " + Math.Round(shipSpeed, 2).ToString());
- }
- void DrawOrientationIndicator()
- {
- if (!inGravity)
- {
- for (int j = 1; j <= horizontalGridSize; j++)
- {
- AddToGrid(verticalMidpoint, j, spaceOrientationColor); //draws a horizontal line
- }
- for (int j = 1; j <= verticalGridSize; j++)
- {
- AddToGrid(j, horizontalMidpoint, spaceOrientationColor); //draws a vertical line
- }
- }
- else //draws a nose orientation indicator that looks like --- W ---
- {
- AddToGrid(verticalMidpoint, horizontalMidpoint, planetaryOrientationColor);
- AddToGrid(verticalMidpoint + 1, horizontalMidpoint + 1, planetaryOrientationColor);
- AddToGrid(verticalMidpoint + 1, horizontalMidpoint - 1, planetaryOrientationColor);
- AddToGrid(verticalMidpoint, horizontalMidpoint - 2, planetaryOrientationColor);
- AddToGrid(verticalMidpoint, horizontalMidpoint + 2, planetaryOrientationColor);
- AddToGrid(verticalMidpoint - 1, horizontalMidpoint - 3, planetaryOrientationColor);
- AddToGrid(verticalMidpoint - 1, horizontalMidpoint + 3, planetaryOrientationColor);
- for (int j = horizontalMidpoint - planeSymbolWidth; j < horizontalMidpoint - 4; j++)
- {
- AddToGrid(verticalMidpoint, j, planetaryOrientationColor);
- }
- for (int j = horizontalMidpoint + 5; j <= horizontalMidpoint + planeSymbolWidth; j++)
- {
- AddToGrid(verticalMidpoint, j, planetaryOrientationColor);
- }
- }
- }
- void GetRollPitchAndHeading()
- {
- /// Get Needed Vectors ///
- Vector3D shipForwardVec = reference.WorldMatrix.Forward;
- Vector3D shipLeftVec = reference.WorldMatrix.Left;
- Vector3D shipDownVec = reference.WorldMatrix.Down;
- Vector3D gravityVec = reference.GetNaturalGravity();
- Vector3D planetRelativeLeftVec = shipForwardVec.Cross(gravityVec);
- reference.TryGetPlanetElevation(MyPlanetElevation.Surface, out altitude);
- if (gravityVec.LengthSquared() == 0)
- {
- Echo("No natural gravity field detected");
- inGravity = false;
- AssignGridDefaultValues(emptyGrid);
- DrawOrientationIndicator();
- return;
- }
- inGravity = true;
- AssignGridDefaultValues(skyGrid);
- isUpsideDown = false;
- /// Compute Pitch and Roll ///
- if (!VectorIsSameDirection(shipDownVec, gravityVec))
- {
- isUpsideDown = true;
- }
- rollAngle = VectorAngleBetween(shipLeftVec, planetRelativeLeftVec) * rad2deg;
- rollAngle *= VectorCompareDirection(VectorProjection(shipLeftVec, gravityVec), gravityVec); //ccw is positive
- if (rollAngle > 90 || rollAngle < -90)
- {
- rollAngle = 180 - rollAngle; //accounts for upsidedown
- }
- pitchAngle = VectorAngleBetween(shipForwardVec, gravityVec) * rad2deg; //angle from nose direction to gravity
- pitchAngle -= 90; //as 90 degrees is level with ground
- //Echo("Roll angle: " + Math.Round(rollAngle, 2).ToString() + "\nPitch angle: " + Math.Round(pitchAngle, 2).ToString());
- GetHorizonLine(); //gets horizon line
- if (enableOrientationIndicator)
- {
- DrawOrientationIndicator(); //draws orientation indicator
- }
- /// Compute Bearing ///
- //get east vector
- Vector3D relativeEastVec = gravityVec.Cross(absoluteNorthVec);
- //get relative north vector
- Vector3D relativeNorthVec = relativeEastVec.Cross(gravityVec);
- //project forward vector onto a plane comprised of the north and east vectors
- Vector3D forwardProjNorthVec = VectorProjection(shipForwardVec, relativeNorthVec);
- Vector3D forwardProjEastVec = VectorProjection(shipForwardVec, relativeEastVec);
- Vector3D forwardProjPlaneVec = forwardProjEastVec + forwardProjNorthVec;
- //find angle from abs north to projected forward vector measured clockwise
- bearingAngle = VectorAngleBetween(forwardProjPlaneVec, relativeNorthVec) * rad2deg;
- if (VectorIsSameDirection(shipForwardVec, relativeEastVec) == false)
- {
- bearingAngle = 360 - bearingAngle; //because of how the angle is measured
- }
- //Echo("Bearing: " + Math.Round(bearingAngle, 2).ToString());
- }
- void GetHorizonLine()
- {
- int horizontalOffset = (int)Math.Round(pitchAngle / pitchIncrement * Math.Sin(rollAngle * deg2rad)); //offset of every point in the horizontal direction
- int verticalOffset = (int)Math.Round(pitchAngle / pitchIncrement * Math.Cos(rollAngle * deg2rad)); //offset of every point in the vertical direction
- double constant = 1;
- if (isUpsideDown)
- {
- verticalOffset *= -1;
- constant = -1;
- }
- int adjustedMidpoint = verticalMidpoint - horizontalOffset; //offsets our midpoint horizontally due to pitch and roll
- for (int i = 1; i <= horizontalGridSize; i++) //rows
- {
- int thisHeight = 0;
- if (i < verticalMidpoint)
- {
- thisHeight = adjustedMidpoint - (int)Math.Round((adjustedMidpoint - i) * Math.Tan(constant * rollAngle * deg2rad));
- }
- else
- {
- thisHeight = adjustedMidpoint + (int)Math.Round((i - adjustedMidpoint) * Math.Tan(constant * rollAngle * deg2rad));
- }
- thisHeight += verticalOffset; //offset our computed height by this value
- AddToGrid(thisHeight, i, horizonLineColor);
- for (int j = 1; j <= verticalGridSize; j++)
- {
- if (!isUpsideDown)
- {
- if (j > thisHeight)
- {
- AddToGrid(j, i, groundColor);
- }
- }
- else
- {
- if (j < thisHeight)
- {
- AddToGrid(j, i, groundColor);
- }
- }
- }
- }
- }
- void DrawGrid() //draws graphical grid
- {
- gridBuilder.Clear();
- gridBuilder.Append(headingAndVelocityPixelString).Append("\n");
- foreach (StringBuilder thisRow in horizonGrid)
- {
- gridBuilder.Append("\ue077").AppendLine(thisRow.ToString());
- }
- gridBuilder.Append("\n" + altitudePixelString);
- WriteToTextPanel(readoutScreenNameTag, gridBuilder.ToString());
- }
- void GetNumberPixelString() //gets a graphical string from velocity and heading
- {
- //Velocity Splitting
- string velocityString = string.Format("{0:000}", shipSpeed);
- string altitudeString = string.Format("{0:00000}", altitude);
- string headingOrAccelString = "";
- if (inGravity)
- headingOrAccelString = string.Format("{0:000}", bearingAngle);
- else
- headingOrAccelString = string.Format("{0:000}", shipAcceleration);
- DecomposeNumber velocityStruct;
- DecomposeNumber headingOrAccelStruct;
- DecomposeNumber altitudeStruct;
- List<string> velocityWordStruct = GetPixelsFromNumber("spd");
- List<string> altitudeWordStruct = GetPixelsFromNumber("alt");
- List<string> headingOrAccelWordStruct;
- if (inGravity)
- headingOrAccelWordStruct = GetPixelsFromNumber("hdg");
- else
- headingOrAccelWordStruct = GetPixelsFromNumber("acc");
- List<string> space = new List<string>()
- {
- "\ue076",
- "\ue076",
- "\ue076",
- "\ue076",
- "\ue076"
- };
- velocityStruct.Hundreds = GetPixelsFromNumber(velocityString.Substring(0, 1));
- velocityStruct.Tens = GetPixelsFromNumber(velocityString.Substring(1, 1));
- velocityStruct.Ones = GetPixelsFromNumber(velocityString.Substring(2, 1));
- headingOrAccelStruct.Hundreds = GetPixelsFromNumber(headingOrAccelString.Substring(0, 1));
- headingOrAccelStruct.Tens = GetPixelsFromNumber(headingOrAccelString.Substring(1, 1));
- headingOrAccelStruct.Ones = GetPixelsFromNumber(headingOrAccelString.Substring(2, 1));
- altitudeStruct.TenThousands = GetPixelsFromNumber(altitudeString.Substring(0, 1));
- altitudeStruct.Thousands = GetPixelsFromNumber(altitudeString.Substring(1, 1));
- altitudeStruct.Hundreds = GetPixelsFromNumber(altitudeString.Substring(2, 1));
- altitudeStruct.Tens = GetPixelsFromNumber(altitudeString.Substring(3, 1));
- altitudeStruct.Ones = GetPixelsFromNumber(altitudeString.Substring(4, 1));
- List<List<string>> headingAndVelocityCharacterList = new List<List<string>>()
- {
- space, velocityWordStruct, velocityStruct.Hundreds, velocityStruct.Tens, velocityStruct.Ones, space, headingOrAccelWordStruct, headingOrAccelStruct.Hundreds, headingOrAccelStruct.Tens, headingOrAccelStruct.Ones
- };
- List<List<string>> altitudeCharacterList = new List<List<string>>();
- if (inGravity)
- {
- altitudeCharacterList = new List<List<string>>()
- {
- altitudeWordStruct, altitudeStruct.TenThousands, altitudeStruct.Thousands, altitudeStruct.Hundreds, altitudeStruct.Tens, altitudeStruct.Ones
- };
- }
- else
- {
- altitudeCharacterList = new List<List<string>>()
- {
- altitudeWordStruct, GetPixelsFromNumber("no grav")
- };
- }
- headingAndVelocityPixelString = ConstructPixelString(headingAndVelocityCharacterList);
- altitudePixelString = ConstructPixelString(altitudeCharacterList);
- }
- StringBuilder pixelString = new StringBuilder();
- string ConstructPixelString(List<List<string>> pixelCharacters) //each list has 1-5 rows
- {
- pixelString.Clear();
- for (int i = 0; i < 5; i++)
- {
- foreach (List<string> thisCharacter in pixelCharacters)
- {
- pixelString.Append(thisCharacter[i]);
- }
- pixelString.Append("\n");
- }
- return pixelString.ToString();
- }
- struct DecomposeNumber
- {
- public List<string> Ones;
- public List<string> Tens;
- public List<string> Hundreds;
- public List<string> Thousands;
- public List<string> TenThousands;
- }
- const string largeSpace = "\ue078\ue077";
- List<string> GetPixelsFromNumber(string num) //gets graphical representation from a double
- {
- List<string> numberGraphicRows = new List<string>();
- switch (num)
- {
- case "0":
- numberGraphicRows = rows0;
- break;
- case "1":
- numberGraphicRows = rows1;
- break;
- case "2":
- numberGraphicRows = rows2;
- break;
- case "3":
- numberGraphicRows = rows3;
- break;
- case "4":
- numberGraphicRows = rows4;
- break;
- case "5":
- numberGraphicRows = rows5;
- break;
- case "6":
- numberGraphicRows = rows6;
- break;
- case "7":
- numberGraphicRows = rows7;
- break;
- case "8":
- numberGraphicRows = rows8;
- break;
- case "9":
- numberGraphicRows = rows9;
- break;
- case "spd":
- numberGraphicRows = rowsSpd;
- break;
- case "hdg":
- numberGraphicRows = rowsHdg;
- break;
- case "alt":
- numberGraphicRows = rowsAlt;
- break;
- case "no grav":
- numberGraphicRows = rowsNoGrav;
- break;
- case "acc":
- numberGraphicRows = rowsAcc;
- break;
- default:
- numberGraphicRows = rows0;
- break;
- }
- return numberGraphicRows;
- }
- void WriteToTextPanel(string textPanelName, string textToWrite, bool append = false)
- {
- var listScreens = new List<IMyTextPanel>();
- if (!ignoreBlocksOnConnectors)
- {
- GridTerminalSystem.GetBlocksOfType(listScreens, block => block.CustomName.Contains(textPanelName));
- }
- else
- {
- var connectors = new List<IMyShipConnector>();
- var connectorGrids = new List<IMyCubeGrid>();
- GridTerminalSystem.GetBlocksOfType(connectors, block => block.CubeGrid == Me.CubeGrid);
- foreach (IMyShipConnector thisConnector in connectors)
- {
- if (thisConnector.Status == MyShipConnectorStatus.Connected)
- {
- connectorGrids.Add(thisConnector.OtherConnector.CubeGrid);
- }
- }
- GridTerminalSystem.GetBlocksOfType(listScreens, block => block.CustomName.Contains(textPanelName) && !connectorGrids.Contains(block.CubeGrid));
- }
- if (listScreens.Count == 0)
- {
- Echo("Error: No text panel with name tag '" + textPanelName + "' was found");
- return;
- }
- else
- {
- for (int i = 0; i < listScreens.Count; i++)
- {
- var thisScreen = listScreens[i];
- thisScreen.WritePublicText(textToWrite, append);
- thisScreen.ShowTextureOnScreen();
- thisScreen.ShowPublicTextOnScreen();
- thisScreen.SetValue("FontSize", 0.3f); //for large grid
- thisScreen.SetValue<long>("Font", 1147350002);
- }
- }
- }
- int VectorCompareDirection(Vector3D a, Vector3D b) //returns -1 if vectors return negative dot product
- {
- double check = a.Dot(b);
- if (check < 0)
- return -1;
- else
- return 1;
- }
- double VectorAngleBetween(Vector3D a, Vector3D b) //returns radians
- {
- if (a.LengthSquared() == 0 || b.LengthSquared() == 0)
- return 0;
- else
- return Math.Acos(MathHelper.Clamp(a.Dot(b) / a.Length() / b.Length(), -1, 1));
- }
- Vector3D VectorProjection(Vector3D a, Vector3D b) //projects a onto b
- {
- Vector3D projection = a.Dot(b) / b.LengthSquared() * b;
- return projection;
- }
- bool VectorIsSameDirection(Vector3D a, Vector3D b) //returns true if vectors produce positive dot product
- {
- double check = a.Dot(b);
- if (check < 0)
- return false;
- else
- return true;
- }
- //Whip's Running Symbol Method v6
- int runningSymbolVariant = 0;
- string RunningSymbol()
- {
- runningSymbolVariant++;
- string strRunningSymbol = "";
- if (runningSymbolVariant == 0)
- strRunningSymbol = "|";
- else if (runningSymbolVariant == 1)
- strRunningSymbol = "/";
- else if (runningSymbolVariant == 2)
- strRunningSymbol = "--";
- else if (runningSymbolVariant == 3)
- {
- strRunningSymbol = "\\";
- runningSymbolVariant = -1;
- }
- return strRunningSymbol;
- }
- List<string> rows0 = new List<string>();
- List<string> rows1 = new List<string>();
- List<string> rows2 = new List<string>();
- List<string> rows3 = new List<string>();
- List<string> rows4 = new List<string>();
- List<string> rows5 = new List<string>();
- List<string> rows6 = new List<string>();
- List<string> rows7 = new List<string>();
- List<string> rows8 = new List<string>();
- List<string> rows9 = new List<string>();
- List<string> rowsSpd = new List<string>();
- List<string> rowsHdg = new List<string>();
- List<string> rowsAlt = new List<string>();
- List<string> rowsNoGrav = new List<string>();
- List<string> rowsAcc= new List<string>();
- void InitializeCharacterLists()
- {
- #region character_list_declaration
- rows0 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rows1 = new List<string>()
- {
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor })
- };
- rows2 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rows3 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rows4 = new List<string>()
- {
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor })
- };
- rows5 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rows6 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rows7 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, numberColor, backgroundColor, backgroundColor })
- };
- rows8 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rows9 = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, numberColor, numberColor, backgroundColor })
- };
- rowsSpd = new List<string>()
- {
- new string(new char[] { backgroundColor, textColor, textColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor }),
- new string(new char[] { textColor, textColor, textColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { backgroundColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor }),
- new string(new char[] { textColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor })
- };
- rowsHdg = new List<string>()
- {
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor }),
- new string(new char[] { textColor, textColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor })
- };
- rowsAlt = new List<string>()
- {
- largeSpace + new string(new char[] { backgroundColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor }),
- largeSpace + new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor }),
- largeSpace + new string(new char[] { textColor, textColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor }),
- largeSpace + new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor }),
- largeSpace + new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, textColor, textColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor })
- };
- rowsNoGrav = new List<string>()
- {
- new string(new char[] { numberColor, numberColor, backgroundColor, backgroundColor, backgroundColor, numberColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, numberColor, numberColor, backgroundColor, backgroundColor, numberColor, numberColor, backgroundColor, backgroundColor, backgroundColor, numberColor, backgroundColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, backgroundColor, numberColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, backgroundColor, numberColor, backgroundColor, numberColor, numberColor, backgroundColor, numberColor, numberColor, backgroundColor, backgroundColor, numberColor, numberColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, backgroundColor, numberColor, backgroundColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, numberColor, backgroundColor, backgroundColor }),
- new string(new char[] { numberColor, backgroundColor, numberColor, backgroundColor, backgroundColor, numberColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, numberColor, numberColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, numberColor, backgroundColor, backgroundColor, backgroundColor })
- };
- rowsAcc= new List<string>()
- {
- new string(new char[] { backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor }),
- new string(new char[] { textColor, textColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor, backgroundColor, backgroundColor, textColor, backgroundColor }),
- new string(new char[] { textColor, backgroundColor, textColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, textColor, textColor, backgroundColor, backgroundColor, backgroundColor })
- };
- #endregion
- }
- /*
- ///WHAT'S CHANGED///
- - Fixed incorrect display of pitch
- - Fixed some variable name typos
- - This code can now display altitude (only in DEV branch!)
- - Removed character limit bypass
- - Clamped arccosine inputs to avoid NaN errors
- - Used string builder instead of string to help performance
- - Added in monospace support - v29
- - Added more color presets - v30
- - Added in a custom color method - v30
- - Fixed colorblind mode settings not updating the text and number colors - v31
- - Added automatic font selection - 31
- - [REVERTED] ~~Code now ignores programs on other grids connected via connectors - v32~~
- - Changed color defaults - v35
- - [REVERTED] ~~Added method to only consider grids that are the same as the program or are attached via rotor trick - v34~~ - v37
- */
Advertisement
Add Comment
Please, Sign In to add comment