-
Notifications
You must be signed in to change notification settings - Fork 126
Eric/competition #156
Eric/competition #156
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,9 +14,22 @@ def C(c): | |
| return 0x1F & c | ||
|
|
||
|
|
||
| # Missing here: | ||
| # Some characters for text input (e.g., +). | ||
| # General menu handling isn't part of this either. | ||
| class TextCharacters(enum.IntEnum): | ||
| PLUS = ord("+") | ||
| MINUS = ord("-") | ||
| SPACE = ord(" ") | ||
| APOS = ord("'") | ||
| QUOTE = ord('"') | ||
| NUM_0 = ord("0") | ||
| NUM_1 = ord("1") | ||
| NUM_2 = ord("2") | ||
| NUM_3 = ord("3") | ||
| NUM_4 = ord("4") | ||
| NUM_5 = ord("5") | ||
| NUM_6 = ord("6") | ||
| NUM_7 = ord("7") | ||
| NUM_8 = ord("8") | ||
| NUM_9 = ord("9") | ||
|
|
||
|
|
||
| class CompassCardinalDirection(enum.IntEnum): | ||
|
|
@@ -76,6 +89,12 @@ class MiscAction(enum.IntEnum): | |
| MORE = ord("\r") # read the next message | ||
|
|
||
|
|
||
| class UnsafeActions(enum.IntEnum): | ||
| # currently these result in an error or undesirable behaviour | ||
| HELP = ord("?") # give a help message | ||
| PREVMSG = C("p") # view recent game messages | ||
heiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class Command(enum.IntEnum): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, this was "all commands" (or something), and the "RL relevant ones" we gathered in their own enum. What's the philosophy now?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This enum wasn't really all commands because it didnt contain movement etc? I figured if we were splitting out lets at least clearly mark the very dangerous ones. |
||
| EXTCMD = ord("#") # perform an extended command | ||
| EXTLIST = M("?") # list all extended commands | ||
|
|
@@ -100,7 +119,6 @@ class Command(enum.IntEnum): | |
| FIGHT = ord("F") # Prefix: force fight even if you don't see a monster | ||
| FORCE = M("f") # force a lock | ||
| GLANCE = ord(";") # show what type of thing a map symbol corresponds to | ||
| HELP = ord("?") # give a help message | ||
| HISTORY = ord("V") # show long version and game history | ||
| INVENTORY = ord("i") # show your inventory | ||
| INVENTTYPE = ord("I") # inventory specific item types | ||
|
|
@@ -121,7 +139,6 @@ class Command(enum.IntEnum): | |
| PAY = ord("p") # pay your shopping bill | ||
| PICKUP = ord(",") # pick up things at the current location | ||
| PRAY = M("p") # pray to the gods for help | ||
| PREVMSG = C("p") # view recent game messages | ||
| PUTON = ord("P") # put on an accessory (ring, amulet, etc) | ||
| QUAFF = ord("q") # quaff (drink) something | ||
| QUIT = M("q") # exit without saving current game | ||
|
|
@@ -132,6 +149,7 @@ class Command(enum.IntEnum): | |
| RIDE = M("R") # mount or dismount a saddled steed | ||
| RUB = M("r") # rub a lamp or a stone | ||
| RUSH = ord("g") # Prefix: rush until something interesting is seen | ||
| RUSH2 = ord("G") # Prefix: rush until something interesting is seen | ||
| SAVE = ord("S") # save the game and exit | ||
| SEARCH = ord("s") # search for traps and secret doors | ||
| SEEALL = ord("*") # show all equipment in use | ||
|
|
@@ -163,6 +181,7 @@ class Command(enum.IntEnum): | |
| + list(MiscDirection) | ||
| + list(MiscAction) | ||
| + list(Command) | ||
| + list(TextCharacters) | ||
| ) | ||
|
|
||
| NON_RL_ACTIONS = ( | ||
|
|
@@ -172,13 +191,11 @@ class Command(enum.IntEnum): | |
| Command.EXTCMD, # Potentially useful for some wizard actions. | ||
| Command.EXTLIST, | ||
| Command.GLANCE, | ||
| Command.HELP, | ||
| Command.HISTORY, | ||
| Command.KNOWN, # Could potentially be useful. | ||
| Command.KNOWNCLASS, # Could potentially be useful. | ||
| Command.OPTIONS, | ||
| Command.OVERVIEW, # Could potentially be useful. | ||
| Command.PREVMSG, # Could potentially be useful. | ||
| Command.TELEPORT, | ||
| Command.QUIT, | ||
| Command.REDRAW, | ||
|
|
@@ -191,13 +208,15 @@ class Command(enum.IntEnum): | |
| ) | ||
|
|
||
| _USEFUL_ACTIONS = list(ACTIONS) | ||
| for action in NON_RL_ACTIONS: | ||
| for action in NON_RL_ACTIONS + tuple(TextCharacters): | ||
| _USEFUL_ACTIONS.remove(action) | ||
| _USEFUL_ACTIONS.append(TextCharacters.SPACE) | ||
| USEFUL_ACTIONS = tuple(_USEFUL_ACTIONS) | ||
| del _USEFUL_ACTIONS | ||
|
|
||
| _ACTIONS_DICT = {} | ||
| for enum_class in [ | ||
| TextCharacters, | ||
| CompassDirection, | ||
| CompassDirectionLonger, | ||
| MiscDirection, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,11 +45,12 @@ def get_action(env, action_mode, is_raw_env): | |
| action = env.action_space.sample() | ||
| else: | ||
| action = random.choice(_ACTIONS) | ||
| print(action) | ||
| elif action_mode == "human": | ||
| while True: | ||
| with no_echo(): | ||
| ch = ord(os.read(0, 1)) | ||
| if ch in [nethack.C("c"), ord(b"q")]: | ||
| if ch in [nethack.C("c")]: | ||
| print("Received exit code {}. Aborting.".format(ch)) | ||
| return None | ||
| try: | ||
|
|
@@ -67,7 +68,18 @@ def get_action(env, action_mode, is_raw_env): | |
| return action | ||
|
|
||
|
|
||
| def play(env, mode, ngames, max_steps, seeds, savedir, no_render, render_mode, debug): | ||
| def play( | ||
| env, | ||
| mode, | ||
| ngames, | ||
| max_steps, | ||
| seeds, | ||
| savedir, | ||
| no_render, | ||
| render_mode, | ||
| print_frames_separately, | ||
| **kwargs, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do we use the **kwargs for?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
| ): | ||
| env_name = env | ||
| is_raw_env = env_name == "raw" | ||
|
|
||
|
|
@@ -100,10 +112,15 @@ def play(env, mode, ngames, max_steps, seeds, savedir, no_render, render_mode, d | |
| while True: | ||
| if not no_render: | ||
| if not is_raw_env: | ||
| print("Previous reward:", reward) | ||
| if action is not None: | ||
| print("Previous action: %s" % repr(env._actions[action])) | ||
| print("--------") | ||
| print(f"Previous reward: {str(reward):64s}") | ||
| act_str = repr(env._actions[action]) if action is not None else "" | ||
| print(f"Previous action: {str(act_str):64s}") | ||
| print("--------") | ||
| env.render(render_mode) | ||
| print("--------") | ||
| if not print_frames_separately: | ||
| print("\033[31A") # Go up 31 lines. | ||
| else: | ||
| print("Previous action:", action) | ||
| _, chars, _, _, blstats, message, *_ = obs | ||
|
|
@@ -114,6 +131,7 @@ def play(env, mode, ngames, max_steps, seeds, savedir, no_render, render_mode, d | |
| print(blstats) | ||
|
|
||
| action = get_action(env, mode, is_raw_env) | ||
|
|
||
| if action is None: | ||
| break | ||
|
|
||
|
|
@@ -194,7 +212,7 @@ def main(): | |
| parser.add_argument( | ||
| "--max-steps", | ||
| type=int, | ||
| default=10000, | ||
| default=1_000_000, | ||
| help="Number of maximum steps per episode.", | ||
| ) | ||
| parser.add_argument( | ||
|
|
@@ -219,6 +237,12 @@ def main(): | |
| choices=["human", "full", "ansi"], | ||
| help="Render mode. Defaults to 'human'.", | ||
| ) | ||
| parser.add_argument( | ||
| "--print-frames-separately", | ||
| "-p", | ||
| action="store_true", | ||
| help="Don't overwrite frames, print them all.", | ||
| ) | ||
| flags = parser.parse_args() | ||
|
|
||
| if flags.debug: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.