private int getNextDestination(AbsDirection prvDirection, Junction currentJunction, List <Road> roads, ref Road nextRoad) { if (currentJunction.isNullJunction) { return(-1); } int failedDirection = 0; bool[] failedDirIndex = new bool[3]; failedDirIndex[0] = false; failedDirIndex[1] = false; failedDirIndex[2] = false; while (failedDirection < 3) { // sample next destination RltDirection direction = getRandomDirection(failedDirIndex); if (direction == RltDirection.NA) { Debug.Log("ERROR : Trajectory - getNextDestination - Error in generating random direction"); return(-1); } if (direction == RltDirection.Straight) { if (currentJunction.connectRoadIdx[(int)prvDirection] >= 0) { nextRoad = roads[currentJunction.connectRoadIdx[(int)prvDirection]]; return((int)Util.addDirection(prvDirection, direction)); } else { failedDirIndex[(int)RltDirection.Straight] = true; failedDirection++; } } else if (direction == RltDirection.Left) { if (currentJunction.connectRoadIdx[((int)prvDirection + 3) % 4] >= 0) { nextRoad = roads[currentJunction.connectRoadIdx[((int)prvDirection + 3) % 4]]; return((int)Util.addDirection(prvDirection, direction)); } else { failedDirIndex[(int)RltDirection.Left] = true; failedDirection++; } } else if (direction == RltDirection.Right) { if (currentJunction.connectRoadIdx[((int)prvDirection + 1) % 4] >= 0) { nextRoad = roads[currentJunction.connectRoadIdx[((int)prvDirection + 1) % 4]]; return((int)Util.addDirection(prvDirection, direction)); } else { failedDirIndex[(int)RltDirection.Right] = true; failedDirection++; } } } return(-1); }
public void initialize(Junction givenSource, Junction givenTarget) { lanes = new List <Lane>(); lines = new List <Line>(); source = givenSource; target = givenTarget; direction = Util.vec22AbsDirection(givenTarget.coordIndex - givenSource.coordIndex); this.isHorizontal = (direction == AbsDirection.N || direction == AbsDirection.S) ? false : true; if (givenSource.isNullJunction || givenTarget.isNullJunction) { Vector2 center = givenSource.isNullJunction ? givenTarget.center : givenSource.center; JunctionSize size = givenSource.isNullJunction ? givenTarget.size : givenSource.size; center = source.center; if (isHorizontal) { int startI = source.coordIndex.x < target.coordIndex.x ? -size.numOfHrzLane : 0; length = size.lenOfHrzLane * SimParameter.unitBlockSize; numOfLanes = size.numOfHrzLane; for (int i = 0; i < size.numOfHrzLane; i++) { if (direction == AbsDirection.E) { createLane(center, size.numOfVtcLane, (size.lenOfHrzLane + size.numOfVtcLane), startI + i, i, direction, isHorizontal); } else if (direction == AbsDirection.W) { createLane(center, -(size.lenOfHrzLane + size.numOfVtcLane), -size.numOfVtcLane, startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(center, -(size.lenOfHrzLane + size.numOfVtcLane), -size.numOfVtcLane, startI + i, isHorizontal); } if (i == 0) { continue; } if (direction == AbsDirection.E) { createRoadLine(center, size.numOfVtcLane, (size.lenOfHrzLane + size.numOfVtcLane), startI + i, isHorizontal); } else if (direction == AbsDirection.W) { createRoadLine(center, -(size.lenOfHrzLane + size.numOfVtcLane), -size.numOfVtcLane, startI + i, isHorizontal); } } } else { int startI = source.coordIndex.y > target.coordIndex.y ? -size.numOfVtcLane : 0; length = size.lenOfVtcLane * SimParameter.unitBlockSize; numOfLanes = size.numOfVtcLane; for (int i = 0; i < size.numOfVtcLane; i++) { if (direction == AbsDirection.N) { createLane(center, size.numOfHrzLane, (size.lenOfVtcLane + size.numOfHrzLane), startI + i, i, direction, isHorizontal); } else if (direction == AbsDirection.S) { createLane(center, -(size.lenOfVtcLane + size.numOfHrzLane), -size.numOfHrzLane, startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(center, size.numOfHrzLane, (size.lenOfVtcLane + size.numOfHrzLane), startI + i, isHorizontal); } if (i == 0) { continue; } if (direction == AbsDirection.N) { createRoadLine(center, size.numOfHrzLane, (size.lenOfVtcLane + size.numOfHrzLane), startI + i, isHorizontal); } else if (direction == AbsDirection.S) { createRoadLine(center, -(size.lenOfVtcLane + size.numOfHrzLane), -size.numOfHrzLane, startI + i, isHorizontal); } } } } else { if (isHorizontal) { if (source.size.numOfHrzLane != target.size.numOfHrzLane) { Debug.Log("ERROR: Road - initialize - numOfHrzLane is not matched."); } int numOfHrzLane = source.size.numOfHrzLane; int startI = direction == AbsDirection.E ? -numOfHrzLane : 0; length = (source.size.lenOfHrzLane + target.size.lenOfHrzLane) * SimParameter.unitBlockSize; numOfLanes = numOfHrzLane; for (int i = 0; i < numOfHrzLane; i++) { if (source.center.x < target.center.x) { createLane(source.center, source.size.numOfVtcLane, source.size.numOfVtcLane + (source.size.lenOfHrzLane + target.size.lenOfHrzLane), startI + i, i, direction, isHorizontal); } else { createLane(target.center, target.size.numOfVtcLane, target.size.numOfVtcLane + (target.size.lenOfHrzLane + source.size.lenOfHrzLane), startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(target.center, target.size.numOfVtcLane, target.size.numOfVtcLane + (target.size.lenOfHrzLane + source.size.lenOfHrzLane), startI + i, isHorizontal); } if (i == 0) { continue; } if (source.center.x < target.center.x) { createRoadLine(source.center, source.size.numOfVtcLane, source.size.numOfVtcLane + (source.size.lenOfHrzLane + target.size.lenOfHrzLane), startI + i, isHorizontal); } else { createRoadLine(target.center, target.size.numOfVtcLane, target.size.numOfVtcLane + (target.size.lenOfHrzLane + source.size.lenOfHrzLane), startI + i, isHorizontal); } } } else { if (source.size.numOfVtcLane != target.size.numOfVtcLane) { Debug.Log("ERROR: Road - initialize - numOfVtcLane is not matched."); } int numOfVtcLane = source.size.numOfVtcLane; int startI = direction == AbsDirection.S ? -numOfVtcLane : 0; length = (source.size.lenOfVtcLane + target.size.lenOfVtcLane) * SimParameter.unitBlockSize; numOfLanes = numOfVtcLane; for (int i = 0; i < numOfVtcLane; i++) { if (source.center.y < target.center.y) { createLane(source.center, source.size.numOfHrzLane, source.size.numOfHrzLane + (source.size.lenOfVtcLane + target.size.lenOfVtcLane), startI + i, i, direction, isHorizontal); } else { createLane(target.center, target.size.numOfHrzLane, target.size.numOfHrzLane + (target.size.lenOfVtcLane + source.size.lenOfVtcLane), startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(source.center, source.size.numOfHrzLane, source.size.numOfHrzLane + (source.size.lenOfVtcLane + target.size.lenOfVtcLane), startI + i, isHorizontal); } if (i == 0) { continue; } if (source.center.y < target.center.y) { createRoadLine(source.center, source.size.numOfHrzLane, source.size.numOfHrzLane + (source.size.lenOfVtcLane + target.size.lenOfVtcLane), startI + i, isHorizontal); } else { createRoadLine(target.center, target.size.numOfHrzLane, target.size.numOfHrzLane + (target.size.lenOfVtcLane + source.size.lenOfVtcLane), startI + i, isHorizontal); } } } } if (direction == AbsDirection.E || direction == AbsDirection.S) { lanes.Reverse(); } }