public PathNode(int x, int y) //Grid<PathNode> grid, //this.grid = grid; { this.x = x; this.y = y; height = TerrainGen.GenerateTerrainAt(x, y); isWalkable = height > 0; isWater = height <= 0; }
void GenerateTerrain() { for (int x = (int)bounds.xMin; x < bounds.xMax + 1; x++) { for (int z = (int)bounds.zMin; z < bounds.zMax + 1; z++) { Vector2Int localPoint = GetLocalCoord(new Vector2Int(x, z)); terrainHeightMap[localPoint.x, localPoint.y] = TerrainGen.GenerateTerrainAt(x, z); } } state = ChunkState.PRELOADED; }
// Loads cheap snapshot of chunk public void Snapshot() { if (state >= ChunkState.SNAPSHOTTED) { return; } Vector2Int center = bounds.GetCenter(); Vector2Int localCenter = GetLocalCoord(center); terrainSnapshot = TerrainGen.GenerateTerrainAt(center.x, center.y); terrainHeightMap[localCenter.x, localCenter.y] = terrainSnapshot; densitySnapshot = TerrainGen.CalculateDensityAtChunk(center);//CalculateDensityAt(center.x, center.y); densityMap[localCenter.x, localCenter.y] = densitySnapshot; state = ChunkState.SNAPSHOTTED; }
public void CalcDensityCenters() { if (!highwayCentersGenerated) { for (int i = 0; i < WorldManager.regionDim; i++) { for (int j = 0; j < WorldManager.regionDim; j++) { if (densityRanking.Count == 0) { densityRanking.Add(new Vector2Int(i, j)); } else { for (int idx = 0; idx < densityRanking.Count; idx++) { Vector2Int currIdx = (Vector2Int)densityRanking[idx]; float currDensity = densitySnapshots[currIdx.x, currIdx.y]; if (currDensity < densitySnapshots[i, j]) { densityRanking.Insert(idx, new Vector2Int(i, j)); break; } else if (idx == densityRanking.Count - 1) //Add to end { densityRanking.Add(new Vector2Int(i, j)); break; } } } } } // determine # centers based on highest density int centers = 0; Vector2Int top = (Vector2Int)densityRanking[0]; if (densitySnapshots[(int)top.x, (int)top.y] == 0.9f) { centers = 3; } else if (densitySnapshots[(int)top.x, (int)top.y] > 0.7f) { centers = 2; } else if (densitySnapshots[(int)top.x, (int)top.y] > 0.2f) { centers = 1; } // choose n coordinates from the top rankings as centers for (int i = 0; i < centers; i++) { Vector2Int v = (Vector2Int)densityRanking[i]; Vector2 worldV = C2W(v) + regionIdx * WorldManager.regionDim * WorldManager.chunkSize; Vector2Int worldVInt = Util.VecToVecInt(worldV); if (!TooClose(v, densityChunks) && TerrainGen.GenerateTerrainAt(worldVInt.x, worldVInt.y) > 0) { //Debug.Log(TerrainGen.GenerateTerrainAt(v.x, v.y) + " " + v); densityChunks.Add(v); } else { centers++; } } // converting density chunk indices to world coordinates for (int i = 0; i < densityChunks.Count; i++) { Vector2Int chunk = (Vector2Int)densityChunks[i]; Vector2 center = C2W(chunk) + regionIdx * WorldManager.regionDim * WorldManager.chunkSize; //center += rand.NextVector2(0, 10); densityCenters.Add((center, densitySnapshots[chunk.x, chunk.y]));