Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  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 = true;
  39.                 Entity.NeedsUpdate |= MyEntityUpdateEnum.EACH_10TH_FRAME;
  40.             }
  41.  
  42.             if (beacon != null && beacon.BlockDefinition.SubtypeId.Equals("NoFlyZoneSmall"))
  43.             {
  44.                 logicEnabled = true;
  45.                 Entity.NeedsUpdate |= MyEntityUpdateEnum.EACH_10TH_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.             try
  70.             {
  71.  
  72.                 //if (DateTime.Now - lastUpdate < TimeSpan.FromMilliseconds(250)) return;
  73.  
  74.                 if (!logicEnabled || beacon == null || !beacon.Enabled || !beacon.IsWorking || !beacon.IsFunctional) return;
  75.  
  76.                 List<IMyEntity> l = new List<IMyEntity>();
  77.  
  78.                 BoundingSphereD sphere = new BoundingSphereD((Entity as Sandbox.ModAPI.Ingame.IMyBeacon).GetPosition(), (Entity as Sandbox.ModAPI.Ingame.IMyBeacon).Radius);
  79.                 l = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere);
  80.  
  81.                 var parentGrid = beacon.CubeGrid;
  82.                 if (entity == null)
  83.                     entity = MyAPIGateway.Entities.GetEntityById(parentGrid.EntityId);
  84.  
  85.                 if (parentGrid != null)
  86.                 {
  87.                     int i = 0;
  88.  
  89.                     foreach (IMyEntity e in l)
  90.                     {
  91.                         IMyCubeGrid grid = (e as IMyCubeGrid);
  92.  
  93.                         if (grid != null)
  94.                         {
  95.                             List<IMySlimBlock> blocks = new List<IMySlimBlock>();
  96.                             grid.GetBlocks(blocks, b => b != null && b.FatBlock != null && (b.FatBlock as Sandbox.ModAPI.Ingame.IMyThrust) != null && b.FatBlock.IsWorking && b.FatBlock.IsFunctional &&
  97.                                 b.FatBlock.BlockDefinition.ToString().Contains("MyObjectBuilder_ThrustDefinition"));
  98.  
  99.                             foreach (IMySlimBlock b in blocks)
  100.                             {
  101.                                 if ((b.FatBlock as Sandbox.ModAPI.Ingame.IMyThrust).Enabled)
  102.                                 {
  103.                                     var damage = grid.GridSizeEnum.Equals(MyCubeSize.Large) ? 0.5f : 0.05f;
  104.                                     b.DecreaseMountLevel(damage, null, true);
  105.                                     b.ApplyAccumulatedDamage();
  106.  
  107.                                     (b.FatBlock as Sandbox.ModAPI.Ingame.IMyThrust).RequestEnable(false);
  108.                                 }
  109.                                 i++;
  110.                             }
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.             catch (Exception e)
  116.             {
  117.                 MyAPIGateway.Utilities.ShowMessage("NoFlyZone", "An error happened in the mod");
  118.             }
  119.             //todo: export i to custom info text
  120.             //lastUpdate = DateTime.Now;
  121.  
  122.         }
  123.  
  124.         public override void UpdateBeforeSimulation10()
  125.         {
  126.         }
  127.  
  128.         public override void UpdateBeforeSimulation100()
  129.         {
  130.  
  131.         }
  132.  
  133.         public override void UpdateOnceBeforeFrame()
  134.         {
  135.         }
  136.  
  137.         public override VRage.ObjectBuilders.MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
  138.         {
  139.             return _objectBuilder;
  140.         }
  141.  
  142.         private void log(string text)
  143.         {
  144.             if (logger == null)
  145.             {
  146.                 try
  147.                 {
  148.                     logger = MyAPIGateway.Utilities.WriteFileInLocalStorage(this.GetType().Name + "-" + timeofload + ".log", this.GetType());
  149.                 }
  150.                 catch (Exception)
  151.                 {
  152.                     MyAPIGateway.Utilities.ShowMessage("AICombatLib", "Could not open the log file:" + this.GetType().Name + "-" + timeofload + ".log");
  153.                     return;
  154.                 }
  155.             }
  156.  
  157.             String datum = DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
  158.             logger.WriteLine(datum + ": " + text);
  159.             logger.Flush();
  160.  
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement