OSPATHBT.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <OSPATH.h>
  21. #ifdef NO_DEBUG_SEARCH
  22. #undef err_when
  23. #undef err_here
  24. #undef err_if
  25. #undef err_else
  26. #undef err_now
  27. #define err_when(cond)
  28. #define err_here()
  29. #define err_if(cond)
  30. #define err_else
  31. #define err_now(msg)
  32. #undef DEBUG
  33. #endif
  34. NodePriorityQueue SeekPath::open_node_list;
  35. NodePriorityQueue SeekPath::closed_node_list;
  36. //-------- Begin of function NodePriorityQueue::reset_priority_queue -------//
  37. void NodePriorityQueue::reset_priority_queue()
  38. {
  39. size = 0U;
  40. memset(elements, 0, sizeof(Node*)*(MAX_ARRAY_SIZE));
  41. }
  42. //-------- End of function NodePriorityQueue::reset_priority_queue ---------//
  43. //-------- Begin of function NodePriorityQueue::insert_node -------//
  44. void NodePriorityQueue::insert_node(Node *insertNode)
  45. {
  46. UINT i = ++size;
  47. register int f=insertNode->node_f;
  48. Node **localElements = elements;
  49. while(i>1 && localElements[i/2]->node_f > f)
  50. {
  51. localElements[i] = localElements[i/2];
  52. i /= 2;
  53. }
  54. localElements[i] = insertNode;
  55. }
  56. //-------- End of function NodePriorityQueue::insert_node ---------//
  57. //-------- Begin of function NodePriorityQueue::return_min -------//
  58. Node* NodePriorityQueue::return_min()
  59. {
  60. if(!size)
  61. return NULL;
  62. UINT i, child, doubleI;
  63. UINT localSize = size--;
  64. Node **localElements = elements;
  65. Node *minElement = localElements[1];
  66. Node *lastElement = localElements[localSize];
  67. int lastF = lastElement->node_f;
  68. //--------- doubleI = i*2 ---------//
  69. for(i=1, doubleI=2; doubleI<=localSize; i=child, doubleI=i<<1)
  70. {
  71. child = doubleI;
  72. if(child!=localSize && localElements[child+1]->node_f < localElements[child]->node_f)
  73. child++;
  74. if(lastF > localElements[child]->node_f)
  75. localElements[i] = localElements[child];
  76. else
  77. break;
  78. }
  79. localElements[i] = lastElement;
  80. return minElement;
  81. }
  82. //-------- End of function NodePriorityQueue::return_min ---------//
  83. //-------- Begin of function SeekPath::return_best_node -------//
  84. // return : <Node*> the best node.
  85. // NULL if no more node on the open node list. There is no
  86. // possible path between the starting and destination points.
  87. //
  88. Node* SeekPath::return_best_node()
  89. {
  90. Node *tempNode = open_node_list.return_min();
  91. if(tempNode)
  92. closed_node_list.insert_node(tempNode);
  93. return tempNode;
  94. }
  95. //-------- End of function SeekPath::return_best_node ---------//
  96. //----- Begin of function SeekPath::return_closest_node -------//
  97. // Return the node that is closest to the destination.
  98. //
  99. Node* SeekPath::return_closest_node()
  100. {
  101. Node *nodePtr;
  102. Node *shortestNode;
  103. int shortestDistance=0x7FFFFFFF;
  104. shortestNode = open_node_list.return_min();
  105. if(shortestNode)
  106. shortestDistance = shortestNode->node_h;
  107. nodePtr = closed_node_list.return_min();
  108. if(nodePtr && nodePtr->node_h<shortestDistance)
  109. {
  110. shortestNode = nodePtr;
  111. shortestDistance = nodePtr->node_h;
  112. }
  113. //--------------------------------------------------------------------------------//
  114. // there may be some nodes with the same shortest Distance from the destination node.
  115. // However, one should be closer to the starting node. The following is used to find
  116. // out the closer node.
  117. //--------------------------------------------------------------------------------//
  118. if(!shortestNode)
  119. return NULL;
  120. nodePtr = shortestNode->parent_node;
  121. while(nodePtr)
  122. {
  123. if(nodePtr->node_h <= shortestDistance)
  124. {
  125. shortestDistance = nodePtr->node_h;
  126. shortestNode = nodePtr;
  127. }
  128. else
  129. break;
  130. nodePtr=nodePtr->parent_node;
  131. }
  132. return shortestNode;
  133. }
  134. //----- End of function SeekPath::return_closest_node -------//