diff --git a/Layman's SafeZone/Data/Scripts/bob/bobzone_activemeasures.cs b/Layman's SafeZone/Data/Scripts/bob/bobzone_activemeasures.cs index 6ef62b76..df593e91 100644 --- a/Layman's SafeZone/Data/Scripts/bob/bobzone_activemeasures.cs +++ b/Layman's SafeZone/Data/Scripts/bob/bobzone_activemeasures.cs @@ -46,27 +46,29 @@ public class Session : MySessionComponentBase public static double tock; public static Session Instance; + + public static float radius = 500f; public long tick = 0; private HashSet players; private List spacket = new List(); private List dirties = new List(); public static HashSet zoneblocks = new HashSet(); + //public static ConcurrentBag dirties = new ConcurrentBag(); public static Dictionary zonelookup = new Dictionary(); - private Dictionary playerInZone = new Dictionary(); // Track players in zone - public override void Init(MyObjectBuilder_SessionComponent sessionComponent) { MyAPIGateway.Session.DamageSystem.RegisterBeforeDamageHandler(9, ProcessDamage); + } - + public override void BeforeStart() { isHost = MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE || MyAPIGateway.Multiplayer.IsServer; isServer = MyAPIGateway.Utilities.IsDedicated; } - + public override void UpdateBeforeSimulation() { if (MyAPIGateway.Session == null) @@ -76,6 +78,10 @@ public override void UpdateBeforeSimulation() tick++; + //MyAPIGateway.Utilities.ShowNotification("PLAYER: " + MyAPIGateway.Session.Player.Identity.IdentityId.ToString(), 1); + + //if (isHost) + //{ List playerlist = new List(); MyAPIGateway.Players.GetPlayers(playerlist); players = new HashSet(playerlist); @@ -85,53 +91,39 @@ public override void UpdateBeforeSimulation() if (player == null) return; - bool isInAnyZone = false; - foreach (IMyCubeBlock zoneblock in zoneblocks) { if (player.Controller?.ControlledEntity?.Entity is IMyCharacter) { IMyCharacter character = player.Controller.ControlledEntity.Entity as IMyCharacter; double distance = (zoneblock.WorldMatrix.Translation - character.WorldMatrix.Translation).LengthSquared(); - if (distance < radius * radius) + if (distance < radius*radius) { - isInAnyZone = true; - if (!playerInZone.ContainsKey(player.Identity.IdentityId) || !playerInZone[player.Identity.IdentityId]) + if (MyIDModule.GetRelationPlayerBlock(zoneblock.OwnerId, player.Identity.IdentityId) == MyRelationsBetweenPlayerAndBlock.Enemies) { - playerInZone[player.Identity.IdentityId] = true; - var color = GetFactionColor(zoneblock.OwnerId); - var colorVector = color.ToVector4(); - //MyVisualScriptLogicProvider.SendChatMessageColored("You have entered the zone.", colorVector, zoneblock.CubeGrid.CustomName); + character.Physics.AddForce(MyPhysicsForceType.APPLY_WORLD_FORCE, Vector3D.Normalize(character.WorldMatrix.Translation - zoneblock.WorldMatrix.Translation) * 1000 * character.Physics.Mass, null, null, applyImmediately: true); } + MyVisualScriptLogicProvider.SetPlayersHydrogenLevel(player.Identity.IdentityId, 1f); + } } - } - - if (!isInAnyZone) - { - if (playerInZone.ContainsKey(player.Identity.IdentityId) && playerInZone[player.Identity.IdentityId]) + else if (player.Controller?.ControlledEntity?.Entity is IMyCubeBlock && MyIDModule.GetRelationPlayerBlock(zoneblock.OwnerId, player.Identity.IdentityId) == MyRelationsBetweenPlayerAndBlock.Enemies) { - playerInZone[player.Identity.IdentityId] = false; - //MyAPIGateway.Utilities.ShowNotification("You have left the zone.", 2000, MyFontEnum.Red); + IMyCubeBlock terminalBlock = player.Controller.ControlledEntity.Entity as IMyCubeBlock; + IMyCubeGrid grid = terminalBlock.CubeGrid; + var sphere = new BoundingSphereD(zoneblock.WorldMatrix.Translation, radius); + if (grid.WorldAABB.Intersects(ref sphere)) + { + grid.Physics.AddForce(MyPhysicsForceType.APPLY_WORLD_FORCE, Vector3D.Normalize(grid.Physics.CenterOfMassWorld - zoneblock.WorldMatrix.Translation) * 1000 * grid.Physics.Mass, null, null, applyImmediately: true); + } } + } }); + //} } - protected override void UnloadData() - { - // No direct way to unregister damage handlers in current SE API - // Ensure no references to prevent memory leaks or unintended behavior - // uhh yeah sure buddy - MyAPIGateway.Session.DamageSystem.RegisterBeforeDamageHandler(9, null); - players?.Clear(); - dirties?.Clear(); - zoneblocks?.Clear(); - zonelookup?.Clear(); - playerInZone?.Clear(); - } - - public void ProcessDamage(object target, ref MyDamageInformation info) + public void ProcessDamage(object target, ref MyDamageInformation info) { IMySlimBlock slim = target as IMySlimBlock; long idZone = 0; @@ -163,6 +155,7 @@ public void ProcessDamage(object target, ref MyDamageInformation info) if (player != null && MyIDModule.GetRelationPlayerBlock(idZone, player.Identity.IdentityId) == MyRelationsBetweenPlayerAndBlock.Enemies) { info.Amount = 0; + // MyAPIGateway.Utilities.ShowNotification("IS ENEMY. OWNER = " + idZone.ToString() + ", ATTACKER = " + player.Identity.IdentityId.ToString(), 600); } else { @@ -173,47 +166,44 @@ public void ProcessDamage(object target, ref MyDamageInformation info) if (MyIDModule.GetRelationPlayerBlock(idZone, block.OwnerId) == MyRelationsBetweenPlayerAndBlock.Enemies) { info.Amount = 0; + //MyAPIGateway.Utilities.ShowNotification("IS ENEMY GRINDER BLOCK. OWNER = " + idZone.ToString() + ", ATTACKER = " + block.OwnerId.ToString(), 600); } + //else + //MyAPIGateway.Utilities.ShowNotification("IS FRIENDLY GRINDER BLOCK", 600); } else if (tool != null) { if (MyIDModule.GetRelationPlayerBlock(idZone, tool.OwnerIdentityId) == MyRelationsBetweenPlayerAndBlock.Enemies) { info.Amount = 0; + //MyAPIGateway.Utilities.ShowNotification("IS ENEMY GRINDER TOOL. OWNER = " + idZone.ToString() + ", ATTACKER = " + tool.OwnerId.ToString(), 600); } + //else + //MyAPIGateway.Utilities.ShowNotification("IS FRIENDLY GRINDER TOOl", 600); } + //else + //MyAPIGateway.Utilities.ShowNotification("IS FRIENDLY", 600); } } else { info.Amount = 0; + // MyAPIGateway.Utilities.ShowNotification("IS WEAPON", 600); } - } - private Color GetFactionColor(long ownerId) - { - var faction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(ownerId); - if (faction != null) - { - var colorMask = faction.CustomColor; - return ColorMaskToRgb(colorMask).ToColor(); - } - return Color.White; // Default to white if no faction or color is found } - private Vector3 ColorMaskToRgb(Vector3 colorMask) - { - return MyColorPickerConstants.HSVOffsetToHSV(colorMask).HSVtoColor(); - } } - + [MyEntityComponentDescriptor(typeof(MyObjectBuilder_UpgradeModule), false, "bobzone")] public class bobzoneblock : MyGameLogicComponent { + public IMyUpgradeModule ModBlock { get; private set; } public IMyCubeGrid ModGrid; public string faction; public float radius = 50f; // defer to session value + public Color color = new Color(255, 255, 255, 10); private MatrixD matrix; private long tock = 0; @@ -229,44 +219,51 @@ public override void UpdateAfterSimulation() { if (ModBlock == null || !ModBlock.IsFunctional || ModBlock.MarkedForClose || ModBlock.Closed || ModBlock.MarkedForClose || !ModBlock.Enabled || !ModGrid.IsStatic) { + lock (Session.zoneblocks) { Session.zoneblocks.Remove(ModBlock); } - + return; } + //MyAPIGateway.Utilities.ShowNotification("ZONEBLOCK: " + ModBlock.OwnerId.ToString(), 1); + lock (Session.zoneblocks) { Session.zoneblocks.Add(ModBlock); } - + tock++; Vector3D storage = matrix.Up; matrix = ModBlock.WorldMatrix; - double rad = ((double)tock / 100) % 360 * Math.PI / 180; - matrix = MatrixD.CreateWorld(matrix.Translation, matrix.Up, matrix.Right * Math.Cos(rad) + matrix.Forward * Math.Sin(rad)); + double rad = ((double)tock/100) % 360 * Math.PI / 180; + matrix = MatrixD.CreateWorld(matrix.Translation, matrix.Up, matrix.Right * Math.Cos(rad) + matrix.Forward * Math.Sin(rad)); + if (!Session.isServer) + { + double renderdistance = (matrix.Translation - MyAPIGateway.Session.Camera.Position).Length(); + var factionColor = GetFactionColor(ModBlock.OwnerId); + if (renderdistance < 20*radius) + + DrawRing(matrix.Translation, radius, 32, factionColor); // Draw a ring with 32 segments + + //MySimpleObjectDraw.DrawTransparentSphere(ref matrix, radius, ref color, MySimpleObjectRasterizer.Solid, 20, MyStringId.GetOrCompute("SafeZoneShield_Material"), null, -1, -1, null, BlendTypeEnum.PostPP, 1); + //MySimpleObjectDraw.DrawTransparentSphere(ref matrix, radius.Value, ref drawColor, MySimpleObjectRasterizer.Solid, 35, shield_mat, null, -1, -1, null, BlendTypeEnum.PostPP, 1); + + //if (renderdistance < radius) + //MyAPIGateway.Utilities.ShowNotification("INSIDE " + ModBlock.GetOwnerFactionTag() + "'S BOBZONE", 1); + + } if (!Session.isServer) { double renderdistance = (matrix.Translation - MyAPIGateway.Session.Camera.Position).Length(); if (renderdistance < 20 * radius) { - var factionColor = GetFactionColor(ModBlock.OwnerId); - DrawRing(matrix.Translation, radius, 32, factionColor); // Draw a ring with 32 segments - } - } - } - - public override void Close() - { - lock (Session.zoneblocks) - { - Session.zoneblocks.Remove(ModBlock); + } } - base.Close(); } private Color GetFactionColor(long ownerId) @@ -307,5 +304,5 @@ public static Color ToColor(this Vector3 vector) return new Color(vector.X, vector.Y, vector.Z); } } +} -} \ No newline at end of file