Skip to content

Commit

Permalink
A bit of cleanup of drone syncing code. Not perfect but it should do …
Browse files Browse the repository at this point in the history
…its job.
  • Loading branch information
sp00ktober committed Jan 2, 2024
1 parent 571dcde commit 616ee14
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ public static IEnumerable<CodeInstruction> IdleDroneProcedure_Transpiler(IEnumer
}

// skip targets that we already asked the host about, but only ask as much as we can handle with our idle drones.
// replace: if (!factory.constructionSystem.constructServing.Contains(num12))
// with the checks from below
[HarmonyTranspiler]
[HarmonyPatch(nameof(ConstructionModuleComponent.SearchForNewTargets))]
public static IEnumerable<CodeInstruction> SearchForNewTargets_Transpiler(IEnumerable<CodeInstruction> instructions)
Expand Down Expand Up @@ -255,20 +257,18 @@ public static IEnumerable<CodeInstruction> SearchForNewTargets_Transpiler(IEnume

int[] myDronePlans = DroneManager.GetPlayerDronePlans(Multiplayer.Session.LocalPlayer.Id);

// returning true here means the targetConstructionObjectId will not be selected as a valid next target
bool a = alreadyContained;
bool b = DroneManager.IsPendingBuildRequest(targetConstructionObjectId);
bool c = (Multiplayer.Session.LocalPlayer.IsClient && DroneManager.CountPendingBuildRequest() > GameMain.mainPlayer.mecha.constructionModule.droneCount);
bool d = (Multiplayer.Session.LocalPlayer.IsHost && (myDronePlans == null ? 0 : myDronePlans.Count()) > GameMain.mainPlayer.mecha.constructionModule.droneCount);
bool isPendingBuildRequest = DroneManager.IsPendingBuildRequest(targetConstructionObjectId);
bool clientNoIdleDrones = (Multiplayer.Session.LocalPlayer.IsClient && DroneManager.CountPendingBuildRequest() > GameMain.mainPlayer.mecha.constructionModule.droneCount);
bool hostNoIdleDrones = (Multiplayer.Session.LocalPlayer.IsHost && (myDronePlans == null ? 0 : myDronePlans.Count()) > GameMain.mainPlayer.mecha.constructionModule.droneCount);

if (!a && b && Multiplayer.Session.LocalPlayer.IsHost)
if (!alreadyContained && isPendingBuildRequest && Multiplayer.Session.LocalPlayer.IsHost)
{
// this seems to be a deadlock desync, but a should be the correct value
DroneManager.RemoveBuildRequest(targetConstructionObjectId);
}

//UnityEngine.Debug.LogWarning($"{a} | {b} | {c} | {d}");
return a || b || c || d;
// returning true here means the targetConstructionObjectId will not be selected as a valid next target
return alreadyContained || isPendingBuildRequest || clientNoIdleDrones || hostNoIdleDrones;
}));

return matcher.InstructionEnumeration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public static IEnumerable<CodeInstruction> UpdateDrones_Transpiler2(IEnumerable<
return instructions;
}

// change: bool flag = ptr.owner == 0;
// to: bool flag = ptr.owner <= 0;
matcher
.SetInstruction(new CodeInstruction(OpCodes.Pop)) // remove the pushed 0
.Advance(1)
Expand Down Expand Up @@ -164,6 +166,8 @@ public static IEnumerable<CodeInstruction> UpdateDrones_Transpiler2(IEnumerable<
return instructions;
}

// change: if (constructionModuleComponent.id != ptr.owner || ptr2.id != ptr.craftId)
// to: if (ptr2.id != ptr.craftId)
matcher
.InsertAndAdvance(
HarmonyLib.Transpilers.EmitDelegate<ownerAndIdMatchMecha>((int id, int owner) =>
Expand All @@ -173,7 +177,7 @@ public static IEnumerable<CodeInstruction> UpdateDrones_Transpiler2(IEnumerable<
return id != owner; // game does exit when id does not match owner, so we do too when multiplayer is inactive
}

return false; // we set owner to negative values in ConstructionModuleComponent_Transpiler to mark drones from other players. Those still need to be rendered/updated, as well as those from battle bases
return false; // this might be a bit too open but will render any drone regardless of the games checks.
}));
var jmpOut = matcher.Operand;
matcher.SetInstruction(new CodeInstruction(OpCodes.Brtrue, jmpOut));
Expand Down
4 changes: 0 additions & 4 deletions NebulaWorld/Player/DroneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static void RemovePlayerDronePlan(ushort playerId, int entityId)
{
value.Remove(entityId);
}
UnityEngine.Debug.LogWarning($"There are still {PlayerDroneBuildingPlans.Count(kv => kv.Key == playerId)} PlayerDroneBuildingPlans left for this player ({playerId}.");
}
public static void RemovePlayerDronePlan(int entityId)
{
Expand All @@ -67,7 +66,6 @@ public static void RemovePlayerDronePlan(int entityId)
if (kvp.Value.Contains(entityId))
{
RemovePlayerDronePlan(kvp.Key, entityId);
UnityEngine.Debug.LogWarning($"There are still {PlayerDroneBuildingPlans.Count(kv => kv.Key == kvp.Key)} PlayerDroneBuildingPlans left for this player ({kvp.Key}.");
return;
}
}
Expand All @@ -78,7 +76,6 @@ public static void RemovePlayerDronePlans(ushort playerId)
{
PlayerDroneBuildingPlans.Remove(playerId);
}
UnityEngine.Debug.LogWarning($"There are still {PlayerDroneBuildingPlans.Count(kv => kv.Key == playerId)} PlayerDroneBuildingPlans left for this player ({playerId}.");
}

public static int[] GetPlayerDronePlans(ushort playerId)
Expand Down Expand Up @@ -152,7 +149,6 @@ public static int CountPendingBuildRequest()
public static void RemoveBuildRequest(int entityId)
{
bool res = PendingBuildRequests.Remove(entityId);
UnityEngine.Debug.LogWarning($"There are still {PendingBuildRequests.Count} PendingBuildRequests left.");
}

public static void EjectDronesOfOtherPlayer(ushort playerId, int planetId, int targetObjectId)
Expand Down

0 comments on commit 616ee14

Please sign in to comment.