Search in sources :

Example 6 with XMLBlockState

use of com.microsoft.Malmo.Utils.BlockDrawingHelper.XMLBlockState in project malmo by Microsoft.

the class BuildBattleDecoratorImplementation method updateAndScorePlayerVolume.

private void updateAndScorePlayerVolume(World w, boolean updateReward) {
    int wrongBlocks = 0;
    int rightBlocks = 0;
    int totalMatchingBlocks = 0;
    BlockDrawingHelper drawContext = new BlockDrawingHelper();
    drawContext.beginDrawing(w);
    for (int x = this.sourceBounds.getMin().getX(); x <= this.sourceBounds.getMax().getX(); x++) {
        for (int y = this.sourceBounds.getMin().getY(); y <= this.sourceBounds.getMax().getY(); y++) {
            for (int z = this.sourceBounds.getMin().getZ(); z <= this.sourceBounds.getMax().getZ(); z++) {
                BlockPos goalStructurePos = new BlockPos(x, y, z);
                BlockPos playerStructurePos = goalStructurePos.add(this.delta);
                // We don't compare the world's block states, since we re-colour them to give
                // feedback on right / wrong blocks.
                // Instead, query our internal representations:
                IBlockState srcState = getSourceBlockState(w, goalStructurePos);
                IBlockState dstState = getDestBlockState(w, playerStructurePos);
                if (srcState == null || dstState == null)
                    // Shouldn't happen unless we've had an out-of-bounds error somehow.
                    continue;
                boolean destAir = w.isAirBlock(playerStructurePos);
                if (srcState.equals(dstState)) {
                    // They match. We count this if the dest block is not air.
                    if (!destAir)
                        rightBlocks++;
                    if (blockTypeOnCorrectPlacement != null && !w.isAirBlock(goalStructurePos)) {
                        // Mark both source and destination blocks for correct placement:
                        drawContext.setBlockState(w, playerStructurePos, blockTypeOnCorrectPlacement);
                        drawContext.setBlockState(w, goalStructurePos, blockTypeOnCorrectPlacement);
                    }
                    totalMatchingBlocks++;
                } else {
                    // Non-match. We call this wrong if the dest block is not air.
                    if (!destAir) {
                        wrongBlocks++;
                        if (blockTypeOnIncorrectPlacement != null) {
                            // Recolour the destination block only:
                            drawContext.setBlockState(w, playerStructurePos, blockTypeOnIncorrectPlacement);
                        }
                    }
                    // Check the source block - if it was previously correct, and has become incorrect,
                    // then we will need to reset the world's blockstate:
                    IBlockState actualState = w.getBlockState(goalStructurePos);
                    if (!actualState.equals(srcState))
                        drawContext.setBlockState(w, goalStructurePos, new XMLBlockState(srcState));
                }
            }
        }
    }
    drawContext.endDrawing(w);
    int score = rightBlocks - wrongBlocks;
    boolean sendData = false;
    boolean sendCompletionBonus = false;
    int reward = 0;
    if (updateReward && score != this.currentScore) {
        reward = score - this.currentScore;
        sendData = true;
    }
    this.currentScore = score;
    if (totalMatchingBlocks == this.structureVolume) {
        if (!this.structureHasBeenCompleted) {
            // final block.)
            if (updateReward)
                sendCompletionBonus = true;
        }
        this.structureHasBeenCompleted = true;
    }
    this.valid = true;
    if (sendData) {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("reward", Integer.toString(reward));
        data.put("completed", Boolean.toString(sendCompletionBonus));
        MalmoMod.safeSendToAll(MalmoMessageType.SERVER_BUILDBATTLEREWARD, data);
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockDrawingHelper(com.microsoft.Malmo.Utils.BlockDrawingHelper) HashMap(java.util.HashMap) BlockPos(net.minecraft.util.math.BlockPos) XMLBlockState(com.microsoft.Malmo.Utils.BlockDrawingHelper.XMLBlockState)

Aggregations

XMLBlockState (com.microsoft.Malmo.Utils.BlockDrawingHelper.XMLBlockState)6 BlockPos (net.minecraft.util.math.BlockPos)4 BlockDrawingHelper (com.microsoft.Malmo.Utils.BlockDrawingHelper)3 DrawBlockBasedObjectType (com.microsoft.Malmo.Schemas.DrawBlockBasedObjectType)2 HashMap (java.util.HashMap)2 IBlockState (net.minecraft.block.state.IBlockState)2 MalmoMod (com.microsoft.Malmo.MalmoMod)1 BlockType (com.microsoft.Malmo.Schemas.BlockType)1 BuildBattleDecorator (com.microsoft.Malmo.Schemas.BuildBattleDecorator)1 Colour (com.microsoft.Malmo.Schemas.Colour)1 MovingTargetDecorator (com.microsoft.Malmo.Schemas.MovingTargetDecorator)1 Pos (com.microsoft.Malmo.Schemas.Pos)1 Variation (com.microsoft.Malmo.Schemas.Variation)1 ArrayList (java.util.ArrayList)1 Vec3i (net.minecraft.util.math.Vec3i)1