1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

Entities redesign and a few ERM features

* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
This commit is contained in:
AlexVinS
2018-03-17 17:58:30 +03:00
committed by AlexVinS
parent 11bb46780a
commit ecaa9f5d0b
475 changed files with 22491 additions and 7123 deletions

View File

@@ -9,10 +9,11 @@
*/
#pragma once
#include "ERMParser.h"
#include "ERMScriptModule.h"
class ERMInterpreter;
namespace VERMInterpreter
{
using namespace ERM;
@@ -98,7 +99,6 @@ namespace VERMInterpreter
{}
};
// All numeric variables are integer variables and have a range of -2147483647...+2147483647
// c stores game active day number //indirect variable
// d current value //not an actual variable but a modifier
@@ -114,73 +114,25 @@ namespace VERMInterpreter
// z1..z1000 String variables //global
// z-1..z-10 Function local string variables //local
struct TriggerLocalVars
{
static const int EVAR_NUM = 100; //number of evar locals
static const int YVAR_NUM = 100; //number of yvar locals
TriggerLocalVars();
double & getEvar(int num);
int & getYvar(int num);
private:
double evar[EVAR_NUM]; //negative indices
int yvar[YVAR_NUM];
};
struct FunctionLocalVars
{
static const int NUM_PARAMETERS = 16; //number of function parameters
static const int NUM_LOCALS = 100;
static const int NUM_STRINGS = 10;
static const int NUM_FLOATINGS = 100;
int & getParam(int num);
int & getLocal(int num);
std::string & getString(int num);
double & getFloat(int num);
void reset();
private:
int params[NUM_PARAMETERS]; //x-vars
int locals[NUM_LOCALS]; //y-vars
std::string strings[NUM_STRINGS]; //z-vars (negative indices)
double floats[NUM_FLOATINGS]; //e-vars (positive indices)
};
struct ERMEnvironment
{
ERMEnvironment();
static const int NUM_QUICKS = 't' - 'f' + 1; //it should be 15
int & getQuickVar(const char letter); //'f' - 't' variables
int & getStandardVar(int num); //get v-variable
std::string & getZVar(int num);
bool & getFlag(int num);
static const int NUM_STANDARDS = 10000;
static const int NUM_STRINGS = 1000;
std::map<std::string, ERM::TVarExpNotMacro> macroBindings;
static const int NUM_FLAGS = 1000;
private:
int quickVars[NUM_QUICKS]; //referenced by letter ('f' to 't' inclusive)
int standardVars[NUM_STANDARDS]; //v-vars
std::string strings[NUM_STRINGS]; //z-vars (positive indices)
bool flags[NUM_FLAGS];
};
struct TriggerType
{
//the same order of trigger types in this enum and in validTriggers array is obligatory!
enum ETrigType{AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR,
MW, OB, PI, SN, TH, TM} type;
enum ETrigType
{
AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR, MW, OB, PI, SN, TH, TM
};
ETrigType type;
static ETrigType convertTrigger(const std::string & trig)
{
static const std::string validTriggers[] = {"AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
static const std::string validTriggers[] =
{
"AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
"GE", "GM", "HE", "HL", "HM", "IP", "LE", "MF", "MG", "MM", "MR", "MW", "OB", "PI", "SN",
"TH", "TM"};
"TH", "TM"
};
for(int i=0; i<ARRAY_COUNT(validTriggers); ++i)
{
@@ -202,38 +154,30 @@ namespace VERMInterpreter
};
struct FileInfo
{
std::string filename;
int length;
};
struct LinePointer
{
const FileInfo * file; //non-owning
int lineNum;
int realLineNum;
int fileLength;
LinePointer() : file(nullptr)
LinePointer()
: fileLength(-1)
{}
LinePointer(const FileInfo * finfo, int line, int _realLineNum) : file(finfo), lineNum(line),
LinePointer(int _fileLength, int line, int _realLineNum)
: fileLength(_fileLength),
lineNum(line),
realLineNum(_realLineNum)
{}
//lexicographical order
bool operator<(const LinePointer & rhs) const
{
if(file->filename != rhs.file->filename)
return file->filename < rhs.file->filename;
return lineNum < rhs.lineNum;
}
bool operator!=(const LinePointer & rhs) const
{
return file->filename != rhs.file->filename || lineNum != rhs.lineNum;
return lineNum != rhs.lineNum;
}
LinePointer & operator++()
{
@@ -242,45 +186,14 @@ namespace VERMInterpreter
}
bool isValid() const
{
return file && lineNum < file->length;
return fileLength > 0 && lineNum < fileLength;
}
};
struct LexicalPtr
{
LinePointer line; //where to start
std::vector<int> entryPoints; //defines how to pass to current location
bool operator<(const LexicalPtr & sec) const
{
if(line != sec.line)
return line < sec.line;
if(entryPoints.size() != sec.entryPoints.size())
return entryPoints.size() < sec.entryPoints.size();
for(int g=0; g<entryPoints.size(); ++g)
{
if(entryPoints[g] < sec.entryPoints[g])
return true;
}
return false;
}
};
//call stack, represents dynamic range
struct Stack
{
std::vector<LexicalPtr> stack;
};
struct Trigger
{
LinePointer line;
TriggerLocalVars ermLocalVars;
Stack * stack; //where we are stuck at execution
Trigger() : stack(nullptr)
Trigger()
{}
};
@@ -302,99 +215,7 @@ namespace VERMInterpreter
typedef boost::variant<char, double, int, std::string> TLiteral;
//for operator <, but this one seems to be implemented in boost alerady
struct _opLTvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opLTvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) < rhs;
}
};
// bool operator<(const TLiteral & t1, const TLiteral & t2)
// {
// if(t1.type() == t2.type())
// {
// return boost::apply_visitor(_opLTvis(t1), t2);
// }
// throw EVermScriptExecError("These types are incomparable!");
// }
//for operator <=
struct _opLEvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opLEvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) <= rhs;
}
};
bool operator<=(const TLiteral & t1, const TLiteral & t2);
//operator >
struct _opGTvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opGTvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) > rhs;
}
};
bool operator>(const TLiteral & t1, const TLiteral & t2);
//operator >=
struct _opGEvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opGEvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) >= rhs;
}
};
bool operator>=(const TLiteral & t1, const TLiteral & t2);
//operator =
struct _opEQvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opEQvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) == rhs;
}
};
//VFunc
struct VFunc;
//VOption & stuff
typedef boost::variant<VNIL, boost::recursive_wrapper<VNode>, VSymbol, TLiteral, ERM::Tcommand, boost::recursive_wrapper<VFunc> > VOption; //options in v-expression, VNIl should be the default
typedef boost::variant<VNIL, boost::recursive_wrapper<VNode>, VSymbol, TLiteral, ERM::Tcommand> VOption; //options in v-expression, VNIl should be the default
template<typename T, typename SecType>
T& getAs(SecType & opt)
@@ -414,84 +235,6 @@ namespace VERMInterpreter
return false;
}
//why it doesn't work?
// template<typename TBasicVariant>
// struct IntVarinant : public TBasicVariant
// {
// template<typename T>
// bool isA() const
// {
// return type() == typeid(T);
// }
// template<typename T>
// T getAs()
// {
// if(isA<T>())
// return boost::get<T>(*this);
// else
// throw EVermScriptExecError("Getting improved variant with wrongly specified type");
// }
//
// IntVarinant(const VNode & val) : TBasicVariant(val)
// {}
// IntVarinant(const VNIL & val) : TBasicVariant(val)
// {}
// IntVarinant(const TLiteral & val) : TBasicVariant(val)
// {}
// IntVarinant(const VSymbol & val) : TBasicVariant(val)
// {}
// IntVarinant(const int & val) : TBasicVariant(val)
// {}
// IntVarinant(const char & val) : TBasicVariant(val)
// {}
// IntVarinant(const double & val) : TBasicVariant(val)
// {}
// IntVarinant(const ERM::Tcommand & val) : TBasicVariant(val)
// {}
// TBasicVariant & getAsPlaintVariant()
// {
// return *this;
// }
//
// IntVarinant()
// {}
// };
///main environment class, manages symbols
class Environment
{
private:
std::map<std::string, VOption> symbols;
Environment * parent;
public:
Environment() : parent(nullptr)
{}
void setPatent(Environment * _parent);
Environment * getPatent() const;
enum EIsBoundMode {GLOBAL_ONLY, LOCAL_ONLY, ANYWHERE};
bool isBound(const std::string & name, EIsBoundMode mode) const;
VOption & retrieveValue(const std::string & name);
enum EUnbindMode{LOCAL, RECURSIVE_UNTIL_HIT, FULLY_RECURSIVE};
///returns true if symbol was really unbound
bool unbind(const std::string & name, EUnbindMode mode);
void localBind(std::string name, const VOption & sym);
void bindAtFirstHit(std::string name, const VOption & sym); //if symbol is locally defines, it gets overwritten; otherwise it is bind globally
};
//this class just introduces a new dynamic range when instantiated, nothing more
class IntroduceDynamicEnv
{
public:
IntroduceDynamicEnv();
~IntroduceDynamicEnv();
};
struct VermTreeIterator
{
private:
@@ -509,9 +252,7 @@ namespace VERMInterpreter
VermTreeIterator & operator=(const std::vector<VOption> & opt);
VermTreeIterator & operator=(const VOptionList & opt);
VOption & getAsItem();
VermTreeIterator getAsCDR();
VOptionList getAsList();
VOption & getIth(int i);
size_t size() const;
VermTreeIterator& operator=(const VermTreeIterator & rhs)
@@ -535,32 +276,6 @@ namespace VERMInterpreter
public:
VermTreeIterator car();
VermTreeIterator cdr();
bool isNil() const;
};
struct VFunc
{
enum Eopt {DEFAULT, LT, GT, LE, GE, EQ, ADD, SUB, MULT, DIV, MOD} option;
std::vector<VSymbol> args;
VOptionList body;
bool macro; //true - act as macro, false - act as function
VFunc(const VOptionList & _body, bool asMacro = false) : option(DEFAULT), body(_body), macro(asMacro)
{}
VFunc(Eopt func) : option(func), macro(false)
{}
VFunc& operator=(const VFunc & rhs)
{
if(this == &rhs)
{
return *this;
}
args = rhs.args;
body = rhs.body;
return *this;
}
VOption operator()(VermTreeIterator params);
};
struct OptionConverterVisitor : boost::static_visitor<VOption>
@@ -586,275 +301,29 @@ namespace VERMInterpreter
VNode( const VOption & first, const VOptionList & rest); //merges given arguments into [a, rest];
void setVnode( const VOption & first, const VOptionList & rest);
};
//v printer
void printVOption(const VOption & opt);
}
class ERMInterpreter;
struct TriggerIdentifierMatch
{
bool allowNoIdetifier;
std::map< int, std::vector<int> > matchToIt; //match subidentifiers to these numbers
static const int MAX_SUBIDENTIFIERS = 16;
ERMInterpreter * ermEnv;
bool tryMatch(VERMInterpreter::Trigger * interptrig) const;
};
struct IexpValStr
{
private:
union
{
int val;
int * integervar;
double * flvar;
std::string * stringvar;
} val;
public:
std::string name;
std::string getName() const;
enum {WRONGVAL, INT, INTVAR, FLOATVAR, STRINGVAR} type;
void setTo(const IexpValStr & second);
void setTo(int val);
void setTo(double val);
void setTo(const std::string & val);
int getInt() const;
double getFloat() const;
std::string getString() const;
IexpValStr() : type(WRONGVAL)
{}
IexpValStr(int _val) : type(INT)
{
val.val = _val;
}
IexpValStr(int* _val) : type(INTVAR)
{
val.integervar = _val;
}
IexpValStr(double * _val) : type(FLOATVAR)
{
val.flvar = _val;
}
IexpValStr(std::string * _val) : type(STRINGVAR)
{
val.stringvar = _val;
}
#define OPERATOR_DEFINITION_FULL(OPSIGN) \
template<typename T> \
IexpValStr operator OPSIGN(const T & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec); \
break; \
case STRINGVAR: \
ret.setTo(ret.getString() OPSIGN sec); \
break; \
} \
return ret; \
} \
IexpValStr operator OPSIGN(const IexpValStr & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
break; \
case STRINGVAR: \
ret.setTo(ret.getString() OPSIGN sec.getString()); \
break; \
} \
return ret; \
} \
template<typename T> \
IexpValStr & operator OPSIGN ## = (const T & sec) \
{ \
*this = *this OPSIGN sec; \
return *this; \
}
#define OPERATOR_DEFINITION(OPSIGN) \
template<typename T> \
IexpValStr operator OPSIGN(const T & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec); \
break; \
} \
return ret; \
} \
IexpValStr operator OPSIGN(const IexpValStr & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
break; \
} \
return ret; \
} \
template<typename T> \
IexpValStr & operator OPSIGN ## = (const T & sec) \
{ \
*this = *this OPSIGN sec; \
return *this; \
}
#define OPERATOR_DEFINITION_INTEGER(OPSIGN) \
template<typename T> \
IexpValStr operator OPSIGN(const T & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec); \
break; \
} \
return ret; \
} \
IexpValStr operator OPSIGN(const IexpValStr & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
break; \
} \
return ret; \
} \
template<typename T> \
IexpValStr & operator OPSIGN ## = (const T & sec) \
{ \
*this = *this OPSIGN sec; \
return *this; \
}
OPERATOR_DEFINITION_FULL(+)
OPERATOR_DEFINITION(-)
OPERATOR_DEFINITION(*)
OPERATOR_DEFINITION(/)
OPERATOR_DEFINITION_INTEGER(%)
};
class ERMInterpreter : public CScriptingModule
class ERMInterpreter
{
/*not so*/ public:
// friend class ScriptScanner;
// friend class TriggerIdMatchHelper;
// friend class TriggerIdentifierMatch;
// friend class ConditionDisemboweler;
// friend struct LVL2IexpDisemboweler;
// friend struct VR_SPerformer;
// friend struct ERMExpDispatch;
// friend struct VRPerformer;
std::vector<VERMInterpreter::FileInfo*> files;
std::vector< VERMInterpreter::FileInfo* > fileInfos;
std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
ERM::TLine &retrieveLine(VERMInterpreter::LinePointer linePtr);
static ERM::TTriggerBase & retrieveTrigger(ERM::TLine &line);
VERMInterpreter::Environment * globalEnv;
VERMInterpreter::ERMEnvironment * ermGlobalEnv;
typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
TtriggerListType triggers, postTriggers;
VERMInterpreter::Trigger * curTrigger;
VERMInterpreter::FunctionLocalVars * curFunc;
static const int TRIG_FUNC_NUM = 30000;
VERMInterpreter::FunctionLocalVars funcVars[TRIG_FUNC_NUM + 1]; //+1 because we use [0] as a global set of y-vars
VERMInterpreter::FunctionLocalVars * getFuncVars(int funNum); //0 is a global func-like set
TtriggerListType triggers;
TtriggerListType postTriggers;
std::vector<VERMInterpreter::LinePointer> instructions;
IexpValStr getIexp(const ERM::TIexp & iexp) const;
IexpValStr getIexp(const ERM::TMacroUsage & macro) const;
IexpValStr getIexp(const ERM::TIdentifierInternal & tid) const;
IexpValStr getIexp(const ERM::TVarpExp & tid) const;
IexpValStr getIexp(const ERM::TBodyOptionItem & opit) const;
static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
void executeLine(const VERMInterpreter::LinePointer & lp);
void executeLine(const ERM::TLine &line);
void executeTrigger(VERMInterpreter::Trigger & trig, int funNum = -1, std::vector<int> funParams=std::vector<int>());
static bool isCMDATrigger(const ERM::Tcommand & cmd);
static bool isATrigger(const ERM::TLine & line);
static ERM::EVOtions getExpType(const ERM::TVOption & opt);
IexpValStr getVar(std::string toFollow, boost::optional<int> initVal) const;
std::string processERMString(std::string ermstring);
VERMInterpreter::VOption eval( VERMInterpreter::VOption line, VERMInterpreter::Environment * env = nullptr );
VERMInterpreter::VOptionList evalEach( VERMInterpreter::VermTreeIterator list, VERMInterpreter::Environment * env = nullptr );
ERM::TLine & retrieveLine(const VERMInterpreter::LinePointer & linePtr);
static ERM::TTriggerBase & retrieveTrigger(ERM::TLine & line);
public:
typedef std::map< int, std::vector<int> > TIDPattern;
void executeInstructions(); //called when starting a new game, before most of the map settings are done
void executeTriggerType(VERMInterpreter::TriggerType tt, bool pre, const TIDPattern & identifier, const std::vector<int> &funParams=std::vector<int>()); //use this to run triggers
void executeTriggerType(const char *trigger, int id); //convenience version of above, for pre-trigger when there is only one argument
void executeTriggerType(const char *trigger); //convenience version of above, for pre-trigger when there are no args
void setCurrentlyVisitedObj(int3 pos); //sets v998 - v1000 to given value
void scanForScripts();
vstd::CLoggerBase * logger;
enum EPrintMode{ALL, ERM_ONLY, VERM_ONLY};
void printScripts(EPrintMode mode = ALL);
void scanScripts(); //scans for functions, triggers etc.
ERMInterpreter();
bool checkCondition( ERM::Tcondition cond );
int getRealLine(const VERMInterpreter::LinePointer &lp);
//overload CScriptingModule
virtual void heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start) override;
virtual void init() override;//sets up environment etc.
virtual void executeUserCommand(const std::string &cmd) override;
virtual void giveInfoCB(CPrivilegedInfoCallback *cb) override;
virtual void giveActionCB(IGameEventRealizer *cb) override;
virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) override;
const CGObjectInstance *getObjFrom(int3 pos);
template <typename T>
const T *getObjFromAs(int3 pos)
{
const T* obj = dynamic_cast<const T*>(getObjFrom(pos));
if(obj)
return obj;
else
throw VERMInterpreter::EScriptExecError("Wrong cast attempted, object is not of a desired type!");
}
ERMInterpreter(vstd::CLoggerBase * logger_);
virtual ~ERMInterpreter();
std::string loadScript(const std::string & name, const std::string & source);
};