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

Fix for drone deadlock #190

Merged
merged 7 commits into from
Apr 19, 2021
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using NebulaModel.Attributes;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets.Factory;
using NebulaModel.Packets.Processors;
using NebulaWorld.Factory;
using NebulaWorld.Player;

namespace NebulaClient.PacketProcessors.Factory.Entity
{
Expand Down Expand Up @@ -31,6 +33,13 @@ public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn)
planet.audio.Init();
}

//Remove building from drone queue
GameMain.mainPlayer.mecha.droneLogic.serving.Remove(-packet.PrebuildId);
planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId);
if (!DroneManager.RemoveBuildRequest(-packet.PrebuildId))
{
Log.Warn($"Build Request was not succesfully removed.");
}
planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId);

// Make sure to free the physics once the FlattenTerrain is done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn)
}
}

//Remove building from drone queue
GameMain.mainPlayer.mecha.droneLogic.serving.Remove(-packet.PrebuildId);
planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId);

// Make sure to free all temp data if we were not on this planet once the BuildFinally is done
Expand Down
9 changes: 7 additions & 2 deletions NebulaModel/Packets/Players/NewDroneOrderPacket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace NebulaModel.Packets.Players
using NebulaModel.DataStructures;
using UnityEngine;

namespace NebulaModel.Packets.Players
{
public class NewDroneOrderPacket
{
Expand All @@ -8,16 +11,18 @@ public class NewDroneOrderPacket
public ushort PlayerId { get; set; }
public int Stage { get; set; }
public int Priority { get; set; }
public Float3 EntityPos { get; set; }

public NewDroneOrderPacket() { }
public NewDroneOrderPacket(int planetId, int droneId, int entityId, ushort playerId, int stage, int priority)
public NewDroneOrderPacket(int planetId, int droneId, int entityId, ushort playerId, int stage, int priority, Vector3 entityPos)
{
PlanetId = planetId;
DroneId = droneId;
EntityId = entityId;
PlayerId = playerId;
Stage = stage;
Priority = priority;
EntityPos = entityPos.ToFloat3();
}
}
}
6 changes: 6 additions & 0 deletions NebulaPatcher/Patches/Dynamic/PlanetFactory_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NebulaWorld.Factory;
using UnityEngine;
using NebulaWorld.Planet;
using NebulaWorld.Player;

namespace NebulaPatcher.Patches.Dynamic
{
Expand Down Expand Up @@ -55,6 +56,11 @@ public static bool BuildFinally_Prefix(PlanetFactory __instance, Player player,
LocalPlayer.SendPacket(new BuildEntityRequest(__instance.planetId, prebuildId));
}

if (!LocalPlayer.IsMasterClient && !FactoryManager.EventFromServer)
{
DroneManager.AddBuildRequestSent(-prebuildId);
}

return LocalPlayer.IsMasterClient || FactoryManager.EventFromServer;
}

Expand Down
32 changes: 29 additions & 3 deletions NebulaPatcher/Patches/Transpilers/MechaDroneLogic_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace NebulaPatcher.Patches.Transpiler
[HarmonyPatch(typeof(MechaDroneLogic))]
class MechaDroneLogic_Patch
{
/*
* Call DroneManager.BroadcastDroneOrder(int droneId, int entityId, int stage) when drone gets new order
*/
[HarmonyTranspiler]
[HarmonyPatch("UpdateTargets")]
static IEnumerable<CodeInstruction> UpdateTargets_Transpiler(IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
/*
* Call DroneManager.BroadcastDroneOrder(int droneId, int entityId, int stage) when drone gets new order
*/
for (int i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Pop &&
Expand All @@ -32,6 +32,32 @@ static IEnumerable<CodeInstruction> UpdateTargets_Transpiler(IEnumerable<CodeIns
break;
}
}

/*
* Update search for new targets. Do not include those for whose build request was sent and client is waiting for the response from server
* Change:
if (a.sqrMagnitude > this.sqrMinBuildAlt && sqrMagnitude <= num2 && !this.serving.Contains(num4))
*
* To:
if (a.sqrMagnitude > this.sqrMinBuildAlt && sqrMagnitude <= num2 && !this.serving.Contains(num4) && !DroneManager.IsPendingBuildRequest(num4))
*/
for (int i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Brtrue &&
codes[i - 1].opcode == OpCodes.Callvirt &&
codes[i + 1].opcode == OpCodes.Ldloc_S &&
codes[i - 2].opcode == OpCodes.Ldloc_S &&
codes[i + 2].opcode == OpCodes.Stloc_2)
{
codes.InsertRange(i + 1, new CodeInstruction[] {
new CodeInstruction(OpCodes.Ldloc_S, 6),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(DroneManager), nameof(DroneManager.IsPendingBuildRequest), new System.Type[] { typeof(int) })),
new CodeInstruction(OpCodes.Brtrue_S, codes[i].operand),
});
break;
}
}

