Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // R e a d m e
- // -----------
- //
- // In this file you can include any instructions or other comments you want to have injected onto the
- // top of your final script. You can safely delete this file if you do not want any such comments.
- //
- //This program, running on station, can:
- //set tanks to stockpile true on dock and to false on undock.
- bool fill_tanks_on_dock = true;
- //set batteries to charge on dock w station and to discharge on undock.
- bool fill_batteries_on_dock = true;
- bool disable_ejectors_on_dock = true;//imperfect failsafe for if i am dumbass and dock ship w running ejector+sorter to base.
- //if there is lcd with dbgsurf in CD, it makes debug output. otherwise it no-ops, the lcd is not required
- IMyTextSurface dbgsurf = null;
- string dbglog = "";
- public void log(string s)
- {
- if(dbgsurf != null)
- {
- dbglog += s + "\n";
- dbgsurf.WriteText(dbglog);
- }
- }
- public Program()
- {
- connectors.Clear();
- GridTerminalSystem.GetBlocksOfType(connectors, b => b.CubeGrid == Me.CubeGrid);
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- var blox = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType(blox);
- for(int i = 0; i < blox.Count; i++)
- {
- if(blox[i] is IMyTextSurface)
- {
- if (blox[i].CustomData.Contains("dbgsurf")) dbgsurf = ((IMyTextSurface)blox[i]);
- }
- }
- log("rdy");
- }
- public void Main(string argument, UpdateType updateSource)
- {
- updateConnectors();
- }
- List<IMyShipConnector> connectors = new List<IMyShipConnector>();
- public class ConnectorInfo
- {
- public Program program = null;
- bool lcon = false;
- public IMyShipConnector connector = null;
- public IMyShipConnector otherConnector = null;
- public List<IMyTerminalBlock> connectedGridBlocks = new List<IMyTerminalBlock>();
- public bool upd()
- {
- bool con = false;
- try
- {
- con = connector.Status == MyShipConnectorStatus.Connected;
- }
- catch (Exception e)
- {
- program.log("e1 "+ e.Message);
- try
- {
- con = connector.OtherConnector != null;
- }
- catch (Exception e2)
- {
- program.log("e2 "+ e2.Message);
- }
- }
- if (lcon != con)
- {
- lcon = con;
- if (con)
- {
- program.log("connected");
- otherConnector = null;
- try
- {
- otherConnector = connector.OtherConnector;
- if (otherConnector != null)
- {
- program.log("populating blist");
- connectedGridBlocks.Clear();
- program.GridTerminalSystem.GetBlocksOfType(connectedGridBlocks, x => x.CubeGrid == otherConnector.CubeGrid);
- //program.connectedGridBlocks[otherConnector] = b;
- }
- }catch(Exception e)
- {
- program.log("e3 "+e.Message);
- }
- }
- program.connect_change(con, this);
- if (!con)
- {
- otherConnector = null;
- connectedGridBlocks.Clear();
- program.log("clear blist");
- }
- return true;
- }
- else return false;
- }
- }
- Dictionary<IMyShipConnector, ConnectorInfo> connectorState = new Dictionary<IMyShipConnector, ConnectorInfo>();
- //Dictionary<IMyShipConnector, List<IMyTerminalBlock>> connectedGridBlocks = new Dictionary<IMyShipConnector, List<IMyTerminalBlock>>();
- public void connect_change(bool connected, ConnectorInfo state)
- {
- log("connect_change "+ connected);
- if (connected)
- {
- for(int i = 0; i < state.connectedGridBlocks.Count; i++)
- {
- var b = state.connectedGridBlocks[i];
- if(fill_batteries_on_dock && b is IMyBatteryBlock)
- {
- IMyBatteryBlock bat = (IMyBatteryBlock)b;
- bat.ChargeMode = ChargeMode.Recharge;
- log(bat.CustomName+" rech");
- }
- else if (fill_tanks_on_dock && b is IMyGasTank)
- {
- ((IMyGasTank)b).Stockpile = true;
- }
- else if(disable_ejectors_on_dock && b is IMyShipConnector)
- {
- IMyShipConnector cnct = (IMyShipConnector)b;
- if(cnct.ThrowOut)
- {
- cnct.Enabled = false;
- }
- }
- }
- }else
- {
- for (int i = 0; i < state.connectedGridBlocks.Count; i++)
- {
- var b = state.connectedGridBlocks[i];
- if (fill_batteries_on_dock && b is IMyBatteryBlock)
- {
- IMyBatteryBlock bat = (IMyBatteryBlock)b;
- bat.ChargeMode = ChargeMode.Discharge;
- log(bat.CustomName + " disch");
- }else if(fill_tanks_on_dock && b is IMyGasTank)
- {
- ((IMyGasTank)b).Stockpile = false;
- }
- }
- }
- }
- public bool updateConnectors()
- {
- bool evnt = false;
- foreach (var c in connectors)
- {
- ConnectorInfo v = null;
- connectorState.TryGetValue(c, out v);
- if (v == null)
- {
- v = new ConnectorInfo();
- v.program = this;
- v.connector = c;
- connectorState[c] = v;
- }
- evnt = v.upd() || evnt;
- }
- return evnt;
- }
Add Comment
Please, Sign In to add comment