From 4316903e9ae27278fa427f114f72bf68d98efa54 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 16 Apr 2006 12:48:55 +0000 Subject: [PATCH] lua for the gmtool --- src/eressea/Jamfile | 25 ++++++----- src/eressea/console.c | 25 +++++++++-- src/eressea/console.h | 2 + src/eressea/gmmain.cpp | 73 ++++++++++++++++++++++++++++++++ src/eressea/gmtool.c | 38 +++++++++++++++-- src/eressea/gmtool.h | 73 +++++--------------------------- src/eressea/gmtool.vcproj | 12 +++++- src/eressea/gmtool_structs.h | 82 ++++++++++++++++++++++++++++++++++++ src/eressea/server.cpp | 4 +- 9 files changed, 250 insertions(+), 84 deletions(-) create mode 100644 src/eressea/gmmain.cpp create mode 100644 src/eressea/gmtool_structs.h diff --git a/src/eressea/Jamfile b/src/eressea/Jamfile index 23cf321da..6125995a2 100644 --- a/src/eressea/Jamfile +++ b/src/eressea/Jamfile @@ -7,6 +7,9 @@ SubDirHdrs $(SUBDIR)/../common/util ; SubDirHdrs $(SUBDIR)/../common ; SubDirHdrs $(SUBDIR)/.. ; +UsingLua ; +UsingLuabind ; + SEARCH_SOURCE += [ FDirName $(SUBDIR) curses ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) lua ] ; SubDirC++Flags -DHAVE_LUA ; @@ -39,22 +42,17 @@ LUASERVER_SOURCES = GMTOOL_SOURCES = listbox.c - gmtool.c + console.c editing.c + gmmain.cpp + gmtool.c ; -# the curses-based GM tool -Main $(GMTOOL) : $(GMTOOL_SOURCES) ; -LinkLibraries $(GMTOOL) : - kernel modules items attributes spells races triggers util ; -libxml2 $(GMTOOL) ; -LINKLIBS on $(GMTOOL) += -L$(LUABIND_ROOT)/lib -lm -lncurses ; - # old eressea server. use only for testing. LinkLibraries $(SERVER) : gamecode kernel items modules attributes spells races triggers util ; libxml2 $(SERVER) ; -LINKLIBS on $(SERVER) += -L$(LUABIND_ROOT)/lib -lm ; +LINKLIBS on $(SERVER) += -lm ; Main $(SERVER) : $(SERVER_SOURCES) ; # eressea-server with lua scripting @@ -63,7 +61,12 @@ LinkLibraries $(LUASERVER) : luabind $(LUASERVER) ; libxml2 $(LUASERVER) ; LINKLIBS on $(LUASERVER) += -lm -ldl -lstdc++ ; -UsingLua ; -UsingLuabind ; Main $(LUASERVER) : $(LUASERVER_SOURCES) ; +# the curses-based GM tool (now with luabind) +LinkLibraries $(GMTOOL) : + kernel modules items attributes spells races triggers util ; +luabind $(GMTOOL) ; +libxml2 $(GMTOOL) ; +LINKLIBS on $(GMTOOL) += -lm -lncurses ; +Main $(GMTOOL) : $(GMTOOL_SOURCES) ; diff --git a/src/eressea/console.c b/src/eressea/console.c index 081ba45f3..ace0f41a3 100644 --- a/src/eressea/console.c +++ b/src/eressea/console.c @@ -16,9 +16,6 @@ ** this macro defines a function to show the prompt and reads the ** next line for manual input */ -#ifndef lua_readline -#define lua_readline(L,prompt) readline(L,prompt) - /* maximum length of an input line */ #ifndef MAXINPUT #define MAXINPUT 512 @@ -39,7 +36,8 @@ readline(lua_State *l, const char *prompt) return 1; } } -#endif + +int (*lua_readline)(lua_State *l, const char *prompt) = readline; #ifndef PROMPT #define PROMPT "E> " @@ -178,3 +176,22 @@ lua_console(lua_State * L) fputs("\n", stdout); return 0; } + +int +lua_do(lua_State * L) +{ + int status = load_string(L); + if (status != -1) { + if (status == 0) status = lcall(L, 0, 0); + l_report(L, status); + if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ + lua_getglobal(L, "print"); + lua_insert(L, 1); + if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) + l_message(NULL, lua_pushfstring(L, "error calling `print' (%s)", + lua_tostring(L, -1))); + } + } + lua_settop(L, 0); /* clear stack */ + return 0; +} diff --git a/src/eressea/console.h b/src/eressea/console.h index b0f101d74..fcc53b0de 100644 --- a/src/eressea/console.h +++ b/src/eressea/console.h @@ -13,6 +13,8 @@ extern "C" { #include extern int lua_console(lua_State * L); + extern int lua_do(lua_State * L); + extern int (*lua_readline)(lua_State *l, const char *prompt); #ifdef __cplusplus } diff --git a/src/eressea/gmmain.cpp b/src/eressea/gmmain.cpp new file mode 100644 index 000000000..3d38549dd --- /dev/null +++ b/src/eressea/gmmain.cpp @@ -0,0 +1,73 @@ +/* vi: set ts=2: + * +-------------------+ Christian Schlittchen + * | | Enno Rehling + * | Eressea PBEM host | Katja Zedel + * | (c) 1998 - 2006 | + * | | This program may not be used, modified or distributed + * +-------------------+ without prior permission by the authors of Eressea. + * + */ + +/* wenn config.h nicht vor curses included wird, kompiliert es unter windows nicht */ +#include +#include + +#include "console.h" +#include "gmtool.h" + +/* lua includes */ +#include "lua/bindings.h" +#include "lua/script.h" +#include +#include +#include + +static lua_State * +lua_init(void) +{ + lua_State * L = lua_open(); + luaopen_base(L); + luaopen_math(L); + luaopen_string(L); + luaopen_io(L); + luaopen_table(L); +#if 0 + luabind::open(L); + bind_objects(L); + bind_eressea(L); + bind_script(L); + bind_spell(L); + bind_alliance(L); + bind_region(L); + bind_item(L); + bind_faction(L); + bind_unit(L); + bind_ship(L); + bind_building(L); + bind_event(L); + bind_message(L); +#endif + lua_readline = curses_readline; + return L; +} + +static void +lua_done(lua_State * L) +{ +#if 0 + reset_scripts(); +#endif + lua_close(L); +} + +int +main(int argc, char *argv[]) +{ + lua_State* luaState = lua_init(); + global.vm_state = luaState; + + int result = gmmain(argc, argv); + lua_done(luaState); + + return result; +} diff --git a/src/eressea/gmtool.c b/src/eressea/gmtool.c index 5e54312bf..cd7e24c87 100644 --- a/src/eressea/gmtool.c +++ b/src/eressea/gmtool.c @@ -14,7 +14,9 @@ #include #include "gmtool.h" +#include "gmtool_structs.h" #include "editing.h" +#include "console.h" #include "curses/listbox.h" #include @@ -73,6 +75,8 @@ static int force_color = 0; #define IFL_FACTIONS (1<<2) #define IFL_BUILDINGS (1<<3) +static WINDOW * hstatus; + static int usage(const char * prog, const char * arg) { @@ -221,7 +225,7 @@ game_done(void) static void init_curses(void) { - int fg, bg; + short fg, bg; initscr(); if (has_colors() || force_color) { @@ -968,6 +972,11 @@ handlekey(state * st, int c) } } while (c==0); break; + case 'L': + if (global.vm_state) { + lua_do((lua_State*)global.vm_state); + } + break; case 'H': select_regions(st, MODE_HIGHLIGHT); break; @@ -1165,6 +1174,8 @@ run_mapper(void) init_view(&st.display, hwinmap); coor2point(&st.display.topleft, &tl); + hstatus = st.wnd_status->handle; /* the lua console needs this */ + while (!g_quit) { int c; point p; @@ -1223,8 +1234,22 @@ run_mapper(void) endwin(); } +#define MAXINPUT 512 int -main(int argc, char *argv[]) +curses_readline(lua_State * L, const char * prompt) +{ + static char buffer[MAXINPUT]; + askstring(hstatus, prompt, buffer, MAXINPUT); + if (buffer[0]==0) { + return 0; /* read fails */ + } else { + lua_pushstring(L, buffer); + return 1; + } +} + +int +gmmain(int argc, char *argv[]) { int i; char * lc_ctype; @@ -1245,7 +1270,6 @@ main(int argc, char *argv[]) if (i!=0) return i; game_init(); - if (turn>first_turn) { char datafile[12]; sprintf(datafile, "%u", turn); @@ -1267,3 +1291,11 @@ main(int argc, char *argv[]) return 0; } + +#ifdef USE_C_MAIN +int +main(int argc, char *argv[]) +{ + return gmmain(argc, argv); +} +#endif diff --git a/src/eressea/gmtool.h b/src/eressea/gmtool.h index d5838841f..edeaab65f 100644 --- a/src/eressea/gmtool.h +++ b/src/eressea/gmtool.h @@ -8,70 +8,19 @@ * */ -/* types imported from eressea: */ -struct region; +#ifndef H_GMTOOL +#define H_GMTOOL -typedef struct extent { - /* Ein Vektor */ - int width, height; -} extent; +#ifdef __cplusplus +extern "C" { +#endif -typedef struct point { - /* Eine Koordinate in einer Ascii-Karte */ - int x, y; -} point; + extern int gmmain(int argc, char *argv[]); + extern int curses_readline(struct lua_State * L, const char * prompt); -typedef struct coordinate { - /* Eine Koordinate in der Welt */ - int x, y, p; -} coordinate; +#ifdef __cplusplus +} +#endif -typedef struct map_region { - struct region * r; - coordinate coord; -} map_region; -typedef struct view { - struct map_region * regions; - int plane; - coordinate topleft; /* upper left corner in map. */ - extent extent; /* dimensions. */ -} view; - -typedef struct tag { - coordinate coord; - struct tag * nexthash; -} tag; - -#define MAXTHASH 512 - -typedef struct selection { - tag * tags[MAXTHASH]; -} selection; - -typedef struct state { - coordinate cursor; - selection * selected; - struct state * undo; - struct faction * topf; - view display; - int modified; - unsigned int info_flags; - struct window * wnd_info; - struct window * wnd_map; - struct window * wnd_status; -} state; - -typedef struct window { - boolean (*handlekey)(struct window * win, struct state * st, int key); - void (*paint)(struct window * win, const struct state * st); - - WINDOW * handle; - struct window * next; - struct window * prev; - boolean initialized; - int update; -} window; - -#define TWIDTH 2 /* width of tile */ -#define THEIGHT 1 /* height of tile */ +#endif diff --git a/src/eressea/gmtool.vcproj b/src/eressea/gmtool.vcproj index df7314975..097246e2f 100644 --- a/src/eressea/gmtool.vcproj +++ b/src/eressea/gmtool.vcproj @@ -91,7 +91,6 @@ BasicRuntimeChecks="3" RuntimeLibrary="4" BufferSecurityCheck="TRUE" - DisableLanguageExtensions="TRUE" UsePrecompiledHeader="0" PrecompiledHeaderThrough="stdafx.h" PrecompiledHeaderFile=".\Debug/gmtool.pch" @@ -108,7 +107,7 @@ + + + + + + diff --git a/src/eressea/gmtool_structs.h b/src/eressea/gmtool_structs.h new file mode 100644 index 000000000..09cd391f2 --- /dev/null +++ b/src/eressea/gmtool_structs.h @@ -0,0 +1,82 @@ +/* vi: set ts=2: + * +-------------------+ Christian Schlittchen + * | | Enno Rehling + * | Eressea PBEM host | Katja Zedel + * | (c) 1998 - 2006 | + * | | This program may not be used, modified or distributed + * +-------------------+ without prior permission by the authors of Eressea. + * + */ + +#ifndef H_GMTOOL_STRUCTS +#define H_GMTOOL_STRUCTS + +/* types imported from eressea: */ +struct region; + +typedef struct extent { + /* Ein Vektor */ + int width, height; +} extent; + +typedef struct point { + /* Eine Koordinate in einer Ascii-Karte */ + int x, y; +} point; + +typedef struct coordinate { + /* Eine Koordinate in der Welt */ + int x, y, p; +} coordinate; + +typedef struct map_region { + struct region * r; + coordinate coord; +} map_region; + +typedef struct view { + struct map_region * regions; + int plane; + coordinate topleft; /* upper left corner in map. */ + extent extent; /* dimensions. */ +} view; + +typedef struct tag { + coordinate coord; + struct tag * nexthash; +} tag; + +#define MAXTHASH 512 + +typedef struct selection { + tag * tags[MAXTHASH]; +} selection; + +typedef struct state { + coordinate cursor; + selection * selected; + struct state * undo; + struct faction * topf; + view display; + int modified; + unsigned int info_flags; + struct window * wnd_info; + struct window * wnd_map; + struct window * wnd_status; +} state; + +typedef struct window { + boolean (*handlekey)(struct window * win, struct state * st, int key); + void (*paint)(struct window * win, const struct state * st); + + WINDOW * handle; + struct window * next; + struct window * prev; + boolean initialized; + int update; +} window; + +#define TWIDTH 2 /* width of tile */ +#define THEIGHT 1 /* height of tile */ + +#endif diff --git a/src/eressea/server.cpp b/src/eressea/server.cpp index abde15336..6e8da46cb 100644 --- a/src/eressea/server.cpp +++ b/src/eressea/server.cpp @@ -286,7 +286,7 @@ readshortpwds() } #endif -lua_State * +static lua_State * lua_init(void) { lua_State * luaState = lua_open(); @@ -312,7 +312,7 @@ lua_init(void) return luaState; } -void +static void lua_done(lua_State * luaState) { reset_scripts();