예제 #1
0
    public void SpawnChunksAroundPoint(Vector3 point)
    {
        SpawnChunkFeild();
        Loom.QueueAsyncTask(WorldThreadName, () => {
            MazeGen module = new MazeGen(VoxelSettings.SuperSizeX / 2, 4, VoxelSettings.SuperSizeZ / 2, VoxelSettings.seed, 2, 1);
            for (int x = -1; x <= VoxelSettings.maxChunksX; x++)
            {
                for (int z = -1; z <= VoxelSettings.maxChunksZ; z++)
                {
                    Vector3Int location3D = new Vector3Int(x, 0, z);
                    Vector2Int location2D = new Vector2Int(location3D.x, location3D.z);
                    try
                    {
                        if (Chunks.ContainsKey(location3D) && !Chunks[location3D].Generated)
                        {
                            float[,] surface = Chunks[location3D].GenerateChunk(module);
                            Chunks[location3D].Render(false);
                        }
                    }
                    catch (Exception e)
                    {
                        SafeDebug.LogException(e);
                    }
                }
            }
            SafeDebug.Log("Finished rendering.");

            Loom.QueueOnMainThread(() =>
            {
                //MapTexture = module.GetTexture();
            });
        });
    }
예제 #2
0
 private Vector3Int[] Pathfind(Vector3Int start, Vector3Int goal, out bool pathBrocken)
 {
     try {
         List <Vector3Int> frontier = new List <Vector3Int>();
         Dictionary <Vector3Int, Vector3Int> cameFrom = new Dictionary <Vector3Int, Vector3Int>();
         cameFrom.Clear();
         frontier.Add(start);
         cameFrom.Add(start, start);
         int       maxDistance = (SmoothVoxelSettings.radius * SmoothVoxelSettings.ChunkSizeX * 2);
         double    maxBlocks   = (4d / 3d) * Math.PI * Math.Pow(maxDistance, 3);
         Stopwatch watch       = new Stopwatch();
         watch.Start();
         System.Threading.ManualResetEvent reset = new System.Threading.ManualResetEvent(false);
         bool goalFound = false;
         while (frontier.Count != 0 && !goalFound)
         {
             Vector3Int current = frontier[0];
             frontier.RemoveAt(0);
             if (Vector3.Distance(current, Vector3.zero) < maxDistance)
             {
                 Vector3Int[] neighbors = GetNeighbors(current);
                 for (int nIndex = 0; nIndex < neighbors.Length; nIndex++)
                 {
                     if (!cameFrom.ContainsKey(neighbors[nIndex]) && TerrainController.Instance.GetBlock(neighbors[nIndex]).iso < testIso)
                     {
                         frontier.Add(neighbors[nIndex]);
                         cameFrom.Add(neighbors[nIndex], current);
                         if (neighbors[nIndex] == goal)
                         {
                             goalFound = false;
                         }
                         added++;
                     }
                 }
             }
             else
             {
                 break;
             }
             iterations++;
         }
         frontier.Clear();
         watch.Stop();
         return(Trace(cameFrom, goal, out pathBrocken));
         //SafeDebug.Log("time: " + watch.Elapsed);
     }
     catch (Exception e) {
         SafeDebug.LogException(e);
     }
     pathBrocken = true;
     return(new Vector3Int[0]);
 }
예제 #3
0
 public MazeGen(int mazeSizeX, int mazeSizeY, int mazeSizeZ, int seed, int cellSize, int smooth)
 {
     try
     {
         myMaze = new Maze();
         myMaze.GenerateMaze(mazeSizeX, mazeSizeY, mazeSizeZ, seed, smooth);
         Voxels = myMaze.GetMaze(mazeSizeX * 2, mazeSizeY * 2, cellSize);
     }
     catch (Exception e)
     {
         SafeDebug.LogException(e);
     }
 }
예제 #4
0
 private void CreateImage(Vector2Int location, float[][] surfaceData)
 {
     try {
         Texture2D chunkImg = new Texture2D(VoxelSettings.ChunkSizeX, VoxelSettings.ChunkSizeZ);
         for (int x = 0; x < chunkImg.width; x++)
         {
             for (int z = 0, zr = chunkImg.height - 1; z < chunkImg.height; z++, zr--)
             {
                 byte colorVal = (byte)(surfaceData[x][z]);
                 chunkImg.SetPixel(x, zr, new Color(colorVal, colorVal, colorVal));
             }
         }
         chunkImg.Apply();
         ChunkImgList.Add(chunkImg);
         SurfaceImages.Add(location, chunkImg);
         SurfaceImagesDictionary.Add(new SurfaceImagesDictEntry(location, chunkImg));
     }
     catch (Exception e) {
         SafeDebug.LogException(e);
     }
 }
예제 #5
0
    private Vector3Int[] Trace(Dictionary <Vector3Int, Vector3Int> cameFrom, Vector3Int goal, out bool pathBrocken)
    {
        Vector3Int current = goal;

        pathBrocken = false;
        List <Vector3Int> path = new List <Vector3Int>();

        try {
            for (int i = 0; i < 10000; i++)
            {
                if (cameFrom.ContainsKey(current))
                {
                    current = cameFrom[current];
                    if (!path.Contains(current))
                    {
                        path.Add(current);
                    }
                    if (current == start)
                    {
                        break;
                    }
                }
                else
                {
                    SafeDebug.Log("Path brocken");
                    pathBrocken = true;
                    break;
                }
            }
            path.Reverse();
        }
        catch (Exception e) {
            SafeDebug.LogException(e);
            SafeDebug.LogError("length: " + path.Count);
            throw new Exception(":(");
        }
        return(path.ToArray());
    }