public static WorldSectionData SaveWorldSection(int x, int y, int x2, int y2) { var width = x2 - x + 1; var height = y2 - y + 1; var data = new WorldSectionData(width, height) { X = x, Y = y, Chests = new List <WorldSectionData.ChestData>(), Signs = new List <WorldSectionData.SignData>(), ItemFrames = new List <WorldSectionData.ItemFrameData>(), LogicSensors = new List <WorldSectionData.LogicSensorData>(), TrainingDummies = new List <WorldSectionData.TrainingDummyData>() }; for (var i = x; i <= x2; i++) { for (var j = y; j <= y2; j++) { data.ProcessTile(Main.tile[i, j], i - x, j - y); } } return(data); }
public static WorldSectionData LoadWorldData(string path) { using (var reader = new BinaryReader( new BufferedStream( new GZipStream(File.Open(path, FileMode.Open), CompressionMode.Decompress), BUFFER_SIZE))) { var x = reader.ReadInt32(); var y = reader.ReadInt32(); var width = reader.ReadInt32(); var height = reader.ReadInt32(); var worldData = new WorldSectionData(width, height) { X = x, Y = y }; for (var i = 0; i < width; i++) { for (var j = 0; j < height; j++) { worldData.Tiles[i, j] = reader.ReadTile(); } } try { var signCount = reader.ReadInt32(); worldData.Signs = new WorldSectionData.SignData[signCount]; for (var i = 0; i < signCount; i++) { worldData.Signs[i] = reader.ReadSign(); } var chestCount = reader.ReadInt32(); worldData.Chests = new WorldSectionData.ChestData[chestCount]; for (var i = 0; i < chestCount; i++) { worldData.Chests[i] = reader.ReadChest(); } var itemFrameCount = reader.ReadInt32(); worldData.ItemFrames = new WorldSectionData.ItemFrameData[itemFrameCount]; for (var i = 0; i < itemFrameCount; i++) { worldData.ItemFrames[i] = reader.ReadItemFrame(); } return(worldData); } catch (EndOfStreamException) // old version file { return(worldData); } } }
public static void LoadWorldSection(WorldSectionData Data, int?X = null, int?Y = null, bool Tiles = true) { int x = (X ?? Data.X), y = (Y ?? Data.Y); if (Tiles) { for (var i = 0; i < Data.Width; i++) { for (var j = 0; j < Data.Height; j++) { int _x = i + x, _y = j + y; if (!InMapBoundaries(_x, _y)) { continue; } Main.tile[_x, _y] = Data.Tiles[i, j]; Main.tile[_x, _y].skipLiquid(true); } } } ClearObjects(x, y, x + Data.Width, y + Data.Height); foreach (var sign in Data.Signs) { var id = Sign.ReadSign(sign.X + x, sign.Y + y); if ((id == -1) || !InMapBoundaries(sign.X, sign.Y)) { continue; } Sign.TextSign(id, sign.Text); } foreach (var itemFrame in Data.ItemFrames) { var id = TEItemFrame.Place(itemFrame.X + x, itemFrame.Y + y); if (id == -1) { continue; } var frame = (TEItemFrame)TileEntity.ByID[id]; if (!InMapBoundaries(frame.Position.X, frame.Position.Y)) { continue; } frame.item = new Item(); frame.item.netDefaults(itemFrame.Item.NetId); frame.item.stack = itemFrame.Item.Stack; frame.item.prefix = itemFrame.Item.PrefixId; } foreach (var chest in Data.Chests) { int chestX = chest.X + x, chestY = chest.Y + y; int id; if ((id = Chest.FindChest(chestX, chestY)) == -1 && (id = Chest.CreateChest(chestX, chestY)) == -1) { continue; } Chest _chest = Main.chest[id]; if (!InMapBoundaries(chest.X, chest.Y)) { continue; } for (var index = 0; index < chest.Items.Length; index++) { var netItem = chest.Items[index]; var item = new Item(); item.netDefaults(netItem.NetId); item.stack = netItem.Stack; item.prefix = netItem.PrefixId; Main.chest[id].item[index] = item; } } foreach (var logicSensor in Data.LogicSensors) { var id = TELogicSensor.Place(logicSensor.X + x, logicSensor.Y + y); if (id == -1) { continue; } var sensor = (TELogicSensor)TileEntity.ByID[id]; if (!InMapBoundaries(sensor.Position.X, sensor.Position.Y)) { continue; } sensor.logicCheck = logicSensor.Type; } foreach (var trainingDummy in Data.TrainingDummies) { var id = TETrainingDummy.Place(trainingDummy.X + x, trainingDummy.Y + y); if (id == -1) { continue; } var dummy = (TETrainingDummy)TileEntity.ByID[id]; if (!InMapBoundaries(dummy.Position.X, dummy.Position.Y)) { continue; } dummy.npc = -1; } ResetSection(x, y, x + Data.Width, y + Data.Height); }
public static WorldSectionData LoadWorldData(Stream stream) { using (var reader = new BinaryReader(new BufferedStream(new GZipStream(stream, CompressionMode.Decompress), BUFFER_SIZE))) { var x = reader.ReadInt32(); var y = reader.ReadInt32(); var width = reader.ReadInt32(); var height = reader.ReadInt32(); var worldData = new WorldSectionData(width, height) { X = x, Y = y }; for (var i = 0; i < width; i++) { for (var j = 0; j < height; j++) { worldData.Tiles[i, j] = reader.ReadTile(); } } try { var signCount = reader.ReadInt32(); worldData.Signs = new WorldSectionData.SignData[signCount]; for (var i = 0; i < signCount; i++) { worldData.Signs[i] = reader.ReadSign(); } var chestCount = reader.ReadInt32(); worldData.Chests = new WorldSectionData.ChestData[chestCount]; for (var i = 0; i < chestCount; i++) { worldData.Chests[i] = reader.ReadChest(); } var itemFrameCount = reader.ReadInt32(); worldData.ItemFrames = new WorldSectionData.ItemFrameData[itemFrameCount]; for (var i = 0; i < itemFrameCount; i++) { worldData.ItemFrames[i] = reader.ReadItemFrame(); } } catch (EndOfStreamException) // old version file { } try { var logicSensorCount = reader.ReadInt32(); worldData.LogicSensors = new WorldSectionData.LogicSensorData[logicSensorCount]; for (var i = 0; i < logicSensorCount; i++) { worldData.LogicSensors[i] = reader.ReadLogicSensor(); } var trainingDummyCount = reader.ReadInt32(); worldData.TrainingDummies = new WorldSectionData.TrainingDummyData[trainingDummyCount]; for (var i = 0; i < trainingDummyCount; i++) { worldData.TrainingDummies[i] = reader.ReadTrainingDummy(); } } catch (EndOfStreamException) // old version file { } return(worldData); } }