diff --git a/src/combined/gamecode.c b/src/combined/gamecode.c index 0ace5a0f0..614ba9002 100644 --- a/src/combined/gamecode.c +++ b/src/combined/gamecode.c @@ -5,8 +5,13 @@ #ifdef BINDINGS_TOLUA #include "eressea/tolua/bindings.c" #include "eressea/tolua/bind_unit.c" +#include "eressea/tolua/bind_ship.c" +#include "eressea/tolua/bind_building.c" +#include "eressea/tolua/bind_region.c" #include "eressea/tolua/bind_faction.c" #include "eressea/tolua/bind_message.c" +#include "eressea/tolua/bind_hashtable.c" +#include "eressea/tolua/bind_gmtool.c" #include "eressea/tolua/helpers.c" #endif diff --git a/src/common/attributes/object.c b/src/common/attributes/object.c index 9ab115bfe..d3c977f8e 100644 --- a/src/common/attributes/object.c +++ b/src/common/attributes/object.c @@ -32,6 +32,7 @@ /* stdc includes */ #include +#include typedef struct object_data { object_type type; diff --git a/src/common/gamecode/economy.c b/src/common/gamecode/economy.c index 6d404e15e..b81a6acc5 100644 --- a/src/common/gamecode/economy.c +++ b/src/common/gamecode/economy.c @@ -60,6 +60,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/src/common/gamecode/give.c b/src/common/gamecode/give.c index c965bd03e..9620c19dc 100644 --- a/src/common/gamecode/give.c +++ b/src/common/gamecode/give.c @@ -42,6 +42,7 @@ #include /* libc includes */ +#include #include #include diff --git a/src/common/gamecode/items.c b/src/common/gamecode/items.c index a7fa35e4b..db1ea09f8 100644 --- a/src/common/gamecode/items.c +++ b/src/common/gamecode/items.c @@ -26,6 +26,7 @@ #include #include +#include #include /* BEGIN studypotion */ diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index 2131141e1..31a90176a 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include @@ -92,11 +93,11 @@ #endif /* libc includes */ +#include #include #include #include #include -#include #include #include diff --git a/src/common/gamecode/monster.c b/src/common/gamecode/monster.c index 32fedc206..e05a1798c 100644 --- a/src/common/gamecode/monster.c +++ b/src/common/gamecode/monster.c @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/gamecode/randenc.c b/src/common/gamecode/randenc.c index d6bf1b1fa..e406301dd 100644 --- a/src/common/gamecode/randenc.c +++ b/src/common/gamecode/randenc.c @@ -57,6 +57,7 @@ /* util includes */ #include +#include #include #include #include diff --git a/src/common/gamecode/report.c b/src/common/gamecode/report.c index 5489e8afa..ec1c10734 100644 --- a/src/common/gamecode/report.c +++ b/src/common/gamecode/report.c @@ -71,6 +71,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/gamecode/spells.c b/src/common/gamecode/spells.c index a58b45a4c..ecb289361 100644 --- a/src/common/gamecode/spells.c +++ b/src/common/gamecode/spells.c @@ -25,6 +25,7 @@ #include +#include /* ------------------------------------------------------------- */ /* Name: Plappermaul diff --git a/src/common/gamecode/study.c b/src/common/gamecode/study.c index 216ff00d5..8e76f7878 100644 --- a/src/common/gamecode/study.c +++ b/src/common/gamecode/study.c @@ -46,6 +46,7 @@ /* util includes */ #include #include +#include #include #include #include diff --git a/src/common/kernel/alliance.c b/src/common/kernel/alliance.c index f80a8a802..f1ea5ee68 100644 --- a/src/common/kernel/alliance.c +++ b/src/common/kernel/alliance.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include diff --git a/src/common/kernel/battle.c b/src/common/kernel/battle.c index 50ef46cd1..6c9841a09 100644 --- a/src/common/kernel/battle.c +++ b/src/common/kernel/battle.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/border.c b/src/common/kernel/border.c index 71c5d7fca..cb94951d1 100644 --- a/src/common/kernel/border.c +++ b/src/common/kernel/border.c @@ -25,6 +25,7 @@ #include /* for backward compat reading of bt_firewall */ #include +#include #include #include #include diff --git a/src/common/kernel/build.c b/src/common/kernel/build.c index e2af2ccc5..b539f233d 100644 --- a/src/common/kernel/build.c +++ b/src/common/kernel/build.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/building.c b/src/common/kernel/building.c index f5495e7bd..7fe87141b 100644 --- a/src/common/kernel/building.c +++ b/src/common/kernel/building.c @@ -610,3 +610,16 @@ void building_setname(building * self, const char * name) if (name) self->name = strdup(name); else self->name = NULL; } + +void +building_addaction(building * b, const char * fname, const char * param) +{ + attrib * a = a_add(&b->attribs, a_new(&at_building_action)); + building_action * data = (building_action*)a->data.v; + data->b = b; + data->fname = strdup(fname); + if (param) { + data->param = strdup(param); + } +} + diff --git a/src/common/kernel/building.h b/src/common/kernel/building.h index 358f349fe..2edcf5f39 100644 --- a/src/common/kernel/building.h +++ b/src/common/kernel/building.h @@ -141,6 +141,7 @@ extern struct building *findbuilding(int n); extern struct unit * buildingowner(const struct region * r, const struct building * b); extern struct attrib_type at_building_action; +void building_addaction(struct building * b, const char * fname, const char * param); #ifdef WDW_PYRAMID extern int wdw_pyramid_level(const struct building *b); diff --git a/src/common/kernel/curse.c b/src/common/kernel/curse.c index 921c47727..bb275183e 100644 --- a/src/common/kernel/curse.c +++ b/src/common/kernel/curse.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/eressea.c b/src/common/kernel/eressea.c index 78bd13f9b..78029b3ae 100644 --- a/src/common/kernel/eressea.c +++ b/src/common/kernel/eressea.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/faction.c b/src/common/kernel/faction.c index 75db1ec34..e2227c9ca 100644 --- a/src/common/kernel/faction.c +++ b/src/common/kernel/faction.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ #include /* libc includes */ +#include #include #include #include @@ -408,7 +410,7 @@ update_interval(struct faction * f, struct region * r) const char * faction_getname(const faction * self) { - return self->name; + return self->name?self->name:""; } void faction_setname(faction * self, const char * name) @@ -419,7 +421,7 @@ void faction_setname(faction * self, const char * name) const char * faction_getemail(const faction * self) { - return self->email; + return self->email?self->email:""; } void faction_setemail(faction * self, const char * email) @@ -430,7 +432,7 @@ void faction_setemail(faction * self, const char * email) const char * faction_getbanner(const faction * self) { - return self->banner; + return self->banner?self->banner:""; } void faction_setbanner(faction * self, const char * banner) diff --git a/src/common/kernel/item.c b/src/common/kernel/item.c index d43d424c3..7d8cc2bed 100644 --- a/src/common/kernel/item.c +++ b/src/common/kernel/item.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/magic.c b/src/common/kernel/magic.c index fd4a3ba8c..15c410af9 100644 --- a/src/common/kernel/magic.c +++ b/src/common/kernel/magic.c @@ -52,6 +52,7 @@ /* util includes */ #include +#include #include #include #include diff --git a/src/common/kernel/message.c b/src/common/kernel/message.c index 422d27968..77bb3cb75 100644 --- a/src/common/kernel/message.c +++ b/src/common/kernel/message.c @@ -34,6 +34,7 @@ /* util includes */ #include #include +#include #include #include #include diff --git a/src/common/kernel/names.c b/src/common/kernel/names.c index b1d6e0243..bd846db10 100644 --- a/src/common/kernel/names.c +++ b/src/common/kernel/names.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include diff --git a/src/common/kernel/pathfinder.c b/src/common/kernel/pathfinder.c index 838a2b959..53a134561 100644 --- a/src/common/kernel/pathfinder.c +++ b/src/common/kernel/pathfinder.c @@ -28,6 +28,7 @@ #include #include +#include boolean allowed_swim(const region * src, const region * r) diff --git a/src/common/kernel/race.c b/src/common/kernel/race.c index 338fe92e5..80411b928 100644 --- a/src/common/kernel/race.c +++ b/src/common/kernel/race.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/region.h b/src/common/kernel/region.h index 0e0bd4ed2..df97c526f 100644 --- a/src/common/kernel/region.h +++ b/src/common/kernel/region.h @@ -18,8 +18,7 @@ extern "C" { #endif -#include -#include +#include "types.h" /* FAST_CONNECT: regions are directly connected to neighbours, saves doing a hash-access each time a neighbour is needed */ diff --git a/src/common/kernel/reports.c b/src/common/kernel/reports.c index ee58a1445..d0c2f63d5 100644 --- a/src/common/kernel/reports.c +++ b/src/common/kernel/reports.c @@ -50,6 +50,7 @@ #include #include #include +#include #include #include @@ -1902,6 +1903,7 @@ eval_order(struct opstack ** stack, const void * userdata) /* order -> string */ size_t len; variant var; + unused(report); write_order(ord, buf, sizeof(buf)); len = strlen(buf); var.v = strcpy(balloc(len+1), buf); diff --git a/src/common/kernel/save.c b/src/common/kernel/save.c index 07edf9268..fffee1c6a 100644 --- a/src/common/kernel/save.c +++ b/src/common/kernel/save.c @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/kernel/ship.c b/src/common/kernel/ship.c index 1c60e5ff6..2206568af 100644 --- a/src/common/kernel/ship.c +++ b/src/common/kernel/ship.c @@ -34,6 +34,7 @@ #include /* libc includes */ +#include #include #include diff --git a/src/common/kernel/ship.h b/src/common/kernel/ship.h index 1baf3a2db..099f19c9b 100644 --- a/src/common/kernel/ship.h +++ b/src/common/kernel/ship.h @@ -18,6 +18,8 @@ extern "C" { #endif +#include "types.h" + #define DAMAGE_SCALE 100 /* multiplier for sh->damage */ /* ship_type::flags */ diff --git a/src/common/kernel/skill.c b/src/common/kernel/skill.c index fc6d9770e..9ca546e97 100644 --- a/src/common/kernel/skill.c +++ b/src/common/kernel/skill.c @@ -35,6 +35,7 @@ #include #include +#include #include #include diff --git a/src/common/kernel/unit.c b/src/common/kernel/unit.c index f9f749ebb..0b39771d8 100644 --- a/src/common/kernel/unit.c +++ b/src/common/kernel/unit.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include diff --git a/src/common/modules/autoseed.c b/src/common/modules/autoseed.c index 3acd815b3..46076202e 100644 --- a/src/common/modules/autoseed.c +++ b/src/common/modules/autoseed.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -45,6 +46,7 @@ #include #include #include +#include const terrain_type * random_terrain(boolean distribution) diff --git a/src/common/modules/autoseed.h b/src/common/modules/autoseed.h index f16cd0560..b39e1fcbe 100644 --- a/src/common/modules/autoseed.h +++ b/src/common/modules/autoseed.h @@ -32,6 +32,9 @@ typedef struct newfaction { struct alliance * allies; } newfaction; +#define ISLANDSIZE 20 +#define TURNS_PER_ISLAND 5 + extern int autoseed(newfaction ** players, int nsize, int max_agediff); extern newfaction * read_newfactions(const char * filename); extern void get_island(struct region * root, struct region_list ** rlist); diff --git a/src/common/modules/gmcmd.c b/src/common/modules/gmcmd.c index 89eb551c5..9b75abef7 100644 --- a/src/common/modules/gmcmd.c +++ b/src/common/modules/gmcmd.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,7 @@ #include #include #include +#include /** ** at_permissions diff --git a/src/common/modules/museum.c b/src/common/modules/museum.c index ce2f4c421..6607e051b 100644 --- a/src/common/modules/museum.c +++ b/src/common/modules/museum.c @@ -48,6 +48,7 @@ /* libc includes */ #include #include +#include #define PFL_MUSEUM PFL_NOMONSTERS | PFL_NORECRUITS | PFL_NOGIVE | PFL_NOATTACK | PFL_NOTERRAIN | PFL_NOMAGIC | PFL_NOSTEALTH | PFL_NOTEACH | PFL_NOBUILD | PFL_NOFEED diff --git a/src/common/modules/score.c b/src/common/modules/score.c index 0a6d0392a..7ac3eb528 100644 --- a/src/common/modules/score.c +++ b/src/common/modules/score.c @@ -39,6 +39,7 @@ /* util includes */ #include +#include /* libc includes */ #include diff --git a/src/common/modules/xecmd.c b/src/common/modules/xecmd.c index 2a6178e6a..3d19e70f3 100644 --- a/src/common/modules/xecmd.c +++ b/src/common/modules/xecmd.c @@ -38,6 +38,7 @@ /* libc includes */ #include #include +#include static void xe_givelaen(unit *u, struct order * ord) diff --git a/src/common/spells/alp.c b/src/common/spells/alp.c index 9bbfc3ba3..7f74c1c7b 100644 --- a/src/common/spells/alp.c +++ b/src/common/spells/alp.c @@ -39,6 +39,7 @@ /* libc includes */ #include #include +#include extern const char *directions[]; diff --git a/src/common/spells/spells.c b/src/common/spells/spells.c index c58623c96..823796161 100644 --- a/src/common/spells/spells.c +++ b/src/common/spells/spells.c @@ -57,10 +57,11 @@ /* util includes */ #include #include -#include +#include +#include #include #include -#include +#include #include #include #include diff --git a/src/common/triggers/unitmessage.c b/src/common/triggers/unitmessage.c index 9291203fb..3e8fd397b 100644 --- a/src/common/triggers/unitmessage.c +++ b/src/common/triggers/unitmessage.c @@ -14,6 +14,7 @@ without prior permission by the authors of Eressea. /* kernel includes */ #include #include +#include /* util includes */ #include diff --git a/src/eressea.vcproj b/src/eressea.vcproj index d0f350b5c..7f04a3412 100644 --- a/src/eressea.vcproj +++ b/src/eressea.vcproj @@ -66,7 +66,7 @@ /> #include -#define ISLANDSIZE 20 -#define TURNS_PER_ISLAND 4 void seed_players(const char * filename, boolean new_island) { diff --git a/src/eressea/eressea-lua.vcproj b/src/eressea/eressea-lua.vcproj index 0e95b1d97..49dc365ed 100644 --- a/src/eressea/eressea-lua.vcproj +++ b/src/eressea/eressea-lua.vcproj @@ -275,6 +275,14 @@ + + + + @@ -283,6 +291,22 @@ RelativePath=".\tolua\bind_faction.h" > + + + + + + + + @@ -291,10 +315,30 @@ RelativePath=".\tolua\bind_message.h" > + + + + + + + + + + diff --git a/src/eressea/gmtool.c b/src/eressea/gmtool.c index 929d46e92..0b6d04180 100644 --- a/src/eressea/gmtool.c +++ b/src/eressea/gmtool.c @@ -62,6 +62,7 @@ #include +#include #include #include @@ -1068,6 +1069,30 @@ update_view(view * vi) } } +state * +state_open(void) +{ + state * st = calloc(sizeof(state), 1); + st->display.plane = 0; + st->cursor.p = 0; + st->cursor.x = 0; + st->cursor.y = 0; + st->selected = calloc(1, sizeof(struct selection)); + st->modified = 0; + st->info_flags = 0xFFFFFFFF; + st->prev = current_state; + current_state = st; + return st; +} + +void +state_close(state * st) +{ + assert(st==current_state); + current_state = st->prev; + free(st); +} + void run_mapper(void) { @@ -1076,7 +1101,7 @@ run_mapper(void) WINDOW * hwinmap; int width, height, x, y; int split = 20, old_flags = log_flags; - state st; + state * st; point tl; log_flags &= ~(LOG_CPERROR|LOG_CPWARNING); @@ -1093,70 +1118,63 @@ run_mapper(void) hwininfo = subwin(stdscr, getmaxy(stdscr)-1, split, y, x+getmaxx(stdscr)-split); hwinstatus = subwin(stdscr, 1, width, height-1, x); - st.wnd_map = win_create(hwinmap); - st.wnd_map->paint = &paint_map; - st.wnd_map->update = 1; - st.wnd_info = win_create(hwininfo); - st.wnd_info->paint = &paint_info_region; - st.wnd_info->handlekey = &handle_info_region; - st.wnd_info->update = 1; - st.wnd_status = win_create(hwinstatus); - st.wnd_status->paint = &paint_status; - st.wnd_status->update = 1; - st.display.plane = 0; - st.cursor.p = 0; - st.cursor.x = 0; - st.cursor.y = 0; - st.selected = calloc(1, sizeof(struct selection)); - st.modified = 0; - st.info_flags = 0xFFFFFFFF; + st = state_open(); + st->wnd_map = win_create(hwinmap); + st->wnd_map->paint = &paint_map; + st->wnd_map->update = 1; + st->wnd_info = win_create(hwininfo); + st->wnd_info->paint = &paint_info_region; + st->wnd_info->handlekey = &handle_info_region; + st->wnd_info->update = 1; + st->wnd_status = win_create(hwinstatus); + st->wnd_status->paint = &paint_status; + st->wnd_status->update = 1; - init_view(&st.display, hwinmap); - coor2point(&st.display.topleft, &tl); + init_view(&st->display, hwinmap); + coor2point(&st->display.topleft, &tl); - hstatus = st.wnd_status->handle; /* the lua console needs this */ - current_state = &st; + hstatus = st->wnd_status->handle; /* the lua console needs this */ while (!g_quit) { int c; point p; window * wnd; - view * vi = &st.display; + view * vi = &st->display; getbegyx(hwinmap, x, y); width = getmaxx(hwinmap)-x; height = getmaxy(hwinmap)-y; - coor2point(&st.cursor, &p); + coor2point(&st->cursor, &p); - if (st.cursor.p != vi->plane) { - vi->plane = st.cursor.p; - st.wnd_map->update |= 1; + if (st->cursor.p != vi->plane) { + vi->plane = st->cursor.p; + st->wnd_map->update |= 1; } if (p.y < tl.y) { - vi->topleft.y = st.cursor.y-vi->size.height/2; - st.wnd_map->update |= 1; + vi->topleft.y = st->cursor.y-vi->size.height/2; + st->wnd_map->update |= 1; } else if (p.y >= tl.y + vi->size.height * THEIGHT) { - vi->topleft.y = st.cursor.y-vi->size.height/2; - st.wnd_map->update |= 1; + vi->topleft.y = st->cursor.y-vi->size.height/2; + st->wnd_map->update |= 1; } if (p.x <= tl.x) { - vi->topleft.x = st.cursor.x+(st.cursor.y-vi->topleft.y)/2-vi->size.width / 2; - st.wnd_map->update |= 1; + vi->topleft.x = st->cursor.x+(st->cursor.y-vi->topleft.y)/2-vi->size.width / 2; + st->wnd_map->update |= 1; } else if (p.x >= tl.x + vi->size.width * TWIDTH-1) { - vi->topleft.x = st.cursor.x+(st.cursor.y-vi->topleft.y)/2-vi->size.width / 2; - st.wnd_map->update |= 1; + vi->topleft.x = st->cursor.x+(st->cursor.y-vi->topleft.y)/2-vi->size.width / 2; + st->wnd_map->update |= 1; } - if (st.wnd_map->update) { + if (st->wnd_map->update) { update_view(vi); coor2point(&vi->topleft, &tl); } for (wnd=wnd_last;wnd!=NULL;wnd=wnd->prev) { if (wnd->update && wnd->paint) { if (wnd->update & 1) { - wnd->paint(wnd, &st); + wnd->paint(wnd, st); wnoutrefresh(wnd->handle); } if (wnd->update & 2) { @@ -1165,18 +1183,18 @@ run_mapper(void) wnd->update = 0; } } - draw_cursor(st.wnd_map->handle, st.selected, vi, &st.cursor, 1); + draw_cursor(st->wnd_map->handle, st->selected, vi, &st->cursor, 1); doupdate(); c = getch(); - draw_cursor(st.wnd_map->handle, st.selected, vi, &st.cursor, 0); - handlekey(&st, c); + draw_cursor(st->wnd_map->handle, st->selected, vi, &st->cursor, 0); + handlekey(st, c); } g_quit = 0; set_readline(NULL); curs_set(1); endwin(); log_flags = old_flags; - current_state = NULL; + state_close(st); } #define MAXINPUT 512 diff --git a/src/eressea/gmtool.h b/src/eressea/gmtool.h index bd71850d4..4864a1230 100644 --- a/src/eressea/gmtool.h +++ b/src/eressea/gmtool.h @@ -16,6 +16,8 @@ extern "C" { #endif struct lua_State; struct selection; + struct state; + extern int gmmain(int argc, char *argv[]); extern int curses_readline(struct lua_State * L, const char * prompt); @@ -25,6 +27,9 @@ extern "C" { extern int force_color; + struct state * state_open(); + void state_close(struct state * ); + #ifdef __cplusplus } #endif diff --git a/src/eressea/gmtool_structs.h b/src/eressea/gmtool_structs.h index 7d4e9859a..f475bb46d 100644 --- a/src/eressea/gmtool_structs.h +++ b/src/eressea/gmtool_structs.h @@ -59,8 +59,7 @@ typedef struct selection { typedef struct state { coordinate cursor; selection * selected; - struct state * undo; - struct faction * topf; + struct state * prev; view display; int modified; unsigned int info_flags; diff --git a/src/eressea/korrektur.c b/src/eressea/korrektur.c index 0bb20e533..7dc62b0c9 100644 --- a/src/eressea/korrektur.c +++ b/src/eressea/korrektur.c @@ -82,6 +82,7 @@ #include #include #include +#include #undef XMAS1999 #undef XMAS2000 diff --git a/src/eressea/lua/building.cpp b/src/eressea/lua/building.cpp index 717f20f14..49543e2d7 100644 --- a/src/eressea/lua/building.cpp +++ b/src/eressea/lua/building.cpp @@ -72,18 +72,6 @@ lc_age(struct attrib * a) return (retval!=0)?AT_AGE_KEEP:AT_AGE_REMOVE; } -static int -building_addaction(building * b, const char * fname, const char * param) -{ - attrib * a = a_add(&b->attribs, a_new(&at_building_action)); - building_action * data = (building_action*)a->data.v; - data->b = b; - data->fname = strdup(fname); - if (param) data->param = strdup(param); - - return 0; -} - static const char * building_getinfo(const building * b) { @@ -97,19 +85,6 @@ building_setinfo(building * b, const char * info) b->display = strdup(info); } -static const char * -buildingname(const building * b) -{ - return (const char *)b->name; -} - -static void -building_setname(building * b, const char * name) -{ - free(b->name); - b->name = strdup(name); -} - static region * building_getregion(const building * b) { @@ -187,7 +162,7 @@ bind_building(lua_State * L) class_("building") .def(self == building()) .def(tostring(self)) - .property("name", &buildingname, &building_setname) + .property("name", &building_getname, &building_setname) .property("info", &building_getinfo, &building_setinfo) .property("units", &building_units, return_stl_iterator) .property("region", &building_getregion, &building_setregion) diff --git a/src/eressea/lua/eressea.cpp b/src/eressea/lua/eressea.cpp index aa6ba848d..166ebb732 100644 --- a/src/eressea/lua/eressea.cpp +++ b/src/eressea/lua/eressea.cpp @@ -21,6 +21,8 @@ #include #include +#include + // lua includes #ifdef _MSC_VER #pragma warning (push) @@ -202,6 +204,7 @@ bind_eressea(lua_State * L) { module(L)[ def("atoi36", &atoi36), + def("rng_int", &rng_int), def("itoa36", &itoa36), def("dice_roll", &dice_rand), def("equipment_setitem", &lua_addequipment), diff --git a/src/eressea/lua/gm.cpp b/src/eressea/lua/gm.cpp index 901e92851..cd19d6fa3 100644 --- a/src/eressea/lua/gm.cpp +++ b/src/eressea/lua/gm.cpp @@ -82,8 +82,8 @@ bind_gmtool(lua_State * L) { module(L, "gmtool")[ def("editor", &run_mapper), - def("selection", &selected_regions, return_stl_iterator), - def("cursor", ¤t_region), + def("get_selection", &selected_regions, return_stl_iterator), + def("get_cursor", ¤t_region), def("highlight", &highlight_region), def("select", &gmtool_select_region), def("select_at", &gmtool_select_coordinate) diff --git a/src/eressea/lua/objects.cpp b/src/eressea/lua/objects.cpp index 413a45b85..77b63066f 100644 --- a/src/eressea/lua/objects.cpp +++ b/src/eressea/lua/objects.cpp @@ -27,91 +27,91 @@ using namespace luabind; namespace eressea { -object objects::get(const char * name) { - lua_State * L = (lua_State *) global.vm_state; - attrib * a = a_find(*mAttribPtr, &at_object); - for (; a && a->type == &at_object; a = a->next) { - if (strcmp(object_name(a), name) == 0) { - variant val; - object_type type; + object objects::get(const char * name) { + lua_State * L = (lua_State *) global.vm_state; + attrib * a = a_find(*mAttribPtr, &at_object); + for (; a && a->type == &at_object; a = a->next) { + if (strcmp(object_name(a), name) == 0) { + variant val; + object_type type; - object_get(a, &type, &val); - switch (type) { - case TNONE: - break; - case TINTEGER: - return object(L, val.i); - case TREAL: - return object(L, val.f); - case TREGION: - return object(L, (region*) val.v); - case TBUILDING: - return object(L, (building*) val.v); - case TUNIT: - return object(L, (unit*) val.v); - case TSHIP: - return object(L, (ship*) val.v); - case TSTRING: - return object(L, std::string((const char*) val.v)); - default: - assert(!"not implemented"); - } - } - } - return object(); // nil -} + object_get(a, &type, &val); + switch (type) { + case TNONE: + break; + case TINTEGER: + return object(L, val.i); + case TREAL: + return object(L, val.f); + case TREGION: + return object(L, (region*) val.v); + case TBUILDING: + return object(L, (building*) val.v); + case TUNIT: + return object(L, (unit*) val.v); + case TSHIP: + return object(L, (ship*) val.v); + case TSTRING: + return object(L, std::string((const char*) val.v)); + default: + assert(!"not implemented"); + } + } + } + return object(); // nil + } -static void set_object(attrib **attribs, const char * name, object_type type, - variant val) { - attrib * a = a_find(*attribs, &at_object); - for (; a && a->type == &at_object; a = a->next) { - if (strcmp(object_name(a), name) == 0) { - object_set(a, type, val); - return; - } - } - a = a_add(attribs, object_create(name, type, val)); -} + static void set_object(attrib **attribs, const char * name, object_type type, + variant val) { + attrib * a = a_find(*attribs, &at_object); + for (; a && a->type == &at_object; a = a->next) { + if (strcmp(object_name(a), name) == 0) { + object_set(a, type, val); + return; + } + } + a = a_add(attribs, object_create(name, type, val)); + } -template<> void objects::set(const char * name, int value) { - variant val = { 0 }; - val.i = value; - set_object(mAttribPtr, name, TINTEGER, val); -} + template<> void objects::set(const char * name, int value) { + variant val = { 0 }; + val.i = value; + set_object(mAttribPtr, name, TINTEGER, val); + } -template<> void objects::set(const char * name, double value) { - variant val = { 0 }; - val.f = (float) value; - set_object(mAttribPtr, name, TREAL, val); -} + template<> void objects::set(const char * name, double value) { + variant val = { 0 }; + val.f = (float) value; + set_object(mAttribPtr, name, TREAL, val); + } -template<> void objects::set(const char * name, - const char * value) { - variant val = { 0 }; - val.v = strdup(value); - set_object(mAttribPtr, name, TSTRING, val); -} + template<> void objects::set(const char * name, + const char * value) { + variant val = { 0 }; + val.v = strdup(value); + set_object(mAttribPtr, name, TSTRING, val); + } -template void objects::set(const char * name, V value) { - variant val = { 0 }; - val.v = &value; - set_object(mAttribPtr, name, T, val); -} + template void objects::set(const char * name, V value) { + variant val = { 0 }; + val.v = &value; + set_object(mAttribPtr, name, T, val); + } } void bind_objects(struct lua_State * L) { - using namespace eressea; + using namespace eressea; - module(L)[class_ ("objects") .def("get", &objects::get) .def( - "set", - (void(objects::*)(const char*, region&))&objects::set) - .def("set", (void(objects::*)(const char*, unit&))&objects::set) - .def("set", (void(objects::*)(const char*, faction&))&objects::set) - .def("set", (void(objects::*)(const char*, building&))&objects::set) - .def("set", (void(objects::*)(const char*, ship&))&objects::set) - // POD: - // .def("set", (void(objects::*)(const char*, int))&objects::set) - .def("set", (void(objects::*)(const char*, double))&objects::set) - .def("set", (void(objects::*)(const char*, const char *))&objects::set) - ]; - } + module(L)[class_ ("objects") .def("get", &objects::get) .def( + "set", + (void(objects::*)(const char*, region&))&objects::set) + .def("set", (void(objects::*)(const char*, unit&))&objects::set) + .def("set", (void(objects::*)(const char*, faction&))&objects::set) + .def("set", (void(objects::*)(const char*, building&))&objects::set) + .def("set", (void(objects::*)(const char*, ship&))&objects::set) + // POD: + // .def("set", (void(objects::*)(const char*, int))&objects::set) + .def("set", (void(objects::*)(const char*, double))&objects::set) + .def("set", (void(objects::*)(const char*, const char *))&objects::set) + ]; +} diff --git a/src/eressea/lua/region.cpp b/src/eressea/lua/region.cpp index cc7444c09..aea0e8758 100644 --- a/src/eressea/lua/region.cpp +++ b/src/eressea/lua/region.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -138,19 +139,11 @@ region_setflag(region * r, int bit, bool set) } static int -region_getresource(const region * r, const char * type) +region_get_resource(const region * r, const char * type) { const resource_type * rtype = rt_find(type); if (rtype!=NULL) { - const rawmaterial * rm; - for (rm=r->resources;rm;rm=rm->next) { - if (rm->type->rtype==rtype) { - return rm->amount; - } - } - if (rtype==rt_find("money")) return rmoney(r); - if (rtype==rt_find("horse")) return rhorses(r); - if (rtype==rt_find("peasant")) return rpeasants(r); + return region_getresource(r, rtype); } else { if (strcmp(type, "seed")==0) return rtrees(r, 0); if (strcmp(type, "sapling")==0) return rtrees(r, 1); @@ -162,23 +155,11 @@ region_getresource(const region * r, const char * type) } static void -region_setresource(region * r, const char * type, int value) +region_set_resource(region * r, const char * type, int value) { const resource_type * rtype = rt_find(type); if (rtype!=NULL) { - rawmaterial * rm = r->resources; - while (rm) { - if (rm->type->rtype==rtype) { - rm->amount = value; - break; - } - rm=rm->next; - } - if (!rm) { - if (rtype==rt_find("money")) rsetmoney(r, value); - else if (rtype==rt_find("peasant")) rsetpeasants(r, value); - else if (rtype==rt_find("horse")) rsethorses(r, value); - } + region_setresource(r, rtype, value); } else { if (strcmp(type, "seed")==0) { rsettrees(r, 0, value); @@ -383,8 +364,8 @@ bind_region(lua_State * L) .def("set_road", ®ion_setroad) .def("next", ®ion_next) - .def("get_resource", ®ion_getresource) - .def("set_resource", ®ion_setresource) + .def("get_resource", ®ion_get_resource) + .def("set_resource", ®ion_set_resource) .def_readonly("x", ®ion::x) .def_readonly("y", ®ion::y) .def_readonly("id", ®ion::uid) diff --git a/src/eressea/lua/test.cpp b/src/eressea/lua/test.cpp index 35e894092..05d51e7b9 100644 --- a/src/eressea/lua/test.cpp +++ b/src/eressea/lua/test.cpp @@ -19,7 +19,6 @@ using namespace luabind; #include -#include #include #include #include @@ -66,7 +65,6 @@ bind_test(lua_State * L) def("loc_skill", &loc_getskill), def("loc_keyword", &loc_getkeyword), def("reorder_units", &reorder_units), - def("adamantium_island", &adamantium_island), - def("rng_int", &rng_int) + def("adamantium_island", &adamantium_island) ]; } diff --git a/src/eressea/server.cpp b/src/eressea/server.cpp index 8ed3a8097..ee432693b 100644 --- a/src/eressea/server.cpp +++ b/src/eressea/server.cpp @@ -83,6 +83,7 @@ /* util includes */ #include +#include #include #include #include @@ -103,6 +104,11 @@ #include "tolua/bind_unit.h" #include "tolua/bind_faction.h" #include "tolua/bind_message.h" +#include "tolua/bind_hashtable.h" +#include "tolua/bind_ship.h" +#include "tolua/bind_building.h" +#include "tolua/bind_region.h" +#include "tolua/bind_gmtool.h" #endif // BINDINGS_TOLUA #ifdef BINDINGS_LUABIND @@ -130,6 +136,7 @@ /* libc includes */ #include +#include #include #include #include @@ -290,9 +297,14 @@ lua_init(void) register_tolua_helpers(); tolua_eressea_open(L); tolua_unit_open(L); + tolua_building_open(L); + tolua_ship_open(L); + tolua_region_open(L); tolua_faction_open(L); tolua_unit_open(L); tolua_message_open(L); + tolua_hashtable_open(L); + tolua_gmtool_open(L); #endif #ifdef BINDINGS_LUABIND diff --git a/src/eressea/tolua/bind_building.c b/src/eressea/tolua/bind_building.c new file mode 100644 index 000000000..077fed878 --- /dev/null +++ b/src/eressea/tolua/bind_building.c @@ -0,0 +1,120 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#include +#include "bind_building.h" +#include "bind_unit.h" + +#include +#include +#include + +#include +#include + +static int +tolua_building_addaction(lua_State* tolua_S) +{ + building* self = (building*)tolua_tousertype(tolua_S, 1, 0); + const char * fname = tolua_tostring(tolua_S, 2, 0); + const char * param = tolua_tostring(tolua_S, 3, 0); + + building_addaction(self, fname, param); + + return 0; +} + +static int +tolua_building_get_objects(lua_State* tolua_S) +{ + building * self = (building *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushusertype(tolua_S, (void*)&self->attribs, "hashtable"); + return 1; +} + +static int tolua_building_get_name(lua_State* tolua_S) +{ + building* self = (building*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, building_getname(self)); + return 1; +} + +static int tolua_building_set_name(lua_State* tolua_S) +{ + building* self = (building*)tolua_tousertype(tolua_S, 1, 0); + building_setname(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + +static int +tolua_building_get_units(lua_State* tolua_S) +{ + building * self = (building *)tolua_tousertype(tolua_S, 1, 0); + unit ** unit_ptr = (unit**)lua_newuserdata(tolua_S, sizeof(unit *)); + unit * u = self->region->units; + + while (u && u->building!=self) u = u->next; + luaL_getmetatable(tolua_S, "unit"); + lua_setmetatable(tolua_S, -2); + + *unit_ptr = u; + + lua_pushcclosure(tolua_S, tolua_unitlist_nextb, 1); + return 1; +} + +static int +tolua_building_get_id(lua_State* tolua_S) +{ + building * self = (building *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushnumber(tolua_S, (lua_Number)self->no); + return 1; +} + +static int +tolua_building_create(lua_State* tolua_S) +{ + region * r = (region *)tolua_tousertype(tolua_S, 1, 0); + const char * bname = tolua_tostring(tolua_S, 2, 0); + const building_type * btype = bt_find(bname); + building * b = new_building(btype, r, NULL); + tolua_pushusertype(tolua_S, (void*)b, "building"); + return 1; +} + + +void +tolua_building_open(lua_State* tolua_S) +{ + /* register user types */ + tolua_usertype(tolua_S, "building"); + tolua_usertype(tolua_S, "building_list"); + + tolua_module(tolua_S, NULL, 0); + tolua_beginmodule(tolua_S, NULL); + { + tolua_cclass(tolua_S, "building", "building", "", NULL); + tolua_beginmodule(tolua_S, "building"); + { + tolua_variable(tolua_S, "id", tolua_building_get_id, NULL); + tolua_variable(tolua_S, "name", tolua_building_get_name, tolua_building_set_name); + tolua_variable(tolua_S, "units", tolua_building_get_units, NULL); + tolua_function(tolua_S, "add_action", tolua_building_addaction); + + tolua_variable(tolua_S, "objects", tolua_building_get_objects, 0); + + tolua_function(tolua_S, "create", tolua_building_create); + } + tolua_endmodule(tolua_S); + } + tolua_endmodule(tolua_S); +} diff --git a/src/eressea/tolua/bind_building.h b/src/eressea/tolua/bind_building.h new file mode 100644 index 000000000..1256dd614 --- /dev/null +++ b/src/eressea/tolua/bind_building.h @@ -0,0 +1,22 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#ifdef __cplusplus +extern "C" { +#endif + + struct lua_State; + void tolua_building_open(struct lua_State *tolua_S); + +#ifdef __cplusplus +} +#endif diff --git a/src/eressea/tolua/bind_faction.c b/src/eressea/tolua/bind_faction.c index 3e8b67f63..832596cee 100644 --- a/src/eressea/tolua/bind_faction.c +++ b/src/eressea/tolua/bind_faction.c @@ -11,13 +11,23 @@ without prior permission by the authors of Eressea. */ #include +#include "bind_faction.h" +#include "bind_unit.h" +#include "bindings.h" +#include +#include #include +#include #include +#include + +#include #include #include + int tolua_factionlist_next(lua_State *tolua_S) { faction** faction_ptr = (faction **)lua_touserdata(tolua_S, lua_upvalueindex(1)); @@ -56,6 +66,24 @@ static int tolua_faction_get_units(lua_State* tolua_S) return 1; } +int tolua_faction_add_item(lua_State *tolua_S) +{ + faction * self = (faction *)tolua_tousertype(tolua_S, 1, 0); + const char * iname = tolua_tostring(tolua_S, 2, 0); + int number = (int)tolua_tonumber(tolua_S, 3, 0); + int result = -1; + + if (iname!=NULL) { + const item_type * itype = it_find(iname); + if (itype!=NULL) { + item * i = i_change(&self->items, itype, number); + result = i?i->number:0; + } // if (itype!=NULL) + } + lua_pushnumber(tolua_S, result); + return 1; +} + static int tolua_faction_get_maxheroes(lua_State* tolua_S) { @@ -130,6 +158,14 @@ tolua_faction_renumber(lua_State* tolua_S) return 0; } +static int +tolua_faction_get_objects(lua_State* tolua_S) +{ + faction * self = (faction *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushusertype(tolua_S, (void*)&self->attribs, "hashtable"); + return 1; +} + static int tolua_faction_get_policy(lua_State* tolua_S) { @@ -214,6 +250,131 @@ tolua_faction_create(lua_State* tolua_S) return 1; } +static int tolua_faction_get_password(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, faction_getpassword(self)); + return 1; +} + +static int tolua_faction_set_password(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + faction_setpassword(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + +static int tolua_faction_get_email(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, faction_getemail(self)); + return 1; +} + +static int tolua_faction_set_email(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + faction_setemail(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + +static int tolua_faction_get_locale(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, locale_name(self->locale)); + return 1; +} + +static int tolua_faction_set_locale(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + self->locale = find_locale(name); + return 0; +} + +static int tolua_faction_get_race(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, self->race->_name[0]); + return 1; +} + +static int tolua_faction_set_race(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + race * rc = rc_find(name); + if (rc!=NULL) { + self->race = rc; + } + + return 0; +} + +static int tolua_faction_get_name(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, faction_getname(self)); + return 1; +} + +static int tolua_faction_set_name(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + faction_setname(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + +static int tolua_faction_get_info(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, faction_getbanner(self)); + return 1; +} + +static int tolua_faction_set_info(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + faction_setbanner(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + +static int tolua_faction_get_alliance(lua_State* tolua_S) +{ + faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushusertype(tolua_S, self->alliance, "alliance"); + return 1; +} + +static int tolua_faction_set_alliance(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + alliance* alli = (alliance*)tolua_tousertype(tolua_S, 2, 0); + + if (!self->alliance) { + setalliance(self, alli); + } + + return 0; +} + +static int tolua_faction_get_items(lua_State* tolua_S) +{ + faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); + item ** item_ptr = (item **)lua_newuserdata(tolua_S, sizeof(item *)); + + luaL_getmetatable(tolua_S, "item"); + lua_setmetatable(tolua_S, -2); + + *item_ptr = self->items; + + lua_pushcclosure(tolua_S, tolua_itemlist_next, 1); + + return 1; +} + + void tolua_faction_open(lua_State* tolua_S) { @@ -228,6 +389,7 @@ tolua_faction_open(lua_State* tolua_S) tolua_beginmodule(tolua_S, "faction"); { tolua_variable(tolua_S, "name", tolua_faction_get_name, tolua_faction_set_name); + tolua_variable(tolua_S, "info", tolua_faction_get_info, tolua_faction_set_info); tolua_variable(tolua_S, "units", tolua_faction_get_units, NULL); tolua_variable(tolua_S, "heroes", tolua_faction_get_heroes, NULL); tolua_variable(tolua_S, "maxheroes", tolua_faction_get_maxheroes, NULL); @@ -247,8 +409,13 @@ tolua_faction_open(lua_State* tolua_S) tolua_function(tolua_S, "get_policy", tolua_faction_get_policy); tolua_function(tolua_S, "get_origin", tolua_faction_get_origin); + tolua_function(tolua_S, "add_item", tolua_faction_add_item); + tolua_variable(tolua_S, "items", tolua_faction_get_items, NULL); + tolua_function(tolua_S, "renumber", tolua_faction_renumber); tolua_function(tolua_S, "create", tolua_faction_create); + + tolua_variable(tolua_S, "objects", tolua_faction_get_objects, NULL); } tolua_endmodule(tolua_S); } diff --git a/src/eressea/tolua/bind_gmtool.c b/src/eressea/tolua/bind_gmtool.c new file mode 100644 index 000000000..bab8c3701 --- /dev/null +++ b/src/eressea/tolua/bind_gmtool.c @@ -0,0 +1,177 @@ +#include + +#include "bind_gmtool.h" +#include "../gmtool.h" +#include "../gmtool_structs.h" + +#include + +#include +#include + +static int +tolua_run_mapper(lua_State* tolua_S) +{ + run_mapper(); + return 0; +} + +static int +tolua_highlight_region(lua_State* tolua_S) +{ + region * r = tolua_tousertype(tolua_S, 1, 0); + int select = tolua_toboolean(tolua_S, 2, 0); + highlight_region(r, select); + return 0; +} + +static int +tolua_current_region(lua_State* tolua_S) +{ + map_region * mr = cursor_region(¤t_state->display, ¤t_state->cursor); + tolua_pushusertype(tolua_S, mr?mr->r:NULL, "region"); + return 1; +} + + +static int +tolua_select_coordinate(lua_State* tolua_S) +{ + int x = (int)tolua_tonumber(tolua_S, 1, 0); + int y = (int)tolua_tonumber(tolua_S, 2, 0); + int select = tolua_toboolean(tolua_S, 3, 0); + if (current_state) { + select_coordinate(current_state->selected, x, y, select); + } + return 0; +} + +static int +tolua_select_region(lua_State* tolua_S) +{ + region * r = tolua_tousertype(tolua_S, 1, 0); + int select = tolua_toboolean(tolua_S, 2, 0); + if (current_state) { + select_coordinate(current_state->selected, r->x, r->y, select); + } + return 0; +} + +typedef struct tag_iterator { + selection * list; + tag * node; + region * r; + int hash; +} tag_iterator; + +void +tag_advance(tag_iterator * iter) +{ + while (iter->hash!=MAXTHASH) { + if (iter->node) { + iter->node = iter->node->nexthash; + } + while (!iter->node && iter->hash != MAXTHASH) { + if (++iter->hash != MAXTHASH) { + iter->node = iter->list->tags[iter->hash]; + } + } + if (iter->node) { + iter->r = findregion((short)iter->node->coord.x, (short)iter->node->coord.y); + if (iter->r) { + break; + } + } + } +} + +void +tag_rewind(tag_iterator * iter) +{ + if (iter->list) { + iter->r = NULL; + iter->node = iter->list->tags[0]; + iter->hash = 0; + if (iter->node) { + iter->r = findregion((short)iter->node->coord.x, (short)iter->node->coord.y); + } + if (!iter->r) { + tag_advance(iter); + } + } else { + iter->node = 0; + iter->hash = MAXTHASH; + } +} + +static int +tolua_tags_next(lua_State *tolua_S) +{ + tag_iterator * iter = (tag_iterator *)lua_touserdata(tolua_S, lua_upvalueindex(1)); + if (iter->node) { + tolua_pushusertype(tolua_S, (void*)iter->r, "region"); + tag_advance(iter); + return 1; + } + else { + return 0; /* no more values to return */ + } +} + +static int +tolua_selected_regions(lua_State* tolua_S) +{ + tag_iterator * iter = (tag_iterator*)lua_newuserdata(tolua_S, sizeof(tag_iterator)); + + luaL_getmetatable(tolua_S, "tag_iterator"); + lua_setmetatable(tolua_S, -2); + + iter->list = current_state->selected; + tag_rewind(iter); + + lua_pushcclosure(tolua_S, tolua_tags_next, 1); + return 1; +} + +static int +tolua_state_open(lua_State* tolua_S) +{ + unused(tolua_S); + state_open(); + return 0; +} + +static int +tolua_state_close(lua_State* tolua_S) +{ + unused(tolua_S); + state_close(current_state); + return 0; +} + +void +tolua_gmtool_open(lua_State* tolua_S) +{ + /* register user types */ + tolua_usertype(tolua_S, "tag_iterator"); + + tolua_module(tolua_S, NULL, 0); + tolua_beginmodule(tolua_S, NULL); + { + tolua_module(tolua_S, "gmtool", 0); + tolua_beginmodule(tolua_S, "gmtool"); + { + tolua_function(tolua_S, "open", tolua_state_open); + tolua_function(tolua_S, "close", tolua_state_close); + + tolua_function(tolua_S, "editor", tolua_run_mapper); + tolua_function(tolua_S, "get_selection", tolua_selected_regions); + tolua_function(tolua_S, "get_cursor", tolua_current_region); + tolua_function(tolua_S, "highlight", tolua_highlight_region); + tolua_function(tolua_S, "select", tolua_select_region); + tolua_function(tolua_S, "select_at", tolua_select_coordinate); + } + tolua_endmodule(tolua_S); + } + tolua_endmodule(tolua_S); +} \ No newline at end of file diff --git a/src/eressea/tolua/bind_gmtool.h b/src/eressea/tolua/bind_gmtool.h new file mode 100644 index 000000000..fcde8059b --- /dev/null +++ b/src/eressea/tolua/bind_gmtool.h @@ -0,0 +1,10 @@ +#ifdef __cplusplus +extern "C" { +#endif + + struct lua_State; + void tolua_gmtool_open(struct lua_State *tolua_S); + +#ifdef __cplusplus +} +#endif diff --git a/src/eressea/tolua/bind_hashtable.c b/src/eressea/tolua/bind_hashtable.c new file mode 100644 index 000000000..f9dbed01e --- /dev/null +++ b/src/eressea/tolua/bind_hashtable.c @@ -0,0 +1,187 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#include +#include "bind_hashtable.h" + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include + +static int +tolua_hashtable_get(lua_State* tolua_S) +{ + hashtable self = (hashtable) tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + attrib * a = a_find(*self, &at_object); + + for (; a && a->type == &at_object; a = a->next) { + const char * obj_name = object_name(a); + if (obj_name && name && strcmp(obj_name, name) == 0) { + variant val; + object_type type; + + object_get(a, &type, &val); + switch (type) { + case TNONE: + lua_pushnil(tolua_S); + break; + case TINTEGER: + lua_pushnumber(tolua_S, (lua_Number)val.i); + break; + case TREAL: + lua_pushnumber(tolua_S, (lua_Number)val.f); + break; + case TREGION: + tolua_pushusertype(tolua_S, val.v, "region"); + break; + case TBUILDING: + tolua_pushusertype(tolua_S, val.v, "building"); + break; + case TUNIT: + tolua_pushusertype(tolua_S, val.v, "unit"); + break; + case TSHIP: + tolua_pushusertype(tolua_S, val.v, "ship"); + break; + case TSTRING: + tolua_pushstring(tolua_S, (const char*) val.v); + break; + default: + assert(!"not implemented"); + } + return 1; + } + } + lua_pushnil(tolua_S); + return 1; +} + +static int +tolua_hashtable_set_number(lua_State* tolua_S) +{ + hashtable self = (hashtable) tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + lua_Number value = tolua_tonumber(tolua_S, 3, 0); + attrib * a = a_find(*self, &at_object); + variant val; + + val.f = (float)value; + + for (; a && a->type == &at_object; a = a->next) { + if (strcmp(object_name(a), name) == 0) { + object_set(a, TREAL, val); + return 0; + } + } + + a = a_add(self, object_create(name, TREAL, val)); + return 0; +} + +static int +tolua_hashtable_set_string(lua_State* tolua_S) +{ + hashtable self = (hashtable) tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + const char * value = tolua_tostring(tolua_S, 3, 0); + attrib * a = a_find(*self, &at_object); + variant val; + + val.v = strdup(value); + + for (; a && a->type == &at_object; a = a->next) { + if (strcmp(object_name(a), name) == 0) { + object_set(a, TSTRING, val); + return 0; + } + } + + a = a_add(self, object_create(name, TSTRING, val)); + return 0; +} + +static int +tolua_hashtable_set_usertype(lua_State* tolua_S, int type) +{ + hashtable self = (hashtable) tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + unit * value = tolua_tousertype(tolua_S, 3, 0); + attrib * a = a_find(*self, &at_object); + variant val; + + val.v = value; + + for (; a && a->type == &at_object; a = a->next) { + if (strcmp(object_name(a), name) == 0) { + object_set(a, type, val); + return 0; + } + } + + a = a_add(self, object_create(name, type, val)); + return 0; +} + + +static int +tolua_hashtable_set(lua_State* tolua_S) +{ + tolua_Error tolua_err; + if (tolua_isnumber(tolua_S, 3, 0, &tolua_err)) { + return tolua_hashtable_set_number(tolua_S); + } else if (tolua_isusertype(tolua_S, 3, "unit", 0, &tolua_err)) { + return tolua_hashtable_set_usertype(tolua_S, TUNIT); + } else if (tolua_isusertype(tolua_S, 3, "faction", 0, &tolua_err)) { + return tolua_hashtable_set_usertype(tolua_S, TFACTION); + } else if (tolua_isusertype(tolua_S, 3, "ship", 0, &tolua_err)) { + return tolua_hashtable_set_usertype(tolua_S, TSHIP); + } else if (tolua_isusertype(tolua_S, 3, "building", 0, &tolua_err)) { + return tolua_hashtable_set_usertype(tolua_S, TBUILDING); + } else if (tolua_isusertype(tolua_S, 3, "region", 0, &tolua_err)) { + return tolua_hashtable_set_usertype(tolua_S, TREGION); + } + return tolua_hashtable_set_string(tolua_S); +} + + + +void +tolua_hashtable_open(lua_State* tolua_S) +{ + /* register user types */ + tolua_usertype(tolua_S, "hashtable"); + + tolua_module(tolua_S, NULL, 0); + tolua_beginmodule(tolua_S, NULL); + { + tolua_cclass(tolua_S, "hashtable", "hashtable", "", NULL); + tolua_beginmodule(tolua_S, "hashtable"); + { + tolua_function(tolua_S, "get", tolua_hashtable_get); + tolua_function(tolua_S, "set", tolua_hashtable_set); + } + tolua_endmodule(tolua_S); + } + tolua_endmodule(tolua_S); +} diff --git a/src/eressea/tolua/bind_hashtable.h b/src/eressea/tolua/bind_hashtable.h new file mode 100644 index 000000000..fc08801db --- /dev/null +++ b/src/eressea/tolua/bind_hashtable.h @@ -0,0 +1,24 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#ifdef __cplusplus +extern "C" { +#endif + + struct lua_State; + void tolua_hashtable_open(struct lua_State *tolua_S); + + typedef struct attrib ** hashtable; + +#ifdef __cplusplus +} +#endif diff --git a/src/eressea/tolua/bind_region.c b/src/eressea/tolua/bind_region.c new file mode 100644 index 000000000..675adf871 --- /dev/null +++ b/src/eressea/tolua/bind_region.c @@ -0,0 +1,257 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#include +#include "bind_region.h" +#include "bind_unit.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +int tolua_regionlist_next(lua_State *tolua_S) +{ + region** region_ptr = (region **)lua_touserdata(tolua_S, lua_upvalueindex(1)); + region * u = *region_ptr; + if (u != NULL) { + tolua_pushusertype(tolua_S, (void*)u, "region"); + *region_ptr = u->next; + return 1; + } + else return 0; /* no more values to return */ +} + +static int +tolua_region_get_id(lua_State* tolua_S) +{ + region * self = (region *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushnumber(tolua_S, (lua_Number)self->uid); + return 1; +} + +static int +tolua_region_get_terrain(lua_State* tolua_S) +{ + region* self = (region*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, self->terrain->_name); + return 1; +} + +static int tolua_region_get_name(lua_State* tolua_S) +{ + region* self = (region*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, region_getname(self)); + return 1; +} + +static int tolua_region_set_units(lua_State* tolua_S) +{ + region* self = (region*)tolua_tousertype(tolua_S, 1, 0); + region_setname(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + + +static int tolua_region_get_flag(lua_State* tolua_S) +{ + region* self = (region*)tolua_tousertype(tolua_S, 1, 0); + int bit = (int)tolua_tonumber(tolua_S, 2, 0); + + lua_pushboolean(tolua_S, (self->flags & (1<flags |= (1<flags &= ~(1<attribs, "hashtable"); + return 1; +} + +static int +tolua_region_create(lua_State* tolua_S) +{ + short x = (short)tolua_tonumber(tolua_S, 1, 0); + short y = (short)tolua_tonumber(tolua_S, 2, 0); + const char * tname = tolua_tostring(tolua_S, 3, 0); + + const terrain_type * terrain = get_terrain(tname); + region * r = findregion(x, y); + region * result = r; + + if (terrain==NULL) { + if (r!=NULL) { + if (r->units!=NULL) { + /* TODO: error message */ + result = NULL; + } + } + } + if (r==NULL) { + result = new_region(x, y, 0); + } + if (result) { + terraform_region(result, terrain); + } + + tolua_pushusertype(tolua_S, result, "region"); + return 1; +} + +static int tolua_region_get_units(lua_State* tolua_S) +{ + region * self = (region *)tolua_tousertype(tolua_S, 1, 0); + unit ** unit_ptr = (unit**)lua_newuserdata(tolua_S, sizeof(unit *)); + + luaL_getmetatable(tolua_S, "unit"); + lua_setmetatable(tolua_S, -2); + + *unit_ptr = self->units; + + lua_pushcclosure(tolua_S, tolua_unitlist_next, 1); + return 1; +} + +static int +tolua_region_getkey(lua_State* tolua_S) +{ + region * self = (region *)tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + + int flag = atoi36(name); + attrib * a = find_key(self->attribs, flag); + lua_pushboolean(tolua_S, a!=NULL); + + return 1; +} + +static int +tolua_region_setkey(lua_State* tolua_S) +{ + region * self = (region *)tolua_tousertype(tolua_S, 1, 0); + const char * name = tolua_tostring(tolua_S, 2, 0); + int value = tolua_toboolean(tolua_S, 3, 0); + + int flag = atoi36(name); + attrib * a = find_key(self->attribs, flag); + if (a==NULL && value) { + add_key(&self->attribs, flag); + } else if (a!=NULL && !value) { + a_remove(&self->attribs, a); + } + return 0; +} + +void +tolua_region_open(lua_State* tolua_S) +{ + /* register user types */ + tolua_usertype(tolua_S, "region"); + + tolua_module(tolua_S, NULL, 0); + tolua_beginmodule(tolua_S, NULL); + { + tolua_cclass(tolua_S, "region", "region", "", NULL); + tolua_beginmodule(tolua_S, "region"); + { + tolua_variable(tolua_S, "id", tolua_region_get_id, NULL); + tolua_variable(tolua_S, "name", tolua_region_get_name, tolua_region_set_units); + tolua_variable(tolua_S, "units", tolua_region_get_units, NULL); + tolua_variable(tolua_S, "terrain", tolua_region_get_terrain, NULL); + tolua_function(tolua_S, "get_resource", tolua_region_get_resource); + tolua_function(tolua_S, "set_resource", tolua_region_set_resource); + tolua_function(tolua_S, "get_flag", tolua_region_get_flag); + tolua_function(tolua_S, "create", tolua_region_create); + + tolua_function(tolua_S, "get_key", tolua_region_getkey); + tolua_function(tolua_S, "set_key", tolua_region_setkey); + + tolua_variable(tolua_S, "objects", tolua_region_get_objects, 0); + } + tolua_endmodule(tolua_S); + } + tolua_endmodule(tolua_S); +} diff --git a/src/eressea/tolua/bind_region.h b/src/eressea/tolua/bind_region.h new file mode 100644 index 000000000..61e9958d4 --- /dev/null +++ b/src/eressea/tolua/bind_region.h @@ -0,0 +1,23 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#ifdef __cplusplus +extern "C" { +#endif + + struct lua_State; + void tolua_region_open(struct lua_State *tolua_S); + int tolua_regionlist_next(struct lua_State *tolua_S); + +#ifdef __cplusplus +} +#endif diff --git a/src/eressea/tolua/bind_ship.c b/src/eressea/tolua/bind_ship.c new file mode 100644 index 000000000..c975eb63c --- /dev/null +++ b/src/eressea/tolua/bind_ship.c @@ -0,0 +1,109 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#include +#include "bind_ship.h" +#include "bind_unit.h" + +#include +#include +#include +#include + +#include + +#include +#include + +static int +tolua_ship_get_id(lua_State* tolua_S) +{ + ship * self = (ship *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushnumber(tolua_S, (lua_Number)self->no); + return 1; +} + +static int tolua_ship_get_name(lua_State* tolua_S) +{ + ship* self = (ship*) tolua_tousertype(tolua_S, 1, 0); + tolua_pushstring(tolua_S, ship_getname(self)); + return 1; +} + +static int tolua_ship_set_name(lua_State* tolua_S) +{ + ship* self = (ship*)tolua_tousertype(tolua_S, 1, 0); + ship_setname(self, tolua_tostring(tolua_S, 2, 0)); + return 0; +} + +static int +tolua_ship_get_units(lua_State* tolua_S) +{ + ship * self = (ship *)tolua_tousertype(tolua_S, 1, 0); + unit ** unit_ptr = (unit**)lua_newuserdata(tolua_S, sizeof(unit *)); + unit * u = self->region->units; + + while (u && u->ship!=self) u = u->next; + luaL_getmetatable(tolua_S, "unit"); + lua_setmetatable(tolua_S, -2); + + *unit_ptr = u; + + lua_pushcclosure(tolua_S, tolua_unitlist_nexts, 1); + return 1; +} + +static int +tolua_ship_get_objects(lua_State* tolua_S) +{ + ship * self = (ship *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushusertype(tolua_S, (void*)&self->attribs, "hashtable"); + return 1; +} + +static int +tolua_ship_create(lua_State* tolua_S) +{ + region * r = (region *)tolua_tousertype(tolua_S, 1, 0); + const char * sname = tolua_tostring(tolua_S, 2, 0); + const ship_type * stype = st_find(sname); + ship * sh = new_ship(stype, NULL, r); + sh->size = stype->construction->maxsize; + tolua_pushusertype(tolua_S, (void*)sh, "ship"); + return 1; +} + +void +tolua_ship_open(lua_State* tolua_S) +{ + /* register user types */ + tolua_usertype(tolua_S, "ship"); + + tolua_module(tolua_S, NULL, 0); + tolua_beginmodule(tolua_S, NULL); + { + tolua_cclass(tolua_S, "ship", "ship", "", NULL); + tolua_beginmodule(tolua_S, "ship"); + { + tolua_variable(tolua_S, "id", tolua_ship_get_id, NULL); + tolua_variable(tolua_S, "name", tolua_ship_get_name, tolua_ship_set_name); + tolua_variable(tolua_S, "units", tolua_ship_get_units, NULL); + + tolua_variable(tolua_S, "objects", tolua_ship_get_objects, 0); + + tolua_function(tolua_S, "create", tolua_ship_create); + } + tolua_endmodule(tolua_S); + } + tolua_endmodule(tolua_S); +} diff --git a/src/eressea/tolua/bind_ship.h b/src/eressea/tolua/bind_ship.h new file mode 100644 index 000000000..a68bdf16e --- /dev/null +++ b/src/eressea/tolua/bind_ship.h @@ -0,0 +1,22 @@ +/* vi: set ts=2: ++-------------------+ +| | Enno Rehling +| Eressea PBEM host | Christian Schlittchen +| (c) 1998 - 2008 | Katja Zedel +| | Henning Peters ++-------------------+ + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ + +#ifdef __cplusplus +extern "C" { +#endif + + struct lua_State; + void tolua_ship_open(struct lua_State *tolua_S); + +#ifdef __cplusplus +} +#endif diff --git a/src/eressea/tolua/bind_unit.c b/src/eressea/tolua/bind_unit.c index ae01808bc..67d82d554 100644 --- a/src/eressea/tolua/bind_unit.c +++ b/src/eressea/tolua/bind_unit.c @@ -12,6 +12,9 @@ without prior permission by the authors of Eressea. #include +#include "bind_unit.h" +#include "bindings.h" + // Atributes includes #include #include @@ -43,42 +46,12 @@ without prior permission by the authors of Eressea. #include #include -int tolua_spelllist_next(lua_State *tolua_S) +static int +tolua_unit_get_objects(lua_State* tolua_S) { - spell_list** spell_ptr = (spell_list **)lua_touserdata(tolua_S, lua_upvalueindex(1)); - spell_list* slist = *spell_ptr; - if (slist != NULL) { - tolua_pushusertype(tolua_S, slist->data, "spell"); - *spell_ptr = slist->next; - return 1; - } - else return 0; /* no more values to return */ -} - -int tolua_itemlist_next(lua_State *tolua_S) -{ - item** item_ptr = (item **)lua_touserdata(tolua_S, lua_upvalueindex(1)); - item* itm = *item_ptr; - if (itm != NULL) { - tolua_pushstring(tolua_S, itm->type->rtype->_name[0]); - *item_ptr = itm->next; - return 1; - } - else return 0; /* no more values to return */ -} - -int tolua_orderlist_next(lua_State *tolua_S) -{ - order** order_ptr = (order **)lua_touserdata(tolua_S, lua_upvalueindex(1)); - order* ord = *order_ptr; - if (ord != NULL) { - char cmd[8192]; - write_order(ord, cmd, sizeof(cmd)); - tolua_pushstring(tolua_S, cmd); - *order_ptr = ord->next; - return 1; - } - else return 0; /* no more values to return */ + unit * self = (unit *)tolua_tousertype(tolua_S, 1, 0); + tolua_pushusertype(tolua_S, (void*)&self->attribs, "hashtable"); + return 1; } int tolua_unitlist_nextf(lua_State *tolua_S) @@ -93,6 +66,42 @@ int tolua_unitlist_nextf(lua_State *tolua_S) else return 0; /* no more values to return */ } +int tolua_unitlist_nextb(lua_State *tolua_S) +{ + unit** unit_ptr = (unit **)lua_touserdata(tolua_S, lua_upvalueindex(1)); + unit * u = *unit_ptr; + if (u != NULL) { + unit * unext = u->next; + tolua_pushusertype(tolua_S, (void*)u, "unit"); + + while (unext && unext->building!=u->building) { + unext = unext->next; + } + *unit_ptr = unext; + + return 1; + } + else return 0; /* no more values to return */ +} + +int tolua_unitlist_nexts(lua_State *tolua_S) +{ + unit** unit_ptr = (unit **)lua_touserdata(tolua_S, lua_upvalueindex(1)); + unit * u = *unit_ptr; + if (u != NULL) { + unit * unext = u->next; + tolua_pushusertype(tolua_S, (void*)u, "unit"); + + while (unext && unext->ship!=u->ship) { + unext = unext->next; + } + *unit_ptr = unext; + + return 1; + } + else return 0; /* no more values to return */ +} + int tolua_unitlist_next(lua_State *tolua_S) { unit** unit_ptr = (unit **)lua_touserdata(tolua_S, lua_upvalueindex(1)); @@ -888,6 +897,8 @@ tolua_unit_open(lua_State * tolua_S) tolua_variable(tolua_S, "number", tolua_unit_get_number, tolua_unit_set_number); tolua_variable(tolua_S, "race", tolua_unit_get_race, tolua_unit_set_race); tolua_variable(tolua_S, "hp_max", tolua_unit_get_hpmax, 0); + + tolua_variable(tolua_S, "objects", tolua_unit_get_objects, 0); } tolua_endmodule(tolua_S); } diff --git a/src/eressea/tolua/bind_unit.h b/src/eressea/tolua/bind_unit.h index 8de703d25..3666dec77 100644 --- a/src/eressea/tolua/bind_unit.h +++ b/src/eressea/tolua/bind_unit.h @@ -15,6 +15,8 @@ extern "C" { #endif struct lua_State; + int tolua_unitlist_nextb(struct lua_State *tolua_S); + int tolua_unitlist_nexts(struct lua_State *tolua_S); int tolua_unitlist_nextf(struct lua_State *tolua_S); int tolua_unitlist_next(struct lua_State *tolua_S); void tolua_unit_open(struct lua_State *tolua_S); diff --git a/src/eressea/tolua/bindings.c b/src/eressea/tolua/bindings.c index 0d47f49a4..d2fc9c1e0 100644 --- a/src/eressea/tolua/bindings.c +++ b/src/eressea/tolua/bindings.c @@ -14,14 +14,11 @@ without prior permission by the authors of Eressea. #include "bindings.h" #include "bind_unit.h" #include "bind_faction.h" +#include "bind_region.h" #include -#include -#include -#include -#include #include #include #include @@ -34,6 +31,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include #include #include @@ -44,86 +42,116 @@ without prior permission by the authors of Eressea. #include #include +#include #include +#include + +#include +#include +#include +#include +#include +#include #include #include #include +#include -static int tolua_get_building_name(lua_State* tolua_S) +int tolua_orderlist_next(lua_State *tolua_S) { - building* self = (building*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, building_getname(self)); - return 1; -} - -static int tolua_set_building_name(lua_State* tolua_S) -{ - building* self = (building*)tolua_tousertype(tolua_S, 1, 0); - building_setname(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - -static int -tolua_region_get_terrain(lua_State* tolua_S) -{ - region* self = (region*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, self->terrain->_name); - return 1; -} - -static int tolua_region_get_name(lua_State* tolua_S) -{ - region* self = (region*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, region_getname(self)); - return 1; -} - -static int tolua_region_set_units(lua_State* tolua_S) -{ - region* self = (region*)tolua_tousertype(tolua_S, 1, 0); - region_setname(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - -static int tolua_get_ship_name(lua_State* tolua_S) -{ - ship* self = (ship*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, ship_getname(self)); - return 1; -} - -static int tolua_set_ship_name(lua_State* tolua_S) -{ - ship* self = (ship*)tolua_tousertype(tolua_S, 1, 0); - ship_setname(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - -static int tolua_regionlist_next(lua_State *tolua_S) -{ - region** region_ptr = (region **)lua_touserdata(tolua_S, lua_upvalueindex(1)); - region * u = *region_ptr; - if (u != NULL) { - tolua_pushusertype(tolua_S, (void*)u, "region"); - *region_ptr = u->next; + order** order_ptr = (order **)lua_touserdata(tolua_S, lua_upvalueindex(1)); + order* ord = *order_ptr; + if (ord != NULL) { + char cmd[8192]; + write_order(ord, cmd, sizeof(cmd)); + tolua_pushstring(tolua_S, cmd); + *order_ptr = ord->next; return 1; } else return 0; /* no more values to return */ } -static int tolua_region_get_units(lua_State* tolua_S) +int tolua_spelllist_next(lua_State *tolua_S) { - region * self = (region *)tolua_tousertype(tolua_S, 1, 0); - unit ** unit_ptr = (unit**)lua_newuserdata(tolua_S, sizeof(unit *)); + spell_list** spell_ptr = (spell_list **)lua_touserdata(tolua_S, lua_upvalueindex(1)); + spell_list* slist = *spell_ptr; + if (slist != NULL) { + tolua_pushusertype(tolua_S, slist->data, "spell"); + *spell_ptr = slist->next; + return 1; + } + else return 0; /* no more values to return */ +} - luaL_getmetatable(tolua_S, "unit"); - lua_setmetatable(tolua_S, -2); +int tolua_itemlist_next(lua_State *tolua_S) +{ + item** item_ptr = (item **)lua_touserdata(tolua_S, lua_upvalueindex(1)); + item* itm = *item_ptr; + if (itm != NULL) { + tolua_pushstring(tolua_S, itm->type->rtype->_name[0]); + *item_ptr = itm->next; + return 1; + } + else return 0; /* no more values to return */ +} - *unit_ptr = self->units; +static int +tolua_autoseed(lua_State * tolua_S) +{ + const char * filename = tolua_tostring(tolua_S, 1, 0); + int new_island = tolua_toboolean(tolua_S, 2, 0); + newfaction * players = read_newfactions(filename); - lua_pushcclosure(tolua_S, tolua_unitlist_next, 1); + if (players!=NULL) { + while (players) { + int n = listlen(players); + int k = (n+ISLANDSIZE-1)/ISLANDSIZE; + k = n / k; + n = autoseed(&players, k, new_island?0:TURNS_PER_ISLAND); + if (n==0) { + break; + } + } + } + return 0; +} + + +static int +tolua_getkey(lua_State* tolua_S) +{ + const char * name = tolua_tostring(tolua_S, 1, 0); + + int flag = atoi36(name); + attrib * a = find_key(global.attribs, flag); + lua_pushboolean(tolua_S, a!=NULL); + + return 1; +} + +static int +tolua_setkey(lua_State* tolua_S) +{ + const char * name = tolua_tostring(tolua_S, 1, 0); + int value = tolua_toboolean(tolua_S, 2, 0); + + int flag = atoi36(name); + attrib * a = find_key(global.attribs, flag); + if (a==NULL && value) { + add_key(&global.attribs, flag); + } else if (a!=NULL && !value) { + a_remove(&global.attribs, a); + } + + return 0; +} + +static int +tolua_rng_int(lua_State* tolua_S) +{ + lua_pushnumber(tolua_S, (lua_Number)rng_int()); return 1; } @@ -584,106 +612,6 @@ tolua_get_unit(lua_State* tolua_S) return 1; } -static int tolua_region_get_flag(lua_State* tolua_S) -{ - region* self = (region*)tolua_tousertype(tolua_S, 1, 0); - int bit = (int)tolua_tonumber(tolua_S, 2, 0); - - lua_pushboolean(tolua_S, (self->flags & (1<flags |= (1<flags &= ~(1<units!=NULL) { - /* TODO: error message */ - result = NULL; - } - } - } - if (r==NULL) { - result = new_region(x, y, 0); - } - if (result) { - terraform_region(result, terrain); - } - - tolua_pushusertype(tolua_S, result, "region"); - return 1; -} - static int tolua_alliance_create(lua_State* tolua_S) { @@ -739,87 +667,6 @@ tolua_get_alliance_factions(lua_State* tolua_S) return 1; } -static int tolua_faction_get_password(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, faction_getpassword(self)); - return 1; -} - -static int tolua_faction_set_password(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - faction_setpassword(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - -static int tolua_faction_get_email(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, faction_getemail(self)); - return 1; -} - -static int tolua_faction_set_email(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - faction_setemail(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - -static int tolua_faction_get_locale(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, locale_name(self->locale)); - return 1; -} - -static int tolua_faction_set_locale(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - const char * name = tolua_tostring(tolua_S, 2, 0); - self->locale = find_locale(name); - return 0; -} - -static int tolua_faction_get_race(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, self->race->_name[0]); - return 1; -} - -static int tolua_faction_set_race(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - const char * name = tolua_tostring(tolua_S, 2, 0); - race * rc = rc_find(name); - if (rc!=NULL) { - self->race = rc; - } - - return 0; -} - -static int tolua_faction_get_alliance(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushusertype(tolua_S, self->alliance, "alliance"); - return 1; -} - -static int tolua_faction_set_alliance(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - alliance* alli = (alliance*)tolua_tousertype(tolua_S, 2, 0); - - if (!self->alliance) { - setalliance(self, alli); - } - - return 0; -} - static int tolua_get_alliance_id(lua_State* tolua_S) { alliance* self = (alliance*) tolua_tousertype(tolua_S, 1, 0); @@ -841,35 +688,6 @@ static int tolua_set_alliance_name(lua_State* tolua_S) return 0; } -static int tolua_faction_get_name(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, faction_getname(self)); - return 1; -} - -static int tolua_faction_set_name(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - faction_setname(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - -static int tolua_faction_get_info(lua_State* tolua_S) -{ - faction* self = (faction*) tolua_tousertype(tolua_S, 1, 0); - tolua_pushstring(tolua_S, faction_getbanner(self)); - return 1; -} - -static int tolua_faction_set_info(lua_State* tolua_S) -{ - faction* self = (faction*)tolua_tousertype(tolua_S, 1, 0); - faction_setbanner(self, tolua_tostring(tolua_S, 2, 0)); - return 0; -} - - int tolua_eressea_open(lua_State* tolua_S) { @@ -882,9 +700,6 @@ tolua_eressea_open(lua_State* tolua_S) tolua_usertype(tolua_S, "item"); tolua_usertype(tolua_S, "alliance"); tolua_usertype(tolua_S, "event"); - tolua_usertype(tolua_S, "region"); - tolua_usertype(tolua_S, "ship"); - tolua_usertype(tolua_S, "building"); tolua_module(tolua_S, NULL, 0); tolua_beginmodule(tolua_S, NULL); @@ -899,33 +714,6 @@ tolua_eressea_open(lua_State* tolua_S) } tolua_endmodule(tolua_S); - tolua_cclass(tolua_S, "region", "region", "", NULL); - tolua_beginmodule(tolua_S, "region"); - { - tolua_variable(tolua_S, "name", tolua_region_get_name, tolua_region_set_units); - tolua_variable(tolua_S, "units", tolua_region_get_units, NULL); - tolua_variable(tolua_S, "terrain", tolua_region_get_terrain, NULL); - tolua_function(tolua_S, "get_resource", tolua_region_get_resource); - tolua_function(tolua_S, "set_resource", tolua_region_set_resource); - tolua_function(tolua_S, "get_flag", tolua_region_get_flag); - tolua_function(tolua_S, "create", tolua_region_create); - } - tolua_endmodule(tolua_S); - - tolua_cclass(tolua_S, "building", "building", "", NULL); - tolua_beginmodule(tolua_S, "building"); - { - tolua_variable(tolua_S, "name", tolua_get_building_name, tolua_set_building_name); - } - tolua_endmodule(tolua_S); - - tolua_cclass(tolua_S, "ship", "ship", "", NULL); - tolua_beginmodule(tolua_S, "ship"); - { - tolua_variable(tolua_S, "name", tolua_get_ship_name, tolua_set_ship_name); - } - tolua_endmodule(tolua_S); - tolua_function(tolua_S, "get_faction", tolua_get_faction); tolua_function(tolua_S, "get_unit", tolua_get_unit); tolua_function(tolua_S, "get_alliance", tolua_get_alliance); @@ -988,6 +776,13 @@ tolua_eressea_open(lua_State* tolua_S) tolua_function(tolua_S, "update_scores", tolua_update_scores); tolua_function(tolua_S, "learn_skill", tolua_learn_skill); + + tolua_function(tolua_S, "autoseed", tolua_autoseed); + + tolua_function(tolua_S, "get_key", tolua_getkey); + tolua_function(tolua_S, "set_key", tolua_setkey); + + tolua_function(tolua_S, "rng_int", tolua_rng_int); } tolua_endmodule(tolua_S); return 1; diff --git a/src/eressea/tolua/bindings.h b/src/eressea/tolua/bindings.h index 8495a4b48..84539f654 100644 --- a/src/eressea/tolua/bindings.h +++ b/src/eressea/tolua/bindings.h @@ -15,7 +15,10 @@ extern "C" { #endif struct lua_State; - extern int tolua_eressea_open(struct lua_State* tolua_S); + int tolua_eressea_open(struct lua_State* tolua_S); + int tolua_spelllist_next(struct lua_State *tolua_S); + int tolua_itemlist_next(struct lua_State *tolua_S); + int tolua_orderlist_next(struct lua_State *tolua_S); #ifdef __cplusplus } diff --git a/src/eressea/tolua/helpers.c b/src/eressea/tolua/helpers.c index 4e6a0f2e8..f6d6937d2 100644 --- a/src/eressea/tolua/helpers.c +++ b/src/eressea/tolua/helpers.c @@ -30,6 +30,8 @@ without prior permission by the authors of Eressea. #include #include +#include + static int lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order * ord) { @@ -235,7 +237,7 @@ lua_initfamiliar(unit * u) lua_pop(L, 1); } } else { - log_error(("familiar(%s) calling '%s': not a function.\n", + log_warning(("familiar(%s) calling '%s': not a function.\n", unitname(u), fname)); lua_pop(L, 1); } diff --git a/src/eressea/tolua/message.xml b/src/eressea/tolua/message.xml index 1dcf75484..6fa598759 100644 --- a/src/eressea/tolua/message.xml +++ b/src/eressea/tolua/message.xml @@ -1,8 +1,18 @@ - - - + + + + + + + + + + + + + @@ -10,15 +20,15 @@ - - + + - - - + + + - + diff --git a/src/res/asgard/races.xml b/src/res/asgard/races.xml index 8ea4434b6..1bb526f52 100644 --- a/src/res/asgard/races.xml +++ b/src/res/asgard/races.xml @@ -87,7 +87,6 @@ - @@ -115,7 +114,6 @@ - @@ -143,7 +141,6 @@ - @@ -172,7 +169,6 @@ - @@ -205,9 +201,7 @@ - - - + @@ -234,7 +228,7 @@ - + @@ -265,7 +259,6 @@ - @@ -325,7 +318,6 @@ - @@ -355,7 +347,6 @@ - @@ -385,7 +376,6 @@ - @@ -416,7 +406,6 @@ - @@ -447,7 +436,6 @@ - @@ -476,7 +464,6 @@ - @@ -537,7 +524,6 @@ - diff --git a/src/res/races.xml b/src/res/races.xml index 9f16ff18d..19bdfe5a6 100644 --- a/src/res/races.xml +++ b/src/res/races.xml @@ -93,7 +93,7 @@ - + @@ -123,7 +123,7 @@ - + @@ -153,7 +153,7 @@ - + @@ -184,7 +184,7 @@ - + @@ -219,7 +219,7 @@ - + @@ -250,7 +250,7 @@ - + @@ -283,7 +283,7 @@ - + @@ -345,7 +345,7 @@ - + @@ -376,7 +376,7 @@ - + @@ -406,7 +406,7 @@ - + @@ -438,7 +438,7 @@ - + @@ -470,7 +470,7 @@ - + @@ -500,7 +500,7 @@ - + @@ -533,7 +533,7 @@ - + @@ -563,7 +563,7 @@ - + diff --git a/src/scripts/adamant.lua b/src/scripts/adamant.lua index 79b6eaa2f..e72ee3cdc 100644 --- a/src/scripts/adamant.lua +++ b/src/scripts/adamant.lua @@ -43,8 +43,8 @@ end -- make a tunnel from the cursor to the first selected region function mktunnel() - local from = gmtool.cursor() - local to = gmtool.selection()() + local from = gmtool.get_cursor() + local to = gmtool.get_selection()() if to~=nil then terraform(from.x, from.y, "glacier") create_tunnel(from, to) @@ -55,7 +55,7 @@ end -- turn all selected regions into targets for a wonky tunnel ("tnnL") function mkanchors() - for r in gmtool.selection() do + for r in gmtool.get_selection() do if not r:get_key("tnnL") then r:set_key("tnnL", true) if r:get_flag(0) then diff --git a/src/scripts/eressea/tunnels.lua b/src/scripts/eressea/tunnels.lua index f14cec492..cad8e4da7 100644 --- a/src/scripts/eressea/tunnels.lua +++ b/src/scripts/eressea/tunnels.lua @@ -34,7 +34,7 @@ local function get_target(param) if ntargets==0 then return nil end - local rn = math.mod(test.rng_int(), ntargets) + local rn = math.mod(rng_int(), ntargets) return targets[rn] end diff --git a/src/scripts/gm/move.lua b/src/scripts/gm/move.lua index 17f063a34..d08a69ed9 100644 --- a/src/scripts/gm/move.lua +++ b/src/scripts/gm/move.lua @@ -18,7 +18,7 @@ function swap_region(r, tr) end function move_selection(x, y) - for r in gmtool.selection() do + for r in gmtool.get_selection() do local tx = r.x+x local ty = r.y+y local tr = get_region(tx, ty) diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index 6fbfeeaeb..5cb3b267a 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -28,7 +28,56 @@ local function test_read_write() free_game() end -function test_unit() +local function test_gmtool() + free_game() + local r1 = region.create(1, 0, "plain") + local r2 = region.create(1, 1, "plain") + local r3 = region.create(1, 2, "plain") + gmtool.open() + gmtool.select(r1, true) + gmtool.select_at(0, 1, true) + gmtool.select(r2, true) + gmtool.select_at(0, 2, true) + gmtool.select(r3, false) + gmtool.select(r3, true) + gmtool.select_at(0, 3, false) + gmtool.select(r3, false) + + local selections = 0 + for r in gmtool.get_selection() do + selections=selections+1 + end + assert(selections==2) + print(gmtool.get_cursor()) + + gmtool.close() +end + +local function test_faction() + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("enno@eressea.de", "human", "de") + assert(f) + f.info = "Spazz" + assert(f.info=="Spazz") + f:add_item("donotwant", 42) + f:add_item("stone", 42) + f:add_item("sword", 42) + local items = 0 + for u in f.items do + items = items + 1 + end + assert(items==2) + unit.create(f, r) + unit.create(f, r) + local units = 0 + for u in f.units do + units = units + 1 + end + assert(units==2) +end + +local function test_unit() free_game() local r = region.create(0, 0, "plain") local f = faction.create("enno@eressea.de", "human", "de") @@ -36,6 +85,8 @@ function test_unit() u.number = 20 u.name = "Enno" assert(u.name=="Enno") + u.info = "Spazz" + assert(u.info=="Spazz") u:add_item("sword", 4) assert(u:get_item("sword")==4) assert(u:get_pooled("sword")==4) @@ -43,7 +94,7 @@ function test_unit() assert(u:get_item("sword")==2) end -function test_region() +local function test_region() free_game() local r = region.create(0, 0, "plain") r:set_resource("horse", 42) @@ -54,7 +105,29 @@ function test_region() assert(r:get_resource("peasant") == 200) end -function loadscript(name) +local function test_building() + free_game() + local u + local f = faction.create("enno@eressea.de", "human", "de") + local r = region.create(0, 0, "plain") + local b = building.create(r, "castle") + u = unit.create(f, r) + u.number = 1 + u.building = b + u = unit.create(f, r) + u.number = 2 + -- u.building = b + u = unit.create(f, r) + u.number = 3 + u.building = b + local units = 0 + for u in b.units do + units = units + 1 + end + assert(units==2) +end + +local function loadscript(name) local script = scriptpath .. "/" .. name print("- loading " .. script) if pcall(dofile, script)==0 then @@ -62,7 +135,7 @@ function loadscript(name) end end -function test_message() +local function test_message() free_game() local r = region.create(0, 0, "plain") local f = faction.create("enno@eressea.de", "human", "de") @@ -77,8 +150,24 @@ function test_message() return msg end +local function test_hashtable() + free_game() + local f = faction.create("enno@eressea.de", "human", "de") + f.objects:set("enno", "smart guy") + f.objects:set("age", 10) + assert(f.objects:get("jesus") == nil) + assert(f.objects:get("enno") == "smart guy") + assert(f.objects:get("age") == 10) + f.objects:set("age", nil) + assert(f.objects:get("age") == nil) +end + loadscript("extensions.lua") test_read_write() test_region() +test_faction() +test_building() test_unit() test_message() +test_hashtable() +test_gmtool() diff --git a/src/tools/codegen/codegen.cpp b/src/tools/codegen/codegen.cpp index 2552358ad..3195cb839 100644 --- a/src/tools/codegen/codegen.cpp +++ b/src/tools/codegen/codegen.cpp @@ -5,6 +5,7 @@ /* libc includes */ #include +const char * prefix = "tolua_"; const char * tmp_includes = "#include \n" "#include \n" @@ -26,14 +27,54 @@ readfile(const char * filename) return doc; } -void -parse_module(xmlXPathContextPtr xpath, FILE * out) +static void +open_function(xmlNodePtr node, FILE * out) { - xmlChar * name = xmlGetProp(xpath->node, BAD_CAST "name"); - xmlXPathObjectPtr result = xmlXPathEvalExpression(BAD_CAST "module", xpath); - xmlNodePtr node; + xmlChar * name = xmlGetProp(node, BAD_CAST "name"); + xmlNodePtr call; - xmlXPathFreeObject(result); + if (fname) { + fprintf(out, " tolua_function(tolua_S, \"%s\", %s);\n", name, fname); + } + xmlFree(name); +} + +static void +open_variable(xmlNodePtr node, FILE * out) +{ + xmlChar * name = xmlGetProp(node, BAD_CAST "name"); + xmlChar * getter = BAD_CAST "0"; + xmlChar * setter = BAD_CAST "0"; + fprintf(out, " tolua_variable(tolua_S, \"%s\", %s%s, %s%s);\n", name, prefix, getter, prefix, setter); + xmlFree(name); +} + +static void +open_method(xmlNodePtr node, FILE * out) +{ + xmlChar * name = xmlGetProp(node, BAD_CAST "name"); + xmlFree(name); +} + +static void +open_class(xmlNodePtr node, FILE * out) +{ + xmlChar * lname = xmlGetProp(node, BAD_CAST "name"); + xmlChar * name = xmlGetProp(node, BAD_CAST "ctype"); + xmlChar * base = xmlGetProp(node, BAD_CAST "base"); + const char * col = "NULL"; + fprintf(out, " tolua_cclass(tolua_S, \"%s\", \"%s\", \"%s\", %s);\n", + lname, name, base, col); + xmlFree(lname); + xmlFree(name); + xmlFree(base); +} + +static void +open_module(xmlNodePtr root, FILE * out) +{ + xmlNodePtr node; + xmlChar * name = xmlGetProp(root, BAD_CAST "name"); if (name) { fprintf(out, " tolua_module(tolua_S, \"%s\", 0);\n", name); fprintf(out, " tolua_beginmodule(tolua_S, \"%s\");\n", name); @@ -43,58 +84,61 @@ parse_module(xmlXPathContextPtr xpath, FILE * out) fputs(" tolua_beginmodule(tolua_S, 0);\n", out); } - for (node=xpath->node->children;node;node=node->next) { + for (node=root->children;node;node=node->next) { if (strcmp((const char *)node->name, "class")==0) { - xmlChar * lname = xmlGetProp(node, BAD_CAST "name"); - xmlChar * name = xmlGetProp(node, BAD_CAST "ctype"); - xmlChar * base = xmlGetProp(node, BAD_CAST "base"); - const char * col = "NULL"; - fprintf(out, " tolua_cclass(tolua_S, \"%s\", \"%s\", \"%s\", %s);\n", - lname, name, base, col); - xmlFree(lname); - xmlFree(name); - xmlFree(base); + open_class(node, out); } else if (strcmp((const char *)node->name, "module")==0) { - xpath->node = node; - parse_module(xpath, out); - xpath->node = node->parent; + open_module(node, out); + } else if (strcmp((const char *)node->name, "function")==0) { + open_function(node, out); + } else if (strcmp((const char *)node->name, "method")==0) { + open_method(node, out); + } else if (strcmp((const char *)node->name, "variable")==0) { + open_variable(node, out); } } fputs(" tolua_endmodule(tolua_S);\n", out); } -int -writefile(xmlDocPtr doc, FILE * out) +static void +open_type(xmlNodePtr root, FILE * out) { - xmlXPathContextPtr xpath = xmlXPathNewContext(doc); - xmlXPathObjectPtr result = xmlXPathEvalExpression(BAD_CAST "/package", xpath); - xmlChar * pkg_name = xmlGetProp(doc->children, BAD_CAST "name"); + xmlChar * name = xmlGetProp(root, BAD_CAST "name"); + fprintf(out, " tolua_usertype(tolua_S, \"%s\");\n", name); + xmlFree(name); +} - fputs(tmp_includes, out); - fputc('\n', out); - fprintf(out, "int tolua_%s_open(struct lua_State * L) {\n", pkg_name); +void +open_package(xmlNodePtr root, FILE * out) +{ + xmlNodePtr node; + xmlChar * pkg_name = xmlGetProp(root, BAD_CAST "name"); + fprintf(out, "int %s%s_open(struct lua_State * L) {\n", prefix, pkg_name); xmlFree(pkg_name); fputs(" tolua_open(L);\n", out); - result = xmlXPathEvalExpression(BAD_CAST "/package/type", xpath); - if (result->nodesetval!=NULL) { - int i; - xmlNodeSetPtr nodes = result->nodesetval; - for (i=0;i!=nodes->nodeNr;++i) { - xmlChar * name = xmlGetProp(nodes->nodeTab[i], BAD_CAST "name"); - fprintf(out, " tolua_usertype(tolua_S, \"%s\");\n", name); + for (node=root->children;node;node=node->next) { + if (strcmp((const char *)node->name, "type")==0) { + open_type(node, out); + } else if (strcmp((const char *)node->name, "module")==0) { + open_module(node, out); } } - xmlXPathFreeObject(result); - - xpath->node = doc->children; - parse_module(xpath, out); - + fputs(" return 0;\n", out); fputs("}\n", out); +} - xmlXPathFreeContext(xpath); +int +writefile(xmlDocPtr doc, FILE * out) +{ + /* includes etc. */ + fputs(tmp_includes, out); + fputc('\n', out); + /* functions */ + /* open */ + open_package(doc->children, out); return 0; } diff --git a/src/tools/codegen/codegen.vcproj b/src/tools/codegen/codegen.vcproj index 0fdf5fe54..17c01e332 100644 --- a/src/tools/codegen/codegen.vcproj +++ b/src/tools/codegen/codegen.vcproj @@ -191,6 +191,10 @@ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav" UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}" > + +