Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. package com.virtual_hex.engine;
  2.  
  3. import com.virtual_hex.engine.procgen.*;
  4. import com.virtual_hex.engine.utilities.ColorUtils;
  5. import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
  6. import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
  7. import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. import javax.imageio.ImageIO;
  12. import java.awt.*;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.Map;
  17. import java.util.UUID;
  18. import java.util.concurrent.ThreadLocalRandom;
  19.  
  20. public class WorldGenTestMain {
  21.  
  22. /**
  23. * Simply a Logger Reference
  24. */
  25. private static final Logger LOGGER = LoggerFactory.getLogger(WorldGenTestMain.class);
  26.  
  27.  
  28. static int saddleBrown = ColorUtils.argb2Argb(1, 139, 69, 19); // Saddle Brown
  29. static int skyBlue = ColorUtils.argb2Argb(1, 135, 206, 235); // Blue Sky
  30. static int stoneGrey = ColorUtils.argb2Argb(1, 120, 120, 120); // Stone
  31. static int oilBlack = ColorUtils.argb2Argb(1, 48, 50, 47); // Stone
  32.  
  33. public static int getColor(int id){
  34. switch (id){
  35. case 0: return 0;
  36. case 1: return saddleBrown;
  37. case 2: return skyBlue;
  38. case 3: return stoneGrey;
  39. case 4: return oilBlack;
  40. default: return 0;
  41. }
  42. }
  43.  
  44. int elevation;
  45.  
  46. public static void main(String[] args) throws IOException {
  47. // TODO Clonable system so we can compare and make change list
  48.  
  49. int zoneWidth = 100;
  50. int zoneHeight = 100;
  51.  
  52. Zone2Di zone = new Zone2Di(zoneWidth, zoneHeight, 0);
  53.  
  54. Feature2Di root = zone.rootFeature.root;
  55. Map<UUID, Feature2Di> allNestedFeatures = zone.rootFeature.allNestedFeatures;
  56.  
  57. Feature2Di sky = new Feature2Di("sky", new Rectangle2Di(new Point2Di(0, 0), zoneWidth, zoneHeight / 2));
  58. sky.transforms.add(new WorldTransform2D() {
  59. @Override
  60. public int transform(int x, int y, int id) {
  61. LOGGER.debug("Transform Sky: {}, {} - ID: {}", x, y, id);
  62. return sky.shape.in(x, y) ? 2 : id;
  63. }
  64. });
  65. Feature2Di terrain = new Feature2Di("terrain", new Rectangle2Di(new Point2Di(0, 50), zoneWidth, 50));
  66. terrain.transforms.add(new WorldTransform2D() {
  67. @Override
  68. public int transform(int x, int y, int id) {
  69. LOGGER.debug("Transform Terrain: {}, {} - ID: {}", x, y, id);
  70. return terrain.shape.in(x, y) ? 1 : id;
  71. }
  72. });
  73. Feature2Di stone = new Feature2Di("stone", new Rectangle2Di(new Point2Di(0, 70), zoneWidth, 30));
  74. stone.transforms.add(new WorldTransform2D() {
  75. @Override
  76. public int transform(int x, int y, int id) {
  77. LOGGER.debug("Transform Stone: {}, {} - ID: {}", x, y, id);
  78. return stone.shape.in(x, y) ? 3 : id;
  79. }
  80. });
  81. Feature2Di oil = new Feature2Di("oil", new Rectangle2Di(new Point2Di(0, 75), zoneWidth, 25));
  82. oil.transforms.add(new WorldTransform2D() {
  83.  
  84. ThreadLocalRandom r =ThreadLocalRandom.current();
  85. int oilPocketsCount = r.nextInt(5, 25);
  86. Int2ObjectMap<Int2IntArrayMap> oilPockets = setOilPockets();
  87.  
  88. private Int2ObjectMap<Int2IntArrayMap> setOilPockets() {
  89. Int2ObjectMap<Int2IntArrayMap> oilPockets = new Int2ObjectOpenHashMap<>();
  90. int min = 75;
  91. int max = 100;
  92. for (int i = 0; i < oilPocketsCount; i++) {
  93.  
  94. oilPockets.computeIfAbsent(r.nextInt(0, max),
  95. x -> new Int2IntArrayMap()).computeIfAbsent(r.nextInt(min, max),
  96. y -> r.nextInt(1, 6));
  97. }
  98. return oilPockets;
  99. }
  100.  
  101. @Override
  102. public int transform(int x, int y, int id) {
  103. LOGGER.debug("Transform Oil: {}, {} - ID: {}", x, y, id);
  104. Int2IntArrayMap int2IntArrayMap = oilPockets.get(x);
  105. if(int2IntArrayMap != null){
  106. int size = int2IntArrayMap.get(y);
  107. if(size != 0){
  108. return 4;
  109. } else {
  110. return id;
  111. }
  112. } else {
  113. return id;
  114. }
  115. }
  116. });
  117.  
  118. allNestedFeatures.put(sky.id, sky);
  119. allNestedFeatures.put(terrain.id, terrain);
  120. allNestedFeatures.put(stone.id, stone);
  121. allNestedFeatures.put(oil.id, oil);
  122.  
  123. terrain.childrenIds.add(stone.id);
  124. stone.childrenIds.add(oil.id);
  125.  
  126. root.childrenIds.add(sky.id);
  127. root.childrenIds.add(terrain.id);
  128.  
  129. // Initialize
  130. zone.generateMap();
  131.  
  132. int width = zone.map.width;
  133. int height = zone.map.height;
  134.  
  135. // Constructs alpha BufferedImage of one of the predefined image types.
  136. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  137.  
  138. // Create alpha graphics which can be used to draw into the buffered image
  139. Graphics2D g2d = bufferedImage.createGraphics();
  140.  
  141.  
  142. zone.map.fastLoop(new CoordinateIdHandler2Di() {
  143. @Override
  144. public void handle(int x, int y, int id) {
  145. bufferedImage.setRGB(x, y, getColor(id));
  146. }
  147. });
  148.  
  149.  
  150. // Save as PNG
  151. File file = new File("world_test.png");
  152. ImageIO.write(bufferedImage, "png", file);
  153.  
  154.  
  155. // Disposes of this graphics context and releases any system resources that it is using.
  156. g2d.dispose();
  157.  
  158. }
  159.  
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement