diff --git a/src/eressea/lua/building.cpp b/src/eressea/lua/building.cpp index c28ac0403..ccfde1ab4 100644 --- a/src/eressea/lua/building.cpp +++ b/src/eressea/lua/building.cpp @@ -1,16 +1,21 @@ #include #include #include +#include "list.h" // kernel includes #include #include +#include // lua includes #include #include #include +// util includes +#include + using namespace luabind; static building * @@ -130,6 +135,44 @@ building_setname(building& b, const char * name) set_string(&b.name, name); } +static std::ostream& +operator<<(std::ostream& stream, building& b) +{ + stream << b.name; + stream << " (" << itoa36(b.no) << ")"; + stream << ", " << b.type->_name; + stream << " size " << b.size; + return stream; +} + +static bool +operator==(const building& a, const building&b) +{ + return a.no==b.no; +} + +class buildingunit { +public: + static unit * next(unit * node) { + building * b = node->building; + do { + node = node->next; + } while (node !=NULL && node->building!=b); + return node; + } + static unit * value(unit * node) { return node; } +}; + + +static eressea::list +building_units(const building& b) { + region * r = b.region; + unit * u = r->units; + while (u!=NULL && u->building!=&b) u=u->next; + return eressea::list(u); +} + + void bind_building(lua_State * L) { @@ -139,10 +182,13 @@ bind_building(lua_State * L) def("add_building", &add_building), class_("building") + .def(tostring(self)) + .def(self == building()) .property("name", &building_getname, &building_setname) + .property("info", &building_getinfo, &building_setinfo) + .property("units", &building_units, return_stl_iterator) .def_readonly("region", &building::region) .def_readonly("id", &building::no) - .property("info", &building_getinfo, &building_setinfo) .def_readwrite("size", &building::size) .def("add_effect", &building_addeffect) ]; diff --git a/src/eressea/lua/unit.cpp b/src/eressea/lua/unit.cpp index 8ed9d1bc2..8c547b81b 100644 --- a/src/eressea/lua/unit.cpp +++ b/src/eressea/lua/unit.cpp @@ -229,6 +229,19 @@ unit_getregion(const unit& u) return u.region; } +static void +unit_setbuilding(unit& u, building& b) +{ + leave(u.region, u); + u.building = &b; +} + +static building * +unit_getbuilding(const unit& u) +{ + return u.building; +} + static int unit_getid(const unit& u) { @@ -398,6 +411,7 @@ bind_unit(lua_State * L) .def("remove_spell", &unit_removespell) .property("magic", &unit_getmagic, &unit_setmagic) .property("aura", &unit_getaura, &unit_setaura) + .property("building", &unit_getbuilding, &unit_setbuilding) .property("region", &unit_getregion, &unit_setregion) .property("is_familiar", &unit_isfamiliar) .property("spells", &unit_spells, return_stl_iterator)