Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. Node = {x=0,y=0,z=0,g=0,h=0,f=0,pid="",ID=0,active=true,
  2.  
  3. setActive = function(self,state)
  4. self.active = state
  5. end,
  6.  
  7. isActive = function(self)
  8. return self.active
  9. end,
  10.  
  11. getPos = function(self)
  12. return self.x,self.y,self.z
  13. end,
  14.  
  15. setPos = function(self,xs,ys,zs)
  16. self.x,self.y,self.z = xs,ys,zs
  17. end,
  18.  
  19. getID = function(self)
  20. return self.ID
  21. end,
  22.  
  23. setID = function(self,id)
  24. self.ID = id
  25. end,
  26.  
  27. getPid = function(self)
  28. return self.pid
  29. end,
  30.  
  31. setPid = function(self,id)
  32. self.pid = id
  33. end,
  34.  
  35. getPosString = function(self)
  36. return self.x..","..self.y..","..self.z
  37. end}
  38.  
  39. function Node:new(xs,ys,zs,xe,ye,ze,id,pid,s,penalty,o)
  40. if not penalty then penalty = 0 end
  41. o = o or {}
  42. setmetatable(o, self)
  43. self.__index = self
  44. if not s then o.g = nodes[pid].g + 10
  45. o.pid = nodes[pid]:getPid()..pid.."," end
  46. o.h = (math.abs(xs-xe)+math.abs(ys-ye)+math.abs(zs-ze)-1)*10+penalty
  47. o.f = o.h + o.g
  48. o.x,o.y,o.z = xs,ys,zs
  49. o.ID = id
  50. nodes[id] = o
  51. nodeF[id] = o.f
  52. return o
  53. end
  54.  
  55. function exploreTypeSel(xq,yq,zq,xw,yw,zw,goback,mode)
  56. print("Warning: exploreTypeSel() is in beta testing, expect an occasional bug.")
  57. mapSidesType()
  58. if goback == nil then goback = true end
  59. if mode == nil then mode = true end
  60. local xs,ys,zs = getPos()
  61. local q,w
  62. local xe,ye,ze
  63. local a
  64. local done = false
  65. local loops = 1
  66. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  67. local tmp = #q
  68. if #q == 0 then exploreProgress(1,1,4) return true end
  69. for i=1,2,1 do
  70. while not done do
  71. w = getSeperated(q[loops]..",|")
  72. xe,ye,ze = w[1],w[2],w[3]
  73. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  74. if a and a ~= 6 then
  75. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  76. pathfind(t1,t2,t3,10)
  77. mapSidesType()
  78. q = getCubeSel(xq,yq,zq,xw,yw,zw,126,i)
  79. tmp = #q
  80. end
  81. loops = loops + 1
  82. if loops > tmp then done = true end
  83. if mode and not done then
  84. if i == 0 then
  85. exploreProgress(loops,tmp,i)
  86. else
  87. exploreProgress(loops,tmp,i)
  88. end
  89. end
  90. end
  91. done = false
  92. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  93. tmp = #q
  94. loops = 1
  95. end
  96. while not done do --Safety loop, gets any missed spots at the cost of being slower. That's why we did a fast loop before, to clear out most of the unmapped blocks
  97. w = getSeperated(q[loops]..",|")
  98. xe,ye,ze = w[1],w[2],w[3]
  99. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  100. if a and a ~= 6 then
  101. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  102. pathfind(t1,t2,t3,10)
  103. mapSidesType()
  104. end
  105. loops = loops + 1
  106. if loops > tmp then done = true end
  107. if mode and not done then
  108. exploreProgress(loops,tmp,3)
  109. end
  110. end
  111. exploreProgress(loops-1,tmp,4)
  112. if goback then pathfind(xs,ys,zs) end
  113. return done
  114. end
  115.  
  116. function exploreSel(xq,yq,zq,xw,yw,zw,goback,mode)
  117. print("Warning: exploreSel() is in beta testing, expect an occasional bug.")
  118. mapSides()
  119. if goback == nil then goback = true end
  120. if mode == nil then mode = true end
  121. local xs,ys,zs = getPos()
  122. local q,w
  123. local xe,ye,ze
  124. local t1,t2,t3
  125. local a
  126. local done = false
  127. local loops = 1
  128. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  129. local tmp = #q
  130. if #q == 0 then exploreProgress(1,1,4) return true end
  131. for i=1,2,1 do
  132. while not done do
  133. w = getSeperated(q[loops]..",|")
  134. xe,ye,ze = w[1],w[2],w[3]
  135. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  136. if a and a ~= 6 then
  137. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  138. pathfind(t1,t2,t3,10)
  139. mapSides()
  140. q = getCubeSel(xq,yq,zq,xw,yw,zw,126,i)
  141. tmp = #q
  142. end
  143. loops = loops + 1
  144. if loops > tmp then done = true end
  145. if mode and not done then
  146. if i == 0 then
  147. exploreProgress(loops,tmp,i)
  148. else
  149. exploreProgress(loops,tmp,i)
  150. end
  151. end
  152. end
  153. done = false
  154. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  155. tmp = #q
  156. loops = 1
  157. end
  158. while not done do --Safety loop, gets any missed spots at the cost of being slower. That's why we did a fast loop before, to clear out most of the unmapped blocks
  159. w = getSeperated(q[loops]..",|")
  160. xe,ye,ze = w[1],w[2],w[3]
  161. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  162. if a and a ~= 6 then
  163. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  164. pathfind(t1,t2,t3,10)
  165. mapSides()
  166. end
  167. loops = loops + 1
  168. if loops > tmp then done = true end
  169. if mode and not done then
  170. exploreProgress(loops,tmp,3)
  171. end
  172. end
  173. exploreProgress(loops-1,tmp,4)
  174. if goback then pathfind(xs,ys,zs) end
  175. return done
  176. end
  177.  
  178. function pathfind(xe,ye,ze,tc,up,s,t)
  179. local ue
  180. if not tc then ue = false else ue = true end
  181. if not tc then tc = 10 else tc = math.abs(tc) end
  182. if not up then up = false end
  183. if not s then s = 0 end
  184. if not t then t = false else t = true end
  185. local xs,ys,zs = getPos()
  186. local waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  187. local tmp,done,breaking
  188. local tries = 0
  189. local prev = waypoints
  190. local cooldown = 0
  191. while not done do
  192. tries = tries + 1
  193. tmp = 0
  194. for i=1,min(#prev,#waypoints),1 do
  195. if prev[i]:getPos() ~= waypoints[i]:getPos() then breaking = i end
  196. end
  197. if not breaking then breaking = -1 end
  198. if breaking > -1 then pathfind(waypoints[breaking - 1]:getPos()) end
  199. for k,v in pairs(waypoints) do
  200. cooldown = cooldown + 1 if cooldown >= 100 then cooldown = 0 sleep(0) end
  201. if (k >= breaking or breaking == -1) and not goToCoordsPath(v:getPos()) and k ~= 1 then
  202. tmp = -1
  203. done = false
  204. break
  205. end
  206. end
  207. prev = waypoints
  208. if tmp ~= -1 then
  209. goToCoordsPath(xe,ye,ze)
  210. done = true
  211. else
  212. if s < 1 then
  213. if t then mapSidesType() else mapSides() end
  214. if tries >= tc then tries = 0 xs,ys,zs = getPos() end
  215. waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  216. else
  217. explore(s)
  218. if tries >= tc then tries = 0 xs,ys,zs = getPos() end
  219. waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  220. end
  221. end
  222. end
  223. return true
  224. end
  225.  
  226. function getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  227. local returnpack = {}
  228. local done = false
  229. local xp,yp,zp = xs,ys,zs
  230. local penalty = 0
  231. if xs == xe and ys == ye and zs == ze then return {xs..","..ys..","..zs} end
  232. local open = {}
  233. nodes = {} nodeF = {}
  234. local closest = nil
  235. if s and getBlock(xe,ye,ze) == 126 then
  236. closest = getClosest(xe,ye,ze,5,air)
  237. if #closest ~= 0 then
  238. local w = getSeperated(closest[1]..",|")
  239. pathfind(w[1],w[2],w[3])
  240. end
  241. end
  242. local tmp,temp
  243. local cooldown = 0
  244. open[1] = Node:new(xs,ys,zs,xe,ye,ze,1,false,true)
  245. table.sort(nodeF)
  246. table.sort(open, function(a,b) return a.f<b.f end)
  247. while not done do
  248. for ii=0,5,1 do
  249. tmp = nil
  250. temp = false
  251. cooldown = cooldown + 1 if cooldown > 50 then sleep(0) cooldown = 0 end
  252. xs,ys,zs = open[1]:getPos()
  253. if (getBlockInDir(xs,ys,zs,ii) == 0 or (ue and getBlockInDir(xs,ys,zs,ii) == 126)) and temp ~= true then
  254. if getBlockInDir(xs,ys,zs,ii) == 126 then penalty = up end
  255. xs,ys,zs = getCoordsInDir(xs,ys,zs,ii)
  256. for iii=1,#open,1 do if open[iii]:getPosString() == xs..","..ys..","..zs then tmp = true temp = open[iii] end end
  257. if not tmp then
  258. open[#open+1] = Node:new(xs,ys,zs,xe,ye,ze,#nodes+1,open[1].ID,false,penalty)
  259. if open[#open]:getPosString() == xe..","..ye..","..ze then done = open[#open] end
  260. elseif open[1].g > temp.g + 10 then
  261. open[1].pid = temp.pid
  262. tmp = getSeperated(open[1].pid..",|")
  263. open[1].g = nodes[tmp[#tmp]].g + 10
  264. open[1].f = open[1].g + open[1].h
  265. nodeF[open[1].ID] = open[1].f
  266. table.sort(nodeF)
  267. table.sort(open, function(a,b) return a.f<b.f end)
  268. end
  269. end
  270. penalty = 0
  271. end
  272. if temp == true then xs,ys,zs = xp,yp,zp open[1] = Node:new(xp,yp,zp,xe,ye,ze,1,false,true) break end
  273. nodeF[open[1].ID] = math.huge
  274. table.remove(open,1)
  275. table.sort(nodeF)
  276. table.sort(open, function(a,b) return a.f<b.f end)
  277. if #open == 0 then done = false end
  278. end
  279. table.sort(nodeF)
  280. table.sort(open, function(a,b) return a.f<b.f end)
  281. if done then
  282. tmp = getSeperated(done.pid.."|")
  283. for i=1,#tmp,1 do
  284. table.insert(returnpack,nodes[tmp[i]])
  285. end
  286. nodes = {} nodeF = {}
  287. return returnpack
  288. else nodes = {} nodeF = {} return {} end
  289. end
Add Comment
Please, Sign In to add comment