OpenClonk
C4MapScriptAlgoModifier Class Referenceabstract

#include <C4MapScript.h>

Inheritance diagram for C4MapScriptAlgoModifier:
[legend]
Collaboration diagram for C4MapScriptAlgoModifier:
[legend]

Public Member Functions

 C4MapScriptAlgoModifier (const C4PropList *props, int32_t min_ops=0, int32_t max_ops=0)
 
 ~C4MapScriptAlgoModifier () override
 
void Clear ()
 
virtual bool operator() (int32_t x, int32_t y, uint8_t &fg, uint8_t &bg) const =0
 

Protected Member Functions

bool GetXYProps (const C4PropList *props, C4PropertyName k, int32_t *out_xy, bool zero_defaults)
 

Protected Attributes

std::vector< C4MapScriptAlgo * > operands
 

Detailed Description

Definition at line 163 of file C4MapScript.h.

Constructor & Destructor Documentation

◆ C4MapScriptAlgoModifier()

C4MapScriptAlgoModifier::C4MapScriptAlgoModifier ( const C4PropList props,
int32_t  min_ops = 0,
int32_t  max_ops = 0 
)

Definition at line 221 of file C4MapScriptAlgo.cpp.

222 {
223  // Evaluate "Op" property of all algos that take another algo or layer as an operand
224  // Op may be a proplist or an array of proplists
225  C4Value vops; int32_t n; C4ValueArray temp_ops;
226  props->GetProperty(P_Op, &vops);
227  C4ValueArray *ops = vops.getArray();
228  if (!ops)
229  {
230  C4PropList *op = vops.getPropList();
231  if (op)
232  {
233  temp_ops.SetItem(0, vops);
234  ops = &temp_ops;
235  n = 1;
236  }
237  }
238  else
239  {
240  n = ops->GetSize();
241  }
242  if (!ops || n<min_ops || (max_ops && n>max_ops))
243  throw C4AulExecError(FormatString(R"(C4MapScriptAlgo: Expected between %d and %d operands in property "Op".)", (int)min_ops, (int)max_ops).getData());
244  operands.resize(n);
245  try
246  {
247  // can easily crash this by building a recursive prop list
248  // unfortunately, protecting against that is not trivial
249  for (int32_t i=0; i<n; ++i)
250  {
251  C4MapScriptAlgo *new_algo = FnParAlgo(ops->GetItem(i).getPropList());
252  if (!new_algo) throw C4AulExecError(FormatString(R"(C4MapScriptAlgo: Operand %d in property "Op" not valid.)", (int)i).getData());
253  operands[i] = new_algo;
254  }
255  }
256  catch (...)
257  {
258  Clear();
259  throw;
260  }
261 }
C4MapScriptAlgo * FnParAlgo(C4PropList *algo_par)
@ P_Op
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
std::vector< C4MapScriptAlgo * > operands
Definition: C4MapScript.h:166
bool GetProperty(C4PropertyName k, C4Value *pResult) const
Definition: C4PropList.h:105
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
void SetItem(int32_t iElemNr, const C4Value &Value)
int32_t GetSize() const
Definition: C4ValueArray.h:36
C4ValueArray * getArray() const
Definition: C4Value.h:118
C4PropList * getPropList() const
Definition: C4Value.h:116

References Clear(), FnParAlgo(), FormatString(), C4Value::getArray(), C4ValueArray::GetItem(), C4PropList::GetProperty(), C4Value::getPropList(), C4ValueArray::GetSize(), operands, P_Op, and C4ValueArray::SetItem().

Here is the call graph for this function:

◆ ~C4MapScriptAlgoModifier()

C4MapScriptAlgoModifier::~C4MapScriptAlgoModifier ( )
inlineoverride

Definition at line 169 of file C4MapScript.h.

169 { Clear(); }

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void C4MapScriptAlgoModifier::Clear ( )

Definition at line 263 of file C4MapScriptAlgo.cpp.

264 {
265  // Child algos are owned by this algo, so delete them
266  for (auto & operand : operands) delete operand;
267  operands.clear();
268 }

References operands.

Referenced by C4MapScriptAlgoModifier(), and ~C4MapScriptAlgoModifier().

Here is the caller graph for this function:

◆ GetXYProps()

bool C4MapScriptAlgo::GetXYProps ( const C4PropList props,
C4PropertyName  k,
int32_t *  out_xy,
bool  zero_defaults 
)
protectedinherited

Definition at line 26 of file C4MapScriptAlgo.cpp.

27 {
28  // Evaluate property named "k" in proplist props to store two numbers in out_xy:
29  // If props->k is a single integer, fill both numbers in out_xy with it
30  // If props->k is an array, check that it contains two numbers and store them in out_xy
31  if (!props->HasProperty(&Strings.P[k]))
32  {
33  if (zero_defaults) out_xy[0] = out_xy[1] = 0;
34  return false;
35  }
36  C4Value val; C4ValueArray *arr;
37  props->GetProperty(k, &val);
38  if ((arr = val.getArray()))
39  {
40  if (arr->GetSize() != 2)
41  throw C4AulExecError(FormatString(R"(C4MapScriptAlgo: Expected either integer or array with two integer elements in property "%s".)", Strings.P[k].GetCStr()).getData());
42  out_xy[0] = arr->GetItem(0).getInt();
43  out_xy[1] = arr->GetItem(1).getInt();
44  }
45  else
46  {
47  out_xy[0] = out_xy[1] = val.getInt();
48  }
49  return true;
50 }
C4StringTable Strings
Definition: C4Globals.cpp:42
bool HasProperty(C4String *k) const
Definition: C4PropList.h:122
const char * GetCStr() const
Definition: C4StringTable.h:49
C4String P[P_LAST]
int32_t getInt() const
Definition: C4Value.h:112
const char * getData() const
Definition: StdBuf.h:442

References FormatString(), C4Value::getArray(), C4String::GetCStr(), StdStrBuf::getData(), C4Value::getInt(), C4ValueArray::GetItem(), C4PropList::GetProperty(), C4ValueArray::GetSize(), C4PropList::HasProperty(), C4StringTable::P, and Strings.

Referenced by C4MapScriptAlgoBorder::C4MapScriptAlgoBorder(), and C4MapScriptAlgoTurbulence::C4MapScriptAlgoTurbulence().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator()()

Member Data Documentation

◆ operands


The documentation for this class was generated from the following files: