Skip to content

Commit

Permalink
Merge pull request #284 from InvalidArgument3/enceladus
Browse files Browse the repository at this point in the history
oh these dont even work past sync range in mp
  • Loading branch information
InvalidArgument3 authored Dec 7, 2024
2 parents dc31248 + 9867207 commit bc5e565
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 35 deletions.
5 changes: 5 additions & 0 deletions Ringway/Data/Scripts/TeleportGateway/NetworkMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static class NetworkHandler
public const ushort TeleportResponseId = 8002;
public const ushort JumpRequestId = 8003;
public const ushort SyncSettingsId = 8004;
public const ushort ChannelUpdateId = 8005;


public static void Register()
Expand All @@ -133,6 +134,8 @@ public static void Register()
MyAPIGateway.Multiplayer.RegisterMessageHandler(TeleportResponseId, HandleTeleportResponse);
MyAPIGateway.Multiplayer.RegisterMessageHandler(JumpRequestId, HandleJumpRequest);
MyAPIGateway.Multiplayer.RegisterMessageHandler(SyncSettingsId, HandleSyncSettings);
MyAPIGateway.Multiplayer.RegisterMessageHandler(ChannelUpdateId, TeleportCore.OnChannelUpdateReceived);

}

public static void Unregister()
Expand All @@ -142,6 +145,8 @@ public static void Unregister()
MyAPIGateway.Multiplayer.UnregisterMessageHandler(TeleportResponseId, HandleTeleportResponse);
MyAPIGateway.Multiplayer.UnregisterMessageHandler(JumpRequestId, HandleJumpRequest);
MyAPIGateway.Multiplayer.UnregisterMessageHandler(SyncSettingsId, HandleSyncSettings);
MyAPIGateway.Multiplayer.UnregisterMessageHandler(ChannelUpdateId, TeleportCore.OnChannelUpdateReceived);

}

private static void HandleSyncSettings(byte[] data)
Expand Down
71 changes: 46 additions & 25 deletions Ringway/Data/Scripts/TeleportGateway/TeleportCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static class TeleportCore {
internal static Dictionary<string, List<long>> _TeleportLinks = new Dictionary<string, List<long>>();
internal static Dictionary<long, TeleportGateway> _instances = new Dictionary<long, TeleportGateway>();
internal static readonly object _lock = new object();
public static Dictionary<long, GateInfo> _gateChannels = new Dictionary<long, GateInfo>();


public static void UpdateTeleportLinks() {
lock (_lock) {
Expand All @@ -44,7 +46,8 @@ public static void UpdateTeleportLinks() {
}
}

MyLogger.Log($"TPCore: UpdateTeleportLinks: Total gateways found: {gateways.Count}");

MyLogger.Log($"TPCore: UpdateTeleportLinks: Total gateways found: {gateways.Count}");

foreach (var gateway in gateways) {
var gatewayLogic = gateway.GameLogic.GetAs<TeleportGateway>();
Expand Down Expand Up @@ -176,6 +179,25 @@ public static void ServerProcessTeleportRequest(TeleportRequestMessage message)
}
}

public static void OnChannelUpdateReceived(byte[] data)
{
var message = MyAPIGateway.Utilities.SerializeFromBinary<ChannelUpdateMessage>(data);
if (message == null) return;

lock (_lock)
{
if (!_gateChannels.ContainsKey(message.EntityId))
{
_gateChannels[message.EntityId] = new GateInfo();
}

_gateChannels[message.EntityId].GatewayName = message.GatewayName;
}

UpdateTeleportLinks();
}


public static void TeleportEntity(IMyEntity entity, IMyCollector sourceGateway, IMyCollector destGateway) {
MyLogger.Log($"TPCore: TeleportEntity: Teleporting entity {entity.EntityId}");

Expand Down Expand Up @@ -313,43 +335,37 @@ public static void ClientApplyTeleportResponse(TeleportResponseMessage message)
}
}

