Skip to content

Commit

Permalink
Merge pull request #804 from microsoft/inventory
Browse files Browse the repository at this point in the history
Inventory
  • Loading branch information
AndKram authored Sep 6, 2019
2 parents e46272c + 565762f commit 26433ad
Show file tree
Hide file tree
Showing 28 changed files with 8,108 additions and 5,173 deletions.
10 changes: 8 additions & 2 deletions Malmo/samples/Python_examples/inventory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def getContainerXML():
while world_state.is_mission_running:
world_state = agent_host.getWorldState()
if world_state.number_of_rewards_since_last_state > 0:
total_reward += world_state.rewards[-1].getValue()
reward = world_state.rewards[-1].getValue()
if reward != 0:
print("Got reward of " + str(reward))
total_reward += reward
if world_state.number_of_observations_since_last_state > 0:
obs = json.loads(world_state.observations[-1].text)

Expand Down Expand Up @@ -306,7 +309,10 @@ def getContainerXML():
# Mission has ended.
# Get final reward:
if world_state.number_of_rewards_since_last_state > 0:
total_reward += world_state.rewards[-1].getValue()
reward = world_state.rewards[-1].getValue()
if reward != 0:
print("Got final reward of " + str(reward))
total_reward += reward

test_passed = True
if False in list(completed_boxes.values()):
Expand Down
4 changes: 3 additions & 1 deletion Malmo/samples/Python_examples/sample_missions_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
# -- set up the mission -- #
#mission_file_no_ext = '../Sample_missions/default_world_1' # Survive and find gold, diamond or redstone!
#mission_file_no_ext = '../Sample_missions/default_flat_1' # Move to a wooden hut in a snow tempest!
mission_file_no_ext = '../Sample_missions/tricky_arena_1' # Mind your step to the redstone!
# This is the default mission to run:
mission_file_no_ext = '../Sample_missions/tricky_arena_1' # Mind your step to the redstone!
#mission_file_no_ext = '../Sample_missions/inventory_handlers' # Tour of crafting, smelting, collecting!
#mission_file_no_ext = '../Sample_missions/eating_1' # Eat a healthy diet!
#mission_file_no_ext = '../Sample_missions/cliff_walking_1' # Burning lava!

Expand Down
37 changes: 36 additions & 1 deletion Malmo/src/MissionSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace malmo
const std::vector<std::string> MissionSpec::all_inventory_commands = { "swapInventoryItems", "combineInventoryItems", "discardCurrentItem",
"hotbar.1", "hotbar.2", "hotbar.3", "hotbar.4", "hotbar.5", "hotbar.6", "hotbar.7", "hotbar.8", "hotbar.9" };
const std::vector<std::string> MissionSpec::all_simplecraft_commands = { "craft" };
const std::vector<std::string> MissionSpec::all_nearbycraft_commands = { "craftNearby" };
const std::vector<std::string> MissionSpec::all_nearbysmelt_commands = { "smeltNearby" };
const std::vector<std::string> MissionSpec::all_chat_commands = { "chat" };
const std::vector<std::string> MissionSpec::all_mission_quit_commands = { "quit" };
const std::vector<std::string> MissionSpec::all_human_level_commands = { "forward", "left", "right", "jump", "sneak", "sprint", "inventory", "swapHands", "drop", "use", "attack", "moveMouse",
Expand Down Expand Up @@ -110,6 +112,7 @@ namespace malmo
child.erase("FlatWorldGenerator");
child.erase("FileWorldGenerator");
child.erase("DefaultWorldGenerator");
child.erase("BiomeGenerator");
}
}

Expand Down Expand Up @@ -137,6 +140,10 @@ namespace malmo
if (defaultWorldGenerator) {
defaultWorldGenerator.get().put("<xmlattr>.forceReset", true);
}
const auto& biomeWorldGenerator = mission.get_child_optional("Mission.ServerSection.ServerHandlers.BiomeGenerator");
if (biomeWorldGenerator) {
biomeWorldGenerator.get().put("<xmlattr>.forceReset", true);
}
}

void MissionSpec::setTimeOfDay(int t,bool allowTimeToPass)
Expand Down Expand Up @@ -359,6 +366,11 @@ namespace malmo
mission.put("Mission.AgentSection.AgentHandlers.ObservationFromChat", "");
}

void MissionSpec::observeCompass()
{
mission.put("Mission.AgentSection.AgentHandlers.ObservationFromCompass", "");
}

// ------------------ settings for the agents : command handlers --------------------------------

void MissionSpec::removeAllCommandHandlers()
Expand All @@ -371,6 +383,9 @@ namespace malmo
child.erase("DiscreteMovementCommands");
child.erase("AbsoluteMovementCommands");
child.erase("SimpleCraftCommands");
child.erase("NearbyCraftCommands");
child.erase("NearbySmeltCommands");
child.erase("PlaceCommands");
child.erase("ChatCommands");
child.erase("MissionQuitCommands");
}
Expand Down Expand Up @@ -415,11 +430,16 @@ namespace malmo
{
addVerbToCommandType(verb, "Mission.AgentSection.AgentHandlers.InventoryCommands");
}

void MissionSpec::allowAllChatCommands()
{
mission.put("Mission.AgentSection.AgentHandlers.ChatCommands", "");
}

void MissionSpec::allowAllPlaceCommands()
{
mission.put("Mission.AgentSection.AgentHandlers.PlaceCommands", "");
}

// ------------------------------- information ---------------------------------------------------

Expand Down Expand Up @@ -517,6 +537,15 @@ namespace malmo

if (e.second.get_child_optional("AgentHandlers.SimpleCraftCommands"))
command_handlers.push_back("SimpleCraft");

if (e.second.get_child_optional("AgentHandlers.NearbyCraftCommands"))
command_handlers.push_back("NearbyCraft");

if (e.second.get_child_optional("AgentHandlers.NearbySmeltCommands"))
command_handlers.push_back("NearbySmelt");

if (e.second.get_child_optional("AgentHandlers.PlaceCommands"))
command_handlers.push_back("Place");

if (e.second.get_child_optional("AgentHandlers.MissionQuitCommands"))
command_handlers.push_back("MissionQuit");
Expand Down Expand Up @@ -577,6 +606,12 @@ namespace malmo
else if (command_handler == "SimpleCraft") {
allowed_commands = all_simplecraft_commands;
}
else if (command_handler == "NearbyCraft") {
allowed_commands = all_nearbycraft_commands;
}
else if (command_handler == "NearbySmelt") {
allowed_commands = all_nearbysmelt_commands;
}
else if (command_handler == "Chat") {
allowed_commands = all_chat_commands;
}
Expand Down
9 changes: 9 additions & 0 deletions Malmo/src/MissionSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ namespace malmo
//! Asks for chat messages to be included in the observations.
void observeChat();

//! Asks for compass information to be included in the observations.
void observeCompass();

// -------------------- settings for the agents : command handlers -------------------------

//! Remove any existing command handlers from the mission specification. Use with other functions to add exactly the command handlers you want.
Expand Down Expand Up @@ -296,6 +299,10 @@ namespace malmo
//! Only applies to the first agent in the mission. For multi-agent missions, specify the command handlers for each in the XML.
void allowAllChatCommands();

//! Adds a place command handler if none present, with neither an allow-list or a deny-list, thus allowing any command to be sent.
//! Only applies to the first agent in the mission. For multi-agent missions, specify the command handlers for each in the XML.
void allowAllPlaceCommands();

// ------------------------- information --------------------------------------

//! Returns the short description of the mission.
Expand Down Expand Up @@ -379,6 +386,8 @@ namespace malmo
static const std::vector<std::string> all_discrete_movement_commands;
static const std::vector<std::string> all_inventory_commands;
static const std::vector<std::string> all_simplecraft_commands;
static const std::vector<std::string> all_nearbycraft_commands;
static const std::vector<std::string> all_nearbysmelt_commands;
static const std::vector<std::string> all_chat_commands;
static const std::vector<std::string> all_mission_quit_commands;
static const std::vector<std::string> all_human_level_commands;
Expand Down
20 changes: 20 additions & 0 deletions MalmoEnv/malmoenv/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class CommandParser:
inventoryCommands = "Inventory"
chatCommands = "Chat"
simpleCraftCommands = "SimpleCraft"
nearbyCraftCommands = "NearbyCraft"
nearbySmeltCommands = "NearbySmelt"
missionQuitCommands = "MissionQuit"
humanLevelCommands = "HumanLevel"

