Skip to content

Commit

Permalink
Updated gps manager to use Guids instead of structs for identificatio…
Browse files Browse the repository at this point in the history
…n of gpss
  • Loading branch information
8vogt committed Feb 17, 2021
1 parent 705ad99 commit 7401f40
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 76 deletions.
6 changes: 3 additions & 3 deletions SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ private void AddAllPersistentGps()
case MySystemObjectType.MOON:
if (settings.MoonGPSMode == MyGPSGenerationMode.PERSISTENT)
{
MyGPSManager.Static.AddPersistentGps(item.DisplayName, MOON_GPS_COLOR, item.CenterPosition);
MyGPSManager.Static.AddPersistentGps(item.DisplayName, MOON_GPS_COLOR, item.CenterPosition, item.Id);
}
break;
case MySystemObjectType.PLANET:
if (settings.PlanetGPSMode == MyGPSGenerationMode.PERSISTENT)
{
MyGPSManager.Static.AddPersistentGps(item.DisplayName, PLANET_GPS_COLOR, item.CenterPosition);
MyGPSManager.Static.AddPersistentGps(item.DisplayName, PLANET_GPS_COLOR, item.CenterPosition, item.Id);
}
break;
case MySystemObjectType.ASTEROIDS:
Expand All @@ -187,7 +187,7 @@ private void AddAllPersistentGps()
MyAbstractAsteroidObjectProvider provider = null;
if (MyAsteroidObjectsManager.Static.AsteroidObjectProviders.TryGetValue(asteroid.AsteroidTypeName, out provider))
{
MyGPSManager.Static.AddPersistentGps(item.DisplayName, RING_GPS_COLOR, provider.GetAsteroidObjectShape(asteroid).GetPointInShape());
MyGPSManager.Static.AddPersistentGps(item.DisplayName, RING_GPS_COLOR, provider.GetAsteroidObjectShape(asteroid).GetPointInShape(), item.Id);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,27 @@ public override void UpdateGpsForPlayer(MyEntityTracker entity)

if(shape.Contains(entityPosition) != ContainmentType.Disjoint)
{
MyGPSManager.Static.RemoveDynamicGps(asteroid.DisplayName, Color.Yellow, player.GetPlayerIdentityId());
MyGPSManager.Static.RemoveDynamicGps(player.GetPlayerIdentityId(), asteroid.Id);
}

Vector3D closestPos = shape.GetClosestPoint(entityPosition);
double distance = Vector3D.Subtract(entityPosition, closestPos).Length();
if (distance > 5000000)
{
MyGPSManager.Static.RemoveDynamicGps(asteroid.DisplayName, Color.Yellow, player.GetPlayerIdentityId());
MyGPSManager.Static.RemoveDynamicGps(player.GetPlayerIdentityId(), asteroid.Id);
}
else if(distance > 5)
{
if(MyGPSManager.Static.DynamicGpsExists(asteroid.DisplayName, Color.Yellow, player.GetPlayerIdentityId()))
if(MyGPSManager.Static.DynamicGpsExists(asteroid.Id, player.GetPlayerIdentityId()))
{
MyGPSManager.Static.ModifyDynamicGps(asteroid.DisplayName, Color.Yellow, closestPos, player.GetPlayerIdentityId());
MyGPSManager.Static.ModifyDynamicGps(asteroid.DisplayName, Color.Yellow, closestPos, player.GetPlayerIdentityId(), asteroid.Id);
continue;
}
MyGPSManager.Static.AddDynamicGps(asteroid.DisplayName, Color.Yellow, closestPos, player.GetPlayerIdentityId());
MyGPSManager.Static.AddDynamicGps(asteroid.DisplayName, Color.Yellow, closestPos, player.GetPlayerIdentityId(), asteroid.Id);
}
else
{
MyGPSManager.Static.RemoveDynamicGps(asteroid.DisplayName, Color.Yellow, player.GetPlayerIdentityId());
MyGPSManager.Static.RemoveDynamicGps(player.GetPlayerIdentityId(), asteroid.Id);
}
}
}
Expand Down Expand Up @@ -264,8 +264,6 @@ protected override MyProceduralCell GenerateCellSeeds(Vector3I cellId)

var ring = GetAsteroidObjectAt(position);

MyPluginLog.Debug("Asteroid object is there " + ring);

