public Fixture[] GetAllFixtures() { List <Fixture> fixtures = new List <Fixture>(); foreach (TileFixtureLayers layer in TileDefinition.GetTileFixtureLayerNames()) { fixtures.Add(GetTileFixtureAtLayer(layer)); } foreach (WallFixtureLayers layer in TileDefinition.GetWallFixtureLayerNames()) { fixtures.Add(GetWallFixtureAtLayer(layer)); } foreach (FloorFixtureLayers layer in TileDefinition.GetFloorFixtureLayerNames()) { fixtures.Add(GetFloorFixtureAtLayer(layer)); } return(fixtures.ToArray()); }
public static TileDefinition ReadNetworkableTileDefinition(this NetworkReader reader) { TileDefinition tileDefinition = new TileDefinition(); tileDefinition.fixtures = new FixturesContainer(); // Read plenum string plenumName = reader.ReadString(); if (!string.IsNullOrEmpty(plenumName)) { tileDefinition.plenum = plenums.FirstOrDefault(plenum => plenum.name == plenumName); if (tileDefinition.plenum == null) { Debug.LogError($"Network recieved plenum with name {plenumName} could not be found"); } } // Read turf string turfName = reader.ReadString(); if (!string.IsNullOrEmpty(turfName)) { tileDefinition.turf = turfs.FirstOrDefault(turf => turf.name == turfName); if (tileDefinition.turf == null) { Debug.LogError($"Network recieved turf with name {turfName} could not be found"); } } // Read tile fixtures foreach (TileFixtureLayers layer in TileDefinition.GetTileFixtureLayerNames()) { string fixtureName = reader.ReadString(); if (!string.IsNullOrEmpty(fixtureName)) { TileFixture tf = (TileFixture)fixtures.FirstOrDefault(fixture => fixture.name == fixtureName); tileDefinition.fixtures.SetTileFixtureAtLayer(tf, layer); if (tf == null) { Debug.LogError($"Network recieved fixture with name {fixtureName} could not be found"); } } } // Read wall fixtures foreach (WallFixtureLayers layer in TileDefinition.GetWallFixtureLayerNames()) { string fixtureName = reader.ReadString(); if (!string.IsNullOrEmpty(fixtureName)) { WallFixture wf = (WallFixture)fixtures.FirstOrDefault(fixture => fixture.name == fixtureName); tileDefinition.fixtures.SetWallFixtureAtLayer(wf, layer); if (wf == null) { Debug.LogError($"Network recieved fixture with name {fixtureName} could not be found"); } } } // Read floor fixtures foreach (FloorFixtureLayers layer in TileDefinition.GetFloorFixtureLayerNames()) { string fixtureName = reader.ReadString(); if (!string.IsNullOrEmpty(fixtureName)) { FloorFixture ff = (FloorFixture)fixtures.FirstOrDefault(fixture => fixture.name == fixtureName); tileDefinition.fixtures.SetFloorFixtureAtLayer(ff, layer); if (ff == null) { Debug.LogError($"Network recieved fixture with name {fixtureName} could not be found"); } } } // If the boolean is false, subStates should be null. if (reader.ReadBoolean()) { using (var stream = new MemoryStream(reader.ReadBytesAndSize())) { tileDefinition.subStates = new BinaryFormatter().Deserialize(stream) as object[]; } } // TODO: Should substates be initialized to null array? return(tileDefinition); }
// TODO: This could be made more efficient. Especially when the subStates are all nulls. public static void WriteNetworkableTileDefinition(this NetworkWriter writer, TileDefinition definition) { // Write plenum writer.WriteString(definition.plenum?.name ?? ""); // Write turf writer.WriteString(definition.turf?.name ?? ""); // Write all tile fixtures foreach (TileFixtureLayers layer in TileDefinition.GetTileFixtureLayerNames()) { Fixture f = definition.fixtures.GetTileFixtureAtLayer(layer); if (f) { writer.WriteString(f.name ?? ""); } else { writer.WriteString(""); } } // Write all wall fixtures foreach (WallFixtureLayers layer in TileDefinition.GetWallFixtureLayerNames()) { Fixture f = definition.fixtures.GetWallFixtureAtLayer(layer); if (f) { writer.WriteString(f.name ?? ""); } else { writer.WriteString(""); } } // Write all floor fixtures foreach (FloorFixtureLayers layer in TileDefinition.GetFloorFixtureLayerNames()) { Fixture f = definition.fixtures.GetFloorFixtureAtLayer(layer); if (f) { writer.WriteString(f.name ?? ""); } else { writer.WriteString(""); } } // Use C# serializer to serialize the object array, cos the Mirror one isn't powerful enough. // Can't serialize null values so put a boolean indicating array presence first if (definition.subStates == null || definition.subStates.All(obj => obj == null)) { writer.WriteBoolean(false); } else { writer.WriteBoolean(true); using (var stream = new MemoryStream()) { new BinaryFormatter().Serialize(stream, definition.subStates); writer.WriteBytesAndSize(stream.ToArray()); } } }
// Remove the selection if the option is impossible // For example: tables cannot be build in walls, or wall fixtures cannot be build on floors public static TileDefinition ValidateFixtures(TileDefinition tileDefinition) { bool altered = false; string reason = ""; // If lattice, remove tile fixtures if (tileDefinition.plenum.name.Contains("Lattice")) { foreach (TileFixtureLayers layer in TileDefinition.GetTileFixtureLayerNames()) { if (tileDefinition.fixtures.GetTileFixtureAtLayer(layer) != null) { altered = true; tileDefinition.fixtures.SetTileFixtureAtLayer(null, layer); } } if (altered) { reason += "Lattices do not support any wall/floor fixture.\n"; } } if (((tileDefinition.turf != null && tileDefinition.turf.isWall) || tileDefinition.turf == null) && tileDefinition.plenum.name.Contains("Lattice")) { // Remove floor fixtures foreach (FloorFixtureLayers layer in TileDefinition.GetFloorFixtureLayerNames()) { if (tileDefinition.fixtures.GetFloorFixtureAtLayer(layer) != null) { altered = true; reason += "Cannot set a floor fixture on lattice.\n"; Debug.Log("Cannot set a floor fixture on lattice"); tileDefinition.fixtures.SetFloorFixtureAtLayer(null, layer); } } } if ((tileDefinition.turf != null && !tileDefinition.turf.isWall) || tileDefinition.turf == null || tileDefinition.plenum.name.Contains("Lattice")) { // Remove wall fixtures foreach (WallFixtureLayers layer in TileDefinition.GetWallFixtureLayerNames()) { if (tileDefinition.fixtures.GetWallFixtureAtLayer(layer) != null) { altered = true; reason += "Cannot set a wall fixture when there is no wall.\n"; Debug.Log("Cannot set a wall fixture when there is no wall"); } tileDefinition.fixtures.SetWallFixtureAtLayer(null, layer); } } // Prevent low wall mounts on glass walls and reinforced glass walls if (tileDefinition.turf != null && tileDefinition.turf.isWall && tileDefinition.turf.name.Contains("GlassWall")) { foreach (WallFixtureLayers layer in TileDefinition.GetWallFixtureLayerNames()) { if (layer == WallFixtureLayers.LowWallNorth || layer == WallFixtureLayers.LowWallEast || layer == WallFixtureLayers.LowWallSouth || layer == WallFixtureLayers.LowWallWest) { if (tileDefinition.fixtures.GetWallFixtureAtLayer(layer) != null) { altered = true; reason += "Glass walls do not allow low wall fixtures.\n"; tileDefinition.fixtures.SetWallFixtureAtLayer(null, layer); } } } } // Restrict pipes to their own layer TileFixture pipe = tileDefinition.fixtures.GetTileFixtureAtLayer(TileFixtureLayers.Pipe1); if (pipe != null && !pipe.name.Contains("1")) { altered = true; tileDefinition.fixtures.SetTileFixtureAtLayer(null, TileFixtureLayers.Pipe1); } pipe = tileDefinition.fixtures.GetTileFixtureAtLayer(TileFixtureLayers.Pipe2); if (pipe != null && !pipe.name.Contains("2")) { altered = true; tileDefinition.fixtures.SetTileFixtureAtLayer(null, TileFixtureLayers.Pipe2); } pipe = tileDefinition.fixtures.GetTileFixtureAtLayer(TileFixtureLayers.Pipe3); if (pipe != null && !pipe.name.Contains("3")) { altered = true; tileDefinition.fixtures.SetTileFixtureAtLayer(null, TileFixtureLayers.Pipe3); } #if UNITY_EDITOR if (altered) { EditorUtility.DisplayDialog("Fixture combination", "Invalid because of the following: \n\n" + reason + "\n" + "Definition has been reset.", "ok"); } #endif return(tileDefinition); }