- Add a simple macro system for auto lines (the goal is to help mostly with repetitive card auto lines such as the ones we have in MotD mod) Check the MotD mod for examples.
-- I added an AbilityParser.cpp file, mid term goal is to move AbilityFactory there, so that MTGAbility.cpp becomes a bit less big. -- I tried to add the file reference in Makefiles, but only tested windows compilation so far - Fixed bugs related to "castRestriction" variables in MTGAbility. these variables were declared in both the parent and children classes, leading to bugs and duplicate code/content The test suite passes
This commit is contained in:
126
projects/mtg/src/AbilityParser.cpp
Normal file
126
projects/mtg/src/AbilityParser.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
|
||||
#include "AbilityParser.h"
|
||||
#include "utils.h"
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
vector<AutoLineMacro *> AutoLineMacro::gAutoLineMacros;
|
||||
map<string, bool> AutoLineMacro::gAutoLineMacrosIndex;
|
||||
|
||||
AutoLineMacro::AutoLineMacro(string& s)
|
||||
{
|
||||
parse(s);
|
||||
}
|
||||
|
||||
void AutoLineMacro::parse(string& s)
|
||||
{
|
||||
//we convert to lower, because the counterpart (auto strings) is converted to lower at parse time
|
||||
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
|
||||
|
||||
size_t firstSpace = s.find(" ");
|
||||
if (firstSpace == string::npos)
|
||||
{
|
||||
DebugTrace("FATAL:error parsing macro : " << s);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t firstParenthesis = s.find("(");
|
||||
|
||||
if (firstParenthesis != string::npos && firstParenthesis < firstSpace)
|
||||
{
|
||||
//The string has params
|
||||
mName = s.substr(0, firstParenthesis);
|
||||
size_t firstClosingParenthesis = s.find(")");
|
||||
string params = s.substr(firstParenthesis + 1,firstClosingParenthesis - (firstParenthesis + 1));
|
||||
mParams = split(params, ',');
|
||||
mResult = s.substr(firstClosingParenthesis + 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
//no params
|
||||
mName = s.substr(0, firstSpace);
|
||||
mResult = s.substr(firstSpace + 1);
|
||||
}
|
||||
|
||||
boost::replace_all(mResult, "\\n", "\n");
|
||||
}
|
||||
|
||||
string AutoLineMacro::process(string& s)
|
||||
{
|
||||
string temp = s;
|
||||
if (!mParams.size())
|
||||
{
|
||||
//no params, simple macro
|
||||
boost::replace_all(temp, mName, mResult);
|
||||
return temp;
|
||||
}
|
||||
|
||||
//params, complex macro
|
||||
string toFind = mName + "(";
|
||||
string result;
|
||||
size_t occurence = temp.find(toFind);
|
||||
if (occurence == string::npos)
|
||||
return s;
|
||||
|
||||
while (occurence != string::npos)
|
||||
{
|
||||
result.append(temp.substr(0, occurence));
|
||||
size_t closingParenthesis = temp.find(")");
|
||||
size_t paramsStart = occurence + toFind.length();
|
||||
string params = temp.substr(paramsStart, closingParenthesis - paramsStart);
|
||||
|
||||
vector<string> vParams = split(params, ',');
|
||||
if (vParams.size() != mParams.size())
|
||||
{
|
||||
return s;
|
||||
}
|
||||
string tempResult = mResult;
|
||||
for (size_t i = 0; i < vParams.size(); ++i)
|
||||
{
|
||||
boost::replace_all(tempResult, mParams[i], vParams[i]);
|
||||
}
|
||||
result.append(tempResult);
|
||||
|
||||
temp = temp.substr(closingParenthesis + 1);
|
||||
occurence = temp.find(toFind);
|
||||
}
|
||||
result.append(temp);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
bool AutoLineMacro::AddMacro(string& s)
|
||||
{
|
||||
AutoLineMacro * alm = NEW AutoLineMacro(s);
|
||||
if (gAutoLineMacrosIndex[alm->mName])
|
||||
{
|
||||
DebugTrace("WARNING, Macro already exists: " << alm->mName);
|
||||
delete alm;
|
||||
return false;
|
||||
}
|
||||
|
||||
gAutoLineMacrosIndex[alm->mName] = true;
|
||||
gAutoLineMacros.push_back(alm);
|
||||
return true;
|
||||
}
|
||||
|
||||
void AutoLineMacro::Destroy()
|
||||
{
|
||||
for (size_t i = 0; i < gAutoLineMacros.size(); ++i)
|
||||
{
|
||||
SAFE_DELETE(gAutoLineMacros[i]);
|
||||
}
|
||||
}
|
||||
|
||||
string AutoLineMacro::Process(string& s)
|
||||
{
|
||||
string result = s;
|
||||
for (size_t i = 0; i < gAutoLineMacros.size(); ++i)
|
||||
{
|
||||
result = gAutoLineMacros[i]->process(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user