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..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 { @@ -42,6 +44,7 @@ public override void LoadData() } MyAPIGateway.Multiplayer.RegisterMessageHandler(32000, OnMessageReceived); + MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; } catch (Exception ex) { @@ -66,6 +69,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 +80,63 @@ protected override void UnloadData() I = null; } + 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(' '); + 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 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; + 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