Expand All @@ -50,6 +52,8 @@ class CommandParser:
"hotbar.7", "hotbar.8", "hotbar.9"]
all_chat = ["chat"]
all_simplecraft = ["craft"]
all_nearbycraft = ["nearbyCraft"]
all_nearbysmelt = ["nearbySmelt"]
all_mission_quit = ["quit"]
all_human_level = ["forward", "left", "right", "jump", "sneak", "sprint", "inventory",
"swapHands", "drop", "use", "attack", "moveMouse",
Expand Down Expand Up @@ -126,6 +130,14 @@ def get_actions(self, commands):
if verb != 'chat':
raise CommandHandlerException("Invalid chat command")
actions.append(verb)
elif type == 'NearbyCraft':
if verb != 'nearbyCraft':
raise CommandHandlerException("Invalid nearby craft command")
actions.append(verb)
elif type == 'NearbySmelt':
if verb != 'nearbySmelt':
raise CommandHandlerException("Invalid nearby smelt command")
actions.append(verb)
elif type == 'SimpleCraft':
if verb != 'craft':
raise CommandHandlerException("Invalid craft command")
Expand All @@ -152,6 +164,10 @@ def _command_hander(self, handlers, turnbased, commands):
commands.extend(self._add_commands(CommandParser.chatCommands, turnbased, ch))
elif ch.tag == CommandParser.ns + "SimpleCraftCommands":
commands.extend(self._add_commands(CommandParser.simpleCraftCommands, turnbased, ch))
elif ch.tag == CommandParser.ns + "NearbyCraftCommands":
commands.extend(self._add_commands(CommandParser.nearbyCraftCommands, turnbased, ch))
elif ch.tag == CommandParser.ns + "NearbySmeltCommands":
commands.extend(self._add_commands(CommandParser.nearbySmeltCommands, turnbased, ch))
elif ch.tag == CommandParser.ns + "MissionQuitCommands":
commands.extend(self._add_commands(CommandParser.missionQuitCommands, turnbased, ch))
elif ch.tag == CommandParser.ns + "HumanLevelCommands":
Expand Down Expand Up @@ -189,6 +205,10 @@ def _fill_command_list(self, command_type, turnbased, allow, deny):
allow = [(command_type, turnbased, c) for c in CommandParser.all_human_level]
elif command_type == CommandParser.simpleCraftCommands:
allow = [(command_type, turnbased, c) for c in CommandParser.all_simplecraft]
elif command_type == CommandParser.nearbyCraftCommands:
allow = [(command_type, turnbased, c) for c in CommandParser.all_nearbycraft]
elif command_type == CommandParser.nearbySmeltCommands:
allow = [(command_type, turnbased, c) for c in CommandParser.all_nearbysmelt]
elif command_type == CommandParser.missionQuitCommands:
allow = [(command_type, turnbased, c) for c in CommandParser.missionQuitCommands]
elif command_type == CommandParser.chatCommands:
Expand Down
11 changes: 3 additions & 8 deletions Minecraft/src/main/java/com/microsoft/Malmo/MalmoMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.microsoft.Malmo;

import com.microsoft.Malmo.MissionHandlers.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
Expand Down Expand Up @@ -57,14 +58,6 @@
import net.minecraftforge.fml.relauncher.Side;

import com.microsoft.Malmo.Client.MalmoModClient;
import com.microsoft.Malmo.MissionHandlers.AbsoluteMovementCommandsImplementation;
import com.microsoft.Malmo.MissionHandlers.DiscreteMovementCommandsImplementation;
import com.microsoft.Malmo.MissionHandlers.InventoryCommandsImplementation;
import com.microsoft.Malmo.MissionHandlers.ObservationFromFullInventoryImplementation;
import com.microsoft.Malmo.MissionHandlers.ObservationFromFullStatsImplementation;
import com.microsoft.Malmo.MissionHandlers.ObservationFromGridImplementation;
import com.microsoft.Malmo.MissionHandlers.ObservationFromSystemImplementation;
import com.microsoft.Malmo.MissionHandlers.SimpleCraftCommandsImplementation;
import com.microsoft.Malmo.Schemas.MissionInit;
import com.microsoft.Malmo.Server.MalmoModServer;
import com.microsoft.Malmo.Utils.AddressHelper;
Expand Down Expand Up @@ -134,6 +127,8 @@ public void preInit(FMLPreInitializationEvent event)
network.registerMessage(ObservationFromGridImplementation.GridRequestMessageHandler.class, ObservationFromGridImplementation.GridRequestMessage.class, 2, Side.SERVER);
network.registerMessage(MalmoMessageHandler.class, MalmoMessage.class, 3, Side.CLIENT); // Malmo messages from server to client
network.registerMessage(SimpleCraftCommandsImplementation.CraftMessageHandler.class, SimpleCraftCommandsImplementation.CraftMessage.class, 4, Side.SERVER);
network.registerMessage(NearbyCraftCommandsImplementation.CraftNearbyMessageHandler.class, NearbyCraftCommandsImplementation.CraftNearbyMessage.class, 13, Side.SERVER);
network.registerMessage(NearbySmeltCommandsImplementation.SmeltNearbyMessageHandler.class, NearbySmeltCommandsImplementation.SmeltNearbyMessage.class, 14, Side.SERVER);
network.registerMessage(AbsoluteMovementCommandsImplementation.TeleportMessageHandler.class, AbsoluteMovementCommandsImplementation.TeleportMessage.class, 5, Side.SERVER);
network.registerMessage(MalmoMessageHandler.class, MalmoMessage.class, 6, Side.SERVER); // Malmo messages from client to server
network.registerMessage(InventoryCommandsImplementation.InventoryMessageHandler.class, InventoryCommandsImplementation.InventoryMessage.class, 7, Side.SERVER);
Expand Down
Loading

0 comments on commit 26433ad

Please sign in to comment.