OpenClonk
C4PXS Class Reference

#include <C4PXS.h>

Collaboration diagram for C4PXS:
[legend]

Protected Member Functions

bool Execute ()
 
void Deactivate ()
 

Protected Attributes

int32_t Mat {MNone}
 
C4Real x {Fix0}
 
C4Real y {Fix0}
 
C4Real xdir {Fix0}
 
C4Real ydir {Fix0}
 

Friends

class C4PXSSystem
 

Detailed Description

Definition at line 25 of file C4PXS.h.

Member Function Documentation

◆ Deactivate()

void C4PXS::Deactivate ( )
protected

Definition at line 136 of file C4PXS.cpp.

137 {
139  {
140  C4RCExecPXS rc;
141  rc.x=x; rc.y=y; rc.iMat=Mat;
142  rc.pos = 2;
143  AddDbgRec(RCT_ExecPXS, &rc, sizeof(rc));
144  }
145  Mat=MNone;
146 }
C4Config Config
Definition: C4Config.cpp:930
const int32_t MNone
Definition: C4Constants.h:177
constexpr bool DEBUGREC_PXS
Definition: C4Include.h:32
void AddDbgRec(C4RecordChunkType eType, const void *pData, int iSize)
Definition: C4Record.cpp:32
int32_t pos
Definition: C4Record.h:175
@ RCT_ExecPXS
Definition: C4Record.h:67
int32_t iMat
Definition: C4Record.h:173
C4Real y
Definition: C4Record.h:171
C4Real x
Definition: C4Record.h:171
int32_t DebugRec
Definition: C4Config.h:63
C4ConfigGeneral General
Definition: C4Config.h:255
int32_t Mat
Definition: C4PXS.h:29
C4Real y
Definition: C4PXS.h:30
C4Real x
Definition: C4PXS.h:30

References AddDbgRec(), Config, C4ConfigGeneral::DebugRec, DEBUGREC_PXS, C4Config::General, C4RCExecPXS::iMat, Mat, MNone, C4RCExecPXS::pos, RCT_ExecPXS, C4RCExecPXS::x, x, C4RCExecPXS::y, and y.

Referenced by Execute().

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

◆ Execute()

bool C4PXS::Execute ( )
protected

Definition at line 33 of file C4PXS.cpp.

34 {
36  {
37  C4RCExecPXS rc;
38  rc.x=x; rc.y=y; rc.iMat=Mat;
39  rc.pos = 0;
40  AddDbgRec(RCT_ExecPXS, &rc, sizeof(rc));
41  }
42  int32_t inmat;
43 
44  // Safety
45  if (!MatValid(Mat))
46  { Deactivate(); return false; }
47 
48  // Out of bounds
49  if ((x<0) || (x>=::Landscape.GetWidth()) || (y<-10) || (y>=::Landscape.GetHeight()))
50  { Deactivate(); return false; }
51 
52  // Material conversion
53  int32_t iX = fixtoi(x), iY = fixtoi(y);
54  inmat=GBackMat(iX,iY);
56  if (pReact && (*pReact->pFunc)(pReact, iX,iY, iX,iY, xdir,ydir, Mat,inmat, meePXSPos, nullptr))
57  { Deactivate(); return false; }
58 
59  // Gravity
60  ydir+=GravAccel;
61 
62  if (GBackDensity(iX, iY + 1) < ::MaterialMap.Map[Mat].Density)
63  {
64  // Air speed: Wind plus some random
65  int32_t iWind = Weather.GetWind(iX, iY);
66  C4Real txdir = itofix(iWind, 15) + C4REAL256(Random(1200) - 600);
67  C4Real tydir = C4REAL256(Random(1200) - 600);
68 
69  // Air friction, based on WindDrift. MaxSpeed is ignored.
70  int32_t iWindDrift = std::max(::MaterialMap.Map[Mat].WindDrift - 20, 0);
71  xdir += ((txdir - xdir) * iWindDrift) * WindDrift_Factor;
72  ydir += ((tydir - ydir) * iWindDrift) * WindDrift_Factor;
73  }
74 
75  C4Real ctcox = x + xdir;
76  C4Real ctcoy = y + ydir;
77 
78  int32_t iToX = fixtoi(ctcox), iToY = fixtoi(ctcoy);
79 
80  // In bounds?
81  if (Inside<int32_t>(iToX, 0, ::Landscape.GetWidth()-1) && Inside<int32_t>(iToY, 0, ::Landscape.GetHeight()-1))
82  // Check path
83  if (::Landscape._PathFree(iX, iY, iToX, iToY))
84  {
85  x=ctcox; y=ctcoy;
86  return true;
87  }
88 
89  // Test path to target position
90  int32_t iX0 = iX, iY0 = iY;
91  bool fStopMovement = false;
92  do
93  {
94  // Step
95  int32_t inX = iX + Sign(iToX - iX), inY = iY + Sign(iToY - iY);
96  // Contact?
97  inmat = GBackMat(inX, inY);
99  if (pReact)
100  {
101  if ((*pReact->pFunc)(pReact, iX,iY, inX,inY, xdir,ydir, Mat,inmat, meePXSMove, &fStopMovement))
102  {
103  // destructive contact
104  Deactivate();
105  return false;
106  }
107  else
108  {
109  // no destructive contact, but speed or position changed: Stop moving for now
110  if (fStopMovement)
111  {
112  // But keep fractional positions to allow proper movement on moving ground
113  if (iX != iX0) x = itofix(iX);
114  if (iY != iY0) y = itofix(iY);
115  return true;
116  }
117  // there was a reaction func, but it didn't do anything - continue movement
118  }
119  }
120  iX = inX; iY = inY;
121  }
122  while (iX != iToX || iY != iToY);
123 
124  // No contact? Free movement
125  x=ctcox; y=ctcoy;
127  {
128  C4RCExecPXS rc;
129  rc.x=x; rc.y=y; rc.iMat=Mat;
130  rc.pos = 1;
131  AddDbgRec(RCT_ExecPXS, &rc, sizeof(rc));
132  }
133  return true;
134 }
C4Landscape Landscape
int32_t GBackMat(int32_t x, int32_t y)
Definition: C4Landscape.h:219
int32_t GBackDensity(int32_t x, int32_t y)
Definition: C4Landscape.h:224
C4MaterialMap MaterialMap
Definition: C4Material.cpp:974
bool MatValid(int32_t mat)
Definition: C4Material.h:210
@ meePXSPos
Definition: C4Material.h:36
@ meePXSMove
Definition: C4Material.h:37
#define GravAccel
Definition: C4Physics.h:27
uint32_t Random()
Definition: C4Random.cpp:43
C4Fixed itofix(int32_t x)
Definition: C4Real.h:261
int fixtoi(const C4Fixed &x)
Definition: C4Real.h:259
C4Real C4REAL256(int x)
Definition: C4Real.h:268
C4Weather Weather
Definition: C4Weather.cpp:206
int Sign(T val)
Definition: Standard.h:45
Definition: C4Real.h:59
int32_t GetWidth() const
int32_t GetHeight() const
bool _PathFree(int32_t x, int32_t y, int32_t x2, int32_t y2) const
int32_t WindDrift
Definition: C4Material.h:105
int32_t Density
Definition: C4Material.h:92
C4MaterialReaction * GetReactionUnsafe(int32_t iPXSMat, int32_t iLandscapeMat)
Definition: C4Material.h:191
C4Material * Map
Definition: C4Material.h:169
C4Real ydir
Definition: C4PXS.h:30
C4Real xdir
Definition: C4PXS.h:30
void Deactivate()
Definition: C4PXS.cpp:136
int32_t GetWind(int32_t x, int32_t y)
Definition: C4Weather.cpp:94
C4MaterialReactionFunc pFunc
Definition: C4Material.h:47

References AddDbgRec(), C4REAL256(), Config, Deactivate(), C4ConfigGeneral::DebugRec, DEBUGREC_PXS, C4MaterialCore::Density, fixtoi(), GBackDensity(), GBackMat(), C4Config::General, C4Landscape::GetHeight(), C4MaterialMap::GetReactionUnsafe(), C4Landscape::GetWidth(), C4Weather::GetWind(), GravAccel, C4RCExecPXS::iMat, itofix(), Landscape, C4MaterialMap::Map, Mat, MaterialMap, MatValid(), meePXSPos, C4MaterialReaction::pFunc, C4RCExecPXS::pos, Random(), RCT_ExecPXS, Weather, C4MaterialCore::WindDrift, C4RCExecPXS::x, x, xdir, C4RCExecPXS::y, y, and ydir.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ C4PXSSystem

friend class C4PXSSystem
friend

Definition at line 27 of file C4PXS.h.

Member Data Documentation

◆ Mat

int32_t C4PXS::Mat {MNone}
protected

◆ x

C4Real C4PXS::x {Fix0}
protected

◆ xdir

C4Real C4PXS::xdir {Fix0}
protected

Definition at line 30 of file C4PXS.h.

Referenced by C4PXSSystem::Create(), C4PXSSystem::Draw(), Execute(), and C4PXSSystem::Load().

◆ y

C4Real C4PXS::y {Fix0}
protected

◆ ydir

C4Real C4PXS::ydir {Fix0}
protected

Definition at line 30 of file C4PXS.h.

Referenced by C4PXSSystem::Create(), C4PXSSystem::Draw(), Execute(), and C4PXSSystem::Load().


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