Skip to content

Commit

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

configgable spawnableareas
  • Loading branch information
InvalidArgument3 authored Jun 17, 2024
2 parents 60392ee + 3b96ef3 commit ad336a0
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Invalid.DynamicRoids;
using Sandbox.ModAPI;
using VRageMath;
using System.Collections.Generic;

namespace DynamicAsteroids
{
Expand Down Expand Up @@ -60,16 +61,7 @@ public static class AsteroidSettings
public static int[] PlatinumDropRange = { 500, 2500 };
public static int[] UraniniteDropRange = { 500, 2500 };

public static SpawnableArea[] ValidSpawnLocations = {
new SpawnableArea
{
CenterPosition = new Vector3D(148001024.50, 1024.50, 1024.50),
Normal = new Vector3D(1, 10, 0.5).Normalized(),
Radius = 60268000 * 2.5,
InnerRadius = 60268000 * 1.2,
HeightFromCenter = 1000,
}
};
public static List<SpawnableArea> ValidSpawnLocations = new List<SpawnableArea>();

public static bool CanSpawnAsteroidAtPoint(Vector3D point, out Vector3D velocity)
{
Expand Down Expand Up @@ -199,6 +191,14 @@ public static void SaveSettings()
WriteIntArray(writer, "GoldDropRange", GoldDropRange);
WriteIntArray(writer, "PlatinumDropRange", PlatinumDropRange);
WriteIntArray(writer, "UraniniteDropRange", UraniniteDropRange);

writer.WriteLine("[SpawnableAreas]");
foreach (var area in ValidSpawnLocations)
{
writer.WriteLine($"Name={area.Name}");
writer.WriteLine($"CenterPosition={area.CenterPosition.X},{area.CenterPosition.Y},{area.CenterPosition.Z}");
writer.WriteLine($"Radius={area.Radius}");
}
}
}
catch (Exception ex)
Expand All @@ -216,6 +216,7 @@ public static void LoadSettings()
using (var reader = MyAPIGateway.Utilities.ReadFileInWorldStorage("AsteroidSettings.cfg", typeof(AsteroidSettings)))
{
string line;
SpawnableArea currentArea = null;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("[") || string.IsNullOrWhiteSpace(line))
Expand Down Expand Up @@ -380,10 +381,33 @@ public static void LoadSettings()
case "UraniniteDropRange":
UraniniteDropRange = ReadIntArray(value);
break;
case "Name":
if (currentArea != null) ValidSpawnLocations.Add(currentArea);
currentArea = new SpawnableArea { Name = value };
break;
case "CenterPosition":
var coords = value.Split(',');
currentArea.CenterPosition = new Vector3D(double.Parse(coords[0]), double.Parse(coords[1]), double.Parse(coords[2]));
break;
case "Radius":
currentArea.Radius = double.Parse(value);
break;
}
}
if (currentArea != null) ValidSpawnLocations.Add(currentArea);
}
}
else
{
// Create default configuration if it doesn't exist
ValidSpawnLocations.Add(new SpawnableArea
{
Name = "DefaultArea",
CenterPosition = new Vector3D(0.0, 0.0, 0.0),
Radius = 10000
});
SaveSettings();
}
}
catch (Exception ex)
{
Expand All @@ -410,26 +434,19 @@ private static int[] ReadIntArray(string value)

public class SpawnableArea
{
public Vector3D CenterPosition;
public Vector3D Normal;
public double Radius;
public double InnerRadius;
public double HeightFromCenter;
public string Name { get; set; }
public Vector3D CenterPosition { get; set; }
public double Radius { get; set; }

public bool ContainsPoint(Vector3D point)
{
point -= CenterPosition;
double pointDistanceSq = point.LengthSquared();
if (pointDistanceSq > Radius * Radius || pointDistanceSq < InnerRadius * InnerRadius)
return false;
if (Math.Abs(Vector3D.Dot(point, Normal)) > HeightFromCenter)
return false;
return true;
double distanceSquared = (point - CenterPosition).LengthSquared();
return distanceSquared <= Radius * Radius;
}

public Vector3D VelocityAtPoint(Vector3D point)
{
return -(point - CenterPosition).Cross(Normal).Normalized() * AsteroidSettings.AsteroidVelocityBase;
return (point - CenterPosition).Normalized() * AsteroidSettings.AsteroidVelocityBase;
}
}
}

0 comments on commit ad336a0

Please sign in to comment.