Search in sources :

Example 1 with RangeDefinition

use of com.microsoft.Malmo.Schemas.RangeDefinition in project malmo by Microsoft.

the class ObservationFromNearbyEntitiesImplementation method writeObservationsToJSON.

@Override
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++;
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) EntityLivingBase(net.minecraft.entity.EntityLivingBase) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.google.gson.JsonObject) DrawItem(com.microsoft.Malmo.Schemas.DrawItem) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) ItemStack(net.minecraft.item.ItemStack) RangeDefinition(com.microsoft.Malmo.Schemas.RangeDefinition) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 DrawItem (com.microsoft.Malmo.Schemas.DrawItem)1 RangeDefinition (com.microsoft.Malmo.Schemas.RangeDefinition)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)1 Entity (net.minecraft.entity.Entity)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1