public static long DFSSize(int i, int j, int[][] explored, int[][] matrix)
        {
            var initalPosition = new Positon
            {
                I = i,
                J = j,
            };

            var PositionsToProcess = new Stack <Positon>();

            PositionsToProcess.Push(initalPosition);

            long size = 1;

            explored[i][j] = 1;

            while (PositionsToProcess.Any())
            {
                var currentPosition = PositionsToProcess.Pop();
                currentPosition.CalculatePositions(explored, matrix);
                var nextPositions = currentPosition.Positons;

                foreach (var nextPosition in nextPositions)
                {
                    size = DFSSizeRecursive(nextPosition, explored, matrix, size);
                }
            }
            return(size);
        }
Ejemplo n.º 2
0
    public Mesh GenerateCubes(float samplingRate, int cellSize, float scale)
    {
        this.samplingRate = samplingRate;
        this.cellSize     = cellSize;
        this.scale        = scale;
        Positon pos = new Positon();

        // TODO: calculate the value 41817
        Dictionary <Vector3, int> hashedVertexes  = new Dictionary <Vector3, int>(41817);
        LinkedList <int>          linkedTriangles = new LinkedList <int>();
        int hashedIndex = 0;
        LinkedList <Vector3> linkedVectors = new LinkedList <Vector3>();



        for (int x = 0; x < (cellSize * scale); x++)
        {
            pos.x = x / scale;
            for (int y = 0; y < (cellSize * scale); y++)
            {
                pos.y = y / scale;
                for (int z = 0; z < (cellSize * scale); z++)
                {
                    pos.z = z / scale;

                    hashedIndex = March(pos, hashedVertexes, linkedTriangles, hashedIndex, linkedVectors);
                }
            }
        }

        MeshData mesh = new MeshData(32);

        /*
         * int mexVertexPerMesh = 64000;
         * int numMeshes = linkedVectors.Count / mexVertexPerMesh + 1;
         * int vectorsRemaining = linkedVectors.Count % mexVertexPerMesh;
         * LinkedList<Vector3[]> meshes = new LinkedList<Vector3[]>();
         * for (int i = 0; i < numMeshes; i++)
         * {
         *  if (i == numMeshes - 1)
         *  {
         *      Vector3[] vec = new Vector3[64000];
         *      int[] tri = new int[64000];
         *  }
         *  else
         *  {
         *      Vector3[] vec = new Vector3[vectorsRemaining];
         *  }
         * }
         *
         */

        Vector3[] finalVectors = LinkListToArray(linkedVectors);
        mesh.vertices = finalVectors;
        int[] finalTriangles = LinkListToArray(linkedTriangles);
        mesh.triangles = finalTriangles;
        return(mesh.CreateMesh());
    }
Ejemplo n.º 3
0
    public float GetFloatValueWithCubeOffset(int vector, Positon pos)
    {
        Vector3 v = new Vector3();

        v.x = (float)GetScaledCubeOffset(vector, pos.x, 0) / samplingRate;
        v.y = (float)GetScaledCubeOffset(vector, pos.y, 1) / samplingRate;
        v.z = (float)GetScaledCubeOffset(vector, pos.z, 2) / samplingRate;

        return((float)noise.Sample(v));
    }
Ejemplo n.º 4
0
    public int CubeToCaseNumber(Positon pos)
    {
        int result = 0;

        for (int i = 0; i < 8; i++)
        {
            result = result | (int)((GetFloatValueWithCubeOffset(i, pos) >= 0) ? Mathf.Pow(2, i) : 0);
        }

        return(result);
    }
Ejemplo n.º 5
0
        public static int KnightProblem(int a, int b, int n)
        {
            var VisitedMatrix = new bool[n][];

            for (var i = 0; i < n; i++)
            {
                VisitedMatrix[i] = new bool[n];
            }

            var initalPosition = new Positon
            {
                A     = 0,
                B     = 0,
                Steps = 0,
            };

            VisitedMatrix[0][0] = true;

            var PositionsToProcess = new Queue <Positon>();

            PositionsToProcess.Enqueue(initalPosition);

            while (PositionsToProcess.Any())
            {
                var currentPosition = PositionsToProcess.Dequeue();
                currentPosition.CalulatePositions(a, b, n);
                var nextPositions = currentPosition.Positons;

                foreach (var nextPosition in nextPositions)
                {
                    if (nextPosition.A == n - 1 && nextPosition.B == n - 1)
                    {
                        return(nextPosition.Steps);
                    }
                    if (!VisitedMatrix[nextPosition.A][nextPosition.B])
                    {
                        PositionsToProcess.Enqueue(nextPosition);
                        VisitedMatrix[nextPosition.A][nextPosition.B] = true;
                    }
                }
            }
            return(-1);
        }
