Advertisement
Phoenix84

lcd

Feb 7th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.60 KB | None | 0 0
  1.         public static void RefreshLCDs(this IMyFunctionalBlock ftl)
  2.         {
  3.             var objBuilderType = new MyObjectBuilderType(typeof(MyObjectBuilder_TextPanel));
  4.             var lcdblocks = ftl.GetGroupedBlocks(objBuilderType);
  5.             var ftld = ftl.GetFTLData();
  6.  
  7.             if (lcdblocks.Count == 0)
  8.             {
  9.                 var list = new List<IMySlimBlock>();
  10.  
  11.                 // Get all the lcdblocks on the entity
  12.                 if (ftl.GetTopMostParent() is Sandbox.ModAPI.Ingame.IMyCubeGrid)
  13.                 {
  14.                     (ftl.GetTopMostParent() as Sandbox.ModAPI.IMyCubeGrid).GetBlocks(list,
  15.                         (x) => x.FatBlock != null && x.FatBlock.BlockDefinition.TypeId == objBuilderType);
  16.                 }
  17.  
  18.                 // Search all of them finding the best one
  19.                 foreach (var entity in list)
  20.                 {
  21.                     var lcdblock = entity.FatBlock as Sandbox.ModAPI.Ingame.IMyTextPanel;
  22.  
  23.                     if (lcdblock == null || !lcdblock.IsFunctional)
  24.                         continue;
  25.  
  26.                     //Logger.Instance.LogDebug("Found LCD: " + lcdblock.DisplayNameText);
  27.  
  28.                     if (!string.IsNullOrEmpty(lcdblock.DisplayNameText) &&
  29.                         (lcdblock.DisplayNameText.ToUpperInvariant().Contains("FTL") ||
  30.                             lcdblock.GetPublicTitle().ToUpperInvariant().Contains("FTL") ||
  31.                             lcdblock.GetPrivateTitle().ToUpperInvariant().Contains("FTL")))
  32.                     {
  33.                         // If a lcdblock contains the text FTL, always use it
  34.                         lcdblocks.Add(lcdblock as IMyTerminalBlock);
  35.                         break;
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             if (lcdblocks.Count == 0)
  41.                 return;
  42.  
  43.             // TODO: have different LCDs display different information
  44.             StringBuilder LCDText = new StringBuilder(60);
  45.             StringBuilder LCDTextUpgrade = new StringBuilder(80);
  46.  
  47.             // Calculate distance
  48.             double distance = (ftld.jumpDest - ftl.GetTopMostParent().PositionComp.WorldMatrix.Translation).Length();
  49.             string distunits = "m";
  50.             double range = ftld.baseRange * ftld.rangeFactor;
  51.             string rangeunits = "m";
  52.             string status = ftld.jumpState.ToString();
  53.  
  54.             if (!ftl.IsFunctional)
  55.                 status = "Offline";
  56.             if ((ftld.flags & JumpFlags.Disabled) == JumpFlags.Disabled)
  57.                 status = "Disabled";
  58.  
  59.             CalculateUnits(ref distance, ref distunits);
  60.             CalculateUnits(ref range, ref rangeunits);
  61.  
  62.             LCDText.Append(string.Format("FTL Status: {0}\n", status));
  63.             LCDTextUpgrade.Append(string.Format("Upgrade Modules:\n"));
  64.  
  65.             //Logger.Instance.Active = false;
  66.             if (ftl.IsFunctional && !((ftld.flags & JumpFlags.Disabled) == JumpFlags.Disabled))
  67.             {
  68.                 LCDText.Append(string.Format("Power: {0,16:F2} MW\n", ftl.GetPowerWanted()));
  69.                 //LCDText.Append(string.Format("Power: {0,21:P0}\n", (MyAPIGateway.Session.CreativeMode ? 1.0 : GetTotalFTLPower(m_ftl.GetFTLDrives()) / m_ftl.GetPowerPercentage())));
  70.                 //LCDText.Append(string.Format("Power Allocated: {0:F0}/{1:P0}\n", GetTotalFTLPower(m_ftl.GetFTLDrives()) * 100, m_ftl.GetPowerPercentage()));
  71.                 LCDText.Append(string.Format("Distance: {0,13:N0} {1,-2}\n", distance, distunits));
  72.                 LCDText.Append(string.Format("Mass: {0,15:N0} kg\n", ftld.totalMass));
  73.  
  74.                 bool doesCollide = false;
  75.  
  76.                 // Do we collide?
  77.                 if ((ftld.flags & JumpFlags.ShowCollision) == JumpFlags.ShowCollision)
  78.                 {
  79.                     VRageMath.BoundingSphereD jumpSphere;
  80.                     jumpSphere = new VRageMath.BoundingSphereD(ftld.jumpDest, ftl.GetTopMostParent().PositionComp.LocalVolume.Radius);
  81.                     List<IMyEntity> entities = MyAPIGateway.Entities.GetEntitiesInSphere(ref jumpSphere);
  82.                     VRageMath.Vector3 lastPos = new VRageMath.Vector3();
  83.  
  84.                     if (MyAPIGateway.Entities.IsInsideVoxel(
  85.                         new VRageMath.Vector3(jumpSphere.Center.X, jumpSphere.Center.Y, jumpSphere.Center.Z),
  86.                         VRageMath.Vector3D.Zero,
  87.                         out lastPos))
  88.                     {
  89.                         doesCollide = true;
  90.                     }
  91.  
  92.                     if (!doesCollide)
  93.                     {
  94.                         foreach (var entity in entities)
  95.                         {
  96.                             if (entity is Sandbox.ModAPI.IMyCubeGrid && entity.GetTopMostParent().EntityId != ftl.GetTopMostParent().EntityId)
  97.                             {
  98.                                 doesCollide = true;
  99.                                 break;
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.  
  105.                 if (ftld.jumpState == JumpState.Spooling)
  106.                     LCDText.Append(string.Format("Jumping in {0:F0}s\n", (ftld.jumpTime - DateTime.Now).TotalSeconds));
  107.                 else if (ftld.jumpState == JumpState.Cooldown)
  108.                     LCDText.Append(string.Format("Cooldown for {0:F0}s\n", (ftld.resetTime - DateTime.Now).TotalSeconds));
  109.                 else
  110.                     LCDText.Append("\n");
  111.  
  112.                 if (IsInhibited(ftld.jumpDest) || IsInhibited(ftl.GetPosition()))
  113.                     LCDText.Append("\nDestination:\nX = ?\nY = ?\nZ = ?\n");
  114.                 else
  115.                     LCDText.Append(string.Format("\nDestination{3}:\nX = {0:N0}\nY = {1:N0}\nZ = {2:N0}\n",
  116.                         ftld.jumpDest.X, ftld.jumpDest.Y, ftld.jumpDest.Z, doesCollide ? " (Warning!)" : string.Empty));
  117.  
  118.                 var capblocks = ftl.GetCapacitors();
  119.                 int totalcaps = 0;
  120.  
  121.                 foreach (var cap in capblocks)
  122.                 {
  123.                     if (cap.HasCapacityRemaining && cap.IsWorking && cap.IsFunctional)
  124.                         totalcaps++;
  125.                 }
  126.  
  127.                 LCDTextUpgrade.Append(string.Format("Max Range: {0,10:N0} {1,-2}\n", range, rangeunits));
  128.                 LCDTextUpgrade.Append(string.Format("Max Accuracy:{0,12:P0}\n", ftld.accuracyFactor));
  129.                 LCDTextUpgrade.Append(string.Format("Spool Efficiency:{0,8:P0}\n", ftld.spoolFactor));
  130.                 LCDTextUpgrade.Append(string.Format("Power Efficiency:{0,8:P0}\n", ftld.powerFactor));
  131.                 LCDTextUpgrade.Append(string.Format("Working Capacitors: {0}\r\n", totalcaps));
  132.             }
  133.             else
  134.             {
  135.                 LCDTextUpgrade.Append(string.Format("FTL Offline\n"));
  136.             }
  137.             Logger.Instance.Active = true;
  138.  
  139.             foreach (var entity in lcdblocks)
  140.             {
  141.                 Sandbox.ModAPI.Ingame.IMyTextPanel lcdblock = entity as Sandbox.ModAPI.Ingame.IMyTextPanel;
  142.  
  143.                 //if (Globals.Debug)
  144.                 //    Sandbox.ModAPI.MyAPIGateway.Utilities.ShowNotification("Found LCD: " + lcdblock.DisplayNameText, 3500);
  145.                 var existingText = lcdblock.GetPublicText();
  146.                 var clearText = "                    \n                    \n                    \n                    \n                    \n                    \n                    \n                    ";
  147.  
  148.                 // For network optimization, don't do anything if the text is the same
  149.                 if (existingText == LCDText.ToString())
  150.                     continue;
  151.  
  152.                 if (!ftl.IsBlockAccessible(entity))
  153.                 {
  154.                     var accessDeniedText = "FTL Status:         \nAccess Denied";
  155.  
  156.                     if (lcdblock.CustomName.ToUpperInvariant().Contains("UPGRADES"))
  157.                         accessDeniedText = "Upgrade Modules:    \nAccess Denied";
  158.  
  159.                     if (existingText != accessDeniedText)
  160.                     {
  161.                         // The spaces prevent the ghosting that happens when updating text
  162.                         lcdblock.WritePublicText(string.Empty);
  163.                         lcdblock.WritePublicText(clearText);
  164.                         lcdblock.WritePublicText(accessDeniedText);
  165.                     }
  166.                     continue;
  167.                 }
  168.  
  169.                 // Update LCD text
  170.                 if (lcdblock.CustomName.ToUpperInvariant().Contains("UPGRADE") ||
  171.                     lcdblock.GetPublicTitle().ToUpperInvariant().Contains("UPGRADE") ||
  172.                     lcdblock.GetPrivateTitle().ToUpperInvariant().Contains("UPGRADE"))
  173.                 {
  174.                     if (existingText != LCDTextUpgrade.ToString())
  175.                     {
  176.                         lcdblock.WritePublicText(string.Empty);
  177.                         lcdblock.WritePublicText(clearText);
  178.                         lcdblock.WritePublicText(LCDTextUpgrade.ToString());
  179.                     }
  180.                     else
  181.                     {
  182.                         continue;
  183.                     }
  184.                 }
  185.                 else
  186.                 {
  187.                     if (existingText != LCDText.ToString())
  188.                     {
  189.                         lcdblock.WritePublicText(string.Empty);
  190.                         lcdblock.WritePublicText(clearText);
  191.                         lcdblock.WritePublicText(LCDText.ToString());
  192.                     }
  193.                     else
  194.                     {
  195.                         continue;
  196.                     }
  197.                 }
  198.  
  199.                 if (FTLAdmin.Configuration.FixedFontSize)
  200.                 {
  201.                     float fontsize = 2.0f;          // Default to a more conservative 2.0, in case there are modded blocks
  202.  
  203.                     // Set font size depending on the block (only handles stock blocks)
  204.                     switch (lcdblock.BlockDefinition.SubtypeId)
  205.                     {
  206.                         case "LargeLCDPanelWide":
  207.                         case "SmallLCDPanelWide":
  208.                             fontsize = 3.5f;
  209.                             break;
  210.                         case "LargeLCDPanel":
  211.                         case "SmallLCDPanel":
  212.                             fontsize = 1.7f;
  213.                             break;
  214.                         case "LargeTextPanel":
  215.                         case "SmallTextPanel":
  216.                             fontsize = 1.7f;
  217.                             break;
  218.                     }
  219.  
  220.                     lcdblock.SetValueFloat("FontSize", fontsize);
  221.                 }
  222.                 lcdblock.ShowPublicTextOnScreen();
  223.             }
  224.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement