2003-11-10 00:36:11 +01:00
|
|
|
#include <config.h>
|
|
|
|
#include <eressea.h>
|
2004-03-07 13:50:53 +01:00
|
|
|
#include "list.h"
|
2004-06-23 00:00:36 +02:00
|
|
|
#include "script.h"
|
2003-11-10 00:36:11 +01:00
|
|
|
|
2004-02-05 20:04:58 +01:00
|
|
|
// Atributes includes
|
|
|
|
#include <attributes/racename.h>
|
2004-07-10 12:59:47 +02:00
|
|
|
#include <attributes/key.h>
|
2004-02-05 20:04:58 +01:00
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
// kernel includes
|
2004-06-21 18:45:27 +02:00
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/magic.h>
|
|
|
|
#include <kernel/order.h>
|
2004-02-17 00:58:47 +01:00
|
|
|
#include <kernel/race.h>
|
2003-12-14 11:02:29 +01:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/skill.h>
|
2004-03-09 01:00:07 +01:00
|
|
|
#include <kernel/spell.h>
|
2004-06-21 18:45:27 +02:00
|
|
|
#include <kernel/unit.h>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
2004-04-11 18:16:26 +02:00
|
|
|
// util includes
|
2004-04-11 17:11:19 +02:00
|
|
|
#include <util/base36.h>
|
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
// lua includes
|
|
|
|
#include <lua.hpp>
|
|
|
|
#include <luabind/luabind.hpp>
|
|
|
|
#include <luabind/iterator_policy.hpp>
|
|
|
|
|
2004-04-11 17:11:19 +02:00
|
|
|
#include <ostream>
|
2004-06-26 22:51:19 +02:00
|
|
|
#include <string>
|
2003-11-10 00:36:11 +01:00
|
|
|
using namespace luabind;
|
2003-12-14 11:02:29 +01:00
|
|
|
|
2004-03-07 13:50:53 +01:00
|
|
|
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); }
|
|
|
|
};
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
static eressea::list<spell *, spell_ptr *, bind_spell_ptr>
|
2004-03-07 13:50:53 +01:00
|
|
|
unit_spells(const unit& u) {
|
|
|
|
sc_mage * mage = get_mage(&u);
|
2004-06-26 22:51:19 +02:00
|
|
|
if (mage==NULL) return eressea::list<spell *, spell_ptr *, bind_spell_ptr>(NULL);
|
2004-03-07 13:50:53 +01:00
|
|
|
spell_ptr * splist = mage->spellptr;
|
2004-06-26 22:51:19 +02:00
|
|
|
return eressea::list<spell *, spell_ptr *, bind_spell_ptr>(splist);
|
2004-03-07 13:50:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class bind_spell_list {
|
|
|
|
public:
|
|
|
|
static spell_list * next(spell_list * node) { return node->next; }
|
|
|
|
static spell * value(spell_list * node) { return node->data; }
|
|
|
|
};
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
static eressea::list<spell *, spell_list *, bind_spell_list>
|
2004-03-07 13:50:53 +01:00
|
|
|
unit_familiarspells(const unit& u) {
|
|
|
|
spell_list * spells = familiarspells(u.race);
|
2004-06-26 22:51:19 +02:00
|
|
|
return eressea::list<spell *, spell_list *, bind_spell_list>(spells);
|
|
|
|
}
|
|
|
|
|
|
|
|
class bind_orders {
|
|
|
|
public:
|
|
|
|
static order * next(order * node) { return node->next; }
|
|
|
|
static std::string value(order * node) {
|
|
|
|
char * cmd = getcommand(node);
|
|
|
|
std::string s(cmd);
|
|
|
|
free(cmd);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static eressea::list<std::string, order *, bind_orders>
|
|
|
|
unit_orders(const unit& u) {
|
|
|
|
return eressea::list<std::string, order *, bind_orders>(u.orders);
|
2004-03-07 13:50:53 +01:00
|
|
|
}
|
|
|
|
|
2003-12-14 17:34:00 +01:00
|
|
|
static unit *
|
|
|
|
add_unit(faction * f, region * r)
|
|
|
|
{
|
|
|
|
if (f->units==NULL) return addplayer(r, f);
|
|
|
|
return createunit(r, f, 0, f->race);
|
|
|
|
}
|
|
|
|
|
2003-12-14 11:02:29 +01:00
|
|
|
static void
|
|
|
|
unit_setnumber(unit& u, int number)
|
|
|
|
{
|
2003-12-14 20:17:59 +01:00
|
|
|
if (u.number==0) {
|
|
|
|
set_number(&u, number);
|
|
|
|
u.hp = unit_max_hp(&u) * number;
|
2004-04-11 01:59:03 +02:00
|
|
|
} else {
|
|
|
|
scale_number(&u, number);
|
2003-12-14 20:17:59 +01:00
|
|
|
}
|
2003-12-14 11:02:29 +01:00
|
|
|
}
|
|
|
|
|
2004-02-05 20:04:58 +01:00
|
|
|
static void
|
|
|
|
unit_setracename(unit& u, const char * name)
|
|
|
|
{
|
|
|
|
set_racename(&u.attribs, name);
|
|
|
|
}
|
|
|
|
|
2003-12-14 11:02:29 +01:00
|
|
|
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);
|
2004-01-04 17:02:02 +01:00
|
|
|
if (sv==NULL) return 0;
|
2003-12-14 11:02:29 +01:00
|
|
|
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;
|
|
|
|
}
|
2003-11-10 00:36:11 +01:00
|
|
|
|
2004-02-17 00:58:47 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-09 01:00:07 +01:00
|
|
|
static void
|
|
|
|
unit_addspell(unit& u, const char * name)
|
|
|
|
{
|
|
|
|
bool add = false;
|
2004-04-10 22:25:40 +02:00
|
|
|
spell_list * slist = spells;
|
|
|
|
while (slist!=NULL) {
|
|
|
|
spell * sp = slist->data;
|
2004-03-09 01:00:07 +01:00
|
|
|
if (strcmp(name, sp->sname)==0) {
|
|
|
|
if (add) log_error(("two spells are called %s.\n", name));
|
|
|
|
addspell(&u, sp->id);
|
|
|
|
add = true;
|
|
|
|
}
|
2004-04-10 22:25:40 +02:00
|
|
|
slist=slist->next;
|
2004-03-09 01:00:07 +01:00
|
|
|
}
|
|
|
|
if (!add) log_error(("spell %s could not be found\n", name));
|
|
|
|
}
|
|
|
|
|
2004-03-19 01:14:41 +01:00
|
|
|
static bool
|
|
|
|
unit_isfamiliar(const unit& u)
|
|
|
|
{
|
2004-04-10 22:25:40 +02:00
|
|
|
return is_familiar(&u)!=0;
|
2004-03-19 01:14:41 +01:00
|
|
|
}
|
|
|
|
|
2004-03-09 01:00:07 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-19 00:48:40 +01:00
|
|
|
static int
|
|
|
|
unit_hpmax(const unit& u)
|
|
|
|
{
|
|
|
|
return unit_max_hp(&u);
|
|
|
|
}
|
2004-03-09 01:00:07 +01:00
|
|
|
|
2004-04-11 01:21:58 +02:00
|
|
|
static void
|
|
|
|
unit_setregion(unit& u, region& r)
|
|
|
|
{
|
|
|
|
move_unit(&u, &r, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static region *
|
|
|
|
unit_getregion(const unit& u)
|
|
|
|
{
|
|
|
|
return u.region;
|
|
|
|
}
|
|
|
|
|
2004-07-10 14:54:25 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
static int
|
|
|
|
unit_getid(const unit& u)
|
|
|
|
{
|
|
|
|
return u.no;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unit_setid(unit& u, int id)
|
|
|
|
{
|
|
|
|
unit * nu = findunit(id);
|
|
|
|
if (nu==NULL) {
|
|
|
|
uunhash(&u);
|
|
|
|
u.no = id;
|
|
|
|
uhash(&u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-11 02:20:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
static const char *
|
|
|
|
unit_getinfo(const unit& u)
|
|
|
|
{
|
|
|
|
return u.display;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unit_setinfo(unit& u, const char * info)
|
|
|
|
{
|
|
|
|
set_string(&u.display, info);
|
|
|
|
}
|
|
|
|
|
2004-07-10 12:59:47 +02:00
|
|
|
static bool
|
|
|
|
get_flag(const unit& u, const char * name)
|
|
|
|
{
|
|
|
|
int flag = atoi36(name);
|
|
|
|
attrib * a = find_key(u.attribs, flag);
|
|
|
|
return (a!=NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_flag(unit& u, const char * name, bool value)
|
|
|
|
{
|
|
|
|
int flag = atoi36(name);
|
|
|
|
attrib * a = find_key(u.attribs, flag);
|
|
|
|
if (a==NULL && value) {
|
|
|
|
add_key(&u.attribs, flag);
|
|
|
|
} else if (a!=NULL && !value) {
|
|
|
|
a_remove(&u.attribs, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-11 17:11:19 +02:00
|
|
|
static std::ostream&
|
|
|
|
operator<<(std::ostream& stream, unit& u)
|
|
|
|
{
|
2004-07-10 12:59:47 +02:00
|
|
|
const char * rcname = get_racename(u.attribs);
|
|
|
|
stream << u.name << " (" << itoa36(u.no) << "), " << u.number << " " << u.race->_name[0];
|
|
|
|
if (rcname) stream << "/" << rcname;
|
2004-04-11 17:11:19 +02:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
operator==(const unit& a, const unit&b)
|
|
|
|
{
|
|
|
|
return a.no==b.no;
|
|
|
|
}
|
|
|
|
|
2004-05-30 01:50:39 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-31 12:27:33 +02:00
|
|
|
static void
|
|
|
|
unit_addorder(unit& u, const char * str)
|
|
|
|
{
|
2004-07-09 20:28:38 +02:00
|
|
|
order * ord = parse_order(str, u.faction->locale);
|
2004-06-21 18:45:27 +02:00
|
|
|
addlist(&u.orders, ord);
|
2004-05-31 13:50:32 +02:00
|
|
|
u.faction->lastorders = turn;
|
2004-05-31 12:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unit_clearorders(unit& u)
|
|
|
|
{
|
2004-06-21 18:45:27 +02:00
|
|
|
free_orders(&u.orders);
|
2004-05-31 12:27:33 +02:00
|
|
|
}
|
|
|
|
|
2004-06-23 00:00:36 +02:00
|
|
|
static void
|
|
|
|
unit_setscript(struct unit& u, const functor<void>& f)
|
|
|
|
{
|
|
|
|
luabind::functor<void> * fptr = new luabind::functor<void>(f);
|
|
|
|
setscript(&u.attribs, fptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
void
|
|
|
|
bind_unit(lua_State * L)
|
|
|
|
{
|
|
|
|
module(L)[
|
|
|
|
def("get_unit", &findunit),
|
2003-12-14 17:34:00 +01:00
|
|
|
def("add_unit", &add_unit),
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
class_<struct unit>("unit")
|
2004-04-11 17:11:19 +02:00
|
|
|
.def(tostring(self))
|
|
|
|
.def(self == unit())
|
2004-06-26 22:51:19 +02:00
|
|
|
.property("name", &unit_getname, &unit_setname)
|
|
|
|
.property("info", &unit_getinfo, &unit_setinfo)
|
|
|
|
.property("id", &unit_getid, &unit_setid)
|
2003-12-14 11:02:29 +01:00
|
|
|
.def_readonly("faction", &unit::faction)
|
2004-03-19 01:07:32 +01:00
|
|
|
.def_readwrite("hp", &unit::hp)
|
2004-05-30 03:21:28 +02:00
|
|
|
.def_readwrite("status", &unit::status)
|
2004-06-26 22:51:19 +02:00
|
|
|
|
|
|
|
// orders:
|
2004-05-31 12:27:33 +02:00
|
|
|
.def("add_order", &unit_addorder)
|
|
|
|
.def("clear_orders", &unit_clearorders)
|
2004-06-26 22:51:19 +02:00
|
|
|
.property("orders", &unit_orders, return_stl_iterator)
|
|
|
|
|
2004-07-10 12:59:47 +02:00
|
|
|
// key-attribute:
|
|
|
|
.def("set_flag", &set_flag)
|
|
|
|
.def("get_flag", &get_flag)
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
// items:
|
2003-12-14 11:02:29 +01:00
|
|
|
.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)
|
2004-06-23 00:00:36 +02:00
|
|
|
.def("set_brain", &unit_setscript)
|
2004-02-05 20:04:58 +01:00
|
|
|
.def("set_racename", &unit_setracename)
|
2004-03-09 01:00:07 +01:00
|
|
|
.def("add_spell", &unit_addspell)
|
|
|
|
.def("remove_spell", &unit_removespell)
|
2004-05-30 02:29:33 +02:00
|
|
|
.property("magic", &unit_getmagic, &unit_setmagic)
|
2004-05-30 01:50:39 +02:00
|
|
|
.property("aura", &unit_getaura, &unit_setaura)
|
2004-07-10 14:54:25 +02:00
|
|
|
.property("building", &unit_getbuilding, &unit_setbuilding)
|
2004-04-11 01:21:58 +02:00
|
|
|
.property("region", &unit_getregion, &unit_setregion)
|
2004-03-19 01:14:41 +01:00
|
|
|
.property("is_familiar", &unit_isfamiliar)
|
2004-03-07 13:50:53 +01:00
|
|
|
.property("spells", &unit_spells, return_stl_iterator)
|
|
|
|
.property("familiarspells", &unit_familiarspells, return_stl_iterator)
|
2003-12-14 11:02:29 +01:00
|
|
|
.property("number", &unit_getnumber, &unit_setnumber)
|
2004-02-17 00:58:47 +01:00
|
|
|
.property("race", &unit_getrace, &unit_setrace)
|
2004-03-19 01:07:32 +01:00
|
|
|
.property("hp_max", &unit_hpmax)
|
2003-11-10 00:36:11 +01:00
|
|
|
];
|
|
|
|
}
|