OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Script.cpp File Reference
#include "C4Include.h"
#include "C4Version.h"
#include "lib/C4Random.h"
#include "script/C4AulExec.h"
#include "script/C4AulDefFunc.h"
Include dependency graph for C4Script.cpp:

Go to the source code of this file.

Macros

#define MAKE_AND_RETURN_ARRAY(values)
 
#define F(f)   ::AddFunc(p, #f, Fn##f)
 

Functions

StdStrBuf FnStringFormat (C4PropList *_this, C4String *szFormatPar, C4Value *Pars, int ParCount)
 
void InitCoreFunctionMap (C4AulScriptEngine *pEngine)
 

Variables

C4ScriptConstDef C4ScriptConstMap []
 
C4ScriptFnDef C4ScriptFnMap []
 

Macro Definition Documentation

#define F (   f)    ::AddFunc(p, #f, Fn##f)

Referenced by InitCoreFunctionMap().

#define MAKE_AND_RETURN_ARRAY (   values)
Value:
do { \
C4ValueArray *matrix = new C4ValueArray(sizeof(values) / sizeof(*values)); \
for (size_t i = 0; i < sizeof(values) / sizeof(*values); ++i) \
(*matrix)[i] = C4VInt(values[i]); \
return matrix; \
} while (0)
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242

Definition at line 126 of file C4Script.cpp.

Function Documentation

StdStrBuf FnStringFormat ( C4PropList _this,
C4String szFormatPar,
C4Value Pars,
int  ParCount 
)

Definition at line 29 of file C4Script.cpp.

References StdStrBuf::Append(), StdStrBuf::AppendChar(), StdStrBuf::AppendCharacter(), StdStrBuf::AppendFormat(), C4String::GetCStr(), C4Value::getStr(), Inside(), SCopy(), and SLen().

