public InternalBuildingNode GetEntrance(Person person, PassengerBehaviour behaviour) { InternalBuildingNode return_entrance = null; if (entrances.Count == 1) { return(entrances[0]); } for (int i = 0; i < entrances.Count; i++) { if (behaviour.CanVisit(entrances[i])) { // can visit this entrance, return it. // print("returning entrance = " + i); return(entrances[i]); // otherwise continue } } if (return_entrance == null) { print("do not know what entrance to get"); } return(return_entrance); }
static Vector2[] GetPath(InternalBuildingNode endObject) { InternalBuildingNode currentObject = endObject; Vector2[] returnPath = new Vector2[GetPathLength(endObject)]; int index = 0; while (currentObject.HasFakeParent() && index < 10000) { Debug.DrawLine(currentObject.position3, currentObject.GetFakeParent().position3, Color.red, 10f); returnPath[index] = currentObject.position2; currentObject = currentObject.GetFakeParent(); //print("index = " + index + " and pos = " + returnPath[index]); index++; } if (index > 9990) { print("failed to get path, while loop crash!"); } // print("index length = " + index + " and array length = " + GetPathLength(endObject)); return(returnPath); }
//int canVisitCounter = 0; public bool CanVisit(InternalBuildingNode startNode) { // first get correct category string behaviourString = behaviours[currentBehaviour]; string[] data = behaviourString.Split(new char[] { ' ' }); string category = ""; if (data.Length > 1) { category = data [1]; } //Debug.Log("Category = " + category); // do test path Vector2[] path = null; InternalBuildingNode curNode = currentNode; currentNode = startNode; //p_service = building.GetService(person); if (employee && category == "employee") { p_service = building.GetService(person); path = building.GetPathToEndNode(currentNode, p_service.GetEmployeeSpot()); } else { //Debug.Log("data[0] = " + data[0]); if (data[0] == "queue") { p_service = building.GetPassengerService(category); path = building.GetPathToEndNode(currentNode, p_service.JoinQueueNode()); p_service = null; } else if (data[0] == "exit") { // Debug.Log("from " + curNode.name + " to " + startNode.name); path = building.GetPathToEndNode(curNode, startNode); // entrance (startNode) is destination } else { path = building.GetPathToEndNode(currentNode, category); } } currentNode = curNode; if (path == null) { return(false); } else { return(true); } }
// --------------------------------- FOR PASSENGERS --------------------------------- public PassengerBehaviour VisitBuilding(Person person) { PassengerBehaviour behaviour = new PassengerBehaviour(this, person, passengerBehaviour); InternalBuildingNode entranceNode = GetEntrance(person, behaviour); // print("entrance node = " + entranceNode.position2); behaviour.SetEntrance(entranceNode); return(behaviour); }
public InternalBuildingNode[] GetInternalConnections() { int i = 0; InternalBuildingNode[] internalConnections = new InternalBuildingNode[nodeConnections.Count]; foreach(BuildingNode node in nodeConnections) { internalConnections[i] = node.internalNode; i++; } return internalConnections; }
public InternalService(PassengerServiceFactory p_service) { queue = new List<Passenger>(); // add all internal nodes queue_start = p_service.queueStart.internalNode; queue_end = p_service.queueEnd.internalNode; passenger_node = p_service.passengerNode.internalNode; employee_node = p_service.employeeNode.internalNode; category = p_service.category; maxQueueSize = p_service.maxQueueSize; }
public InternalService(PassengerServiceFactory p_service) { queue = new List <Passenger>(); // add all internal nodes queue_start = p_service.queueStart.internalNode; queue_end = p_service.queueEnd.internalNode; passenger_node = p_service.passengerNode.internalNode; employee_node = p_service.employeeNode.internalNode; category = p_service.category; maxQueueSize = p_service.maxQueueSize; }
// // //function GetPathLength(curNode : Node) : int { // if (curNode.fakeParent == null) { // return 0; // } else { // return 1 + GetPathLength(curNode.fakeParent); // } //} // /** * Generic return path function */ static Vector2[] GeneratePath(InternalBuildingNode endObject, Dictionary <int, InternalBuildingNode> openList) { int index = 0; InternalBuildingNode curObject = endObject; bool loopEnabled = true; Vector3[] waypoints = new Vector3[1024]; // add position of first cell, then look if that cell has a fakeParent // if so, increase index and add in next loop to list // otherwise, stop loop while (loopEnabled) { //print ("index = " + index + " and size = " + openList.Count); waypoints[index] = curObject.position3; if (curObject.HasFakeParent()) { curObject = curObject.GetFakeParent(); index++; } else { loopEnabled = false; } // while loop protection if (index > 999999) { loopEnabled = false; print("while loop crash"); } } // create returnable list with correct size and add end position at start Vector2[] return_waypoints = new Vector2[index + 1]; //return_waypoints [0] = new Vector2(endPosition.x, endPosition.z); for (int i = 0; i < return_waypoints.Length; i++) { return_waypoints[i] = new Vector2(waypoints[i].x, waypoints[i].z); } if (debugging) { //DrawPath(return_waypoints, 0.2f); } return(return_waypoints); }
public InternalBuildingNode[] GetInternalConnections() { int i = 0; InternalBuildingNode[] internalConnections = new InternalBuildingNode[nodeConnections.Count]; foreach (BuildingNode node in nodeConnections) { internalConnections[i] = node.internalNode; i++; } return(internalConnections); }
/* * Called by passenger service through passenger to go somewhere after queue and service */ public void MoveQueue(bool serviceCompleted) { if (serviceCompleted) { currentBehaviour++; } else { currentTask = "visitQueue"; } // make sure currentNode is right one currentNode = p_service.PassengerSpotNode(); ExecuteBehaviour(); }
/** * Called by CreatePath to calculate H value for given gridCell * */ static float CalculateH(InternalBuildingNode curObject, InternalBuildingNode endObject) { Vector2 curPos, endPos; curPos = curObject.position2; // THIS SHOULD probably be array position endPos = endObject.position2; // THIS SHOULD probably be array position Vector2 calculateHDistance = new Vector2(Mathf.Abs(curPos.x - endPos.x), Mathf.Abs(curPos.y - endPos.y)); //Vector2 calculateHDistance = Vector2.Distance(curPos, endPos.y); // float newH = ((calculateHDistance.x / grid.sizeGridX) * HORIZONTAL_VERTICAL_COST) + ((calculateHDistance.y / grid.sizeGridX) * HORIZONTAL_VERTICAL_COST); MIGHT NEEED THIS!!!!!!!!! float newH = (calculateHDistance.x * HORIZONTAL_VERTICAL_COST) + (calculateHDistance.y * HORIZONTAL_VERTICAL_COST); curObject.SetHCost(newH); return(newH); }
static int GetPathLength(InternalBuildingNode endObject) { int counter = 0; InternalBuildingNode currentObject = endObject; while (currentObject.HasFakeParent() && counter < 10000) { currentObject = currentObject.GetFakeParent(); counter++; } if (counter > 9990) { print("failed to get path, while loop crash!"); } return(counter); }
/** * Called by CreatePath to return adjacent cells * * Only return cells that are empty are not in */ static InternalBuildingNode[] ReturnAdjacentNodes(InternalBuildingNode cur_pf_object, Dictionary <int, InternalBuildingNode> closedList, Dictionary <int, InternalBuildingNode> openList) { InternalBuildingNode[] adjacentObjects = cur_pf_object.GetNeighbours(); InternalBuildingNode[] returnList = new InternalBuildingNode[adjacentObjects.Length]; int index = 0; for (var x = 0; x < adjacentObjects.Length; x++) { try { // check if in closed list if (closedList.ContainsKey(adjacentObjects[x].GetID())) { // do nothing // check if in open list } else if (openList.ContainsKey(adjacentObjects[x].GetID())) { // check if g cost can be lower // take g with original parent, save old parent float originalG = CalculateG(adjacentObjects[x]); InternalBuildingNode oldParent = adjacentObjects[x].GetFakeParent(); // change parent to check if new one works better adjacentObjects[x].SetFakeParent(cur_pf_object); if (originalG < CalculateG(adjacentObjects[x])) // if cost of G is higher after switching parent { adjacentObjects[x].SetFakeParent(oldParent); // then switch it back } // otherwise add to returnable list } else { returnList[index] = adjacentObjects[x]; index++; } } catch { } } return(returnList); }
public Vector2[] GetPathToEndNode(InternalBuildingNode startNode, string category) { // first get random available end node List <InternalBuildingNode> list = GetEdgeNodes(category); if (list.Count > 0) { InternalBuildingNode randomNode = list[Random.Range(0, list.Count)]; // then get path (SHOULD GET THE RIGHT ENTRANCE WHEN MULTIPLE LIKE IN GATE) Vector2[] path = BuildingPathfinding.CreatePath(startNode, randomNode); return(path); } else { print("could not find any available edge nodes with category = " + category); return(null); } }
static float CalculateG(InternalBuildingNode curObject) { float newG = 0; if (!curObject.HasFakeParent()) // gridCell.ultimateParent { print("problems!!!"); return(-999); // if there is a parent } else { // check if connection is straight or diagonal (ID's because of performance issues) newG = (HORIZONTAL_VERTICAL_COST * curObject.GetGCostMultiplier()) + curObject.GetFakeParent().GetGCost(); curObject.SetGCost(newG); return(newG); } }
Vector2[] Visit(string category, bool nodeAvailable) { Vector2[] path; if (employee && category == "employee") { p_service = building.GetService(person); path = building.GetPathToEndNode(currentNode, p_service.GetEmployeeSpot()); } else { path = building.GetPathToEndNode(currentNode, category); } person.VisitPosition(path); // set currentNode to next node currentNode = building.GetEndNode(path[0]); currentNode.available = nodeAvailable; return(path); }
static InternalBuildingNode GetLowestF(InternalBuildingNode endObject, Dictionary <int, InternalBuildingNode> openList) { InternalBuildingNode lowestFObject = null; float lowestF = -1f; // calculate G, H and thus F for all openList gridCells for (var obj = openList.GetEnumerator(); obj.MoveNext();) { // first one if (lowestF == -1f) { lowestF = CalculateG(obj.Current.Value) + CalculateH(obj.Current.Value, endObject); lowestFObject = obj.Current.Value; } else if (CalculateG(obj.Current.Value) + CalculateH(obj.Current.Value, endObject) < lowestF) { lowestF = CalculateG(obj.Current.Value) + CalculateH(obj.Current.Value, endObject); lowestFObject = obj.Current.Value; } } return(lowestFObject); }
void Exit() { entrance = building.GetEntrance(person, this); Vector2[] path = BuildingPathfinding.CreatePath(currentNode, entrance); person.VisitPosition(path); }
Vector2[] Visit(string category, bool nodeAvailable) { Vector2[] path; if (employee && category == "employee") { p_service = building.GetService(person); path = building.GetPathToEndNode(currentNode, p_service.GetEmployeeSpot()); } else { path = building.GetPathToEndNode(currentNode, category); } person.VisitPosition(path); // set currentNode to next node currentNode = building.GetEndNode(path[0]); currentNode.available = nodeAvailable; return path; }
/* * Called by passenger service through passenger to go somewhere after queue and service */ public void MoveQueue(bool serviceCompleted) { if (serviceCompleted) currentBehaviour++; else currentTask = "visitQueue"; // make sure currentNode is right one currentNode = p_service.PassengerSpotNode(); ExecuteBehaviour(); }
public void SetEntrance(InternalBuildingNode _entrance) { entrance = _entrance; currentNode = entrance; }
public Vector2[] GetPathToEndNode(InternalBuildingNode startNode, string category) { // first get random available end node List<InternalBuildingNode> list = GetEdgeNodes(category); if (list.Count > 0) { InternalBuildingNode randomNode = list[Random.Range(0, list.Count)]; // then get path (SHOULD GET THE RIGHT ENTRANCE WHEN MULTIPLE LIKE IN GATE) Vector2[] path = BuildingPathfinding.CreatePath(startNode, randomNode); return path; } else { print("could not find any available edge nodes with category = " + category); return null; } }
static int GetPathLength(InternalBuildingNode endObject) { int counter = 0; InternalBuildingNode currentObject = endObject; while(currentObject.HasFakeParent() && counter < 10000) { currentObject = currentObject.GetFakeParent(); counter++; } if (counter > 9990) print("failed to get path, while loop crash!"); return counter; }
public static Vector2[] CreatePath(InternalBuildingNode startObject, InternalBuildingNode endObject) { Dictionary<int,InternalBuildingNode> openDictionaryList = new Dictionary<int,InternalBuildingNode>(); Dictionary<int,InternalBuildingNode> closedDictionaryList = new Dictionary<int,InternalBuildingNode>(); InternalBuildingNode curObject = startObject; InternalBuildingNode[] adjacentObjects; // FIRST step, only one time ------------------------------------------------------------------------------------------------------// if(closedDictionaryList.Count == 0) { closedDictionaryList.Add(curObject.GetID(), curObject); // get adjacent roads adjacentObjects = ReturnAdjacentNodes(curObject, closedDictionaryList, openDictionaryList); // add to open list for (var i = 0; i < adjacentObjects.Length; i++) { if (adjacentObjects[i] != null) { // add all to open list openDictionaryList[adjacentObjects[i].GetID()] = adjacentObjects[i]; // parent to start point adjacentObjects[i].SetFakeParent(curObject); } } } // SECOND step, look for lowest F value in openList --------------------------------------------------------------------------------// // 1. Add gridCell this to closedList and remove from closedList. // 2. Get cells adjacent to lowest F value // * don't include ones in closed list. // * Check if they're already on open list, if so: // o don't include them but check if their G cost can be lower with currentCell with lowest F value) as parent. // * Get adjacent cells that remain and add them to openList. // 3. Then start all over again if we've not reached the target yet. bool loopingEnabled = true; bool searchingFailure = false; int loopCount = 0; // keep looping while curernt cell is not last one while(loopingEnabled && !searchingFailure) { if (debugging) { print ("openList size: " + openDictionaryList.Count); print ("closedList size: " + closedDictionaryList.Count); } // calculate the lowest F value and return corresponding gridCell curObject = GetLowestF(endObject, openDictionaryList); if (curObject == null) { searchingFailure = true; break; } if (curObject.GetID() != 0); if (closedDictionaryList.ContainsKey(curObject.GetID())) { // don't add } else { // add closedDictionaryList.Add(curObject.GetID(), curObject); // remove from open list openDictionaryList.Remove(curObject.GetID()); } // get adjecent cells adjacentObjects = ReturnAdjacentNodes(curObject, closedDictionaryList, openDictionaryList); // add to open list for (int j = 0; j < 8; j++) { // if (adjacentCells[j]) { NEEED TO DO NULL CHEEECK try { // add all to open list openDictionaryList[adjacentObjects[j].GetID()] = adjacentObjects[j]; // parent to current cell adjacentObjects[j].SetFakeParent(curObject); } catch { } // } } // see if we can leave loop ?? if (curObject.GetID() == endObject.GetID()) { loopingEnabled = false; } if (loopCount >= 10000) { // can't find path print("pathfinding can't find path"); loopingEnabled = false; searchingFailure = true; return null; } // print ("openList size: " + openDictionaryList.Count); // print ("closedList size: " + closedDictionaryList.Count); loopCount++; } if (searchingFailure) { print("search failure"); ResetPathfinding (openDictionaryList, closedDictionaryList); return null; } else { //print("search sucess"); Vector2[] vectorPath = GetPath(endObject); ResetPathfinding (openDictionaryList, closedDictionaryList); return vectorPath; } }
public void SetFakeParent(InternalBuildingNode pf_object) { fakeParent = pf_object; }
/** * Called by CreatePath to return adjacent cells * * Only return cells that are empty are not in */ static InternalBuildingNode[] ReturnAdjacentNodes(InternalBuildingNode cur_pf_object, Dictionary<int, InternalBuildingNode> closedList, Dictionary<int, InternalBuildingNode> openList) { InternalBuildingNode[] adjacentObjects = cur_pf_object.GetNeighbours(); InternalBuildingNode[] returnList = new InternalBuildingNode[adjacentObjects.Length]; int index = 0; for (var x = 0 ; x < adjacentObjects.Length; x++) { try { // check if in closed list if (closedList.ContainsKey(adjacentObjects[x].GetID())) { // do nothing // check if in open list } else if (openList.ContainsKey(adjacentObjects[x].GetID())) { // check if g cost can be lower // take g with original parent, save old parent float originalG = CalculateG(adjacentObjects[x]); InternalBuildingNode oldParent = adjacentObjects[x].GetFakeParent(); // change parent to check if new one works better adjacentObjects[x].SetFakeParent(cur_pf_object); if (originalG < CalculateG(adjacentObjects[x])) { // if cost of G is higher after switching parent adjacentObjects[x].SetFakeParent(oldParent); // then switch it back } // otherwise add to returnable list } else { returnList[index] = adjacentObjects[x]; index++; } } catch { } } return returnList; }
public Vector2[] GetPathToEndNode(InternalBuildingNode startNode, InternalBuildingNode endNode) { Vector2[] path = BuildingPathfinding.CreatePath(startNode, endNode); return(path); }
// // //function GetPathLength(curNode : Node) : int { // if (curNode.fakeParent == null) { // return 0; // } else { // return 1 + GetPathLength(curNode.fakeParent); // } //} // /** * Generic return path function */ static Vector2[] GeneratePath(InternalBuildingNode endObject, Dictionary<int, InternalBuildingNode> openList) { int index = 0; InternalBuildingNode curObject = endObject; bool loopEnabled = true; Vector3[] waypoints = new Vector3[1024]; // add position of first cell, then look if that cell has a fakeParent // if so, increase index and add in next loop to list // otherwise, stop loop while (loopEnabled) { //print ("index = " + index + " and size = " + openList.Count); waypoints[index] = curObject.position3; if (curObject.HasFakeParent()) { curObject = curObject.GetFakeParent(); index++; } else loopEnabled = false; // while loop protection if (index > 999999) { loopEnabled = false; print("while loop crash"); } } // create returnable list with correct size and add end position at start Vector2[] return_waypoints = new Vector2[index + 1]; //return_waypoints [0] = new Vector2(endPosition.x, endPosition.z); for (int i = 0; i < return_waypoints.Length; i++) { return_waypoints[i] = new Vector2(waypoints[i].x, waypoints[i].z); } if (debugging) { //DrawPath(return_waypoints, 0.2f); } return return_waypoints; }
static InternalBuildingNode GetLowestF(InternalBuildingNode endObject, Dictionary<int, InternalBuildingNode> openList) { InternalBuildingNode lowestFObject = null; float lowestF = -1f; // calculate G, H and thus F for all openList gridCells for(var obj = openList.GetEnumerator(); obj.MoveNext();) { // first one if (lowestF == -1f) { lowestF = CalculateG (obj.Current.Value) + CalculateH(obj.Current.Value, endObject); lowestFObject = obj.Current.Value; } else if(CalculateG(obj.Current.Value) + CalculateH(obj.Current.Value, endObject) < lowestF) { lowestF = CalculateG(obj.Current.Value) + CalculateH(obj.Current.Value, endObject); lowestFObject = obj.Current.Value; } } return lowestFObject; }
public static Vector2[] CreatePath(InternalBuildingNode startObject, InternalBuildingNode endObject) { Dictionary <int, InternalBuildingNode> openDictionaryList = new Dictionary <int, InternalBuildingNode>(); Dictionary <int, InternalBuildingNode> closedDictionaryList = new Dictionary <int, InternalBuildingNode>(); InternalBuildingNode curObject = startObject; InternalBuildingNode[] adjacentObjects; // FIRST step, only one time ------------------------------------------------------------------------------------------------------// if (closedDictionaryList.Count == 0) { closedDictionaryList.Add(curObject.GetID(), curObject); // get adjacent roads adjacentObjects = ReturnAdjacentNodes(curObject, closedDictionaryList, openDictionaryList); // add to open list for (var i = 0; i < adjacentObjects.Length; i++) { if (adjacentObjects[i] != null) { // add all to open list openDictionaryList[adjacentObjects[i].GetID()] = adjacentObjects[i]; // parent to start point adjacentObjects[i].SetFakeParent(curObject); } } } // SECOND step, look for lowest F value in openList --------------------------------------------------------------------------------// // 1. Add gridCell this to closedList and remove from closedList. // 2. Get cells adjacent to lowest F value // * don't include ones in closed list. // * Check if they're already on open list, if so: // o don't include them but check if their G cost can be lower with currentCell with lowest F value) as parent. // * Get adjacent cells that remain and add them to openList. // 3. Then start all over again if we've not reached the target yet. bool loopingEnabled = true; bool searchingFailure = false; int loopCount = 0; // keep looping while curernt cell is not last one while (loopingEnabled && !searchingFailure) { if (debugging) { print("openList size: " + openDictionaryList.Count); print("closedList size: " + closedDictionaryList.Count); } // calculate the lowest F value and return corresponding gridCell curObject = GetLowestF(endObject, openDictionaryList); if (curObject == null) { searchingFailure = true; break; } if (curObject.GetID() != 0) { ; } if (closedDictionaryList.ContainsKey(curObject.GetID())) { // don't add } else { // add closedDictionaryList.Add(curObject.GetID(), curObject); // remove from open list openDictionaryList.Remove(curObject.GetID()); } // get adjecent cells adjacentObjects = ReturnAdjacentNodes(curObject, closedDictionaryList, openDictionaryList); // add to open list for (int j = 0; j < 8; j++) { // if (adjacentCells[j]) { NEEED TO DO NULL CHEEECK try { // add all to open list openDictionaryList[adjacentObjects[j].GetID()] = adjacentObjects[j]; // parent to current cell adjacentObjects[j].SetFakeParent(curObject); } catch { } // } } // see if we can leave loop ?? if (curObject.GetID() == endObject.GetID()) { loopingEnabled = false; } if (loopCount >= 10000) { // can't find path print("pathfinding can't find path"); loopingEnabled = false; searchingFailure = true; return(null); } // print ("openList size: " + openDictionaryList.Count); // print ("closedList size: " + closedDictionaryList.Count); loopCount++; } if (searchingFailure) { print("search failure"); ResetPathfinding(openDictionaryList, closedDictionaryList); return(null); } else { //print("search sucess"); Vector2[] vectorPath = GetPath(endObject); ResetPathfinding(openDictionaryList, closedDictionaryList); return(vectorPath); } }
/** * Called by CreatePath to calculate H value for given gridCell * */ static float CalculateH(InternalBuildingNode curObject, InternalBuildingNode endObject) { Vector2 curPos, endPos; curPos = curObject.position2; // THIS SHOULD probably be array position endPos = endObject.position2; // THIS SHOULD probably be array position Vector2 calculateHDistance = new Vector2( Mathf.Abs(curPos.x - endPos.x), Mathf.Abs(curPos.y - endPos.y) ); //Vector2 calculateHDistance = Vector2.Distance(curPos, endPos.y); // float newH = ((calculateHDistance.x / grid.sizeGridX) * HORIZONTAL_VERTICAL_COST) + ((calculateHDistance.y / grid.sizeGridX) * HORIZONTAL_VERTICAL_COST); MIGHT NEEED THIS!!!!!!!!! float newH = (calculateHDistance.x * HORIZONTAL_VERTICAL_COST) + ( calculateHDistance.y * HORIZONTAL_VERTICAL_COST); curObject.SetHCost(newH); return newH; }
static float CalculateG(InternalBuildingNode curObject) { float newG = 0; if (!curObject.HasFakeParent()) { // gridCell.ultimateParent print("problems!!!"); return -999; // if there is a parent } else { // check if connection is straight or diagonal (ID's because of performance issues) newG = (HORIZONTAL_VERTICAL_COST * curObject.GetGCostMultiplier())+ curObject.GetFakeParent().GetGCost(); curObject.SetGCost(newG); return newG; } }
//int canVisitCounter = 0; public bool CanVisit(InternalBuildingNode startNode) { // first get correct category string behaviourString = behaviours[currentBehaviour]; string[] data = behaviourString.Split(new char[] {' '}); string category = ""; if (data.Length > 1) category = data [1]; //Debug.Log("Category = " + category); // do test path Vector2[] path = null; InternalBuildingNode curNode = currentNode; currentNode = startNode; //p_service = building.GetService(person); if (employee && category == "employee") { p_service = building.GetService(person); path = building.GetPathToEndNode(currentNode, p_service.GetEmployeeSpot()); } else { //Debug.Log("data[0] = " + data[0]); if (data[0] == "queue") { p_service = building.GetPassengerService(category); path = building.GetPathToEndNode(currentNode, p_service.JoinQueueNode()); p_service = null; } else if (data[0] == "exit") { // Debug.Log("from " + curNode.name + " to " + startNode.name); path = building.GetPathToEndNode(curNode, startNode); // entrance (startNode) is destination } else { path = building.GetPathToEndNode(currentNode, category); } } currentNode = curNode; if (path == null) return false; else return true; }
public bool ExecuteBehaviour() { if (currentBehaviour >= behaviours.Length) { return(false); } string behaviourString = behaviours[currentBehaviour]; string[] data = behaviourString.Split(new char[] { ' ' }); //Debug.Log("executing behaviour number: " + currentBehaviour); if (data[0] == "visit") // eg. visit sink { bool nodeAvailability = false; // will this edge node be available (for others) when visiting? if (data.Length > 2) // eg. visit sink door or visit toilet door 2 { if (data[2] == "door") { Vector2[] path = Visit(data[1]); // get current node and path path[0] = new Vector2(currentNode.door.transform.position.x, currentNode.door.transform.position.z); person.VisitPosition(path); // differentiate between single pass through door or double pass if (data.Length > 3) { doubleDoor = true; } //else // doubleDoor = false; if (door == null) { door = currentNode.door; } currentTask = data[2]; // current task = door return(true); } else if (data[2] == "available") { nodeAvailability = true; } } Visit(data[1], nodeAvailability); } else if (data[0] == "wait") // eg. wait 10-15 or wait 12 // if unlimited waiting (wait for ) { if (data.Length == 1) { currentTask = "wait infinite"; return(true); } else { string[] waitTime = data[1].Split(new char[] { '-' }); if (waitTime.Length == 1) { Wait(int.Parse(waitTime[0])); } else if (waitTime.Length > 1) { Wait(Random.Range(int.Parse(waitTime[0]), int.Parse(waitTime[1]))); } else { Debug.Log("PROBLEM - Don't understand this wait string"); } } } else if (data[0] == "exit") // eg. exit { Exit(); } else if (data[0] == "queue") { if (p_service == null) { // setup p_service and add to queue for service p_service = building.GetPassengerService(data[1]); InternalBuildingNode endNode = p_service.JoinQueue((Passenger)person); // set on way Vector2[] path = building.GetPathToEndNode(currentNode, endNode); person.VisitPosition(path); currentNode = endNode; currentTask = "visitQueue"; return(true); } else if (currentTask == "visitQueue") { finalPosition = p_service.GetQueuePosition((Passenger)person); person.VisitPosition(finalPosition); currentTask = "enteringQueue"; return(true); } else if (currentTask == "enteringQueue") { // waiting in queue } else { Debug.Log("waiting in queue, currentTask = " + currentTask); return(true); } } else if (data[0] == "plane") // go the airplane and enter { Passenger passenger = (Passenger)person; passenger.EnterAirplane(); } else { Debug.Log("can't find this command: " + data[0]); } doubleDoor = false; currentTask = data[0]; //currentBehaviour++; return(true); }
public bool ExecuteBehaviour() { if (currentBehaviour >= behaviours.Length) { return false; } string behaviourString = behaviours[currentBehaviour]; string[] data = behaviourString.Split(new char[] {' '}); //Debug.Log("executing behaviour number: " + currentBehaviour); if (data[0] == "visit") { // eg. visit sink bool nodeAvailability = false; // will this edge node be available (for others) when visiting? if (data.Length > 2) { // eg. visit sink door or visit toilet door 2 if (data[2] == "door") { Vector2[] path = Visit(data[1]); // get current node and path path[0] = new Vector2(currentNode.door.transform.position.x, currentNode.door.transform.position.z); person.VisitPosition(path); // differentiate between single pass through door or double pass if (data.Length > 3) doubleDoor = true; //else // doubleDoor = false; if (door == null) door = currentNode.door; currentTask = data[2]; // current task = door return true; } else if (data[2] == "available") nodeAvailability = true; } Visit(data[1], nodeAvailability); } else if (data[0] == "wait") { // eg. wait 10-15 or wait 12 // if unlimited waiting (wait for ) if (data.Length == 1) { currentTask = "wait infinite"; return true; } else { string[] waitTime = data[1].Split(new char[] {'-'}); if (waitTime.Length == 1) Wait(int.Parse(waitTime[0])); else if (waitTime.Length > 1) Wait(Random.Range(int.Parse(waitTime[0]), int.Parse(waitTime[1]))); else Debug.Log("PROBLEM - Don't understand this wait string"); } } else if (data[0] == "exit") { // eg. exit Exit(); } else if (data[0] == "queue") { if (p_service == null) { // setup p_service and add to queue for service p_service = building.GetPassengerService(data[1]); InternalBuildingNode endNode = p_service.JoinQueue((Passenger) person); // set on way Vector2[] path = building.GetPathToEndNode(currentNode, endNode); person.VisitPosition(path); currentNode = endNode; currentTask = "visitQueue"; return true; } else if (currentTask == "visitQueue") { finalPosition = p_service.GetQueuePosition((Passenger) person); person.VisitPosition(finalPosition); currentTask = "enteringQueue"; return true; } else if (currentTask == "enteringQueue") { // waiting in queue } else { Debug.Log("waiting in queue, currentTask = " + currentTask); return true; } } else if (data[0] == "plane") { // go the airplane and enter Passenger passenger = (Passenger) person; passenger.EnterAirplane(); } else { Debug.Log("can't find this command: " + data[0]); } doubleDoor = false; currentTask = data[0]; //currentBehaviour++; return true; }
public Vector2[] GetPathToEndNode(InternalBuildingNode startNode, InternalBuildingNode endNode) { Vector2[] path = BuildingPathfinding.CreatePath(startNode, endNode); return path; }
static Vector2[] GetPath(InternalBuildingNode endObject) { InternalBuildingNode currentObject = endObject; Vector2[] returnPath = new Vector2[GetPathLength(endObject)]; int index = 0; while(currentObject.HasFakeParent() && index < 10000) { Debug.DrawLine(currentObject.position3, currentObject.GetFakeParent().position3, Color.red, 10f); returnPath[index] = currentObject.position2; currentObject = currentObject.GetFakeParent(); //print("index = " + index + " and pos = " + returnPath[index]); index++; } if (index > 9990) print("failed to get path, while loop crash!"); // print("index length = " + index + " and array length = " + GetPathLength(endObject)); return returnPath; }