PT_

Untitled

PT_
Jun 21st, 2017
141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FindPath:
  2. di
  3. ld iy, PathFindingData
  4. ld (iy+PFStartX), 2
  5. ld (iy+PFStartY), 4
  6. ld (iy+PFEndX), 6
  7. ld (iy+PFEndY), 5
  8. ld (iy+PFAmountOfOpenTiles), 1
  9. ld (iy+PFAmountOfClosedTiles), 0
  10. ld hl, PFOpenedList
  11. ld (hl), 2
  12. inc hl
  13. ld (hl), 4
  14. inc hl
  15. ld (hl), 0
  16. inc hl
  17. ld (hl), 5-4+6-2
  18. jr FindPath2
  19. TempMapData:
  20. .db 0, 0, 0, 0, 0, 0, 0, 0, 0
  21. .db 0, 1, 1, 1, 1, 1, 1, 1, 0
  22. .db 0, 1, 1, 1, 0, 1, 1, 1, 0
  23. .db 0, 1, 1, 1, 0, 1, 1, 1, 0
  24. .db 0, 1, 1, 1, 0, 1, 1, 1, 0
  25. .db 0, 1, 0, 1, 0, 1, 1, 1, 0
  26. .db 0, 1, 1, 1, 1, 1, 1, 1, 0
  27. .db 0, 0, 0, 0, 0, 0, 0, 0, 0
  28. FindPath2:
  29. ; Lists stack entry:
  30. ; 1b - X
  31. ; 1b - Y
  32. ; 1b - Depth
  33. ; 1b - Total score
  34.  
  35. ld ix, PFOpenedList-4 ; Get the minimum score
  36. xor a, a
  37. ld b, a ; B = index of tile in open list
  38. ld (iy+PFIndexOfCurInOpenList), a
  39. dec a ; A = current minimum score
  40. GetMinimumTile:
  41. lea ix, ix+4
  42. ld c, (ix+3) ; C = score of tile in open list
  43. cp a, c
  44. jr c, +_
  45. ld a, c ; The score is lower than the current score, so change the active tile and score
  46. ld (iy+PFIndexOfCurInOpenList), b
  47. _: inc b
  48. ld d, a ; D = temp current minimum score
  49. ld a, b
  50. cp a, (iy+PFAmountOfOpenTiles) ; Check if the index is equal to the amount of tiles in the open list
  51. ld a, d
  52. jr nz, GetMinimumTile
  53. ld e, (iy+PFIndexOfCurInOpenList) ; Get the tile with the lowest score
  54. ld d, 4
  55. mlt de
  56. ld ix, PFOpenedList
  57. add ix, de
  58. ld e, (iy+PFAmountOfClosedTiles) ; Copy current tile to closed tiles
  59. ld d, 4
  60. mlt de
  61. ld hl, PFClosedList
  62. add hl, de
  63. ld bc, (ix)
  64. ld (hl), bc
  65. ld a, (ix+3)
  66. inc hl
  67. inc hl
  68. inc hl
  69. ld (hl), a
  70. inc (iy+PFAmountOfClosedTiles)
  71. ld (iy+PFCurX), bc ; Copy the X, Y and depth of the current tile
  72. ld a, (iy+PFIndexOfCurInOpenList)
  73. ld b, (iy+PFAmountOfOpenTiles)
  74. dec b
  75. ld (iy+PFAmountOfOpenTiles), b
  76. sub a, b
  77. jr z, CheckLeftNeighbour ; The selected tile was the last in the list, so don't move the others
  78. lea de, ix ; Move selected tile to closed list
  79. lea hl, ix+4
  80. ld c, a
  81. ld b, 4
  82. ldir
  83. CheckLeftNeighbour:
  84. ld d, (iy+PFCurX)
  85. ld e, (iy+PFCurY)
  86. ld a, d
  87. or a, a
  88. jr z, CheckUpperNeighbour
  89. dec a
  90. ld d, a
  91. call FindTileInBothLists
  92. call nz, AddTileIfWalkable
  93. CheckUpperNeighbour:
  94. ld a, (iy+PFCurY)
  95. or a, a
  96. jr z, CheckRightNeighbour
  97. dec a
  98. ld e, a
  99. ld d, (iy+PFCurX)
  100. call FindTileInBothLists
  101. call nz, AddTileIfWalkable
  102. CheckRightNeighbour:
  103. ld a, (iy+PFCurX)
  104. cp a, 8
  105. jr z, CheckLowerNeightbour
  106. inc a
  107. ld d, a
  108. ld e, (iy+PFCurY)
  109. call FindTileInBothLists
  110. call nz, AddTileIfWalkable
  111. CheckLowerNeightbour:
  112. ld a, (iy+PFCurY)
  113. cp a, 7
  114. jr z, +_
  115. inc a
  116. ld e, a
  117. ld d, (iy+PFCurX)
  118. call FindTileInBothLists
  119. call nz, AddTileIfWalkable
  120. _: jp FindPath2
  121. FoundPath:
  122. ret
  123.  
  124. FindTileInBothLists:
  125. ld a, (iy+PFEndX)
  126. cp a, d
  127. jr nz, +_
  128. ld a, (iy+PFEndY)
  129. cp a, e
  130. jr nz, +_
  131. pop hl
  132. jr FoundPath
  133. _: ld hl, PFOpenedList
  134. ld b, (iy+PFAmountOfOpenTiles)
  135. call FindTileInList
  136. ret z
  137. ld hl, PFClosedList
  138. ld b, (iy+PFAmountOfClosedTiles)
  139. FindTileInList:
  140. ; Input:
  141. ; B = Amount of elements
  142. ; D = X coordinate
  143. ; E = Y coordinate
  144. ; HL = Pointer to list
  145. ; Output:
  146. ; Z = Found the tile
  147. ; NZ = Didn't find the tile
  148. ld a, b
  149. or a, a
  150. jr nz, FindTileInListLoop
  151. inc a
  152. ret
  153. FindTileInListLoop:
  154. ld a, (hl)
  155. inc hl
  156. ld c, (hl)
  157. inc hl
  158. inc hl
  159. inc hl
  160. cp a, d
  161. jr nz, +_
  162. ld a, c
  163. cp a, e
  164. ret z
  165. _: djnz FindTileInListLoop
  166. ret
  167.  
  168. AddTileIfWalkable:
  169. ld l, e
  170. ld h, 9
  171. mlt hl
  172. ld c, d
  173. ld b, 1
  174. mlt bc
  175. add hl, bc
  176. ld bc, TempMapData
  177. add hl, bc
  178. ld a, (hl)
  179. or a, a
  180. ret z
  181. ld c, (iy+PFAmountOfOpenTiles)
  182. ld b, 4
  183. mlt bc
  184. ld hl, PFOpenedList
  185. add hl, bc
  186. ld (hl), d
  187. inc hl
  188. ld (hl), e
  189. inc hl
  190. ld a, (iy+PFCurTileDepth)
  191. inc a
  192. ld (hl), a
  193. inc hl
  194. ld c, a
  195. ld a, (iy+PFEndX) ; Find the distance to end, abs(X_end - X_curr) + abs(Y_end - Y_curr)
  196. sub a, d ; abs(X_end - X_curr)
  197. jr nc, +_
  198. neg
  199. _: ld b, a
  200. ld a, (iy+PFEndY) ; abs(Y_end - Y_curr)
  201. sub a, e
  202. jr nc, +_
  203. neg
  204. _: add a, b ; Add them, and add the distance
  205. add a, c
  206. ld (hl), a
  207. inc (iy+PFAmountOfOpenTiles)
  208. ret
RAW Paste Data