From d1f2f9996b14d02a224a3cfc1c223f156f171c0d Mon Sep 17 00:00:00 2001 From: InvalidArgument3 Date: Mon, 17 Jun 2024 23:00:31 -0500 Subject: [PATCH 1/2] command to add roid areas --- .../DynamicAsteroids/AsteroidSettings.cs | 24 +++++++++ .../Scripts/DynamicAsteroids/MainSession.cs | 51 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs index 60474c99..f215dd3d 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs @@ -5,6 +5,7 @@ using Sandbox.ModAPI; using VRageMath; using System.Collections.Generic; +using System.Linq; namespace DynamicAsteroids { @@ -445,6 +446,28 @@ private static int[] ReadIntArray(string value) } return array; } + + public static void AddSpawnableArea(string name, Vector3D center, double radius) + { + ValidSpawnLocations.Add(new SpawnableArea + { + Name = name, + CenterPosition = center, + Radius = radius + }); + SaveSettings(); + } + + public static void RemoveSpawnableArea(string name) + { + var area = ValidSpawnLocations.FirstOrDefault(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + if (area != null) + { + ValidSpawnLocations.Remove(area); + SaveSettings(); + } + } + } public class SpawnableArea @@ -463,5 +486,6 @@ public Vector3D VelocityAtPoint(Vector3D point) { return (point - CenterPosition).Normalized() * AsteroidSettings.AsteroidVelocityBase; } + } } diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs index 214828d0..1eef5292 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs @@ -42,6 +42,7 @@ public override void LoadData() } MyAPIGateway.Multiplayer.RegisterMessageHandler(32000, OnMessageReceived); + MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; } catch (Exception ex) { @@ -66,6 +67,7 @@ protected override void UnloadData() AsteroidSettings.SaveSettings(); // Save settings to the config file MyAPIGateway.Multiplayer.UnregisterMessageHandler(32000, OnMessageReceived); + MyAPIGateway.Utilities.MessageEntered -= OnMessageEntered; } catch (Exception ex) { @@ -76,6 +78,55 @@ protected override void UnloadData() I = null; } + private void OnMessageEntered(string messageText, ref bool sendToOthers) + { + if (messageText.StartsWith("/dynamicasteroids") || messageText.StartsWith("/dn")) + { + var args = messageText.Split(' '); + if (args.Length > 1) + { + switch (args[1].ToLower()) + { + case "createspawnarea": + double radius; + if (args.Length == 3 && double.TryParse(args[2], out radius)) + { + CreateSpawnArea(radius); + sendToOthers = false; + } + break; + + case "removespawnarea": + if (args.Length == 3) + { + RemoveSpawnArea(args[2]); + sendToOthers = false; + } + break; + } + } + } + } + + private void CreateSpawnArea(double radius) + { + var player = MyAPIGateway.Session.Player; + if (player == null) return; + + var position = player.GetPosition(); + var name = $"Area_{position.GetHashCode()}"; + AsteroidSettings.AddSpawnableArea(name, position, radius); + Log.Info($"Created spawn area '{name}' at {position} with radius {radius}"); + MyAPIGateway.Utilities.ShowMessage("DynamicAsteroids", $"Created spawn area '{name}' at {position} with radius {radius}"); + } + + private void RemoveSpawnArea(string name) + { + AsteroidSettings.RemoveSpawnableArea(name); + Log.Info($"Removed spawn area '{name}'"); + MyAPIGateway.Utilities.ShowMessage("DynamicAsteroids", $"Removed spawn area '{name}'"); + } + public override void UpdateAfterSimulation() { try From 0f9714824f39d7b682dd966478aaae789b9eaf3e Mon Sep 17 00:00:00 2001 From: InvalidArgument3 Date: Mon, 17 Jun 2024 23:05:36 -0500 Subject: [PATCH 2/2] admins only --- .../Data/Scripts/DynamicAsteroids/MainSession.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs index 1eef5292..176f854f 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs @@ -7,6 +7,8 @@ using VRageMath; using ProtoBuf; using Sandbox.Game.Entities; +using VRage.Game.ModAPI; +using VRage.Game; namespace DynamicAsteroids { @@ -80,6 +82,9 @@ protected override void UnloadData() private void OnMessageEntered(string messageText, ref bool sendToOthers) { + var player = MyAPIGateway.Session.Player; + if (player == null || !IsPlayerAdmin(player)) return; + if (messageText.StartsWith("/dynamicasteroids") || messageText.StartsWith("/dn")) { var args = messageText.Split(' '); @@ -108,6 +113,11 @@ private void OnMessageEntered(string messageText, ref bool sendToOthers) } } + private bool IsPlayerAdmin(IMyPlayer player) + { + return MyAPIGateway.Session.OnlineMode == MyOnlineModeEnum.OFFLINE || MyAPIGateway.Session.IsUserAdmin(player.SteamUserId); + } + private void CreateSpawnArea(double radius) { var player = MyAPIGateway.Session.Player;