#include #include #include "list.h" // Atributes includes #include // kernel includes #include #include #include #include #include #include #include #include // util includes #include // lua includes #include #include #include #include using namespace luabind; class bind_spell_ptr { public: static spell_ptr * next(spell_ptr * node) { return node->next; } static spell * value(spell_ptr * node) { return find_spellbyid(node->spellid); } }; static eressea::list unit_spells(const unit& u) { sc_mage * mage = get_mage(&u); if (mage==NULL) return eressea::list(NULL); spell_ptr * splist = mage->spellptr; return eressea::list(splist); } class bind_spell_list { public: static spell_list * next(spell_list * node) { return node->next; } static spell * value(spell_list * node) { return node->data; } }; static eressea::list unit_familiarspells(const unit& u) { spell_list * spells = familiarspells(u.race); return eressea::list(spells); } static unit * add_unit(faction * f, region * r) { if (f->units==NULL) return addplayer(r, f); return createunit(r, f, 0, f->race); } static void unit_setnumber(unit& u, int number) { if (u.number==0) { set_number(&u, number); u.hp = unit_max_hp(&u) * number; } else { scale_number(&u, number); } } static void unit_setracename(unit& u, const char * name) { set_racename(&u.attribs, name); } static int unit_getnumber(const unit& u) { return u.number; } static int unit_getitem(const unit& u, const char * iname) { const item_type * itype = it_find(iname); if (itype!=NULL) { return i_get(u.items, itype); } return -1; } static int unit_additem(unit& u, const char * iname, int number) { const item_type * itype = it_find(iname); if (itype!=NULL) { item * i = i_change(&u.items, itype, number); return i?i->number:0; } // if (itype!=NULL) return -1; } static int unit_getskill(const unit& u, const char * skname) { skill_t sk = sk_find(skname); if (sk!=NOSKILL) { skill * sv = get_skill(&u, sk); if (sv==NULL) return 0; return sv->level; } return -1; } static int unit_effskill(const unit& u, const char * skname) { skill_t sk = sk_find(skname); if (sk!=NOSKILL) { return effskill(&u, sk); } return -1; } static int unit_setskill(unit& u, const char * skname, int level) { skill_t sk = sk_find(skname); if (sk!=NOSKILL) { set_level(&u, sk, level); return level; } // if (sk!=NULL) return -1; } static const char * unit_getrace(const unit& u) { return u.race->_name[0]; } static void unit_setrace(unit& u, const char * rcname) { race * rc = rc_find(rcname); if (rc!=NULL) { u.race = rc; } } static void unit_addspell(unit& u, const char * name) { bool add = false; spell_list * slist = spells; while (slist!=NULL) { spell * sp = slist->data; if (strcmp(name, sp->sname)==0) { if (add) log_error(("two spells are called %s.\n", name)); addspell(&u, sp->id); add = true; } slist=slist->next; } if (!add) log_error(("spell %s could not be found\n", name)); } static bool unit_isfamiliar(const unit& u) { return is_familiar(&u)!=0; } static void unit_removespell(unit& u, const spell * sp) { sc_mage * mage = get_mage(&u); if (mage!=NULL) { spell_ptr ** isptr = &mage->spellptr; while (*isptr && (*isptr)->spellid != sp->id) { isptr = &(*isptr)->next; } if (*isptr) { spell_ptr * sptr = *isptr; *isptr = sptr->next; free(sptr); } } } static int unit_hpmax(const unit& u) { return unit_max_hp(&u); } static void unit_setregion(unit& u, region& r) { move_unit(&u, &r, NULL); } static region * unit_getregion(const unit& u) { return u.region; } static const char * unit_getname(const unit& u) { return u.name; } static void unit_setname(unit& u, const char * name) { set_string(&u.name, name); } static std::ostream& operator<<(std::ostream& stream, unit& u) { stream << u.name << " (" << itoa36(u.no) << ")"; return stream; } static bool operator==(const unit& a, const unit&b) { return a.no==b.no; } static int unit_getaura(const unit& u) { return get_spellpoints(&u); } static void unit_setaura(unit& u, int points) { return set_spellpoints(&u, points); } static const char * unit_getmagic(const unit& u) { sc_mage * mage = get_mage(&u); return mage?magietypen[mage->magietyp]:NULL; } static void unit_setmagic(unit& u, const char * type) { sc_mage * mage = get_mage(&u); magic_t mtype; for (mtype=0;mtype!=MAXMAGIETYP;++mtype) { if (strcmp(magietypen[mtype], type)==0) break; } if (mtype==MAXMAGIETYP) return; if (mage==NULL) { mage = create_mage(&u, mtype); } } static void unit_addorder(unit& u, const char * str) { char buf[1024]; char * s = buf; boolean quote = false; while (*str) { switch (*str) { case '"': quote=!quote; break; case ' ': if (quote) *s++ = SPACE_REPLACEMENT; else *s++ = ' '; break; default: *s++ = *str; } ++str; } *s=0; addstrlist(&u.orders, buf); u.faction->lastorders = turn; } static void unit_clearorders(unit& u) { freestrlist(u.orders); u.orders = NULL; } void bind_unit(lua_State * L) { module(L)[ def("get_unit", &findunit), def("add_unit", &add_unit), class_("unit") .property("name", &unit_getname, &unit_setname) .def(tostring(self)) .def(self == unit()) .def_readonly("faction", &unit::faction) .def_readonly("id", &unit::no) .def_readwrite("hp", &unit::hp) .def_readwrite("status", &unit::status) .def("add_order", &unit_addorder) .def("clear_orders", &unit_clearorders) .def("get_item", &unit_getitem) .def("add_item", &unit_additem) .def("get_skill", &unit_getskill) .def("eff_skill", &unit_effskill) .def("set_skill", &unit_setskill) .def("set_racename", &unit_setracename) .def("add_spell", &unit_addspell) .def("remove_spell", &unit_removespell) .property("magic", &unit_getmagic, &unit_setmagic) .property("aura", &unit_getaura, &unit_setaura) .property("region", &unit_getregion, &unit_setregion) .property("is_familiar", &unit_isfamiliar) .property("spells", &unit_spells, return_stl_iterator) .property("familiarspells", &unit_familiarspells, return_stl_iterator) .property("number", &unit_getnumber, &unit_setnumber) .property("race", &unit_getrace, &unit_setrace) .property("hp_max", &unit_hpmax) ]; }