Advertisement
PT_

Untitled

PT_
Jun 20th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. FindPath:
  2. ld iy, PathFindingData
  3. ld (iy+PFStartX), 2
  4. ld (iy+PFStartY), 4
  5. ld (iy+PFEndX), 6
  6. ld (iy+PFEndY), 5
  7. ld (iy+PFAmountOfOpenTiles), 1
  8. ld (iy+PFAmountOfClosedTiles), 0
  9. ld hl, PFOpenedList
  10. ld (hl), 2
  11. inc hl
  12. ld (hl), 4
  13. inc hl
  14. ld (hl), 0
  15. inc hl
  16. ld (hl), 5-4+6-2
  17. jr FindPath2
  18. TempMapData:
  19. .db 0, 0, 0, 0, 0, 0, 0, 0, 0
  20. .db 0, 1, 1, 1, 1, 1, 1, 1, 0
  21. .db 0, 1, 1, 1, 0, 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, 0, 1, 0, 1, 1, 1, 0
  25. .db 0, 1, 1, 1, 1, 1, 1, 1, 0
  26. .db 0, 0, 0, 0, 0, 0, 0, 0, 0
  27. FindPath2:
  28. ; Get the minimum score
  29. ld ix, PFOpenedList-4
  30. xor a, a
  31. ld b, a ; B = index of tile in open list
  32. ld (iy+PFIndexOfCurInOpenList), a
  33. dec a ; A = current minimum score
  34. GetMinimumTile:
  35. lea ix, ix+4
  36. ld c, (ix+3) ; C = score of tile in open list
  37. cp a, c
  38. jr c, +_
  39. ld a, c ; The score is lower than the current score, so change the active tile and score
  40. ld (iy+PFIndexOfCurInOpenList), b
  41. _: inc b
  42. ld d, a ; D = temp current minimum score
  43. ld a, b
  44. cp a, (iy+PFAmountOfOpenTiles) ; Check if the index is equal to the amount of tiles in the open list
  45. ld a, d
  46. jr nz, GetMinimumTile
  47. ld e, (iy+PFIndexOfCurInOpenList) ; Get the tile with the lowest score
  48. ld d, 4
  49. mlt de
  50. ld ix, PFOpenedList
  51. add ix, de
  52. ld hl, (ix)
  53. ld (iy+PFCurX), hl ; Copy the X, Y and depth of the current tile
  54. ld a, (iy+PFIndexOfCurInOpenList)
  55. ld b, (iy+PFAmountOfOpenTiles)
  56. dec b
  57. ld (iy+PFAmountOfOpenTiles), b
  58. sub a, b
  59. jr z, CheckLeftNeighbour ; The selected tile was the last in the list, so don't move the others
  60. lea de, ix ; Move selected tile to closed list
  61. lea hl, ix+4
  62. ld c, a
  63. ld b, 4
  64. ldir
  65. CheckLeftNeighbour:
  66. ld a, (iy+PFCurX)
  67. or a, a
  68. jr z, CheckUpperNeighbour
  69. dec a
  70. ld d, a
  71. ld e, (iy+PFCurY)
  72. call FindTileInBothLists
  73. call nz, CheckIfTileIsWalkable
  74. call nz, AddTileToOpenList
  75. CheckUpperNeighbour:
  76. ld a, (iy+PFCurY)
  77. or a, a
  78. jr z, CheckRightNeighbour
  79. dec a
  80. ld e, a
  81. ld d, (iy+PFCurX)
  82. call FindTileInBothLists
  83. call nz, CheckIfTileIsWalkable
  84. call nz, AddTileToOpenList
  85. CheckRightNeighbour:
  86. ld a, (iy+PFCurX)
  87. cp a, 8
  88. jr z, CheckLowerNeightbour
  89. inc a
  90. ld d, a
  91. ld e, (iy+PFCurY)
  92. call FindTileInBothLists
  93. call nz, CheckIfTileIsWalkable
  94. call nz, AddTileToOpenList
  95. CheckLowerNeightbour:
  96. ld a, (iy+PFCurY)
  97. cp a, 7
  98. jr z, +_
  99. inc a
  100. ld e, a
  101. ld d, (iy+PFCurX)
  102. call FindTileInBothLists
  103. call nz, CheckIfTileIsWalkable
  104. call nz, AddTileToOpenList
  105. _: ret
  106.  
  107. FindTileInBothLists:
  108. ld hl, PFOpenedList
  109. ld b, (iy+PFAmountOfOpenTiles
  110. call FindTileInList
  111. ld hl, PFClosedList
  112. ld b, (iy+PFAmountOfClosedTiles)
  113. FindTileInList:
  114. ; Input:
  115. ; D = X coordinate
  116. ; E = Y coordinate
  117. ld a, b
  118. or a, a
  119. jr nz, FindTileInListLoop
  120. inc a
  121. ret
  122. FindTileInListLoop:
  123. ld a, (hl)
  124. inc hl
  125. ld c, (hl)
  126. inc hl
  127. inc hl
  128. inc hl
  129. cp a, d
  130. jr nz, +_
  131. ld a, c
  132. cp a, e
  133. ret z
  134. _: djnz FindTileInListLoop
  135. ret
  136.  
  137. CheckIfTileIsWalkable:
  138. ld l, e
  139. ld h, 9
  140. mlt hl
  141. ld c, d
  142. ld b, 1
  143. mlt bc
  144. add hl, bc
  145. ld bc, TempMapData
  146. add hl, bc
  147. ld a, (hl)
  148. or a, a
  149. ret
  150.  
  151. AddTileToOpenList:
  152. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement