Beispiel #1
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.oracle_glyph = None
     for glyph in range(nethack.GLYPH_MON_OFF, nethack.GLYPH_PET_OFF):
         if nethack.permonst(nethack.glyph_to_mon(glyph)).mname == "Oracle":
             self.oracle_glyph = glyph
             break
     assert self.oracle_glyph is not None
Beispiel #2
0
    def test_glyph_to(self):
        assert np.all(
            nethack.glyph_to_mon(
                np.array([
                    nethack.GLYPH_MON_OFF,
                    nethack.GLYPH_PET_OFF,
                    nethack.GLYPH_DETECT_OFF,
                    nethack.GLYPH_RIDDEN_OFF,
                    nethack.GLYPH_STATUE_OFF,
                ])) == 0)

        # STATUE and CORPSE from onames.h (generated by makedefs).
        # Returned by glyph_to_obj.
        corpse = get_object("corpse").oc_name_idx
        statue = get_object("statue").oc_name_idx
        np.testing.assert_array_equal(
            nethack.glyph_to_obj(
                np.array([
                    nethack.GLYPH_BODY_OFF,
                    nethack.GLYPH_STATUE_OFF,
                    nethack.GLYPH_OBJ_OFF,
                ])),
            np.array([corpse, statue, 0]),
        )

        for idx in range(nethack.MAXPCHARS):  # Find the arrow trap.
            if nethack.symdef.from_idx(idx).explanation == "arrow trap":
                np.testing.assert_array_equal(
                    nethack.glyph_to_trap(
                        np.array([
                            nethack.GLYPH_CMAP_OFF,
                            nethack.GLYPH_CMAP_OFF + idx
                        ])),
                    # Traps are one-indexed in defsym_to_trap as per rm.h.
                    np.array([nethack.NO_GLYPH, 1]),
                )
                break

        np.testing.assert_array_equal(
            nethack.glyph_to_cmap(
                np.array([
                    nethack.GLYPH_CMAP_OFF,
                    nethack.GLYPH_STATUE_OFF,
                ])),
            np.array([0, nethack.NO_GLYPH]),
        )

        assert nethack.glyph_to_swallow(nethack.GLYPH_SWALLOW_OFF) == 0

        np.testing.assert_array_equal(
            nethack.glyph_to_warning(
                np.arange(nethack.GLYPH_WARNING_OFF,
                          nethack.GLYPH_STATUE_OFF)),
            np.arange(nethack.WARNCOUNT),
        )
Beispiel #3
0
    def test_permonst_and_class_sym(self):
        glyph = 155  # Lichen.

        mon = nethack.permonst(nethack.glyph_to_mon(glyph))

        assert mon.mname == "lichen"

        cs = nethack.class_sym.from_mlet(mon.mlet)

        assert cs.sym == "F"
        assert cs.explain == "fungus or mold"

        assert nethack.NHW_MESSAGE == 1
        assert hasattr(nethack, "MAXWIN")
Beispiel #4
0
    def test_simple(self):
        glyph = 155  # Lichen.

        mon = nethack.permonst(nethack.glyph_to_mon(glyph))

        self.assertEqual(mon.mname, "lichen")

        cs = nethack.class_sym.from_mlet(mon.mlet)

        self.assertEqual(cs.sym, "F")
        self.assertEqual(cs.explain, "fungus or mold")

        self.assertEqual(nethack.NHW_MESSAGE, 1)
        self.assertTrue(hasattr(nethack, "MAXWIN"))
Beispiel #5
0
    def test_run(self):
        archivefile = tempfile.mktemp(suffix="nethack_test", prefix=".zip")
        game = nethack.NetHack(archivefile=archivefile)

        response = game.reset()
        actions = [
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
        ]

        for action in actions:
            while not response.ProgramState().InMoveloop():
                response, done, info = game.step(nethack.MiscAction.MORE)

            response, done, info = game.step(action)
            if done:
                # Only the good die young.
                response = game.reset()

            obs = response.Observation()
            chars = _fb_ndarray_to_np(obs.Chars())
            glyphs = _fb_ndarray_to_np(obs.Glyphs())

            status = response.Blstats()
            x, y = status.CursX(), status.CursY()

            self.assertEqual(np.count_nonzero(chars == ord("@")), 1)
            self.assertEqual(chars[y, x], ord("@"))

            mon = nethack.permonst(nethack.glyph_to_mon(glyphs[y][x]))
            self.assertEqual(mon.mname, "monk")
            self.assertEqual(mon.mlevel, 10)

            class_sym = nethack.class_sym.from_mlet(mon.mlet)
            self.assertEqual(class_sym.sym, "@")
            self.assertEqual(class_sym.explain, "human or elf")

        self.assertEqual(os.waitpid(info["pid"], os.WNOHANG), (0, 0))

        del game  # Should kill process.

        with self.assertRaisesRegex(OSError, "No (child|such)? process"):
            os.waitpid(info["pid"], 0)
