klassekatze

BatteryBoss

Aug 6th, 2026
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 KB | None | 0 0
  1. // R e a d m e
  2. // -----------
  3. //
  4. // In this file you can include any instructions or other comments you want to have injected onto the
  5. // top of your final script. You can safely delete this file if you do not want any such comments.
  6. //
  7. //This program, running on station, can:
  8.        
  9.  
  10.  
  11.  
  12. //set tanks to stockpile true on dock and to false on undock.
  13. bool fill_tanks_on_dock = true;
  14. //set batteries to charge on dock w station and to discharge on undock.
  15. bool fill_batteries_on_dock = true;
  16.  
  17. bool disable_ejectors_on_dock = true;//imperfect failsafe for if i am dumbass and dock ship w running ejector+sorter to base.
  18.  
  19. //if there is lcd with dbgsurf in CD, it makes debug output. otherwise it no-ops, the lcd is not required
  20. IMyTextSurface dbgsurf = null;
  21. string dbglog = "";
  22. public void log(string s)
  23. {
  24.     if(dbgsurf != null)
  25.     {
  26.         dbglog += s + "\n";
  27.         dbgsurf.WriteText(dbglog);
  28.     }
  29. }
  30. public Program()
  31. {
  32.     connectors.Clear();
  33.     GridTerminalSystem.GetBlocksOfType(connectors, b => b.CubeGrid == Me.CubeGrid);
  34.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  35.  
  36.     var blox = new List<IMyTerminalBlock>();
  37.     GridTerminalSystem.GetBlocksOfType(blox);
  38.  
  39.     for(int i = 0; i < blox.Count; i++)
  40.     {
  41.         if(blox[i] is IMyTextSurface)
  42.         {
  43.             if (blox[i].CustomData.Contains("dbgsurf")) dbgsurf = ((IMyTextSurface)blox[i]);
  44.         }
  45.     }
  46.  
  47.     log("rdy");
  48.  
  49.  
  50. }
  51. public void Main(string argument, UpdateType updateSource)
  52. {
  53.     updateConnectors();
  54. }
  55.  
  56. List<IMyShipConnector> connectors = new List<IMyShipConnector>();
  57. public class ConnectorInfo
  58. {
  59.     public Program program = null;
  60.     bool lcon = false;
  61.     public IMyShipConnector connector = null;
  62.     public IMyShipConnector otherConnector = null;
  63.     public List<IMyTerminalBlock> connectedGridBlocks = new List<IMyTerminalBlock>();
  64.  
  65.     public bool upd()
  66.     {
  67.         bool con = false;
  68.         try
  69.         {
  70.  
  71.             con = connector.Status == MyShipConnectorStatus.Connected;
  72.         }
  73.         catch (Exception e)
  74.         {
  75.             program.log("e1 "+ e.Message);
  76.             try
  77.             {
  78.                 con = connector.OtherConnector != null;
  79.             }
  80.             catch (Exception e2)
  81.             {
  82.                 program.log("e2 "+ e2.Message);
  83.             }
  84.         }
  85.  
  86.         if (lcon != con)
  87.         {
  88.             lcon = con;
  89.             if (con)
  90.             {
  91.                 program.log("connected");
  92.                 otherConnector = null;
  93.                 try
  94.                 {
  95.                     otherConnector = connector.OtherConnector;
  96.                     if (otherConnector != null)
  97.                     {
  98.                         program.log("populating blist");
  99.                         connectedGridBlocks.Clear();
  100.                         program.GridTerminalSystem.GetBlocksOfType(connectedGridBlocks, x => x.CubeGrid == otherConnector.CubeGrid);
  101.                         //program.connectedGridBlocks[otherConnector] = b;
  102.                     }
  103.                 }catch(Exception e)
  104.                 {
  105.                     program.log("e3 "+e.Message);
  106.                 }
  107.             }
  108.             program.connect_change(con, this);
  109.             if (!con)
  110.             {
  111.                 otherConnector = null;
  112.                 connectedGridBlocks.Clear();
  113.                 program.log("clear blist");
  114.             }
  115.             return true;
  116.         }
  117.         else return false;
  118.     }
  119. }
  120. Dictionary<IMyShipConnector, ConnectorInfo> connectorState = new Dictionary<IMyShipConnector, ConnectorInfo>();
  121.  
  122. //Dictionary<IMyShipConnector, List<IMyTerminalBlock>> connectedGridBlocks = new Dictionary<IMyShipConnector, List<IMyTerminalBlock>>();
  123.  
  124. public void connect_change(bool connected, ConnectorInfo state)
  125. {
  126.     log("connect_change "+ connected);
  127.     if (connected)
  128.     {
  129.         for(int i = 0; i < state.connectedGridBlocks.Count; i++)
  130.         {
  131.             var b = state.connectedGridBlocks[i];
  132.             if(fill_batteries_on_dock && b is IMyBatteryBlock)
  133.             {
  134.                 IMyBatteryBlock bat = (IMyBatteryBlock)b;
  135.                 bat.ChargeMode = ChargeMode.Recharge;
  136.                 log(bat.CustomName+" rech");
  137.             }
  138.             else if (fill_tanks_on_dock && b is IMyGasTank)
  139.             {
  140.                 ((IMyGasTank)b).Stockpile = true;
  141.             }
  142.             else if(disable_ejectors_on_dock && b is IMyShipConnector)
  143.             {
  144.                 IMyShipConnector cnct = (IMyShipConnector)b;
  145.                 if(cnct.ThrowOut)
  146.                 {
  147.                     cnct.Enabled = false;
  148.                 }
  149.             }
  150.         }
  151.     }else
  152.     {
  153.         for (int i = 0; i < state.connectedGridBlocks.Count; i++)
  154.         {
  155.             var b = state.connectedGridBlocks[i];
  156.             if (fill_batteries_on_dock && b is IMyBatteryBlock)
  157.             {
  158.                 IMyBatteryBlock bat = (IMyBatteryBlock)b;
  159.                 bat.ChargeMode = ChargeMode.Discharge;
  160.                 log(bat.CustomName + " disch");
  161.             }else if(fill_tanks_on_dock && b is IMyGasTank)
  162.             {
  163.                 ((IMyGasTank)b).Stockpile = false;
  164.             }
  165.         }
  166.     }
  167. }
  168. public bool updateConnectors()
  169. {
  170.     bool evnt = false;
  171.     foreach (var c in connectors)
  172.     {
  173.         ConnectorInfo v = null;
  174.         connectorState.TryGetValue(c, out v);
  175.         if (v == null)
  176.         {
  177.             v = new ConnectorInfo();
  178.             v.program = this;
  179.             v.connector = c;
  180.             connectorState[c] = v;
  181.         }
  182.         evnt = v.upd() || evnt;
  183.     }
  184.     return evnt;
  185. }
  186.  
Add Comment
Please, Sign In to add comment