Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TonelGenOld
- {
- static private bool[,] res;
- static private bool[,] secondLayer;
- static private Random r = new Random();
- static private int h, w;
- static public bool[,] TGen(int inPutWidth, int inPutHeight)
- {
- w = inPutWidth;
- h = inPutHeight;
- res = new bool[w, h];
- secondLayer = new bool[w, h];
- w--;
- h--;
- //
- for (int x = 1; x < w; x++)
- {
- for (int y = 1; y < h; y++)
- {
- if (r.Next(0, 50) == 0) res[x, y] = true;
- }
- }
- layerTwoInut();
- makeOneLayer();
- FinalProverka();
- return res;
- }
- static private void layerTwoInut()
- {
- for (int x = 1; x < w; x++)
- {
- for (int y = 1; y < h; y++)
- {
- if (res[x, y]) roundLook(x, y);
- }
- }
- }
- static private void roundLook(int sx, int sy)
- {
- for (int x = sx - 1; x >= 0; x--)
- {
- if (res[x, sy]) doLine(x, sy, sx, sy);
- }
- for (int x = sx + 1; x < w; x++)
- {
- if(res[x, sy]) doLine(sx, sy, x, sy);
- }
- for (int y = sy - 1; y >= 0; y--)
- {
- if (res[sx, y]) doLine(sx, y, sx, sy);
- }
- for (int y = sy + 1; y < h; y++)
- {
- if (res[sx, y]) doLine(sx, sy, sx, y);
- }
- }
- static private void doLine(int sx, int sy, int fx, int fy)
- {
- for (int x = sx; x <= fx; x++)
- {
- for (int y = sy; y <= fy; y++)
- {
- secondLayer[x, y] = true;
- }
- }
- }
- static private void makeOneLayer()
- {
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- if (secondLayer[x, y]) res[x, y] = true;
- }
- }
- }
- static private void FinalProverka()
- {
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- if (res[x, y])
- {
- if (LookAroundIsAlone(x, y))
- {
- ForAlone(x, y);
- }
- }
- }
- }
- }
- static private bool LookAroundIsAlone(int x, int y)
- {
- int c = 0;
- if (res[x + 1, y]) c++;
- if (res[x - 1, y]) c++;
- if (res[x, y + 1]) c++;
- if (res[x, y - 1]) c++;
- if (c < 2) return true;
- return false;
- }
- static private void ForAlone(int ix, int iy)
- {
- int x = ix;
- int y = iy;
- for (x++; x < w; x++)
- {
- if (res[x, y])
- {
- for (x--; x > ix; x--)
- {
- res[x, y] = true;
- }
- break;
- }
- }
- x = ix;
- y = iy;
- for (x--; x > 0; x--)
- {
- if (res[x, y])
- {
- for (x++; x < ix; x++)
- {
- res[x, y] = true;
- }
- break;
- }
- }
- x = ix;
- y = iy;
- for (y++; y < h; y++)
- {
- if (res[x, y])
- {
- for (y--; y > iy; y--)
- {
- res[x, y] = true;
- }
- break;
- }
- }
- x = ix;
- y = iy;
- for (y--; y > 0; y--)
- {
- if (res[x, y])
- {
- for (y++; y < iy; y++)
- {
- res[x, y] = true;
- }
- break;
- }
- }
- }
- static private void endSkan()
- {
- int ix, iy;
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- if (res[x, y])
- {
- if (LookAroundIsAlone(x, y))
- {
- ix = x;
- ix++;
- if (ix < w) res[ix, y] = true;
- ix++;
- if (ix < w) res[ix, y] = true;
- ix++;
- if (ix < w) res[ix, y] = true;
- ix++;
- if (ix < w) res[ix, y] = true;
- ix++;
- if (ix < w) res[ix, y] = true;
- ix = x;
- ix--;
- if (ix > 1) res[ix, y] = true;
- ix--;
- if (ix > 1) res[ix, y] = true;
- ix--;
- if (ix > 1) res[ix, y] = true;
- ix--;
- if (ix > 1) res[ix, y] = true;
- ix--;
- if (ix > 1) res[ix, y] = true;
- iy = y;
- iy++;
- if (ix < h) res[x, iy] = true;
- iy++;
- if (ix < h) res[x, iy] = true;
- iy++;
- if (ix < h) res[x, iy] = true;
- iy++;
- if (ix < h) res[x, iy] = true;
- iy++;
- if (ix < h) res[x, iy] = true;
- iy = y;
- iy--;
- if (ix > 1) res[x, iy] = true;
- iy--;
- if (ix > 1) res[x, iy] = true;
- iy--;
- if (ix > 1) res[x, iy] = true;
- iy--;
- if (ix > 1) res[x, iy] = true;
- iy--;
- if (ix > 1) res[x, iy] = true;
- }
- }
- }
- }
- FinalProverka();
- }
- }
- class TonelDungeonGeneratorNew
- {
- private static bool[,] layer1;
- private static bool[,] layer2;
- private static int height, width;
- private static int frequency = 50;
- private static Random r;
- public static bool[,] Generate(int iheight, int iwidth)
- {
- r = new Random();
- height = iheight;
- width = iwidth;
- frequency = (height + width) / 2;
- layer1 = new bool[width, height];
- layer2 = new bool[width, height];
- Init(frequency);
- PrimaryTunneling();
- AdditionalWays();
- bool[,] res = new bool[width, height];
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- if (layer1[x, y])
- {
- res[x, y] = true;
- }
- }
- }
- layer1 = null;
- layer2 = null;
- return res;
- }
- private static void JoiningLayers()
- {
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- if (layer2[x, y])
- {
- layer1[x, y] = true;
- }
- layer2[x, y] = false;
- }
- }
- }
- private static void Init(int frequency)
- {
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- if (r.Next(frequency) == 0)
- {
- layer1[x, y] = true;
- }
- }
- }
- }
- private static void PrimaryTunneling()
- {
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- if (layer1[x, y])
- {
- Tunneling_1(x, y);
- }
- }
- }
- JoiningLayers();
- }
- private static void Tunneling_1(int ix, int iy)
- {
- bool draw_flag = false;
- for (int x = 0; x < ix; x++)
- {
- if (draw_flag)
- {
- layer2[x, iy] = true;
- }
- else
- {
- if (layer1[x, iy])
- {
- draw_flag = true;
- }
- }
- }
- draw_flag = false;
- for (int x = ix + 1; x < width; x++)
- {
- if (draw_flag)
- {
- layer2[x, iy] = true;
- }
- else
- {
- if (layer1[x, iy])
- {
- draw_flag = true;
- }
- }
- }
- draw_flag = false;
- for (int y = 0; y < iy; y++)
- {
- if (draw_flag)
- {
- layer2[ix, y] = true;
- }
- else
- {
- if (layer1[ix, y])
- {
- draw_flag = true;
- }
- }
- }
- draw_flag = false;
- for (int y = 0; y < iy; y++)
- {
- if (draw_flag)
- {
- layer2[ix, y] = true;
- }
- else
- {
- if (layer1[ix, y])
- {
- draw_flag = true;
- }
- }
- }
- }
- private static int LookAround(int x, int y)
- {
- int c = 0;
- if (x - 1 > 0) if (layer1[x - 1, y]) c++;
- if (x + 1 < width - 1) if (layer1[x + 1, y]) c++;
- if (y - 1 > 0) if (layer1[x, y - 1]) c++;
- if (y + 1 < height - 1) if (layer1[x, y + 1]) c++;
- return c;
- }
- private static void TunnelingAround(int ix, int iy)
- {
- for (int x = ix - 1; x > 0; x--)
- {
- if (layer1[x, iy]) break;
- layer2[x, iy] = true;
- }
- for (int x = ix + 1; x < width - 1; x++)
- {
- if (layer1[x, iy]) break;
- layer2[x, iy] = true;
- }
- for (int y = iy - 1; y > 0; y--)
- {
- if (layer1[ix, y]) break;
- layer2[ix, y] = true;
- }
- for (int y = iy + 1; y < height - 1; y--)
- {
- if (layer1[ix, y]) break;
- layer2[ix, y] = true;
- }
- }
- private static void AdditionalWays()
- {
- bool again = true;
- while (again)
- {
- again = false;
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- if (layer1[x, y])
- {
- if (LookAround(x, y) < 2)
- {
- again = true;
- TunnelingAround(x, y);
- }
- }
- }
- }
- if (again) JoiningLayers();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement