add support to create scenarios in lua also

This commit is contained in:
Chris Beck 2014-11-05 02:35:42 -05:00
parent ce99658dfe
commit d8fcd104ae
2 changed files with 44 additions and 0 deletions

View File

@ -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);
}

View File

@ -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_;
};