public StorageBuilding FindStorageBuildingThatHas(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>(); int smallestAmountOfItem = 0; foreach (GameObject go in objs) { StorageBuilding strg = go.GetComponent <StorageBuilding>(); if (this is StorageBuilding) { if (strg == (StorageBuilding)this) { continue; } } //if storage building does not store that type of item, continue if (strg.typeStored != type) { continue; } //if storage building or this structure have no entrances, continue List <Node> entrancesHere = CheckAdjRoads(); List <Node> entrancesThere = strg.CheckAdjRoads(); if (entrancesHere.Count == 0 || entrancesThere.Count == 0) { continue; } if (strg.Inventory[item] == 0) { continue; } //if has less than the needed amount of Item and less than other discovered inventories, continue if (strg.Inventory[item] < num && strg.Inventory[item] < smallestAmountOfItem) { continue; } smallestAmountOfItem = strg.Inventory[item]; float distance = entrancesHere[0].DistanceTo(entrancesThere[0]); queue.Enqueue(strg, distance); } int numChecking = 0; foreach (StorageBuilding strg in queue) { 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; } } return(destination); }
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); }