Skip to content

Commit

Permalink
Merge pull request #66 from InvalidArgument3/roidnetworking-narrowing…
Browse files Browse the repository at this point in the history
…downthecause

command to create asteroid areas
  • Loading branch information
InvalidArgument3 authored Jun 18, 2024
2 parents 0ae13a8 + 0f97148 commit 0681311
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Sandbox.ModAPI;
using VRageMath;
using System.Collections.Generic;
using System.Linq;

namespace DynamicAsteroids
{
Expand Down Expand Up @@ -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
Expand All @@ -463,5 +486,6 @@ public Vector3D VelocityAtPoint(Vector3D point)
{
return (point - CenterPosition).Normalized() * AsteroidSettings.AsteroidVelocityBase;
}

}
}
61 changes: 61 additions & 0 deletions Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using VRageMath;
using ProtoBuf;
using Sandbox.Game.Entities;
using VRage.Game.ModAPI;
using VRage.Game;

namespace DynamicAsteroids
{
Expand Down Expand Up @@ -42,6 +44,7 @@ public override void LoadData()
}

MyAPIGateway.Multiplayer.RegisterMessageHandler(32000, OnMessageReceived);
MyAPIGateway.Utilities.MessageEntered += OnMessageEntered;
}
catch (Exception ex)
{
Expand All @@ -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)
{
Expand All @@ -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
Expand Down

0 comments on commit 0681311

Please sign in to comment.