Skip to content

Commit

Permalink
Implemented alternative plate splitting algorithm for plate creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchamann committed Apr 16, 2011
1 parent 84887d1 commit e9d2b9d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
15 changes: 14 additions & 1 deletion src/ca/hamann/mapgen/gui/genscreen/NewMapDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
Expand Down Expand Up @@ -45,6 +46,9 @@ public class NewMapDialog extends JDialog {
private JLabel lazySpreadingLabel;
private JCheckBox lazySpreadingCheckBox;

private JLabel plateCreationMethodLabel;
private JComboBox plateCreationMethodComboBox;

private JButton cancel;
private JButton makeMap;

Expand Down Expand Up @@ -225,6 +229,13 @@ public boolean shouldYieldFocus(JComponent input) {

addLabelComponentPair(c, lazySpreadingLabel, lazySpreadingCheckBox);

plateCreationMethodLabel = new JLabel("Plate Creation Method");
plateCreationMethodComboBox = new JComboBox(new String[] {
"Flood Fill", "Plate Splitting" });
plateCreationMethodComboBox.setEditable(false);

addLabelComponentPair(c, plateCreationMethodLabel, plateCreationMethodComboBox);

cancel = new JButton("Cancel");

cancel.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -295,8 +306,10 @@ private void initConfig() {
}

config.setStartingSeed(seed);

config.setLazySpreading(lazySpreadingCheckBox.isSelected());

config.setPlateCreationMethod((String) plateCreationMethodComboBox.getSelectedItem());

screen.initMaps(config);
}
Expand Down
7 changes: 5 additions & 2 deletions src/ca/hamann/mapgen/tectonic/PlateGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Random;

import ca.hamann.mapgen.MapConfiguration;
import ca.hamann.mapgen.containers.LocationIterator;
import ca.hamann.mapgen.containers.LocationList;
import ca.hamann.mapgen.direction.DirectionSequence;
import ca.hamann.mapgen.direction.DirectionSequenceGenerator;
Expand Down Expand Up @@ -46,8 +47,10 @@ public void seedPlates() {
}

private void fillMapWithStartPlate() {
// TODO Auto-generated method stub

LocationIterator iterator = grid.iterator();
while (iterator.hasNext()) {
tectMap.setPlateIndex(iterator.next(), 1);
}
}

private int[] randomizeStartingLocations(int plateCount) {
Expand Down
67 changes: 37 additions & 30 deletions src/ca/hamann/mapgen/tectonic/PlateSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,57 @@

import ca.hamann.mapgen.containers.LocationIterator;
import ca.hamann.mapgen.containers.LocationList;
import ca.hamann.mapgen.gui.genscreen.MapProcess;
import ca.hamann.mapgen.sinusoidal.SinusoidalLocation;

public class PlateSplitter {

private TectonicMap tectMap;
private PlateSpreader spreader;
private TectonicMap tectMap;
private PlateSpreader spreader;

private Random random = new Random(0);
private Random random = new Random(0);

public PlateSplitter(TectonicMap tectMap) {
this.tectMap = tectMap;
spreader = new PlateSpreader(tectMap);
}
public PlateSplitter(TectonicMap tectMap) {
this.tectMap = tectMap;
spreader = new PlateSpreader(tectMap);
}

public LocationList collectPlate(int plateIndex) {
LocationList plate = new LocationList();
LocationIterator iterator = tectMap.getGrid().iterator();
public LocationList collectPlate(int plateIndex) {
LocationList plate = new LocationList();
LocationIterator iterator = tectMap.getGrid().iterator();

while (iterator.hasNext()) {
SinusoidalLocation next = iterator.next();
while (iterator.hasNext()) {
SinusoidalLocation next = iterator.next();

if (tectMap.getPlateIndex(next) == plateIndex) {
tectMap.setPlateIndex(next, 0);
plate.add(next);
}
}
if (tectMap.getPlateIndex(next) == plateIndex) {
tectMap.setPlateIndex(next, 0);
plate.add(next);
}
}

return plate;
}
return plate;
}

public void splitPlate(int oldPlateIndex, int emptyPlateIndex) {
LocationList oldPlate = collectPlate(oldPlateIndex);
public void splitPlate(int oldPlateIndex, int emptyPlateIndex) {
LocationList oldPlate = collectPlate(oldPlateIndex);

SinusoidalLocation loc1 = oldPlate.remove(random.nextInt(oldPlate.size()));
SinusoidalLocation loc2 = oldPlate.remove(random.nextInt(oldPlate.size()));
SinusoidalLocation loc1 = oldPlate.remove(random.nextInt(oldPlate
.size()));
SinusoidalLocation loc2 = oldPlate.remove(random.nextInt(oldPlate
.size()));

spreader.getNeighbourSet(oldPlateIndex).add(loc1);
spreader.getNeighbourSet(emptyPlateIndex).add(loc2);
spreader.getNeighbourSet(oldPlateIndex).add(loc1);
spreader.getNeighbourSet(emptyPlateIndex).add(loc2);

spreader.generatePlates();
spreader.floodFillPlates();

EmptyLocationFiller filler = new EmptyLocationFiller(tectMap);
filler.setRiftValleyMaker(false);
filler.fillEmptyLocations();
}
EmptyLocationFiller filler = new EmptyLocationFiller(tectMap);
filler.setRiftValleyMaker(false);
filler.fillEmptyLocations();
}

public void setProcess(MapProcess process) {
spreader.setProcess(process);
}

}
28 changes: 26 additions & 2 deletions src/ca/hamann/mapgen/tectonic/PlateSpreader.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ private boolean isLocationFree(SinusoidalLocation loc) {
}

public TectonicMap generatePlates() {
String plateCreationMethod = config.getPlateCreationMethod();

if ("Flood Fill".equals(plateCreationMethod)) {
floodFillPlates();
} else if ("Plate Splitting".equals(plateCreationMethod)) {
splitPlates();
}

return tectMap;
}

private void splitPlates() {
int plateCount = 1;
PlateSplitter splitter = new PlateSplitter(tectMap);
splitter.setProcess(process);
while (plateCount < config.getPlateCount()) {

int randomPlate = floodFillRandom.nextInt(plateCount) + 1;
splitter.splitPlate(randomPlate, plateCount + 1);
plateCount++;
}

tectMap.generateBaseElevations();
}

public void floodFillPlates() {
Set<Integer> keys = edgeSets.keySet();
boolean done = false;
int count = 0;
Expand All @@ -131,8 +157,6 @@ public TectonicMap generatePlates() {
updateGui();
}
}

return tectMap;
}

protected void updateGui() {
Expand Down

0 comments on commit e9d2b9d

Please sign in to comment.