Skip to content

Commit

Permalink
Merge pull request CleverRaven#13352 from BevapDin/tuk
Browse files Browse the repository at this point in the history
Reset Lua state when a new world is loaded
  • Loading branch information
Coolthulhu committed Aug 20, 2015
2 parents 3a8c08e + 932ee73 commit d6882d9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 73 deletions.
38 changes: 34 additions & 4 deletions src/catalua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern "C" {

using item_stack_iterator = std::list<item>::iterator;

lua_State *lua_state;
lua_State *lua_state = nullptr;

// Keep track of the current mod from which we are executing, so that
// we know where to load files from.
Expand All @@ -68,6 +68,8 @@ static void luaL_setfuncs( lua_State * const L, const luaL_Reg arrary[], int con
}
#endif

void lua_dofile(lua_State *L, const char *path);

// Helper functions for making working with the lua API more straightforward.
// --------------------------------------------------------------------------

Expand Down Expand Up @@ -722,7 +724,7 @@ int call_lua(std::string tocall)
}


void lua_callback(lua_State *, const char *callback_name)
void lua_callback(const char *callback_name)
{
call_lua(std::string("mod_callback(\"") + std::string(callback_name) + "\")");
}
Expand Down Expand Up @@ -953,7 +955,7 @@ static int game_register_iuse(lua_State *L)
#include "lua/catabindings.cpp"

