From 2b15c42ecae40972bacfdb1d0b331fba9d635c64 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 30 Nov 2009 02:31:14 +0000 Subject: [PATCH] - Eressea now uses lunit for unit testing - several command line parameters have changed - .ini files simplified --- src/common/gamecode/economy.c | 2 +- src/common/gamecode/randenc.c | 5 +- src/common/kernel.vcproj | 4 + src/common/kernel/alliance.c | 2 +- src/common/kernel/eressea.c | 16 +- src/common/kernel/eressea.h | 1 - src/common/kernel/save.c | 36 +- src/common/kernel/save.h | 4 +- src/eressea.vcproj | 19 +- src/eressea/console.c | 271 ++++++----- src/eressea/console.h | 2 +- src/eressea/gmtool.c | 15 +- src/eressea/gmtool.h | 2 +- src/eressea/server.c | 128 ++--- src/eressea/tolua/bind_region.c | 13 +- src/eressea/tolua/bindings.c | 22 +- src/external/lunit/lunit | 128 ----- src/scripts/e3a/config-test.lua | 1 + src/scripts/e3a/rules.lua | 1 - src/scripts/eressea.lua | 4 +- src/scripts/init-e3a.lua | 42 ++ src/scripts/run-e3a.lua | 35 +- src/scripts/{tests.lua => tests/common.lua} | 496 ++++---------------- src/scripts/tests/e3a.lua | 259 ++++++++++ 24 files changed, 635 insertions(+), 873 deletions(-) delete mode 100644 src/external/lunit/lunit create mode 100644 src/scripts/e3a/config-test.lua create mode 100644 src/scripts/init-e3a.lua rename src/scripts/{tests.lua => tests/common.lua} (54%) create mode 100644 src/scripts/tests/e3a.lua diff --git a/src/common/gamecode/economy.c b/src/common/gamecode/economy.c index c46b77fb2..7806e7ab2 100644 --- a/src/common/gamecode/economy.c +++ b/src/common/gamecode/economy.c @@ -3137,7 +3137,7 @@ expandwork(region * r, request * work_begin, request * work_end, int maxwork) if (!blessedharvest_ct) { blessedharvest_ct = ct_find("blessedharvest"); } - if (blessedharvest_ct) { + if (blessedharvest_ct && r->attribs) { int happy = (int)curse_geteffect(get_curse(r->attribs, blessedharvest_ct)); happy = MIN(happy, jobs); earnings += happy; diff --git a/src/common/gamecode/randenc.c b/src/common/gamecode/randenc.c index f38c6e6e0..8450650eb 100644 --- a/src/common/gamecode/randenc.c +++ b/src/common/gamecode/randenc.c @@ -246,14 +246,15 @@ find_manual(region * r, unit * u) static void get_villagers(region * r, unit * u) { - unit *newunit; + unit *newunit; message * msg = msg_message("encounter_villagers", "unit", u); const char * name = LOC(u->faction->locale, "villagers"); r_addmessage(r, u->faction, msg); msg_release(msg); - newunit = create_unit(r, u->faction, rng_int() % 20 + 3, u->faction->race, 0, name, u); + newunit = create_unit(r, u->faction, rng_int() % 20 + 3, u->faction->race, 0, name, u); + leave(newunit, true); fset(newunit, UFL_ISNEW|UFL_MOVED); equip_unit(newunit, get_equipment("random_villagers")); } diff --git a/src/common/kernel.vcproj b/src/common/kernel.vcproj index 93e7d2ea8..7ab5fc7c6 100644 --- a/src/common/kernel.vcproj +++ b/src/common/kernel.vcproj @@ -352,6 +352,10 @@ RelativePath=".\kernel\karma.h" > + + diff --git a/src/common/kernel/alliance.c b/src/common/kernel/alliance.c index 80d8a203f..a5292d910 100644 --- a/src/common/kernel/alliance.c +++ b/src/common/kernel/alliance.c @@ -278,7 +278,7 @@ perform_join(void) alliance_transaction * ti = *tip; while (ti) { faction * fi = ti->u->faction; - if (fi->alliance==al) { + if (fi && fi->alliance==al) { int fid; init_tokens(ti->ord); skip_token(); diff --git a/src/common/kernel/eressea.c b/src/common/kernel/eressea.c index 43473a97c..308868523 100644 --- a/src/common/kernel/eressea.c +++ b/src/common/kernel/eressea.c @@ -590,7 +590,6 @@ skill_limit(faction * f, skill_t sk) } char * g_basedir; -char * g_resourcedir; const char * basepath(void) @@ -599,14 +598,6 @@ basepath(void) return "."; } -const char * -resourcepath(void) -{ - static char zText[MAX_PATH]; - if (g_resourcedir) return g_resourcedir; - return strcat(strcpy(zText, basepath()), "/res"); -} - int count_skill(faction * f, skill_t sk) { @@ -2212,12 +2203,12 @@ int init_data(const char * filename) { int l; - char zText[80]; - sprintf(zText, "%s/%s", resourcepath(), filename); - l = read_xml(zText); + l = read_xml(filename); if (l) return l; + init_locales(); + if (turn<0) { turn = first_turn; } @@ -3098,7 +3089,6 @@ kernel_init(void) attrib_init(); translation_init(); - if (turn<0) turn = lastturn(); if (sqlpatch) { sprintf(zBuffer, "%s/patch-%d.sql", datapath(), turn); sql_init(zBuffer); diff --git a/src/common/kernel/eressea.h b/src/common/kernel/eressea.h index 953677632..85f12e7ef 100644 --- a/src/common/kernel/eressea.h +++ b/src/common/kernel/eressea.h @@ -356,7 +356,6 @@ enum { extern int movewhere(const struct unit *u, const char * token, struct region * r, struct region** resultp); extern const char * basepath(void); -extern const char * resourcepath(void); extern void kernel_init(void); extern void kernel_done(void); diff --git a/src/common/kernel/save.c b/src/common/kernel/save.c index 1587c5a26..d661b99dc 100644 --- a/src/common/kernel/save.c +++ b/src/common/kernel/save.c @@ -89,7 +89,7 @@ #define MAXPERSISTENT 128 /* exported symbols symbols */ -const char * xmlfile = "eressea.xml"; +const char * game_name = "eressea"; const char * g_datadir; int firstx = 0, firsty = 0; int enc_gamedata = 0; @@ -346,7 +346,7 @@ factionorders(void) /* Die Partei hat sich zumindest gemeldet, so daß sie noch * nicht als untätig gilt */ - /* TODO: +1 ist ein Workaround, weil turn erst in process_orders + /* TODO: +1 ist ein Workaround, weil cturn erst in process_orders * incrementiert wird. */ f->lastorders = global.data_turn+1; @@ -656,19 +656,19 @@ read_owner(struct storage * store, region_owner **powner) } int -lastturn(void) +current_turn(void) { char zText[MAX_PATH]; - int turn = 0; + int cturn = 0; FILE * f; sprintf(zText, "%s/turn", basepath()); f = cfopen(zText, "r"); if (f) { - fscanf(f, "%d\n", &turn); + fscanf(f, "%d\n", &cturn); fclose(f); } - return turn; + return cturn; } static void @@ -1397,20 +1397,16 @@ readgame(const char * filename, int mode, int backup) if (store->version >= SAVEXMLNAME_VERSION) { char basefile[1024]; - const char *basearg = "(null)"; store->r_str_buf(store, basefile, sizeof(basefile)); - assert(xmlfile != NULL); - basearg = strrchr(xmlfile, '/'); - if (basearg==NULL) { - basearg = xmlfile; - } else { - ++basearg; - } - if (strcmp(basearg, basefile)!=0) { - log_warning(("xmlfile mismatch: datafile contains %s, argument/default is %s\n", basefile, basearg)); - printf("WARNING: any key to continue, Ctrl-C to stop\n"); - getchar(); + if (strcmp(game_name, basefile)!=0) { + char buffer[64]; + snprintf(buffer, sizeof(buffer), "%s.xml", game_name); + if (strcmp(basefile, buffer)!=0) { + log_warning(("game mismatch: datafile contains %s, game is %s\n", basefile, game_name)); + printf("WARNING: any key to continue, Ctrl-C to stop\n"); + getchar(); + } } } a_read(store, &global.attribs); @@ -1676,11 +1672,11 @@ writegame(const char *filename, int mode) /* globale Variablen */ - base = strrchr(xmlfile, '/'); + base = strrchr(game_name, '/'); if (base) { store->w_str(store, base+1); } else { - store->w_str(store, xmlfile); + store->w_str(store, game_name); } store->w_brk(store); diff --git a/src/common/kernel/save.h b/src/common/kernel/save.h index a0565184f..07749e0e1 100644 --- a/src/common/kernel/save.h +++ b/src/common/kernel/save.h @@ -43,11 +43,11 @@ extern void rsf(FILE * F, char *s, size_t len); /* Versionsänderungen: */ extern int data_version; -extern const char *xmlfile; +extern const char *game_name; extern int enc_gamedata; extern void init_locales(void); -extern int lastturn(void); +extern int current_turn(void); extern void read_items(struct storage * store, struct item **it); extern void write_items(struct storage * store, struct item *it); diff --git a/src/eressea.vcproj b/src/eressea.vcproj index 9ca64273e..ee0a17f1f 100644 --- a/src/eressea.vcproj +++ b/src/eressea.vcproj @@ -259,7 +259,7 @@ > @@ -272,7 +272,7 @@ > @@ -285,20 +285,7 @@ > - - - - - diff --git a/src/eressea/console.c b/src/eressea/console.c index 4d650d70f..33c461730 100644 --- a/src/eressea/console.c +++ b/src/eressea/console.c @@ -1,3 +1,4 @@ +#include #include "console.h" /* lua includes */ @@ -18,36 +19,39 @@ ** next line for manual input */ /* maximum length of an input line */ -#ifndef MAXINPUT -#define MAXINPUT 512 +#ifndef LUA_MAXINPUT +#define LUA_MAXINPUT 512 #endif -static int -stdin_readline(lua_State *l, const char *prompt) +#if defined(LUA_USE_READLINE) +#include +#include +#include +#define default_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) +#define lua_saveline(L,idx) \ + if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ + add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_freeline(L,b) ((void)L, free(b)) +#else +#define default_readline(L,b,p) \ + ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ + fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ +#define lua_saveline(L,idx) { (void)L; (void)idx; } +#define lua_freeline(L,b) { (void)L; (void)b; } +#endif + +static int (*my_readline)(lua_State *l, char *, size_t, const char *prompt) = NULL; +static int lua_readline(lua_State *l, char *b, const char *prompt) { - static char buffer[MAXINPUT]; - if (prompt) { - fputs(prompt, stdout); - fflush(stdout); - } - if (fgets(buffer, sizeof(buffer), stdin) == NULL) - return 0; /* read fails */ - else { - lua_pushstring(l, buffer); - return 1; + if (my_readline) { + return my_readline(l, b, LUA_MAXINPUT, prompt); + } else { + return default_readline(l, b, prompt); } } -static int (*lua_readline)(lua_State *l, const char *prompt) = stdin_readline; - -void -set_readline(readline foo) -{ - if (foo) { - lua_readline = foo; - } else { - lua_readline = stdin_readline; - } +void set_readline(readline foo) { + my_readline = foo; } #ifndef PROMPT @@ -55,9 +59,84 @@ set_readline(readline foo) #endif #ifndef PROMPT2 -#define PROMPT2 "E>> " +#define PROMPT2 ".. " #endif +/* BAD hack, all this action stuff. */ +#define STATESTACK_MAX 16 +static lua_State * state_stack[STATESTACK_MAX]; +int state_stack_top = -1; + +static const char *progname = "eressea"; + +static void +lstop(lua_State *l, lua_Debug *ar) +{ + (void)ar; /* unused arg. */ + lua_sethook(l, NULL, 0, 0); + luaL_error(l, "interrupted!"); +} + +static void +laction(int i) +{ + signal(i, SIG_DFL); /* if another SIGINT happens before lstop, + terminate process (default action) */ + assert(state_stack_top>=0 && state_stack_top 0 && b[l-1] == '\n') /* line ends with newline? */ + b[l-1] = '\0'; /* remove it */ + if (firstline && b[0] == '=') /* first line starts with `=' ? */ + lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ + else + lua_pushstring(L, b); + lua_freeline(L, b); + return 1; } -static int -l_report(lua_State * L, int status) -{ - const char *msg; - if (status) { - msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error with no message)"; - l_message(NULL, msg); - lua_pop(L, 1); - } - return status; -} -/* -** this macro can be used by some `history' system to save lines -** read in manual input -*/ -#ifndef lua_saveline -#define lua_saveline(L,line) /* empty */ -#endif - -static int -load_string(lua_State * L) -{ +static int loadline (lua_State *L) { int status; lua_settop(L, 0); - if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */ - return -1; - if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */ - lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */ - lua_remove(L, -2); /* remove original line */ - } + if (!pushline(L, 1)) + return -1; /* no input */ for (;;) { /* repeat until gets a complete line */ status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */ + if (!pushline(L, 0)) /* no more input? */ return -1; - lua_concat(L, lua_gettop(L)); /* join lines */ + lua_pushliteral(L, "\n"); /* add a new line... */ + lua_insert(L, -2); /* ...between the two lines */ + lua_concat(L, 3); /* join them */ } - lua_saveline(L, lua_tostring(L, 1)); + lua_saveline(L, 1); lua_remove(L, 1); /* remove line */ return status; } -static void -lstop(lua_State *l, lua_Debug *ar) -{ - (void)ar; /* unused arg. */ - lua_sethook(l, NULL, 0, 0); - luaL_error(l, "interrupted!"); -} - -/* BAD hack, all this action stuff. */ -#define STATESTACK_MAX 16 -static lua_State * state_stack[STATESTACK_MAX]; -int state_stack_top = -1; - - -static void -laction(int i) -{ - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ - assert(state_stack_top>=0 && state_stack_top 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(progname, lua_pushfstring(L, + "error calling " LUA_QL("print") " (%s)", + lua_tostring(L, -1))); + } } - signal(SIGINT, laction); - status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); - signal(SIGINT, SIG_DFL); - state_stack_top = pop_state; - lua_remove(L, base); /* remove traceback function */ - return status; + lua_settop(L, 0); /* clear stack */ + fputs("\n", stdout); + fflush(stdout); + progname = oldprogname; } int lua_console(lua_State * L) { - int status; - while ((status = load_string(L)) != -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 */ - fputs("\n", stdout); + dotty(L); return 0; } int lua_do(lua_State * L) { - int status = load_string(L); + int status = loadline(L); if (status != -1) { - if (status == 0) status = lcall(L, 0, 0); - l_report(L, status); + if (status == 0) status = docall(L, 0, 0); + report(L, status); if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ lua_getglobal(L, "print"); lua_insert(L, 1); diff --git a/src/eressea/console.h b/src/eressea/console.h index f812dee5a..4c41e5a58 100644 --- a/src/eressea/console.h +++ b/src/eressea/console.h @@ -16,7 +16,7 @@ extern "C" { extern int lua_console(struct lua_State * L); extern int lua_do(struct lua_State * L); - typedef int (*readline)(struct lua_State *, const char *); + typedef int (*readline)(struct lua_State *, char *, size_t, const char *); extern void set_readline(readline foo); #ifdef __cplusplus diff --git a/src/eressea/gmtool.c b/src/eressea/gmtool.c index 1e199f959..654972e15 100644 --- a/src/eressea/gmtool.c +++ b/src/eressea/gmtool.c @@ -72,7 +72,6 @@ extern const char * g_reportdir; extern const char * g_datadir; extern const char * g_basedir; -extern const char * g_resourcedir; static int g_quit; int force_color = 0; @@ -1254,16 +1253,10 @@ run_mapper(void) state_close(st); } -#define MAXINPUT 512 int -curses_readline(struct lua_State * L, const char * prompt) +curses_readline(struct lua_State * L, char * buffer, size_t size, 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; - } + unused(L); + askstring(hstatus, prompt, buffer, size); + return buffer[0]!=0; } diff --git a/src/eressea/gmtool.h b/src/eressea/gmtool.h index 60833361f..daa99f27b 100644 --- a/src/eressea/gmtool.h +++ b/src/eressea/gmtool.h @@ -20,7 +20,7 @@ extern "C" { struct region; extern int gmmain(int argc, char *argv[]); - extern int curses_readline(struct lua_State * L, const char * prompt); + extern int curses_readline(struct lua_State * L, char * buffer, size_t size, const char * prompt); extern void highlight_region(struct region *r, int on); extern void select_coordinate(struct selection * selected, int x, int y, int on); diff --git a/src/eressea/server.c b/src/eressea/server.c index 5393851e5..192876491 100644 --- a/src/eressea/server.c +++ b/src/eressea/server.c @@ -152,7 +152,6 @@ extern const char * g_reportdir; extern const char * g_datadir; extern const char * g_basedir; - extern const char * g_resourcedir; extern boolean nobattle; extern boolean nomonsters; @@ -169,10 +168,8 @@ static char * orders = NULL; static int nowrite = 0; static boolean g_writemap = false; static boolean g_ignore_errors = false; -static const char * luafile = NULL; -static const char * preload = NULL; -static const char * lua_path = NULL; -static const char * script_path = "scripts"; +static const char * luafile = "init.lua"; +static const char * entry_point = NULL; static int memdebug = 0; static int g_console = 1; #if defined(HAVE_SIGACTION) && defined(HAVE_EXECINFO) @@ -246,9 +243,8 @@ game_init(void) register_archetypes(); enable_xml_gamecode(); - init_data(xmlfile); + /* init_data(game_name); */ - init_locales(); init_archetypes(); init_attributes(); init_itemtypes(); @@ -269,9 +265,7 @@ static const struct { {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_LOADLIBNAME, luaopen_package}, -/* {LUA_DBLIBNAME, luaopen_debug}, -*/ #if LUA_VERSION_NUM>=501 {LUA_OSLIBNAME, luaopen_os}, #endif @@ -453,20 +447,18 @@ read_args(int argc, char **argv, lua_State * luaState) nocr = true; nocr = true; } - else if (strcmp(argv[i]+2, "xml")==0) xmlfile = argv[++i]; + else if (strcmp(argv[i]+2, "xml")==0) game_name = argv[++i]; else if (strcmp(argv[i]+2, "ignore-errors")==0) g_ignore_errors = true; else if (strcmp(argv[i]+2, "nonr")==0) nonr = true; else if (strcmp(argv[i]+2, "lomem")==0) lomem = true; else if (strcmp(argv[i]+2, "nobattle")==0) nobattle = true; else if (strcmp(argv[i]+2, "nomonsters")==0) nomonsters = true; else if (strcmp(argv[i]+2, "nodebug")==0) battledebug = false; - else if (strcmp(argv[i]+2, "console")==0) luafile=NULL; + else if (strcmp(argv[i]+2, "console")==0) entry_point=NULL; else if (strcmp(argv[i]+2, "crabsolute")==0) opt_cr_absolute_coords = true; else if (strcmp(argv[i]+2, "color")==0) { /* force the editor to have colors */ force_color = 1; - } else if (strcmp(argv[i]+2, "current")==0) { - turn = -1; } else if (strcmp(argv[i]+2, "help")==0) return usage(argv[0], NULL); @@ -475,26 +467,23 @@ read_args(int argc, char **argv, lua_State * luaState) } else switch(argv[i][1]) { case 'C': g_console = 1; - luafile=NULL; + entry_point = NULL; break; case 'R': g_reportdir = argv[++i]; break; case 'e': g_console = 0; - luafile = argv[++i]; + entry_point = argv[++i]; break; case 'd': g_datadir = argv[++i]; break; - case 'r': - g_resourcedir = argv[++i]; - break; case 'b': g_basedir = argv[++i]; break; case 'i': - xmlfile = argv[++i]; + game_name = argv[++i]; break; case 't': g_console = 0; @@ -532,9 +521,6 @@ read_args(int argc, char **argv, lua_State * luaState) setLuaNumber(luaState, argv[i], atof(c)); } break; - case 'l': - script_path = argv[++i]; - break; case 'w': g_writemap = true; break; @@ -543,40 +529,12 @@ read_args(int argc, char **argv, lua_State * luaState) } } - if (orders==NULL) { - static char orderfile[MAX_PATH]; - sprintf(orderfile, "%s/orders.%d", basepath(), turn); - orders = orderfile; + if (orders!=NULL) { + setLuaString(luaState, "orderfile", orders); } - /* add some more variables to the lua globals */ - if (script_path) { - char str[512]; - sprintf(str, "?;?.lua;%s/?.lua;%s/?", script_path, script_path); - setLuaString(luaState, "LUA_PATH", str); - } - if (lua_path) { - char buffer[512]; - const char * str; - int t; - lua_getglobal(luaState, "package"); - t = lua_type(luaState, -1); - lua_pushstring(luaState, "path"); - lua_gettable(luaState, -2); - t = lua_type(luaState, -1); - str = lua_tostring(luaState, -1); - lua_pop(luaState, 1); - sprintf(buffer, "%s;%s", str, lua_path); - lua_pushstring(luaState, "path"); - lua_pushstring(luaState, buffer); - lua_settable(luaState, -3); - } - setLuaString(luaState, "datapath", datapath()); - setLuaString(luaState, "scriptpath", script_path); setLuaString(luaState, "basepath", basepath()); setLuaString(luaState, "reportpath", reportpath()); - setLuaString(luaState, "resourcepath", resourcepath()); - setLuaString(luaState, "orderfile", orders); return 0; } @@ -599,30 +557,26 @@ load_inifile(const char * filename) dictionary * d = iniparser_new(filename); if (d) { const char * str; - g_basedir = iniparser_getstring(d, "common:base", g_basedir); - g_resourcedir = iniparser_getstring(d, "common:res", g_resourcedir); - xmlfile = iniparser_getstring(d, "common:xml", xmlfile); - script_path = iniparser_getstring(d, "common:scripts", script_path); - lomem = iniparser_getint(d, "common:lomem", lomem)?1:0; - memdebug = iniparser_getint(d, "common:memcheck", memdebug); + g_basedir = iniparser_getstring(d, "eressea:base", g_basedir); + game_name = iniparser_getstring(d, "eressea:game", game_name); + lomem = iniparser_getint(d, "eressea:lomem", lomem)?1:0; + memdebug = iniparser_getint(d, "eressea:memcheck", memdebug); - lua_path = iniparser_getstring(d, "common:luapath", lua_path); - - str = iniparser_getstring(d, "common:encoding", NULL); + str = iniparser_getstring(d, "eressea:encoding", NULL); if (str) enc_gamedata = xmlParseCharEncoding(str); verbosity = iniparser_getint(d, "eressea:verbose", 2); sqlpatch = iniparser_getint(d, "eressea:sqlpatch", false); battledebug = iniparser_getint(d, "eressea:debug", battledebug)?1:0; - preload = iniparser_getstring(d, "eressea:preload", preload); - luafile = iniparser_getstring(d, "eressea:run", luafile); + entry_point = iniparser_getstring(d, "eressea:run", entry_point); + luafile = iniparser_getstring(d, "eressea:load", luafile); g_reportdir = iniparser_getstring(d, "eressea:report", g_reportdir); /* editor settings */ force_color = iniparser_getint(d, "editor:color", force_color); - str = iniparser_getstring(d, "common:locales", "de,en"); + str = iniparser_getstring(d, "eressea:locales", "de,en"); make_locales(str); } inifile = d; @@ -719,48 +673,22 @@ main(int argc, char *argv[]) write_spells(); } /* run the main script */ - if (preload!=NULL) { - char * tokens = strdup(preload); - char * filename = strtok(tokens, ":"); - while (filename) { - char buf[MAX_PATH]; - if (script_path) { - sprintf(buf, "%s/%s", script_path, filename); - } - else strcpy(buf, filename); - lua_getglobal(L, "dofile"); - lua_pushstring(L, buf); - if (lua_pcall(L, 1, 0, 0) != 0) { - my_lua_error(L); - } - filename = strtok(NULL, ":"); - } - } - if (g_console) lua_console(L); - else if (luafile) { + if (luafile) { char buf[MAX_PATH]; - if (script_path) { - sprintf(buf, "%s/%s", script_path, luafile); - } - else strcpy(buf, luafile); -#ifdef BINDINGS_LUABIND - try { - luabind::call_function(L, "dofile", buf); - } - catch (std::runtime_error& rte) { - log_error(("%s.\n", rte.what())); - } - catch (luabind::error& e) { - lua_State* L = e.state(); - my_lua_error(L); - } -#elif defined(BINDINGS_TOLUA) + strcpy(buf, luafile); lua_getglobal(L, "dofile"); lua_pushstring(L, buf); if (lua_pcall(L, 1, 0, 0) != 0) { my_lua_error(L); } -#endif + } + if (entry_point) { + lua_getglobal(L, entry_point); + if (lua_pcall(L, 0, 1, 0) != 0) { + my_lua_error(L); + } + } else { + lua_console(L); } #ifdef MSPACES malloc_stats(); diff --git a/src/eressea/tolua/bind_region.c b/src/eressea/tolua/bind_region.c index 62b2fa851..2268fb245 100644 --- a/src/eressea/tolua/bind_region.c +++ b/src/eressea/tolua/bind_region.c @@ -352,15 +352,10 @@ tolua_region_create(lua_State* L) assert(!pnormalize(&x, &y, pl)); r = result = findregion(x, y); - if (terrain==NULL) { - if (r!=NULL) { - if (r->units!=NULL) { - /* TODO: error message */ - result = NULL; - } - } - } - if (r==NULL) { + if (terrain==NULL && r!=NULL && r->units!=NULL) { + /* TODO: error message */ + result = NULL; + } else if (r==NULL) { result = new_region(x, y, pl, 0); } if (result) { diff --git a/src/eressea/tolua/bindings.c b/src/eressea/tolua/bindings.c index 3a0148124..9aaa5505b 100644 --- a/src/eressea/tolua/bindings.c +++ b/src/eressea/tolua/bindings.c @@ -269,6 +269,13 @@ tolua_update_guards(lua_State * L) return 0; } +static int +tolua_set_turn(lua_State * L) +{ + turn = (int)tolua_tonumber(L, 1, 0); + return 0; +} + static int tolua_get_turn(lua_State * L) { @@ -591,6 +598,7 @@ tolua_process_orders(lua_State* L) processorders(); return 0; } + static int tolua_write_passwords(lua_State* L) { @@ -666,6 +674,14 @@ tolua_read_game(lua_State* L) return 1; } +static int +tolua_read_turn(lua_State* L) +{ + int cturn = current_turn(); + tolua_pushnumber(L, (lua_Number)cturn); + return 1; +} + static int tolua_get_faction(lua_State* L) { @@ -933,7 +949,7 @@ int tolua_read_xml(lua_State* L) { const char * filename = tolua_tostring(L, 1, 0); - read_xml(filename); + init_data(filename); return 0; } @@ -988,6 +1004,7 @@ tolua_eressea_open(lua_State* L) tolua_function(L, TOLUA_CAST "factions", tolua_get_factions); tolua_function(L, TOLUA_CAST "regions", tolua_get_regions); + tolua_function(L, TOLUA_CAST "read_turn", tolua_read_turn); tolua_function(L, TOLUA_CAST "read_game", tolua_read_game); tolua_function(L, TOLUA_CAST "write_game", tolua_write_game); tolua_function(L, TOLUA_CAST "free_game", tolua_free_game); @@ -1022,7 +1039,8 @@ tolua_eressea_open(lua_State* L) tolua_function(L, TOLUA_CAST "update_guards", tolua_update_guards); - tolua_function(L, TOLUA_CAST "get_turn", tolua_get_turn); + tolua_function(L, TOLUA_CAST "set_turn", &tolua_set_turn); + tolua_function(L, TOLUA_CAST "get_turn", &tolua_get_turn); tolua_function(L, TOLUA_CAST "get_season", tolua_get_season); tolua_function(L, TOLUA_CAST "equipment_setitem", tolua_equipment_setitem); diff --git a/src/external/lunit/lunit b/src/external/lunit/lunit deleted file mode 100644 index 6bc010abd..000000000 --- a/src/external/lunit/lunit +++ /dev/null @@ -1,128 +0,0 @@ -#! /bin/sh - -# This file is part of lunit 0.5. -# -# For Details about lunit look at: http://www.mroth.net/lunit/ -# -# Author: Michael Roth -# -# Copyright (c) 2004-2009 Michael Roth -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - -if test $# = 0 ; then - echo "$0: Usage Error. Try $0 --help" >&2 - exit 1 -fi - -if [ `uname` = "Darwin" ]; then - scriptname="$(readlink -n "$0")" -else - scriptname="$(readlink -n -f "$0")" -fi -interpreter="lua" -options="" - -while true ; do - case "$1" in - -h|--help) - cat < -This program comes WITHOUT WARRANTY OF ANY KIND. - -Usage: lunit [OPTIONS] [--] scripts - -Options: - - -i, --interpreter LUA Complete path of the lua binary to use. - -p, --path PATH Sets the LUA_PATH environment for the tests. - --cpath CPATH Sets the LUA_CPATH environment for the tests. - -r, --runner RUNNER Testrunner to use, defaults to 'lunit-console'. - -t, --test PATTERN Which tests to run, may contain * or ? wildcards. - --loadonly Only load the tests. - --dontforce Do not force to load $scriptname*.lua. - -h, --help Print this help screen. - --version Print lunit version. - -Please report bugs to . -EOT - exit ;; - - --version) - echo "lunit 0.5 Copyright 2004-2009 Michael Roth " - exit ;; - - -i|--interpreter) - interpreter="$2" - shift 2 ;; - - -p|--path) - LUA_PATH="$2" - export LUA_PATH - shift 2 ;; - - --cpath) - LUA_CPATH="$2" - export LUA_CPATH - shift 2 ;; - - --loadonly) - options="$options $1" - shift 1 ;; - - --dontforce) - scriptname="" - shift 1 ;; - - -r|--runner|-t|--test) - options="$options $1 $2" - shift 2 ;; - - --) - break ;; - - -*) - echo "$0: Invalid option: $1" >&2 - exit 1 ;; - - *) - break ;; - esac -done - - -exec "$interpreter" - "$scriptname" $options "$@" < 0 or stats.failed > 0 then - os.exit(1) - end -EOT diff --git a/src/scripts/e3a/config-test.lua b/src/scripts/e3a/config-test.lua new file mode 100644 index 000000000..67bb64082 --- /dev/null +++ b/src/scripts/e3a/config-test.lua @@ -0,0 +1 @@ +maxnmrs = 500 diff --git a/src/scripts/e3a/rules.lua b/src/scripts/e3a/rules.lua index 709065466..9e581e2eb 100644 --- a/src/scripts/e3a/rules.lua +++ b/src/scripts/e3a/rules.lua @@ -1,4 +1,3 @@ -print("loaded rules.lua") -- when appending to this, make sure the item has a canuse-function! local goblin_denied = " plate lance mallornlance greatbow axe greatsword halberd rustyaxe rustyhalberd towershield " function item_canuse(u, iname) diff --git a/src/scripts/eressea.lua b/src/scripts/eressea.lua index cf58d5be3..0a5b14073 100644 --- a/src/scripts/eressea.lua +++ b/src/scripts/eressea.lua @@ -72,9 +72,7 @@ function process(orders) local nmrs = get_nmrs(1) -- nmrs = 0 - if maxnmrs == nil then - maxnmrs = 80 - end + maxnmrs = maxnmrs or 80 if nmrs >= maxnmrs then print("Shit. More than " .. maxnmrs .. " factions with 1 NMR (" .. nmrs .. ")") write_summary() diff --git a/src/scripts/init-e3a.lua b/src/scripts/init-e3a.lua new file mode 100644 index 000000000..7deda7732 --- /dev/null +++ b/src/scripts/init-e3a.lua @@ -0,0 +1,42 @@ +require "e3a.rules" +require "e3a.multi" +require "default" +require "spells" +require "extensions" + +function run_tests() + print("running tests") + require "lunit" + lunit.clearstats() + local argv = tests or {} + local stats = lunit.main(argv) + if stats.errors > 0 or stats.failed > 0 then + return 1 + end + return 0 +end + +function run_turn() + require "run-e3a" + + -- the locales that this gameworld supports. + local locales = { "de", "en" } + local confirmed_multis = { + } + local suspected_multis = { + "odin" + } + + local turn = get_turn() + if turn==0 then + turn = read_turn() + set_turn(turn) + end + + orderfile = orderfile or basepath .. '/orders.' .. turn + print("executing turn " .. get_turn() .. " with " .. orderfile) + process(orderfile, confirmed_multis, suspected_multis, locales) + dbupdate() + + return 0 +end diff --git a/src/scripts/run-e3a.lua b/src/scripts/run-e3a.lua index 222632faf..98da4f450 100644 --- a/src/scripts/run-e3a.lua +++ b/src/scripts/run-e3a.lua @@ -1,11 +1,3 @@ --- the locales that this gameworld supports. -local locales = { "de", "en" } -local confirmed_multis = { -} -local suspected_multis = { -"odin" -} - function num_oceans(r) local oceans = 0 local p = r:next(5) @@ -107,17 +99,6 @@ function change_locales() end end -function load_scripts() - scripts = { - "spells.lua", - "extensions.lua", - "e3a/multi.lua", - } - for index, value in pairs(scripts) do - loadscript(value) - end -end - function best_scores(n) local f, numf, top @@ -147,7 +128,7 @@ end function write_statistics() end -function process(orders) +function process(orders, confirmed_multis, suspected_multis, locales) -- initialize starting equipment for new players print(orders) if open_game(get_turn())~=0 then @@ -175,9 +156,7 @@ function process(orders) local nmrs = get_nmrs(1) -- nmrs = 0 - if maxnmrs == nil then - maxnmrs = 30 - end + maxnmrs = maxnmrs or 30 if nmrs >= maxnmrs then print("Shit. More than " .. maxnmrs .. " factions with 1 NMR (" .. nmrs .. ")") write_summary() @@ -203,7 +182,6 @@ function process(orders) -- use newfactions file to place out new players -- autoseed(basepath .. "/newfactions", false) - -- read_xml(resourcepath.."/e3a-update.xml") write_files(locales) write_statistics() @@ -223,12 +201,3 @@ function dbupdate() edb:update_factions() edb:update_scores() end - --- orderfile: contains the name of the orders. -load_scripts() -if orderfile==nil then - print "you must specify an orderfile" -else - process(orderfile) - dbupdate() -end diff --git a/src/scripts/tests.lua b/src/scripts/tests/common.lua similarity index 54% rename from src/scripts/tests.lua rename to src/scripts/tests/common.lua index 3389953a2..5983b90ba 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests/common.lua @@ -1,8 +1,56 @@ -local function test_plane() +require "lunit" + +function one_unit(r, f) + local u = unit.create(f, r, 1) + u:add_item("money", u.number * 100) + u:clear_orders() + return u +end + +function two_units(r, f1, f2) + return one_unit(r, f1), one_unit(r, f2) +end + +function two_factions() + local f1 = faction.create("noreply@eressea.de", "human", "de") + f1.id = 1 + local f2 = faction.create("noreply@eressea.de", "orc", "de") + f2.id = 2 + return f1, f2 +end + +module( "common", package.seeall, lunit.testcase ) + +function test_fleeing_units_can_be_transported() + free_game() + local r = region.create(0, 0, "plain") + local r1 = region.create(1, 0, "plain") + local f1, f2 = two_factions() + local u1, u2 = two_units(r, f1, f2) + local u3 = one_unit(r, f2) + u1.number = 100 + u1:add_order("ATTACKIEREN " .. itoa36(u2.id)) + u2.number = 100 + u2:add_order("FAHREN " .. itoa36(u3.id)) + u3.number = 100 + u3:add_order("KAEMPFE FLIEHE") + u3:add_order("TRANSPORT " .. itoa36(u2.id)) + u3:add_order("NACH O ") + u3:set_skill("riding", 2) + u3:add_item("horse", u2.number) + u3:add_order("KAEMPFE FLIEHE") + process_orders() + write_reports() + assert_equal(u3.region.id, r1.id, "transporter did not move") + assert_equal(u2.region.id, r1.id, "transported unit did not move") +end + +function test_plane() free_game() local pl = plane.create(0, -3, -3, 7, 7) local nx, ny = plane.normalize(pl, 4, 4) - assert(nx==-3 and ny==-3) + assert_equal(nx, -3, "normalization failed") + assert_equal(ny, -3, "normalization failed") local f = faction.create("noreply@eressea.de", "human", "de") f.id = atoi36("tpla") local r, x, y @@ -12,19 +60,18 @@ local function test_plane() local u = unit.create(f, r, 1) end end end - write_reports() end -local function test_rename() +function test_rename() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") local u = unit.create(f, r) u:add_item("aoh", 1) - assert(u:get_item("ao_healing")==1) + assert_equal(u:get_item("ao_healing"), 1) end -local function test_blessed() +function test_blessed() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -40,24 +87,22 @@ local function test_blessed() u:add_spell("blessedharvest") u:clear_orders() u:add_order("ZAUBERE STUFE 3 Regentanz") - print(r:get_resource("money")) + assert_equal(0, r:get_resource("money"), 0) process_orders() - print(r:get_resource("money")) + assert_equal(200, r:get_resource("money")) u:clear_orders() u:add_order("ARBEITEN") - for i=1,3 do - process_orders() - print(r:get_resource("money")) - end + process_orders() + assert_equal(400, r:get_resource("money")) end -local function test_pure() +function test_pure() free_game() local r = region.create(0, 0, "plain") end -local function test_read_write() +function test_read_write() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -66,28 +111,28 @@ local function test_read_write() local fno = f.id local uno = u.id local result = 0 - assert(r.terrain=="plain") + assert_equal(r.terrain, "plain") result = write_game("test_read_write.dat", "binary") - assert(result==0) - assert(get_region(0, 0)~=nil) - assert(get_faction(fno)~=nil) - assert(get_unit(uno)~=nil) + assert_equal(result, 0) + assert_not_equal(get_region(0, 0), nil) + assert_not_equal(get_faction(fno), nil) + assert_not_equal(get_unit(uno), nil) r = nil f = nil u = nil free_game() - assert(get_region(0, 0)==nil) - assert(get_faction(fno)==nil) - assert(get_unit(uno)==nil) + assert_equal(get_region(0, 0), nil) + assert_equal(nil, get_faction(fno)) + assert_equal(nil, get_unit(uno)) result = read_game("test_read_write.dat", "binary") - assert(result==0) - assert(get_region(0, 0)~=nil) - assert(get_faction(fno)~=nil) - assert(get_unit(uno)~=nil) + assert_equal(0, result) + assert_not_equal(nil, get_region(0, 0)) + assert_not_equal(nil, get_faction(fno)) + assert_not_equal(nil, get_unit(uno)) free_game() end -local function test_gmtool() +function test_gmtool() free_game() local r1 = region.create(1, 0, "plain") local r2 = region.create(1, 1, "plain") @@ -106,13 +151,13 @@ local function test_gmtool() for r in gmtool.get_selection() do selections=selections+1 end - assert(selections==2) - assert(gmtool.get_cursor()==nil) + assert_equal(2, selections) + assert_equal(nil, gmtool.get_cursor()) gmtool.close() end -local function test_faction() +function test_faction() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -136,7 +181,7 @@ local function test_faction() assert(units==2) end -local function test_unit() +function test_unit() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -153,7 +198,7 @@ local function test_unit() assert(u:get_item("sword")==2) end -local function test_region() +function test_region() free_game() local r = region.create(0, 0, "plain") r:set_resource("horse", 42) @@ -171,7 +216,7 @@ local function test_region() assert(tostring(r) == "Alabasterheim (0,0)") end -local function test_building() +function test_building() free_game() local u local f = faction.create("noreply@eressea.de", "human", "de") @@ -198,15 +243,7 @@ local function test_building() assert(r2.buildings()==b) end -local function loadscript(name) - local script = scriptpath .. "/" .. name - print("- loading " .. script) - if pcall(dofile, script)==0 then - print("Could not load " .. script) - end -end - -local function test_message() +function test_message() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -221,7 +258,7 @@ local function test_message() return msg end -local function test_hashtable() +function test_hashtable() free_game() local f = faction.create("noreply@eressea.de", "human", "de") f.objects:set("enno", "smart guy") @@ -272,7 +309,7 @@ function test_events() assert(fail==0) end -local function test_recruit2() +function test_recruit2() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -287,7 +324,7 @@ local function test_recruit2() process_orders() end -local function test_guard() +function test_guard() free_game() region.create(1, 0, "plain") local r = region.create(0, 0, "plain") @@ -316,66 +353,7 @@ local function test_guard() assert(u1.region==r) end -local function test_owners() - free_game() - local r = region.create(0, 0, "plain") - local f1 = faction.create("noreply@eressea.de", "human", "de") - local u1 = unit.create(f1, r, 1) - local f2 = faction.create("noreply@eressea.de", "human", "de") - local u2 = unit.create(f2, r, 1) - local u3 = unit.create(f2, r, 1) - - local b3 = building.create(r, "castle") - b3.size = 2 - u3.building = b3 - local b1 = building.create(r, "castle") - b1.size = 1 - u1.building = b1 - local b2 = building.create(r, "castle") - b2.size = 2 - u2.building = b2 - - update_owners() - assert(r.owner==u3.faction) - b1.size=3 - b2.size=3 - update_owners() - assert(r.owner==u2.faction) - b1.size=4 - update_owners() - assert(r.owner==u1.faction) -end - -local function test_morale() - free_game() - local r = region.create(0, 0, "plain") - assert(r.morale==1) - local f1 = faction.create("noreply@eressea.de", "human", "de") - local u1 = unit.create(f1, r, 1) - local f2 = faction.create("noreply@eressea.de", "human", "de") - local u2 = unit.create(f2, r, 1) - - local b = building.create(r, "castle") - b.size = 10 - u1.building = b - u2.building = b - update_owners() - assert(r.morale==1) - r.morale = 5 - assert(r.owner==u1.faction) - u1:clear_orders() - u1:add_order("GIB " .. itoa36(u2.id) .. " KOMMANDO") - process_orders() - u1:clear_orders() - assert(r.owner==u2.faction) - assert(r.morale==3) -- 5-MORALE_TRANSFER - u2.building = nil - update_owners() - assert(r.owner==u1.faction) - assert(r.morale==0) -end - -local function test_recruit() +function test_recruit() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -393,37 +371,7 @@ local function test_recruit() -- assert(u:get_item("money")==10) end -local function test_spells() - free_game() - local r = region.create(0, 0, "plain") - local f = faction.create("noreply@eressea.de", "human", "de") - local u = unit.create(f, r, 1) - u.race = "elf" - u:clear_orders() - u:add_item("money", 10000) - u:set_skill("magic", 5) - u:add_order("LERNE MAGIE Illaun") - process_orders() - local sp - local nums = 0 - if f.spells~=nil then - for sp in f.spells do - nums = nums + 1 - end - assert(nums>0) - for sp in u.spells do - nums = nums - 1 - end - assert(nums==0) - else - for sp in u.spells do - nums = nums + 1 - end - assert(nums>0) - end -end - -local function test_produce() +function test_produce() free_game() local r = region.create(0, 0, "plain") local f = faction.create("noreply@eressea.de", "human", "de") @@ -438,110 +386,6 @@ local function test_produce() assert(u:get_item("sword")==1) end -local function test_alliance() - free_game() - local r = region.create(0, 0, "plain") - local f1 = faction.create("noreply@eressea.de", "human", "de") - local u1 = unit.create(f1, r, 1) - u1:add_item("money", u1.number * 100) - local f2 = faction.create("info@eressea.de", "human", "de") - local u2 = unit.create(f2, r, 1) - u2:add_item("money", u2.number * 100) - assert(f1.alliance==nil) - assert(f2.alliance==nil) - u1:clear_orders() - u2:clear_orders() - u1:add_order("ALLIANZ NEU pink") - u1:add_order("ALLIANZ EINLADEN " .. itoa36(f2.id)) - u2:add_order("ALLIANZ BEITRETEN pink") - process_orders() - assert(f1.alliance~=nil) - assert(f2.alliance~=nil) - assert(f2.alliance==f1.alliance) - u1:clear_orders() - u2:clear_orders() - u1:add_order("ALLIANZ KOMMANDO " .. itoa36(f2.id)) - process_orders() - assert(f1.alliance~=nil) - assert(f2.alliance~=nil) - assert(f2.alliance==f1.alliance) - u1:clear_orders() - u2:clear_orders() - u2:add_order("ALLIANZ AUSSTOSSEN " .. itoa36(f1.id)) - process_orders() - assert(f1.alliance==nil) - assert(f2.alliance~=nil) - u1:clear_orders() - u2:clear_orders() - u2:add_order("ALLIANZ NEU zing") - u1:add_order("ALLIANZ BEITRETEN zing") -- no invite! - process_orders() - assert(f1.alliance==nil) - assert(f2.alliance~=nil) - u1:clear_orders() - u2:clear_orders() - u1:add_order("ALLIANZ NEU zack") - u1:add_order("ALLIANZ EINLADEN " .. itoa36(f2.id)) - u2:add_order("ALLIANZ BEITRETEN zack") - process_orders() - assert(f1.alliance==f2.alliance) - assert(f2.alliance~=nil) -end - -local function spells_csv() - local f = io.open("spells.csv", "w") - for sp in spells() do - f:write('"' .. sp.name .. '",' .. sp.level .. ',' .. sp.school .. ',"' .. sp.text .. '"\n') - end - f:close() - fail = 1 -end - -function test_taxes() - free_game() - local r = region.create(0, 0, "plain") - r.peasants = 1000 - r:set_resource("money", 5000) - local f = faction.create("noreply@eressea.de", "human", "de") - local u = unit.create(f, r, 1) - u:add_item("money", u.number * 10) - u:clear_orders() - u:add_order("LERNE Holzfaellen") -- do not work - local b = building.create(r, "watch") - b.size = 10 - u.building = b - update_owners() - process_orders() - assert(r.morale==1) - assert(u:get_item("money")==25) -end - -function test_market() - free_game() - local r - for x = -1, 1 do for y = -1, 1 do - r = region.create(x, y, "plain") - r.peasants = 5000 - end end - r = get_region(0, 0) - local b = building.create(r, "market") - b.size = 10 - local f = faction.create("noreply@eressea.de", "human", "de") - f.id = 42 - local u = unit.create(f, r, 1) - u.building = b - u:add_item("money", u.number * 10000) - for i = 0, 5 do - local rn = r:next(i) - end - process_orders() - local len = 0 - for i in u.items do - len = len + 1 - end - assert(len>1) -end - function test_work() free_game() local r = region.create(0, 0, "plain") @@ -614,26 +458,6 @@ function test_herbalism() process_orders() end -function test_leave() - free_game() - local r = region.create(0, 0, "plain") - local f = faction.create("noreply@eressea.de", "human", "de") - f.id = 42 - local b1 = building.create(r, "castle") - b1.size = 10 - local b2 = building.create(r, "lighthouse") - b2.size = 10 - local u = unit.create(f, r, 1) - u.building = b1 - assert(u.building~=nil) - u:add_item("money", u.number * 100) - u:clear_orders() - u:add_order("BETRETE BURG " .. itoa36(b2.id)) - update_owners() - process_orders() - assert(u.building.id==b1.id) -- region owners may not leave -end - function test_mallorn() free_game() local r = region.create(0, 0, "plain") @@ -670,44 +494,6 @@ function test_mallorn() assert(u3:get_item("mallorn")==1) end -local function two_units(r, f1, f2) - local u1, u2 - u1 = unit.create(f1, r, 1) - u2 = unit.create(f2, r, 1) - u1:add_item("money", u1.number * 100) - u2:add_item("money", u2.number * 100) - return u1, u2 -end - -local function two_factions() - local f1 = faction.create("noreply@eressea.de", "human", "de") - f1.id = 1 - local f2 = faction.create("noreply@eressea.de", "orc", "de") - f2.id = 2 - return f1, f2 -end - -function test_canoe() - free_game() - local f = faction.create("noreply@eressea.de", "human", "de") - local src = region.create(0, 0, "ocean") - local land = region.create(1, 0, "plain") - region.create(2, 0, "ocean") - local dst = region.create(3, 0, "ocean") - local sh = ship.create(src, "canoe") - local u1, u2 = two_units(src, f, f) - u1.ship = sh - u2.ship = sh - u1:set_skill("sailing", 10) - u1:clear_orders() - u1:add_order("NACH O O O") - process_orders() - assert(u2.region.id==land.id) - u1:add_order("NACH O O O") - process_orders() - assert(u2.region.id==dst.id) -end - function test_control() free_game() local u1, u2 = two_units(region.create(0, 0, "plain"), two_factions()) @@ -716,42 +502,12 @@ function test_control() u1.building = b u2.building = b update_owners() - assert(b.owner==u1) + assert_equal(u1, b.owner) u1:clear_orders() u1:add_order("GIB " .. itoa36(u2.id) .. " KOMMANDO") u1:add_order("VERLASSE") process_orders() - assert(b.owner==u2) -end - -function test_give() - free_game() - local u1, u2 = two_units(region.create(0, 0, "plain"), two_factions()) - local r = u2.region - u1.faction.age = 10 - u2.faction.age = 10 - u1:add_item("money", 500) - local m1, m2 = u1:get_item("money"), u2:get_item("money") - u1:clear_orders() - u1:add_order("GIB " .. itoa36(u2.id) .. " 332 Silber") - u2:clear_orders() - u2:add_order("LERNEN Hiebwaffen") - process_orders() - assert(u1:get_item("money")==m1-10*u1.number) - assert(u2:get_item("money")==m2-10*u2.number) - - m1, m2 = u1:get_item("money"), u2:get_item("money") - u1:clear_orders() - u1:add_order("GIB " .. itoa36(u2.id) .. " 332 Silber") - u2:clear_orders() - u2:add_order("HELFE " .. itoa36(u1.faction.id) .. " GIB") - u2:add_item("horse", 100) - u2:add_order("GIB 0 ALLES PFERD") - local h = r:get_resource("horse") - process_orders() - assert(r:get_resource("horse")>=h+100) - assert(u1:get_item("money")==m1-332-10*u1.number) - assert(u2:get_item("money")==m2+110-10*u2.number) + assert_equal(u2, b.owner) end function test_storage() @@ -762,7 +518,7 @@ function test_storage() local u = unit.create(f, r, 1) u:add_item("money", u.number * 100) store = storage.create("test.unit.dat", "wb") - assert(store) + assert_not_equal(store, nil) store:write_unit(u) store:close() free_game() @@ -777,81 +533,3 @@ function test_storage() assert(u) assert(u:get_item("money") == u.number * 100) end - -loadscript("extensions.lua") -e3only = { - ["give"] = test_give, - ["canoe"] = test_canoe, - ["morale"] = test_morale, - ["owners"] = test_owners, - ["taxes"] = test_taxes, - ["spells"] = test_spells, - ["alliance"] = test_alliance, - ["leave"] = test_leave, - ["market"] = test_market -} - -tests = { - ["pure"] = test_pure, - ["read_write"] = test_read_write, - ["control"] = test_control, - ["faction"] = test_faction, - ["region"] = test_region, - ["building"] = test_building, - ["unit"] = test_unit, - ["message"] = test_message, - ["hashtable"] = test_hashtable, - ["gmtool"] = test_gmtool, - ["events"] = test_events, - ["produce"] = test_produce, - ["rename"] = test_rename, - ["recruit"] = test_recruit, - ["recruit2"] = test_recruit2, - ["herbalism"] = test_herbalism, - ["storage"] = test_storage, - ["mallorn"] = test_mallorn, - ["upkeep"] = test_upkeep, - ["id"] = test_id, - ["work"] = test_work, - ["plane"] = test_plane, - ["guard"] = test_guard -} - -mytests = { - ["guard"] = test_guard, - ["plane"] = test_plane, - ["owners"] = test_owners -} - -fail = 0 - -thetests = tests - -function test(tests) - for k, v in pairs(tests) do - local status, err = pcall(v) - if not status then - fail = fail + 1 - print("[FAIL] " .. k .. ": " .. err) - else - print("[OK] " .. k) - end - end -end - -function is_e3() - free_game() - r = region.create(0, 0, "plain") - b = building.create(r, "market") - return b~=nil -end - -test(tests) -if is_e3() then test(e3only) end - --- spells_csv() - -if fail > 0 then - print(fail .. " tests failed.") - io.stdin:read() -end diff --git a/src/scripts/tests/e3a.lua b/src/scripts/tests/e3a.lua new file mode 100644 index 000000000..14373e9bb --- /dev/null +++ b/src/scripts/tests/e3a.lua @@ -0,0 +1,259 @@ +require "lunit" + +module( "e3", package.seeall, lunit.testcase ) + +function test_owners() + free_game() + local r = region.create(0, 0, "plain") + local f1 = faction.create("noreply@eressea.de", "human", "de") + local u1 = unit.create(f1, r, 1) + local f2 = faction.create("noreply@eressea.de", "human", "de") + local u2 = unit.create(f2, r, 1) + local u3 = unit.create(f2, r, 1) + + local b3 = building.create(r, "castle") + b3.size = 2 + u3.building = b3 + local b1 = building.create(r, "castle") + b1.size = 1 + u1.building = b1 + local b2 = building.create(r, "castle") + b2.size = 2 + u2.building = b2 + + update_owners() + assert(r.owner==u3.faction) + b1.size=3 + b2.size=3 + update_owners() + assert(r.owner==u2.faction) + b1.size=4 + update_owners() + assert(r.owner==u1.faction) +end + +function test_taxes() + free_game() + local r = region.create(0, 0, "plain") + r.peasants = 1000 + r:set_resource("money", 5000) + local f = faction.create("noreply@eressea.de", "human", "de") + local u = unit.create(f, r, 1) + u:add_item("money", u.number * 10) + u:clear_orders() + u:add_order("LERNE Holzfaellen") -- do not work + local b = building.create(r, "watch") + b.size = 10 + u.building = b + update_owners() + process_orders() + assert_equal(1, r.morale) + assert_equal(25, u:get_item("money")) +end + +function test_leave() + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("noreply@eressea.de", "human", "de") + f.id = 42 + local b1 = building.create(r, "castle") + b1.size = 10 + local b2 = building.create(r, "lighthouse") + b2.size = 10 + local u = unit.create(f, r, 1) + u.building = b1 + assert_not_equal(nil, u.building) + u:add_item("money", u.number * 100) + u:clear_orders() + u:add_order("BETRETE BURG " .. itoa36(b2.id)) + update_owners() + process_orders() + assert_equal(u.building.id, b1.id, "region owner has left the building") -- region owners may not leave +end + +function test_market() + free_game() + local r + for x = -1, 1 do for y = -1, 1 do + r = region.create(x, y, "plain") + r.peasants = 5000 + end end + r = get_region(0, 0) + local b = building.create(r, "market") + b.size = 10 + local f = faction.create("noreply@eressea.de", "human", "de") + f.id = 42 + local u = unit.create(f, r, 1) + u.building = b + u:add_item("money", u.number * 10000) + for i = 0, 5 do + local rn = r:next(i) + end + process_orders() + local len = 0 + for i in u.items do + len = len + 1 + end + assert(len>1) +end + +function test_spells() + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("noreply@eressea.de", "human", "de") + local u = unit.create(f, r, 1) + u.race = "elf" + u:clear_orders() + u:add_item("money", 10000) + u:set_skill("magic", 5) + u:add_order("LERNE MAGIE Illaun") + process_orders() + local sp + local nums = 0 + if f.spells~=nil then + for sp in f.spells do + nums = nums + 1 + end + assert(nums>0) + for sp in u.spells do + nums = nums - 1 + end + assert(nums==0) + else + for sp in u.spells do + nums = nums + 1 + end + assert(nums>0) + end +end + +function test_alliance() + free_game() + local r = region.create(0, 0, "plain") + local f1 = faction.create("noreply@eressea.de", "human", "de") + local u1 = unit.create(f1, r, 1) + u1:add_item("money", u1.number * 100) + local f2 = faction.create("info@eressea.de", "human", "de") + local u2 = unit.create(f2, r, 1) + u2:add_item("money", u2.number * 100) + assert(f1.alliance==nil) + assert(f2.alliance==nil) + u1:clear_orders() + u2:clear_orders() + u1:add_order("ALLIANZ NEU pink") + u1:add_order("ALLIANZ EINLADEN " .. itoa36(f2.id)) + u2:add_order("ALLIANZ BEITRETEN pink") + process_orders() + assert(f1.alliance~=nil) + assert(f2.alliance~=nil) + assert(f2.alliance==f1.alliance) + u1:clear_orders() + u2:clear_orders() + u1:add_order("ALLIANZ KOMMANDO " .. itoa36(f2.id)) + process_orders() + assert(f1.alliance~=nil) + assert(f2.alliance~=nil) + assert(f2.alliance==f1.alliance) + u1:clear_orders() + u2:clear_orders() + u2:add_order("ALLIANZ AUSSTOSSEN " .. itoa36(f1.id)) + process_orders() + assert(f1.alliance==nil) + assert(f2.alliance~=nil) + u1:clear_orders() + u2:clear_orders() + u2:add_order("ALLIANZ NEU zing") + u1:add_order("ALLIANZ BEITRETEN zing") -- no invite! + process_orders() + assert(f1.alliance==nil) + assert(f2.alliance~=nil) + u1:clear_orders() + u2:clear_orders() + u1:add_order("ALLIANZ NEU zack") + u1:add_order("ALLIANZ EINLADEN " .. itoa36(f2.id)) + u2:add_order("ALLIANZ BEITRETEN zack") + process_orders() + assert(f1.alliance==f2.alliance) + assert(f2.alliance~=nil) +end + +function test_morale() + free_game() + local r = region.create(0, 0, "plain") + assert_equal(1, r.morale) + local f1 = faction.create("noreply@eressea.de", "human", "de") + local u1 = unit.create(f1, r, 1) + local f2 = faction.create("noreply@eressea.de", "human", "de") + local u2 = unit.create(f2, r, 1) + + local b = building.create(r, "castle") + b.size = 10 + u1.building = b + u2.building = b + update_owners() + assert_equal(1, r.morale) + r.morale = 5 + assert_equal(r.owner, u1.faction) + u1:clear_orders() + u1:add_order("GIB " .. itoa36(u2.id) .. " KOMMANDO") + process_orders() + u1:clear_orders() + assert_equal(u2.faction, r.owner) + assert_equal(3, r.morale) -- 5-MORALE_TRANSFER + u2.building = nil + update_owners() + assert_equal(r.owner, u1.faction) + assert_equal(0, r.morale) +end + +function test_canoe_passes_through_land() + free_game() + local f = faction.create("noreply@eressea.de", "human", "de") + local src = region.create(0, 0, "ocean") + local land = region.create(1, 0, "plain") + region.create(2, 0, "ocean") + local dst = region.create(3, 0, "ocean") + local sh = ship.create(src, "canoe") + local u1, u2 = two_units(src, f, f) + u1.ship = sh + u2.ship = sh + u1:set_skill("sailing", 10) + u1:clear_orders() + u1:add_order("NACH O O O") + process_orders() + assert_equal(u2.region.id, land.id, "canoe did not stop at coast") + u1:add_order("NACH O O O") + process_orders() + write_reports() + assert_equal(u2.region.id, dst.id, "canoe could not leave coast") +end + +function test_give_only_a_third_of_items() + free_game() + local u1, u2 = two_units(region.create(0, 0, "plain"), two_factions()) + local r = u2.region + u1.faction.age = 10 + u2.faction.age = 10 + u1:add_item("money", 500) + local m1, m2 = u1:get_item("money"), u2:get_item("money") + u1:clear_orders() + u1:add_order("GIB " .. itoa36(u2.id) .. " 332 Silber") + u2:clear_orders() + u2:add_order("LERNEN Hiebwaffen") + process_orders() + assert(u1:get_item("money")==m1-10*u1.number) + assert(u2:get_item("money")==m2-10*u2.number) + + m1, m2 = u1:get_item("money"), u2:get_item("money") + u1:clear_orders() + u1:add_order("GIB " .. itoa36(u2.id) .. " 332 Silber") + u2:clear_orders() + u2:add_order("HELFE " .. itoa36(u1.faction.id) .. " GIB") + u2:add_item("horse", 100) + u2:add_order("GIB 0 ALLES PFERD") + local h = r:get_resource("horse") + process_orders() + assert(r:get_resource("horse")>=h+100) + assert_equal(m1-332-10*u1.number, u1:get_item("money")) + assert_equal(m2+110-10*u2.number, u2:get_item("money")) +end