Skip to content

Adds respawning handler for agents - MUST UPDATE OLD ENV-SPECS #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 36 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6e62961
Added bounded world handlers co-authored by wguss
brandonhoughton Feb 4, 2021
1d4cb60
Testing out dirt collection minigame
brandonhoughton Feb 4, 2021
2ac0f15
Update BoundedWorldHandler
brandonhoughton Feb 4, 2021
78cf687
Added BundledWorldDecratorImplemntation.java
brandonhoughton Feb 4, 2021
04aae20
Revert "human action space + enabled GUI (#449)"
brandonhoughton Feb 4, 2021
3d99d74
New handler
MadcowD Feb 4, 2021
4cb31dd
Addkinf foliage
MadcowD Feb 4, 2021
a2f2f11
Updating pullback
MadcowD Feb 4, 2021
28601ba
Removing prints
MadcowD Feb 4, 2021
9aa2e4d
Add quit from death handlers for agent and server, implement default …
brandonhoughton Feb 4, 2021
46522ab
Good!
MadcowD Feb 4, 2021
40a821c
Remove large inv
brandonhoughton Feb 4, 2021
386d7ef
Better respawning
MadcowD Feb 6, 2021
bae28af
Merge branch 'wb/dirty_rebased' of github.com:minerllabs/minerl into …
MadcowD Feb 6, 2021
521b9ab
Update the recorder function in rapid
brandonhoughton Feb 6, 2021
d021bab
Adding hack to prevent menu open and close causing issues :)
MadcowD Feb 8, 2021
88db31c
Merge branch 'wb/dirty_rebased' of github.com:minerllabs/minerl into …
MadcowD Feb 8, 2021
b4b583d
Rebase on dev
brandonhoughton Mar 10, 2021
8f4fa2d
Update test_build_deploy.sh
brandonhoughton Mar 11, 2021
dfa0551
Added bounded world handlers co-authored by wguss
brandonhoughton Feb 4, 2021
94cdeb0
Testing out dirt collection minigame
brandonhoughton Feb 4, 2021
1b643b9
Update BoundedWorldHandler
brandonhoughton Feb 4, 2021
81ddb74
Added BundledWorldDecratorImplemntation.java
brandonhoughton Feb 4, 2021
426c9ec
Revert "human action space + enabled GUI (#449)"
brandonhoughton Feb 4, 2021
cd58b00
New handler
MadcowD Feb 4, 2021
ced9603
Addkinf foliage
MadcowD Feb 4, 2021
7fe5da8
Updating pullback
MadcowD Feb 4, 2021
ff04c6f
Removing prints
MadcowD Feb 4, 2021
fd69a88
Add quit from death handlers for agent and server, implement default …
brandonhoughton Feb 4, 2021
877e38d
Good!
MadcowD Feb 4, 2021
3ab329c
Better respawning
MadcowD Feb 6, 2021
409c758
Remove large inv
brandonhoughton Feb 4, 2021
05f3438
Adding hack to prevent menu open and close causing issues :)
MadcowD Feb 8, 2021
d719a73
Update the recorder function in rapid
brandonhoughton Feb 6, 2021
3d93181
Rebase on dev
brandonhoughton Mar 10, 2021
ac0f89d
Merge remote-tracking branch 'origin/brandon/respawn' into brandon/re…
brandonhoughton Mar 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// --------------------------------------------------------------------------------------------------
// Copyright (c) 2016 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------------------------------

package com.microsoft.Malmo.MissionHandlers;

import com.microsoft.Malmo.MissionHandlerInterfaces.IWantToQuit;
import com.microsoft.Malmo.Schemas.AgentQuitFromTimeUp;
import com.microsoft.Malmo.Schemas.MissionInit;
import net.minecraft.client.Minecraft;
import net.minecraft.util.DamageSource;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

/**
* IWantToQuit object that returns true when an agent dies.
* Also records the reason for death.
*/

