Пример #1
0
	void Awake() {
		hexType = HexType.Grass;
		roadBlock = null;
		setTexture (0);
		gameController = GameObject.Find ("GameController");
	}
Пример #2
0
	private int generateRoads() {
		Roads.RoadEnd roadEnd=mapData.getRoadEnd();

		// Generate road starts
		MapHexa.Coordinate firstCoords;
		MapHexa.Coordinate secondCoords;
		MapHexa.Coordinate thirdCoords;

		switch (roadEnd.getEndPos()) {
		case Roads.RoadEnd.EndPositions.East:
			firstCoords.hexaId = 0;
			firstCoords.rowId = Random.Range (1, rows - 1);
			secondCoords.rowId = 0;
			secondCoords.hexaId = Random.Range (1, columns - 5);
			thirdCoords.rowId = rows-1;
			thirdCoords.hexaId = Random.Range (1, columns - 5);
			break;
		case Roads.RoadEnd.EndPositions.West:
			firstCoords.hexaId = columns;
			firstCoords.rowId = Random.Range (1, rows - 1);
			secondCoords.rowId = 0;
			secondCoords.hexaId = Random.Range (5, columns - 1);
			thirdCoords.rowId = rows-1;
			thirdCoords.hexaId = Random.Range (5, columns - 1);
			break;
		default:
			// Default at East
			Debug.LogWarning ("RoadEnd endpos default proc: value is " + roadEnd.getEndPos());
			firstCoords.hexaId = 0;
			firstCoords.rowId = Random.Range (1, rows - 1);
			secondCoords.rowId = 0;
			secondCoords.hexaId = Random.Range (1, columns - 5);
			thirdCoords.rowId = rows-1;
			thirdCoords.hexaId = Random.Range (1, columns - 5);
			MapHexa.Coordinate coords;
			coords.hexaId = columns-2;
			coords.rowId = rows - 2;
			roadEnd.setCoords (coords);
			break;
		}

		// Set starting hexa type as a Road
		mapData.getHexa(firstCoords).GetComponent<MapHexa>().GetComponent<MapHexa>().setType(MapHexa.HexType.Road);
		// Create first road and set first block to it
		Roads.Road firstRoad = new Roads.Road (0); // First road with id 0
		mapData.getRoads().addRoad(firstRoad); // TODO: move to constructor
		Roads.Road.RoadBlock firstBlock = new Roads.Road.RoadBlock(firstCoords,0);
		firstBlock.finalRoad = true;
		firstRoad.addRoadBlock(firstBlock);

		// Generate first road
		System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
		sw.Start ();
		int roadOneSuccess = buildMainRoad(ref firstRoad, mapData.getHexa(firstCoords).GetComponent<MapHexa>(),firstBlock,1,MapHexa.HexDir.W);
		sw.Stop ();

		// Same to second road
		mapData.getHexa(secondCoords).GetComponent<MapHexa>().GetComponent<MapHexa>().setType(MapHexa.HexType.Road);

		Roads.Road secondRoad = new Roads.Road (1); // Second road with id 1
		mapData.getRoads().addRoad(secondRoad); // TODO: move to constructor
		Roads.Road.RoadBlock secondBlock = new Roads.Road.RoadBlock(secondCoords,0);
		secondBlock.finalRoad = true;
		secondRoad.addRoadBlock(secondBlock);
		// Check if the road is connected with one block already
		int roadTwoSuccess;
		if (isOtherRoadNearby (mapData.getHexa(secondCoords).GetComponent<MapHexa>(), secondRoad.roadId) )
			roadTwoSuccess = 1;
		else 
			// Generate second road
			roadTwoSuccess = buildSecRoads (ref secondRoad, mapData.getHexa(secondCoords).GetComponent<MapHexa>(),secondBlock,1,MapHexa.HexDir.NE);

		// ... And to third road TODO: Function this
		mapData.getHexa(thirdCoords).GetComponent<MapHexa>().GetComponent<MapHexa>().setType(MapHexa.HexType.Road);

		Roads.Road thirdRoad = new Roads.Road (2); // Third road with id 2
		mapData.getRoads().addRoad(thirdRoad); // TODO: move to constructor
		Roads.Road.RoadBlock thirdBlock = new Roads.Road.RoadBlock(thirdCoords,0);
		thirdBlock.finalRoad = true;
		thirdRoad.addRoadBlock(thirdBlock);
		// Check if the road is connected with one block already
		int roadThreeSuccess;
		if (isOtherRoadNearby (mapData.getHexa(thirdCoords).GetComponent<MapHexa>(), thirdRoad.roadId) )
			roadThreeSuccess = 1;
		else 
			// Generate third road
			roadThreeSuccess = buildSecRoads (ref thirdRoad, mapData.getHexa(thirdCoords).GetComponent<MapHexa>(),thirdBlock,1,MapHexa.HexDir.SE);
		
		if (   roadOneSuccess == 0 
			|| roadTwoSuccess == 0
			|| roadThreeSuccess == 0
		) {
			Debug.Log ("Road build fail");
			Debug.Log ("Total time: "+ sw.ElapsedMilliseconds);
			return 0;
		} else {
			Debug.Log ("Road build success");
			Debug.Log ("Total time: "+ sw.ElapsedMilliseconds);
			return 1;
			//Debug.Log ("Time used to Base: "+timeUsedToBaseCheck);
			//Debug.Log ("Time used to Deletes: "+timeUsedToDeletes);
			//Debug.Log ("Time used to End: "+timeUsedToEndCheck);
			//Debug.Log ("Time used to Begin: "+timeUsedToBegin);
		}


		// Generate rest of the roads
	}
Пример #3
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;
	}
Пример #4
0
	// 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;
	}
Пример #5
0
 void Awake()
 {
     hexType = HexType.Grass;
     roadBlock = null;
     setTexture (0);
     gameController = GameObject.Find ("GameController");
     selecter = GameObject.Find("MapHexaSelectHighlight");
 }