Skip to content

Commit

Permalink
more work on DungeonGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
tukkek committed Feb 13, 2018
1 parent d331265 commit 526dd3d
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 21 deletions.
24 changes: 18 additions & 6 deletions javelin/controller/generator/dungeon/DungeonGenerator.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package javelin.controller.generator.dungeon;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;

import javelin.controller.db.Preferences;
import javelin.controller.generator.dungeon.tables.ConnectionTable;
import javelin.controller.generator.dungeon.template.Irregular;
import javelin.controller.generator.dungeon.template.Template;

Expand All @@ -16,20 +17,31 @@ public class DungeonGenerator {
*/
static final int PERMUTATIONS = 10;

ArrayList<Template> pool = new ArrayList<Template>();
LinkedList<Template> pool = new LinkedList<Template>();
ConnectionTable connections = new ConnectionTable();
VirtualMap map = new VirtualMap();
String log = "";

public DungeonGenerator() {
generatepool();
draw(pool.pop());
// log = pool.pop().toString();
write();
}

void draw(Template root) {
map.draw(root, 0, 0);
log += root.toString() + "\n";
log += map.toString() + "\n";
}

void generatepool() {
for (Template t : TEMPLATES) {
for (int i = 0; i < PERMUTATIONS; i++) {
pool.add(t.create());
}
}
Collections.shuffle(pool);
for (Template t : pool) {
log += t + "\n";
}
write();
}

void write() { // debug
Expand Down
59 changes: 59 additions & 0 deletions javelin/controller/generator/dungeon/VirtualMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package javelin.controller.generator.dungeon;

import java.util.HashMap;

import javelin.controller.Point;
import javelin.controller.generator.dungeon.template.Template;

public class VirtualMap {
public Character fill = '_';
HashMap<Point, Character> map = new HashMap<Point, Character>();

public boolean draw(Template t, int rootx, int rooty, boolean check) {
for (int x = 0; x < t.width; x++) {
for (int y = 0; y < t.height; y++) {
Point p = new Point(x + rootx, y + rooty);
if (!check) {
map.put(p, t.tiles[x][y]);
continue;
}
Character c = map.get(p);
if (c != null) {
return false;
}
}
}
return true;
}

public String rasterize() {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (Point p : map.keySet()) {
min = Math.min(min, Math.min(p.x, p.y));
max = Math.max(max, Math.max(p.x, p.y));
}
StringBuilder raster = new StringBuilder();
for (int y = max; y >= min; y--) {
for (int x = min; x <= max; x++) {
Character c = map.get(new Point(x, y));
raster.append(c == null ? fill : c);
}
raster.append('\n');
}
return raster.toString();
}

@Override
public String toString() {
return rasterize();
}

public boolean draw(Template t, int x, int y) {
if (!draw(t, x, y, true)) {
return false;
}
draw(t, x, y, false);
return true;
}
}
11 changes: 11 additions & 0 deletions javelin/controller/generator/dungeon/tables/ConnectionTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package javelin.controller.generator.dungeon.tables;

public class ConnectionTable extends DungeonTable {
public static final Row CORRIDOR = new Row(2, 2, 2, true);
public static final Row ROOM = new Row(2, 2, 2, true);

public ConnectionTable() {
add(CORRIDOR);
add(ROOM);
}
}
34 changes: 34 additions & 0 deletions javelin/controller/generator/dungeon/tables/DungeonTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package javelin.controller.generator.dungeon.tables;

import java.util.ArrayList;

import tyrant.mikera.engine.RPG;

public class DungeonTable implements Cloneable {
ArrayList<Row> table = new ArrayList<Row>();
ArrayList<Row> rows = new ArrayList<Row>();

void add(Row r) {
int n = r.getamount();
for (int i = 0; i < n; i++) {
table.add(r);
}
}

public Row roll() {
return RPG.pick(table);
}

@Override
protected DungeonTable clone() {
try {
DungeonTable clone = (DungeonTable) super.clone();
for (int i = 0; i < rows.size(); i++) {
clone.rows.set(i, clone.rows.get(i).clone());
}
return clone;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
42 changes: 42 additions & 0 deletions javelin/controller/generator/dungeon/tables/Row.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package javelin.controller.generator.dungeon.tables;

import tyrant.mikera.engine.RPG;

public class Row implements Cloneable {
int min;
int max;
int change;
boolean optional;

public Row(int min, int max, int change, boolean optional) {
super();
this.min = min;
this.max = max;
this.change = change;
this.optional = optional;
change();
}

void change() {
int amount = RPG.r(-change, +change);
min += amount;
max += amount;
if (!optional) {
min = Math.max(1, min);
max = Math.max(1, max);
}
}

public int getamount() {
return RPG.r(min, max);
}

@Override
protected Row clone() {
try {
return (Row) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
22 changes: 11 additions & 11 deletions javelin/controller/generator/dungeon/template/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ public abstract class Direction {
ArrayList<Point> getborder(Template t) {
ArrayList<Point> border = new ArrayList<Point>();
for (int x = 0; x < t.width; x++) {
border.add(new Point(x, 0));
border.add(new Point(x, t.height - 1));
}
return border;
}
};
public static final Direction EAST = new Direction("East", +1, 0) {
public static final Direction SOUTH = new Direction("South", 0, +1) {
@Override
ArrayList<Point> getborder(Template t) {
ArrayList<Point> border = new ArrayList<Point>();
for (int y = 0; y < t.width; y++) {
border.add(new Point(t.width - 1, y));
for (int x = 0; x < t.width; x++) {
border.add(new Point(x, 0));
}
return border;
}
};
public static final Direction SOUTH = new Direction("South", 0, +1) {
public static final Direction EAST = new Direction("East", -1, 0) {
@Override
ArrayList<Point> getborder(Template t) {
ArrayList<Point> border = new ArrayList<Point>();
for (int x = 0; x < t.width; x++) {
border.add(new Point(x, t.height - 1));
for (int y = 0; y < t.height; y++) {
border.add(new Point(t.width - 1, y));
}
return border;
}
};
public static final Direction WEST = new Direction("West", -1, 0) {
public static final Direction WEST = new Direction("West", +1, 0) {
@Override
ArrayList<Point> getborder(Template t) {
ArrayList<Point> border = new ArrayList<Point>();
for (int y = 0; y < t.width; y++) {
for (int y = 0; y < t.height; y++) {
border.add(new Point(0, y));
}
return border;
Expand All @@ -49,12 +49,12 @@ ArrayList<Point> getborder(Template t) {
public static final Direction[] DIRECTIONS = new Direction[] { NORTH, SOUTH,
WEST, EAST };

public Point delta;
public Point reverse;
public String name;

private Direction(String name, int x, int y) {
this.name = name;
delta = new Point(x, y);
reverse = new Point(x, y);
}

abstract ArrayList<Point> getborder(Template t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Irregular extends Template {
private static final Point[] ADJACENT = new Point[] { new Point(-1, 0),
new Point(+1, 0), new Point(0, -1), new Point(0, +1) };
private static final int PERCENTMIN = 30;
private static final int PERCENTMAX = 70;
private static final int PERCENTMAX = 60;

@Override
public void generate() {
Expand Down
6 changes: 3 additions & 3 deletions javelin/controller/generator/dungeon/template/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ void rotate() {
@Override
public String toString() {
String s = "";
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int y = height - 1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
s += tiles[x][y];
}
s += "\n";
Expand Down Expand Up @@ -139,7 +139,7 @@ Point findentry(Direction d) {
ArrayList<Point> doors = d.getborder(this);
Collections.shuffle(doors);
for (Point door : doors) {
if (tiles[door.x - d.delta.x][door.y - d.delta.y] == FLOOR) {
if (tiles[door.x + d.reverse.x][door.y + d.reverse.y] == FLOOR) {
return door;
}
}
Expand Down

0 comments on commit 526dd3d

Please sign in to comment.