Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework power tower syncing #627

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions NebulaModel/Packets/Factory/PowerTower/PowerTowerChargerUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace NebulaModel.Packets.Factory.PowerTower;

public class PowerTowerChargerUpdate
{
public PowerTowerChargerUpdate() { }

public PowerTowerChargerUpdate(int planetId, int nodeId, bool charging)
{
PlanetId = planetId;
NodeId = nodeId;
Charging = charging;
}

public int PlanetId { get; set; }
public int NodeId { get; set; }
public bool Charging { get; set; }
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#region

using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Factory.PowerTower;
using NebulaWorld;

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.PowerTower;

[RegisterPacketProcessor]
internal class PowerTowerChargerUpdateProcessor : PacketProcessor<PowerTowerChargerUpdate>
{
protected override void ProcessPacket(PowerTowerChargerUpdate packet, NebulaConnection conn)
{
if (packet.PlanetId == -1)
{
// When a player disconnect, clear all records and restart
Multiplayer.Session.PowerTowers.LocalChargerIds.Clear();
Multiplayer.Session.PowerTowers.RemoteChargerHashIds.Clear();
return;
}

var factory = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory;
if (factory is not { powerSystem: not null })
{
return;
}
var hashId = ((long)packet.PlanetId << 32) | (long)packet.NodeId;
if (packet.Charging)
{
Multiplayer.Session.PowerTowers.RemoteChargerHashIds.Add(hashId);
}
else
{
Multiplayer.Session.PowerTowers.RemoteChargerHashIds.Remove(hashId);
}
}
}

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions NebulaNetwork/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ public void PlayerDisconnected(INebulaConnection conn)
{
availablePlayerIds.Enqueue(player.Id);
}
Multiplayer.Session.PowerTowers.OnClientDisconnect();
Multiplayer.Session.Statistics.UnRegisterPlayer(player.Id);
Multiplayer.Session.DysonSpheres.UnRegisterPlayer(conn);

Expand Down
24 changes: 4 additions & 20 deletions NebulaPatcher/Patches/Dynamic/PowerSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@ public static void PowerSystem_GameTick_Prefix(long time, ref bool isActive)
}
}

[HarmonyPostfix]
[HarmonyPatch(nameof(PowerSystem.GameTick))]
public static void PowerSystem_GameTick_Postfix(PowerSystem __instance)
{
if (!Multiplayer.IsActive)
{
return;
}
for (var i = 1; i < __instance.netCursor; i++)
{
var pNet = __instance.netPool[i];
pNet.energyRequired += Multiplayer.Session.PowerTowers.GetExtraDemand(__instance.planet.id, i);
}
Multiplayer.Session.PowerTowers.GivePlayerPower();
Multiplayer.Session.PowerTowers.UpdateAllAnimations(__instance.planet.id);
}

[HarmonyPrefix]
[HarmonyPatch(nameof(PowerSystem.RemoveNodeComponent))]
public static bool RemoveNodeComponent(PowerSystem __instance, int id)
Expand All @@ -47,9 +30,10 @@ public static bool RemoveNodeComponent(PowerSystem __instance, int id)
return true;
}
// as the destruct is synced across players this event is too
// and as such we can safely remove power demand for every player
var pComp = __instance.nodePool[id];
Multiplayer.Session.PowerTowers.RemExtraDemand(__instance.planet.id, pComp.networkId, id);
// and as such we can safely remove power demand for every player
Multiplayer.Session.PowerTowers.LocalChargerIds.Remove(id);
var hashId = (long)__instance.factory.planetId << 32 | (long)id;
Multiplayer.Session.PowerTowers.RemoteChargerHashIds.Remove(hashId);

return true;
}
Expand Down
Loading