public static long GetDestinationGatewayId(string link, long sourceGatewayId) {
List<long> linkedGateways;
lock (_lock) {
if (!_TeleportLinks.TryGetValue(link, out linkedGateways) || linkedGateways.Count < 2) {
MyLogger.Log($"TPCore: GetDestinationGatewayId: No valid linked gateways found for link {link}");
return 0;
}
}

var sourceGateway = MyAPIGateway.Entities.GetEntityById(sourceGatewayId) as IMyCollector;
if (sourceGateway == null) {
MyLogger.Log($"TPCore: GetDestinationGatewayId: Source gateway {sourceGatewayId} not found");
public static long GetDestinationGatewayId(string link, long sourceGatewayId)
{
if (!_TeleportLinks.ContainsKey(link))
return 0;
}

var sourcePosition = sourceGateway.GetPosition();
var linkedGateways = _TeleportLinks[link];
if (linkedGateways.Count == 0)
return 0;

// Find the nearest gateway that is not the source gateway
long nearestGatewayId = 0;
double nearestDistance = double.MaxValue;
var sourcePosition = MyAPIGateway.Entities.GetEntityById(sourceGatewayId)?.GetPosition() ?? Vector3D.Zero;

foreach (var gatewayId in linkedGateways) {
if (gatewayId == sourceGatewayId) continue;
foreach (var gatewayId in linkedGateways)
{
if (gatewayId == sourceGatewayId)
continue;

var destinationGateway = MyAPIGateway.Entities.GetEntityById(gatewayId) as IMyCollector;
if (destinationGateway == null) continue;
var gateway = MyAPIGateway.Entities.GetEntityById(gatewayId);
if (gateway == null)
continue;

var distance = Vector3D.Distance(sourcePosition, destinationGateway.GetPosition());
if (distance < nearestDistance) {
var distance = Vector3D.Distance(sourcePosition, gateway.GetPosition());
if (distance < nearestDistance)
{
nearestDistance = distance;
nearestGatewayId = gatewayId;
}
}

if (nearestGatewayId == 0) {
MyLogger.Log($"TPCore: GetDestinationGatewayId: No valid destination gateway found for link {link}");
}

return nearestGatewayId;
}

Expand Down Expand Up @@ -488,4 +504,9 @@ private static IMyPlayer GetPlayerById(long playerId) {
return playerList.Find(p => p.IdentityId == playerId);
}
}
public class GateInfo
{
public long EntityId { get; set; }
public string GatewayName { get; set; }
}
}
47 changes: 37 additions & 10 deletions Ringway/Data/Scripts/TeleportGateway/TeleportGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TeleportGateway : MyGameLogicComponent {
private const float POWER_PER_100KM = 1.0f; // 1 MWh per 100km
private const float MIN_TELEPORT_CHARGE_PERCENTAGE = 0.1f; // 10% charge threshold

private const double MAX_TELEPORT_DISTANCE = 1000000.0 * 1000; // 100,000,000 km in meters
private const double MAX_TELEPORT_DISTANCE = 1000000.0 * 1000; // 1,000,000 km in meters
private static MyParticleEffect teleportEffect;

static TeleportGateway() {
Expand Down Expand Up @@ -549,20 +549,36 @@ private static void CreateControls() {
MyLogger.Log("TPGate: CreateControl: Custom controls and actions created");
}

private static IMyTerminalControl CreateGatewayNameControl() {
private static IMyTerminalControl CreateGatewayNameControl()
{
var control =
MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlTextbox, IMyCollector>("GatewayName");
control.Title = MyStringId.GetOrCompute("Gateway Name");
control.Getter = (block) => {
control.Getter = (block) =>
{
var gateway = block.GameLogic.GetAs<TeleportGateway>();
return gateway != null ? new StringBuilder(gateway.Settings.GatewayName) : new StringBuilder();
};
control.Setter = (block, value) => {
control.Setter = (block, value) =>
{
var gateway = block.GameLogic.GetAs<TeleportGateway>();
if (gateway != null) {
if (gateway != null)
{
gateway.Settings.GatewayName = value.ToString();
gateway.Settings.Changed = true;
gateway.TrySave();

// Send the updated channel information to the server
if (!MyAPIGateway.Multiplayer.IsServer)
{
var message = new ChannelUpdateMessage
{
EntityId = gateway.RingwayBlock.EntityId,
GatewayName = gateway.Settings.GatewayName
};
var data = MyAPIGateway.Utilities.SerializeToBinary(message);
MyAPIGateway.Multiplayer.SendMessageToServer(NetworkHandler.ChannelUpdateId, data);
}
}
};
control.SupportsMultipleBlocks = false;
Expand Down Expand Up @@ -835,11 +851,13 @@ private static void NotifyPlayersInRange(string text, Vector3D position, double
}
}

public static void ProcessJumpRequest(long gatewayId, string link) {
public static void ProcessJumpRequest(long gatewayId, string link)
{
MyLogger.Log($"TPGate: ProcessJumpRequest: Processing jump request for gateway {gatewayId}, link {link}");

var block = MyAPIGateway.Entities.GetEntityById(gatewayId) as IMyCollector;
if (block == null || !block.IsWorking) {
if (block == null || !block.IsWorking)
{
MyLogger.Log($"TPCore: ProcessJumpRequest: Gateway {gatewayId} is null or not functional");
return;
}
Expand Down Expand Up @@ -902,7 +920,6 @@ public static void ProcessJumpRequest(long gatewayId, string link) {
}
}


// Teleport unpiloted ships in range and play directional effects
List<IMyEntity> potentialEntities = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere);
foreach (var entity in potentialEntities)
Expand Down Expand Up @@ -935,7 +952,8 @@ public static void ProcessJumpRequest(long gatewayId, string link) {
shipsToTeleport++;
}

if (teleportAttempted) {
if (teleportAttempted)
{
MyLogger.Log($"TPGate: ProcessJumpRequest: Teleport attempted");
NotifyPlayersInRange(
$"TPGate: Teleporting {playersToTeleport} player(s) and {shipsToTeleport} ship(s)",
Expand All @@ -945,6 +963,15 @@ public static void ProcessJumpRequest(long gatewayId, string link) {
);
}
}

}
[ProtoContract]
public class ChannelUpdateMessage
{
[ProtoMember(1)]
public long EntityId { get; set; }
[ProtoMember(2)]
public string GatewayName { get; set; }
}


}

0 comments on commit bc5e565

Please sign in to comment.