Ejemplo n.º 6
0
    /* Paramiters:
     * a:   The frist floats used in the lerp
     * b:   The second float used in the lerp
     * v1:  The first indexValue of the cube vertex used in the lerp
     * v2:  The second indexValue of the cube vertex used in the lerp
     * pos: The position offset in the local desity volume
     */
    public Vector3 LerpVectors(int v1, int v2, Positon pos)
    {
        Vector3 r  = new Vector3();
        Vector3 va = new Vector3(GetScaledCubeOffset(v1, pos.x, 0), GetScaledCubeOffset(v1, pos.y, 1), GetScaledCubeOffset(v1, pos.z, 2));
        Vector3 vb = new Vector3(GetScaledCubeOffset(v2, pos.x, 0), GetScaledCubeOffset(v2, pos.y, 1), GetScaledCubeOffset(v2, pos.z, 2));
        float   a  = GetFloatValueWithCubeOffset(v1, pos);
        float   b  = GetFloatValueWithCubeOffset(v2, pos);

        for (int i = 0; i < 3; i++)
        {
            if (va[i] != vb[i]) // Divde by zero exception escape
            {
                r[i] = Mathf.Lerp(va[i], vb[i], Mathf.Abs(a) / Mathf.Abs(b - a));
            }
            else
            {
                r[i] = va[i];
            }
        }
        return(r);
    }
Ejemplo n.º 7
0
    public Vector3 EdgeToVector(int edge, Positon pos)
    {
        Vector3 result = new Vector3();

        if (edge < 8)
        {
            if ((edge + 1) % 4 == 0)
            {
                result = LerpVectors(edge, edge - 3, pos);
            }
            else
            {
                result = LerpVectors(edge, edge + 1, pos);
            }
        }
        else
        {
            result = LerpVectors(edge - 8, edge - 4, pos);
        }
        return(result);
    }
        public static long DFSSizeRecursive(Positon position, int[][] explored, int[][] matrix, long size)
        {
            if (explored[position.I][position.J] == 0)
            {
                size++;
                explored[position.I][position.J] = (int)size;
            }
            position.CalculatePositions(explored, matrix);
            var accessableNodes = position.Positons;

            if (!accessableNodes.Any())
            {
                return(size);
            }


            for (var i = 0; i < accessableNodes.Count; i++)
            {
                size = Math.Max(DFSSizeRecursive(accessableNodes[i], explored, matrix, size), size);
            }
            return(size);
        }
Ejemplo n.º 9
0
    private int March(Positon pos, Dictionary <Vector3, int> hashedVertexes, LinkedList <int> linkedTriangles, int hashedIndex, LinkedList <Vector3> finalVectors)
    {
        int caseNumber   = CubeToCaseNumber(pos);
        int numberOfPoly = case_to_numpolys[caseNumber];

        for (int polygon = 0; polygon < numberOfPoly; polygon++)
        {
            int a = g_triTable[caseNumber, polygon, 0];
            int b = g_triTable[caseNumber, polygon, 1];
            int c = g_triTable[caseNumber, polygon, 2];

            Vector3[] vec =
            {
                EdgeToVector(a, pos),
                EdgeToVector(b, pos),
                EdgeToVector(c, pos)
            };

            int testOut;
            for (int v = 0; v < 3; v++)
            {
                if (hashedVertexes.TryGetValue(vec[v], out testOut))
                {
                    linkedTriangles.AddLast(testOut);
                }
                else
                {
                    hashedVertexes.Add(vec[v], hashedIndex);
                    finalVectors.AddLast(vec[v]);
                    linkedTriangles.AddLast(hashedIndex);
                    hashedIndex++;
                }
            }
        }

        return(hashedIndex);
    }
