Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.virtual_hex.engine;
- import com.virtual_hex.engine.procgen.*;
- import com.virtual_hex.engine.utilities.ColorUtils;
- import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
- import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
- import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Map;
- import java.util.UUID;
- import java.util.concurrent.ThreadLocalRandom;
- public class WorldGenTestMain {
- /**
- * Simply a Logger Reference
- */
- private static final Logger LOGGER = LoggerFactory.getLogger(WorldGenTestMain.class);
- static int saddleBrown = ColorUtils.argb2Argb(1, 139, 69, 19); // Saddle Brown
- static int skyBlue = ColorUtils.argb2Argb(1, 135, 206, 235); // Blue Sky
- static int stoneGrey = ColorUtils.argb2Argb(1, 120, 120, 120); // Stone
- static int oilBlack = ColorUtils.argb2Argb(1, 48, 50, 47); // Stone
- public static int getColor(int id){
- switch (id){
- case 0: return 0;
- case 1: return saddleBrown;
- case 2: return skyBlue;
- case 3: return stoneGrey;
- case 4: return oilBlack;
- default: return 0;
- }
- }
- int elevation;
- public static void main(String[] args) throws IOException {
- // TODO Clonable system so we can compare and make change list
- int zoneWidth = 100;
- int zoneHeight = 100;
- Zone2Di zone = new Zone2Di(zoneWidth, zoneHeight, 0);
- Feature2Di root = zone.rootFeature.root;
- Map<UUID, Feature2Di> allNestedFeatures = zone.rootFeature.allNestedFeatures;
- Feature2Di sky = new Feature2Di("sky", new Rectangle2Di(new Point2Di(0, 0), zoneWidth, zoneHeight / 2));
- sky.transforms.add(new WorldTransform2D() {
- @Override
- public int transform(int x, int y, int id) {
- LOGGER.debug("Transform Sky: {}, {} - ID: {}", x, y, id);
- return sky.shape.in(x, y) ? 2 : id;
- }
- });
- Feature2Di terrain = new Feature2Di("terrain", new Rectangle2Di(new Point2Di(0, 50), zoneWidth, 50));
- terrain.transforms.add(new WorldTransform2D() {
- @Override
- public int transform(int x, int y, int id) {
- LOGGER.debug("Transform Terrain: {}, {} - ID: {}", x, y, id);
- return terrain.shape.in(x, y) ? 1 : id;
- }
- });
- Feature2Di stone = new Feature2Di("stone", new Rectangle2Di(new Point2Di(0, 70), zoneWidth, 30));
- stone.transforms.add(new WorldTransform2D() {
- @Override
- public int transform(int x, int y, int id) {
- LOGGER.debug("Transform Stone: {}, {} - ID: {}", x, y, id);
- return stone.shape.in(x, y) ? 3 : id;
- }
- });
- Feature2Di oil = new Feature2Di("oil", new Rectangle2Di(new Point2Di(0, 75), zoneWidth, 25));
- oil.transforms.add(new WorldTransform2D() {
- ThreadLocalRandom r =ThreadLocalRandom.current();
- int oilPocketsCount = r.nextInt(5, 25);
- Int2ObjectMap<Int2IntArrayMap> oilPockets = setOilPockets();
- private Int2ObjectMap<Int2IntArrayMap> setOilPockets() {
- Int2ObjectMap<Int2IntArrayMap> oilPockets = new Int2ObjectOpenHashMap<>();
- int min = 75;
- int max = 100;
- for (int i = 0; i < oilPocketsCount; i++) {
- oilPockets.computeIfAbsent(r.nextInt(0, max),
- x -> new Int2IntArrayMap()).computeIfAbsent(r.nextInt(min, max),
- y -> r.nextInt(1, 6));
- }
- return oilPockets;
- }
- @Override
- public int transform(int x, int y, int id) {
- LOGGER.debug("Transform Oil: {}, {} - ID: {}", x, y, id);
- Int2IntArrayMap int2IntArrayMap = oilPockets.get(x);
- if(int2IntArrayMap != null){
- int size = int2IntArrayMap.get(y);
- if(size != 0){
- return 4;
- } else {
- return id;
- }
- } else {
- return id;
- }
- }
- });
- allNestedFeatures.put(sky.id, sky);
- allNestedFeatures.put(terrain.id, terrain);
- allNestedFeatures.put(stone.id, stone);
- allNestedFeatures.put(oil.id, oil);
- terrain.childrenIds.add(stone.id);
- stone.childrenIds.add(oil.id);
- root.childrenIds.add(sky.id);
- root.childrenIds.add(terrain.id);
- // Initialize
- zone.generateMap();
- int width = zone.map.width;
- int height = zone.map.height;
- // Constructs alpha BufferedImage of one of the predefined image types.
- BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- // Create alpha graphics which can be used to draw into the buffered image
- Graphics2D g2d = bufferedImage.createGraphics();
- zone.map.fastLoop(new CoordinateIdHandler2Di() {
- @Override
- public void handle(int x, int y, int id) {
- bufferedImage.setRGB(x, y, getColor(id));
- }
- });
- // Save as PNG
- File file = new File("world_test.png");
- ImageIO.write(bufferedImage, "png", file);
- // Disposes of this graphics context and releases any system resources that it is using.
- g2d.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement