public void writeObservationsToJSON()

in Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/ObservationFromNearbyEntitiesImplementation.java [59:153]


    public void writeObservationsToJSON(JsonObject json, MissionInit missionInit)
    {
        this.tickCount++;

        EntityPlayerSP player = Minecraft.getMinecraft().player;

        // Get all the currently loaded entities:
        List<?> entities = Minecraft.getMinecraft().world.getLoadedEntityList();

        // Get the list of RangeDefinitions that need firing:
        List<RangeDefinition> rangesToFire = new ArrayList<RangeDefinition>();
        int index = 0;
        for (RangeDefinition rd : this.oneparams.getRange())
        {
            if (this.tickCount - this.lastFiringTimes[index] >= rd.getUpdateFrequency())
            {
                rangesToFire.add(rd);
                this.lastFiringTimes[index] = this.tickCount;
            }
            index++;
        }

        // Create a list of empty lists to populate:
        List<List<Entity>> entitiesInRange = new ArrayList<List<Entity>>();
        for (int i = 0; i < rangesToFire.size(); i++)
            entitiesInRange.add(new ArrayList<Entity>());

        // Populate all our lists according to which entities are in range:
        for (Object obj : entities)
        {
            if (obj instanceof Entity)
            {
                Entity e = (Entity)obj;
                index = 0;
                for (RangeDefinition rd : rangesToFire)
                {
                    if (Math.abs(e.posX - player.posX) < rd.getXrange().doubleValue() &&
                        Math.abs(e.posY - player.posY) < rd.getYrange().doubleValue() && 
                        Math.abs(e.posZ - player.posZ) < rd.getZrange().doubleValue())
                        {
                            // Belongs in this list:
                            entitiesInRange.get(index).add(e);
                        }
                    index++;
                }
            }
        }

        // Now build up a JSON array for each populated list:
        index = 0;
        for (List<Entity> entsInRangeList : entitiesInRange)
        {
            if (!entitiesInRange.isEmpty())
            {
                JsonArray arr = new JsonArray();
                for (Entity e : entsInRangeList)
                {
                    JsonObject jsent = new JsonObject();
                    jsent.addProperty("yaw", e.rotationYaw);
                    jsent.addProperty("x", e.posX);
                    jsent.addProperty("y", e.posY);
                    jsent.addProperty("z", e.posZ);
                    jsent.addProperty("pitch", e.rotationPitch);
                    jsent.addProperty("id", e.getCachedUniqueIdString());
                    jsent.addProperty("motionX", e.motionX);
                    jsent.addProperty("motionY", e.motionY);
                    jsent.addProperty("motionZ", e.motionZ);
                    String name = MinecraftTypeHelper.getUnlocalisedEntityName(e);
                    if (e instanceof EntityItem)
                    {
                        ItemStack is = ((EntityItem)e).getEntityItem();
                        DrawItem di = MinecraftTypeHelper.getDrawItemFromItemStack(is);
                        if (di != null)
                        {
                            name = di.getType();
                            if (di.getColour() != null)
                                jsent.addProperty("colour", di.getColour().value());
                            if (di.getVariant() != null)
                                jsent.addProperty("variation",  di.getVariant().getValue());
                        }
                        jsent.addProperty("quantity", is.getCount());
                    }
                    else if (e instanceof EntityLivingBase)
                    {
                        EntityLivingBase el = (EntityLivingBase)e;
                        jsent.addProperty("life", el.getHealth());
                    }
                    jsent.addProperty("name", name);
                    arr.add(jsent);
                }
                json.add(this.oneparams.getRange().get(index).getName(), arr);
                index++;
            }
        }
    }