Skip to content

Commit

Permalink
DungeonGenerator: RoomSizeTable
Browse files Browse the repository at this point in the history
  • Loading branch information
tukkek committed Feb 16, 2018
1 parent 7db4874 commit 0880285
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 10 deletions.
12 changes: 10 additions & 2 deletions javelin/controller/generator/dungeon/DungeonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ public class DungeonGenerator {
static final int DEBUGSIZE = 1;
static final int POOLTARGET = 100;

public String ascii;
public LevelTables tables = new LevelTables();
public char[][] grid;
public String ascii;

LinkedList<Template> pool = new LinkedList<Template>();
LevelTables tables = new LevelTables();
VirtualMap map = new VirtualMap();

static int ntemplates;
static int ncorridors;
private static int minrooms;
private static int maxrooms;
/**
* TODO temporary: will need to be refactored when more than one level can
* be generated (with one set of tables/parameters per level) and/or for
* multithreading. Should be as simple as passing an instance of this or of
* a new class GeneratorLevel to Templates.
*/
public static DungeonGenerator instance;

static {
setupparameters();
Expand All @@ -45,6 +52,7 @@ public class DungeonGenerator {
* {@link #generate(int, int)}.
*/
private DungeonGenerator(int sizehint) {
instance = this;
generatepool(sizehint);
draw(pool.pop(), new Point(0, 0));
/* TODO make this a Table 5±10 */
Expand Down
12 changes: 9 additions & 3 deletions javelin/controller/generator/dungeon/tables/DungeonTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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();
Expand All @@ -23,12 +22,19 @@ public Row roll() {
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());
for (int i = 0; i < table.size(); i++) {
clone.table.set(i, clone.table.get(i).clone());
}
clone.modify();
return clone;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}

protected void modify() {
for (Row r : table) {
r.modify();
}
}
}
5 changes: 3 additions & 2 deletions javelin/controller/generator/dungeon/tables/LevelTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
public class LevelTables {
HashMap<Class<? extends DungeonTable>, DungeonTable> tables = new HashMap<Class<? extends DungeonTable>, DungeonTable>();

public DungeonTable get(Class<? extends DungeonTable> table) {
public <K extends DungeonTable> K get(Class<K> table) {
DungeonTable t = tables.get(table);
if (t == null) {
try {
t = table.newInstance();
t.modify();
tables.put(table, t);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
return t;
return (K) t;
}
}
65 changes: 65 additions & 0 deletions javelin/controller/generator/dungeon/tables/NumericTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package javelin.controller.generator.dungeon.tables;

import tyrant.mikera.engine.RPG;

/**
* Subverts {@link DungeonTable} to instead provice a number and not a
* {@link Row} object, making it easier to implement things like "room size
* table". A value can be returned via {@link #rollvalue()}.
*
* Subclasses will need to implement a zero-argument constructor.
*
* @author alex
*/
public class NumericTable extends DungeonTable {
int min;
int max;
int delta;
boolean optional;
/**
* Exposed amount of modification.
*/
public int change;

class NumericValue extends Row {
final int value;

public NumericValue(int value) {
super(1, 1, 0, false);
this.value = value;
}
}

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

@Override
public NumericValue roll() {
return (NumericValue) super.roll();
}

public int rollvalue() {
return roll().value;
}

@Override
protected void modify() {
change = RPG.r(-delta, +delta);
modify(change);
}

void modify(int change) {
int floor = optional ? 0 : 1;
min = Math.max(min + change, floor);
max = Math.max(max + change, floor);
table.clear();
for (int i = min; i <= max; i++) {
add(new NumericValue(i));
}
}
}
43 changes: 43 additions & 0 deletions javelin/controller/generator/dungeon/tables/RoomSizeTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package javelin.controller.generator.dungeon.tables;

import javelin.controller.Point;

public class RoomSizeTable extends NumericTable {
public class RoomSize extends NumericTable {
public RoomSize(int min, int max, int delta, boolean optional) {
super(min, max, delta, optional);
}

@Override
protected void modify() {
super.modify();
while (min <= 2 && min <= 2) {
super.modify();
}
}
}

NumericTable width = new RoomSize(3, 7, 3, false);
NumericTable height = new RoomSize(1, 6, 2, false);

public RoomSizeTable() {
super(0, 0, 0, false);
width.modify();
height.modify(width.change);
}

@Override
protected void modify() {
width.modify();
height.modify(width.change);
}

@Override
public NumericValue roll() {
throw new UnsupportedOperationException();
}

public Point rolldimensions() {
return new Point(width.rollvalue(), height.rollvalue());
}
}
7 changes: 5 additions & 2 deletions javelin/controller/generator/dungeon/tables/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ public Row(int min, int max, int change, boolean optional) {
this.max = max;
this.change = change;
this.optional = optional;
change();
}

void change() {
public Row(int min, int max, boolean optional) {
this(min, max, (min + max) / 2, optional);
}

void modify() {
int amount = RPG.r(-change, +change);
min += amount;
max += amount;
Expand Down
6 changes: 5 additions & 1 deletion javelin/controller/generator/dungeon/template/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.List;

import javelin.controller.Point;
import javelin.controller.generator.dungeon.DungeonGenerator;
import javelin.controller.generator.dungeon.Roomlike;
import javelin.controller.generator.dungeon.tables.RoomSizeTable;
import javelin.controller.generator.dungeon.template.Iterator.TemplateTile;
import javelin.controller.generator.dungeon.template.corridor.LinearCorridor;
import javelin.controller.generator.dungeon.template.corridor.WindingCorridor;
Expand Down Expand Up @@ -52,7 +54,9 @@ protected void init(int width, int height) {
}

protected void initrandom() {
init(RPG.r(3, 7), RPG.r(1, 6));
Point dimensions = DungeonGenerator.instance.tables.get(RoomSizeTable.class)
.rolldimensions();
init(dimensions.x, dimensions.x);
}

public abstract void generate();
Expand Down

0 comments on commit 0880285

Please sign in to comment.