@@ -23,8 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
23
23
/* *****************************************************************************/
24
24
25
25
#include " pathfinder.h"
26
- #include " serverenvironment.h"
27
- #include " server.h"
26
+ #include " map.h"
28
27
#include " nodedef.h"
29
28
30
29
// #define PATHFINDER_DEBUG
@@ -180,10 +179,8 @@ class MapGridNodeContainer : public GridNodeContainer {
180
179
class Pathfinder {
181
180
182
181
public:
183
- /* *
184
- * default constructor
185
- */
186
- Pathfinder () = default ;
182
+ Pathfinder () = delete ;
183
+ Pathfinder (Map *map, const NodeDefManager *ndef) : m_map(map), m_ndef(ndef) {}
187
184
188
185
~Pathfinder ();
189
186
@@ -197,8 +194,7 @@ class Pathfinder {
197
194
* @param max_drop maximum number of blocks a path may drop
198
195
* @param algo Algorithm to use for finding a path
199
196
*/
200
- std::vector<v3s16> getPath (ServerEnvironment *env,
201
- v3s16 source,
197
+ std::vector<v3s16> getPath (v3s16 source,
202
198
v3s16 destination,
203
199
unsigned int searchdistance,
204
200
unsigned int max_jump,
@@ -328,7 +324,9 @@ class Pathfinder {
328
324
friend class GridNodeContainer ;
329
325
GridNodeContainer *m_nodes_container = nullptr ;
330
326
331
- ServerEnvironment *m_env = 0 ; /* *< minetest environment pointer */
327
+ Map *m_map = nullptr ;
328
+
329
+ const NodeDefManager *m_ndef = nullptr ;
332
330
333
331
friend class PathfinderCompareHeuristic ;
334
332
@@ -410,18 +408,15 @@ class PathfinderCompareHeuristic
410
408
/* implementation */
411
409
/* *****************************************************************************/
412
410
413
- std::vector<v3s16> get_path (ServerEnvironment* env ,
414
- v3s16 source,
415
- v3s16 destination,
416
- unsigned int searchdistance,
417
- unsigned int max_jump,
418
- unsigned int max_drop,
419
- PathAlgorithm algo)
411
+ std::vector<v3s16> get_path (Map* map, const NodeDefManager *ndef ,
412
+ v3s16 source,
413
+ v3s16 destination,
414
+ unsigned int searchdistance,
415
+ unsigned int max_jump,
416
+ unsigned int max_drop,
417
+ PathAlgorithm algo)
420
418
{
421
- Pathfinder searchclass;
422
-
423
- return searchclass.getPath (env,
424
- source, destination,
419
+ return Pathfinder (map, ndef).getPath (source, destination,
425
420
searchdistance, max_jump, max_drop, algo);
426
421
}
427
422
@@ -521,13 +516,13 @@ void PathGridnode::setCost(v3s16 dir, const PathCost &cost)
521
516
522
517
void GridNodeContainer::initNode (v3s16 ipos, PathGridnode *p_node)
523
518
{
524
- const NodeDefManager *ndef = m_pathf->m_env -> getGameDef ()-> ndef () ;
519
+ const NodeDefManager *ndef = m_pathf->m_ndef ;
525
520
PathGridnode &elem = *p_node;
526
521
527
522
v3s16 realpos = m_pathf->getRealPos (ipos);
528
523
529
- MapNode current = m_pathf->m_env -> getMap (). getNode (realpos);
530
- MapNode below = m_pathf->m_env -> getMap (). getNode (realpos + v3s16 (0 , -1 , 0 ));
524
+ MapNode current = m_pathf->m_map -> getNode (realpos);
525
+ MapNode below = m_pathf->m_map -> getNode (realpos + v3s16 (0 , -1 , 0 ));
531
526
532
527
533
528
if ((current.param0 == CONTENT_IGNORE) ||
@@ -610,8 +605,7 @@ PathGridnode &MapGridNodeContainer::access(v3s16 p)
610
605
611
606
612
607
/* *****************************************************************************/
613
- std::vector<v3s16> Pathfinder::getPath (ServerEnvironment *env,
614
- v3s16 source,
608
+ std::vector<v3s16> Pathfinder::getPath (v3s16 source,
615
609
v3s16 destination,
616
610
unsigned int searchdistance,
617
611
unsigned int max_jump,
@@ -624,15 +618,8 @@ std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
624
618
#endif
625
619
std::vector<v3s16> retval;
626
620
627
- // check parameters
628
- if (env == 0 ) {
629
- ERROR_TARGET << " Missing environment pointer" << std::endl;
630
- return retval;
631
- }
632
-
633
621
// initialization
634
622
m_searchdistance = searchdistance;
635
- m_env = env;
636
623
m_maxjump = max_jump;
637
624
m_maxdrop = max_drop;
638
625
m_start = source;
@@ -681,15 +668,14 @@ std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
681
668
#endif
682
669
683
670
// fail if source or destination is walkable
684
- const NodeDefManager *ndef = m_env->getGameDef ()->ndef ();
685
- MapNode node_at_pos = m_env->getMap ().getNode (destination);
686
- if (ndef->get (node_at_pos).walkable ) {
671
+ MapNode node_at_pos = m_map->getNode (destination);
672
+ if (m_ndef->get (node_at_pos).walkable ) {
687
673
VERBOSE_TARGET << " Destination is walkable. " <<
688
674
" Pos: " << PP (destination) << std::endl;
689
675
return retval;
690
676
}
691
- node_at_pos = m_env-> getMap (). getNode (source);
692
- if (ndef ->get (node_at_pos).walkable ) {
677
+ node_at_pos = m_map-> getNode (source);
678
+ if (m_ndef ->get (node_at_pos).walkable ) {
693
679
VERBOSE_TARGET << " Source is walkable. " <<
694
680
" Pos: " << PP (source) << std::endl;
695
681
return retval;
@@ -843,7 +829,6 @@ v3s16 Pathfinder::getRealPos(v3s16 ipos)
843
829
/* *****************************************************************************/
844
830
PathCost Pathfinder::calcCost (v3s16 pos, v3s16 dir)
845
831
{
846
- const NodeDefManager *ndef = m_env->getGameDef ()->ndef ();
847
832
PathCost retval;
848
833
849
834
retval.updated = true ;
@@ -857,7 +842,7 @@ PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
857
842
return retval;
858
843
}
859
844
860
- MapNode node_at_pos2 = m_env-> getMap (). getNode (pos2);
845
+ MapNode node_at_pos2 = m_map-> getNode (pos2);
861
846
862
847
// did we get information about node?
863
848
if (node_at_pos2.param0 == CONTENT_IGNORE ) {
@@ -866,9 +851,9 @@ PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
866
851
return retval;
867
852
}
868
853
869
- if (!ndef ->get (node_at_pos2).walkable ) {
854
+ if (!m_ndef ->get (node_at_pos2).walkable ) {
870
855
MapNode node_below_pos2 =
871
- m_env-> getMap (). getNode (pos2 + v3s16 (0 , -1 , 0 ));
856
+ m_map-> getNode (pos2 + v3s16 (0 , -1 , 0 ));
872
857
873
858
// did we get information about node?
874
859
if (node_below_pos2.param0 == CONTENT_IGNORE ) {
@@ -878,7 +863,7 @@ PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
878
863
}
879
864
880
865
// test if the same-height neighbor is suitable
881
- if (ndef ->get (node_below_pos2).walkable ) {
866
+ if (m_ndef ->get (node_below_pos2).walkable ) {
882
867
// SUCCESS!
883
868
retval.valid = true ;
884
869
retval.value = 1 ;
@@ -889,19 +874,19 @@ PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
889
874
else {
890
875
// test if we can fall a couple of nodes (m_maxdrop)
891
876
v3s16 testpos = pos2 + v3s16 (0 , -1 , 0 );
892
- MapNode node_at_pos = m_env-> getMap (). getNode (testpos);
877
+ MapNode node_at_pos = m_map-> getNode (testpos);
893
878
894
879
while ((node_at_pos.param0 != CONTENT_IGNORE) &&
895
- (!ndef ->get (node_at_pos).walkable ) &&
880
+ (!m_ndef ->get (node_at_pos).walkable ) &&
896
881
(testpos.Y > m_limits.MinEdge .Y )) {
897
882
testpos += v3s16 (0 , -1 , 0 );
898
- node_at_pos = m_env-> getMap (). getNode (testpos);
883
+ node_at_pos = m_map-> getNode (testpos);
899
884
}
900
885
901
886
// did we find surface?
902
887
if ((testpos.Y >= m_limits.MinEdge .Y ) &&
903
888
(node_at_pos.param0 != CONTENT_IGNORE) &&
904
- (ndef ->get (node_at_pos).walkable )) {
889
+ (m_ndef ->get (node_at_pos).walkable )) {
905
890
if ((pos2.Y - testpos.Y - 1 ) <= m_maxdrop) {
906
891
// SUCCESS!
907
892
retval.valid = true ;
@@ -927,34 +912,34 @@ PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
927
912
928
913
v3s16 targetpos = pos2; // position for jump target
929
914
v3s16 jumppos = pos; // position for checking if jumping space is free
930
- MapNode node_target = m_env-> getMap (). getNode (targetpos);
931
- MapNode node_jump = m_env-> getMap (). getNode (jumppos);
915
+ MapNode node_target = m_map-> getNode (targetpos);
916
+ MapNode node_jump = m_map-> getNode (jumppos);
932
917
bool headbanger = false ; // true if anything blocks jumppath
933
918
934
919
while ((node_target.param0 != CONTENT_IGNORE) &&
935
- (ndef ->get (node_target).walkable ) &&
920
+ (m_ndef ->get (node_target).walkable ) &&
936
921
(targetpos.Y < m_limits.MaxEdge .Y )) {
937
922
// if the jump would hit any solid node, discard
938
923
if ((node_jump.param0 == CONTENT_IGNORE) ||
939
- (ndef ->get (node_jump).walkable )) {
924
+ (m_ndef ->get (node_jump).walkable )) {
940
925
headbanger = true ;
941
926
break ;
942
927
}
943
928
targetpos += v3s16 (0 , 1 , 0 );
944
929
jumppos += v3s16 (0 , 1 , 0 );
945
- node_target = m_env-> getMap (). getNode (targetpos);
946
- node_jump = m_env-> getMap (). getNode (jumppos);
930
+ node_target = m_map-> getNode (targetpos);
931
+ node_jump = m_map-> getNode (jumppos);
947
932
948
933
}
949
934
// check headbanger one last time
950
935
if ((node_jump.param0 == CONTENT_IGNORE) ||
951
- (ndef ->get (node_jump).walkable )) {
936
+ (m_ndef ->get (node_jump).walkable )) {
952
937
headbanger = true ;
953
938
}
954
939
955
940
// did we find surface without banging our head?
956
941
if ((!headbanger) && (targetpos.Y <= m_limits.MaxEdge .Y ) &&
957
- (!ndef ->get (node_target).walkable )) {
942
+ (!m_ndef ->get (node_target).walkable )) {
958
943
959
944
if (targetpos.Y - pos2.Y <= m_maxjump) {
960
945
// SUCCESS!
@@ -1254,21 +1239,20 @@ v3s16 Pathfinder::walkDownwards(v3s16 pos, unsigned int max_down) {
1254
1239
if (max_down == 0 )
1255
1240
return pos;
1256
1241
v3s16 testpos = v3s16 (pos);
1257
- MapNode node_at_pos = m_env->getMap ().getNode (testpos);
1258
- const NodeDefManager *ndef = m_env->getGameDef ()->ndef ();
1242
+ MapNode node_at_pos = m_map->getNode (testpos);
1259
1243
unsigned int down = 0 ;
1260
1244
while ((node_at_pos.param0 != CONTENT_IGNORE) &&
1261
- (!ndef ->get (node_at_pos).walkable ) &&
1245
+ (!m_ndef ->get (node_at_pos).walkable ) &&
1262
1246
(testpos.Y > m_limits.MinEdge .Y ) &&
1263
1247
(down <= max_down)) {
1264
1248
testpos += v3s16 (0 , -1 , 0 );
1265
1249
down++;
1266
- node_at_pos = m_env-> getMap (). getNode (testpos);
1250
+ node_at_pos = m_map-> getNode (testpos);
1267
1251
}
1268
1252
// did we find surface?
1269
1253
if ((testpos.Y >= m_limits.MinEdge .Y ) &&
1270
1254
(node_at_pos.param0 != CONTENT_IGNORE) &&
1271
- (ndef ->get (node_at_pos).walkable )) {
1255
+ (m_ndef ->get (node_at_pos).walkable )) {
1272
1256
if (down == 0 ) {
1273
1257
pos = testpos;
1274
1258
} else if ((down - 1 ) <= max_down) {
0 commit comments