public static bool BuildFinally_Prefix(PlanetFactory __instance, Player player, int prebuildId) { if (!SimulatedWorld.Initialized) { return(true); } if (LocalPlayer.IsMasterClient) { if (!FactoryManager.ContainsPrebuildRequest(__instance.planetId, prebuildId)) { // This prevents duplicating the entity when multiple players trigger the BuildFinally for the same entity at the same time. // If it occurs in any other circumstances, it means that we have some desynchronization between clients and host prebuilds buffers. Log.Warn($"BuildFinally was called without having a corresponding PrebuildRequest for the prebuild {prebuildId} on the planet {__instance.planetId}"); return(false); } // Remove the prebuild request from the list since we will now convert it to a real building FactoryManager.RemovePrebuildRequest(__instance.planetId, prebuildId); } if (LocalPlayer.IsMasterClient || !FactoryManager.EventFromServer) { LocalPlayer.SendPacket(new BuildEntityRequest(__instance.planetId, prebuildId)); } return(LocalPlayer.IsMasterClient || FactoryManager.EventFromServer); }
public static bool DestructFinally_Prefix(PlanetFactory __instance, Player player, int objId, ref int protoId) { if (!SimulatedWorld.Initialized) { return(true); } // TODO: handle if 2 clients or if host and client trigger a destruct of the same object at the same time // If the object is a prebuild, remove it from the prebuild request list if (LocalPlayer.IsMasterClient && objId < 0) { if (!FactoryManager.ContainsPrebuildRequest(__instance.planetId, -objId)) { Log.Warn($"DestructFinally was called without having a corresponding PrebuildRequest for the prebuild {-objId} on the planet {__instance.planetId}"); return(false); } FactoryManager.RemovePrebuildRequest(__instance.planetId, -objId); } if (LocalPlayer.IsMasterClient || !FactoryManager.EventFromServer) { LocalPlayer.SendPacket(new DestructEntityRequest(__instance.planetId, objId, FactoryManager.PacketAuthor == -1 ? LocalPlayer.PlayerId : FactoryManager.PacketAuthor)); } return(LocalPlayer.IsMasterClient || FactoryManager.EventFromServer); }
public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn) { if (!FactoryManager.ContainsPrebuildRequest(packet.PlanetId, packet.PrebuildId)) { Log.Warn($"BuildEntityRequest received does not have a corresponding PrebuildRequest with the id {packet.PrebuildId} for the planet {packet.PlanetId}"); return; } using (FactoryManager.EventFromClient.On()) { PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId); FactoryManager.EventFactory = planet.factory; FactoryManager.PacketAuthor = packet.AuthorId; FactoryManager.TargetPlanet = packet.PlanetId; FactoryManager.AddPlanetTimer(packet.PlanetId); //Remove building from drone queue GameMain.mainPlayer.mecha.droneLogic.serving.Remove(-packet.PrebuildId); planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId); FactoryManager.EventFactory = null; FactoryManager.PacketAuthor = -1; FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE; } }
public static bool DoDismantleObject_Prefix(PlayerAction_Build __instance, int objId) { if (!SimulatedWorld.Initialized) { return(true); } int planetId = FactoryManager.TargetPlanet != FactoryManager.PLANET_NONE ? FactoryManager.TargetPlanet : __instance.planet?.id ?? -1; // TODO: handle if 2 clients or if host and client trigger a destruct of the same object at the same time // If the object is a prebuild, remove it from the prebuild request list if (LocalPlayer.IsMasterClient && objId < 0) { if (!FactoryManager.ContainsPrebuildRequest(planetId, -objId)) { Log.Warn($"DestructFinally was called without having a corresponding PrebuildRequest for the prebuild {-objId} on the planet {planetId}"); return(false); } FactoryManager.RemovePrebuildRequest(planetId, -objId); } if (LocalPlayer.IsMasterClient || !FactoryManager.IsIncomingRequest) { LocalPlayer.SendPacket(new DestructEntityRequest(planetId, objId, FactoryManager.PacketAuthor == FactoryManager.AUTHOR_NONE ? LocalPlayer.PlayerId : FactoryManager.PacketAuthor)); } return(LocalPlayer.IsMasterClient || FactoryManager.IsIncomingRequest); }
public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn) { if (!FactoryManager.ContainsPrebuildRequest(packet.PlanetId, packet.PrebuildId)) { Log.Warn($"BuildEntityRequest received does not have a corresponding PrebuildRequest with the id {packet.PrebuildId} for the planet {packet.PlanetId}"); return; } using (FactoryManager.EventFromClient.On()) { PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId); FactoryManager.EventFactory = planet.factory; FactoryManager.PacketAuthor = packet.AuthorId; // Physics could be null, if the host is not on the requested planet // Make sure to init all the planet data required to perform the BuildFinally of the distant planet if (packet.PlanetId != GameMain.localPlanet?.id) { planet.physics = new PlanetPhysics(planet); planet.physics.Init(); planet.audio = new PlanetAudio(planet); planet.audio.Init(); if (AccessTools.Field(typeof(CargoTraffic), "beltRenderingBatch").GetValue(planet.factory.cargoTraffic) == null) { planet.factory.cargoTraffic.CreateRenderingBatches(); } } //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 if (packet.PlanetId != GameMain.localPlanet?.id) { planet.physics.Free(); planet.physics = null; planet.audio.Free(); planet.audio = null; } FactoryManager.EventFactory = null; FactoryManager.PacketAuthor = -1; } }
public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn) { if (!FactoryManager.ContainsPrebuildRequest(packet.PlanetId, packet.PrebuildId)) { Log.Warn($"BuildEntityRequest received does not have a corresponding PrebuildRequest with the id {packet.PrebuildId} for the planet {packet.PlanetId}"); return; } FactoryManager.EventFromClient = true; PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId); // Physics could be null, if the host is not on the requested planet // Make sure to init all the planet data required to perform the BuildFinally of the distant planet if (packet.PlanetId != GameMain.localPlanet?.id) { planet.physics = new PlanetPhysics(planet); planet.physics.Init(); planet.audio = new PlanetAudio(planet); planet.audio.Init(); // TODO: We also need to handle the FlattenTerrain issue here. } 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 if (packet.PlanetId != GameMain.localPlanet?.id) { planet.physics.Free(); planet.physics = null; planet.audio.Free(); planet.audio = null; } FactoryManager.EventFromClient = false; }
public override void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn) { if (IsHost && !FactoryManager.ContainsPrebuildRequest(packet.PlanetId, packet.PrebuildId)) { Log.Warn($"BuildEntityRequest received does not have a corresponding PrebuildRequest with the id {packet.PrebuildId} for the planet {packet.PlanetId}"); return; } PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId); // We only execute the code if the client has loaded the factory at least once. // Else it will get it once it goes to the planet for the first time. if (planet.factory != null) { using (FactoryManager.IsIncomingRequest.On()) { FactoryManager.EventFactory = planet.factory; FactoryManager.TargetPlanet = packet.PlanetId; FactoryManager.PacketAuthor = packet.AuthorId; FactoryManager.AddPlanetTimer(packet.PlanetId); //Remove building from drone queue GameMain.mainPlayer.mecha.droneLogic.serving.Remove(-packet.PrebuildId); planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId); if (IsClient) { DroneManager.RemoveBuildRequest(-packet.PrebuildId); } FactoryManager.EventFactory = null; FactoryManager.PacketAuthor = FactoryManager.AUTHOR_NONE; FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE; } } }