Beispiel #6
0
    def test_run(self):
        game = nethack.Nethack(observation_keys=("glyphs", "chars", "colors",
                                                 "blstats", "program_state"))
        _, _, _, _, program_state = game.reset()
        actions = [
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
        ]

        for action in actions:
            while not program_state[3]:  # in_moveloop.
                obs, done = game.step(nethack.MiscAction.MORE)
                _, _, _, _, program_state = obs

            obs, done = game.step(action)
            if done:
                # Only the good die young.
                obs = game.reset()

            glyphs, chars, colors, blstats, _ = obs

            x, y = blstats[:2]

            assert np.count_nonzero(chars == ord("@")) == 1

            # That's where you're @.
            assert chars[y, x] == ord("@")

            # You're bright (4th bit, 8) white (7), too.
            assert colors[y, x] == 8 ^ 7

            mon = nethack.permonst(nethack.glyph_to_mon(glyphs[y][x]))
            assert mon.mname == "monk"
            assert mon.mlevel == 10

            class_sym = nethack.class_sym.from_mlet(mon.mlet)
            assert class_sym.sym == "@"
            assert class_sym.explain == "human or elf"

        game.close()
        assert os.path.isfile(
            os.path.join(os.getcwd(),
                         "nle.ttyrec%i.bz2" % nethack.TTYREC_VERSION))
Beispiel #7
0
    def test_run(self):
        # TODO: Implement ttyrecording filename in libnethack wrapper.
        # archivefile = tempfile.mktemp(suffix="nethack_test", prefix=".zip")

        game = nethack.Nethack(observation_keys=("glyphs", "chars", "colors",
                                                 "blstats", "program_state"))
        _, _, _, _, program_state = game.reset()
        actions = [
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
            nethack.MiscAction.MORE,
        ]

        for action in actions:
            while not program_state[3]:  # in_moveloop.
                obs, done = game.step(nethack.MiscAction.MORE)
                _, _, _, _, program_state = obs

            obs, done = game.step(action)
            if done:
                # Only the good die young.
                obs = game.reset()

            glyphs, chars, colors, blstats, _ = obs

            x, y = blstats[:2]

            assert np.count_nonzero(chars == ord("@")) == 1

            # That's where you're @.
            assert chars[y, x] == ord("@")

            # You're bright (4th bit, 8) white (7), too.
            assert colors[y, x] == 8 ^ 7

            mon = nethack.permonst(nethack.glyph_to_mon(glyphs[y][x]))
            assert mon.mname == "monk"
            assert mon.mlevel == 10

            class_sym = nethack.class_sym.from_mlet(mon.mlet)
            assert class_sym.sym == "@"
            assert class_sym.explain == "human or elf"

        game.close()
Beispiel #8
0
    def test_permonst_and_class_sym(self):
        glyph = 155  # Lichen.

        mon = nethack.permonst(nethack.glyph_to_mon(glyph))

        assert mon.mname == "lichen"

        cs = nethack.class_sym.from_mlet(mon.mlet)

        assert cs.sym == "F"
        assert cs.explain == "fungus or mold"

        assert nethack.NHW_MESSAGE == 1
        assert hasattr(nethack, "MAXWIN")

        # Slightly irritating to need `chr` here.
        cs = nethack.class_sym.from_oc_class(chr(nethack.WAND_CLASS))
        assert cs.sym == "/"
        assert cs.explain == "wand"

        obj = nethack.objclass(0)
        cs = nethack.class_sym.from_oc_class(obj.oc_class)
        assert cs.sym == "]"
        assert cs.explain == "strange object"