A boring overland world was generated from a grammar!

This commit is contained in:
Woody Folsom
2012-03-17 14:15:38 -04:00
parent 8bedf88fd3
commit 653c7fc3df
7 changed files with 231 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package dk.itu.mario.level;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import dk.itu.mario.MarioInterface.GamePlay;
@@ -11,6 +12,7 @@ import dk.itu.mario.engine.sprites.SpriteTemplate;
import dk.itu.mario.level.grammar.GrammarTuner;
import dk.itu.mario.level.grammar.LevelGrammar;
import dk.itu.mario.level.grammar.LevelGrammarFactory;
import dk.itu.mario.level.grammar.LevelParseTree;
import dk.itu.mario.level.matcher.ArchetypeMatcher;
import dk.itu.mario.level.matcher.LevelArchetype;
import dk.itu.mario.level.matcher.PlayerProfile;
@@ -82,6 +84,7 @@ public class PCGLevel extends Level {
grammar = GrammarTuner.tune(grammar, profile, archetype);
System.out.println("Creating level.");
create(seed, profile, archetype, grammar);
}
@Override
@@ -109,22 +112,42 @@ public class PCGLevel extends Level {
}
public void create(long seed, PlayerProfile profile,
private void create(long seed, PlayerProfile profile,
LevelArchetype archetype, LevelGrammar grammar) {
if (dataRecorder == DataRecorder.BLANK_RECORD) {
System.out
.println("DataRecorder record is BLANK - using GamePlay metrics only.");
}
System.out.println("Sample level parameters: "
+ grammar.generateRandom(seed));
this.type = archetype.getTypeInt();
this.difficulty = archetype.getDifficultyLevel();
lastSeed = seed;
random = new Random(seed);
System.out.println("Generating level for component list: ");
LevelParseTree parseTree = grammar.generateRandomTree(seed, width);
List<LevelComponent> levelTemplate = parseTree.getLevelTemplate();
int length = 0;
for (LevelComponent lcomp : levelTemplate) {
LevelComponent.TYPE lctype = lcomp.getType();
System.out.println("Building for: " + lcomp);
switch (lctype) {
case FLAT:
while (length < Math.min(width-64,lcomp.getEnd())) {
length += buildStraight(length, lcomp.getEnd(), true);
}
break;
default :
System.out.println("Cannot build level segment for unrecognized LevelComponent type: " + type);
}
}
System.out.println("Total length built: " + length);
/* Original non-PCG code:
// create the start location
int length = buildStraight(0, width, true);
length += buildPipeJump(length, width - length);
@@ -134,13 +157,17 @@ public class PCGLevel extends Level {
while (length < width - 64) {
length += buildStraight(length, width - length, true);
}
*/
// set the end piece
int floor = height - 1 - random.nextInt(4);
xExit = length + 8;
yExit = floor;
fillEndPiece(length, floor);
}
private void fillEndPiece(int length, int floor) {
// fills the end piece
for (int x = length; x < width; x++) {
for (int y = 0; y < height; y++) {
@@ -168,9 +195,7 @@ public class PCGLevel extends Level {
}
fixWalls();
}
private void addEnemyLine(int x0, int x1, int y) {
for (int x = x0; x < x1; x++) {
if (random.nextInt(35) < difficulty + 1) {