Skip to content

Commit

Permalink
Merge pull request #145 from Baldie-dev/building-drones-sync
Browse files Browse the repository at this point in the history
Player's drone sync + labor division
  • Loading branch information
Mushroom authored Apr 16, 2021
2 parents 6653d8f + 26be60d commit 824e8cd
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 14 deletions.
11 changes: 11 additions & 0 deletions NebulaClient/MultiplayerClientSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NebulaModel.Packets.Session;
using NebulaModel.Utils;
using NebulaWorld;
using System;
using System.Net;
using UnityEngine;
using WebSocketSharp;
Expand Down Expand Up @@ -81,6 +82,16 @@ public void DestroySession()
{
serverConnection?.SendPacket(new StarBroadcastPacket(PacketProcessor.Write(packet), GameMain.data.localStar?.id ?? -1));
}
public void SendPacketToLocalPlanet<T>(T packet) where T : class, new()
{
serverConnection?.SendPacket(new PlanetBroadcastPacket(PacketProcessor.Write(packet), GameMain.mainPlayer.planetId));
}
public void SendPacketToPlanet<T>(T packet, int planetId) where T : class, new()
{
//Should send packet to particular planet
//Not needed at the moment, used only on the host side
throw new NotImplementedException();
}

public void Reconnect()
{
Expand Down
2 changes: 2 additions & 0 deletions NebulaClient/NebulaClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@
<Compile Include="PacketProcessors\Planet\FactoryDataProcessor.cs" />
<Compile Include="PacketProcessors\Planet\PlanetDataResponseProcessor.cs" />
<Compile Include="PacketProcessors\Planet\VegetationMinedProcessor.cs" />
<Compile Include="PacketProcessors\Players\NewDroneOrderProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerAnimationUpdateProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerColorChangeProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerMovementProcessor.cs" />
<Compile Include="PacketProcessors\Players\RemoveDroneOrdersProcessor.cs" />
<Compile Include="PacketProcessors\Session\HandshakeResponseProcessor.cs" />
<Compile Include="PacketProcessors\Session\PlayerDisconnectedProcessor.cs" />
<Compile Include="PacketProcessors\Session\PlayerJoiningProcessor.cs" />
Expand Down
17 changes: 17 additions & 0 deletions NebulaClient/PacketProcessors/Players/NewDroneOrderProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Players;
using NebulaModel.Packets.Processors;
using NebulaWorld;

namespace NebulaClient.PacketProcessors.Players
{
[RegisterPacketProcessor]
class NewDroneOrderProcessor : IPacketProcessor<NewDroneOrderPacket>
{
public void ProcessPacket(NewDroneOrderPacket packet, NebulaConnection conn)
{
SimulatedWorld.UpdateRemotePlayerDrone(packet);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NebulaModel.Networking;
using NebulaHost.PacketProcessors.Players;
using NebulaModel.Packets.Processors;

namespace NebulaClient.PacketProcessors.Players
{
class RemoveDroneOrdersProcessor : IPacketProcessor<RemoveDroneOrdersPacket>
{
public void ProcessPacket(RemoveDroneOrdersPacket packet, NebulaConnection conn)
{
if (packet.QueuedEntityIds != null)
{
for (int i = 0; i < packet.QueuedEntityIds.Length; i++)
{
GameMain.mainPlayer.mecha.droneLogic.serving.Remove(packet.QueuedEntityIds[i]);
}
}
}
}
}
10 changes: 10 additions & 0 deletions NebulaHost/MultiplayerHostSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public void DestroySession()
PlayerManager.SendPacketToLocalStar(packet);
}

public void SendPacketToLocalPlanet<T>(T packet) where T : class, new()
{
PlayerManager.SendPacketToLocalPlanet(packet);
}

public void SendPacketToPlanet<T>(T packet, int planetId) where T : class, new()
{
PlayerManager.SendPacketToPlanet(packet, planetId);
}

private void Update()
{
gameStateUpdateTimer += Time.deltaTime;
Expand Down
2 changes: 2 additions & 0 deletions NebulaHost/NebulaHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@
<Compile Include="PacketProcessors\GameHistory\GameHistoryResearchContributionProcessor.cs" />
<Compile Include="PacketProcessors\Planet\FactoryLoadRequestProcessor.cs" />
<Compile Include="PacketProcessors\Planet\PlanetDataRequestProcessor.cs" />
<Compile Include="PacketProcessors\Players\NewDroneOrderProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerAnimationUpdateProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerColorChangedProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerMechaDataProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerMovementProcessor.cs" />
<Compile Include="PacketProcessors\Planet\VegetationMinedProcessor.cs" />
<Compile Include="PacketProcessors\Routers\PlanetBroadcastProcessor.cs" />
<Compile Include="PacketProcessors\Players\PlayerUpdateLocalStarIdProcessor.cs" />
<Compile Include="PacketProcessors\Routers\StarBroadcastProcessor.cs" />
<Compile Include="PacketProcessors\Session\HandshakeRequestProcessor.cs" />
Expand Down
45 changes: 45 additions & 0 deletions NebulaHost/PacketProcessors/Players/NewDroneOrderProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Players;
using NebulaModel.Packets.Processors;
using NebulaWorld;
using NebulaWorld.Player;

namespace NebulaHost.PacketProcessors.Players
{
[RegisterPacketProcessor]
class NewDroneOrderProcessor : IPacketProcessor<NewDroneOrderPacket>
{
private PlayerManager playerManager;

public NewDroneOrderProcessor()
{
playerManager = MultiplayerHostSession.Instance.PlayerManager;
}

public void ProcessPacket(NewDroneOrderPacket packet, NebulaConnection conn)
{
//Host does not need to know about flying drones of other players if he is not on the same planet
if (GameMain.mainPlayer.planetId != packet.PlanetId)
{
return;
}

Player player = playerManager.GetPlayer(conn);

if (player != null)
{
if (packet.Stage == 1 || packet.Stage == 2)
{
DroneManager.AddPlayerDronePlan(player.Id, packet.EntityId);
}
else if (packet.Stage == 3)
{
DroneManager.RemovePlayerDronePlan(player.Id, packet.EntityId);
}

SimulatedWorld.UpdateRemotePlayerDrone(packet);
}
}
}
}
28 changes: 28 additions & 0 deletions NebulaHost/PacketProcessors/Routers/PlanetBroadcastProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Processors;
using NebulaModel.Packets.Routers;

namespace NebulaHost.PacketProcessors.Routers
{
[RegisterPacketProcessor]
class PlanetBroadcastProcessor : IPacketProcessor<PlanetBroadcastPacket>
{
private PlayerManager playerManager;
public PlanetBroadcastProcessor()
{
playerManager = MultiplayerHostSession.Instance.PlayerManager;
}
public void ProcessPacket(PlanetBroadcastPacket packet, NebulaConnection conn)
{
Player player = playerManager.GetPlayer(conn);
if (player != null)
{
//Forward packet to other users
playerManager.SendRawPacketToPlanet(packet.PacketObject, packet.PlanetId, conn);
//Forward packet to the host
MultiplayerHostSession.Instance.PacketProcessor.EnqueuePacketForProcessing(packet.PacketObject, conn);
}
}
}
}
52 changes: 51 additions & 1 deletion NebulaHost/PlayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using NebulaModel.DataStructures;
using NebulaHost.PacketProcessors.Players;
using NebulaModel.DataStructures;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets.Session;
using NebulaWorld;
using NebulaWorld.Player;
using NebulaWorld.Statistics;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -81,6 +83,28 @@ public Player GetSyncingPlayer(NebulaConnection conn)
}
}

public void SendPacketToLocalPlanet<T>(T packet) where T : class, new()
{
foreach (Player player in GetConnectedPlayers())
{
if (player.Data.LocalPlanetId == GameMain.data.mainPlayer.planetId)
{
player.SendPacket(packet);
}
}
}

public void SendPacketToPlanet<T>(T packet, int planetId) where T : class, new()
{
foreach (Player player in GetConnectedPlayers())
{
if (player.Data.LocalPlanetId == planetId)
{
player.SendPacket(packet);
}
}
}

public void SendRawPacketToStar(byte[] rawPacket, int starId, NebulaConnection sender)
{
foreach (Player player in GetConnectedPlayers())
Expand All @@ -92,6 +116,17 @@ public void SendRawPacketToStar(byte[] rawPacket, int starId, NebulaConnection s
}
}

public void SendRawPacketToPlanet(byte[] rawPacket, int planetId, NebulaConnection sender)
{
foreach (Player player in GetConnectedPlayers())
{
if (player.Data.LocalPlanetId == planetId && player.Connection != sender)
{
player.SendRawPacket(rawPacket);
}
}
}

public void SendPacketToOtherPlayers<T>(T packet, Player sender) where T : class, new()
{
foreach (Player player in GetConnectedPlayers())
Expand Down Expand Up @@ -125,6 +160,21 @@ public void PlayerDisconnected(NebulaConnection conn)
connectedPlayers.Remove(conn);
availablePlayerIds.Enqueue(player.Id);
StatisticsManager.instance.UnRegisterPlayer(player.Id);

//Notify players about queued building plans for drones
int[] DronePlans = DroneManager.GetPlayerDronePlans(player.Id);
if (DronePlans != null && DronePlans.Length > 0 && player.Data.LocalPlanetId > 0)
{
LocalPlayer.SendPacketToPlanet(new RemoveDroneOrdersPacket(DronePlans), player.Data.LocalPlanetId);
//Remove it also from host queue, if host is on the same planet
if (GameMain.mainPlayer.planetId == player.Data.LocalPlanetId)
{
for(int i = 0; i < DronePlans.Length; i++)
{
GameMain.mainPlayer.mecha.droneLogic.serving.Remove(DronePlans[i]);
}
}
}
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions NebulaModel/NebulaModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@
<Compile Include="Packets\Planet\FactoryLoadRequest.cs" />
<Compile Include="Packets\Planet\PlanetDataRequest.cs" />
<Compile Include="Packets\Planet\PlanetDataResponse.cs" />
<Compile Include="Packets\Players\NewDroneOrderPacket.cs" />
<Compile Include="Packets\Players\PlayerAnimationUpdate.cs" />
<Compile Include="Packets\Players\PlayerMechaData.cs" />
<Compile Include="Packets\Players\PlayerMovement.cs" />
<Compile Include="Packets\Players\PlayerColorChanged.cs" />
<Compile Include="Packets\Players\PlayerTechBonuses.cs" />
<Compile Include="Packets\Players\RemoveDroneOrdersPacket.cs" />
<Compile Include="Packets\Players\PlayerUpdateLocalStarId.cs" />
<Compile Include="Packets\Processors\IPacketProcessor.cs" />
<Compile Include="Packets\Routers\PlanetBroadcastPacket.cs" />
<Compile Include="Packets\Routers\StarBroadcastPacket.cs" />
<Compile Include="Packets\Session\HandshakeRequest.cs" />
<Compile Include="Packets\Session\HandshakeResponse.cs" />
Expand Down
23 changes: 23 additions & 0 deletions NebulaModel/Packets/Players/NewDroneOrderPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace NebulaModel.Packets.Players
{
public class NewDroneOrderPacket
{
public int PlanetId { get; set; }
public int DroneId { get; set; }
public int EntityId { get; set; }
public ushort PlayerId { get; set; }
public int Stage { get; set; }
public int Priority { get; set; }

public NewDroneOrderPacket() { }
public NewDroneOrderPacket(int planetId, int droneId, int entityId, ushort playerId, int stage, int priority)
{
PlanetId = planetId;
DroneId = droneId;
EntityId = entityId;
PlayerId = playerId;
Stage = stage;
Priority = priority;
}
}
}
14 changes: 14 additions & 0 deletions NebulaModel/Packets/Players/RemoveDroneOrdersPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace NebulaHost.PacketProcessors.Players
{
public class RemoveDroneOrdersPacket
{
public int[] QueuedEntityIds { get; set; }

public RemoveDroneOrdersPacket() { }

public RemoveDroneOrdersPacket(int[] queuedEntityIds)
{
QueuedEntityIds = queuedEntityIds;
}
}
}
15 changes: 15 additions & 0 deletions NebulaModel/Packets/Routers/PlanetBroadcastPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NebulaModel.Packets.Routers
{
public class PlanetBroadcastPacket
{
public byte[] PacketObject { get; set; }
public int PlanetId { get; set; }

public PlanetBroadcastPacket() { }
public PlanetBroadcastPacket(byte[] packetObject, int planetId)
{
PacketObject = packetObject;
PlanetId = planetId;
}
}
}
2 changes: 2 additions & 0 deletions NebulaPatcher/NebulaPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="Patches\Dynamic\GameScenarioLogic_Patch.cs" />
<Compile Include="Patches\Dynamic\GameStatData_Patch.cs" />
<Compile Include="Patches\Dynamic\PlanetData_Patch.cs" />
<Compile Include="Patches\Dynamic\Mecha_Patch.cs" />
<Compile Include="Patches\Dynamic\PlanetFactory_Patch.cs" />
<Compile Include="Patches\Dynamic\Debugging.cs" />
<Compile Include="Patches\Dynamic\ArriveLeavePlanet_Patch.cs" />
Expand Down Expand Up @@ -82,6 +83,7 @@
<Compile Include="Patches\Dynamic\UIStarmap_Patch.cs" />
<Compile Include="Patches\Dynamic\VFInput_Patch.cs" />
<Compile Include="Patches\Transpilers\PlanetFactory_Patch.cs" />
<Compile Include="Patches\Transpilers\MechaDroneLogic_Patch.cs" />
<Compile Include="Patches\Transpilers\PlayerAction_Build_Patch.cs" />
<Compile Include="Patches\Transpilers\PlayerAction_Mine_Patch.cs" />
<Compile Include="Patches\Dynamic\UIEscMenu_Patch.cs" />
Expand Down
15 changes: 13 additions & 2 deletions NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ private static void InitLandingPlace(GameData gameData, PlanetData planet)
gameData.mainPlayer.controller.velocityOnLanding = Vector3.zero;
}

[HarmonyPrefix]
[HarmonyPostfix]
[HarmonyPatch("OnDraw")]
public static void OnDraw_Prefix()
public static void OnDraw_Postfix()
{
if (SimulatedWorld.Initialized)
{
Expand Down Expand Up @@ -199,5 +199,16 @@ public static void ArriveStar_Prefix(GameData __instance, StarData star)
LocalPlayer.SendPacket(new PlayerUpdateLocalStarId(star.id));
}
}

[HarmonyPrefix]
[HarmonyPatch("LeavePlanet")]
public static void LeavePlanet_Prefix(GameData __instance)
{
//Players should clear the list of drone orders of other players when they leave the planet
if (SimulatedWorld.Initialized)
{
GameMain.mainPlayer.mecha.droneLogic.serving.Clear();
}
}
}
}
Loading

0 comments on commit 824e8cd

Please sign in to comment.