preloading lua files (for running tests.lua) from eressea.ini

fixed building taxes test
This commit is contained in:
Enno Rehling 2009-07-01 11:51:21 +00:00
parent 97daeead0c
commit 23b1d33f77
8 changed files with 53 additions and 19 deletions

View File

@ -61,7 +61,7 @@ typedef struct building_type {
void (*init)(struct building_type*);
void (*age)(struct building *);
int (*protection)(struct building *, struct unit *);
float (*taxes)(struct building *);
double (*taxes)(struct building *);
struct attrib * attribs;
} building_type;

View File

@ -320,7 +320,7 @@ parse_buildings(xmlDocPtr doc)
} else if (strcmp((const char*)propValue, "protection")==0) {
btype->protection = (int (*)(struct building*, struct unit *))fun;
} else if (strcmp((const char*)propValue, "taxes")==0) {
btype->taxes = (float (*)(struct building*))fun;
btype->taxes = (double (*)(struct building*))fun;
} else if (strcmp((const char*)propValue, "age")==0) {
btype->age = (void (*)(struct building*))fun;
} else {

View File

@ -171,6 +171,7 @@ static boolean g_writemap = false;
static boolean g_ignore_errors = false;
static boolean opt_reportonly = false;
static const char * luafile = NULL;
static const char * preload = NULL;
static const char * script_path = "scripts";
static int memdebug = 0;
@ -267,6 +268,10 @@ static const struct {
{LUA_IOLIBNAME, luaopen_io},
{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
@ -588,6 +593,7 @@ load_inifile(const char * filename)
verbosity = iniparser_getint(d, "eressea:verbose", 2);
battledebug = iniparser_getint(d, "eressea:debug", battledebug)?1:0;
preload = iniparser_getstring(d, "eressea:preload", preload);
luafile = iniparser_getstring(d, "eressea:run", luafile);
g_reportdir = iniparser_getstring(d, "eressea:report", g_reportdir);
@ -657,7 +663,7 @@ main(int argc, char *argv[])
int i;
char * lc_ctype;
char * lc_numeric;
lua_State * luaState = lua_init();
lua_State * L = lua_init();
static int write_csv = 1;
setup_signal_handler();
@ -671,14 +677,14 @@ main(int argc, char *argv[])
if (lc_ctype) lc_ctype = strdup(lc_ctype);
if (lc_numeric) lc_numeric = strdup(lc_numeric);
global.vm_state = luaState;
global.vm_state = L;
load_inifile("eressea.ini");
if (verbosity>=4) {
printf("\n%s PBEM host\n"
"Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n"
"Compilation: " __DATE__ " at " __TIME__ "\nVersion: %f\n\n", global.gamename, version());
}
if ((i=read_args(argc, argv, luaState))!=0) return i;
if ((i=read_args(argc, argv, L))!=0) return i;
#ifdef CRTDBG
init_crtdbg();
@ -692,7 +698,24 @@ main(int argc, char *argv[])
write_spells();
}
/* run the main script */
if (luafile==NULL) lua_console(luaState);
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 (luafile==NULL) lua_console(L);
else {
char buf[MAX_PATH];
if (script_path) {
@ -701,7 +724,7 @@ main(int argc, char *argv[])
else strcpy(buf, luafile);
#ifdef BINDINGS_LUABIND
try {
luabind::call_function<int>(luaState, "dofile", buf);
luabind::call_function<int>(L, "dofile", buf);
}
catch (std::runtime_error& rte) {
log_error(("%s.\n", rte.what()));
@ -711,10 +734,10 @@ main(int argc, char *argv[])
my_lua_error(L);
}
#elif defined(BINDINGS_TOLUA)
lua_getglobal(luaState, "dofile");
lua_pushstring(luaState, buf);
if (lua_pcall(luaState, 1, 0, 0) != 0) {
my_lua_error(luaState);
lua_getglobal(L, "dofile");
lua_pushstring(L, buf);
if (lua_pcall(L, 1, 0, 0) != 0) {
my_lua_error(L);
}
#endif
}
@ -723,7 +746,7 @@ main(int argc, char *argv[])
#endif
game_done();
kernel_done();
lua_done(luaState);
lua_done(L);
log_close();
setlocale(LC_CTYPE, lc_ctype);

View File

@ -122,6 +122,14 @@ tolua_building_get_id(lua_State* L)
return 1;
}
static int
tolua_building_get_type(lua_State* L)
{
building * self = (building *)tolua_tousertype(L, 1, 0);
tolua_pushstring(L, self->type->_name);
return 1;
}
static int
tolua_building_create(lua_State* L)
{
@ -160,6 +168,7 @@ tolua_building_open(lua_State* L)
tolua_function(L, "__tostring", tolua_building_tostring);
tolua_variable(L, "id", tolua_building_get_id, NULL);
tolua_variable(L, "type", tolua_building_get_type, NULL);
tolua_variable(L, "name", tolua_building_get_name, tolua_building_set_name);
tolua_variable(L, "units", tolua_building_get_units, NULL);
tolua_variable(L, "region", tolua_building_get_region, tolua_building_set_region);

View File

@ -435,12 +435,12 @@ lua_building_protection(building * b, unit * u)
return result;
}
static float
static double
lua_building_taxes(building * b)
{
lua_State * L = (lua_State *)global.vm_state;
const char * fname = "building_taxes";
float result = 0.0F;
double result = 0.0F;
lua_pushstring(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
@ -453,7 +453,7 @@ lua_building_taxes(building * b)
buildingname(b), fname, error));
lua_pop(L, 1);
} else {
result = (float)lua_tonumber(L, -1);
result = (double)lua_tonumber(L, -1);
}
} else {
log_error(("building_taxes(%s) calling '%s': not a function.\n",

View File

@ -116,6 +116,11 @@
<param name="modules.wormholes" value="0"/>
<param name="modules.markets" value="1"/>
<!-- resource limitations -->
<param name="magic.regeneration" value="0.5"/>
<param name="magic.power" value="0.5"/>
<param name="resource.factor" value="0.25"/>
<param name="skills.cost" value="500"/>
<param name="entertain.base" value="0"/>
<param name="entertain.perlevel" value="20"/>

View File

@ -9,8 +9,6 @@ function loadscript(name)
end
end
loadscript("default.lua")
function change_locales()
-- local localechange = { }
local localechange = { de = { "rtph" } }
@ -30,7 +28,6 @@ function load_scripts()
scripts = {
"spells.lua",
"extensions.lua",
"e3a/rules.lua"
}
for index, value in pairs(scripts) do
loadscript(value)

View File

@ -360,7 +360,7 @@ function test_taxes()
u:clear_orders()
u:add_order("LERNE Wahrnehmung")
local b = building.create(r, "watch")
b.size = 4
b.size = 10
u.building = b
update_owners()
process_orders()