Permalink
Browse files

Map Generation (unfinished)

  • Loading branch information...
1 parent d641c32 commit 146d887b918a2c060c7c94a424a22ddb3660922d @RobertZenz committed Feb 2, 2012
@@ -0,0 +1,4 @@
+Map Generation
+--------------
+
+Unfinished idea to create maps with a cellular automata.
@@ -0,0 +1,29 @@
+/*
+ * Public Domain
+ */
+package mapgeneration;
+
+import org.newdawn.slick.AppGameContainer;
+import org.newdawn.slick.SlickException;
+
+/**
+ *
+ * @author Robert 'Bobby' Zenz
+ */
+public class Main {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ try {
+ AppGameContainer app = new AppGameContainer(new World("Map Generation", 400, 300));
+ app.setDisplayMode(800, 600, false);
+ app.setShowFPS(false);
+ app.setTargetFrameRate(60);
+ app.start();
+ } catch (SlickException ex) {
+ System.err.println(ex);
+ }
+ }
+}
@@ -0,0 +1,77 @@
+/*
+ * Pulic Domain
+ */
+package mapgeneration;
+
+import java.util.Random;
+
+/**
+ *
+ * @author Robert 'Bobby' Zenz
+ */
+public class Map {
+
+ private Tile[] tiles;
+ private int width;
+ private int height;
+
+ public Map(int width, int height) {
+ tiles = new Tile[width * height];
+
+ this.width = width;
+ this.height = height;
+
+ for (int idxX = 0; idxX < width; idxX++) {
+ for (int idxY = 0; idxY < height; idxY++) {
+ tiles[idxY * width + idxX] = new Tile();
+ }
+ }
+ }
+
+ public Tile[] getTiles() {
+ return tiles;
+ }
+
+ public Tile getTile(int x, int y) {
+ return tiles[y * width + x];
+ }
+
+ public Tile getTile(int index) {
+ return tiles[index];
+ }
+
+ public int getWidth() {
+ return width;
+ }
+
+ public int getHeight() {
+ return height;
+ }
+
+ public void init() {
+ for (int idxX = 0; idxX < width; idxX++) {
+ for (int idxY = 0; idxY < height; idxY++) {
+ tiles[idxY * width + idxX].init(this, idxX, idxY);
+ }
+ }
+ }
+
+ public void generate(long seed) {
+ Random random = new Random(seed);
+
+ TileType[] values = TileType.values();
+
+ for (Tile tile : tiles) {
+ tile.setType(values[random.nextInt(values.length - 1) + 1]);
+ }
+ }
+
+ public void doGeneration() {
+ for (Tile tile : tiles) {
+ tile.doGeneration();
+ }
+ for (Tile tile : tiles) {
+ tile.flush();
+ }
+ }
+}
@@ -0,0 +1,141 @@
+/*
+ * Public Domain
+ */
+package mapgeneration;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.newdawn.slick.Color;
+import org.newdawn.slick.Graphics;
+
+/**
+ *
+ * @author Robert 'Bobby' Zenz
+ */
+public class Tile {
+
+ private TileType type = TileType.UNKNOWN;
+ private TileType newType = TileType.UNKNOWN;
+ private List<Tile> neighbors = new ArrayList<Tile>();
+
+ public TileType getType() {
+ return type;
+ }
+
+ public void setType(TileType type) {
+ this.type = type;
+ }
+
+ public void init(Map parent, int x, int y) {
+ neighbors.clear();
+
+ for (int neighborX = Math.max(0, x - 1); neighborX < Math.min(parent.getWidth(), x + 2); neighborX++) {
+ for (int neighborY = Math.max(0, y - 1); neighborY < Math.min(parent.getHeight(), y + 2); neighborY++) {
+ if (neighborX != x || neighborY != y) {
+ neighbors.add(parent.getTile(neighborX, neighborY));
+ }
+ }
+ }
+ }
+
+ public void doGeneration() {
+ newType = type;
+
+ switch (type) {
+ case DESERT:
+ if ((getNeighborCount(TileType.WATER) + getNeighborCount(TileType.SNOW)) >= 3) {
+ newType = TileType.GRASSLAND;
+ }
+ if(getNeighborCount(TileType.GRASSLAND) > 4) {
+ newType = TileType.GRASSLAND;
+ }
+ break;
+
+ case GRASSLAND:
+ break;
+
+ case SNOW:
+ if(getNeighborCount(TileType.GRASSLAND) > 3) {
+ newType = TileType.GRASSLAND;
+ }
+ break;
+
+ case STONE:
+ if (getNeighborCount(TileType.GRASSLAND) >= 3) {
+ newType = TileType.GRASSLAND;
+ }
+ if (getNeighborCount(TileType.SNOW) > 2) {
+ newType = TileType.SNOW;
+ }
+ break;
+
+ case UNKNOWN:
+ break;
+
+ case WATER:
+ if (getNeighborCount(TileType.GRASSLAND) >= 3) {
+ newType = TileType.SNOW;
+ }
+ break;
+ }
+
+ if (getNeighborCount(TileType.WATER) >= 4) {
+ newType = TileType.WATER;
+ }
+ if (getNeighborCount(TileType.DESERT) >= 4) {
+ newType = TileType.DESERT;
+ }
+ }
+
+ public void flush() {
+ type = newType;
+ }
+
+ public Color getColor() {
+ switch (type) {
+ case DESERT:
+ return Color.yellow;
+
+ case GRASSLAND:
+ return Color.green;
+
+ case SNOW:
+ return Color.white;
+
+ case STONE:
+ return Color.gray;
+
+ case UNKNOWN:
+ return Color.magenta;
+
+ case WATER:
+ return Color.blue;
+
+ default:
+ return Color.orange;
+ }
+ }
+
+ public void render(Graphics g, int x, int y, int width, int height) {
+ g.setColor(getColor());
+ g.fillRect(x, y, width, height);
+
+ g.setColor(Color.black);
+
+// int stringY = 0;
+// for (Tile neighbor : neighbors) {
+// g.drawString(neighbor.getType().name(), x, y + stringY);
+// stringY += 15;
+// }
+ }
+
+ private int getNeighborCount(TileType type) {
+ int count = 0;
+ for (Tile neighbor : neighbors) {
+ if (neighbor.getType() == type) {
+ count++;
+ }
+ }
+ return count;
+ }
+}
@@ -0,0 +1,18 @@
+/*
+ * Public Domain
+ */
+
+package mapgeneration;
+
+/**
+ *
+ * @author Robert 'Bobby' Zenz
+ */
+public enum TileType {
+ UNKNOWN,
+ WATER,
+ GRASSLAND,
+ DESERT,
+ STONE,
+ SNOW
+}
@@ -0,0 +1,66 @@
+/*
+ * Public Domain
+ */
+package mapgeneration;
+
+import org.newdawn.slick.BasicGame;
+import org.newdawn.slick.Color;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+
+/**
+ *
+ * @author Robert 'Bobby' Zenz
+ */
+public class World extends BasicGame {
+
+ private Map map;
+
+ public World(String title, int width, int height) {
+ super(title);
+
+ map = new Map(width, height);
+ }
+
+ @Override
+ public void init(GameContainer container) throws SlickException {
+ map.init();
+ map.generate(0);
+ }
+
+ @Override
+ public void update(GameContainer container, int delta) throws SlickException {
+ Input input = container.getInput();
+
+ if (input.isKeyPressed(Input.KEY_RETURN)) {
+ map.doGeneration();
+ }
+ if (input.isKeyDown(Input.KEY_SPACE)) {
+ map.doGeneration();
+ }
+
+ if (input.isKeyPressed(Input.KEY_ESCAPE)) {
+ container.exit();
+ }
+ }
+
+ public void render(GameContainer container, Graphics g) throws SlickException {
+ g.setBackground(Color.white);
+
+ int tileWidth = container.getWidth() / map.getWidth();
+ int tileHeight = container.getHeight() / map.getHeight();
+
+ for (int idxX = 0; idxX < map.getWidth(); idxX++) {
+ for (int idxY = 0; idxY < map.getHeight(); idxY++) {
+ map.getTile(idxX, idxY).render(g, idxX * tileWidth, idxY * tileHeight, tileWidth, tileHeight);
+ g.setColor(Color.black);
+ //g.drawRect(idxX * tileWidth, idxY * tileHeight, tileWidth, tileHeight);
+ }
+ }
+
+ g.setColor(Color.darkGray);
+ g.drawString("FPS: " + Integer.toString(container.getFPS()), 5, 20);
+ }
+}

0 comments on commit 146d887

Please sign in to comment.