Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. private void MakeWells()
  2. {
  3. float widthScale = (Main.maxTilesX / 4200f);
  4. int numberToGenerate = WorldGen.genRand.Next(1, (int)(2f * widthScale));
  5. for (int k = 0; k < numberToGenerate; k++)
  6. {
  7. tryGenerateWell(1000);
  8. }
  9. }
  10.  
  11. //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.
  12. private bool tryGenerateWell(int attempts)
  13. {
  14. for (int i = 0; i < attempts; i++)
  15. {
  16. int structureX = nextX();
  17. int highestBlock = getHighestBlock(structureX);
  18. int structureY = highestBlock - 1;
  19.  
  20. if (Main.tile[structureX, highestBlock].type != TileID.Dirt)
  21. continue;
  22.  
  23. if (structureY < 150)
  24. continue;
  25.  
  26. if (wellOverlapsDungeonOrClouds(structureX, structureY))
  27. continue;
  28.  
  29. bool success = PlaceWell(structureX, structureY);
  30. if (success)
  31. return true;
  32. }
  33. return false;
  34. }
  35.  
  36. //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.
  37. //When dealing with code that might call itself several times (aka recursion), be aware that there is overhead when calling methods.
  38. private int nextX()
  39. {
  40. int x = WorldGen.genRand.Next(300, Main.maxTilesX - 300);
  41. if (withinXBlocksOfWorldSpawn(x, 50))
  42. {
  43. return nextX();
  44. }
  45. return x;
  46. }
  47.  
  48. private int getHighestBlock(int x)
  49. {
  50. int y = 0;
  51. while (!Main.tile[x, y].active() && y < Main.worldSurface)
  52. {
  53. y++;
  54. }
  55.  
  56. return y;
  57. }
  58.  
  59. private bool withinXBlocksOfWorldSpawn(int xCoord, int blocksOfSpawn)
  60. {
  61. return Math.Abs(Main.spawnTileX - xCoord) < blocksOfSpawn;
  62. }
  63.  
  64. private bool wellOverlapsDungeonOrClouds(int i, int j)
  65. {
  66. for (int l = i - 4; l < i + 4; l++)
  67. {
  68. for (int m = j - 6; m < j + 20; m++)
  69. {
  70. if (Main.tile[l, m].active())
  71. {
  72. int type = (int)Main.tile[l, m].type;
  73. if (type == TileID.BlueDungeonBrick || type == TileID.GreenDungeonBrick || type == TileID.PinkDungeonBrick || type == TileID.Cloud || type == TileID.RainCloud)
  74. {
  75. return true;
  76. }
  77. }
  78. }
  79. }
  80. return false;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement