Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from facebookresearch/update-helpers
Browse files Browse the repository at this point in the history
Update helpers
  • Loading branch information
Heinrich Kuttler authored Jul 29, 2020
2 parents 48e016a + 065a86b commit fece820
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion nle/env/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ 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.glyph_to_mon(glyph).mname == "Oracle":
if nethack.permonst(nethack.glyph_to_mon(glyph)).mname == "Oracle":
self.oracle_glyph = glyph
break
assert self.oracle_glyph is not None
Expand Down
16 changes: 12 additions & 4 deletions nle/tests/test_nethack.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def test_run(self):
self.assertEqual(np.count_nonzero(chars == ord("@")), 1)
self.assertEqual(chars[y, x], ord("@"))

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

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

Expand All @@ -69,18 +69,26 @@ class HelperTest(unittest.TestCase):
def test_simple(self):
glyph = 155 # Lichen.

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

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

cs = nethack.mlet_to_class_sym(mon.mlet)
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"))

def test_permonst(self):
mon = nethack.permonst(0)
self.assertEqual(mon.mname, "giant ant")
del mon

mon = nethack.permonst(1)
self.assertEqual(mon.mname, "killer bee")


if __name__ == "__main__":
unittest.main()
36 changes: 21 additions & 15 deletions win/rl/helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ PYBIND11_MODULE(helper, m)
m.attr("MAXWIN") = py::int_(MAXWIN);

m.attr("NUMMONS") = py::int_(NUMMONS);
m.attr("NUM_OBJECTS") = py::int_(NUM_OBJECTS);

// Glyph array offsets. This is what the glyph_is_* functions
// are based on, see display.h.
Expand All @@ -54,7 +55,11 @@ PYBIND11_MODULE(helper, m)
m.attr("NO_GLYPH") = py::int_(NO_GLYPH);
m.attr("GLYPH_INVISIBLE") = py::int_(GLYPH_INVISIBLE);

m.attr("CORPSE") = py::int_(CORPSE);
m.attr("STATUE") = py::int_(STATUE);

m.attr("MAXPCHARS") = py::int_(static_cast<int>(MAXPCHARS));
m.attr("MAXEXPCHARS") = py::int_(static_cast<int>(MAXEXPCHARS));
m.attr("EXPL_MAX") = py::int_(static_cast<int>(EXPL_MAX));
m.attr("NUM_ZAP") = py::int_(static_cast<int>(NUM_ZAP));
m.attr("WARNCOUNT") = py::int_(static_cast<int>(WARNCOUNT));
Expand Down Expand Up @@ -119,7 +124,9 @@ PYBIND11_MODULE(helper, m)
m.def("glyph_is_warning",
[](int glyph) { return glyph_is_warning(glyph); });

py::class_<permonst>(m, "permonst")
py::class_<permonst, std::unique_ptr<permonst, py::nodelete> >(m,
"permonst")
.def(py::init([](int index) -> permonst * { return &mons[index]; }))
.def_readonly("mname", &permonst::mname) /* full name */
.def_readonly("mlet", &permonst::mlet) /* symbol */
.def_readonly("mlevel", &permonst::mlevel) /* base monster level */
Expand All @@ -129,8 +136,7 @@ PYBIND11_MODULE(helper, m)
// .def_readonly("maligntyp", &permonst::maligntyp) /* basic
// monster alignment */
.def_readonly("geno", &permonst::geno) /* creation/geno mask value */
// .def_readonly("mattk", &permonst::mattk) /* attacks matrix
// */
// .def_readonly("mattk", &permonst::mattk) /* attacks matrix */
.def_readonly("cwt", &permonst::cwt) /* weight of corpse */
.def_readonly("cnutrit",
&permonst::cnutrit) /* its nutritional value */
Expand All @@ -151,6 +157,10 @@ PYBIND11_MODULE(helper, m)
;

py::class_<class_sym>(m, "class_sym")
.def_static(
"from_mlet",
[](char let) -> const class_sym * { return &def_monsyms[let]; },
py::return_value_policy::reference)
.def_readonly("sym", &class_sym::sym)
.def_readonly("name", &class_sym::name)
.def_readonly("explain", &class_sym::explain)
Expand All @@ -159,16 +169,12 @@ PYBIND11_MODULE(helper, m)
+ "' explain='" + std::string(cs.explain) + "'>";
});

// m.def("mon", [](const int i) { return mons[i]; });
m.def(
"glyph_to_mon",
[](int glyph) -> const permonst * {
return &mons[glyph_to_mon(glyph)];
},
py::return_value_policy::reference);

m.def(
"mlet_to_class_sym",
[](char let) -> const class_sym * { return &def_monsyms[let]; },
py::return_value_policy::reference);
m.def("glyph_to_mon", [](int glyph) { return glyph_to_mon(glyph); });
m.def("glyph_to_obj", [](int glyph) { return glyph_to_obj(glyph); });
m.def("glyph_to_trap", [](int glyph) { return glyph_to_trap(glyph); });
m.def("glyph_to_cmap", [](int glyph) { return glyph_to_cmap(glyph); });
m.def("glyph_to_swallow",
[](int glyph) { return glyph_to_swallow(glyph); });
m.def("glyph_to_warning",
[](int glyph) { return glyph_to_warning(glyph); });
}

0 comments on commit fece820

Please sign in to comment.