Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void MakeWells()
- {
- float widthScale = (Main.maxTilesX / 4200f);
- int numberToGenerate = WorldGen.genRand.Next(1, (int)(2f * widthScale));
- for (int k = 0; k < numberToGenerate; k++)
- {
- tryGenerateWell(1000);
- }
- }
- //Out paramaters are not normally used. Normally coords would be a class variable, however an out paramater is used so that the well generation code isn't scattered throughout the class.
- private bool tryGenerateWell(int attempts)
- {
- for (int i = 0; i < attempts; i++)
- {
- int structureX = nextX();
- int highestBlock = getHighestBlock(structureX);
- int structureY = highestBlock - 1;
- if (Main.tile[structureX, highestBlock].type != TileID.Dirt)
- continue;
- if (structureY < 150)
- continue;
- if (wellOverlapsDungeonOrClouds(structureX, structureY))
- continue;
- bool success = PlaceWell(structureX, structureY);
- if (success)
- return true;
- }
- return false;
- }
- //We don't have to worry about this method entering an infinite loop because the likelyhood of the method picking a coordnitate within 50 blocks of spawn is very small.
- //When dealing with code that might call itself several times (aka recursion), be aware that there is overhead when calling methods.
- private int nextX()
- {
- int x = WorldGen.genRand.Next(300, Main.maxTilesX - 300);
- if (withinXBlocksOfWorldSpawn(x, 50))
- {
- return nextX();
- }
- return x;
- }
- private int getHighestBlock(int x)
- {
- int y = 0;
- while (!Main.tile[x, y].active() && y < Main.worldSurface)
- {
- y++;
- }
- return y;
- }
- private bool withinXBlocksOfWorldSpawn(int xCoord, int blocksOfSpawn)
- {
- return Math.Abs(Main.spawnTileX - xCoord) < blocksOfSpawn;
- }
- private bool wellOverlapsDungeonOrClouds(int i, int j)
- {
- for (int l = i - 4; l < i + 4; l++)
- {
- for (int m = j - 6; m < j + 20; m++)
- {
- if (Main.tile[l, m].active())
- {
- int type = (int)Main.tile[l, m].type;
- if (type == TileID.BlueDungeonBrick || type == TileID.GreenDungeonBrick || type == TileID.PinkDungeonBrick || type == TileID.Cloud || type == TileID.RainCloud)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement