OpenClonk
C4MapScriptAlgoBorder Class Reference

#include <C4MapScript.h>

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

Public Member Functions

 C4MapScriptAlgoBorder (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 251 of file C4MapScript.h.

Constructor & Destructor Documentation

◆ C4MapScriptAlgoBorder()

C4MapScriptAlgoBorder::C4MapScriptAlgoBorder ( const C4PropList props)

Definition at line 422 of file C4MapScriptAlgo.cpp.

422  : C4MapScriptAlgoModifier(props,1,1)
423 {
424  // Get MAPALGO_Border properties
425  int32_t wdt[2] = {0,0};
426  // Parameter Wdt fills all directions
427  int32_t n_borders = 0;
428  n_borders += GetXYProps(props, P_Wdt, wdt, false);
429  for (int32_t i=0; i<2; ++i) left[i]=top[i]=right[i]=bottom[i]=wdt[i];
430  // Individual direction parameters
431  n_borders += GetXYProps(props, P_Left, left, false);
432  n_borders += GetXYProps(props, P_Top, top, false);
433  n_borders += GetXYProps(props, P_Right, right, false);
434  n_borders += GetXYProps(props, P_Bottom, bottom, false);
435  // Resolve negative/positive values to inner/outer borders
436  ResolveBorderProps(left);
437  ResolveBorderProps(top);
438  ResolveBorderProps(right);
439  ResolveBorderProps(bottom);
440  // If nothing was specified, fill all directions with a default: Draw 1px of outer border
441  if (!n_borders)
442  {
443  left[1] = top[1] = right[1] = bottom[1] = 1;
444  }
445 }
@ P_Wdt
@ P_Bottom
@ P_Top
@ P_Left
@ P_Right
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)

References C4MapScriptAlgo::GetXYProps(), P_Bottom, P_Left, P_Right, P_Top, and P_Wdt.

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(), and C4MapScriptAlgoTurbulence::C4MapScriptAlgoTurbulence().

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

◆ operator()()

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

Implements C4MapScriptAlgo.

Definition at line 447 of file C4MapScriptAlgo.cpp.

448 {
449  // Evaluate MAPALGO_Border at x,y: Check if position is at a border of operand layer
450  // For borders inside operand layer, return the operand material. For outside borders, just return 1. For non-borders, return 0.
451  // Are we inside or outside?
452  const C4MapScriptAlgo &l = *operands[0];
453  bool inside = l(x,y,fg,bg);
454  // Check four sideways directions
455  const int32_t *ymove[] = { top, bottom }, *xmove [] ={ left, right };
456  const int32_t d[] = { -1, +1 };
457  for (int32_t dir=0; dir<2; ++dir)
458  {
459  uint8_t fake_fg, fake_bg;
460  int32_t hgt = ymove[inside!=!dir][!inside];
461  for (int32_t dy=0; dy<hgt; ++dy)
462  if (inside==!l(x,y+d[dir]*(dy+1), fake_fg, fake_bg))
463  return true;
464  int32_t wdt = xmove[inside!=!dir][!inside];
465  for (int32_t dx=0; dx<wdt; ++dx)
466  if (inside==!l(x+d[dir]*(dx+1),y, fake_fg, fake_bg))
467  return true;
468  }
469  // Not on border
470  return false;
471 }

References C4MapScriptAlgoModifier::operands.

Member Data Documentation

◆ operands


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