Merge branch 'master' of woodyfolsom.net:/opt/git/cs8803p3
This commit is contained in:
@@ -41,6 +41,10 @@ public class PCGLevel extends Level {
|
||||
private int type;
|
||||
private Random random;
|
||||
|
||||
public enum ChallengeType {
|
||||
GAP, ENEMY, HARDER_ENEMY, JUMP
|
||||
};
|
||||
|
||||
public PCGLevel(int width, int height) {
|
||||
super(width, height);
|
||||
}
|
||||
@@ -81,13 +85,16 @@ public class PCGLevel extends Level {
|
||||
LevelGrammar grammar;
|
||||
try {
|
||||
String grammarFileName = "grammars/overland.grm";
|
||||
grammar = LevelGrammarFactory.createGrammar(new File(grammarFileName));
|
||||
grammar = LevelGrammarFactory.createGrammar(new File(
|
||||
grammarFileName));
|
||||
System.out.println("Read grammar from file: " + grammarFileName);
|
||||
System.out.println("==== LEVEL GRAMMAR ====");
|
||||
System.out.println(grammar);
|
||||
System.out.println("=======================");
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Failed to parse grammar file due to exception: " + ex.getMessage());
|
||||
System.out
|
||||
.println("Failed to parse grammar file due to exception: "
|
||||
+ ex.getMessage());
|
||||
System.out.println("Defaulting to basic overland grammar.");
|
||||
grammar = LevelGrammarFactory.createGrammar();
|
||||
}
|
||||
@@ -142,7 +149,6 @@ public class PCGLevel extends Level {
|
||||
int length = 0;
|
||||
|
||||
if (TESTING) {
|
||||
difficulty = 10;
|
||||
int minElements = 5;
|
||||
int maxLen;
|
||||
int elementsSoFar = 0;
|
||||
@@ -192,19 +198,22 @@ public class PCGLevel extends Level {
|
||||
else {
|
||||
System.out.println("Generating level for component list: ");
|
||||
LevelParseTree parseTree = grammar.generateRandomTree(seed, width);
|
||||
|
||||
|
||||
int MAX_REGENS = 10;
|
||||
int nRegens = 0;
|
||||
while (!FitnessEvaluator.isFit(parseTree,profile,archetype) && nRegens < MAX_REGENS) {
|
||||
System.out.println("Generated level is NOT fit. Regenerating...");
|
||||
while (!FitnessEvaluator.isFit(parseTree, profile, archetype)
|
||||
&& nRegens < MAX_REGENS) {
|
||||
System.out
|
||||
.println("Generated level is NOT fit. Regenerating...");
|
||||
parseTree = grammar.generateRandomTree(seed, width);
|
||||
nRegens++;
|
||||
}
|
||||
|
||||
|
||||
if (nRegens == MAX_REGENS) {
|
||||
System.out.println("Failed to generate a fit level after " + nRegens + " attempts. Proceeding with unfit level.");
|
||||
System.out.println("Failed to generate a fit level after "
|
||||
+ nRegens + " attempts. Proceeding with unfit level.");
|
||||
}
|
||||
|
||||
|
||||
List<LevelComponent> levelTemplate = parseTree.getLevelTemplate();
|
||||
|
||||
for (LevelComponent lcomp : levelTemplate) {
|
||||
@@ -290,8 +299,10 @@ public class PCGLevel extends Level {
|
||||
if (x == 3) {
|
||||
setBlock(xo + x, floor - 1, Level.CANNON_MIDDLE);
|
||||
setBlock(xo + x, floor - 2, Level.CANNON_TOP);
|
||||
setBlock(xo + x, floor - 3,
|
||||
shouldAddChallenge() ? Level.CANNON_TOP
|
||||
setBlock(
|
||||
xo + x,
|
||||
floor - 3,
|
||||
shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? Level.CANNON_TOP
|
||||
: Level.CANNON_MIDDLE);
|
||||
setBlock(xo + x, floor - 4, Level.CANNON_TOP);
|
||||
}
|
||||
@@ -407,14 +418,14 @@ public class PCGLevel extends Level {
|
||||
if (maxLength >= 26) {
|
||||
|
||||
int floor = height - 1 - random.nextInt(4);
|
||||
int enemyType = shouldAddChallenge() ? (shouldAddChallenge() ? SpriteTemplate.ARMORED_TURTLE
|
||||
int enemyType = shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? SpriteTemplate.ARMORED_TURTLE
|
||||
: SpriteTemplate.GREEN_TURTLE)
|
||||
: SpriteTemplate.GOOMPA;
|
||||
int reward = shouldAddReward();
|
||||
boolean arc = random.nextBoolean();
|
||||
int numEnemies = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
numEnemies += shouldAddChallenge() ? 1 : 0;
|
||||
numEnemies += shouldAddChallenge(ChallengeType.ENEMY) ? 1 : 0;
|
||||
}
|
||||
|
||||
// Create the pit.
|
||||
@@ -494,11 +505,12 @@ public class PCGLevel extends Level {
|
||||
private int buildLemmingTrap(int xo, int maxLength) {
|
||||
if (maxLength >= 14) {
|
||||
int floor = height - 1 - random.nextInt(4);
|
||||
int enemyType = shouldAddChallenge() ? (shouldAddChallenge() ? SpriteTemplate.ARMORED_TURTLE
|
||||
int enemyType = shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? SpriteTemplate.ARMORED_TURTLE
|
||||
: SpriteTemplate.GREEN_TURTLE)
|
||||
: SpriteTemplate.GOOMPA;
|
||||
boolean flying = shouldAddChallenge() && shouldAddChallenge()
|
||||
&& shouldAddChallenge();
|
||||
boolean flying = shouldAddChallenge(ChallengeType.HARDER_ENEMY)
|
||||
&& shouldAddChallenge(ChallengeType.HARDER_ENEMY)
|
||||
&& shouldAddChallenge(ChallengeType.HARDER_ENEMY);
|
||||
int reward = shouldAddReward();
|
||||
|
||||
for (int x = 0; x < 18; x++) {
|
||||
@@ -662,7 +674,7 @@ public class PCGLevel extends Level {
|
||||
|
||||
// This is basically just a randomizer function to call whenever I need to
|
||||
// randomly add difficulty based on the user's skill level.
|
||||
private boolean shouldAddChallenge() {
|
||||
private boolean shouldAddChallenge(ChallengeType ct) {
|
||||
return random.nextInt(11) + 1 <= difficulty;
|
||||
}
|
||||
|
||||
@@ -796,9 +808,9 @@ public class PCGLevel extends Level {
|
||||
setBlock(xo + soFar + x, this.height - 4, Level.ROCK);
|
||||
}
|
||||
|
||||
if (addEnemy && shouldAddChallenge()) {
|
||||
if (addEnemy && shouldAddChallenge(ChallengeType.ENEMY)) {
|
||||
|
||||
enemyType = shouldAddChallenge() ? (shouldAddChallenge() ? (shouldAddChallenge() ? SpriteTemplate.ARMORED_TURTLE
|
||||
enemyType = shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? SpriteTemplate.ARMORED_TURTLE
|
||||
: SpriteTemplate.RED_TURTLE)
|
||||
: SpriteTemplate.GREEN_TURTLE)
|
||||
: SpriteTemplate.GOOMPA;
|
||||
@@ -840,10 +852,11 @@ public class PCGLevel extends Level {
|
||||
int pipeHeight;
|
||||
int gap = 0;
|
||||
boolean space;
|
||||
boolean deadGaps = shouldAddChallenge();
|
||||
boolean deadGaps = shouldAddChallenge(ChallengeType.GAP);
|
||||
|
||||
int numPipes;
|
||||
for (numPipes = 0; shouldAddChallenge(); numPipes++) {
|
||||
for (numPipes = 0; shouldAddChallenge(deadGaps ? ChallengeType.GAP
|
||||
: ChallengeType.JUMP); numPipes++) {
|
||||
}
|
||||
|
||||
localHeight = random.nextInt(2) + 1;
|
||||
@@ -870,7 +883,7 @@ public class PCGLevel extends Level {
|
||||
setBlock(xo + length + 1, this.height - 1 - y,
|
||||
Level.TUBE_TOP_RIGHT);
|
||||
|
||||
if (shouldAddChallenge()) {
|
||||
if (shouldAddChallenge(ChallengeType.ENEMY)) {
|
||||
setSpriteTemplate(xo + length, this.height - y,
|
||||
new SpriteTemplate(
|
||||
SpriteTemplate.JUMP_FLOWER, false));
|
||||
@@ -893,7 +906,7 @@ public class PCGLevel extends Level {
|
||||
}
|
||||
|
||||
for (gap = 0; gap < 4 && length + gap <= maxLength
|
||||
&& shouldAddChallenge(); gap++) {
|
||||
&& shouldAddChallenge(ChallengeType.JUMP); gap++) {
|
||||
|
||||
if (!deadGaps) {
|
||||
for (int y = 0; y < midFloor; y++) {
|
||||
@@ -917,7 +930,8 @@ public class PCGLevel extends Level {
|
||||
int length = 0;
|
||||
int gapLength;
|
||||
|
||||
for (gapLength = 1; shouldAddChallenge() && gapLength < 4; gapLength++) {
|
||||
for (gapLength = 1; shouldAddChallenge(ChallengeType.JUMP)
|
||||
&& gapLength < 4; gapLength++) {
|
||||
}
|
||||
|
||||
int maxNumPlatforms = (maxLength - 3) / (4 + gapLength);
|
||||
@@ -926,7 +940,10 @@ public class PCGLevel extends Level {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int numPlatforms = random.nextInt(maxNumPlatforms);
|
||||
int numPlatforms;
|
||||
for (numPlatforms = 0; numPlatforms < maxNumPlatforms
|
||||
&& shouldAddChallenge(ChallengeType.GAP); numPlatforms++) {
|
||||
}
|
||||
|
||||
if (numPlatforms > 1) {
|
||||
boolean found = false;
|
||||
@@ -1028,10 +1045,10 @@ public class PCGLevel extends Level {
|
||||
setBlock(xo + length + 2, this.height - heightMod, Level.ROCK);
|
||||
setBlock(xo + length + 3, this.height - heightMod, Level.ROCK);
|
||||
|
||||
if (shouldAddChallenge()) {
|
||||
if (shouldAddChallenge(ChallengeType.ENEMY)) {
|
||||
|
||||
enemyType = random.nextBoolean() ? SpriteTemplate.RED_TURTLE
|
||||
: (shouldAddChallenge() ? (shouldAddChallenge() ? SpriteTemplate.ARMORED_TURTLE
|
||||
: (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? SpriteTemplate.ARMORED_TURTLE
|
||||
: SpriteTemplate.GREEN_TURTLE)
|
||||
: SpriteTemplate.GOOMPA);
|
||||
|
||||
@@ -1040,7 +1057,7 @@ public class PCGLevel extends Level {
|
||||
this.height - heightMod - 1,
|
||||
new SpriteTemplate(
|
||||
enemyType,
|
||||
(enemyType == SpriteTemplate.RED_TURTLE && shouldAddChallenge())
|
||||
(enemyType == SpriteTemplate.RED_TURTLE && shouldAddChallenge(ChallengeType.HARDER_ENEMY))
|
||||
|| enemyType != SpriteTemplate.RED_TURTLE));
|
||||
}
|
||||
|
||||
@@ -1099,8 +1116,8 @@ public class PCGLevel extends Level {
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldAddChallenge() && allowEnemies) {
|
||||
int enemyType = shouldAddChallenge() ? (shouldAddChallenge() ? (shouldAddChallenge() ? (shouldAddChallenge()
|
||||
if (shouldAddChallenge(ChallengeType.ENEMY) && allowEnemies) {
|
||||
int enemyType = shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY) ? (shouldAddChallenge(ChallengeType.HARDER_ENEMY)
|
||||
|| (reward > 0 && length >= 5) ? SpriteTemplate.ARMORED_TURTLE
|
||||
: SpriteTemplate.CANNON_BALL)
|
||||
: SpriteTemplate.RED_TURTLE)
|
||||
@@ -1135,7 +1152,7 @@ public class PCGLevel extends Level {
|
||||
|
||||
else {
|
||||
setSpriteTemplate(xo + (length / 2), floor - 1,
|
||||
new SpriteTemplate(enemyType, shouldAddChallenge()));
|
||||
new SpriteTemplate(enemyType, shouldAddChallenge(ChallengeType.HARDER_ENEMY)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user