if (ring == null) continue;

var cellObjectSeed = new MyObjectSeed(cell, position, MyRandom.Instance.Next(ring.AsteroidSize.Min, ring.AsteroidSize.Max));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,26 @@ public override void UpdateGpsForPlayer(MyEntityTracker tracker)

if (settings.PlanetGPSMode != MyGPSGenerationMode.DISCOVERY && settings.MoonGPSMode != MyGPSGenerationMode.DISCOVERY) return;

if(systemObjects.Count > 0)
foreach(var obj in systemObjects)
{
foreach(var obj in systemObjects)
if((obj.Type == MySystemObjectType.MOON && settings.MoonGPSMode == MyGPSGenerationMode.DISCOVERY) || (obj.Type == MySystemObjectType.PLANET && settings.PlanetGPSMode == MyGPSGenerationMode.DISCOVERY))
{
if(obj.Type == MySystemObjectType.MOON || obj.Type == MySystemObjectType.PLANET)
{
var distance = Vector3D.Distance(tracker.CurrentPosition, obj.CenterPosition);
var distance = Vector3D.Distance(tracker.CurrentPosition, obj.CenterPosition);

if(distance <= 50000000)
if(distance <= 50000000)
{
if (MyGPSManager.Static.DynamicGpsExists(obj.Id, entity.GetPlayerIdentityId()))
{
if (MyGPSManager.Static.DynamicGpsExists(obj.DisplayName, GPS_COLOR, entity.GetPlayerIdentityId()))
{
MyGPSManager.Static.ModifyDynamicGps(obj.DisplayName, GPS_COLOR, obj.CenterPosition, entity.GetPlayerIdentityId());
}
else
{
MyGPSManager.Static.AddDynamicGps(obj.DisplayName, GPS_COLOR, obj.CenterPosition, entity.GetPlayerIdentityId());
}
return;
MyGPSManager.Static.ModifyDynamicGps(obj.DisplayName, GPS_COLOR, obj.CenterPosition, entity.GetPlayerIdentityId(), obj.Id);
}

MyGPSManager.Static.RemoveDynamicGps(obj.DisplayName, GPS_COLOR, entity.GetPlayerIdentityId());
else
{
MyGPSManager.Static.AddDynamicGps(obj.DisplayName, GPS_COLOR, obj.CenterPosition, entity.GetPlayerIdentityId(), obj.Id);
}
continue;
}

MyGPSManager.Static.RemoveDynamicGps(entity.GetPlayerIdentityId(), obj.Id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ProtoBuf;
using System;
using System.Collections.Generic;
using VRage;
using VRageMath;
Expand Down Expand Up @@ -37,9 +38,15 @@ public class PersistentGpsData
public SerializableVector3D Position = Vector3D.Zero;

/// <summary>
/// A set of all players, that already know of this gps.
/// The Id of the gps object
/// </summary>
[ProtoMember(4)]
public Guid Id = Guid.Empty;

/// <summary>
/// A set of all players, that already know of this gps.
/// </summary>
[ProtoMember(5)]
public HashSet<long> PlayerIds = new HashSet<long>();
}
}
114 changes: 67 additions & 47 deletions SEWorldGenPlugin/Session/MyGPSManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,38 @@ public class MyGPSManager : MySessionComponentBase
/// <summary>
/// Struct that identifies a gps
/// </summary>
struct MyGpsId
struct MyGpsData
{
public string Name;

public Vector3 Color;

public Vector3D Position;

public MyGpsId(string name, Vector3 color, Vector3D position)
public Guid Id;

public HashSet<long> Players;

public MyGpsData(string name, Vector3 color, Vector3D position, Guid id)
{
Name = name;
Color = color;
Position = position;
Id = id;
Players = new HashSet<long>();
}

public MyGpsData(string name, Vector3 color, Vector3D position, Guid id, HashSet<long> players)
{
Name = name;
Color = color;
Position = position;
Id = id;
Players = players;
}
}


/// <summary>
/// File name for the save file containing the gps data
/// </summary>
Expand All @@ -45,43 +61,48 @@ public MyGpsId(string name, Vector3 color, Vector3D position)
public static MyGPSManager Static;

/// <summary>
/// A map of all global gpss and the players that gps is known to
/// A map of all global gps ids and data
/// </summary>
private Dictionary<MyGpsId, HashSet<long>> m_globalGpss;
private Dictionary<Guid, MyGpsData> m_globalGpss;

/// <summary>
/// A map of all dynamic gpss and their corresponding player ids to the hash of the gps,
/// to allow modification of it.
/// </summary>
private Dictionary<Tuple<MyGpsId, long>, int> m_dynamicGpss;
private Dictionary<Tuple<Guid, long>, int> m_dynamicGpss;

/// <summary>
/// Adds a new gps persistent gps to all players
/// </summary>
/// <param name="name">Name of the gps</param>
/// <param name="color">Color of the gps</param>
/// <param name="pos">Position of the gps</param>
public void AddPersistentGps(string name, Color color, Vector3D pos)
/// <param name="id">The id of the gps</param>
public void AddPersistentGps(string name, Color color, Vector3D pos, Guid id)
{
MyGpsId id = new MyGpsId(name, color, pos);
MyGpsData data = new MyGpsData()
{
Name = name,
Color = color,
Position = pos,
Id = id,
Players = new HashSet<long>()
};

if (!m_globalGpss.ContainsKey(id))
{
MyPluginLog.Debug("Adding new persistent gps " + name);
m_globalGpss[id] = new HashSet<long>();
m_globalGpss[id] = data;
}
}

/// <summary>
/// Checks if a given persistent gps already exists
/// </summary>
/// <param name="name">Name of the gps</param>
/// <param name="color">Color of the gps</param>
/// <param name="pos">Position of the gps</param>
/// <param name="id">The id of the gps</param>
/// <returns>True if the persistent gps exists</returns>
public bool PersistenGpsExists(string name, Color color, Vector3D pos)
public bool PersistenGpsExists(Guid id)
{
MyGpsId id = new MyGpsId(name, color, pos);
return m_globalGpss.ContainsKey(id);
}

Expand All @@ -91,12 +112,12 @@ public bool PersistenGpsExists(string name, Color color, Vector3D pos)
/// <param name="name">Name of the gps</param>
/// <param name="color">Color of the gps</param>
/// <param name="pos">Position of the gps</param>
/// <param name="player">Player, the gps belongs to</param>
/// <param name="playerId">Player, the gps belongs to</param>
/// <param name="id">The id of the gps</param>
/// <returns>False, if the gps is already added, else true</returns>
public bool AddDynamicGps(string name, Color color, Vector3D pos, long playerId)
public bool AddDynamicGps(string name, Color color, Vector3D pos, long playerId, Guid id)
{
MyGpsId id = new MyGpsId(name, color, pos);
Tuple<MyGpsId, long> key = new Tuple<MyGpsId, long>(id, playerId);
Tuple<Guid, long> key = new Tuple<Guid, long>(id, playerId);

if (m_dynamicGpss.ContainsKey(key)) return false;
MyGps gps = new MyGps
Expand All @@ -123,12 +144,12 @@ public bool AddDynamicGps(string name, Color color, Vector3D pos, long playerId)
/// <param name="name">Name of the existing gps</param>
/// <param name="color">Color of the gps, needs to be the same as the old one</param>
/// <param name="pos">New Position of the gps</param>
/// <param name="player"></param>
/// <param name="playerId"></param>
/// <param name="id">The id of the gps</param>
/// <returns></returns>
public bool ModifyDynamicGps(string name, Color color, Vector3D pos, long playerId)
public bool ModifyDynamicGps(string name, Color color, Vector3D pos, long playerId, Guid id)
{
MyGpsId id = new MyGpsId(name, color, pos);
Tuple<MyGpsId, long> key = new Tuple<MyGpsId, long>(id, playerId);
Tuple<Guid, long> key = new Tuple<Guid, long>(id, playerId);

if (m_dynamicGpss.ContainsKey(key))
{
Expand All @@ -138,6 +159,8 @@ public bool ModifyDynamicGps(string name, Color color, Vector3D pos, long player
if (gps == null) return false;

gps.Coords = pos;
gps.Name = name;
gps.GPSColor = color;

MySession.Static.Gpss.SendModifyGps(playerId, gps);

Expand All @@ -153,14 +176,12 @@ public bool ModifyDynamicGps(string name, Color color, Vector3D pos, long player
/// <summary>
/// Tries to remove a dynamic gps marker from the player
/// </summary>
/// <param name="name">Name of the gps</param>
/// <param name="color">Color of the gps</param>
/// <param name="player">Player, the gps belongs to</param>
/// <param name="id">The gps id</param>
/// <param name="playerId">The player id of the player this gps belongs to</param>
/// <returns></returns>
public bool RemoveDynamicGps(string name, Color color, long playerId)
public bool RemoveDynamicGps(long playerId, Guid id)
{
MyGpsId id = new MyGpsId(name, color, Vector3D.Zero);
Tuple<MyGpsId, long> key = new Tuple<MyGpsId, long>(id, playerId);
Tuple<Guid, long> key = new Tuple<Guid, long>(id, playerId);

if (m_dynamicGpss.ContainsKey(key))
{
Expand All @@ -174,14 +195,12 @@ public bool RemoveDynamicGps(string name, Color color, long playerId)
/// <summary>
/// Checks, whether or not the gps already exists.
/// </summary>
/// <param name="name">Name of the gps</param>
/// <param name="color">Color of the gps</param>
/// <param name="player">Player, the gps belongs to</param>
/// <param name="id">The gps id</param>
/// <param name="playerId">The player id of the player this gps belongs to</param>
/// <returns></returns>
public bool DynamicGpsExists(string name, Color color, long playerId)
public bool DynamicGpsExists(Guid id, long playerId)
{
MyGpsId id = new MyGpsId(name, color, Vector3D.Zero);
Tuple<MyGpsId, long> key = new Tuple<MyGpsId, long>(id, playerId);
Tuple<Guid, long> key = new Tuple<Guid, long>(id, playerId);

return m_dynamicGpss.ContainsKey(key);
}
Expand All @@ -198,19 +217,19 @@ public override void UpdateBeforeSimulation()
{
foreach (var p in MySession.Static.Players.GetOnlinePlayers())
{
if (m_globalGpss[entry].Contains(p.Identity.IdentityId)) continue;
if (m_globalGpss[entry].Players.Contains(p.Identity.IdentityId)) continue;

MyGps gps = new MyGps
{
Name = entry.Name,
Coords = entry.Position,
GPSColor = entry.Color,
Name = m_globalGpss[entry].Name,
Coords = m_globalGpss[entry].Position,
GPSColor = m_globalGpss[entry].Color,
ShowOnHud = true,
AlwaysVisible = false,
DiscardAt = null
};
MySession.Static.Gpss.SendAddGps(p.Identity.IdentityId, ref gps, playSoundOnCreation: false);
m_globalGpss[entry].Add(p.Identity.IdentityId);
m_globalGpss[entry].Players.Add(p.Identity.IdentityId);
}
}
}
Expand All @@ -234,13 +253,13 @@ public override void LoadData()
ob = new MyObjectBuilder_WorldGpsData();
}

m_globalGpss = new Dictionary<MyGpsId, HashSet<long>>();
m_dynamicGpss = new Dictionary<Tuple<MyGpsId, long>, int>();
m_globalGpss = new Dictionary<Guid, MyGpsData>();
m_dynamicGpss = new Dictionary<Tuple<Guid, long>, int>();

foreach(var item in ob.PersistentGpss)
{
var id = new MyGpsId(item.Name, item.Color, item.Position);
m_globalGpss[id] = item.PlayerIds;
var data = new MyGpsData(item.Name, item.Color, item.Position, item.Id, item.PlayerIds);
m_globalGpss[item.Id] = data;
}

MyPluginLog.Log("Loading GPS manager data completed");
Expand All @@ -256,13 +275,14 @@ public override void SaveData()
MyPluginLog.Log("Saving GPS manager data");

MyObjectBuilder_WorldGpsData ob = new MyObjectBuilder_WorldGpsData();
foreach(var entry in m_globalGpss.Keys)
foreach(var entry in m_globalGpss)
{
PersistentGpsData item = new PersistentGpsData();
item.Name = entry.Name;
item.Color = entry.Color;
item.Position = entry.Position;
item.PlayerIds = m_globalGpss[entry];
item.Name = entry.Value.Name;
item.Color = entry.Value.Color;
item.Position = entry.Value.Position;
item.PlayerIds = entry.Value.Players;
item.Id = entry.Key;

ob.PersistentGpss.Add(item);
}
Expand Down

0 comments on commit 7401f40

Please sign in to comment.