// tmp algo public static void GenVecDataFromChildren(Block45OctNode node) { Dictionary <int, BlockVec> vecData = new Dictionary <int, BlockVec>(); for (int i = 0; i < 8; i++) { if (node._children[i]._vecData == null && !node._children[i].IsLeaf) { GenVecDataFromChildren(node._children[i]); } List <BlockVec> vecDataChild = node._children[i]._vecData; if (vecDataChild == null) { continue; } int ofsxyz = BlockVec.ToXYZ(S_NearNodeOfs[i, 0] << Block45Constants._shift, S_NearNodeOfs[i, 1] << Block45Constants._shift, S_NearNodeOfs[i, 2] << Block45Constants._shift); int n = vecDataChild.Count; for (int j = 0; j < n; j++) { int xyz = ((vecDataChild[j]._xyz + ofsxyz) >> 1) & 0x7F7F7F; vecData[xyz] = new BlockVec(xyz, vecDataChild[j]._byte0, vecDataChild[j]._byte1); } } node._vecData = new List <BlockVec>(vecData.Values); node.FreeChkData(); }
public static BlockVec DirectionAsVec(Direction dir) { BlockVec outputVec = new BlockVec(0, 0, 0); switch (dir) { case Direction.XPos: outputVec = new BlockVec(1, 0, 0); break; case Direction.XNeg: outputVec = new BlockVec(-1, 0, 0); break; case Direction.YPos: outputVec = new BlockVec(0, 1, 0); break; case Direction.YNeg: outputVec = new BlockVec(0, -1, 0); break; case Direction.ZPos: outputVec = new BlockVec(0, 0, 1); break; case Direction.ZNeg: outputVec = new BlockVec(0, 0, -1); break; } return(outputVec); }
public BlockVec Sub(BlockVec addVec) { BlockVec outVec = new BlockVec(this.x, this.y, this.z); outVec.x -= addVec.x; outVec.y -= addVec.y; outVec.z -= addVec.z; return(outVec); }
public BlockVec Add(BlockVec addVec) { BlockVec outVec = new BlockVec(this.x, this.y, this.z); outVec.x += addVec.x; outVec.y += addVec.y; outVec.z += addVec.z; return(outVec); }
public void RemoveBlock(BlockVec pos) { Block targetBlock = GetBlockByPos(pos); if (targetBlock != null) { world.Remove(targetBlock); } }
public static Vector3[] createFace(Vector3 location, BlockVec.Direction dir) { Vector3 start = location; Vector3 end = location; switch (dir) { case BlockVec.Direction.XPos: case BlockVec.Direction.XNeg: BlockVec modifiedDirVecX = BlockVec.DirectionAsVec(dir); modifiedDirVecX.x -= 1; modifiedDirVecX.x = (int)(modifiedDirVecX.x * 0.5f); start += new Vector3(modifiedDirVecX.x, -1, -1); end += new Vector3(modifiedDirVecX.x, 0, 0); break; case BlockVec.Direction.YPos: case BlockVec.Direction.YNeg: BlockVec modifiedDirVecY = BlockVec.DirectionAsVec(dir); modifiedDirVecY.y -= 1; modifiedDirVecY.y = (int)(modifiedDirVecY.y * 0.5f); start += new Vector3(-1, modifiedDirVecY.y, -1); end += new Vector3(0, modifiedDirVecY.y, 0); break; case BlockVec.Direction.ZPos: case BlockVec.Direction.ZNeg: BlockVec modifiedDirVecZ = BlockVec.DirectionAsVec(dir); modifiedDirVecZ.z -= 1; modifiedDirVecZ.z = (int)(modifiedDirVecZ.z * 0.5f); start += new Vector3(-1, -1, modifiedDirVecZ.z); end += new Vector3(0, 0, modifiedDirVecZ.z); break; /* * case BlockVec.Direction.XPos: * case BlockVec.Direction.XNeg: * start += new Vector3(BlockVec.DirectionAsVec(dir).x, -1, -1); * end += new Vector3(BlockVec.DirectionAsVec(dir).x, 1, 1); * break; * case BlockVec.Direction.YPos: * case BlockVec.Direction.YNeg: * start += new Vector3(-1, BlockVec.DirectionAsVec(dir).y, -1); * end += new Vector3(1, BlockVec.DirectionAsVec(dir).y, 1); * break; * case BlockVec.Direction.ZPos: * case BlockVec.Direction.ZNeg: * start += new Vector3(-1, -1, BlockVec.DirectionAsVec(dir).z); * end += new Vector3(1, 1, BlockVec.DirectionAsVec(dir).z); * break; */ } return(createFace(start, end, dir)); }
public Block GetBlockByPos(BlockVec pos) { foreach (Block block in world) { if (pos.x == block.pos.x && pos.y == block.pos.y && pos.z == block.pos.z) { return(block); } } return(null); }
public byte[] BlockVecs2ByteArray() { byte[] chunkData = s_ChunkDataPool.Get(); Array.Clear(chunkData, 0, chunkData.Length); for (int i = 0; i < _vecData.Count; i++) { BlockVec bv = _vecData[i]; int idx = Block45Kernel.OneIndexNoPrefix(bv.x, bv.y, bv.z); chunkData[idx] = bv._byte0; chunkData[idx + 1] = bv._byte1; } return(chunkData); }
// append all children(inclusive) with lod recursively for savedata private static int AppendToWrite(Block45OctNode node, BinaryWriter bw, int lod) { int count = 0; if (node._pos.w == lod) { List <BlockVec> vecData = node.VecData; if (vecData != null) { IntVector4 blockUnitPos = Block45Constants.ToBlockUnitPos(node._pos.x, node._pos.y, node._pos.z, node._pos.w); int n = vecData.Count; for (int i = 0; i < n; i++) { BlockVec bv = vecData[i]; int x = bv.x - Block45Constants._numVoxelsPrefix; int y = bv.y - Block45Constants._numVoxelsPrefix; int z = bv.z - Block45Constants._numVoxelsPrefix; if (x < 0 || x >= Block45Constants._numVoxelsPerAxis || y < 0 || y >= Block45Constants._numVoxelsPerAxis || z < 0 || z >= Block45Constants._numVoxelsPerAxis) { continue; } bw.Write(blockUnitPos.x + x); bw.Write(blockUnitPos.y + y); bw.Write(blockUnitPos.z + z); bw.Write(bv._byte0); bw.Write(bv._byte1); count++; } } } else if (node._pos.w > lod && node._children != null) { for (int i = 0; i < 8; i++) { count += AppendToWrite(node._children[i], bw, lod); } } return(count); }
public B45Block Read(int x, int y, int z) { if (_vecData == null) { return(new B45Block()); } x += Block45Constants._numVoxelsPrefix; y += Block45Constants._numVoxelsPrefix; z += Block45Constants._numVoxelsPrefix; for (int i = 0; i < _vecData.Count; i++) { BlockVec bv = _vecData[i]; if (x == bv.x && y == bv.y && z == bv.z) { return(new B45Block(bv._byte0, bv._byte1)); } } return(new B45Block()); }
public List <BlockVec.Direction> GetEmptyAround(BlockVec pos) { List <BlockVec.Direction> outDirs = new List <BlockVec.Direction>(); foreach (BlockVec.Direction dir in Enum.GetValues(typeof(BlockVec.Direction))) { BlockVec checkPos = pos.Add(BlockVec.DirectionAsVec(dir)); /* * print(pos.x + " " + pos.y + " " + pos.z); * print(checkPos.x + " " + checkPos.y + " " + checkPos.z); * print(BlockVec.DirectionAsVec(dir).x + " " + BlockVec.DirectionAsVec(dir).y + " " + BlockVec.DirectionAsVec(dir).z); */ if (GetBlockByPos(checkPos) == null)//when fixed, also check isInRange(checkPos, startChunkPos, endChunkPos) { outDirs.Add(dir); } } return(outDirs); }
public void Write(int x, int y, int z, byte b0, byte b1) { x += Block45Constants._numVoxelsPrefix; y += Block45Constants._numVoxelsPrefix; z += Block45Constants._numVoxelsPrefix; if (_vecData != null) { int i = 0; for ( ; i < _vecData.Count; i++) { BlockVec bv = _vecData[i]; if (x == bv.x && y == bv.y && z == bv.z) { if (b0 == 0) { _vecData.RemoveAt(i); } else { bv._byte0 = b0; bv._byte1 = b1; _vecData[i] = bv; } break; } } if (i >= _vecData.Count && b0 != 0) { _vecData.Add(new BlockVec(x, y, z, b0, b1)); } _stamp++; WriteToByteArray(x, y, z, b0, b1); } else if (b0 != 0) { _vecData = new List <BlockVec>(); _vecData.Add(new BlockVec(x, y, z, b0, b1)); _stamp++; WriteToByteArray(x, y, z, b0, b1); } }
public void PlaceBlocks(Block blockAndStartPos, BlockVec endPos, bool replace) { int startX = Math.Min(blockAndStartPos.pos.x, endPos.x); int endX = Math.Max(blockAndStartPos.pos.x, endPos.x); int startY = Math.Min(blockAndStartPos.pos.y, endPos.y); int endY = Math.Max(blockAndStartPos.pos.y, endPos.y); int startZ = Math.Min(blockAndStartPos.pos.z, endPos.z); int endZ = Math.Max(blockAndStartPos.pos.z, endPos.z); for (int x = startX; x < endX; x++) { for (int y = startY; y < endY; y++) { for (int z = startZ; z < endZ; z++) { Block blockToPlace = (Block)blockAndStartPos.Clone(); blockToPlace.pos = new BlockVec(x, y, z); AddBlock(blockToPlace, replace); } } } }
public Block(BlockVec pos) { this.pos = pos; }
public bool isInRange(BlockVec checkPos, Vector2 startPos, Vector2 endPos) { return(checkPos.x > startPos.x && checkPos.x < endPos.x && checkPos.z > startPos.y && checkPos.z < endPos.y); }