Skip to content

Instantly share code, notes, and snippets.

@luvies
Created December 31, 2016 17:14
Show Gist options
  • Save luvies/ee1dd0d0b5e79fb8aa9650ff9661a594 to your computer and use it in GitHub Desktop.
Save luvies/ee1dd0d0b5e79fb8aa9650ff9661a594 to your computer and use it in GitHub Desktop.
/*-*/
// This file was derived from
// http://forum.keenswh.com/threads/guide-setting-up-visual-studio-for-programmable-block-scripting.7225319/
// Huge credit to them for getting the basic information I needed
/* Requires references to (<SE Install> = space engineers install)
* <SE Install>\Sandbox.Common.dll
* <SE Install>\Sandbox.Game.dll
* <SE Install>\VRage.Game.dll
* <SE Install>\VRage.Math.dll
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sandbox.Common;
// using Sandbox.Common.Components // I belive this has moved to VRage.Game.Components (VS will automatically find the missing references anyway)
using Sandbox.Common.ObjectBuilders;
using Sandbox.Definitions;
using Sandbox.Engine;
using Sandbox.ModAPI.Ingame;
// Some definitions are in VRage.Game.ModAPI.Ingame so beware
using Sandbox.ModAPI.Interfaces;
using Sandbox.Game;
namespace Scripts.SpaceAnchorInfoSample // change 'BaseProgram' to the name of the script itself to stop accidental cross-referencing
{
class Program : Sandbox.ModAPI.IMyGridProgram // class name needs to be 'Program' in order to satisfy constructor method
{
// These are members implemented using NotImplementedException
// in order to satisfy VS in extending the Sandbox.ModAPI.IMyGridProgram
// interface (which is where all programming block programs extend from)
public Action<string> Echo { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public TimeSpan ElapsedTime { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public IMyGridTerminalSystem GridTerminalSystem { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool HasMainMethod => throw new NotImplementedException();
public bool HasSaveMethod => throw new NotImplementedException();
public IMyProgrammableBlock Me { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public IMyGridProgramRuntimeInfo Runtime { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string Storage { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public void Save() => throw new NotImplementedException();
/* ===== Additional info =====
* public string IMyTerminalBlock.CustomData -> synced and saved custom data that can be edited via terminal
* Programs extend from
* Sandbox.ModAPI.IMyGridProgram
* Solar power output contained in
* IMyTerminalBlock.DetailedInfo
* Actions can be triggered via
* IMyTerminalBlock.ApplyAction(string action)
* Use 'Right Click -> Peek Definition' to see what you can do with an inferface (the documentation is lacking to say the least)
* The following is an extention class the IMyTerminalBlock interface
* Sandbox.ModAPI.Ingame.TerminalBlockExtentions
*/
/*-*/
// the name of the panels to fetch
// they only have to begin with this, the characters after are ignored
const string LcdPanelName = "LCD - Space Anchor Info";
// the EXACT name of the space anchor to show
// if it's not found the lcd's will just show
// the string in 'SpaceAnchorNotFound'
const string SpaceAnchorName = "Space Anchor - Info Fetch";
// the text to show when the space anchor cannot be found
const string SpaceAnchorNotFound = "Space Anchor not found";
// the format string to display on the lcd
// {0} being the current charge
// {1} being the maximum charge
// {2} being the percentage charged (between 0-1 by default)
// {3} being the localisation of 'MWh' (fetched from the Space Anchor itself)
// the actual string doesn't need all of them, however you cannot use any
// more unless you add them into the formatting function params
// because it's a format string, we can use item parameters for extra formatting
const string LcdFormatString = "The Space Anchor is {2:P2} charged"; // using :P2 on the items to format percent
// optional versions. only have one of the versions, including the above,
// uncommented, since it will cause an error
//const string LcdFormatString = "The Space Anchor is charged to {0}/{1} {3}";
//const string LcdFormatString = "Space Anchor charge: {0}/{1} {3} ({2:P2})";
public void Main(string argument)
{
IMyTerminalBlock spaceAnchor; // we are getting the exact name so only 1 block will match
// get the block
spaceAnchor = GridTerminalSystem.GetBlockWithName(SpaceAnchorName);
var panels = new List<IMyTextPanel>(); // create the list of panels
// load the panels that start with the LcdPanelName string into the list
GridTerminalSystem.GetBlocksOfType(panels, (block) => block.CustomName.StartsWith(LcdPanelName, StringComparison.InvariantCultureIgnoreCase));
string display;
if (spaceAnchor != null) // space anchor was found, so get the info and format it
{
string[] info = spaceAnchor.CustomData.Split('\n');
// info[0] contains the actual string displayed in the detailed info
// info[1] contains the current charge
// info[2] contains the maximum charge
// info[3] contains the localisation of 'MWh' that the config is set to
// so just format the current LcdFormatString with the data
float chrgCurrent = float.Parse(info[1]); // the current charge is a decimal, so parse it into a float
float chrgMax = float.Parse(info[2]); // the maximum charge is a decimal, so parse it into a float
display = string.Format(LcdFormatString, // the string to format
chrgCurrent, // current charge into {0}
chrgMax, // maximum charge into {1}
chrgCurrent / chrgMax, // percent charge into {2} (as number between 0-1)
info[3] // localisation of MWh
);
}
else // space anchor couldn't be found, so just display this
display = SpaceAnchorNotFound;
foreach (var panel in panels) // write the display text to every panel
panel.WritePublicText(display);
}
/*-*/
}
}
/*-*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment