lua for the gmtool

This commit is contained in:
Enno Rehling 2006-04-16 12:48:55 +00:00
parent b74a0b6b50
commit 4316903e9a
9 changed files with 250 additions and 84 deletions

View File

@ -7,6 +7,9 @@ SubDirHdrs $(SUBDIR)/../common/util ;
SubDirHdrs $(SUBDIR)/../common ; SubDirHdrs $(SUBDIR)/../common ;
SubDirHdrs $(SUBDIR)/.. ; SubDirHdrs $(SUBDIR)/.. ;
UsingLua ;
UsingLuabind ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) curses ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) curses ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) lua ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) lua ] ;
SubDirC++Flags -DHAVE_LUA ; SubDirC++Flags -DHAVE_LUA ;
@ -39,22 +42,17 @@ LUASERVER_SOURCES =
GMTOOL_SOURCES = GMTOOL_SOURCES =
<curses>listbox.c <curses>listbox.c
gmtool.c console.c
editing.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. # old eressea server. use only for testing.
LinkLibraries $(SERVER) : LinkLibraries $(SERVER) :
gamecode kernel items modules attributes spells races triggers util ; gamecode kernel items modules attributes spells races triggers util ;
libxml2 $(SERVER) ; libxml2 $(SERVER) ;
LINKLIBS on $(SERVER) += -L$(LUABIND_ROOT)/lib -lm ; LINKLIBS on $(SERVER) += -lm ;
Main $(SERVER) : $(SERVER_SOURCES) ; Main $(SERVER) : $(SERVER_SOURCES) ;
# eressea-server with lua scripting # eressea-server with lua scripting
@ -63,7 +61,12 @@ LinkLibraries $(LUASERVER) :
luabind $(LUASERVER) ; luabind $(LUASERVER) ;
libxml2 $(LUASERVER) ; libxml2 $(LUASERVER) ;
LINKLIBS on $(LUASERVER) += -lm -ldl -lstdc++ ; LINKLIBS on $(LUASERVER) += -lm -ldl -lstdc++ ;
UsingLua ;
UsingLuabind ;
Main $(LUASERVER) : $(LUASERVER_SOURCES) ; 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) ;

View File

@ -16,9 +16,6 @@
** this macro defines a function to show the prompt and reads the ** this macro defines a function to show the prompt and reads the
** next line for manual input ** next line for manual input
*/ */
#ifndef lua_readline
#define lua_readline(L,prompt) readline(L,prompt)
/* maximum length of an input line */ /* maximum length of an input line */
#ifndef MAXINPUT #ifndef MAXINPUT
#define MAXINPUT 512 #define MAXINPUT 512
@ -39,7 +36,8 @@ readline(lua_State *l, const char *prompt)
return 1; return 1;
} }
} }
#endif
int (*lua_readline)(lua_State *l, const char *prompt) = readline;
#ifndef PROMPT #ifndef PROMPT
#define PROMPT "E> " #define PROMPT "E> "
@ -178,3 +176,22 @@ lua_console(lua_State * L)
fputs("\n", stdout); fputs("\n", stdout);
return 0; 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;
}

View File

@ -13,6 +13,8 @@ extern "C" {
#include <lua50/lua.h> #include <lua50/lua.h>
extern int lua_console(lua_State * L); 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 #ifdef __cplusplus
} }

73
src/eressea/gmmain.cpp Normal file
View File

@ -0,0 +1,73 @@
/* vi: set ts=2:
* +-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
* | | Enno Rehling <enno@eressea.de>
* | Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
* | (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 <config.h>
#include <eressea.h>
#include "console.h"
#include "gmtool.h"
/* lua includes */
#include "lua/bindings.h"
#include "lua/script.h"
#include <boost/version.hpp>
#include <lua.hpp>
#include <luabind/luabind.hpp>
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;
}

View File

@ -14,7 +14,9 @@
#include <eressea.h> #include <eressea.h>
#include "gmtool.h" #include "gmtool.h"
#include "gmtool_structs.h"
#include "editing.h" #include "editing.h"
#include "console.h"
#include "curses/listbox.h" #include "curses/listbox.h"
#include <modules/xmas.h> #include <modules/xmas.h>
@ -73,6 +75,8 @@ static int force_color = 0;
#define IFL_FACTIONS (1<<2) #define IFL_FACTIONS (1<<2)
#define IFL_BUILDINGS (1<<3) #define IFL_BUILDINGS (1<<3)
static WINDOW * hstatus;
static int static int
usage(const char * prog, const char * arg) usage(const char * prog, const char * arg)
{ {
@ -221,7 +225,7 @@ game_done(void)
static void static void
init_curses(void) init_curses(void)
{ {
int fg, bg; short fg, bg;
initscr(); initscr();
if (has_colors() || force_color) { if (has_colors() || force_color) {
@ -968,6 +972,11 @@ handlekey(state * st, int c)
} }
} while (c==0); } while (c==0);
break; break;
case 'L':
if (global.vm_state) {
lua_do((lua_State*)global.vm_state);
}
break;
case 'H': case 'H':
select_regions(st, MODE_HIGHLIGHT); select_regions(st, MODE_HIGHLIGHT);
break; break;
@ -1165,6 +1174,8 @@ run_mapper(void)
init_view(&st.display, hwinmap); init_view(&st.display, hwinmap);
coor2point(&st.display.topleft, &tl); coor2point(&st.display.topleft, &tl);
hstatus = st.wnd_status->handle; /* the lua console needs this */
while (!g_quit) { while (!g_quit) {
int c; int c;
point p; point p;
@ -1223,8 +1234,22 @@ run_mapper(void)
endwin(); endwin();
} }
#define MAXINPUT 512
int 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; int i;
char * lc_ctype; char * lc_ctype;
@ -1245,7 +1270,6 @@ main(int argc, char *argv[])
if (i!=0) return i; if (i!=0) return i;
game_init(); game_init();
if (turn>first_turn) { if (turn>first_turn) {
char datafile[12]; char datafile[12];
sprintf(datafile, "%u", turn); sprintf(datafile, "%u", turn);
@ -1267,3 +1291,11 @@ main(int argc, char *argv[])
return 0; return 0;
} }
#ifdef USE_C_MAIN
int
main(int argc, char *argv[])
{
return gmmain(argc, argv);
}
#endif

View File

@ -8,70 +8,19 @@
* *
*/ */
/* types imported from eressea: */ #ifndef H_GMTOOL
struct region; #define H_GMTOOL
typedef struct extent { #ifdef __cplusplus
/* Ein Vektor */ extern "C" {
int width, height; #endif
} extent;
typedef struct point { extern int gmmain(int argc, char *argv[]);
/* Eine Koordinate in einer Ascii-Karte */ extern int curses_readline(struct lua_State * L, const char * prompt);
int x, y;
} point;
typedef struct coordinate { #ifdef __cplusplus
/* Eine Koordinate in der Welt */ }
int x, y, p; #endif
} coordinate;
typedef struct map_region {
struct region * r;
coordinate coord;
} map_region;
typedef struct view { #endif
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 */

View File

@ -91,7 +91,6 @@
BasicRuntimeChecks="3" BasicRuntimeChecks="3"
RuntimeLibrary="4" RuntimeLibrary="4"
BufferSecurityCheck="TRUE" BufferSecurityCheck="TRUE"
DisableLanguageExtensions="TRUE"
UsePrecompiledHeader="0" UsePrecompiledHeader="0"
PrecompiledHeaderThrough="stdafx.h" PrecompiledHeaderThrough="stdafx.h"
PrecompiledHeaderFile=".\Debug/gmtool.pch" PrecompiledHeaderFile=".\Debug/gmtool.pch"
@ -108,7 +107,7 @@
<Tool <Tool
Name="VCLinkerTool" Name="VCLinkerTool"
AdditionalOptions="/MACHINE:I386" AdditionalOptions="/MACHINE:I386"
AdditionalDependencies="curses.lib libxml2.lib" AdditionalDependencies="lua50.lib curses.lib libxml2.lib"
OutputFile=".\Debug/gmtool.exe" OutputFile=".\Debug/gmtool.exe"
LinkIncremental="2" LinkIncremental="2"
SuppressStartupBanner="TRUE" SuppressStartupBanner="TRUE"
@ -216,13 +215,22 @@
<File <File
RelativePath=".\gmtool.h"> RelativePath=".\gmtool.h">
</File> </File>
<File
RelativePath=".\gmtool_structs.h">
</File>
<File <File
RelativePath=".\curses\listbox.h"> RelativePath=".\curses\listbox.h">
</File> </File>
</Filter> </Filter>
<File
RelativePath=".\console.c">
</File>
<File <File
RelativePath=".\editing.c"> RelativePath=".\editing.c">
</File> </File>
<File
RelativePath=".\gmmain.cpp">
</File>
<File <File
RelativePath=".\gmtool.c"> RelativePath=".\gmtool.c">
</File> </File>

View File

@ -0,0 +1,82 @@
/* vi: set ts=2:
* +-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
* | | Enno Rehling <enno@eressea.de>
* | Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
* | (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

View File

@ -286,7 +286,7 @@ readshortpwds()
} }
#endif #endif
lua_State * static lua_State *
lua_init(void) lua_init(void)
{ {
lua_State * luaState = lua_open(); lua_State * luaState = lua_open();
@ -312,7 +312,7 @@ lua_init(void)
return luaState; return luaState;
} }
void static void
lua_done(lua_State * luaState) lua_done(lua_State * luaState)
{ {
reset_scripts(); reset_scripts();