// Load the main file of a mod
void lua_loadmod(lua_State *L, std::string base_path, std::string main_file_name)
void lua_loadmod(std::string base_path, std::string main_file_name)
{
std::string full_path = base_path + "/" + main_file_name;

Expand All @@ -962,7 +964,7 @@ void lua_loadmod(lua_State *L, std::string base_path, std::string main_file_name
int file_exists = stat(full_path.c_str(), &buffer) == 0;
if(file_exists) {
lua_file_path = base_path;
lua_dofile(L, full_path.c_str());
lua_dofile( lua_state, full_path.c_str() );
lua_file_path = "";
}
// debugmsg("Loading from %s", full_path.c_str());
Expand Down Expand Up @@ -1038,6 +1040,11 @@ static const struct luaL_Reg global_funcs [] = {
// Lua initialization.
void game::init_lua()
{
// This is called on each new-game, the old state (if any) is closed to dispose any data
// introduced by mods of the previously loaded world.
if( lua_state != nullptr ) {
lua_close( lua_state );
}
lua_state = luaL_newstate();
if( lua_state == nullptr ) {
debugmsg( "Failed to start Lua. Lua scripting won't be available." );
Expand Down Expand Up @@ -1175,3 +1182,26 @@ long use_function::call( player *player_instance, item *item_instance, bool acti
}
return 0;
}

#ifndef LUA
/* Empty functions for builds without Lua: */
int lua_monster_move( monster * )
{
return 0;
}
int call_lua( std::string ) {
popup( "This binary was not compiled with Lua support." );
return 0;
}
// Implemented in mapgen.cpp:
// int lua_mapgen( map *, std::string, mapgendata, int, float, const std::string & )
void lua_callback( const char * )
{
}
void lua_loadmod( std::string, std::string )
{
}
void game::init_lua()
{
}
#endif
21 changes: 2 additions & 19 deletions src/catalua.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@

#include <string>

#ifdef LUA

class map;
class monster;
struct mapgendata;

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

extern lua_State *lua_state;

/** If this returns 0, no lua function was defined to override behavior.
* If this returns 1, lua behavior was called and regular behavior should be omitted.
*/
Expand All @@ -29,24 +19,17 @@ int call_lua(std::string tocall);
int lua_mapgen(map *m, std::string terrain_type, mapgendata md, int t, float d,
const std::string &scr);

/**
* Execute a lua file.
*/
void lua_dofile(lua_State *L, const char *path);

/**
* Execute a callback that can be overriden by all mods.
*/
void lua_callback(lua_State *L, const char *callback_name);
void lua_callback(const char *callback_name);

/**
* Load the main file of a lua mod.
*
* @param base_path The base path of the mod.
* @param main_file_name The file name of the lua file, usually "main.lua"
*/
void lua_loadmod(lua_State *L, std::string base_path, std::string main_file_name);

#endif
void lua_loadmod(std::string base_path, std::string main_file_name);

#endif
22 changes: 4 additions & 18 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ game::game() :
// Load everything that will not depend on any mods
void game::load_static_data()
{
#ifdef LUA
init_lua(); // Set up lua (SEE catalua.cpp)
#endif
// UI stuff, not mod-specific per definition
inp_mngr.init(); // Load input config JSON
// Init mappings for loading the json stuff
Expand Down Expand Up @@ -232,31 +229,26 @@ void game::load_core_data()
// anyway.
DynamicDataLoader::get_instance().unload_data();

init_lua();
load_data_from_dir(FILENAMES["jsondir"]);
}

void game::load_data_from_dir(const std::string &path)
{
#ifdef LUA
// Process a preload file before the .json files,
// so that custom IUSE's can be defined before
// the items that need them are parsed

lua_loadmod(lua_state, path, "preload.lua");
#endif
lua_loadmod( path, "preload.lua" );

try {
DynamicDataLoader::get_instance().load_data_from_path(path);
} catch( const std::exception &err ) {
debugmsg("Error loading data from json: %s", err.what());
}

#ifdef LUA
// main.lua will be executed after JSON, allowing to
// work with items defined by mod's JSON

lua_loadmod(lua_state, path, "main.lua");
#endif
lua_loadmod( path, "main.lua" );
}

game::~game()
Expand Down Expand Up @@ -1202,9 +1194,7 @@ bool game::do_turn()
if (calendar::turn.hours() == 0 && calendar::turn.minutes() == 0 &&
calendar::turn.seconds() == 0) { // Midnight!
overmap_buffer.process_mongroups();
#ifdef LUA
lua_callback(lua_state, "on_day_passed");
#endif
lua_callback("on_day_passed");
}

if( calendar::once_every(MINUTES(5)) ) { //move hordes every 5 min
Expand Down Expand Up @@ -4208,12 +4198,8 @@ void game::debug()
break;

case 24: {
#ifdef LUA
std::string luacode = string_input_popup(_("Lua:"), TERMX, "", "", "LUA");
call_lua(luacode);
#else
popup( "This binary was not compiled with Lua support." );
#endif
}
break;
case 25:
Expand Down
39 changes: 10 additions & 29 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
#include "npc.h"
#include "vehicle.h"
#include "vehicle_group.h"
#ifdef LUA
#include "catalua.h"
#endif

#ifndef sgn
#define sgn(x) (((x) < 0) ? -1 : 1)
Expand Down Expand Up @@ -308,7 +306,6 @@ mapgen_function * load_mapgen_function(JsonObject &jio, const std::string id_bas
debugmsg("oter_t[%s]: Invalid mapgen function (missing \"name\" value).", id_base.c_str(), mgtype.c_str() );
}
} else if ( mgtype == "lua" ) { // lua script
#ifdef LUA
if ( jio.has_string("script") ) { // minified into one\nline
const std::string mgscript = jio.get_string("script");
ret = new mapgen_function_lua( mgscript, mgweight );
Expand All @@ -327,8 +324,8 @@ mapgen_function * load_mapgen_function(JsonObject &jio, const std::string id_bas
} else {
debugmsg("oter_t[%s]: Invalid mapgen function (missing \"script\" or \"file\" value).", id_base.c_str() );
}
#else
debugmsg("oter_t[%s]: mapgen entry requires a build with LUA=1.",id_base.c_str() );
#ifndef LUA
dbg( D_ERROR ) << "oter_t " << id_base << ": mapgen entry requires a build with LUA=1.";
#endif
} else if ( mgtype == "json" ) {
if ( jio.has_object("object") ) {
Expand Down Expand Up @@ -1345,8 +1342,6 @@ bool mapgen_function_json::setup() {
objects.load_objects<jmapgen_monster>( jo, "place_monster" );
objects.load_objects<jmapgen_make_rubble>( jo, "place_rubble" );

#ifdef LUA
// silently ignore if unsupported in build
if ( jo.has_string("lua") ) { // minified into one\nline
luascript = jo.get_string("lua");
} else if ( jo.has_array("lua") ) { // or 1 line per entry array
Expand All @@ -1357,7 +1352,6 @@ bool mapgen_function_json::setup() {
luascript += "\n";
}
}
#endif

} catch( const JsonError &e ) {
debugmsg("Bad JSON mapgen, discarding:\n %s\n", e.c_str() );
Expand Down Expand Up @@ -1465,7 +1459,6 @@ bool jmapgen_setmap::apply( map *m ) {
return true;
}

void mapgen_lua(map * m, oter_id id, mapgendata md, int t, float d, const std::string & scr);
/*
* Apply mapgen as per a derived-from-json recipe; in theory fast, but not very versatile
*/
Expand All @@ -1479,14 +1472,9 @@ void mapgen_function_json::generate( map *m, oter_id terrain_type, mapgendata md
for( auto &elem : setmap_points ) {
elem.apply( m );
}
#ifdef LUA
if ( ! luascript.empty() ) {
mapgen_lua(m, terrain_type, md, t, d, luascript);
lua_mapgen( m, std::string( terrain_type ), md, t, d, luascript );
}
#else
(void)md;
(void)t;
#endif

objects.apply(m, d);

Expand Down Expand Up @@ -1517,14 +1505,9 @@ void jmapgen_objects::apply(map *m, float density) const {
///// lua mapgen functions
// wip: need moar bindings. Basic stuff works

/*
* Apply interpreted script; slowest, more versatile eventually.
*/
void mapgen_lua(map * m,oter_id id,mapgendata md ,int t,float d, const std::string & scr) {
#ifdef LUA
lua_mapgen(m, std::string(id), md, t, d, scr);
#else
(void)scr;
#ifndef LUA
int lua_mapgen( map *m, std::string id, mapgendata md, int t, float d, const std::string & )
{
mapgen_crater(m,id,md,t,d);
mapf::formatted_set_simple(m, 0, 6,
"\
Expand All @@ -1539,16 +1522,14 @@ void mapgen_lua(map * m,oter_id id,mapgendata md ,int t,float d, const std::stri
* * * ***\n\
* * * * *\n\
***** *** * *\n\
", mapf::basic_bind("*", t_paper), mapf::basic_bind("")); // should never happen: overmap loader skips lua mapgens on !LUA builds.

#endif
", mapf::basic_bind("*", t_paper), mapf::basic_bind(""));
return 0;
}
#endif

#ifdef LUA
void mapgen_function_lua::generate( map *m, oter_id terrain_type, mapgendata dat, int t, float d ) {
mapgen_lua(m, terrain_type, dat, t, d, scr );
lua_mapgen( m, std::string( terrain_type ), dat, t, d, scr );
}
#endif

/////////////
// TODO: clean up variable shadowing in this function
Expand Down
3 changes: 0 additions & 3 deletions src/mapgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ class mapgen_function_lua : public virtual mapgen_function {
mapgen_function_lua(std::string s, int w = 1000) : mapgen_function( w ), scr(s) {
// scr = s; // todo; if ( luaL_loadstring(L, scr.c_str() ) ) { error }
}
#if defined(LUA)
// Prevents instantiating this class in non-lua builds
virtual void generate(map*, oter_id, mapgendata, int, float) override;
#endif
};
/////////////////////////////////////////////////////////
///// global per-terrain mapgen function lists
Expand Down

0 comments on commit d6882d9

Please sign in to comment.