Advertisement
Kirkq

7th Saga Script 1

Aug 7th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. local xdest = 100
  2. local ydest = 88
  3. local savestateinterval = 15
  4.  
  5. local xmin = 0
  6. local xmax = 511
  7. local ymin = 0
  8. local ymax = 511
  9. local map1 = 0xce7134
  10. local map2 = 0xce6154
  11. local map3 = 0xce0000
  12. local map4 = 0xce48e0
  13. local sector1
  14. local sector2
  15. local sector3
  16. local sector4
  17. local xcoord
  18. local ycoord
  19. local distance = {[xdest]={[ydest]=0}}
  20. local paths = {[xdest]={[ydest]=1}}
  21. local dx={0,0,-1,1}
  22. local dy={-1,1,0,0}
  23. local dir
  24. local keys
  25. local directions = {"up", "down", "left", "right"}
  26. local optimalpaths = {}
  27. local npaths = {}
  28. local pathdir = {[0]=1}
  29. local stopped = {}
  30. local step
  31. local replay
  32.  
  33. function updatevalue(newx, newy, xcoord, ycoord, distance, paths, changed)
  34. if newx >= xmin and newx <= xmax and newy >= ymin and newy <= ymax then
  35. sector1 = math.floor(newx/32) + 16*math.floor(newy/32) -- check for map collision
  36. sector2 = AND(newx, 0x18)/8 + AND(newy, 0x18)/2
  37. sector3 = AND(newx, 0x6)/2 + 2*AND(newy, 0x6)
  38. sector4 = math.fmod(newx, 2) + 2*math.fmod(newy, 2)
  39. if AND(memory.readbyte(memory.readbyte(memory.readword(memory.readbyte(map1+sector1)*32+map2+2*sector2)*16+map3+sector3)*32+map4+1),2^(5-sector4)) == 0 then
  40. if distance[newx] == nil then
  41. distance[newx] = {}
  42. paths[newx] = {}
  43. end
  44. if distance[newx][newy] == nil or distance[newx][newy] > distance[xcoord][ycoord]+1 then
  45. distance[newx][newy] = distance[xcoord][ycoord] + 1
  46. paths[newx][newy] = paths[xcoord][ycoord]
  47. table.insert(changed,{x=newx, y=newy})
  48. elseif distance[newx][newy] == distance[xcoord][ycoord]+1 then
  49. paths[newx][newy] = paths[newx][newy] + paths[xcoord][ycoord]
  50. end
  51. end
  52. end
  53. end
  54.  
  55.  
  56. --checks to see that distance to destination has lowered.
  57. function isoptimal(newx, newy, xcoord, ycoord, distance)
  58. if distance[newx] ~= nil and distance[newx][newy] ~= nil and distance[newx][newy] == distance[xcoord][ycoord] - 1 then
  59. return true
  60. else
  61. return false
  62. end
  63. end
  64.  
  65. --function only runs after cycle is done.
  66. function nextframe()
  67. if memory.readbyte(0x7e0722) ~= 2 then
  68. xcoord = math.floor((memory.readword(0x7e07a8) + memory.readword(0x7e0a91)+16)/32)
  69. ycoord = math.floor((memory.readword(0x7e07b0) + memory.readword(0x7e0a94)+16)/32)
  70. region = math.floor(xcoord/8) - 64*math.floor(xcoord/512) + 64*math.floor(ycoord/8) - 4096*math.floor(ycoord/512)
  71. encmap = memory.readword(0x7e07a3) + 0x10000*memory.readbyte(0x7e07a5)
  72. gui.text(0,180,"Current location: (" .. xcoord .. ", " .. ycoord .. ")")
  73. if memory.readbyte(0x7e0723) ~= 2 then
  74. gui.text(0,190,"Encounter region: " .. memory.readbyte(encmap+region))
  75. if distance[xcoord] ~= nil and distance[xcoord][ycoord] ~= nil then
  76. gui.text(0,200,"Distance: " .. distance[xcoord][ycoord])
  77. gui.text(0,210,"Paths: " .. paths[xcoord][ycoord])
  78. end
  79. end
  80. else
  81. gui.text(0,190,"Enemy HP:")
  82. if AND(memory.readword(0x7e1e9e),0xa000) == 0 then
  83. gui.text(96,190,memory.readword(0x7e1e84))
  84. end
  85. if AND(memory.readword(0x7e1f1e),0xa000) == 0 then
  86. gui.text(160,190,memory.readword(0x7e1f04))
  87. end
  88. if AND(memory.readword(0x7e1f9e),0xa000) == 0 then
  89. gui.text(224,190,memory.readword(0x7e1f84))
  90. end
  91. end
  92. snes9x.frameadvance()
  93. end
  94. --
  95.  
  96. ---Begin Cycle
  97. snes9x.speedmode("maximum")
  98.  
  99. local i = 1
  100. local changed = {{x=xdest,y=ydest}}
  101. while i <= # changed do
  102. xcoord = changed[i].x
  103. ycoord = changed[i].y
  104. updatevalue(xcoord-1, ycoord, xcoord, ycoord, distance, paths, changed)
  105. updatevalue(xcoord+1, ycoord, xcoord, ycoord, distance, paths, changed)
  106. updatevalue(xcoord, ycoord-1, xcoord, ycoord, distance, paths, changed)
  107. updatevalue(xcoord, ycoord+1, xcoord, ycoord, distance, paths, changed)
  108. i = i + 1
  109. end
  110.  
  111. while memory.readbyte(0x7e0712) ~= 0 do nextframe() end -- wait until input is accepted
  112.  
  113. if distance[xcoord] ~= nil and distance[xcoord][ycoord] ~= nil then
  114. local states = {}
  115. replay = false
  116. step = 0
  117. repeat
  118. step = step + 1
  119. if math.mod(step, savestateinterval) == 1 and not replay then
  120. if states[step] == nil then
  121. states[step] = savestate.create()
  122. end
  123. savestate.save(states[step])
  124. end
  125. xcoord = math.floor((memory.readword(0x7e07a8) + memory.readword(0x7e0a91)+16)/32)
  126. ycoord = math.floor((memory.readword(0x7e07b0) + memory.readword(0x7e0a94)+16)/32)
  127. if optimalpaths[step] == nil then
  128. optimalpaths[step] = {}
  129. end
  130. if not replay then
  131. npaths[step] = 0
  132. for i=1,4 do
  133. optimalpaths[step][i] = isoptimal(xcoord+dx[i],ycoord+dy[i],xcoord,ycoord,distance)
  134. npaths[step] = npaths[step] + (optimalpaths[step][i] and 1 or 0)
  135. end
  136. end
  137. if stopped[step] then
  138. replay = false
  139. end
  140. if optimalpaths[step][pathdir[step-1]] then
  141. dir=pathdir[step-1]
  142. elseif optimalpaths[step][1+XOR(pathdir[step-1]-1,2)] then
  143. dir=1+XOR(pathdir[step-1]-1,2)
  144. elseif optimalpaths[step][1+XOR(pathdir[step-1]-1,3)] then
  145. dir=1+XOR(pathdir[step-1]-1,3)
  146. elseif optimalpaths[step][1+XOR(pathdir[step-1]-1,1)] then
  147. dir=1+XOR(pathdir[step-1]-1,1)
  148. else
  149. break
  150. end
  151. pathdir[step] = dir
  152. keys = {[directions[dir]]=true}
  153. for i=1,16 do
  154. joypad.set(1,keys)
  155. nextframe()
  156. end
  157. if memory.readbyte(0x7e0722) == 2 then
  158. repeat
  159. stopped[step] = true
  160. optimalpaths[step][pathdir[step]] = false
  161. npaths[step] = npaths[step] - 1
  162. step = step - 1
  163. until step == 0 or npaths[step+1] ~= 0
  164. step = savestateinterval * math.floor(step/savestateinterval)
  165. savestate.load(states[step+1])
  166. replay = true
  167. else
  168. stopped[step] = false
  169. end
  170. until xcoord == xdest and ycoord == ydest
  171. snes9x.speedmode("normal")
  172. snes9x.pause()
  173. end
  174.  
  175. while true do nextframe() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement