/* Given a list of thresholds, connect all the thresholds together in a graph. * Also assign thresholds to their respective zones * Input: threshold graph */ public void BuildThresholdGraph(List <Threshold> thresholdsList) { //look at all the thresholds in the list. //If threshold A and B have zones in common, then connect them for (int i = 0; i < thresholdsList.Count; i++) { Threshold threshold = thresholdsList[i]; List <ThresholdEdge> possibleNeighbors = new List <ThresholdEdge>(); for (int j = 0; j < thresholdsList.Count; j++) { if (j == i) //cannot connect with one's self { continue; } int thresholdAID1 = thresholdsList[i].tzoneID; //threshold A's IDs int thresholdAID2 = thresholdsList[i].zoneId; int thresholdBID1 = thresholdsList[j].tzoneID; //threshold B's IDS int thresholdBID2 = thresholdsList[j].zoneId; if (thresholdAID1 == -1 || thresholdAID2 == -1 || thresholdBID1 == -1 || thresholdBID2 == -1) { //some weird edge case. will have to investigate later continue; } //if they share any ids in common, then B becomes a neighbor of A (A->B) if (thresholdAID1 == thresholdBID1 || thresholdAID1 == thresholdBID2 || thresholdAID2 == thresholdBID1 || thresholdAID2 == thresholdBID2) { if (thresholdAID1 == thresholdBID1 && thresholdAID2 == thresholdBID2) { //however, we do not want to consider connecting thresholds that are in the same zones and connect the same zones continue; } float weight = Vector3.Distance(thresholdsList[i].worldPosition, thresholdsList[j].worldPosition); ThresholdEdge e = new ThresholdEdge(thresholdsList[i], thresholdsList[j], weight); if (!possibleNeighbors.Contains(e)) { possibleNeighbors.Add(e); } } } //done looking at this threshold. add all found neighbors to the threshold cell threshold.TAssignNeighbors(possibleNeighbors); } //finished. add to the global variable thresholdGraph = thresholdsList; }
/* Create a new threshold using the goal Cell that will be used to find a path on * the agentThreshold graph * Input: goal cell * Output: Threshold made from the goal cell. used for deletion when done */ private Threshold AddGoalThreshold(Cell goalCell) { //convert the goal cell into a threshold. //only the neighbors, weights, and position are important. The rest is not //and will be given arbitrary values Threshold goalThreshold = new Threshold(-1, goalCell.zoneId, goalCell.iCost, goalCell.worldPosition, -1, -1, -1); goalThreshold.belongsTo = gameObject.name; //find the neighbors of this new threshold Zone goalZone = map.GetZone(goalCell.zoneId); //List<ThresholdEdge> possibleNeighbors = new List<ThresholdEdge>(); //debug //if (goalZone.thresholds == null) //{ // Debug.Log("this goal has no neighbors. this is:" + goalZone.zoneId); //} foreach (Threshold neighbor in goalZone.thresholds) { //make edges that connect all of these threshold neighbors to the goalThreshold float weight = map.SearchForDistance(neighbor, goalThreshold); ThresholdEdge newEdge = new ThresholdEdge(neighbor, goalThreshold, weight); //add this edge to the agentThreshold neighbor.tedgesToNeighbors.Add(newEdge); //find the corresponding threshold in the agentThresholds //we can find this just by checking if they have the same coordinates //foreach (Threshold agentThreshold in agentThresholds) //{ // if (agentThreshold.worldPosition == neighbor.worldPosition) // { // //continue, since there will only be one corresponding threshold // break; // } //} } //add the threshold to the agentThreshold and we're done! //agentThresholds.Add(goalThreshold); return(goalThreshold); }
/* Create a new threshold using the starting Cell that will be used to find a path * on the agentThreshold graph. * Input: Start cell * Output: Threshold made from start cell. used for deletion when done */ private Threshold AddStartThreshold(Cell startCell) { //convert the startCell into a threshold //only the neighbors, weights, and position are important. The rest is not important //and will be given an arbitary value Threshold startThreshold = new Threshold(-1, -1, startCell.iCost, startCell.worldPosition, -1, -1, -1); startThreshold.belongsTo = gameObject.name; //find the neighbors of this new threshold Zone startZone = map.GetZone(startCell.zoneId); List <ThresholdEdge> possibleNeighbors = new List <ThresholdEdge>(); foreach (Threshold neighbor in startZone.thresholds) { //make edges that connect this neighbor and the startThreshold float weight = map.SearchForDistance(startThreshold, neighbor); ThresholdEdge newEdge = new ThresholdEdge(startThreshold, neighbor, weight); //add the new edge to the list of possibleNeighbors possibleNeighbors.Add(newEdge); //debug!! //Debug.Log("This zone from the MAIN THRESHOLD GRAPH has this many neighbors:" + startZone.thresholds.Count); //find the corresponding threshold in the agentThreshold list //we can find this just by checking if they have the same coordinates //foreach (Threshold agentThreshold in agentThresholds) //{ // if (agentThreshold.worldPosition == neighbor.worldPosition) // { // } //} } //add the possible neighbors to the threshold and we're done! startThreshold.TAssignNeighbors(possibleNeighbors); //agentThresholds.Add(startThreshold); return(startThreshold); }
/* Given a list of thresholds, connect all the thresholds together in a graph. * Also assign thresholds to their respective zones * Input: threshold graph */ private void BuildThresholdGraph(List <Threshold> thresholdsList) { //look at all the thresholds in the list. //If threshold A and B have zones in common, then connect them for (int i = 0; i < thresholdsList.Count; i++) { Threshold threshold = thresholdsList[i]; List <ThresholdEdge> possibleNeighbors = new List <ThresholdEdge>(); for (int j = 0; j < thresholdsList.Count; j++) { if (j == i) //cannot connect with one's self { continue; } int thresholdAID1 = thresholdsList[i].tzoneID; //threshold A's IDs int thresholdAID2 = thresholdsList[i].zoneId; int thresholdBID1 = thresholdsList[j].tzoneID; //threshold B's IDS int thresholdBID2 = thresholdsList[j].zoneId; //if they share any ids in common, then B becomes a neighbor of A (A->B) if (thresholdAID1 == thresholdBID1 || thresholdAID1 == thresholdBID2 || thresholdAID2 == thresholdBID1 || thresholdAID2 == thresholdBID2) { if (thresholdAID1 == thresholdBID1 && thresholdAID2 == thresholdBID2) { //however, we do not want to consider connecting thresholds that are in the same zones and connect the same zones continue; } //DEBUG //if (thresholdAID1 == 24 || thresholdAID2 == 24 || thresholdBID1 == 24 || thresholdBID2 == 24) //{ // if (thresholdAID1 == 25 || thresholdAID2 == 25 || thresholdBID1 == 25 || thresholdBID2 == 25) // { // Debug.Log("looking at zone 25 and 26. ZoneIDs are: "); // Debug.Log(thresholdAID1); // Debug.Log(thresholdAID2); // Debug.Log(thresholdBID1); // Debug.Log(thresholdBID2); // } //} //CHANGED!! REMEMBER TO CHANGE BACK //float weight = Vector3.Distance(thresholdsList[i].worldPosition, thresholdsList[j].worldPosition); float weight = SearchForDistance(thresholdsList[i], thresholdsList[j]); ThresholdEdge e = new ThresholdEdge(thresholdsList[i], thresholdsList[j], weight); if (!possibleNeighbors.Contains(e)) { possibleNeighbors.Add(e); } } } //done looking at this threshold. add all found neighbors to the threshold cell threshold.TAssignNeighbors(possibleNeighbors); } //finished. add to the global variable thresholdGraph = thresholdsList; }