lua bindings

This commit is contained in:
Enno Rehling 2003-12-14 10:02:29 +00:00
parent a8c63c8a7c
commit 601f562ad4
24 changed files with 366 additions and 108 deletions

View File

@ -24,7 +24,7 @@
AdditionalIncludeDirectories="../kernel,../util,../..,.."
PreprocessorDefinitions="_WINDOWS,WIN32,_DEBUG"
BasicRuntimeChecks="0"
RuntimeLibrary="5"
RuntimeLibrary="3"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="0"
PrecompiledHeaderThrough="stdafx.h"
@ -76,7 +76,7 @@
AdditionalIncludeDirectories="../kernel,../util,../..,.."
PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
StringPooling="TRUE"
RuntimeLibrary="4"
RuntimeLibrary="2"
EnableFunctionLevelLinking="TRUE"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="2"

View File

@ -78,7 +78,7 @@
extern const char *directions[];
extern const char *spelldata[];
extern int quiet;
extern boolean opt_cr_absolute_coords;
boolean opt_cr_absolute_coords = false;
/* globals */
#define C_REPORT_VERSION 64

View File

@ -1144,7 +1144,7 @@ maintain(building * b, boolean first)
boolean paid = true, work = first;
unit * u;
if (fval(b, BLD_MAINTAINED)) return true;
if (b->type->maintenance==NULL) return true;
if (b->type==NULL || b->type->maintenance==NULL) return true;
if (is_cursed(b->attribs, C_NOCOST, 0)) {
fset(b, BLD_MAINTAINED);
fset(b, BLD_WORKING);

View File

@ -24,7 +24,7 @@
AdditionalIncludeDirectories="../kernel,../util,../..,.."
PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
StringPooling="TRUE"
RuntimeLibrary="4"
RuntimeLibrary="2"
EnableFunctionLevelLinking="TRUE"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="2"
@ -128,7 +128,7 @@
AdditionalIncludeDirectories="../kernel,../util,../..,.."
PreprocessorDefinitions="_WINDOWS,WIN32"
BasicRuntimeChecks="0"
RuntimeLibrary="5"
RuntimeLibrary="3"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="0"
PrecompiledHeaderThrough="stdafx.h"

View File

@ -113,8 +113,8 @@ RemoveNMRNewbie(void) {
static void
restart(unit *u, const race * rc)
{
faction *f = addplayer(u->region, u->faction->email, u->faction->passw, rc, u->faction->locale, u->faction->subscription)->faction;
unit * nu = f->units;
faction *f = addfaction(u->faction->email, u->faction->passw, rc, u->faction->locale, u->faction->subscription);
unit * nu = addplayer(u->region, f);
strlist ** o=&u->orders;
f->subscription = u->faction->subscription;
fset(f, FFL_RESTART);

View File

@ -30,6 +30,7 @@
/* libc includes */
#include <assert.h>
#include <string.h>
const int FIREWORK_RANGE=10;

View File

@ -24,7 +24,7 @@
AdditionalIncludeDirectories="../kernel,../util,../..,.."
PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
StringPooling="TRUE"
RuntimeLibrary="4"
RuntimeLibrary="2"
EnableFunctionLevelLinking="TRUE"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="2"
@ -75,7 +75,7 @@
AdditionalIncludeDirectories="../kernel,../util,../..,.."
PreprocessorDefinitions="_WINDOWS,WIN32"
BasicRuntimeChecks="0"
RuntimeLibrary="5"
RuntimeLibrary="3"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="0"
PrecompiledHeaderThrough="stdafx.h"

View File

@ -86,6 +86,14 @@ const struct race * new_race[MAXRACES];
boolean sqlpatch = false;
int turn;
char *
strnzcpy(char * dst, const char *src, size_t len)
{
strncpy(dst, src, len);
dst[len]=0;
return dst;
}
static attrib_type at_creator = {
"creator"
/* Rest ist NULL; temporäres, nicht alterndes Attribut */
@ -2066,7 +2074,7 @@ kernel_done(void)
}
const char * localenames[] = {
"de", "en", "fr",
"de", "en",
NULL
};

View File

@ -30,6 +30,7 @@
/* libc includes */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const unit *
@ -86,49 +87,56 @@ unused_faction_id(void)
return id;
}
unit *
addplayer(region *r, const char *email, const char * password,
const struct race * frace, const struct locale *loc,
int subscription)
faction *
addfaction(const char *email, const char * password,
const struct race * frace, const struct locale *loc,
int subscription)
{
int i;
faction *f;
assert(frace != new_race[RC_ORC]);
f = calloc(sizeof(faction), 1);
set_string(&f->email, email);
if (password) {
set_string(&f->passw, password);
} else {
for (i = 0; i < 6; i++) buf[i] = (char) (97 + rand() % 26); buf[i] = 0;
set_string(&f->passw, buf);
}
for (i = 0; i < 6; i++) buf[i] = (char) (97 + rand() % 26); buf[i] = 0;
set_string(&f->override, buf);
f->lastorders = turn;
f->alive = 1;
f->age = 0;
f->race = frace;
f->magiegebiet = 0;
f->locale = loc;
f->subscription = subscription;
f->options = Pow(O_REPORT) | Pow(O_ZUGVORLAGE) | Pow(O_SILBERPOOL) | Pow(O_COMPUTER) | Pow(O_COMPRESS) | Pow(O_ADRESSEN) | Pow(O_STATISTICS);
f->no = unused_faction_id();
register_faction_id(f->no);
sprintf(buf, "%s %s", LOC(loc, "factiondefault"), factionid(f));
set_string(&f->name, buf);
fset(f, FL_UNNAMED);
addlist(&factions, f);
return f;
}
unit *
addplayer(region *r, faction * f)
{
int i;
unit *u;
faction *f;
assert(frace != new_race[RC_ORC]);
f = calloc(sizeof(faction), 1);
set_string(&f->email, email);
if (password) {
set_string(&f->passw, password);
} else {
for (i = 0; i < 6; i++) buf[i] = (char) (97 + rand() % 26); buf[i] = 0;
set_string(&f->passw, buf);
}
for (i = 0; i < 6; i++) buf[i] = (char) (97 + rand() % 26); buf[i] = 0;
set_string(&f->override, buf);
f->lastorders = turn;
f->alive = 1;
f->age = 0;
f->race = frace;
f->magiegebiet = 0;
f->locale = loc;
f->subscription = subscription;
set_ursprung(f, 0, r->x, r->y);
f->options = Pow(O_REPORT) | Pow(O_ZUGVORLAGE) | Pow(O_SILBERPOOL) | Pow(O_COMPUTER) | Pow(O_COMPRESS) | Pow(O_ADRESSEN) | Pow(O_STATISTICS);
f->no = unused_faction_id();
register_faction_id(f->no);
sprintf(buf, "%s %s", LOC(loc, "factiondefault"), factionid(f));
set_string(&f->name, buf);
fset(f, FL_UNNAMED);
addlist(&factions, f);
assert(f->units==NULL);
set_ursprung(f, 0, r->x, r->y);
u = createunit(r, f, 1, f->race);
give_starting_equipment(r, u);
fset(u, UFL_ISNEW);

View File

@ -106,9 +106,10 @@ typedef struct faction_list {
extern const struct unit * random_unit_in_faction(const struct faction *f);
extern const char * factionname(const struct faction * f);
extern void * resolve_faction(void * data);
extern struct unit * addplayer(struct region *r, const char *email,
const char* password, const struct race * frace,
const struct locale *loc, int subscription);
extern struct unit * addplayer(struct region *r, faction * f);
extern struct faction * addfaction(const char *email, const char* password,
const struct race * frace,
const struct locale *loc, int subscription);
extern boolean checkpasswd(const faction * f, const char * passwd, boolean shortp);
extern void destroyfaction(faction * f);

View File

@ -76,7 +76,7 @@
AdditionalIncludeDirectories="../util,../..,.."
PreprocessorDefinitions="_WINDOWS,WIN32"
BasicRuntimeChecks="0"
RuntimeLibrary="5"
RuntimeLibrary="3"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="0"
PrecompiledHeaderThrough="stdafx.h"
@ -128,7 +128,7 @@
AdditionalIncludeDirectories="../util,../..,.."
PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
StringPooling="TRUE"
RuntimeLibrary="4"
RuntimeLibrary="2"
EnableFunctionLevelLinking="TRUE"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="2"

View File

@ -1130,7 +1130,7 @@ spellpower(region * r, unit * u, spell * sp, int cast_level)
}
#ifdef MAGICPOWER
force = force * MAGICPOWER;
force = (int)(force * MAGICPOWER);
#endif
return max(force, 0);
@ -1498,7 +1498,7 @@ regeneration(unit * u)
aura = (rand() % d + rand() % d)/2 + 1;
#ifdef MAGICREGEN
aura = aura * MAGICREGEN;
aura = (int)(aura * MAGICREGEN);
#endif
return aura;

View File

@ -150,20 +150,16 @@
# define HAVE_SNPRINTF
/* MSVC has _access */
_CRTIMP int __cdecl _access(const char *, int);
# define access(f, m) _access(f, m)
# define HAVE_ACCESS
/* MSVC has _strdup */
_CRTIMP char * __cdecl _strdup(const char *);
# define strdup(s) _strdup(s)
# define HAVE_STRDUP
_CRTIMP int __cdecl _stricmp(const char *, const char *);
# define stricmp(a, b) _stricmp(a, b)
# define HAVE_STRICMP
_CRTIMP int __cdecl _strnicmp(const char *, const char *, size_t);
# define strnicmp(a, b, c) _strnicmp(a, b, c)
# define HAVE_STRNICMP
# undef HAVE_STRCASECMP
@ -236,6 +232,7 @@ extern char * strdup(const char *s);
# define true ((boolean)!false)
#endif
#define strnzcpy(dst, src, len) (strncpy(dst, src, len), len?dst[len]=0:0, dst)
/* this function must be implemented in a .o file */
extern char * strnzcpy(char * dst, const char *src, size_t len);
#endif

View File

@ -0,0 +1,32 @@
#include <config.h>
#include <eressea.h>
#include "list.h"
// kernel includes
#include <kernel/region.h>
#include <modules/alliance.h>
// lua includes
#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
using namespace luabind;
static eressea::list<alliance>
get_alliances(void) {
return eressea::list<alliance>(alliances);
}
void
bind_alliance(lua_State * L)
{
module(L)[
def("alliances", &get_alliances, return_stl_iterator),
def("get_alliance", &findalliance),
class_<struct alliance>("alliance")
.def_readonly("name", &alliance::name)
.def_readonly("id", &alliance::id)
];
}

View File

@ -1,5 +1,5 @@
#ifndef BINDINGS_H
#define BINDINGS_H
#ifndef LUA_BINDINGS_H
#define LUA_BINDINGS_H
struct lua_State;
@ -7,33 +7,8 @@ extern void bind_region(struct lua_State * L);
extern void bind_unit(struct lua_State * L);
extern void bind_ship(struct lua_State * L);
extern void bind_building(struct lua_State * L);
namespace eressea {
template<class T>
class list {
public:
class iterator {
public:
iterator(T * index) : m_index(index) {}
T * operator*() { return m_index; }
bool operator==(const iterator& iter) {
return iter.m_index==m_index;
}
iterator& operator++() {
if (m_index) m_index = m_index->next;
return *this;
}
private:
T * m_index;
};
typedef iterator const_iterator;
list<T>(T * clist) : m_clist(clist) {}
iterator begin() const { return iterator(m_clist); }
iterator end() const { return iterator(NULL); }
public:
T * m_clist;
};
};
extern void bind_faction(struct lua_State * L);
extern void bind_alliance(struct lua_State * L);
extern void bind_eressea(struct lua_State * L);
#endif

View File

@ -1,6 +1,6 @@
#include <config.h>
#include <cstring>
#include <eressea.h>
#include "bindings.h"
// kernel includes
#include <building.h>
@ -11,7 +11,6 @@
#include <luabind/iterator_policy.hpp>
using namespace luabind;
using namespace eressea;
void
bind_building(lua_State * L)

View File

@ -0,0 +1,49 @@
#include <config.h>
#include <cstring>
#include <eressea.h>
// kernel includes
#include <modules/alliance.h>
#include <kernel/faction.h>
#include <kernel/unit.h>
#include <kernel/region.h>
#include <util/language.h>
// lua includes
#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
using namespace luabind;
static faction *
add_faction(const char * email, const char * passwd, const char * racename, const char * lang)
{
const race * frace = findrace(racename, default_locale);
locale * loc = find_locale(lang);
faction * f = addfaction(email, passwd, frace, loc, 0);
return f;
}
static unit *
add_unit(faction * f, region * r)
{
if (f->units==NULL) return addplayer(r, f);
return createunit(r, f, 0, f->race);
}
static alliance *
add_alliance(int id, const char * name)
{
return makealliance(id, name);
}
void
bind_eressea(lua_State * L)
{
module(L)[
def("add_unit", &add_unit),
def("add_faction", &add_faction),
def("add_alliance", &add_alliance)
];
}

12
src/eressea/lua/export.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef LUA_EXPORT_H
#define LUA_EXPORT_H
struct lua_State;
extern void bind_region(struct lua_State * L);
extern void bind_unit(struct lua_State * L);
extern void bind_ship(struct lua_State * L);
extern void bind_building(struct lua_State * L);
extern void bind_faction(struct lua_State * L);
#endif

View File

@ -0,0 +1,61 @@
#include <config.h>
#include <eressea.h>
#include "list.h"
// kernel includes
#include <kernel/faction.h>
#include <kernel/unit.h>
#include <modules/alliance.h>
// lua includes
#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
using namespace luabind;
static eressea::list<faction>
get_factions(void) {
return eressea::list<faction>(factions);
}
class factionunit {
public:
static unit * next(unit * node) { return node->nextF; }
static unit * value(unit * node) { return node; }
};
static eressea::list<unit, unit, factionunit>
faction_units(const faction& f)
{
return eressea::list<unit, unit, factionunit>(f.units);
}
static void
faction_setalliance(faction& f, alliance * team)
{
if (f.alliance==0) setalliance(&f, team);
}
static alliance *
faction_getalliance(const faction& f)
{
return f.alliance;
}
void
bind_faction(lua_State * L)
{
module(L)[
def("factions", &get_factions, return_stl_iterator),
def("get_faction", &findfaction),
class_<struct faction>("faction")
.def_readonly("name", &faction::name)
.def_readonly("id", &faction::no)
.property("units", &faction_units, return_stl_iterator)
.property("alliance", &faction_getalliance, &faction_setalliance)
];
}

41
src/eressea/lua/list.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef LUA_LIST_H
#define LUA_LIST_H
namespace eressea {
template<class T, class N = T>
class listnode {
public:
static N * next(N * node) { return node->next; }
static T * value(N * node) { return node; }
};
template<class T, class N = T, class nodetype = listnode<T, N> >
class list {
public:
class iterator {
public:
iterator(N * index) : m_index(index) {}
T * operator*() { return nodetype::value(m_index); }
bool operator==(const iterator& iter) {
return iter.m_index==m_index;
}
iterator& operator++() {
if (m_index) m_index = nodetype::next(m_index);
return *this;
}
private:
N * m_index;
};
typedef iterator const_iterator;
list<T, N, nodetype>(N * clist) : m_clist(clist) {}
iterator begin() const { return iterator(m_clist); }
iterator end() const { return iterator(NULL); }
public:
N * m_clist;
};
};
#endif

View File

@ -1,6 +1,6 @@
#include <config.h>
#include <eressea.h>
#include "bindings.h"
#include "list.h"
// kernel includes
#include <region.h>
@ -14,7 +14,6 @@
#include <luabind/iterator_policy.hpp>
using namespace luabind;
using namespace eressea;
static eressea::list<region>
get_regions(void) {
@ -44,25 +43,24 @@ region_setname(region& r, const char * name) {
static const char *
region_getname(const region& r) {
if (r.land) return r.land->name;
return r.terrain->name;
return terrain[r.terrain].name;
}
static void
region_setinfo(region& r, const char * info) {
if (r.land) set_string(&r.land->display, info);
set_string(&r.display, info);
}
static const char *
region_getinfo(const region& r) {
if (r.land) return r.land->display;
return NULL;
return r.display;
}
void
bind_region(lua_State * L)
{
module(L)[
def("get_regions", &get_regions, return_stl_iterator),
def("regions", &get_regions, return_stl_iterator),
def("get_region", &findregion),
class_<struct region>("region")

View File

@ -1,6 +1,5 @@
#include <config.h>
#include <eressea.h>
#include "bindings.h"
// kernel includes
#include <ship.h>
@ -12,7 +11,6 @@
#include <luabind/iterator_policy.hpp>
using namespace luabind;
using namespace eressea;
void
bind_ship(lua_State * L)

View File

@ -1,10 +1,12 @@
#include <config.h>
#include <eressea.h>
#include "bindings.h"
// kernel includes
#include <region.h>
#include <unit.h>
#include <kernel/region.h>
#include <kernel/item.h>
#include <kernel/faction.h>
#include <kernel/skill.h>
#include <kernel/unit.h>
// lua includes
#include <lua.hpp>
@ -12,7 +14,71 @@
#include <luabind/iterator_policy.hpp>
using namespace luabind;
using namespace eressea;
static void
unit_setnumber(unit& u, int number)
{
if (u.number==0) set_number(&u, number);
}
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);
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;
}
void
bind_unit(lua_State * L)
@ -23,6 +89,13 @@ bind_unit(lua_State * L)
class_<struct unit>("unit")
.def_readonly("name", &unit::name)
.def_readonly("region", &unit::region)
.def_readonly("faction", &unit::faction)
.def_readonly("id", &unit::no)
.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)
.property("number", &unit_getnumber, &unit_setnumber)
];
}

5
src/lua.hpp Normal file
View File

@ -0,0 +1,5 @@
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}