Ejemplo n.º 10
0
        static string PlayGame(LevelModel level, Player player)
        {
            var map = level.Map;

            // from this point on player pos is here and not in map
            var playerPos = level.LastPlayerPos ?? LocationHelper.GetFirstObjectFromMap <PlayerStartObject>(map);

            level.LastPlayerPos = new Positon();
            if (map[playerPos.XAxis, playerPos.YAxis].GetType() == typeof(PlayerStartObject))
            {
                map[playerPos.XAxis, playerPos.YAxis] = new FloorMapObject();
            }


            var input = "";
            var clear = true;

            while (input?.ToLower() != "exit")
            {
                var getAroundMe = LocationHelper.GetWhatsAroundPosition(playerPos, map);

                if (clear)
                {
                    Console.Clear();
                    Console.WriteLine($"-----");
                    Console.WriteLine($"|{getAroundMe.AllAround[0, 0]}{getAroundMe.AllAround[1, 0]}{getAroundMe.AllAround[2, 0]}|");
                    Console.WriteLine($"|{getAroundMe.AllAround[0, 1]}{player.StartOb}{getAroundMe.AllAround[2, 1]}|");
                    Console.WriteLine($"|{getAroundMe.AllAround[0, 2]}{getAroundMe.AllAround[1, 2]}{getAroundMe.AllAround[2, 2]}|");
                    Console.WriteLine($"-----");
                }

                if (getAroundMe.AllAround[1, 1].GetType() == typeof(MapCustomObject))
                {
                    var mapCustomObject = (MapCustomObject)getAroundMe.AllAround[1, 1];
                    if (!string.IsNullOrWhiteSpace(mapCustomObject.Message))
                    {
                        Console.WriteLine(mapCustomObject.Message);
                    }

                    if (mapCustomObject.AddItemId != null)
                    {
                        player.Inventory.Add(new InventoryItem {
                            Id = (int)mapCustomObject.AddItemId, Name = mapCustomObject.Name
                        });
                    }

                    level.Map[playerPos.XAxis, playerPos.YAxis] = new FloorMapObject();
                }

                if (getAroundMe.AllAround[1, 1].GetType() == typeof(MapExitObject))
                {
                    var mapExitObject = (MapExitObject)getAroundMe.AllAround[1, 1];

                    return(mapExitObject.GoToLevel);
                }

                level.LastPlayerPos.XAxis = playerPos.XAxis;
                level.LastPlayerPos.YAxis = playerPos.YAxis;
                clear = true;

                var inventory = player.Inventory.Select(x => x.Name).Aggregate("", (a, b) => $"{a},{b}");
                Console.WriteLine($" Options are Up Down Left Right PickUp  (not case sensetive) items:{inventory}");

                input = Console.ReadLine();

                input = input?.ToLower();
                MapObjectBase moveToPoint = null;
                var           movePos     = new Positon {
                    XAxis = 0, YAxis = 0
                };
                switch (input)
                {
                case "exit":
                    Environment.Exit(1);
                    break;

                case "up":
                case "u":
                    moveToPoint   = getAroundMe.Up;
                    movePos.YAxis = -1;
                    break;

                case "down":
                case "d":
                    moveToPoint   = getAroundMe.Down;
                    movePos.YAxis = 1;
                    break;

                case "left":
                case "l":
                    moveToPoint   = getAroundMe.Left;
                    movePos.XAxis = -1;
                    break;

                case "right":
                case "r":
                    moveToPoint   = getAroundMe.Right;
                    movePos.XAxis = 1;
                    break;
                }

                // ok i admit this is a lot of conditions
                // essentially it can't be null and it can be stood on
                if (moveToPoint != null && moveToPoint.CanStandOn &&
                    // its not a custom object
                    (moveToPoint.GetType() != typeof(MapCustomObject) ||
                     // if it is a custom object does not need a item
                     ((MapCustomObject)moveToPoint).RequiresItemId == null ||
                     // if it is a custom object and needs a item we have it in player inv
                     player.Inventory.Select(x => x.Id).Contains((int)((MapCustomObject)moveToPoint).RequiresItemId)))
                {
                    playerPos.XAxis += movePos.XAxis;
                    playerPos.YAxis += movePos.YAxis;
                    continue;
                }

                clear = false;
                Console.WriteLine($"Can Not do that");
            }

            return("");
        }