From d8fcd104aea19c8f93ad15c24bef6c99a8b209c1 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Wed, 5 Nov 2014 02:35:42 -0500 Subject: [PATCH] add support to create scenarios in lua also --- src/generators/lua_map_generator.cpp | 42 ++++++++++++++++++++++++++++ src/generators/lua_map_generator.hpp | 2 ++ 2 files changed, 44 insertions(+) diff --git a/src/generators/lua_map_generator.cpp b/src/generators/lua_map_generator.cpp index 163d5238349..ba5585056c7 100644 --- a/src/generators/lua_map_generator.cpp +++ b/src/generators/lua_map_generator.cpp @@ -150,6 +150,7 @@ lua_map_generator::lua_map_generator(const config & cfg) : id_(cfg["id"]) , config_name_(cfg["config_name"]) , create_map_(cfg["create_map"]) + , create_scenario_(cfg["create_scenario"]) , mState_(luaL_newstate()) { const char* required[] = {"id", "config_name", "create_map"}; @@ -214,3 +215,44 @@ std::string lua_map_generator::create_map() } return lua_tostring(mState_, -1); } + +config lua_map_generator::create_scenario() +{ + if (!create_scenario_.size()) { + return map_generator::create_scenario(); + } + + { + int errcode = luaL_loadstring(mState_, create_scenario_.c_str()); + if (errcode != LUA_OK) { + std::string msg = "Error when running lua_map_generator create_scenario.\n"; + msg += "The generator was: " + config_name_ + "\n"; + msg += "Error when parsing create_scenario function. "; + if (errcode == LUA_ERRSYNTAX) { + msg += "There was a syntax error:\n"; + } else { + msg += "There was a memory error:\n"; + } + msg += lua_tostring(mState_, -1); + throw mapgen_exception(msg); + } + } + { + int errcode = lua_pcall(mState_, 0, 1, 0); + if (errcode != LUA_OK) { + std::string msg = "Error when running lua_map_generator create_scenario.\n"; + msg += "The generator was: " + config_name_ + "\n"; + msg += "Error when running create_scenario function. "; + if (errcode == LUA_ERRRUN) { + msg += "There was a runtime error:\n"; + } else if (errcode == LUA_ERRERR) { + msg += "There was an error with the attached debugger:\n"; + } else { + msg += "There was a memory or garbage collection error:\n"; + } + msg += lua_tostring(mState_, -1); + throw mapgen_exception(msg); + } + } + return luaW_checkconfig(mState_, -1); +} diff --git a/src/generators/lua_map_generator.hpp b/src/generators/lua_map_generator.hpp index 2f6a87784a7..ddd06876873 100644 --- a/src/generators/lua_map_generator.hpp +++ b/src/generators/lua_map_generator.hpp @@ -41,11 +41,13 @@ public: std::string config_name() const { return config_name_; } virtual std::string create_map(); + virtual config create_scenario(); private: std::string id_, config_name_; std::string create_map_; + std::string create_scenario_; lua_State * mState_; };