From bb4c1563947085bf60236af6e20fedbe06822e6b Mon Sep 17 00:00:00 2001 From: davey Date: Fri, 2 Jun 2017 14:15:44 +0100 Subject: [PATCH] Added reward for damaging entities (#537) --- Malmo/samples/Python_examples/CMakeLists.txt | 1 + ...RewardForDamagingEntityImplementation.java | 91 +++++++++++++++++++ .../Malmo/MissionHandlers/RewardGroup.java | 1 - .../Malmo/Utils/JSONWorldDataHelper.java | 1 + Schemas/Mission.xsd | 1 + Schemas/MissionHandlers.xsd | 22 ++++- changelog.txt | 1 + 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100755 Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardForDamagingEntityImplementation.java diff --git a/Malmo/samples/Python_examples/CMakeLists.txt b/Malmo/samples/Python_examples/CMakeLists.txt index 9fada5385..dba76ab62 100755 --- a/Malmo/samples/Python_examples/CMakeLists.txt +++ b/Malmo/samples/Python_examples/CMakeLists.txt @@ -29,6 +29,7 @@ set( SOURCES discrete_3d_test.py drawing_test.py file_test.py + hit_test.py human_action.py manual_input_test.py MazeRunner.py diff --git a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardForDamagingEntityImplementation.java b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardForDamagingEntityImplementation.java new file mode 100755 index 000000000..5a40bf4f0 --- /dev/null +++ b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardForDamagingEntityImplementation.java @@ -0,0 +1,91 @@ +package com.microsoft.Malmo.MissionHandlers; + +import java.util.HashMap; +import java.util.Map; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.DamageSource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.living.LivingAttackEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import com.microsoft.Malmo.MissionHandlerInterfaces.IRewardProducer; +import com.microsoft.Malmo.Schemas.EntityTypes; +import com.microsoft.Malmo.Schemas.MissionInit; +import com.microsoft.Malmo.Schemas.MobWithReward; +import com.microsoft.Malmo.Schemas.RewardForDamagingEntity; + +public class RewardForDamagingEntityImplementation extends RewardBase implements IRewardProducer +{ + RewardForDamagingEntity params; + Map damages = new HashMap(); + + @Override + public boolean parseParameters(Object params) + { + super.parseParameters(params); + if (params == null || !(params instanceof RewardForDamagingEntity)) + return false; + + this.params = (RewardForDamagingEntity) params; + return true; + } + + @Override + public void getReward(MissionInit missionInit, MultidimensionalReward reward) + { + super.getReward(missionInit, reward); + if (missionInit == null || Minecraft.getMinecraft().thePlayer == null) + return; + synchronized (this.damages) + { + for (MobWithReward mob : this.damages.keySet()) + { + float damage_amount = this.damages.get(mob); + float damage_reward = damage_amount * mob.getReward().floatValue(); + float adjusted_reward = adjustAndDistributeReward(damage_reward, this.params.getDimension(), mob.getDistribution()); + reward.add(this.params.getDimension(), adjusted_reward); + } + this.damages.clear(); + } + } + + @Override + public void prepare(MissionInit missionInit) + { + super.prepare(missionInit); + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void cleanup() + { + super.cleanup(); + MinecraftForge.EVENT_BUS.unregister(this); + } + + @SubscribeEvent + public void onLivingAttackEvent(LivingAttackEvent event) + { + if (event.entity == null || event.source.getEntity() != Minecraft.getMinecraft().thePlayer) + return; + synchronized (this.damages) + { + for (MobWithReward mob : this.params.getMob()) + { + // Have we caught one of these mobs? + for (EntityTypes et : mob.getType()) + { + String mobName = et.value(); + if (event.entity.getName().equals(mobName)) + { + if (this.damages.containsKey(mob)) + this.damages.put(mob, this.damages.get(mob) + event.ammount); + else + this.damages.put(mob, event.ammount); + } + } + } + } + } +} \ No newline at end of file diff --git a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardGroup.java b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardGroup.java index e97376f30..598b40e81 100755 --- a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardGroup.java +++ b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/RewardGroup.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.HashMap; -import com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler; import com.microsoft.Malmo.MissionHandlerInterfaces.IRewardProducer; import com.microsoft.Malmo.Schemas.MissionInit; diff --git a/Minecraft/src/main/java/com/microsoft/Malmo/Utils/JSONWorldDataHelper.java b/Minecraft/src/main/java/com/microsoft/Malmo/Utils/JSONWorldDataHelper.java index 51c3b27e3..e4b559c29 100755 --- a/Minecraft/src/main/java/com/microsoft/Malmo/Utils/JSONWorldDataHelper.java +++ b/Minecraft/src/main/java/com/microsoft/Malmo/Utils/JSONWorldDataHelper.java @@ -110,6 +110,7 @@ public static void buildAchievementStats(JsonObject json, EntityPlayerMP player) json.addProperty("MobsKilled", sfw.readStat((StatBase)StatList.mobKillsStat)); json.addProperty("PlayersKilled", sfw.readStat((StatBase)StatList.playerKillsStat)); json.addProperty("DamageTaken", sfw.readStat((StatBase)StatList.damageTakenStat)); + json.addProperty("DamageDealt", sfw.readStat((StatBase)StatList.damageDealtStat)); /* Other potential reinforcement signals that may be worth researching: json.addProperty("BlocksDestroyed", sfw.readStat((StatBase)StatList.objectBreakStats) - but objectBreakStats is an array of 32000 StatBase objects - indexed by block type.); diff --git a/Schemas/Mission.xsd b/Schemas/Mission.xsd index ea8c52d90..8d35e2ee8 100755 --- a/Schemas/Mission.xsd +++ b/Schemas/Mission.xsd @@ -346,6 +346,7 @@ + diff --git a/Schemas/MissionHandlers.xsd b/Schemas/MissionHandlers.xsd index b41f4f7f4..2d56b481c 100755 --- a/Schemas/MissionHandlers.xsd +++ b/Schemas/MissionHandlers.xsd @@ -1469,7 +1469,7 @@ When present, the Mod will return several observations: - * Achievement statistics: {{{DistanceTravelled}}}, {{{TimeAlive}}}, {{{MobsKilled}}}, {{{PlayersKilled}}}, {{{DamageTaken}}} + * Achievement statistics: {{{DistanceTravelled}}}, {{{TimeAlive}}}, {{{MobsKilled}}}, {{{PlayersKilled}}}, {{{DamageTaken}}}, {{{DamageDealt}}} * Life statistics: {{{Life}}}, {{{Score}}}, {{{Food}}}, {{{Air}}}, {{{XP}}}, {{{IsAlive}}}, {{{Name}}} * Position statistics: {{{XPos}}}, {{{YPos}}}, {{{ZPos}}}, {{{Pitch}}}, {{{Yaw}}} * Environment statistics: {{{WorldTime}}} - current time in ticks, {{{TotalTime}}} - total world time, unaffected by ServerInitialConditions @@ -1663,6 +1663,26 @@ + + + + + + + + + + Sends a reward when an entity is damaged. + + + + + + + + + + diff --git a/changelog.txt b/changelog.txt index 9b3c57138..77303562a 100755 --- a/changelog.txt +++ b/changelog.txt @@ -5,6 +5,7 @@ New: Added socket logging code to Mod, available in Mod UI. New: Added logging code to platform, available through Malmo API. New: startMission now provides more error details in exceptions; multi-agent samples hardened by using this. New: ObservationFromNearbyEntities now returns entity life. +New: RewardForDamagingEntity plus sample. (#537) 0.21.0 -------------------