// Builds first road from start to end. private int buildMainRoad(ref Roads.Road road,MapHexa currentHexa, Roads.Road.RoadBlock currentBlock,int id,MapHexa.HexDir incDir) { //if (id == 83) // return 1; //MapHexa.Coordinate currentCoords = currentRoadBlock.getCoords (); //bool foundNext = false; //Debug.Log ("<color=red> Now at "+currentHexa.getCoords().rowId+" "+currentHexa.getCoords().hexaId+" </color> "); Roads.Road.RoadBlock newBlock; List <MapHexa.HexDir> possibleDirections = initDirectionsExcNbours (incDir); // Randomize next direction for (int i = possibleDirections.Count-1; i >= 0; i--) { // Randomize direction MapHexa.HexDir dir = possibleDirections [Random.Range (0, i+1)]; possibleDirections.Remove (dir); //Debug.Log ("Checking direction "+dir); // Take hexa from that direction if (currentHexa.getNeighbour (dir) == null) continue; MapHexa hexa = currentHexa.getNeighbour (dir).GetComponent<MapHexa>(); // Check if we have reached end if (isEndNear(hexa)) { //Debug.Log ("End found"); newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; road.addRoadBlock (newBlock); newBlock.finalRoad = true; newBlock.nextRoadBlock.roadId = Constants.RoadEndId; newBlock.nextRoadBlock.roadBlockId = Constants.RoadEndId; hexa.finalR = true; return 1; } //Debug.Log ("<color=red>Going to </color> "+dir); // Check if we can go there if (isHexaAtTheEdge (hexa.getCoords ())) { // Continue to next direction if we can't continue; } if (isHexaNearThisRoad(hexa.getCoords(),road.roadId,getCounterDir(dir)) ){ // Continue to next direction if we can't continue; } // Add new roadBlock with given id newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); road.addRoadBlock (newBlock); // Call next roadblock int ret = buildMainRoad(ref road, hexa,newBlock,id+1,getCounterDir(dir)); // Check if we have been successful reaching the end, set hexa to road if (ret == 1) { currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; newBlock.finalRoad = true; hexa.finalR = true; // Finally, remove all the extra blocks if (id == 1) { for (int a = road.roadBlocks.Count-1; a >=0; a--) { if (!road.roadBlocks [a].finalRoad) { road.deleteRoadBlock (road.roadBlocks[a].coord); } } } return 1; } // Leave the block only if ret is 1 // Else we have to continue if we can reach the end from here } // If we fail at this point, clean made block and return to previous block //Debug.Break(); return 0; }
private int buildSecRoads(ref Roads.Road road,MapHexa currentHexa, Roads.Road.RoadBlock currentBlock,int id,MapHexa.HexDir incDir) { Roads.Road.RoadBlock newBlock; List <MapHexa.HexDir> possibleDirections = initDirectionsExcNbours (incDir); // Randomize next direction for (int i = possibleDirections.Count-1; i >= 0; i--) { // Randomize direction MapHexa.HexDir dir = possibleDirections [Random.Range (0, i+1)]; possibleDirections.Remove (dir); //Debug.Log ("Checking direction "+dir); // Take hexa from that direction if (currentHexa.getNeighbour (dir) == null) continue; MapHexa hexa = currentHexa.getNeighbour (dir).GetComponent<MapHexa>(); // Check if we have reached end OR we are colliding other road here //Debug.Log("pörrr"); if (isHexaAtTheEdge (hexa.getCoords ())) { // Continue to next direction if we can't continue; } MapHexa nearbyHexa = isOtherRoadNearby (hexa, road.roadId); if (nearbyHexa != null) { newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; road.addRoadBlock (newBlock); newBlock.finalRoad = true; newBlock.nextRoadBlock.roadId = nearbyHexa.roadBlock.roadId; newBlock.nextRoadBlock.roadBlockId = nearbyHexa.roadBlock.blockId; hexa.finalR = true; return 1; } if (isEndNear(hexa)) { newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; road.addRoadBlock (newBlock); newBlock.finalRoad = true; newBlock.nextRoadBlock.roadId = Constants.RoadEndId; newBlock.nextRoadBlock.roadBlockId = Constants.RoadEndId; hexa.finalR = true; return 1; } // Check if we can go there if (isHexaNearThisRoad(hexa.getCoords(),road.roadId,getCounterDir(dir)) ){ // Continue to next direction if we can't continue; } // Add new roadBlock with given id newBlock = new Roads.Road.RoadBlock (hexa.getCoords (),id); road.addRoadBlock (newBlock); // Call next roadblock int ret = buildSecRoads(ref road, hexa,newBlock,id+1,getCounterDir(dir)); // Check if we have been successful reaching the end, set hexa to road if (ret == 1) { currentBlock.nextRoadBlock.roadId = road.roadId; currentBlock.nextRoadBlock.roadBlockId = newBlock.blockId; newBlock.finalRoad = true; hexa.finalR = true; // Finally, remove all the extra blocks if (id == 1) { for (int a = road.roadBlocks.Count-1; a >=0; a--) { if (!road.roadBlocks [a].finalRoad) { road.deleteRoadBlock (road.roadBlocks[a].coord); } } //Debug.Log ("Roadhexas block: "+ road.getRoadBlock(2).blockId +" and "+ mapData.getHexa(road.getRoadBlock(2).coord).GetComponent<MapHexa>().roadBlock.blockId); } return 1; } // Leave the block only if ret is 1 // Else we have to continue if we can reach the end from here } // If we fail at this point, clean made block and return to previous block //Debug.Break(); return 0; }