public class AgentQuitFromDeathImplementation extends HandlerBase implements IWantToQuit
{
private String quitCode = "";
private DamageSource deathSource = null;


@Override
public boolean parseParameters(Object params)
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick - your opening curly brackets seem inconsistent (sometimes they are on a new line, sometimes on the same line as function / class name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK with either convention, I'll try and normalize. Off-topic should we lint all of the code base? I had a huge diff here because the code became auto-linted

return true;
}


@SubscribeEvent(priority = EventPriority.LOWEST)
public void onClientTick(LivingDeathEvent event)
{
// Use the client tick to ensure we regularly update our state (from the client thread)
this.deathSource = event.getSource();
// TODO this could have a lot more info here about who killed the agent, location, ect.
this.quitCode = this.deathSource.getDamageType();
}

@Override
public boolean doIWantToQuit(MissionInit missionInit) {
return Minecraft.getMinecraft().player.isDead;
}

@Override
public void prepare(MissionInit missionInit) {}

@Override
public void cleanup() {}

@Override
public String getOutcome() { return this.quitCode; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// --------------------------------------------------------------------------------------------------
// Copyright (c) 2016 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------------------------------

package com.microsoft.Malmo.MissionHandlers;

import com.microsoft.Malmo.MissionHandlerInterfaces.IWantToQuit;
import com.microsoft.Malmo.Schemas.ServerQuitFromDeath;
import com.microsoft.Malmo.Schemas.MissionInit;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayerMP;

import java.util.Objects;

/**
* IWantToQuit object that returns true when an agent dies.
* Also records the reason for death.
*/

public class ServerQuitFromDeathImplementation extends AgentQuitFromDeathImplementation implements IWantToQuit
{
private ServerQuitFromDeath params = null;
private Boolean quitWhenAnyDead = true;

@Override
public boolean parseParameters(Object params) {
if (!(params instanceof ServerQuitFromDeath))
return false;

this.params = (ServerQuitFromDeath) params;
this.quitWhenAnyDead = this.params.isQuitWhenAnyDead();
return true;
}


@Override
public boolean doIWantToQuit(MissionInit missionInit) {
for (EntityPlayerMP playerMP : Objects.requireNonNull(Minecraft.getMinecraft().getIntegratedServer()).getPlayerList().playerEntityList) {
if (this.quitWhenAnyDead == playerMP.isDead)
return this.quitWhenAnyDead;
}
return !this.quitWhenAnyDead;
}

@Override
public void prepare(MissionInit missionInit) {}

@Override
public void cleanup() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public abstract class MixinMinecraftGameloop {
@Shadow public Snooper usageSnooper;
@Shadow public abstract int getLimitFramerate();
@Shadow public abstract boolean isFramerateLimitBelowMax();
@Shadow public boolean inGameHasFocus;

@Shadow public boolean inGameHasFocus;
@Shadow private int leftClickCounter;

@Shadow public abstract void displayGuiScreen(GuiScreen guiScreen);

private int numTicksPassed = 0;


Expand Down Expand Up @@ -268,14 +268,14 @@ private void runGameLoop() throws IOException

this.mcProfiler.endSection(); //root
}

@Overwrite
public void setIngameFocus()
{
if (!this.inGameHasFocus) {
this.inGameHasFocus = true;
this.displayGuiScreen((GuiScreen) null);
this.leftClickCounter = 10000;
this.leftClickCounter = 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we leave this 10000 the agent has to wait that long before it is able to interact with anything again. Potentially a better fix would be to set this to 0 on respawn, but this should not affect the behavior of agents

}
}

Expand Down
2 changes: 2 additions & 0 deletions minerl/Malmo/Schemas/Mission.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@
<!-- When adding a new command handler, make sure to update MissionSpec::getListOfCommandHandlers and MissionSpec::getAllowedCommands, and add to TurnBasedApplicableCommandHandlers (below) if appropriate -->

<xs:element ref="AgentQuitFromTimeUp" minOccurs="0"/>
<xs:element ref="AgentQuitFromDeath" minOccurs="0"/>
<xs:element ref="AgentQuitFromReachingPosition" minOccurs="0"/>
<xs:element ref="AgentQuitFromTouchingBlockType" minOccurs="0"/>
<xs:element ref="AgentQuitFromCollectingItem" minOccurs="0"/>
Expand Down Expand Up @@ -580,6 +581,7 @@
</xs:appinfo>
</xs:annotation>
<xs:element ref="ServerQuitFromTimeUp"/>
<xs:element ref="ServerQuitFromDeath"/>
<xs:element ref="ServerQuitWhenAnyAgentFinishes"/>
</xs:choice>
</xs:sequence>
Expand Down
20 changes: 20 additions & 0 deletions minerl/Malmo/Schemas/MissionHandlers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,17 @@
</xs:complexType>
</xs:element>

<xs:element name="ServerQuitFromDeath">
Copy link
Collaborator

@pzhokhov pzhokhov Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe <ServerTimelimit>? QuitFromDeath seems misleading, if this is supposed to be a timelimit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

<xs:annotation>
<xs:documentation>
Specifies a time limit that applies to all agents.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="quitWhenAnyDead" type="xs:boolean" use="optional" default="false"/>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the default behavior here is different then the current default behavior for experiments (they are more simmilar to quitWhenAnyDead=true)

</xs:complexType>
</xs:element>

<xs:element name="ServerQuitWhenAnyAgentFinishes">
<xs:annotation>
<xs:documentation>
Expand Down Expand Up @@ -3109,6 +3120,15 @@
</xs:complexType>
</xs:element>

<xs:element name="AgentQuitFromDeath">
<xs:annotation>
<xs:documentation>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for agent to quit means to not respawn, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct - they will stop being in the episode and the number of active agents will decrease unless the server also wants to quit when any agent dies

Copy link
Collaborator

@pzhokhov pzhokhov Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if AgentQuitFromDeath is true, <ServerQuitFromDeath quitWhenAnyDead=false>, and all agents die? Would the server restart in that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The episode should end when there are 0 agents and the episode has started. I will check

Allows the agent to quit on death. If not specified, agents will respawn per vanilla spawn mechanics.
Sets the quit code of the agent based on how the agent died, e.g. "lava", "cactus", "flyIntoWall"
</xs:documentation>
</xs:annotation>
</xs:element>

<xs:element name="AgentQuitFromTouchingBlockType">
<xs:annotation>
<xs:documentation>
Expand Down
11 changes: 11 additions & 0 deletions minerl/herobraine/hero/handlers/agent/quit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def __init__(self, blocks: List[str]):
self.blocks = blocks


# <AgentQuitFromDeath/>
class AgentQuitFromDeath(Handler):
def to_string(self) -> str:
return "agent_quit_from_death"

def xml_template(self) -> str:
return str(
"""<AgentQuitFromDeath/>"""
)


# <AgentQuitFromCraftingItem>
# <Item type="iron_pickaxe"/>
# <Item type="wooden_axe"/>
Expand Down
17 changes: 17 additions & 0 deletions minerl/herobraine/hero/handlers/server/quit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ def __init__(self, time_limit_ms: int, description="out_of_time"):
self.time_limit_ms = time_limit_ms
self.description = description

class ServerQuitFromDeath(Handler):
""" Forces the server to quit after a certain time_limit_ms
also specifies a description parameter for the xml."""

def to_string(self) -> str:
return "server_quit_from_death"

def xml_template(self) -> str:
return str(
"""<ServerQuitFromTimeUp
quitWhenAnyDead="{{quit_when_any_agent_dies}}"/>
"""
)

def __init__(self, quit_when_any_agent_dies: bool):
self.quit_when_any_agent_dies = quit_when_any_agent_dies


class ServerQuitWhenAnyAgentFinishes(Handler):
""" Forces the server to quit if any of the agents involved quits.
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_build_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mkdir -p $MINERL_DATA_ROOT
pip install -e .
#az storage copy -s $AZ_MINERL_DATA -d $MINERL_DATA_ROOT --recursive --subscription sci

pytest .
pytest . -n 2
pip uninstall -y minerl

pip list
Expand Down