Search in sources :

Example 6 with BlockPos

use of net.minecraft.util.BlockPos 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));
    }
}
Also used : BlockDrawingHelper(com.microsoft.Malmo.Utils.BlockDrawingHelper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.BlockPos) MalmoMod(com.microsoft.Malmo.MalmoMod) XMLBlockState(com.microsoft.Malmo.Utils.BlockDrawingHelper.XMLBlockState)

Example 7 with BlockPos

use of net.minecraft.util.BlockPos in project malmo by Microsoft.

the class MovingTargetDecoratorImplementation method pinchedByPlayer.

private boolean pinchedByPlayer(World world) {
    for (BlockPos bp : this.path) {
        //Block b = world.getBlockState(bp).getBlock();
        //AxisAlignedBB aabb = b.getCollisionBoundingBox(world, bp, b.getDefaultState());
        //aabb.expand(0, 1, 0);
        BlockPos bp2 = new BlockPos(bp.getX() + 1, bp.getY() + 2, bp.getZ() + 1);
        AxisAlignedBB aabb = new AxisAlignedBB(bp, bp2);
        List<Entity> entities = world.getEntitiesWithinAABBExcludingEntity(null, aabb);
        for (Entity ent : entities) if (ent instanceof EntityPlayer)
            return true;
    }
    return false;
}
Also used : AxisAlignedBB(net.minecraft.util.AxisAlignedBB) Entity(net.minecraft.entity.Entity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.BlockPos)

Example 8 with BlockPos

use of net.minecraft.util.BlockPos 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 {
                    // Place a waypoint item here:
                    //(scale % 2 == 0) ? 1 : 0;
                    int offset = 0;
                    drawContext.placeItem(ItemStack.copyItemStack(this.waypointItem), 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);
            }
        }
    }
}
Also used : BlockDrawingHelper(com.microsoft.Malmo.Utils.BlockDrawingHelper) BlockPos(net.minecraft.util.BlockPos) XMLBlockState(com.microsoft.Malmo.Utils.BlockDrawingHelper.XMLBlockState)

Example 9 with BlockPos

use of net.minecraft.util.BlockPos in project malmo by Microsoft.

the class BlockDrawingHelper method DrawPrimitive.

/**
     * Draw a single Minecraft block.
     * @param b Contains information about the block to be drawn.
     * @param w The world in which to draw.
     * @throws Exception Throws an exception if the block type is not recognised.
     */
private void DrawPrimitive(DrawBlock b, World w) throws Exception {
    XMLBlockState blockType = new XMLBlockState(b.getType(), b.getColour(), b.getFace(), b.getVariant());
    if (!blockType.isValid())
        throw new Exception("Unrecogised item type: " + b.getType().value());
    BlockPos pos = new BlockPos(b.getX(), b.getY(), b.getZ());
    setBlockState(w, pos, blockType);
}
Also used : BlockPos(net.minecraft.util.BlockPos)

Example 10 with BlockPos

use of net.minecraft.util.BlockPos in project malmo by Microsoft.

the class BlockDrawingHelper method DrawPrimitive.

/**
     * Spawn a single item at the specified position.
     * @param i Contains information about the item to be spawned.
     * @param w The world in which to spawn.
     * @throws Exception Throws an exception if the item type is not recognised.
     */
private void DrawPrimitive(DrawItem i, World w) throws Exception {
    ItemStack item = MinecraftTypeHelper.getItemStackFromDrawItem(i);
    if (item == null)
        throw new Exception("Unrecognised item type: " + i.getType());
    BlockPos pos = new BlockPos(i.getX(), i.getY(), i.getZ());
    placeItem(item, pos, w, true);
}
Also used : BlockPos(net.minecraft.util.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

BlockPos (net.minecraft.util.BlockPos)30 IBlockState (net.minecraft.block.state.IBlockState)11 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)6 Entity (net.minecraft.entity.Entity)5 BlockDrawingHelper (com.microsoft.Malmo.Utils.BlockDrawingHelper)4 XMLBlockState (com.microsoft.Malmo.Utils.BlockDrawingHelper.XMLBlockState)4 ArrayList (java.util.ArrayList)4 ItemStack (net.minecraft.item.ItemStack)4 AxisAlignedBB (net.minecraft.util.AxisAlignedBB)3 EntityTypes (com.microsoft.Malmo.Schemas.EntityTypes)2 HashMap (java.util.HashMap)2 Block (net.minecraft.block.Block)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 MovingObjectPosition (net.minecraft.util.MovingObjectPosition)2 Vec3 (net.minecraft.util.Vec3)2 World (net.minecraft.world.World)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1