use of com.microsoft.Malmo.Utils.BlockDrawingHelper in project malmo by Microsoft.
the class ClassroomDecoratorImplementation method buildOnWorld.
@Override
public void buildOnWorld(MissionInit missionInit) throws DecoratorException {
this.drawContext = new BlockDrawingHelper();
if (this.buildingWidth == 0) {
// We are using complexity so these need to be sampled from the Gaussian
this.buildingWidth = Math.max((int) (rand.nextGaussian() * 2 + this.buildingComplexity * MAX_BUILDING_SIZE + MIN_ROOM_SIZE), MIN_ROOM_SIZE);
this.buildingLength = Math.max((int) (rand.nextGaussian() * 2 + this.buildingComplexity * MAX_BUILDING_SIZE + MIN_ROOM_SIZE), MIN_ROOM_SIZE);
this.buildingHeight = Math.max(Math.max(this.buildingWidth, this.buildingLength) * ROOM_HEIGHT / MIN_ROOM_SIZE, ROOM_HEIGHT);
}
// create the room grid
ArrayList<Room> rooms = this.createRooms();
// randomize the indices used to query different divisions. This has the effect of making
// the path to the goal random each time.
this.shuffleIndices();
// determine how long the path to the goal should be
if (this.pathLength == 0) {
this.pathLength = (int) ((1 - 0.25 * this.pathComplexity) * rooms.size()) - 1;
} else {
this.pathLength = Math.min(this.pathLength, rooms.size() - 1);
}
// find a path to the goal
ArrayList<Divider> path = new ArrayList<Divider>();
Room startRoom, goalRoom;
if (this.pathLength > 0) {
for (Room room : rooms) {
for (Room markRoom : rooms) {
markRoom.mark = false;
}
if (findPath(path, room, this.pathLength)) {
break;
}
}
if (path.size() < this.pathLength) {
// error
throw new DecoratorException("Unable to find path to goal");
}
startRoom = path.get(0).getIn();
goalRoom = path.get(path.size() - 1).getOut();
for (Divider divider : path) {
divider.setHint(this.rand.nextDouble() < this.hintLikelihood);
}
// create all of the obstacles along the path
createObstacles(path);
} else {
startRoom = goalRoom = rooms.get(0);
startRoom.isComplete = true;
}
// find orphan rooms
for (Room room : rooms) {
if (room.isComplete) {
continue;
}
for (Room markRoom : rooms) {
markRoom.mark = false;
}
path.clear();
if (findPath(path, room, rooms.size())) {
// portion of the building back to the orphan room.
for (Divider obstacle : path) {
obstacle.reverse();
}
// create all of the obstacles along the path
createObstacles(path);
} else {
throw new DecoratorException("Unable to join orphan room to goal path");
}
}
// carve out the building
World world = MinecraftServer.getServer().getEntityWorld();
this.drawContext.beginDrawing(world);
for (int x = START_X; x < START_X + this.buildingWidth; x++) {
for (int y = START_Y; y < START_Y + this.buildingHeight; y++) {
for (int z = START_Z; z < START_Z + this.buildingLength; z++) {
world.setBlockToAir(new BlockPos(x, y, z));
}
}
}
// this should clear all of the torches and levers left over from last time. It doesn't.
drawContext.clearEntities(world, START_X - 1, START_Y - 1, START_Z - 1, START_X + this.buildingWidth, START_Y + this.buildingHeight, START_Z + this.buildingLength);
// draw the rooms
for (Room room : rooms) {
room.draw(world, this.rand, this.palette);
}
// place goal
setBlockState(world, new BlockPos(goalRoom.x + this.rand.nextInt(goalRoom.width - 4) + 2, goalRoom.y, goalRoom.z + goalRoom.length - 2), this.palette.goal);
this.drawContext.endDrawing(world);
// set the agent positions
PosAndDirection p2 = new PosAndDirection();
p2.setX(new BigDecimal(startRoom.x + this.rand.nextInt(goalRoom.width - 2) + 0.5));
p2.setY(new BigDecimal(1 + startRoom.y));
p2.setZ(new BigDecimal(startRoom.z + 0.5));
// TODO - for the moment, force all players to being at the maze start point - but this needs to be optional.
for (AgentSection as : missionInit.getMission().getAgentSection()) {
p2.setPitch(as.getAgentStart().getPlacement().getPitch());
p2.setYaw(as.getAgentStart().getPlacement().getYaw());
as.getAgentStart().setPlacement(p2);
}
}
use of com.microsoft.Malmo.Utils.BlockDrawingHelper in project malmo by Microsoft.
the class MazeDecoratorImplementation method placeBlocks.
private void placeBlocks(World world, Cell[] grid, Cell start, Cell end) {
BlockDrawingHelper drawContext = new BlockDrawingHelper();
drawContext.beginDrawing(world);
int scale = this.mazeParams.getSizeAndPosition().getScale();
// First remove any entities lying around in our area:
drawContext.clearEntities(world, this.xOrg, this.yOrg, this.zOrg, this.xOrg + this.width * scale, this.yOrg + this.mazeParams.getSizeAndPosition().getHeight(), this.zOrg + this.length * scale);
// Clear a volume of air, lay a carpet, and put the random pavement over it:
for (int x = 0; x < this.width * scale; x++) {
for (int z = 0; z < this.length * scale; z++) {
for (int y = 0; y < this.mazeParams.getSizeAndPosition().getHeight(); y++) {
world.setBlockToAir(new BlockPos(x + this.xOrg, y + this.yOrg, z + this.zOrg));
}
BlockPos bp = new BlockPos(x + this.xOrg, this.yOrg, z + this.zOrg);
drawContext.setBlockState(world, bp, this.floorBlock);
Cell c = grid[(x / scale) + ((z / scale) * this.width)];
XMLBlockState bs = (c == null) ? this.gapBlock : (c.isOnOptimalPath ? this.optimalPathBlock : this.pathBlock);
int h = (c == null) ? this.gapHeight : (c.isOnOptimalPath ? this.optimalPathHeight : this.pathHeight);
if (c != null && c.isSubgoal) {
bs = this.subgoalPathBlock;
h = this.subgoalHeight;
}
if (c != null && c.isWaypoint && x % scale == scale / 2 && z % scale == scale / 2) {
if (this.mazeParams.getWaypoints().getWaypointBlock() != null) {
bs = this.waypointBlock;
h = this.pathHeight;
} else if (this.waypointItem != null) {
// Place a waypoint item here:
// (scale % 2 == 0) ? 1 : 0;
int offset = 0;
drawContext.placeItem(this.waypointItem.copy(), new BlockPos(x + this.xOrg + offset, this.yOrg + h + 1, z + this.zOrg + offset), world, (scale % 2 == 1));
}
}
if (c != null && c == start) {
h = this.startHeight;
bs = this.startBlock;
}
if (c != null && c == end) {
h = this.endHeight;
bs = this.endBlock;
}
for (int y = 1; y <= h; y++) {
BlockPos pos = new BlockPos(x + this.xOrg, y + this.yOrg, z + this.zOrg);
drawContext.setBlockState(world, pos, bs);
}
}
}
}
use of com.microsoft.Malmo.Utils.BlockDrawingHelper in project malmo by Microsoft.
the class MovingTargetDecoratorImplementation method buildOnWorld.
@Override
public void buildOnWorld(MissionInit missionInit, World world) throws DecoratorException {
this.path.add(this.startPos);
this.originalPath.add(world.getBlockState(this.startPos));
BlockDrawingHelper drawContext = new BlockDrawingHelper();
drawContext.beginDrawing(world);
drawContext.setBlockState(world, this.startPos, this.blockType);
drawContext.endDrawing(world);
}
use of com.microsoft.Malmo.Utils.BlockDrawingHelper in project malmo by Microsoft.
the class MovingTargetDecoratorImplementation method update.
@Override
public void update(World world) {
if (// Using the turn scheduler?
this.mustWaitTurn && !this.isOurTurn)
return;
if (!this.mustWaitTurn) {
// Not using turn scheduler - using update speed
this.timeSinceLastUpdate++;
if (this.timeSinceLastUpdate < this.speedInTicks)
return;
}
this.timeSinceLastUpdate = 0;
// We're taking it right now.
this.isOurTurn = false;
if (!pinchedByPlayer(world)) {
BlockPos posHead = this.path.peekFirst();
BlockPos posTail = this.path.peekLast();
// For now, only move in 2D - can make this more flexible later.
ArrayList<BlockPos> possibleMovesForward = new ArrayList<BlockPos>();
ArrayList<BlockPos> possibleMovesBackward = new ArrayList<BlockPos>();
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
if (z != 0 && x != 0)
// No diagonal moves.
continue;
if (z == 0 && x == 0)
// Don't allow no-op.
continue;
// Check this is a valid move...
BlockPos candidateHeadPos = new BlockPos(posHead.getX() + x, posHead.getY(), posHead.getZ() + z);
BlockPos candidateTailPos = new BlockPos(posTail.getX() + x, posTail.getY(), posTail.getZ() + z);
if (isValid(world, candidateHeadPos))
possibleMovesForward.add(candidateHeadPos);
if (isValid(world, candidateTailPos))
possibleMovesBackward.add(candidateTailPos);
}
}
// Choose whether to move the "head" or the "tail"
ArrayList<BlockPos> candidates = null;
boolean forwards = true;
if (possibleMovesBackward.isEmpty()) {
candidates = possibleMovesForward;
forwards = true;
} else if (possibleMovesForward.isEmpty()) {
candidates = possibleMovesBackward;
forwards = false;
} else {
forwards = this.rng.nextDouble() < 0.5;
candidates = forwards ? possibleMovesForward : possibleMovesBackward;
}
if (!candidates.isEmpty()) {
BlockDrawingHelper drawContext = new BlockDrawingHelper();
drawContext.beginDrawing(world);
BlockPos newPos = candidates.get(this.rng.nextInt(candidates.size()));
if (forwards) {
// Add the new head:
this.originalPath.addFirst(world.getBlockState(newPos));
drawContext.setBlockState(world, newPos, this.blockType);
this.path.addFirst(newPos);
// Move the tail?
if (this.path.size() > this.pathSize) {
drawContext.setBlockState(world, posTail, new XMLBlockState(this.originalPath.removeLast()));
this.path.removeLast();
}
} else {
// Backwards - add the new tail:
this.originalPath.addLast(world.getBlockState(newPos));
drawContext.setBlockState(world, newPos, this.blockType);
this.path.addLast(newPos);
// Move the head?
if (this.path.size() > this.pathSize) {
drawContext.setBlockState(world, posHead, new XMLBlockState(this.originalPath.removeFirst()));
this.path.removeFirst();
}
}
drawContext.endDrawing(world);
}
}
if (this.mustWaitTurn) {
// Let server know we have finished.
Map<String, String> data = new HashMap<String, String>();
data.put("agentname", this.guid);
MalmoMod.network.sendToServer(new MalmoMod.MalmoMessage(MalmoMessageType.CLIENT_TURN_TAKEN, 0, data));
}
}
use of com.microsoft.Malmo.Utils.BlockDrawingHelper in project malmo by Microsoft.
the class ClassroomDecoratorImplementation method buildOnWorld.
@Override
public void buildOnWorld(MissionInit missionInit, World world) throws DecoratorException {
this.drawContext = new BlockDrawingHelper();
if (this.buildingWidth == 0) {
// We are using complexity so these need to be sampled from the Gaussian
this.buildingWidth = Math.max((int) (rand.nextGaussian() * 2 + this.buildingComplexity * MAX_BUILDING_SIZE + MIN_ROOM_SIZE), MIN_ROOM_SIZE);
this.buildingLength = Math.max((int) (rand.nextGaussian() * 2 + this.buildingComplexity * MAX_BUILDING_SIZE + MIN_ROOM_SIZE), MIN_ROOM_SIZE);
this.buildingHeight = Math.max(Math.max(this.buildingWidth, this.buildingLength) * ROOM_HEIGHT / MIN_ROOM_SIZE, ROOM_HEIGHT);
}
// create the room grid
ArrayList<Room> rooms = this.createRooms();
// randomize the indices used to query different divisions. This has the effect of making
// the path to the goal random each time.
this.shuffleIndices();
// determine how long the path to the goal should be
if (this.pathLength == 0) {
this.pathLength = (int) ((1 - 0.25 * this.pathComplexity) * rooms.size()) - 1;
} else {
this.pathLength = Math.min(this.pathLength, rooms.size() - 1);
}
// find a path to the goal
ArrayList<Divider> path = new ArrayList<Divider>();
Room startRoom, goalRoom;
if (this.pathLength > 0) {
for (Room room : rooms) {
for (Room markRoom : rooms) {
markRoom.mark = false;
}
if (findPath(path, room, this.pathLength)) {
break;
}
}
if (path.size() < this.pathLength) {
// error
throw new DecoratorException("Unable to find path to goal");
}
startRoom = path.get(0).getIn();
goalRoom = path.get(path.size() - 1).getOut();
for (Divider divider : path) {
divider.setHint(this.rand.nextDouble() < this.hintLikelihood);
}
// create all of the obstacles along the path
createObstacles(path);
} else {
startRoom = goalRoom = rooms.get(0);
startRoom.isComplete = true;
}
// find orphan rooms
for (Room room : rooms) {
if (room.isComplete) {
continue;
}
for (Room markRoom : rooms) {
markRoom.mark = false;
}
path.clear();
if (findPath(path, room, rooms.size())) {
// portion of the building back to the orphan room.
for (Divider obstacle : path) {
obstacle.reverse();
}
// create all of the obstacles along the path
createObstacles(path);
} else {
throw new DecoratorException("Unable to join orphan room to goal path");
}
}
// carve out the building
this.drawContext.beginDrawing(world);
for (int x = START_X; x < START_X + this.buildingWidth; x++) {
for (int y = START_Y; y < START_Y + this.buildingHeight; y++) {
for (int z = START_Z; z < START_Z + this.buildingLength; z++) {
world.setBlockToAir(new BlockPos(x, y, z));
}
}
}
// this should clear all of the torches and levers left over from last time. It doesn't.
drawContext.clearEntities(world, START_X - 1, START_Y - 1, START_Z - 1, START_X + this.buildingWidth, START_Y + this.buildingHeight, START_Z + this.buildingLength);
// draw the rooms
for (Room room : rooms) {
room.draw(world, this.rand, this.palette);
}
// place goal
setBlockState(world, new BlockPos(goalRoom.x + this.rand.nextInt(goalRoom.width - 4) + 2, goalRoom.y, goalRoom.z + goalRoom.length - 2), this.palette.goal);
this.drawContext.endDrawing(world);
// set the agent positions
PosAndDirection p2 = new PosAndDirection();
p2.setX(new BigDecimal(startRoom.x + this.rand.nextInt(goalRoom.width - 2) + 0.5));
p2.setY(new BigDecimal(1 + startRoom.y));
p2.setZ(new BigDecimal(startRoom.z + 0.5));
// TODO - for the moment, force all players to being at the maze start point - but this needs to be optional.
for (AgentSection as : missionInit.getMission().getAgentSection()) {
p2.setPitch(as.getAgentStart().getPlacement().getPitch());
p2.setYaw(as.getAgentStart().getPlacement().getYaw());
as.getAgentStart().setPlacement(p2);
}
}
Aggregations