use of com.microsoft.Malmo.Schemas.MissionInit in project malmo by Microsoft.
the class MissionQuitCommandsImplementation method install.
// ------------- ICommandHandler methods -----------
@Override
public void install(MissionInit missionInit) {
// In order to trigger the end of the mission, we need to hook into the quit handlers.
MissionBehaviour mb = parentBehaviour();
mb.addQuitProducer(new IWantToQuit() {
@Override
public void prepare(MissionInit missionInit) {
}
@Override
public String getOutcome() {
return MissionQuitCommandsImplementation.this.quitcomParams.getQuitDescription();
}
@Override
public boolean doIWantToQuit(MissionInit missionInit) {
return MissionQuitCommandsImplementation.this.iWantToQuit;
}
@Override
public void cleanup() {
}
});
}
use of com.microsoft.Malmo.Schemas.MissionInit in project malmo by Microsoft.
the class ObservationFromRecentCommandsImplementation method writeObservationsToJSON.
@Override
public void writeObservationsToJSON(JsonObject json, MissionInit missionInit) {
if (!hookedIntoCommandChain) {
// We need to see the commands as they come in, so we can determine which ones to echo back in the observation message.
// To do this we create our own command handler and insert it at the root of the command chain.
// It's slightly dirty behaviour, but it saves
// a) adding special code into ProjectMalmo.java just to allow for this ObservationProducer to work, and
// b) requiring the user to add a special command handler themselves at the right point in the XML.
MissionBehaviour mb = parentBehaviour();
ICommandHandler oldch = mb.commandHandler;
CommandGroup newch = new CommandGroup() {
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
// See if this command gets handled by the legitimate handlers:
boolean handled = super.onExecute(verb, parameter, missionInit);
if (// Yes, so record it:
handled)
ObservationFromRecentCommandsImplementation.this.addHandledCommand(verb, parameter);
return handled;
}
};
newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
if (oldch != null)
newch.addCommandHandler(oldch);
mb.commandHandler = newch;
this.hookedIntoCommandChain = true;
}
synchronized (this.recentCommandList) {
// Have any commands been processed since we last sent a burst of observations?
if (this.recentCommandList.size() != 0) {
// Yes, so build up a JSON array:
JsonArray commands = new JsonArray();
for (String s : this.recentCommandList) {
commands.add(new JsonPrimitive(s));
}
json.add("CommandsSinceLastObservation", commands);
}
this.recentCommandList.clear();
}
}
use of com.microsoft.Malmo.Schemas.MissionInit in project malmo by Microsoft.
the class RewardForSendingMatchingChatMessageImplementation method prepare.
/**
* Called once before the mission starts - use for any necessary
* initialisation.
*
* @param missionInit
*/
@Override
public void prepare(MissionInit missionInit) {
super.prepare(missionInit);
// We need to see chat commands as they come in.
// Following the example of RewardForSendingCommandImplementation.
MissionBehaviour mb = parentBehaviour();
ICommandHandler oldch = mb.commandHandler;
CommandGroup newch = new CommandGroup() {
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
// See if this command gets handled by the legitimate handlers:
boolean handled = super.onExecute(verb, parameter, missionInit);
if (// Yes, so check if we need to produce a reward
handled && verb.equalsIgnoreCase(ChatCommand.CHAT.value())) {
Iterator<Map.Entry<Pattern, Float>> patternIt = patternMap.entrySet().iterator();
while (patternIt.hasNext()) {
Map.Entry<Pattern, Float> entry = patternIt.next();
Matcher m = entry.getKey().matcher(parameter);
if (m.matches()) {
String distribution = distributionMap.get(entry.getKey());
addAndShareCachedReward(RewardForSendingMatchingChatMessageImplementation.this.params.getDimension(), entry.getValue(), distribution);
}
}
}
return handled;
}
};
newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
if (oldch != null)
newch.addCommandHandler(oldch);
mb.commandHandler = newch;
}
Aggregations