OpenClonk
C4MapScriptAlgoTurbulence Class Reference

#include <C4MapScript.h>

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

Public Member Functions

 C4MapScriptAlgoTurbulence (const C4PropList *props)
 
bool operator() (int32_t x, int32_t y, uint8_t &fg, uint8_t &bg) const override
 
void Clear ()
 

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 241 of file C4MapScript.h.

Constructor & Destructor Documentation

◆ C4MapScriptAlgoTurbulence()

C4MapScriptAlgoTurbulence::C4MapScriptAlgoTurbulence ( const C4PropList props)

Definition at line 369 of file C4MapScriptAlgo.cpp.

369  : C4MapScriptAlgoModifier(props,1,1)
370 {
371  // Get MAPALGO_Turbulence properties
372  seed = props->GetPropertyInt(P_Seed);
373  if (!seed) seed = Random(65536);
374  GetXYProps(props, P_Amplitude, amp, true);
375  GetXYProps(props, P_Scale, scale, true);
376  if (!scale[0]) scale[0] = 10;
377  if (!scale[1]) scale[1] = 10;
378  if (!amp[0] && !amp[1]) { amp[0] = amp[1] = 10; }
379  iterations = props->GetPropertyInt(P_Iterations);
380  if (!iterations) iterations = 2;
381 }
uint32_t Random()
Definition: C4Random.cpp:43
@ P_Scale
@ P_Amplitude
@ P_Iterations
@ P_Seed
bool GetXYProps(const C4PropList *props, C4PropertyName k, int32_t *out_xy, bool zero_defaults)
C4MapScriptAlgoModifier(const C4PropList *props, int32_t min_ops=0, int32_t max_ops=0)
int32_t GetPropertyInt(C4PropertyName k, int32_t default_val=0) const
Definition: C4PropList.cpp:855

References C4PropList::GetPropertyInt(), C4MapScriptAlgo::GetXYProps(), P_Amplitude, P_Iterations, P_Scale, P_Seed, and Random().

Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void C4MapScriptAlgoModifier::Clear ( )
inherited

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 }
std::vector< C4MapScriptAlgo * > operands
Definition: C4MapScript.h:166

References C4MapScriptAlgoModifier::operands.

Referenced by C4MapScriptAlgoModifier::C4MapScriptAlgoModifier(), and C4MapScriptAlgoModifier::~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
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
bool HasProperty(C4String *k) const
Definition: C4PropList.h:122
bool GetProperty(C4PropertyName k, C4Value *pResult) const
Definition: C4PropList.h:105
const char * GetCStr() const
Definition: C4StringTable.h:49
C4String P[P_LAST]
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t GetSize() const
Definition: C4ValueArray.h:36
C4ValueArray * getArray() const
Definition: C4Value.h:118
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().

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

◆ operator()()

bool C4MapScriptAlgoTurbulence::operator() ( int32_t  x,
int32_t  y,
uint8_t &  fg,
uint8_t &  bg 
) const
overridevirtual

Implements C4MapScriptAlgo.

Definition at line 383 of file C4MapScriptAlgo.cpp.

384 {
385  // Evaluate MAPALGO_Turbulence at x,y:
386  // move by a random offset iterations times
387  assert(operands.size()==1);
388  int32_t xy[] = {x, y};
389  for (int32_t iter=0; iter<iterations; ++iter)
390  {
391  int32_t s[2], p[2];
392  for (int dim=0; dim<2; ++dim)
393  {
394  s[dim] = divD(xy[dim], scale[dim]);
395  p[dim] = modD(xy[dim], scale[dim]);
396  }
397  int32_t a[2][2];
398  for (int dim=0; dim<2; ++dim)
399  {
400  int32_t aamp = amp[dim] / (iter+1);
401  if (!aamp) continue;
402  for (int dx=0; dx<2; ++dx) for (int dy=0; dy<2; ++dy) a[dx][dy] = QuerySeededRandomField(seed+dim, s[0]+dx, s[1]+dy, aamp) - aamp/2;
403  int32_t a_interp = a[0][0]*(scale[0]-p[0])*(scale[1]-p[1])
404  + a[1][0]*( p[0])*(scale[1]-p[1])
405  + a[0][1]*(scale[0]-p[0])*( p[1])
406  + a[1][1]*( p[0])*( p[1]);
407  xy[dim] += a_interp / (scale[0]*scale[1]);
408  }
409  }
410  return (*operands[0])(xy[0],xy[1], fg, bg);
411 }
#define s
#define a
int32_t QuerySeededRandomField(int32_t seed, int32_t x, int32_t y, int32_t scale)

References C4MapScriptAlgoModifier::operands, and s.

Member Data Documentation

◆ operands


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