Pol  Revision:cb584c9
uopathnode.h
Go to the documentation of this file.
1 
8 #ifndef __UOPATHNODE_H
9 #define __UOPATHNODE_H
10 // AStar search class
11 #include "../plib/stlastar.h"
12 #include "realms/realm.h"
13 
14 #include "realms.h"
15 
16 namespace Pol
17 {
18 namespace Core
19 {
20 #define BORDER_SKIRT 5
22 {
23 public:
24  int xLow, xHigh, yLow, yHigh;
25 
26  struct BlockNode
27  {
28  short x;
29  short y;
30  short z;
31  };
32 
33  typedef std::vector<BlockNode*> BlockNodeVector;
34 
35 public:
36  AStarBlockers( short xL, short xH, short yL, short yH )
37  {
38  xLow = xL;
39  xHigh = xH;
40  yLow = yL;
41  yHigh = yH;
42  }
43 
44  void AddBlocker( short x, short y, short z )
45  {
46  BlockNode* theNode = new BlockNode;
47 
48  theNode->x = x;
49  theNode->y = y;
50  theNode->z = z;
51 
52  m_List.push_back( theNode );
53  }
54 
56  {
57  for ( auto blockNode : m_List )
58  {
59  delete blockNode;
60  }
61  }
62 
63  bool IsBlocking( short x, short y, short z )
64  {
65  for ( const auto blockNode : m_List )
66  {
67  if ( ( blockNode->x == x ) && ( blockNode->y == y ) &&
68  ( abs( blockNode->z - z ) < settingsManager.ssopt.default_character_height ) )
69  return true;
70  }
71  return false;
72  }
73  BlockNodeVector m_List;
74 };
75 
77 {
78 public:
80  short x;
81  short y;
82  short z;
84 
86  : theBlockers( nullptr ),
87  x( 0 ),
88  y( 0 ),
89  z( 0 ),
90  realm( Core::find_realm( std::string( "britannia" ) ) ){};
91  UOPathState( short newx, short newy, short newz, Realms::Realm* newrealm,
92  AStarBlockers* blockers )
93  {
94  x = newx;
95  y = newy;
96  z = newz;
97  realm = newrealm;
98  theBlockers = blockers;
99  };
100  float GoalDistanceEstimate( UOPathState& nodeGoal );
101  bool IsGoal( UOPathState& nodeGoal );
102  bool GetSuccessors( Plib::AStarSearch<UOPathState>* astarsearch, UOPathState* parent_node,
103  bool doors_block );
104  float GetCost( UOPathState& successor );
105  bool IsSameState( UOPathState& rhs );
106  std::string Name();
107 };
109 {
110  return ( ( rhs.x == x ) && ( rhs.y == y ) && ( rhs.z == z ) && ( rhs.realm == realm ) );
111 }
113 {
114  return ( (float)( abs( x - nodeGoal.x ) + abs( y - nodeGoal.y ) + abs( z - nodeGoal.z ) ) );
115 }
117 {
118  return ( ( nodeGoal.x == x ) && ( nodeGoal.y == y ) &&
119  ( abs( nodeGoal.z - z ) <= settingsManager.ssopt.default_character_height ) );
120  // return (IsSameState(nodeGoal));
121 }
123 {
124  int xdiff = abs( x - successor.x );
125  int ydiff = abs( y - successor.y );
126  if ( xdiff && ydiff )
127  return 1.414f;
128  else
129  return 1.0f;
130 }
131 std::string UOPathState::Name()
132 {
133  fmt::Writer writer;
134  writer.Format( "({},{},{})" ) << x << y << z;
135  return writer.str();
136 }
138  UOPathState* /*parent_node*/, bool doors_block )
139 {
140  Multi::UMulti* supporting_multi = nullptr;
141  Items::Item* walkon_item = nullptr;
142 
143  UOPathState SolutionStartNode = ( *( astarsearch->GetSolutionStart() ) );
144  UOPathState SolutionEndNode = ( *( astarsearch->GetSolutionEnd() ) );
145  UOPathState* NewNode = new UOPathState();
146 
147  for ( short i = -1; i <= 1; i++ )
148  {
149  for ( short j = -1; j <= 1; j++ )
150  {
151  if ( ( i == 0 ) && ( j == 0 ) )
152  continue;
153 
154  short newx = x + i;
155  short newy = y + j;
156  short newz = z;
157 
158  if ( ( newx < 0 ) || ( newx < ( theBlockers->xLow ) ) || ( newx > ( theBlockers->xHigh ) ) ||
159  ( newx > ( (int)realm->width() ) ) )
160  continue;
161 
162  if ( ( newy < 0 ) || ( newy < ( theBlockers->yLow ) ) || ( ( newy > theBlockers->yHigh ) ) ||
163  ( newy > ( (int)realm->height() ) ) )
164  continue;
165 
166  if ( realm->walkheight( newx, newy, z, &newz, &supporting_multi, &walkon_item, doors_block,
168  {
169  // Forbid diagonal move, if between 2 blockers - OWHorus {2011-04-26)
170  bool blocked = false;
171  if ( ( i != 0 ) && ( j != 0 ) ) // do only for diagonal moves
172  {
173  // If both neighbouring tiles are blocked, the move is illegal (diagonal move)
174  if ( !realm->walkheight( x + i, y, z, &newz, &supporting_multi, &walkon_item, doors_block,
176  blocked = !( realm->walkheight( x, y + j, z, &newz, &supporting_multi, &walkon_item,
177  doors_block, Core::MOVEMODE_LAND ) );
178  }
179 
180  if ( !blocked )
181  {
182  NewNode->x = newx;
183  NewNode->y = newy;
184  NewNode->z = newz;
185  NewNode->realm = realm;
186  NewNode->theBlockers = theBlockers;
187 
188  if ( ( !NewNode->IsSameState( SolutionStartNode ) ) &&
189  ( !NewNode->IsSameState( SolutionEndNode ) ) )
190  blocked = ( theBlockers->IsBlocking( newx, newy, newz ) );
191  }
192 
193  if ( !blocked )
194  {
195  if ( !astarsearch->AddSuccessor( *NewNode ) )
196  {
197  delete NewNode;
198  return false;
199  }
200  }
201  }
202  }
203  }
204 
205  delete NewNode;
206  return true;
207 }
208 } // namespace Core
209 } // namespace Pol
210 #endif
std::string Name()
Definition: uopathnode.h:131
float GetCost(UOPathState &successor)
Definition: uopathnode.h:122
AStarBlockers(short xL, short xH, short yL, short yH)
Definition: uopathnode.h:36
STL namespace.
bool AddSuccessor(UserState &State)
Definition: stlastar.h:396
bool GetSuccessors(Plib::AStarSearch< UOPathState > *astarsearch, UOPathState *parent_node, bool doors_block)
Definition: uopathnode.h:137
std::vector< BlockNode * > BlockNodeVector
Definition: uopathnode.h:33
bool IsGoal(UOPathState &nodeGoal)
Definition: uopathnode.h:116
bool IsSameState(UOPathState &rhs)
Definition: uopathnode.h:108
void AddBlocker(short x, short y, short z)
Definition: uopathnode.h:44
BlockNodeVector m_List
Definition: uopathnode.h:73
unsigned char default_character_height
Definition: ssopt.h:97
SettingsManager settingsManager
Definition: settings.cpp:14
Realms::Realm * realm
Definition: uopathnode.h:83
UOPathState(short newx, short newy, short newz, Realms::Realm *newrealm, AStarBlockers *blockers)
Definition: uopathnode.h:91
Realms::Realm * find_realm(const std::string &name)
Definition: realms.cpp:64
float GoalDistanceEstimate(UOPathState &nodeGoal)
Definition: uopathnode.h:112
AStarBlockers * theBlockers
Definition: uopathnode.h:79
bool IsBlocking(short x, short y, short z)
Definition: uopathnode.h:63
UserState * GetSolutionStart()
Definition: stlastar.h:440
Definition: berror.cpp:12
UserState * GetSolutionEnd()
Definition: stlastar.h:470