Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Node = {x=0,y=0,z=0,g=0,h=0,f=0,pid="",ID=0,active=true,
- setActive = function(self,state)
- self.active = state
- end,
- isActive = function(self)
- return self.active
- end,
- getPos = function(self)
- return self.x,self.y,self.z
- end,
- setPos = function(self,xs,ys,zs)
- self.x,self.y,self.z = xs,ys,zs
- end,
- getID = function(self)
- return self.ID
- end,
- setID = function(self,id)
- self.ID = id
- end,
- getPid = function(self)
- return self.pid
- end,
- setPid = function(self,id)
- self.pid = id
- end,
- getPosString = function(self)
- return self.x..","..self.y..","..self.z
- end}
- function Node:new(xs,ys,zs,xe,ye,ze,id,pid,s,penalty,o)
- if not penalty then penalty = 0 end
- o = o or {}
- setmetatable(o, self)
- self.__index = self
- if not s then o.g = nodes[pid].g + 10
- o.pid = nodes[pid]:getPid()..pid.."," end
- o.h = (math.abs(xs-xe)+math.abs(ys-ye)+math.abs(zs-ze)-1)*10+penalty
- o.f = o.h + o.g
- o.x,o.y,o.z = xs,ys,zs
- o.ID = id
- nodes[id] = o
- nodeF[id] = o.f
- return o
- end
- function exploreTypeSel(xq,yq,zq,xw,yw,zw,goback,mode)
- print("Warning: exploreTypeSel() is in beta testing, expect an occasional bug.")
- mapSidesType()
- if goback == nil then goback = true end
- if mode == nil then mode = true end
- local xs,ys,zs = getPos()
- local q,w
- local xe,ye,ze
- local a
- local done = false
- local loops = 1
- q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
- local tmp = #q
- if #q == 0 then exploreProgress(1,1,4) return true end
- for i=1,2,1 do
- while not done do
- w = getSeperated(q[loops]..",|")
- xe,ye,ze = w[1],w[2],w[3]
- a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
- if a and a ~= 6 then
- t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
- pathfind(t1,t2,t3,10)
- mapSidesType()
- q = getCubeSel(xq,yq,zq,xw,yw,zw,126,i)
- tmp = #q
- end
- loops = loops + 1
- if loops > tmp then done = true end
- if mode and not done then
- if i == 0 then
- exploreProgress(loops,tmp,i)
- else
- exploreProgress(loops,tmp,i)
- end
- end
- end
- done = false
- q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
- tmp = #q
- loops = 1
- end
- 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
- w = getSeperated(q[loops]..",|")
- xe,ye,ze = w[1],w[2],w[3]
- a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
- if a and a ~= 6 then
- t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
- pathfind(t1,t2,t3,10)
- mapSidesType()
- end
- loops = loops + 1
- if loops > tmp then done = true end
- if mode and not done then
- exploreProgress(loops,tmp,3)
- end
- end
- exploreProgress(loops-1,tmp,4)
- if goback then pathfind(xs,ys,zs) end
- return done
- end
- function exploreSel(xq,yq,zq,xw,yw,zw,goback,mode)
- print("Warning: exploreSel() is in beta testing, expect an occasional bug.")
- mapSides()
- if goback == nil then goback = true end
- if mode == nil then mode = true end
- local xs,ys,zs = getPos()
- local q,w
- local xe,ye,ze
- local t1,t2,t3
- local a
- local done = false
- local loops = 1
- q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
- local tmp = #q
- if #q == 0 then exploreProgress(1,1,4) return true end
- for i=1,2,1 do
- while not done do
- w = getSeperated(q[loops]..",|")
- xe,ye,ze = w[1],w[2],w[3]
- a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
- if a and a ~= 6 then
- t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
- pathfind(t1,t2,t3,10)
- mapSides()
- q = getCubeSel(xq,yq,zq,xw,yw,zw,126,i)
- tmp = #q
- end
- loops = loops + 1
- if loops > tmp then done = true end
- if mode and not done then
- if i == 0 then
- exploreProgress(loops,tmp,i)
- else
- exploreProgress(loops,tmp,i)
- end
- end
- end
- done = false
- q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
- tmp = #q
- loops = 1
- end
- 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
- w = getSeperated(q[loops]..",|")
- xe,ye,ze = w[1],w[2],w[3]
- a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
- if a and a ~= 6 then
- t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
- pathfind(t1,t2,t3,10)
- mapSides()
- end
- loops = loops + 1
- if loops > tmp then done = true end
- if mode and not done then
- exploreProgress(loops,tmp,3)
- end
- end
- exploreProgress(loops-1,tmp,4)
- if goback then pathfind(xs,ys,zs) end
- return done
- end
- function pathfind(xe,ye,ze,tc,up,s,t)
- local ue
- if not tc then ue = false else ue = true end
- if not tc then tc = 10 else tc = math.abs(tc) end
- if not up then up = false end
- if not s then s = 0 end
- if not t then t = false else t = true end
- local xs,ys,zs = getPos()
- local waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
- local tmp,done,breaking
- local tries = 0
- local prev = waypoints
- local cooldown = 0
- while not done do
- tries = tries + 1
- tmp = 0
- for i=1,min(#prev,#waypoints),1 do
- if prev[i]:getPos() ~= waypoints[i]:getPos() then breaking = i end
- end
- if not breaking then breaking = -1 end
- if breaking > -1 then pathfind(waypoints[breaking - 1]:getPos()) end
- for k,v in pairs(waypoints) do
- cooldown = cooldown + 1 if cooldown >= 100 then cooldown = 0 sleep(0) end
- if (k >= breaking or breaking == -1) and not goToCoordsPath(v:getPos()) and k ~= 1 then
- tmp = -1
- done = false
- break
- end
- end
- prev = waypoints
- if tmp ~= -1 then
- goToCoordsPath(xe,ye,ze)
- done = true
- else
- if s < 1 then
- if t then mapSidesType() else mapSides() end
- if tries >= tc then tries = 0 xs,ys,zs = getPos() end
- waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
- else
- explore(s)
- if tries >= tc then tries = 0 xs,ys,zs = getPos() end
- waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
- end
- end
- end
- return true
- end
- function getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
- local returnpack = {}
- local done = false
- local xp,yp,zp = xs,ys,zs
- local penalty = 0
- if xs == xe and ys == ye and zs == ze then return {xs..","..ys..","..zs} end
- local open = {}
- nodes = {} nodeF = {}
- local closest = nil
- if s and getBlock(xe,ye,ze) == 126 then
- closest = getClosest(xe,ye,ze,5,air)
- if #closest ~= 0 then
- local w = getSeperated(closest[1]..",|")
- pathfind(w[1],w[2],w[3])
- end
- end
- local tmp,temp
- local cooldown = 0
- open[1] = Node:new(xs,ys,zs,xe,ye,ze,1,false,true)
- table.sort(nodeF)
- table.sort(open, function(a,b) return a.f<b.f end)
- while not done do
- for ii=0,5,1 do
- tmp = nil
- temp = false
- cooldown = cooldown + 1 if cooldown > 50 then sleep(0) cooldown = 0 end
- xs,ys,zs = open[1]:getPos()
- if (getBlockInDir(xs,ys,zs,ii) == 0 or (ue and getBlockInDir(xs,ys,zs,ii) == 126)) and temp ~= true then
- if getBlockInDir(xs,ys,zs,ii) == 126 then penalty = up end
- xs,ys,zs = getCoordsInDir(xs,ys,zs,ii)
- for iii=1,#open,1 do if open[iii]:getPosString() == xs..","..ys..","..zs then tmp = true temp = open[iii] end end
- if not tmp then
- open[#open+1] = Node:new(xs,ys,zs,xe,ye,ze,#nodes+1,open[1].ID,false,penalty)
- if open[#open]:getPosString() == xe..","..ye..","..ze then done = open[#open] end
- elseif open[1].g > temp.g + 10 then
- open[1].pid = temp.pid
- tmp = getSeperated(open[1].pid..",|")
- open[1].g = nodes[tmp[#tmp]].g + 10
- open[1].f = open[1].g + open[1].h
- nodeF[open[1].ID] = open[1].f
- table.sort(nodeF)
- table.sort(open, function(a,b) return a.f<b.f end)
- end
- end
- penalty = 0
- end
- 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
- nodeF[open[1].ID] = math.huge
- table.remove(open,1)
- table.sort(nodeF)
- table.sort(open, function(a,b) return a.f<b.f end)
- if #open == 0 then done = false end
- end
- table.sort(nodeF)
- table.sort(open, function(a,b) return a.f<b.f end)
- if done then
- tmp = getSeperated(done.pid.."|")
- for i=1,#tmp,1 do
- table.insert(returnpack,nodes[tmp[i]])
- end
- nodes = {} nodeF = {}
- return returnpack
- else nodes = {} nodeF = {} return {} end
- end
Add Comment
Please, Sign In to add comment