Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Commit

Permalink
Now Lua can be used in scripts tag in faction tag in XML
Browse files Browse the repository at this point in the history
  • Loading branch information
mathusummut committed May 6, 2018
1 parent c760a14 commit d1acd87
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion source/glest_game/game/script_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,16 @@ namespace
script->getCode() + "end\n",
script->getName());
}

//load code
for (int i = 0; i < world->getFactionCount(); ++i) {
FactionType const* type = world->getFaction(i)->getType();
for (int j = 0; j < type->getScriptCount(); j++) {
const Script* script = type->getScript(j);
luaScript.loadCode("function " + script->getName() + "()" +
script->getCode() + "end\n",
script->getName());
}
}

//!!!
// string data_path= getGameReadWritePath(GameConstants::path_data_CacheLookupKey);
Expand Down
19 changes: 19 additions & 0 deletions source/glest_game/types/faction_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,16 @@ namespace Glest {

}

if (factionNode->hasChild("scripts")) {
const XmlNode *scriptsNode = factionNode->getChild("scripts");

for (int i = 0; i < (int) scriptsNode->getChildCount(); ++i) {
const XmlNode *scriptNode = scriptsNode->getChild(i);

scripts.emplace_back(Script(getFunctionName(scriptNode), scriptNode->getText()));
}
}

//read ai behavior
if (factionNode->hasChild("ai-behavior") == true) {
const XmlNode *aiNode = factionNode->getChild("ai-behavior");
Expand Down Expand Up @@ -571,6 +581,15 @@ namespace Glest {
c_str(), __FUNCTION__, __LINE__);
}

string FactionType::getFunctionName(const XmlNode * scriptNode) {
string name = scriptNode->getName();

for (int i = 0; i < (int) scriptNode->getAttributeCount(); ++i) {
name += "_" + scriptNode->getAttribute(i)->getValue();
}
return name;
}

int FactionType::
getAIBehaviorStaticOverideValue(AIBehaviorStaticValueCategory type)
const {
Expand Down
9 changes: 9 additions & 0 deletions source/glest_game/types/faction_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# include <string>
# include "util.h"
# include "leak_dumper.h"
# include "scenario.h"

using Shared::Sound::StrSound;

Expand Down Expand Up @@ -103,6 +104,7 @@ namespace Glest {
std::vector < const UpgradeType *>vctAIBehaviorUpgrades;
std::map < AIBehaviorStaticValueCategory,
int >mapAIBehaviorStaticOverrideValues;
std::vector<Script> scripts;

bool isLinked;

Expand Down Expand Up @@ -133,6 +135,7 @@ namespace Glest {
int getAIBehaviorStaticOverideValue(AIBehaviorStaticValueCategory type)
const;

string getFunctionName(const XmlNode * scriptNode);
//get
bool getIsLinked() const {
return isLinked;
Expand All @@ -147,6 +150,12 @@ namespace Glest {
const UnitType *getUnitType(int i) const {
return &unitTypes[i];
}
int getScriptCount() const {
return (int) scripts.size();
}
const Script *getScript(int i) const {
return &scripts[i];
}
const UpgradeType *getUpgradeType(int i) const {
return &upgradeTypes[i];
}
Expand Down
6 changes: 6 additions & 0 deletions source/glest_game/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ namespace Glest {
techTree = new TechTree(pathList);
techtreeChecksum = techTree->loadTech(techName, factions,
checksum, loadedFileList, validationMode);
XmlTree xmlTree;
string currentPath = techTree->findPath(techName, pathList);
endPathWithSlash(currentPath);
string path = currentPath + lastDir(currentPath) + ".xml";
const XmlNode *techTreeNode = xmlTree.getRootNode();
if (scriptManager) scriptManager->init(this, this->getGame()->getGameCameraPtr(), techTreeNode);
return techtreeChecksum;
}

Expand Down

0 comments on commit d1acd87

Please sign in to comment.