예제 #1
0
        public override void OnBlockPlaced(IWorldAccessor world, BlockPos blockPos, ref EnumHandling handled)
        {
            testBlockPos.Clear();
            lworld = world;
            if (world.Side != EnumAppSide.Server)
            {
                return;
            }
            BlockPos pos = blockPos;

            targetPos = world.GetNearestEntity(pos.ToVec3d(), 64, 64).Pos.AsBlockPos;
            Vec3d    posv = pos.ToVec3d();
            Vec3d    tgtv = targetPos.ToVec3d();
            BlockPos mP   = ((posv.Add(tgtv)) / 2).AsBlockPos;
            int      sR   = (int)mP.DistanceTo(pos) + 4;
            var      grid = new CubeGrid(sR * 2, sR * 2, sR * 2);

            if (!grid.isInBounds(world, targetPos) || !grid.isPassable(world, targetPos) || !grid.isWalkable(world, targetPos))
            {
                return;
            }
            for (int x = mP.X - sR; x <= mP.X + sR; x++)
            {
                for (int y = mP.Y - sR; y <= mP.Y + sR; y++)
                {
                    for (int z = mP.Z - sR; z <= mP.Z + sR; z++)
                    {
                        BlockPos currentPos = new BlockPos(x, y, z);
                        if (grid.isInBounds(world, currentPos) && grid.isPassable(world, currentPos) && grid.isWalkable(world, currentPos))
                        {
                            grid.air.Add(currentPos);
                        }
                        else
                        {
                            grid.walls.Add(currentPos);
                        }
                    }
                }
            }
            var astar = new AStarSearch(world, grid, pos, targetPos);

            DrawGrid(world, astar, mP, sR);
            world.BlockAccessor.SetBlock(2278, pos);
            //world.BlockAccessor.SetBlock(2279, mP);
            world.BlockAccessor.SetBlock(2278, targetPos);
            testBlockPos.Add(targetPos);
            testBlockPos.Add(pos);
            //testBlockPos.Add(mP);
            return;
        }
예제 #2
0
        public override void OnBlockPlaced(IWorldAccessor world, BlockPos blockPos, ref EnumHandling handled)
        {
            if (world.Side != EnumAppSide.Server)
            {
                return;
            }
            pastCost.Clear();
            costPos.Clear();
            BlockPos pos = blockPos;
            int      sD  = 16;

            targetPos = pos + new BlockPos(-2, 0, 8);
            int sR   = sD / 2;
            var grid = new CubeGrid(sD, sD, sD);

            if (!isInBounds(world, targetPos) || !isPassable(world, targetPos) || !isWalkable(world, targetPos) || isDangerous(world, targetPos))
            {
                return;
            }
            for (int x = pos.X - sR; x <= pos.X + sR; x++)
            {
                for (int y = pos.Y - sR; y <= pos.Y + sR; y++)
                {
                    for (int z = pos.Z - sR; z <= pos.Z + sR; z++)
                    {
                        BlockPos currentPos = new BlockPos(x, y, z);
                        if (isInBounds(world, currentPos) && isPassable(world, currentPos) && isWalkable(world, currentPos) && !isDangerous(world, currentPos))
                        {
                            costPos.Add(currentPos);
                            pastCost.Add(currentPos, currentPos.ManhattenDistance(targetPos));
                            grid.air.Add(currentPos);
                        }
                        else
                        {
                            grid.walls.Add(currentPos);
                        }
                    }
                }
            }
            var astar = new AStarSearch(world, grid, pastCost, pos, targetPos);

            DrawGrid(world, astar, pos, sR);
            world.BlockAccessor.SetBlock(1, pos);
            world.BlockAccessor.SetBlock(903, targetPos);
            return;
        }
예제 #3
0
 private void DrawGrid(IWorldAccessor world, AStarSearch astar, BlockPos pos, int sR)
 {
     for (int x = pos.X - sR; x < pos.X + sR; x++)
     {
         for (int y = pos.Y - sR; y < pos.Y + sR; y++)
         {
             for (int z = pos.Z - sR; z < pos.Z + sR; z++)
             {
                 BlockPos id = new BlockPos(x, y, z);
                 if (!astar.cameFrom.TryGetValue(id, out BlockPos ptr))
                 {
                     ptr = id;
                 }
                 else
                 {
                     world.BlockAccessor.SetBlock(2277, ptr);
                     testBlockPos.Add(ptr);
                 }
             }
         }
     }
     listenerId = world.RegisterGameTickListener(Ticker, 2500);
 }
예제 #4
0
 public List <BlockPos> CurrentPath(IWorldAccessor world, AStarSearch astar, BlockPos pos, int sR)
 {
     currentPath.Clear();
     for (int x = pos.X - sR; x < pos.X + sR; x++)
     {
         for (int y = pos.Y - sR; y < pos.Y + sR; y++)
         {
             for (int z = pos.Z - sR; z < pos.Z + sR; z++)
             {
                 BlockPos id = new BlockPos(x, y, z);
                 if (!astar.cameFrom.TryGetValue(id, out BlockPos ptr))
                 {
                     ptr = id;
                 }
                 else
                 {
                     currentPath.Add(ptr);
                 }
             }
         }
     }
     return(currentPath);
 }
예제 #5
0
        public List <BlockPos> MakeGrid(IWorldAccessor world, BlockPos targetPos, BlockPos startPos)
        {
            BlockPos pos  = startPos;
            Vec3d    posv = pos.ToVec3d();
            Vec3d    tgtv = targetPos.ToVec3d();
            BlockPos mP   = ((posv.Add(tgtv)) / 2).AsBlockPos;
            int      sR   = (int)mP.DistanceTo(pos) + 4;
            var      grid = new CubeGrid(sR * 2, sR * 2, sR * 2);

            if (!grid.isInBounds(world, targetPos) || !grid.isPassable(world, targetPos) || !grid.isWalkable(world, targetPos))
            {
                return(new List <BlockPos> {
                    startPos
                });
            }
            for (int x = mP.X - sR; x <= mP.X + sR; x++)
            {
                for (int y = mP.Y - sR; y <= mP.Y + sR; y++)
                {
                    for (int z = mP.Z - sR; z <= mP.Z + sR; z++)
                    {
                        BlockPos currentPos = new BlockPos(x, y, z);
                        if (grid.isInBounds(world, currentPos) && grid.isPassable(world, currentPos) && grid.isWalkable(world, currentPos))
                        {
                            grid.air.Add(currentPos);
                        }
                        else
                        {
                            grid.walls.Add(currentPos);
                        }
                    }
                }
            }
            var astar = new AStarSearch(world, grid, pos, targetPos);

            return(CurrentPath(world, astar, mP, sR));
        }
예제 #6
0
 private void DrawGrid(IWorldAccessor world, AStarSearch astar, BlockPos pos, int sR)
 {
     for (int x = pos.X - sR; x < pos.X + sR; x++)
     {
         for (int y = pos.Y - sR; y < pos.Y + sR; y++)
         {
             for (int z = pos.Z - sR; z < pos.Z + sR; z++)
             {
                 ushort   ff  = 32;
                 BlockPos id  = new BlockPos(x, y, z);
                 BlockPos ptr = id;
                 if (!astar.cameFrom.TryGetValue(id, out ptr))
                 {
                     ptr = id;
                     ff  = 0;
                 }
                 if (ff != 0)
                 {
                     world.BlockAccessor.SetBlock(ff, ptr);
                 }
             }
         }
     }
 }