SHARE
TWEET

Untitled

a guest Nov 12th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using VRage.ModAPI;
  5. using VRage.Game;
  6. using VRage.Game.Components;
  7. using VRageMath;
  8. using VRage.Game.ModAPI;
  9. using Sandbox.ModAPI;
  10.  
  11. namespace NoFlyZone
  12. {
  13.     //[MyEntityComponentDescriptor(typeof(Sandbox.Common.ObjectBuilders.MyObjectBuilder_Beacon))]
  14.     [MyEntityComponentDescriptor(typeof(Sandbox.Common.ObjectBuilders.MyObjectBuilder_Beacon), true, new string[] { "Beacon", "NoFlyZoneLarge", "NoFlyZoneSmall" })]
  15.     public class NoFlyZone : MyGameLogicComponent
  16.     {
  17.  
  18.         private VRage.ObjectBuilders.MyObjectBuilder_EntityBase _objectBuilder;
  19.         private DateTime lastUpdate = DateTime.MinValue;
  20.         private Sandbox.ModAPI.Ingame.IMyBeacon beacon;
  21.         private IMyEntity entity;
  22.         private System.IO.TextWriter logger = null;
  23.         private String timeofload = "" + DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day + " " + DateTime.Now.Hour + "." + DateTime.Now.Minute + "." + DateTime.Now.Second;
  24.         private bool logicEnabled = false;
  25.         public override void Close()
  26.         {
  27.  
  28.         }
  29.  
  30.         public override void Init(VRage.ObjectBuilders.MyObjectBuilder_EntityBase objectBuilder)
  31.         {
  32.             _objectBuilder = objectBuilder;
  33.  
  34.             beacon = (Entity as Sandbox.ModAPI.Ingame.IMyBeacon);
  35.  
  36.             if (beacon != null && beacon.BlockDefinition.SubtypeId.Equals("NoFlyZoneLarge"))
  37.             {
  38.                 logicEnabled = false;
  39.                 Entity.NeedsUpdate |= MyEntityUpdateEnum.EACH_100TH_FRAME;
  40.             }
  41.  
  42.             if (beacon != null && beacon.BlockDefinition.SubtypeId.Equals("NoFlyZoneSmall"))
  43.             {
  44.                 logicEnabled = true;
  45.                 Entity.NeedsUpdate |= MyEntityUpdateEnum.EACH_100TH_FRAME;
  46.             }
  47.  
  48.  
  49.         }
  50.  
  51.         public override void MarkForClose()
  52.         {
  53.         }
  54.  
  55.         public override void UpdateAfterSimulation()
  56.         {
  57.         }
  58.  
  59.         public override void UpdateBeforeSimulation()
  60.         {
  61.         }
  62.  
  63.         public override void UpdateAfterSimulation100()
  64.         {
  65.         }
  66.  
  67.         public override void UpdateAfterSimulation10()
  68.         {
  69.            
  70.             try
  71.             {
  72.  
  73.                 //if (DateTime.Now - lastUpdate < TimeSpan.FromMilliseconds(250)) return;
  74.  
  75.                 if (!logicEnabled || beacon == null || !beacon.Enabled || !beacon.IsWorking || !beacon.IsFunctional) return;
  76.  
  77.                 List<IMyEntity> l = new List<IMyEntity>();
  78.  
  79.                 BoundingSphereD sphere = new BoundingSphereD((Entity as Sandbox.ModAPI.Ingame.IMyBeacon).GetPosition(), (Entity as Sandbox.ModAPI.Ingame.IMyBeacon).Radius);
  80.                 l = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere);
  81.  
  82.                 var parentGrid = beacon.CubeGrid;
  83.                 if (entity == null)
  84.                     entity = MyAPIGateway.Entities.GetEntityById(parentGrid.EntityId);
  85.  
  86.                 if (parentGrid != null)
  87.                 {
  88.                     int i = 0;
  89.                    
  90.                     foreach (IMyEntity e in l)
  91.                     {
  92.                         MyAPIGateway.Utilities.ShowMessage("Thor", "We made it pa!");
  93.                         IMyCubeGrid grid = (e as IMyCubeGrid);
  94.  
  95.                         if (grid != null)
  96.                         {
  97.                             List<IMySlimBlock> blocks = new List<IMySlimBlock>();
  98.                             grid.GetBlocks(blocks, b => b != null && b.FatBlock != null && (b.FatBlock as Sandbox.ModAPI.Ingame.IMyThrust) != null && b.FatBlock.IsWorking && b.FatBlock.IsFunctional &&
  99.                                 b.FatBlock.BlockDefinition.ToString().Contains("MyObjectBuilder_ThrustDefinition"));
  100.                             MyAPIGateway.Utilities.ShowMessage("Thor", "We made it pa!");
  101.                             foreach (IMySlimBlock b in blocks)
  102.                             {
  103.                                 if ((b.FatBlock as Sandbox.ModAPI.Ingame.IMyThrust).Enabled == true)
  104.                                 {
  105.                                     var damage = grid.GridSizeEnum.Equals(MyCubeSize.Large) ? 0.5f : 0.05f;
  106.                                     b.DecreaseMountLevel(damage, null, true);
  107.                                     b.ApplyAccumulatedDamage();
  108.  
  109.                                     (b.FatBlock as Sandbox.ModAPI.Ingame.IMyThrust).Enabled = false;
  110.                                 }
  111.                                 i++;
  112.                             }
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             catch (Exception e)
  118.             {
  119.                 MyAPIGateway.Utilities.ShowMessage("NoFlyZone", "An error happened in the mod:" + e);
  120.             }
  121.             //todo: export i to custom info text
  122.             //lastUpdate = DateTime.Now;
  123.  
  124.         }
  125.  
  126.         public override void UpdateBeforeSimulation10()
  127.         {
  128.         }
  129.  
  130.         public override void UpdateBeforeSimulation100()
  131.         {
  132.  
  133.         }
  134.  
  135.         public override void UpdateOnceBeforeFrame()
  136.         {
  137.         }
  138.  
  139.         public override VRage.ObjectBuilders.MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
  140.         {
  141.             return _objectBuilder;
  142.         }
  143.  
  144.         private void log(string text)
  145.         {
  146.             if (logger == null)
  147.             {
  148.                 try
  149.                 {
  150.                     logger = MyAPIGateway.Utilities.WriteFileInLocalStorage(this.GetType().Name + "-" + timeofload + ".log", this.GetType());
  151.                 }
  152.                 catch (Exception)
  153.                 {
  154.                     MyAPIGateway.Utilities.ShowMessage("AICombatLib", "Could not open the log file:" + this.GetType().Name + "-" + timeofload + ".log");
  155.                     return;
  156.                 }
  157.             }
  158.  
  159.             String datum = DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
  160.             logger.WriteLine(datum + ": " + text);
  161.             logger.Flush();
  162.  
  163.         }
  164.     }
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top