Ejemplo n.º 1
0
    public SimplePriorityQueue <StorageBuilding> FindStorageBuildingToAccept(ItemOrder io)
    {
        int      num  = io.amount;
        int      item = io.item;
        ItemType type = io.type;

        GameObject[] objs = GameObject.FindGameObjectsWithTag("StorageBuilding");
        SimplePriorityQueue <StorageBuilding> queue = new SimplePriorityQueue <StorageBuilding>();

        if (objs.Length == 0)
        {
            return(queue);
        }


        foreach (GameObject go in objs)
        {
            StorageBuilding strg = go.GetComponent <StorageBuilding>();

            //if null, continue
            if (strg == null)
            {
                continue;
            }

            //only add to list if it stores this type
            if (strg.typeStored != type)
            {
                continue;
            }

            if (!strg.Operational)
            {
                continue;
            }

            //only add to list if it has an entrance
            List <Node> entrancesHere  = GetAdjRoadTiles();
            List <Node> entrancesThere = strg.GetAdjRoadTiles();
            if (entrancesHere.Count == 0 || entrancesThere.Count == 0)
            {
                continue;
            }

            //only add to list if it can accept amount
            if (!strg.CanAcceptAmount(num, item))
            {
                continue;
            }

            float distance = entrancesHere[0].DistanceTo(entrancesThere[0]);

            queue.Enqueue(strg, distance);
        }

        return(queue);
    }
Ejemplo n.º 2
0
    public StorageBuilding FindStorageBuildingToAccept(int num, int item, ItemType type)
    {
        GameObject[] objs = GameObject.FindGameObjectsWithTag("StorageBuilding");

        if (objs.Length == 0)
        {
            return(null);
        }

        StorageBuilding destination = null;

        SimplePriorityQueue <StorageBuilding> queue = new SimplePriorityQueue <StorageBuilding>();

        foreach (GameObject go in objs)
        {
            StorageBuilding strg = go.GetComponent <StorageBuilding>();

            //only add to list if it stores this type
            if (strg.typeStored != type)
            {
                continue;
            }

            //only add to list if it has an entrance
            List <Node> entrancesHere  = CheckAdjRoads();
            List <Node> entrancesThere = strg.CheckAdjRoads();
            if (entrancesHere.Count == 0 || entrancesThere.Count == 0)
            {
                continue;
            }

            //only add to list if it can accept amount
            if (!strg.CanAcceptAmount(num, item))
            {
                continue;
            }

            float distance = entrancesHere[0].DistanceTo(entrancesThere[0]);

            queue.Enqueue(strg, distance);
        }

        int numChecking = 0;

        foreach (StorageBuilding strg in queue)
        {
            //only check first five closest buildings
            numChecking++;
            if (numChecking == 5)
            {
                break;
            }

            List <Node> entrancesHere  = CheckAdjRoads();
            List <Node> entrancesThere = strg.CheckAdjRoads();

            //find path to new place
            List <Node> path = world.Map.pathfinder.FindPath(entrancesHere[0], entrancesThere[0]);
            if (path.Count == 0)
            {
                continue;
            }
            else
            {
                destination = strg;
                break;
            }
        }

        return(destination);
    }