Advertisement
klassekatze

connector change event

Feb 7th, 2025 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. static List<IMyShipConnector> connectors = new List<IMyShipConnector>();//initially populated as usual during typical setup
  2.  
  3. class ConnectorInfo
  4. {
  5.     bool lcon = false;
  6.     public IMyShipConnector connector = null;
  7.     public IMyShipConnector otherConnector = null;
  8.  
  9.     public bool sortConnected = false;
  10.     //public bool blockConnected = false;
  11.  
  12.     public bool upd()
  13.     {
  14.         var con = connector.Status == MyShipConnectorStatus.Connected;
  15.         var o = connector.OtherConnector;
  16.         if (lcon != con)
  17.         {
  18.             lcon = con;
  19.             if (!con) o = null;
  20.             otherConnector = o;
  21.             sortConnected = false;
  22.             if (otherConnector != null)
  23.             {
  24.                 var n = connector.CustomName;
  25.                 var n2 = otherConnector.CustomName;
  26.                 if (n.Contains(nosort) || n.Contains(nosort2) ||
  27.                 n2.Contains(nosort) || n2.Contains(nosort2))
  28.                 {
  29.                     sortConnected = false;
  30.                 }
  31.                 else sortConnected = true;
  32.             }
  33.             return true;
  34.         }
  35.         else return false;
  36.     }
  37.  
  38. }
  39.  
  40. Dictionary<IMyShipConnector, ConnectorInfo> connectorState = new Dictionary<IMyShipConnector, ConnectorInfo>();
  41. public bool updateConnectors()
  42. {
  43.     bool evnt = false;
  44.     foreach (var c in connectors)
  45.     {
  46.         ConnectorInfo v = null;
  47.         connectorState.TryGetValue(c, out v);
  48.         if (v == null)
  49.         {
  50.             v = new ConnectorInfo();
  51.             v.connector = c;
  52.             connectorState[c] = v;
  53.         }
  54.         evnt = v.upd() || evnt;
  55.     }
  56.     return evnt;
  57. }
  58.  
  59. bool cnctE = false;
  60. public void connectEvent2()
  61. {
  62.     try
  63.     {
  64.         bool evnt = updateConnectors();
  65.  
  66.         if (evnt)
  67.         {
  68.  
  69.             log("connector change");
  70.             List<IMyProgrammableBlock> pgms = new List<IMyProgrammableBlock>();
  71.             GridTerminalSystem.GetBlocksOfType(pgms, b => b != Me && b.HasPlayerAccess(Me.OwnerId) && b.CustomData.StartsWith("KTZINV") && b.IsWorking);
  72.             bool awake = true;
  73.             if (pgms.Count > 0)
  74.             {
  75.                 bool stati = Me.CubeGrid.IsStatic;
  76.                 foreach (var c in pgms)
  77.                 {
  78.                     if (c.CubeGrid.IsStatic && !stati)
  79.                     {
  80.                         awake = false;
  81.                         break;
  82.                     }
  83.                 }
  84.                 if (awake)
  85.                 {
  86.                     foreach (var c in pgms)
  87.                     {
  88.                         if (c.EntityId < Me.EntityId && (!stati || c.CubeGrid.IsStatic))
  89.                         {
  90.                             awake = false;
  91.                             break;
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.             SLEEPING = !awake;
  97.             if (SLEEPING)
  98.             {
  99.                 gInv.lastStatus = "Sleeping while a connected KTZInv runs.";
  100.                 if (statusLog != null) statusLog.WriteText(gInv.lastStatus);
  101.                 return;
  102.             }
  103.  
  104.             recalcInvBlocks();
  105.         }
  106.     }
  107.     catch (Exception e)//because torch or something i forget
  108.     {
  109.         if (!cnctE)
  110.         {
  111.             cnctE = true;
  112.             log("Exception: " + e.ToString());
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement