Skip to content

Commit

Permalink
Merge pull request #489 from starfi5h/pr-dysonSphereStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomGamers authored Dec 25, 2021
2 parents 200d1cb + 48420dd commit b1e2225
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 58 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

0.7.6:

- @starfi5h: Added syncing of ray receiver output
- @starfi5h: Fixed lighting of remote players
- @starfi5h: Fixed clients receiving duplicate items when cancelling manual research

0.7.5:

- @sp00ktober: Fixed error caused by warning system introduced in previous update
Expand Down
17 changes: 0 additions & 17 deletions NebulaModel/DataStructures/DataStructureExtensions.cs

This file was deleted.

26 changes: 0 additions & 26 deletions NebulaModel/DataStructures/NebulaAnimationState.cs

This file was deleted.

19 changes: 19 additions & 0 deletions NebulaModel/Packets/Universe/DysonSphereStatusPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace NebulaModel.Packets.Universe
{
public class DysonSphereStatusPacket
{
public int StarIndex { get; set; }
public float GrossRadius { get; set; }
public long EnergyReqCurrentTick { get; set; }
public long EnergyGenCurrentTick { get; set; }

public DysonSphereStatusPacket() {}
public DysonSphereStatusPacket(DysonSphere dysonSphere)
{
StarIndex = dysonSphere.starData.index;
GrossRadius = dysonSphere.grossRadius;
EnergyReqCurrentTick = dysonSphere.energyReqCurrentTick;
EnergyGenCurrentTick = dysonSphere.energyGenCurrentTick;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NebulaAPI;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Universe;

namespace NebulaNetwork.PacketProcessors.Universe
{
[RegisterPacketProcessor]
internal class DysonSphereStatusProcessor : PacketProcessor<DysonSphereStatusPacket>
{
public override void ProcessPacket(DysonSphereStatusPacket packet, NebulaConnection conn)
{
DysonSphere dysonSphere = GameMain.data.dysonSpheres[packet.StarIndex];
if (IsHost || dysonSphere == null)
{
return;
}
dysonSphere.grossRadius = packet.GrossRadius;
dysonSphere.energyReqCurrentTick = packet.EnergyReqCurrentTick;
dysonSphere.energyGenCurrentTick = packet.EnergyGenCurrentTick;
}
}
}
19 changes: 18 additions & 1 deletion NebulaNetwork/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NebulaModel.Networking.Serialization;
using NebulaModel.Packets.GameHistory;
using NebulaModel.Packets.GameStates;
using NebulaModel.Packets.Universe;
using NebulaModel.Utils;
using NebulaWorld;
using System.Net.Sockets;
Expand All @@ -22,11 +23,13 @@ public class Server : NetworkProvider
private const float GAME_RESEARCH_UPDATE_INTERVAL = 2;
private const float STATISTICS_UPDATE_INTERVAL = 1;
private const float LAUNCH_UPDATE_INTERVAL = 2;
private const float DYSONSPHERE_UPDATE_INTERVAL = 5;

private float gameStateUpdateTimer = 0;
private float gameResearchHashUpdateTimer = 0;
private float productionStatisticsUpdateTimer = 0;
private float dysonLaunchUpateTimer = 0;
private float dysonLaunchUpateTimer = 1;
private float dysonSphereUpdateTimer = 0;

private WebSocketServer socket;

Expand Down Expand Up @@ -125,6 +128,7 @@ public override void Update()
gameResearchHashUpdateTimer += Time.deltaTime;
productionStatisticsUpdateTimer += Time.deltaTime;
dysonLaunchUpateTimer += Time.deltaTime;
dysonSphereUpdateTimer += Time.deltaTime;

if (gameStateUpdateTimer > GAME_STATE_UPDATE_INTERVAL)
{
Expand Down Expand Up @@ -154,6 +158,19 @@ public override void Update()
Multiplayer.Session.Launch.SendBroadcastIfNeeded();
}

if (dysonSphereUpdateTimer > DYSONSPHERE_UPDATE_INTERVAL)
{
dysonSphereUpdateTimer = 0;
DysonSphere[] dysonSpheres = GameMain.data.dysonSpheres;
for (int i = 0; i < dysonSpheres.Length; i++)
{
if (dysonSpheres[i] != null)
{
SendPacketToStar(new DysonSphereStatusPacket(dysonSpheres[i]), dysonSpheres[i].starData.id);
}
}
}

PacketProcessor.ProcessPacketQueue();
}

Expand Down
41 changes: 41 additions & 0 deletions NebulaPatcher/Patches/Dynamic/DysonSphere_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,47 @@ namespace NebulaPatcher.Patches.Dynamic
[HarmonyPatch(typeof(DysonSphere))]
internal class DysonSphere_Patch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(DysonSphere.BeforeGameTick))]
public static bool BeforeGameTick_Prefix(DysonSphere __instance, long times)
{
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
return true;
}
//Update swarm and layer energy generation stats every 120 frames
if (times % 120 == 0)
{
__instance.swarm.energyGenCurrentTick = __instance.swarm.sailCount * __instance.energyGenPerSail;
for (int i = 0; i < 10; i++)
{
DysonSphereLayer dysonSphereLayer = __instance.layersSorted[i];
if (dysonSphereLayer != null)
{
dysonSphereLayer.energyGenCurrentTick = 0L;
DysonNode[] nodePool = dysonSphereLayer.nodePool;
DysonShell[] shellPool = dysonSphereLayer.shellPool;
for (int j = 1; j < dysonSphereLayer.nodeCursor; j++)
{
if (nodePool[j] != null && nodePool[j].id == j)
{
dysonSphereLayer.energyGenCurrentTick += nodePool[j].EnergyGenCurrentTick(__instance.energyGenPerNode, __instance.energyGenPerFrame, 0L);
}
}
for (int k = 1; k < dysonSphereLayer.shellCursor; k++)
{
if (shellPool[k] != null && shellPool[k].id == k)
{
dysonSphereLayer.energyGenCurrentTick += shellPool[k].cellPoint * __instance.energyGenPerShell;
}
}
}
}
}
//Sync other Dyson sphere status related to ray receivers on client side by DysonSphereStatusPacket
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(DysonSphere.AddLayer))]
public static bool AddLayer_Prefix(DysonSphere __instance, float orbitRadius, Quaternion orbitRotation, float orbitAngularSpeed)
Expand Down
13 changes: 0 additions & 13 deletions NebulaPatcher/Patches/Dynamic/GameHistoryData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,5 @@ public static bool UnlockTech_Prefix()
//Wait for the authoritative packet for unlocking tech features in multiplayer for clients
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameHistoryData.RemoveTechInQueue))]
public static void RemoveTechInQueue_Prefix(int index, out int __state)
{
__state = GameMain.history.techQueue[index];
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
//we need to know which itemtypes are currently needed for refunds, so trigger refund before cancelling own research
Multiplayer.Session.Network.PlayerManager.SendTechRefundPackagesToClients(__state);
}
Log.Info($"RemoveTechInQueue: remove tech at index {index} with techId { __state}");
}
}
}
32 changes: 32 additions & 0 deletions NebulaPatcher/Patches/Transpilers/PowerSystem_Transpiler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HarmonyLib;
using NebulaModel.Packets.Factory.PowerTower;
using NebulaWorld;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;

Expand Down Expand Up @@ -126,5 +127,36 @@ public static IEnumerable<CodeInstruction> PowerSystem_GameTick_Transpiler(IEnum
})
.InstructionEnumeration();
}

[HarmonyTranspiler]
[HarmonyPatch(nameof(PowerSystem.RequestDysonSpherePower))]
public static IEnumerable<CodeInstruction> PowerSystem_RequestDysonSpherePower_Transpiler(IEnumerable<CodeInstruction> instructions)
{
//Prevent dysonSphere.energyReqCurrentTick from changing on the client side
//Change: if (this.dysonSphere != null)
//To: if (this.dysonSphere != null && (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost))
try
{
CodeMatcher codeMatcher = new CodeMatcher(instructions)
.MatchForward(true,
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(PowerSystem), "dysonSphere")),
new CodeMatch(OpCodes.Brfalse) //IL #93
);
object label = codeMatcher.Instruction.operand;
codeMatcher.Advance(1)
.InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate<Func<bool>>(() =>
{
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost;
}))
.InsertAndAdvance(new CodeInstruction(OpCodes.Brfalse_S, label));
return codeMatcher.InstructionEnumeration();
}
catch
{
NebulaModel.Logger.Log.Error("PowerSystem.RequestDysonSpherePower_Transpiler failed. Mod version not compatible with game version.");
return instructions;
}
}
}
}
2 changes: 2 additions & 0 deletions NebulaWorld/MonoBehaviours/Remote/RemotePlayerAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public void AnimateSailState(PlayerAnimator animator)
public void AnimateRenderers(PlayerAnimator animator)
{
animator.player.mechaArmorModel.inst_armor_mat.SetVector("_InitPositionSet", transform.position);
animator.player.mechaArmorModel.inst_part_ar_mat.SetVector("_InitPositionSet", transform.position);
animator.player.mechaArmorModel.inst_part_sk_mat.SetVector("_InitPositionSet", transform.position);
}
}
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.7.5",
"version": "0.7.6",
"assemblyVersion": {
"precision": "build"
},
Expand Down

0 comments on commit b1e2225

Please sign in to comment.