Example #1
0
File: Node.cs Project: fylux/GameAI
 public Node(int gridX, int gridY, Vector3 worldPos, NodeT type)
 {
     this.type          = type;
     this.worldPosition = worldPos;
     this.gridX         = gridX;
     this.gridY         = gridY;
 }
Example #2
0
    NodeT[,] Load(string filePath)
    {
        try {
            using (StreamReader sr = new StreamReader(filePath)) {
                string[] lines = sr.ReadToEnd().Split(new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
                NodeT[,] tiles = new NodeT[lines.Length, mapX];

                for (int i = 0; i < lines.Length; i++)
                {
                    string[] nums = lines[i].Split(new[] { ',' });
                    for (int j = 0; j < Mathf.Min(nums.Length, mapX); j++)
                    {
                        int indexType;
                        if (!int.TryParse(nums[j], out indexType))
                        {
                            Debug.LogError("Cannot parse" + nums[j]);
                        }
                        tiles[i, j] = (NodeT)indexType;
                    }
                }
                return(tiles);
            }
        } catch (IOException e) {
            Debug.Log(e.Message);
            return(null);
        }
    }
        /// <summary>
        /// Constructor
        /// </summary>
        public LockFreeQueue()
        {
            _count = new ConcurrentInt64();
            // ReSharper disable CompareNonConstrainedGenericWithNull
            _isNullable = default(T) == null;
            // ReSharper restore CompareNonConstrainedGenericWithNull
            var node = new NodeT();

            _head._ptr = _tail._ptr = node;
        }
Example #4
0
    void GenerateMap()
    {
        Dictionary <NodeT, GameObject> terrainType = new Dictionary <NodeT, GameObject>()
        {
            { NodeT.ROAD, Road }, { NodeT.GRASS, Grass }, { NodeT.FOREST, Forest }, { NodeT.WATER, Water }, { NodeT.MOUNTAIN, Mountain }
        };

        bool[,] used = new bool[mapX, mapX];
        for (int x = 0; x < mapX; x++)
        {
            for (int y = 0; y < mapY; y++)
            {
                GameObject influenceTileObj = Instantiate(influenceTile, offset + new Vector3(x * nodeSizeX, -0.5f, y * nodeSizeY), Quaternion.Euler(90, 0, 0));
                influenceTileObj.transform.parent = influenceGrid.transform;
                influenceTileObj.GetComponent <Renderer>().material.SetFloat("_Glossiness", 0f);

                if (used[x, y])
                {
                    continue;
                }

                int   x1 = x, y1 = y;
                NodeT type = tiles[x, y];

                while (x1 + 1 < mapX && Enumerable.Range(y, y1 - y + 1).All(i => tiles[x1 + 1, i] == type && !used[x1 + 1, y]))
                {
                    for (int i = y; i <= y1; ++i)
                    {
                        used[x1 + 1, i] = true;
                    }
                    x1++;
                }

                while (y1 + 1 < mapY && Enumerable.Range(x, x1 - x + 1).All(i => tiles[i, y1 + 1] == type && !used[i, y1 + 1]))
                {
                    for (int i = x; i <= x1; ++i)
                    {
                        used[i, y1 + 1] = true;
                    }
                    y1++;
                }

                int rows = x1 - x + 1;
                int cols = y1 - y + 1;

                float middleX = (float)(x + x1) / 2f;
                float middleY = (float)(y + y1) / 2f;

                GameObject TilePrefab = Instantiate(terrainType[tiles[x, y]], offset + new Vector3(middleX * nodeSizeX, 0, middleY * nodeSizeY), Quaternion.Euler(90, 0, 0));
                TilePrefab.transform.localScale = new Vector3(rows, cols, 1f);
                TilePrefab.transform.parent     = this.transform;
            }
        }
    }
Example #5
0
    Node[,] BuildMap()
    {
        Node[,] grid = new Node[mapX, mapX];

        for (int x = 0; x < mapX; x++)
        {
            for (int y = 0; y < mapY; y++)
            {
                Vector3 position = offset + (Vector3.right * x * nodeSizeX) + (Vector3.forward * y * nodeSizeY);
                NodeT   type     = (NodeT)tiles[x, y];
                grid[x, y] = new Node(x, y, position, type);
                grid[x, y].influenceTile = influenceGrid.transform.GetChild(x * mapY + y).GetComponent <Renderer>();
            }
        }
        return(grid);
    }
        /// <summary>
        /// Enqueue a single element
        /// </summary>
        /// <param name="t"></param>
        public void Enqueue(T t)
        {
            // Allocate a new node from the free list
            var node = new NodeT {
                _value = t
            };

            // copy enqueued value into node

            // keep trying until Enqueue is done
            Boolean bEnqueueNotDone = true;

            while (bEnqueueNotDone)
            {
                // read Tail.ptr and Tail.count together
                PointerT tail = _tail;

                // read next ptr and next count together
                PointerT next = tail._ptr._next;

                // are tail and next consistent
                if (tail._count == _tail._count && tail._ptr == _tail._ptr)
                {
                    // was tail pointing to the last node?
                    if (null == next._ptr)
                    {
                        if (CAS(ref tail._ptr._next, next, new PointerT(node, next._count + 1)))
                        {
                            bEnqueueNotDone = false;
                        }                 // endif
                    }                     // endif

                    else                  // tail was not pointing to last node
                    {
                        // try to swing Tail to the next node
                        CAS(ref _tail, tail, new PointerT(next._ptr, tail._count + 1));
                    }
                }         // endif
            }             // endloop
            _count.Increment();
        }
Example #7
0
    protected void ApplyActuator()
    {// Aqui el Actuator suma los steerings, los aplica a las velocidades, y las limita, teniendo en cuenta los costes
        NodeT node  = Map.NodeFromPosition(position).type;
        float tCost = GetTerrainCost(position);

        Steering steering = ApplySteering();

        if (velocity.magnitude > 0.1f)
        {
            steering += Face.GetSteering(position + velocity, this, interiorAngle, exteriorAngle, 0.1f, false); // Mover a ApplySteering
            steering += WallAvoidance.GetSteering(this, 10000f, Map.terrainMask, 0.7f, 0.7f, 0.5f, false);
            steering += AvoidUnits.GetSteering(this, 1000f, false);
        }

        velocity  += steering.linear * Time.deltaTime / tCost;
        rotation  += steering.angular * Time.deltaTime / tCost;
        velocity.y = 0;

        velocity = Vector3.ClampMagnitude(velocity, (float)MaxVelocity / tCost);
        rotation = Mathf.Clamp(rotation, -MaxRotation, MaxRotation);
    }
 /// <summary>
 /// constructor that allows caller to specify ptr and count
 /// </summary>
 /// <param name="node"></param>
 /// <param name="c"></param>
 public PointerT(NodeT node, long c)
 {
     _ptr   = node;
     _count = c;
 }