Begin of massive changes to the Lua API.

Everything will live in a single eressea module now, and the global
namespace will no longer be polluted by all kinds of modules and
functions.
Also new: Bindings will be generated by tolua from .pkg files,
starting with settings, eressea (read/write/free_game) and process in
this commit.
This commit is contained in:
Enno Rehling 2012-06-06 19:29:36 -07:00
parent 8327db65b8
commit 8b3d735e8c
22 changed files with 404 additions and 236 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ Release/
*.opensdf
*.ipch
*.sh
*.tmp
src/*.vcproj.*.user
tags
build-vs2010

12
scripts/tests/attrib.lua Normal file → Executable file
View File

@ -11,9 +11,9 @@ end
function test_attrib_global()
a = attrib.create('global', {})
write_game('attrib.dat')
free_game()
read_game('attrib.dat')
eressea.write_game('attrib.dat')
eressea.free_game()
eressea.read_game('attrib.dat')
end
function test_attrib()
@ -30,9 +30,9 @@ function test_attrib()
a = attrib.create(u, "enno")
a = attrib.create(u, u2)
a = attrib.create(u, data)
write_game("attrib.dat")
free_game()
read_game("attrib.dat")
eressea.write_game("attrib.dat")
eressea.free_game()
eressea.read_game("attrib.dat")
u = get_unit(uno)
u2 = get_unit(u2no)
assert_false(has_attrib(u, 42))

8
scripts/tests/bson.lua Normal file → Executable file
View File

@ -3,7 +3,7 @@ require "lunit"
module("tests.eressea.bson", package.seeall, lunit.testcase)
function setup()
free_game()
eressea.free_game()
end
function test_bson_create()
@ -24,12 +24,12 @@ end
function test_bson_readwrite()
local i, r = region.create(0, 0, "mountain")
attrib.create(r, 42)
i = write_game("test_read_write.dat")
i = eressea.write_game("test_read_write.dat")
assert_equal(0, i)
free_game()
eressea.free_game()
r = get_region(0, 0)
assert_equal(nil, r)
i = read_game("test_read_write.dat")
i = eressea.read_game("test_read_write.dat")
assert_equal(0, i)
r = get_region(0, 0)
assert_not_equal(nil, r)

50
scripts/tests/common.lua Normal file → Executable file
View File

@ -38,12 +38,12 @@ end
module("tests.eressea.common", package.seeall, lunit.testcase)
function setup()
free_game()
write_game("free.dat")
settings.set("nmr.removenewbie", "0")
settings.set("nmr.timeout", "0")
settings.set("NewbieImmunity", "0")
settings.set("rules.economy.food", "4")
eressea.free_game()
eressea.write_game("free.dat")
eressea.settings.set("nmr.removenewbie", "0")
eressea.settings.set("nmr.timeout", "0")
eressea.settings.set("NewbieImmunity", "0")
eressea.settings.set("rules.economy.food", "4")
end
function DISABLE_test_eventbus_fire()
@ -112,7 +112,7 @@ function test_read_write()
local uno = u.id
local result = 0
assert_equal(r.terrain, "plain")
result = write_game("test_read_write.dat", "binary")
result = eressea.write_game("test_read_write.dat")
assert_equal(result, 0)
assert_not_equal(get_region(0, 0), nil)
assert_not_equal(get_faction(fno), nil)
@ -120,11 +120,11 @@ function test_read_write()
r = nil
f = nil
u = nil
free_game()
eressea.free_game()
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")
result = eressea.read_game("test_read_write.dat")
assert_equal(0, result)
assert_not_equal(nil, get_region(0, 0))
assert_not_equal(nil, get_faction(fno))
@ -388,7 +388,7 @@ function test_work()
end
function test_upkeep()
settings.set("rules.economy.food", "0")
eressea.settings.set("rules.economy.food", "0")
local r = region.create(0, 0, "plain")
local f = faction.create("noreply@eressea.de", "human", "de")
local u = unit.create(f, r, 5)
@ -541,7 +541,7 @@ function test_store_unit()
assert_not_equal(store, nil)
store:write_unit(u)
store:close()
free_game()
eressea.free_game()
-- recreate world:
r = region.create(0, 0, "plain")
f = faction.create("noreply@eressea.de", "human", "de")
@ -668,7 +668,7 @@ function test_food_is_consumed()
u:add_item("money", 100)
u:clear_orders()
u:add_order("LERNEN Reiten") -- don't work
settings.set("rules.economy.food", "4")
eressea.settings.set("rules.economy.food", "4")
process_orders()
assert_equal(100, u:get_item("money"))
end
@ -680,7 +680,7 @@ function test_food_can_override()
u:add_item("money", 100)
u:clear_orders()
u:add_order("LERNEN Reiten") -- don't work
settings.set("rules.economy.food", "0")
eressea.settings.set("rules.economy.food", "0")
process_orders()
assert_equal(90, u:get_item("money"))
end
@ -806,10 +806,10 @@ end
module("tests.report", package.seeall, lunit.testcase)
function setup()
free_game()
settings.set("nmr.removenewbie", "0")
settings.set("nmr.timeout", "0")
settings.set("rules.economy.food", "4")
eressea.free_game()
eressea.settings.set("nmr.removenewbie", "0")
eressea.settings.set("nmr.timeout", "0")
eressea.settings.set("rules.economy.food", "4")
end
local function find_in_report(f, pattern, extension)
@ -894,9 +894,9 @@ end
module("tests.parser", package.seeall, lunit.testcase)
function setup()
free_game()
write_game("free.dat")
settings.set("rules.economy.food", "4") -- FOOD_IS_FREE
eressea.free_game()
eressea.write_game("free.dat")
eressea.settings.set("rules.economy.food", "4") -- FOOD_IS_FREE
end
function test_parser()
@ -909,12 +909,12 @@ function test_parser()
assert_not_nil(file)
file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n')
file:write('EINHEIT ' .. itoa36(u.id) .. "\n")
file:write("LERNEN Hiebwaffen\n")
file:write("BENENNEN EINHEIT 'Goldene Herde'\n")
file:close()
read_orders(filename)
process_orders()
assert_not_equal(0, u:get_skill("melee"))
assert_equal("Goldene Herde", u.name)
end
function test_bug_1922()
@ -928,8 +928,8 @@ function test_bug_1922()
local s = _test_create_ship(r)
settings.set("rules.ship.drifting","0") -- ships are not drifting
settings.set("rules.economy.food","4") -- does not need silver
eressea.settings.set("rules.ship.drifting","0") -- ships are not drifting
eressea.settings.set("rules.economy.food","4") -- does not need silver
u.ship = s
u:clear_orders()
@ -950,7 +950,7 @@ function test_bug_1922_by_foot()
local f = faction.create("noreply@eressea.de", "human", "de")
local u = unit.create(f, r, 1)
settings.set("rules.economy.food","4") -- does not need silver
eressea.settings.set("rules.economy.food","4") -- does not need silver
u:clear_orders()
u:add_order("NACH O")

35
scripts/tests/orders.lua Executable file
View File

@ -0,0 +1,35 @@
require "lunit"
local _G = _G
local eressea = eressea
module("tests.orders", lunit.testcase)
local r, f, u
function setup()
eressea.free_game()
r = _G.region.create(0, 0, "mountain")
f = _G.faction.create("noreply@eressea.de", "human", "de")
u = _G.unit.create(f, r, 1)
u:clear_orders()
eressea.settings.set("rules.economy.food", "4")
eressea.settings.set("nmr.removenewbie", "0")
eressea.settings.set("nmr.timeout", "0")
eressea.settings.set("NewbieImmunity", "0")
end
function test_learn()
u:add_order("LERNEN Hiebwaffen")
_G.process_orders()
assert_not_equal(0, u:get_skill("melee"))
end
function test_give()
local u2 = _G.unit.create(f, r, 1)
u:add_item("money", 10)
u:add_order("GIB " .. u2.id .. "5 SILBER")
_G.process_orders()
assert_not_equal(5, u:get_item("money"))
assert_not_equal(5, u2:get_item("money"))
end

8
scripts/tests/spells.lua Normal file → Executable file
View File

@ -5,10 +5,10 @@ module("tests.eressea.spells", package.seeall, lunit.testcase)
local r, f, u
function setup()
free_game()
settings.set("magic.regeneration.enable", "0")
settings.set("magic.fumble.enable", "0")
settings.set("rules.economy.food", "4")
eressea.free_game()
eressea.settings.set("magic.regeneration.enable", "0")
eressea.settings.set("magic.fumble.enable", "0")
eressea.settings.set("rules.economy.food", "4")
r = region.create(0, 0, "plain")
f = faction.create("spell_payment@eressea.de", "elf", "de")

17
src/CMakeLists.txt Normal file → Executable file
View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 2.6)
project (eressea C)
add_subdirectory(bindings)
IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long")
ELSE(CMAKE_COMPILER_IS_GNUCC)
@ -88,19 +90,6 @@ set (LIB_SRC
attributes/raceprefix.c
attributes/reduceproduction.c
attributes/targetregion.c
# bindings/bind_attrib.c
bindings/bind_building.c
bindings/bind_faction.c
bindings/bind_gmtool.c
bindings/bind_hashtable.c
bindings/bindings.c
bindings/bind_message.c
bindings/bind_region.c
bindings/bind_ship.c
bindings/bind_sqlite.c
bindings/bind_storage.c
bindings/bind_unit.c
bindings/helpers.c
eressea.c
gamecode/archetype.c
gamecode/creation.c
@ -218,7 +207,7 @@ set (LIB_SRC
util/xml.c
)
add_library(${ERESSEA_LIBRARY} ${LIB_SRC} ${LIB_HDR})
add_library(${ERESSEA_LIBRARY} ${LIB_SRC} ${BIND_SRC} ${LIB_HDR})
target_link_libraries(${ERESSEA_LIBRARY} ${ERESSEA_LIBRARIES})
add_executable(${PROJECT_NAME}_test ${TEST_SRC})

78
src/bindings/CMakeLists.txt Executable file
View File

@ -0,0 +1,78 @@
cmake_minimum_required(VERSION 2.6)
project (bindings C)
IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long")
ELSE(CMAKE_COMPILER_IS_GNUCC)
MESSAGE(STATUS "Unknown compiler ${CMAKE_C_COMPILER_ID}")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
set (BINDINGS_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "Eressea Lua Bindings")
find_package (Lua51 REQUIRED)
find_package (ToLua REQUIRED)
find_package (LibXml2 REQUIRED)
find_package (Curses REQUIRED)
find_package (SQLite3 REQUIRED)
include_directories (${ERESSEA_INCLUDE_DIR})
include_directories (${CRITBIT_INCLUDE_DIR})
include_directories (${CRYPTO_INCLUDE_DIR})
include_directories (${QUICKLIST_INCLUDE_DIR})
include_directories (${CUTEST_INCLUDE_DIR})
include_directories (${LUA_INCLUDE_DIR})
include_directories (${TOLUA_INCLUDE_DIR})
include_directories (${LIBXML2_INCLUDE_DIR})
include_directories (${BSON_INCLUDE_DIR})
include_directories (${INIPARSER_INCLUDE_DIR})
FILE (GLOB_RECURSE LIB_HDR *.h)
MACRO(ADD_LUA_MODULE MODULE_NAME FILES)
ADD_LIBRARY (${MODULE_NAME} SHARED ${FILES})
SET_TARGET_PROPERTIES(${MODULE_NAME}
PROPERTIES
PREFIX ""
)
ENDMACRO(ADD_LUA_MODULE)
MACRO(TOLUA_BINDING PKGFILE FILES)
ADD_CUSTOM_COMMAND(
OUTPUT ${PKGFILE}.c
DEPENDS ${FILES} ${PKGFILE}
COMMAND tolua
ARGS -o ${CMAKE_CURRENT_BINARY_DIR}/${PKGFILE}.c ${PKGFILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
ENDMACRO(TOLUA_BINDING)
TOLUA_BINDING(process.pkg bind_process.h)
TOLUA_BINDING(eressea.pkg bind_eressea.h)
TOLUA_BINDING(settings.pkg bind_settings.h)
set (BIND_SRC
process.pkg.c
eressea.pkg.c
settings.pkg.c
bind_process.c
bind_eressea.c
bind_settings.c
# bind_attrib.c
bind_building.c
bind_faction.c
bind_gmtool.c
bind_hashtable.c
bindings.c
bind_message.c
bind_region.c
bind_ship.c
bind_sqlite.c
bind_storage.c
bind_unit.c
helpers.c
)
add_library(${BINDINGS_LIBRARY} ${LIB_SRC} ${BIND_SRC} ${LIB_HDR})
target_link_libraries(${BINDINGS_LIBRARY} ${ERESSEA_LIBRARY})

19
src/bindings/bind_eressea.c Executable file
View File

@ -0,0 +1,19 @@
#include "bind_eressea.h"
#include <platform.h>
#include <kernel/types.h>
#include <kernel/config.h>
#include <kernel/save.h>
#include <util/storage.h>
void eressea_free_game(void) {
free_gamedata();
}
int eressea_read_game(const char * filename) {
return readgame(filename, IO_BINARY, 0);
}
int eressea_write_game(const char * filename) {
return writegame(filename, IO_BINARY);
}

14
src/bindings/bind_eressea.h Executable file
View File

@ -0,0 +1,14 @@
#ifndef BIND_ERESSEA_H
#define BIND_ERESSEA_H
#ifdef __cplusplus
extern "C" {
#endif
void eressea_free_game(void);
int eressea_read_game(const char * filename);
int eressea_write_game(const char * filename);
#ifdef __cplusplus
}
#endif
#endif

18
src/bindings/bind_process.c Executable file
View File

@ -0,0 +1,18 @@
#include "bind_process.h"
#include <platform.h>
#include <kernel/types.h>
#include <kernel/region.h>
#include <gamecode/economy.h>
#include <gamecode/market.h>
void process_produce(void) {
struct region *r;
for (r = regions; r; r = r->next) {
produce(r);
}
}
void process_markets(void) {
do_markets();
}

13
src/bindings/bind_process.h Executable file
View File

@ -0,0 +1,13 @@
#ifndef BIND_ERESSEA_PROCESS_H
#define BIND_ERESSEA_PROCESS_H
#ifdef __cplusplus
extern "C" {
#endif
void process_produce(void);
void process_markets(void);
#ifdef __cplusplus
}
#endif
#endif

15
src/bindings/bind_settings.c Executable file
View File

@ -0,0 +1,15 @@
#include "bind_settings.h"
#include <platform.h>
#include <kernel/types.h>
#include <kernel/config.h>
const char * settings_get(const char *key)
{
return get_param(global.parameters, key);
}
void settings_set(const char *key, const char *value)
{
set_param(&global.parameters, key, value);
}

13
src/bindings/bind_settings.h Executable file
View File

@ -0,0 +1,13 @@
#ifndef BIND_ERESSEA_PROCESS_H
#define BIND_ERESSEA_PROCESS_H
#ifdef __cplusplus
extern "C" {
#endif
const char * settings_get(const char *key);
void settings_set(const char *key, const char *value);
#ifdef __cplusplus
}
#endif
#endif

155
src/bindings/bindings.c Normal file → Executable file
View File

@ -13,7 +13,14 @@ without prior permission by the authors of Eressea.
#include <platform.h>
#include "bindings.h"
#include "bind_unit.h"
#include "bind_storage.h"
#include "bind_building.h"
#include "bind_hashtable.h"
#include "bind_message.h"
#include "bind_building.h"
#include "bind_faction.h"
#include "bind_ship.h"
#include "bind_gmtool.h"
#include "bind_region.h"
#include "helpers.h"
@ -54,6 +61,7 @@ without prior permission by the authors of Eressea.
#include <util/attrib.h>
#include <util/base36.h>
#include <util/console.h>
#include <util/eventbus.h>
#include <util/language.h>
#include <util/lists.h>
@ -66,10 +74,18 @@ without prior permission by the authors of Eressea.
#include <iniparser.h>
#include <tolua.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <time.h>
#include <assert.h>
#define TOLUA_PKG(NAME) extern void tolua_##NAME##_open(lua_State * L)
TOLUA_PKG(eressea);
TOLUA_PKG(process);
TOLUA_PKG(settings);
int log_lua_error(lua_State * L)
{
const char *error = lua_tostring(L, -1);
@ -1016,21 +1032,6 @@ int tolua_read_xml(lua_State * L)
return 0;
}
int tolua_process_markets(lua_State * L)
{
do_markets();
return 0;
}
int tolua_process_produce(lua_State * L)
{
region *r;
for (r = regions; r; r = r->next) {
produce(r);
}
return 0;
}
typedef struct event_args {
int hfunction;
int hargs;
@ -1104,21 +1105,6 @@ static int tolua_report_unit(lua_State * L)
return 1;
}
static int tolua_settings_get(lua_State * L)
{
const char *name = tolua_tostring(L, 1, 0);
tolua_pushstring(L, get_param(global.parameters, name));
return 1;
}
static int tolua_settings_set(lua_State * L)
{
const char *name = tolua_tostring(L, 1, 0);
const char *value = tolua_tostring(L, 2, 0);
set_param(&global.parameters, name, value);
return 0;
}
static void parse_inifile(lua_State * L, dictionary * d, const char *section)
{
int i;
@ -1148,10 +1134,14 @@ static void parse_inifile(lua_State * L, dictionary * d, const char *section)
lua_rawset(L, -3);
}
int tolua_eressea_open(lua_State * L)
int tolua_bindings_open(lua_State * L)
{
tolua_open(L);
tolua_eressea_open(L);
tolua_process_open(L);
tolua_settings_open(L);
/* register user types */
tolua_usertype(L, TOLUA_CAST "spell");
tolua_usertype(L, TOLUA_CAST "spell_list");
@ -1163,12 +1153,6 @@ int tolua_eressea_open(lua_State * L)
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
tolua_module(L, "process", 0);
tolua_beginmodule(L, "process");
{
tolua_function(L, "markets", &tolua_process_markets);
tolua_function(L, "produce", &tolua_process_produce);
} tolua_endmodule(L);
tolua_cclass(L, TOLUA_CAST "alliance", TOLUA_CAST "alliance",
TOLUA_CAST "", NULL);
tolua_beginmodule(L, TOLUA_CAST "alliance");
@ -1198,12 +1182,6 @@ int tolua_eressea_open(lua_State * L)
tolua_function(L, TOLUA_CAST "register", &tolua_eventbus_register);
tolua_function(L, TOLUA_CAST "fire", &tolua_eventbus_fire);
} tolua_endmodule(L);
tolua_module(L, TOLUA_CAST "settings", 1);
tolua_beginmodule(L, TOLUA_CAST "settings");
{
tolua_function(L, TOLUA_CAST "set", &tolua_settings_set);
tolua_function(L, TOLUA_CAST "get", &tolua_settings_get);
} tolua_endmodule(L);
tolua_module(L, TOLUA_CAST "report", 1);
tolua_beginmodule(L, TOLUA_CAST "report");
{
@ -1279,3 +1257,94 @@ int tolua_eressea_open(lua_State * L)
} tolua_endmodule(L);
return 1;
}
static const struct {
const char *name;
int (*func) (lua_State *);
} lualibs[] = {
{
"", luaopen_base}, {
LUA_TABLIBNAME, luaopen_table}, {
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
{
NULL, NULL}
};
static void openlibs(lua_State * L)
{
int i;
for (i = 0; lualibs[i].func; ++i) {
lua_pushcfunction(L, lualibs[i].func);
lua_pushstring(L, lualibs[i].name);
lua_call(L, 1, 0);
}
}
void lua_done(lua_State * L) {
lua_close(L);
}
lua_State *lua_init(void) {
lua_State *L = lua_open();
openlibs(L);
#ifdef BINDINGS_TOLUA
register_tolua_helpers();
tolua_bindings_open(L);
tolua_eressea_open(L);
tolua_sqlite_open(L);
tolua_unit_open(L);
tolua_building_open(L);
tolua_ship_open(L);
tolua_region_open(L);
tolua_faction_open(L);
#ifdef BSON_ATTRIB
tolua_attrib_open(L);
#endif
tolua_unit_open(L);
tolua_message_open(L);
tolua_hashtable_open(L);
tolua_gmtool_open(L);
tolua_storage_open(L);
#endif
return L;
}
int eressea_run(lua_State *L, const char *luafile, const char *entry_point)
{
int err;
global.vm_state = L;
/* run the main script */
if (luafile) {
log_debug("executing script %s\n", luafile);
lua_getglobal(L, "dofile");
lua_pushstring(L, luafile);
err = lua_pcall(L, 1, 0, 0);
if (err != 0) {
log_lua_error(L);
abort();
return err;
}
}
if (entry_point) {
lua_getglobal(L, entry_point);
err = lua_pcall(L, 0, 1, 0);
if (err != 0) {
log_lua_error(L);
abort();
return err;
}
} else {
err = lua_console(L);
}
return err;
}

7
src/bindings/bindings.h Normal file → Executable file
View File

@ -18,7 +18,7 @@ extern "C" {
struct quicklist;
int tolua_sqlite_open(struct lua_State *L);
int tolua_eressea_open(struct lua_State *L);
int tolua_bindings_open(struct lua_State *L);
int tolua_spelllist_next(struct lua_State *L);
int tolua_itemlist_next(struct lua_State *L);
int tolua_orderlist_next(struct lua_State *L);
@ -26,6 +26,11 @@ extern "C" {
const char *elem_type, struct quicklist *list);
int log_lua_error(struct lua_State *L);
void lua_done(struct lua_State *L);
struct lua_State *lua_init(void);
int eressea_run(struct lua_State *L, const char *luafile, const char *entry_point);
#ifdef __cplusplus
}
#endif

7
src/bindings/eressea.pkg Executable file
View File

@ -0,0 +1,7 @@
$#include <bindings/bind_eressea.h>
module eressea {
void eressea_free_game @ free_game(void);
int eressea_read_game @ read_game(const char * filename);
int eressea_write_game @ write_game(const char * filename);
}

8
src/bindings/process.pkg Executable file
View File

@ -0,0 +1,8 @@
$#include <bindings/bind_process.h>
module eressea {
module process {
void process_markets @ markets(void);
void process_produce @ produce(void);
}
}

8
src/bindings/settings.pkg Executable file
View File

@ -0,0 +1,8 @@
$#include <bindings/bind_settings.h>
module eressea {
module settings {
void settings_set @ set(const char *key, const char *value);
const char * settings_get @ get(const char *key);
}
}

134
src/eressea.c Normal file → Executable file
View File

@ -3,30 +3,8 @@
#include "eressea.h"
#include <kernel/config.h>
#include <util/console.h>
#include <util/log.h>
/* lua includes */
#ifdef BINDINGS_TOLUA
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <bindings/bindings.h>
#include <bindings/helpers.h>
#ifdef BSON_ATTRIB
# include <bindings/bind_attrib.h>
#endif
#include <bindings/bind_building.h>
#include <bindings/bind_faction.h>
#include <bindings/bind_gmtool.h>
#include <bindings/bind_hashtable.h>
#include <bindings/bind_message.h>
#include <bindings/bind_region.h>
#include <bindings/bind_ship.h>
#include <bindings/bind_storage.h>
#include <bindings/bind_unit.h>
#endif /* BINDINGS_TOLUA */
#if MUSEUM_MODULE
#include <modules/museum.h>
#endif
@ -52,68 +30,7 @@
#include <items/itemtypes.h>
#include <attributes/attributes.h>
static const struct {
const char *name;
int (*func) (lua_State *);
} lualibs[] = {
{
"", luaopen_base}, {
LUA_TABLIBNAME, luaopen_table}, {
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
{
NULL, NULL}
};
static void openlibs(lua_State * L)
{
int i;
for (i = 0; lualibs[i].func; ++i) {
lua_pushcfunction(L, lualibs[i].func);
lua_pushstring(L, lualibs[i].name);
lua_call(L, 1, 0);
}
}
static void lua_done(lua_State * L)
{
lua_close(L);
}
static lua_State *lua_init(void)
{
lua_State *L = lua_open();
openlibs(L);
#ifdef BINDINGS_TOLUA
register_tolua_helpers();
tolua_eressea_open(L);
tolua_sqlite_open(L);
tolua_unit_open(L);
tolua_building_open(L);
tolua_ship_open(L);
tolua_region_open(L);
tolua_faction_open(L);
#ifdef BSON_ATTRIB
tolua_attrib_open(L);
#endif
tolua_unit_open(L);
tolua_message_open(L);
tolua_hashtable_open(L);
tolua_gmtool_open(L);
tolua_storage_open(L);
#endif
return L;
}
static void game_done(void)
void game_done(void)
{
#ifdef CLEANUP_CODE
/* Diese Routine enfernt allen allokierten Speicher wieder. Das ist nur
@ -129,10 +46,12 @@ static void game_done(void)
#endif
calendar_cleanup();
#endif
kernel_done();
}
static void game_init(void)
void game_init(void)
{
kernel_init();
register_triggers();
register_xmas();
@ -165,48 +84,3 @@ static void game_init(void)
register_gmcmd();
}
int eressea_init(void)
{
global.vm_state = lua_init();
kernel_init();
game_init();
return 0;
}
void eressea_done(void)
{
game_done();
kernel_done();
lua_done((lua_State *) global.vm_state);
}
int eressea_run(const char *luafile, const char *entry_point)
{
int err;
lua_State *L = (lua_State *) global.vm_state;
/* run the main script */
if (luafile) {
lua_getglobal(L, "dofile");
lua_pushstring(L, luafile);
err = lua_pcall(L, 1, 0, 0);
if (err != 0) {
log_lua_error(L);
abort();
return err;
}
}
if (entry_point) {
lua_getglobal(L, entry_point);
err = lua_pcall(L, 0, 1, 0);
if (err != 0) {
log_lua_error(L);
abort();
return err;
}
} else {
err = lua_console(L);
}
return err;
}

5
src/eressea.h Normal file → Executable file
View File

@ -4,9 +4,8 @@
extern "C" {
#endif
int eressea_init(void);
void eressea_done(void);
int eressea_run(const char *luafile, const char *entry_point);
void game_init(void);
void game_done(void);
#ifdef __cplusplus
}

15
src/kernel/faction.c Normal file → Executable file
View File

@ -284,19 +284,22 @@ void destroyfaction(faction * f)
unit *u = f->units;
faction *ff;
if (!f->alive)
if (!f->alive) {
return;
}
fset(f, FFL_QUIT);
spellbook_clear(f->spellbook);
free(f->spellbook);
f->spellbook = 0;
if (f->spellbook) {
spellbook_clear(f->spellbook);
free(f->spellbook);
f->spellbook = 0;
}
while (f->battles) {
struct bmsg *bm = f->battles;
f->battles = bm->next;
if (bm->msgs)
if (bm->msgs) {
free_messagelist(bm->msgs);
}
free(bm);
}