Fixed LUA loading for multiple factions

This commit is contained in:
mathusummut
2019-02-04 00:30:05 +01:00
parent e12e4ff732
commit a124a4a75a
2 changed files with 40 additions and 15 deletions

View File

@@ -552,24 +552,35 @@ namespace Game {
luaScript.registerFunction(getFactionPlayerType, luaScript.registerFunction(getFactionPlayerType,
"getFactionPlayerType"); "getFactionPlayerType");
//load code map<string, Script> scripts;
for (int i = 0; i < scenario->getScriptCount(); ++i) { map<string, Script>::iterator iter;
const Script *
script = scenario->getScript(i); //load faction code
luaScript.loadCode("function " + script->getName() + "()" +
script->getCode() + "end\n",
script->getName());
}
//load code
for (int i = 0; i < world->getFactionCount(); ++i) { for (int i = 0; i < world->getFactionCount(); ++i) {
FactionType const* type = world->getFaction(i)->getType(); FactionType const* type = world->getFaction(i)->getType();
for (int j = 0; j < type->getScriptCount(); j++) { for (int j = 0; j < type->getScriptCount(); j++) {
const Script* script = type->getScript(j); const Script* script = type->getScript(j);
luaScript.loadCode("function " + script->getName() + "()" + iter = scripts.find(script->getName());
script->getCode() + "end\n", if (iter == scripts.end())
script->getName()); scripts[script->getName()] = *script;
else
iter->second.appendCode(script->getCode());
} }
} }
//load scenario code
for (int i = 0; i < scenario->getScriptCount(); ++i) {
const Script* script = scenario->getScript(i);
iter = scripts.find(script->getName());
if (iter == scripts.end())
scripts[script->getName()] = *script;
else
iter->second.appendCode(script->getCode());
}
for (iter = scripts.begin(); iter != scripts.end(); ++iter) {
luaScript.loadCode("function " + iter->second.getName() + "()" +
iter->second.getCode() + "end\n", iter->second.getName());
}
//setup message box //setup message box
messageBox.init(Lang::getInstance().getString("Ok")); messageBox.init(Lang::getInstance().getString("Ok"));

View File

@@ -117,15 +117,29 @@ namespace Game {
string code; string code;
public: public:
Script(const string & name, const string & code) { Script() {
}
Script(const string& name, const string& code) {
this->name = name; this->name = name;
this->code = code; this->code = code;
} const string & getName() const { }
const string& getName() const {
return name; return name;
} }
const string & getCode() const {
const string& getCode() const {
return code; return code;
} }
void setCode(const string& newCode) {
code = newCode;
}
void appendCode(const string& newCode) {
code += newCode;
}
}; };
// ===================================================== // =====================================================