world generation working

master
Peter Babič 9 years ago
parent 43d0c00b71
commit 00a72936bb
  1. 33
      src/world/Coordinate.java
  2. 106
      src/world/Generator.java
  3. 55
      src/world/Point.java
  4. 23
      src/world/RouteFinder.java

@ -1,33 +0,0 @@
package world;
public class Coordinate {
private int x = 0;
private int y = 0;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public Coordinate()
{
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

@ -1,78 +1,94 @@
package world;
import java.util.HashSet;
import java.util.Random;
import world.Coordinate;
import java.util.Set;
import world.Point;
public class Generator {
private static final int SEED = 1;
private static final int SEED = 2;
public static final int PX_STEP = 64;
public static final int PX_OFFSET = 32;
public static final int COLS = 12;
public static final int COLS = 16;
public static final int ROWS = 10;
public static final int NUM_OBSTACLES = (int) (COLS * ROWS * 0.3);
public static final int NUM_OBSTACLES = (int) (COLS * ROWS * 0.25);
// Contains poistions of obstacles, start point and destination
public Coordinate[] obstacles = new Coordinate[NUM_OBSTACLES];
public Coordinate start = new Coordinate();
public Coordinate stop = new Coordinate();
// public static Point[] obstacles = new Point[NUM_OBSTACLES];
// public static Point[] agent = new Point[2];
public Set<Point> obstacles = new HashSet<>();
public Point start;
Random rand;
// Excuse the crudity of the code, never programmed in Java before
// Could not resolve "pass-by-reference" in Java, thus the code is repeating :/
public static void main(String[] args) {
public Generator() {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
rand = new Random();
rand.setSeed(SEED);
// add the obstacles
for (int NdxObstacle = 0; NdxObstacle < NUM_OBSTACLES; NdxObstacle++) {
// try to find free place for an obstacle
while (true) {
int x = randInt(0, COLS);
int y = randInt(0, ROWS);
if (obstacles[NdxObstacle] != null) {
obstacles[NdxObstacle].setX(x);
obstacles[NdxObstacle].setY(y);
break;
}
}
addPoint();
}
// Add the starting position
addStart();
// Add the
// try to find free place for a start position
}
public void addPoint() {
// try to find free place for an obstacle
while (true) {
int x = randInt(0, COLS);
int y = randInt(0, ROWS);
if (start != null) {
start.setX(x);
start.setY(y);
break;
int x = randInt(0, COLS -1);
int y = randInt(0, ROWS -1);
Point point = new Point(x, y);
if (!obstacles.contains(point)) {
obstacles.add(point);
return;
}
}
// try to find free place for a destination
}
public void addStart() {
// try to find free place for an obstacle
while (true) {
int x = randInt(0, COLS);
int y = randInt(0, ROWS);
if (stop != null) {
stop.setX(x);
stop.setY(y);
break;
// -1 is due the offset
int x = randInt(0, COLS -1);
int y = randInt(0, ROWS -1);
Point point = new Point(x, y);
if (!obstacles.contains(point)) {
start = point;
return;
}
}
}
//
// public boolean isObstacle(int x, int y) {
// return this.obstacles
// }
//
/**
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
@ -85,11 +101,7 @@ public class Generator {
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
private static int randInt(int min, int max) {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random(SEED);
private int randInt(int min, int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive

@ -0,0 +1,55 @@
package world;
public class Point{
private int x,y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getX_() {
return x * Generator.PX_STEP + Generator.PX_OFFSET;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public int getY_() {
return y * Generator.PX_STEP + Generator.PX_OFFSET;
}
public void setY(int y) {
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
if (y != point.y) return false;
return true;
}
@Override
public int hashCode() {
int result = x;
result = Generator.PX_STEP * result + y;
return result;
}
}

@ -28,10 +28,10 @@ public class RouteFinder {
Generator gen = new Generator();
// Create the battlefield
int NumPixelRows = Generator.ROWS * Generator.PX_STEP;
int NumPixelCols = Generator.COLS * Generator.PX_STEP;
int NumPixelRows = Generator.ROWS * Generator.PX_STEP;
BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols);
BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelCols, NumPixelRows);
/*
@ -42,21 +42,22 @@ public class RouteFinder {
RobotSpecification[] existingRobots = new RobotSpecification[Generator.NUM_OBSTACLES + 1];
RobotSetup[] robotSetups = new RobotSetup[Generator.NUM_OBSTACLES + 1];
for (int NdxObstacle = 0; NdxObstacle < Generator.NUM_OBSTACLES; NdxObstacle++) {
double InitialObstacleCol = (double) gen.obstacles[NdxObstacle].getX();
double InitialObstacleRow = (double) gen.obstacles[NdxObstacle].getY();
int NdxObstacle = 0;
for (Point p : gen.obstacles) {
// added offset so the tanks are in the middle of the tiles
double InitialObstacleCol = (double) p.getX_();
double InitialObstacleRow = (double) p.getY_();
existingRobots[NdxObstacle] = modelRobots[0];
robotSetups[NdxObstacle] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0);
robotSetups[NdxObstacle++] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0);
}
/*
* Create the agent and place it in a random position without obstacle
*/
existingRobots[world.Generator.NUM_OBSTACLES] = modelRobots[1];
double InitialAgentCol = (double) gen.start.getX();
double InitialAgentRow = (double) gen.start.getY();
robotSetups[world.Generator.NUM_OBSTACLES] = new RobotSetup(InitialAgentCol, InitialAgentRow, 0.0);
existingRobots[Generator.NUM_OBSTACLES] = modelRobots[1];
double InitialAgentCol = (double) gen.start.getX_();
double InitialAgentRow = (double) gen.start.getY_();
robotSetups[Generator.NUM_OBSTACLES] = new RobotSetup(InitialAgentCol, InitialAgentRow, 0.0);
/* Create and run the battle */
BattleSpecification battleSpec = new BattleSpecification(battlefield, numberOfRounds, inactivityTime, gunCoolingRate,

Loading…
Cancel
Save