return codes;
}

Expand Down
27 changes: 26 additions & 1 deletion NebulaWorld/Player/DroneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ public static class DroneManager
public static int[] DronePriorities = new int[255];
public static Random rnd = new Random();
public static Dictionary<ushort, List<int>> PlayerDroneBuildingPlans = new Dictionary<ushort, List<int>>();
public static List<int> PendingBuildRequests = new List<int>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be locked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is modified only from the main thread, from the GameTick() or from the Update() when packets are processed. It is purely used only on the client-side.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be thinking about it too much, but there's not much clarity or convention on when something can be accessed from Host/Client/Both and what is the source of truth for each one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case of PendingBuildRequests it is used by clients to remember, for which prebuilds the request was sent. The issue was following: drone reached the prebuild, client sent request to build it, however from the drone point of view the prebuild still exist and still need to be built. So I have created this list to ignore prebuilds for which the client already sent request and it is just waiting for the response from server.

But it is true, that for the "Manager" classes, it is sometimes hard to see, what is used by clients and what by the host.


public static void BroadcastDroneOrder(int droneId, int entityId, int stage)
{
if (!SimulatedWorld.Initialized)
{
return;
}

int priority = 0;
if (stage == 1 || stage == 2)
{
priority = rnd.Next();
DronePriorities[droneId] = priority;
}
LocalPlayer.SendPacketToLocalPlanet(new NewDroneOrderPacket(GameMain.mainPlayer.planetId, droneId, entityId, LocalPlayer.PlayerId, stage, priority));
else
{
GameMain.mainPlayer.mecha.droneLogic.serving.Remove(entityId);
}
LocalPlayer.SendPacketToLocalPlanet(new NewDroneOrderPacket(GameMain.mainPlayer.planetId, droneId, entityId, LocalPlayer.PlayerId, stage, priority, GameMain.localPlanet.factory.prebuildPool[-entityId].pos));
}

public static void AddPlayerDronePlan(ushort playerId, int entityId)
Expand Down Expand Up @@ -46,5 +56,20 @@ public static int[] GetPlayerDronePlans(ushort playerId)
}
return null;
}

public static void AddBuildRequestSent(int entityId)
{
PendingBuildRequests.Add(entityId);
}

public static bool IsPendingBuildRequest(int entityId)
{
return PendingBuildRequests.Contains(entityId);
}

public static bool RemoveBuildRequest(int entityId)
{
return PendingBuildRequests.Remove(entityId);
}
}
}
21 changes: 20 additions & 1 deletion NebulaWorld/SimulatedWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ public static void UpdateRemotePlayerDrone(NewDroneOrderPacket packet)

if (drone.stage == 1 || drone.stage == 2)
{
//Check if target entity exists as Prebuild
if (GameMain.data.localPlanet.factory.prebuildPool.Length <= -packet.EntityId || GameMain.data.localPlanet.factory.prebuildPool[-packet.EntityId].id == 0)
{
return;
}

//Check target prebuild if it is same prebuild that I have. Sometimes it is same ID, but different prebuild
ref PrebuildData prebuildData = ref GameMain.data.localPlanet.factory.prebuildPool[-packet.EntityId];
if (prebuildData.pos.x != packet.EntityPos.x || prebuildData.pos.y != packet.EntityPos.y || prebuildData.pos.z != packet.EntityPos.z)
{
return;
}

//Check if my drone is already going there
if (!myMecha.droneLogic.serving.Contains(packet.EntityId))
{
Expand Down Expand Up @@ -448,7 +461,13 @@ public static void OnDronesGameTick(long time, float dt)
//Update only moving drones of players on the same planet
if (drones[i].stage != 0 && GameMain.mainPlayer.planetId == remoteModel.Value.Movement.localPlanetId)
{
drones[i].Update(prebuildPool, remotePosition, dt, ref tmp, ref tmp2, 0);
if (drones[i].Update(prebuildPool, remotePosition, dt, ref tmp, ref tmp2, 0) != 0)
{
//Reset drone and release lock
drones[i].stage = 3;
GameMain.mainPlayer.mecha.droneLogic.serving.Remove(drones[i].targetObject);
drones[i].targetObject = 0;
}
}
}
remoteMecha.droneRenderer.Update();
Expand Down