public GridBlockCollection GetBlocksAroundLine(Vector3 startPoint, Vector3 endPoint) { GridBlockCollection gridBlockCollection = new GridBlockCollection(false); Vector3 currentPoint = startPoint; Vector3 direction = Vector3.Normalize(endPoint - startPoint); Single originalDistance = Vector3.Distance(currentPoint, endPoint); while (Vector3.Distance(startPoint, currentPoint) < originalDistance) { GridBlock block = GetBlockByLocation(currentPoint.X, currentPoint.Y); if (block != null && !gridBlockCollection.Contains(block)) { gridBlockCollection.Add(block); } currentPoint += direction; } for (Int32 i = gridBlockCollection.Count - 1; i >= 0; i--) { GridBlock block = gridBlockCollection[i]; if (block == null) { continue; } if (block.ContainerBox == null) { continue; } if (block.ContainerBox.LineInBox(startPoint, endPoint)) { continue; } gridBlockCollection.RemoveAt(i); } return(gridBlockCollection); }
private void LoadGrid(Boolean isServer) { using (FileStream gridBuffer = new FileStream(GridFilename, FileMode.Open, FileAccess.Read)) { for (Int32 i = 1; i <= 16384; i++) { Int32 pos = 0; Int32 blockX = (Int32)System.Math.Floor((i - 1f) % 128f) * 64; Int32 blockY = (Int32)System.Math.Floor((i - 1f) / 128f) * 64; Byte[] gridBytes = new Byte[38]; gridBuffer.Read(gridBytes, 0, 38); GridBlock block = new GridBlock(i, blockX, blockY) { Unknown0 = BitConverter.ToInt16(gridBytes, 0), LowBoxTopMod = BitConverter.ToInt16(gridBytes, pos += 2), LowSidesTextureId = BitConverter.ToInt16(gridBytes, pos += 2), LowTopTextureId = BitConverter.ToInt16(gridBytes, pos += 2), HighTextureId = BitConverter.ToInt16(gridBytes, pos += 2), LowBoxTopZ = BitConverter.ToInt16(gridBytes, pos += 2), MidBoxBottomZ = BitConverter.ToInt16(gridBytes, pos += 2), MidBoxTopZ = BitConverter.ToInt16(gridBytes, pos += 2), LowTileId = BitConverter.ToInt16(gridBytes, pos += 2), HighBoxBottomZ = BitConverter.ToInt16(gridBytes, pos += 2), BlockFlags = BitConverter.ToInt16(gridBytes, pos += 2), LowTopShape = (GridBlockShape)BitConverter.ToInt16(gridBytes, pos += 2), MidBottomShape = (GridBlockShape)BitConverter.ToInt16(gridBytes, pos += 2), MidSidesTextureId = BitConverter.ToInt16(gridBytes, pos += 2), MidTopTextureId = BitConverter.ToInt16(gridBytes, pos += 2), CeilingTextureId = BitConverter.ToInt16(gridBytes, pos += 2), Unknown16 = BitConverter.ToInt16(gridBytes, pos += 2), Unknown17 = BitConverter.ToInt16(gridBytes, pos += 2), Unknown18 = BitConverter.ToInt16(gridBytes, pos) }; if (isServer) { if (block.LowBoxTopMod != 0) { GridBlocks[i - 1].LowBoxTopZ -= block.LowBoxTopMod; } } Int32 locX1 = (Int32)System.Math.Floor((i - 1f) % 128f) * 64; Int32 locY1 = (Int32)System.Math.Floor((i - 1f) / 128f) * 64; block.ContainerBox = new OrientedBoundingBox(new Vector3(locX1, locY1, -1024), new Vector3(64, 64, 2048), 0.0f); block.LowBox = new OrientedBoundingBox(new Vector3(locX1, locY1, -1024), new Vector3(64, 64, 1024 + block.LowBoxTopZ), 0.0f); block.MidBox = new OrientedBoundingBox(new Vector3(locX1, locY1, block.MidBoxBottomZ), new Vector3(64, 64, block.MidBoxTopZ - block.MidBoxBottomZ), 0.0f); if (block.HasSkybox) { block.HighBox = new OrientedBoundingBox(new Vector3(locX1, locY1, block.HighBoxBottomZ + 17), new Vector3(64, 64, block.MidBoxTopZ - block.HighBoxBottomZ), 0.0f); } else { block.HighBox = new OrientedBoundingBox(new Vector3(locX1, locY1, block.HighBoxBottomZ + 17), new Vector3(64, 64, block.MidBoxTopZ), 0.0f); } if (block.LowTileId > 0) { block.LowBoxTile = new Tile(block.LowTileId); foreach (TileBlock tileBlock in Tiles[block.LowTileId].TileBlocks) { block.LowBoxTile.TileBlocks.Add(new TileBlock(tileBlock.TopHeight, tileBlock.BottomHeight, tileBlock.Index)); } for (Int32 y = 0; y < 8; y++) { for (Int32 x = 0; x < 8; x++) { TileBlock tileBlock = block.LowBoxTile.TileBlocks[(y * 8) + x]; Single x1 = 0f, y1 = 0f, z1 = 0f; if (tileBlock.BottomHeight > 0 || tileBlock.TopHeight > 0) { x1 = block.LowBox.Location.X + (8 * x); y1 = block.LowBox.Location.Y + (8 * y); z1 = block.LowBoxTopZ; } if (tileBlock.TopHeight > 0) { tileBlock.TopBoundingBox = new OrientedBoundingBox(new Vector3(x1, y1, z1 - tileBlock.TopHeight + 128), new Vector3(8f, 8f, tileBlock.TopHeight), 0); } if (tileBlock.BottomHeight > 0) { tileBlock.BottomBoundingBox = new OrientedBoundingBox(new Vector3(x1, y1, z1), new Vector3(8f, 8f, tileBlock.BottomHeight), 0); } } } } GridBlocks.Add(block); } gridBuffer.Seek(4, SeekOrigin.Current); for (Int32 i = 1; i <= 997; i++) { Int32 pos = 0; Byte[] gridBytes = new Byte[16]; gridBuffer.Read(gridBytes, 0, 16); GridObject obj = new GridObject { ObjectId = BitConverter.ToInt32(gridBytes, 0), X = BitConverter.ToInt32(gridBytes, pos += 4), Y = BitConverter.ToInt32(gridBytes, pos += 4), Z = BitConverter.ToInt32(gridBytes, pos + 4) }; GridObjects.Add(obj); } } }