Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jul 2, 2024
1 parent 15f344b commit 9b36011
Showing 1 changed file with 52 additions and 105 deletions.
157 changes: 52 additions & 105 deletions Layman's SafeZone/Data/Scripts/bob/bobzone_activemeasures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IMyPlayer> players;
private List<byte> spacket = new List<byte>();
private List<IMyCubeGrid> dirties = new List<IMyCubeGrid>();
public static HashSet<IMyCubeBlock> zoneblocks = new HashSet<IMyCubeBlock>();
//public static ConcurrentBag<IMyCubeGrid> dirties = new ConcurrentBag<IMyCubeGrid>();
public static Dictionary<long, long> zonelookup = new Dictionary<long, long>();

private Dictionary<long, bool> playerInZone = new Dictionary<long, bool>(); // 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)
Expand All @@ -76,6 +78,10 @@ public override void UpdateBeforeSimulation()

tick++;

//MyAPIGateway.Utilities.ShowNotification("PLAYER: " + MyAPIGateway.Session.Player.Identity.IdentityId.ToString(), 1);

//if (isHost)
//{
List<IMyPlayer> playerlist = new List<IMyPlayer>();
MyAPIGateway.Players.GetPlayers(playerlist);
players = new HashSet<IMyPlayer>(playerlist);
Expand All @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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;

Expand All @@ -229,83 +219,40 @@ 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();

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();
}
double renderdistance = (matrix.Translation - MyAPIGateway.Session.Camera.Position).Length();

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
}
if(renderdistance < 20*radius)
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);

private Vector3 ColorMaskToRgb(Vector3 colorMask)
{
return MyColorPickerConstants.HSVOffsetToHSV(colorMask).HSVtoColor();
}
//if (renderdistance < radius)
//MyAPIGateway.Utilities.ShowNotification("INSIDE " + ModBlock.GetOwnerFactionTag() + "'S BOBZONE", 1);

private void DrawRing(Vector3D center, double radius, int segments, Color color)
{
double angleStep = 2 * Math.PI / segments;
for (int i = 0; i < segments; i++)
{
double angle1 = i * angleStep;
double angle2 = (i + 1) * angleStep;
Vector3D start = center + new Vector3D(radius * Math.Cos(angle1), 0, radius * Math.Sin(angle1));
Vector3D end = center + new Vector3D(radius * Math.Cos(angle2), 0, radius * Math.Sin(angle2));
Vector4 colorVector = color.ToVector4();
MySimpleObjectDraw.DrawLine(start, end, MyStringId.GetOrCompute("Square"), ref colorVector, 1f);
}
}
}

public static class Vector3Extensions
{
public static Color ToColor(this Vector3 vector)
{
return new Color(vector.X, vector.Y, vector.Z);
}
}

}

0 comments on commit 9b36011

Please sign in to comment.