30 {
31  int cPar=0;
32 
33  StdStrBuf StringBuf("", false);
34  const char * cpFormat = FnStringPar(szFormatPar);
35  const char * cpType;
36  char szField[20];
37  while (*cpFormat)
38  {
39  // Copy normal stuff
40  while (*cpFormat && (*cpFormat!='%'))
41  StringBuf.AppendChar(*cpFormat++);
42  // Field
43  if (*cpFormat=='%')
44  {
45  // Scan field type
46  for (cpType=cpFormat+1; *cpType && (*cpType == '+' || *cpType == '-' || *cpType == '.' || *cpType == '#' || *cpType == ' ' || Inside(*cpType,'0','9')); cpType++) {}
47  // Copy field
48  SCopy(cpFormat,szField,std::min<unsigned int>(sizeof szField - 1, cpType - cpFormat + 1));
49  // Insert field by type
50  switch (*cpType)
51  {
52  // number
53  case 'd': case 'x': case 'X':
54  {
55  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
56  StringBuf.AppendFormat(szField, Pars[cPar++].getInt());
57  cpFormat+=SLen(szField);
58  break;
59  }
60  // character
61  case 'c':
62  {
63  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
64  StringBuf.AppendCharacter(Pars[cPar++].getInt());
65  cpFormat+=SLen(szField);
66  break;
67  }
68  // C4ID
69  case 'i':
70  // C4Value
71  case 'v':
72  {
73  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
74  StringBuf.Append(static_cast<const StdStrBuf&>(Pars[cPar++].GetDataString(10)));
75  cpFormat+=SLen(szField);
76  break;
77  }
78  // String
79  case 's':
80  {
81  // get string
82  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
83  const char *szStr = "(null)";
84  if (Pars[cPar].GetData())
85  {
86  C4String * pStr = Pars[cPar].getStr();
87  if (!pStr) throw C4AulExecError("string format placeholder without string");
88  szStr = pStr->GetCStr();
89  }
90  ++cPar;
91  StringBuf.AppendFormat(szField, szStr);
92  cpFormat+=SLen(szField);
93  break;
94  }
95  case '%':
96  StringBuf.AppendChar('%');
97  cpFormat+=SLen(szField);
98  break;
99  // Undefined / Empty
100  default:
101  StringBuf.AppendChar('%');
102  cpFormat++;
103  break;
104  }
105  }
106  }
107  return StringBuf;
108 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
C4String * getStr() const
Definition: C4Value.h:117
const char * GetCStr() const
Definition: C4StringTable.h:49
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43

Here is the call graph for this function:

void InitCoreFunctionMap ( C4AulScriptEngine pEngine)

Definition at line 1114 of file C4Script.cpp.

References Abs(), AddFunc(), Angle(), C4V_Int, C4VInt(), Cos(), Distance(), F, C4AulExec::FnLogCallStack(), C4AulExec::FnTranslate(), C4AulScriptEngine::GetPropList(), C4ScriptConstDef::Identifier, Inside(), Random(), C4AulScriptEngine::RegisterGlobalConstant(), Sin(), and WildcardMatch().

Referenced by c4_mapgen_handle_init_script_engine(), and C4Game::InitScriptEngine().

1115 {
1116  // add all def constants (all Int)
1117  for (C4ScriptConstDef *pCDef = &C4ScriptConstMap[0]; pCDef->Identifier; pCDef++)
1118  {
1119  assert(pCDef->ValType == C4V_Int); // only int supported currently
1120  pEngine->RegisterGlobalConstant(pCDef->Identifier, C4VInt(pCDef->Data));
1121  }
1122 
1123  C4PropListStatic * p = pEngine->GetPropList();
1124  // add all def script funcs
1125  for (C4ScriptFnDef *pDef = &C4ScriptFnMap[0]; pDef->Identifier; pDef++)
1126  new C4AulDefFunc(p, pDef);
1127 #define F(f) ::AddFunc(p, #f, Fn##f)
1128  F(ParseInt);
1129  F(Abs);
1130  F(Min);
1131  F(Max);
1132  F(Sin);
1133  F(Cos);
1134  F(Sqrt);
1135  F(ArcSin);
1136  F(ArcCos);
1137  F(BoundBy);
1138  F(Inside);
1139  F(Random);
1140 
1141  F(CreateArray);
1142  F(CreatePropList);
1143  F(GetProperties);
1144  F(GetProperty);
1145  F(SetProperty);
1146  F(GetPrototype);
1147  F(SetPrototype);
1148  F(ResetProperty);
1149  F(GetName);
1150  F(AddEffect);
1151  F(CreateEffect);
1152  F(CheckEffect);
1153  F(RemoveEffect);
1154  F(GetEffect);
1155  F(GetEffectCount);
1156  F(RegexReplace);
1157  F(RegexSearch);
1158  F(RegexMatch);
1159  F(RegexSplit);
1160  F(Distance);
1161  F(Angle);
1162  F(GetChar);
1163  F(GetType);
1164  F(ModulateColor);
1165  F(WildcardMatch);
1166  F(GetLength);
1167  F(SetLength);
1168  F(GetIndexOf);
1169  F(DeepEqual);
1170  F(FatalError);
1171  F(StartCallTrace);
1172  F(StartScriptProfiler);
1173  F(StopScriptProfiler);
1174  F(SortArray);
1175  F(SortArrayByProperty);
1176  F(SortArrayByArrayElement);
1177  F(Trans_Identity);
1178  F(Trans_Translate);
1179  F(Trans_Scale);
1180  F(Trans_Rotate);
1181  F(LocateFunc);
1182  F(FileWrite);
1183  F(eval);
1184  F(StringToIdentifier);
1185  F(GetConstantNameByValue);
1186  F(ReplaceString);
1187 
1188  ::AddFunc(p, "Translate", C4AulExec::FnTranslate);
1189  ::AddFunc(p, "LogCallStack", C4AulExec::FnLogCallStack);
1190 #undef F
1191 }
void RegisterGlobalConstant(const char *szName, const C4Value &rValue)
static bool FnLogCallStack(C4PropList *_this)
Definition: C4AulExec.cpp:130
C4ScriptFnDef C4ScriptFnMap[]
Definition: C4Script.cpp:1102
uint32_t Random()
Definition: C4Random.cpp:43
C4PropListStatic * GetPropList()
Definition: C4Aul.h:151
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242
int32_t Angle(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iPrec)
Definition: Standard.cpp:37
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:25
C4ScriptConstDef C4ScriptConstMap[]
Definition: C4Script.cpp:1053
const char * Identifier
Definition: C4AulDefFunc.h:276
#define F(f)
static C4String * FnTranslate(C4PropList *_this, C4String *text)
Definition: C4AulExec.cpp:100
const char * Identifier
Definition: C4AulDefFunc.h:268
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:374
T Abs(T val)
Definition: Standard.h:42
C4Real Sin(const C4Real &fAngle)
Definition: C4Real.h:265
void AddFunc(C4PropListStatic *Parent, const char *Name, RType(*pFunc)(ThisType *, ParTypes...), bool Public=true)
Definition: C4AulDefFunc.h:260
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
C4Real Cos(const C4Real &fAngle)
Definition: C4Real.h:266

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

C4ScriptConstDef C4ScriptConstMap[]

Definition at line 1053 of file C4Script.cpp.

C4ScriptFnDef C4ScriptFnMap[]
Initial value:
=
{
{ "Call", true, C4V_Any, { C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnCall },
{ "EffectCall", true, C4V_Any, { C4V_Object ,C4V_PropList,C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnEffectCall },
{ "Log", true, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnLog },
{ "DebugLog", true, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnDebugLog },
{ "Format", true, C4V_String, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnFormat },
{ "Trans_Mul", true, C4V_Array, { C4V_Array ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnTrans_Mul},
{ nullptr, false, C4V_Nil, { C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil}, nullptr }
}

Definition at line 1102 of file C4Script.cpp.