Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IEnumerator MapGen_Standart(object[] parms) {
- int Chunk_X = (int)parms[0];
- int Chunk_Y = (int)parms[1];
- TileMap[Chunk_X, Chunk_Y].Chunk = new TileStructure[ChunkSize, ChunkSize, Map_Height];
- for (int Z = 0; Z < Map_Height; Z++) {
- for (int X = 0; X < ChunkSize; X++) {
- for (int Y = 0; Y < ChunkSize; Y++) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 0;
- }
- }
- }
- float Height_Solid = 0;
- float Height_Water = 3;
- float Height_Sand = 4;
- float Height_Grass = 8;
- float[,] Heightmap = Heightmap_Square(Chunk_X, Chunk_Y, Seed, 3);
- float[,] Smoothmap = Heightmap_Square(Chunk_X, Chunk_Y, Seed, 4);
- float[,] Roughtmap = Heightmap_Square(Chunk_X+1, Chunk_Y+1, Seed+1, 3);
- float[,] Offsetmap = Heightmap_Square(Chunk_X+1, Chunk_Y+1, Seed+2, 4);
- for (int X = 0; X < ChunkSize; X++) {
- for (int Y = 0; Y < ChunkSize; Y++) {
- //float Offset = (Mathf.Cos(Offsetmap[X, Y]*12+2)-1+2*Offsetmap[X, Y]*6)*3f;
- float Height = Heightmap[X ,Y] * Smoothmap[X, Y] * 32; //Mathf.Floor(Offset + Heightmap[X, Y]*Roughtmap[X, Y]*20);
- float Stone_Height = Mathf.Floor(Height - (0.6f-Offsetmap[X, Y]*Roughtmap[X, Y])*50);
- float Max_Height = Mathf.Max(0, Height, Height_Water, Stone_Height); //EDIT THIS
- for (int Z = 0; Z < Max_Height; Z++) {
- //DebugText.text = "Max height: " + Max_Height + ", X: " + X + ", Y: " + Y + ", Z: " + Z + "\n" + DebugText.text;
- if (Z <= Height_Solid) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
- } else if (Z <= Height_Water) {
- if (Z <= Stone_Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
- } else if (Z < Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
- } else if (Z == Height) {
- if (Z < Height_Water - 3) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
- } else {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 4;
- }
- } else {
- if (Z == Max_Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 5;
- } else {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 5;
- }
- }
- } else if (Z <= Height_Sand) {
- if (Z <= Stone_Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
- } else if (Z+1 < Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
- } else {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 4;
- }
- } else if (Z <= Height_Grass-Roughtmap[X, Y]*15) {
- if (Z <= Stone_Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
- } else if (Z < Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
- } else {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 2;
- }
- } else {
- if (Z < Stone_Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 1;
- } else if (Z == Stone_Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 6;
- } else if (Z < Height) {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 3;
- } else {
- TileMap[Chunk_X, Chunk_Y].Chunk[X, Y, Z].Type = 6;
- }
- }
- }
- yield return null;
- }
- }
- TileMap[Chunk_X, Chunk_Y].generated = true;
- object[] parms2 = new object[2]{Chunk_X, Chunk_Y};
- StartCoroutine ("CreateChunk", parms2);
- DebugText.text = "\nMath for Chunk[" + Chunk_X + "," + Chunk_Y + "] done, queuing render." + DebugText.text;
- }
- public float Random_By_Coordinate(int input)
- {
- Random.seed = input;
- return Random.Range(0f, 1f);
- }
- public float[,] Heightmap_Square(int Chunk_X, int Chunk_Y, int seed, int Iterations)
- {
- // Fixed Chunk_Size
- int Chunk_Size = ChunkSize;
- // Calculate the Size to accomplish the wanted Chunk_Size with the given Iterations
- int Size = Chunk_Size / (int)Mathf.Pow(2, Iterations);
- //int Size = 2; // Start with a 2x2 heightmap
- //int Iterations = 4; // Size*2^Iterations gives a chunksize of 32 tiles
- //int Chunk_Size = Size * (int)Mathf.Pow(2, Iterations);
- float[,] Heightmap = new float[Chunk_Size+1, Chunk_Size+1];
- // #### Write array of (Size+1)² random elements ####
- for (int ix = 0; ix <= Size; ix++){
- int Real_X = Chunk_X*Chunk_Size + ix * (Chunk_Size / Size);
- for (int iy = 0; iy <= Size; iy++){
- int Real_Y = Chunk_Y*Chunk_Size + iy * (Chunk_Size / Size);
- Heightmap[ix, iy] = Random_By_Coordinate(seed + Real_X + Real_Y*100000);
- }
- }
- // #### Do the iterations ####
- for (int i = 0; i < Iterations; i++){
- // #### Resize the array ####
- for (int ix = Size; ix >= 0; ix--){
- for (int iy = Size; iy >= 0; iy--){
- Heightmap[ix*2, iy*2] = Heightmap[ix, iy];
- }
- }
- Size *= 2;
- // #### The diamond step ####
- for (int ix = 1; ix <= Size; ix+=2){
- for (int iy = 1; iy <= Size; iy+=2){
- Heightmap[ix, iy] = (Heightmap[ix+1, iy+1] + Heightmap[ix-1, iy+1] + Heightmap[ix+1, iy-1] + Heightmap[ix-1, iy-1]) / 4f;
- // #### Add some noise to it
- //Heightmap[ix, iy] += ((float)rand.Next() / 2147483648) - 0.5f;
- // Don't add noise, doesn't work well with infinite maps.
- }
- }
- // #### The square step ####
- for (int ix = 0; ix <= Size; ix+=2){
- for (int iy = 1; iy <= Size; iy+=2){
- Heightmap[ix, iy] = (Heightmap[ix, iy-1] + Heightmap[ix, iy+1]) / 2f;
- }
- }
- for (int ix = 1; ix <= Size; ix+=2){
- for (int iy = 0; iy <= Size; iy+=2){
- Heightmap[ix, iy] = (Heightmap[ix-1, iy] + Heightmap[ix+1, iy]) / 2f;
- }
- }
- }
- // #### The size of the Heightmap is (Chunk_Size+1)² ####
- // #### Resize array to Chunk_Size x Chunk_Size ####
- //Array.Resize(ref Heightmap, new int[] {Chunk_Size, Chunk_Size});
- return Heightmap;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement