Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function edit(tMap)
- tMap=tMap or {}
- tMap.newLayer=function(self)
- local wd,ht
- local run=false
- while not wd and not ht do
- term.clear()
- term.setCursorPos(1,1)
- if run then
- print("Incorrect input\n")
- end
- print("Enter the dimensions of this layer")
- write("x: ")
- wd=tonumber(read())
- write("y: ")
- ht=tonumber(read())
- run=true
- end
- local layer={}
- for y=1,ht do
- layer[y]=string.rep("1",wd)
- end
- self[#self+1]=layer
- self.selection=self.selection or {}
- self.selection.layer=#self
- if not self(self.selection.x,self.selection.y,self.selection.layer) then
- self.selection.x=1
- self.selection.y=1
- end
- end
- tMap.remLayer=function(self)
- term.clear()
- term.setCursorPos(1,1)
- print("Are you sure you want to remove this layer? [y|n]")
- while true do
- local evts={os.pullEvent()}
- if evts[1]=="key" and evts[2]==21 then
- table.remove(self,self.selection.layer)
- self.selection.layer=self.selection.layer-1
- if not self(self.selection.x,self.selection.y,self.selection.layer) then
- self.selection.x=1
- self.selection.y=1
- end
- break
- elseif evts[1]=="key" and evts[2]==49 then
- break
- end
- end
- end
- setmetatable(tMap,{__call=function(self,x,y,z)
- if self[z] and self[z][y] and x<=#self[z][y] then
- return string.sub(self[z][y],x,x)
- end
- end})
- if not tMap[1] then
- tMap:newLayer()
- end
- if not tMap.selection then
- tMap.selection={}
- tMap.selection.layer=tMap.selection.layer or 1
- tMap.selection.x=tMap.selection.x or 1
- tMap.selection.y=tMap.selection.y or 1
- end
- local function render()
- term.clear()
- term.setCursorPos(1,1)
- for y=1,#tMap[tMap.selection.layer] do
- print(tMap[tMap.selection.layer][y])
- end
- term.setCursorPos(tMap.selection.x,tMap.selection.y)
- write("X")
- local scp=term.setCursorPos
- term.setCursorPos=function(x,y)
- return scp(#tMap[tMap.selection.layer][1]+1+x,y)
- end
- term.setCursorPos(1,1)
- print("layer: "..tMap.selection.layer)
- print("["..tMap.selection.x..","..tMap.selection.y.."] = "..tMap(tMap.selection.x,tMap.selection.y,tMap.selection.layer))
- term.setCursorPos=scp
- end
- while true do
- render()
- local evts={os.pullEvent()}
- if evts[1]=="key" and evts[2]==200 and tMap.selection.y>1 then
- tMap.selection.y=tMap.selection.y-1
- elseif evts[1]=="key" and evts[2]==208 and tMap.selection.y<#tMap[tMap.selection.layer] then
- tMap.selection.y=tMap.selection.y+1
- elseif evts[1]=="key" and evts[2]==203 and tMap.selection.x>1 then
- tMap.selection.x=tMap.selection.x-1
- elseif evts[1]=="key" and evts[2]==205 and tMap.selection.x<#tMap[tMap.selection.layer][tMap.selection.y] then
- tMap.selection.x=tMap.selection.x+1
- elseif evts[1]=="key" and (evts[2]==11 or evts[2]==82) then
- local str=tMap[tMap.selection.layer][tMap.selection.y]
- tMap[tMap.selection.layer][tMap.selection.y]=string.sub(str,1,tMap.selection.x-1).."0"..string.sub(str,tMap.selection.x+1)
- elseif evts[1]=="key" and (evts[2]==2 or evts[2]==79) then
- local str=tMap[tMap.selection.layer][tMap.selection.y]
- tMap[tMap.selection.layer][tMap.selection.y]=string.sub(str,1,tMap.selection.x-1).."1"..string.sub(str,tMap.selection.x+1)
- elseif evts[1]=="key" and evts[2]==210 then
- tMap:newLayer()
- elseif evts[1]=="key" and evts[2]==211 then
- tMap:remLayer()
- elseif evts[1]=="key" and evts[2]==78 and tMap.selection.layer<#tMap then
- tMap.selection.layer=tMap.selection.layer+1
- if not tMap(tMap.selection.x,tMap.selection.y,tMap.selection.layer) then
- tMap.selection.x=1
- tMap.selection.y=1
- end
- elseif evts[1]=="key" and evts[2]==74 and tMap.selection.layer>1 then
- tMap.selection.layer=tMap.selection.layer-1
- if not tMap(tMap.selection.x,tMap.selection.y,tMap.selection.layer) then
- tMap.selection.x=1
- tMap.selection.y=1
- end
- elseif evts[1]=="key" and evts[2]==31 then
- return tMap
- end
- end
- end
- function path(map,targx,targy,targz)
- --[VARS]
- local tPath={x=1,y=1,z=1,""}
- local target={x=targx or 1,y=targy or 1,z=targz or 1}
- local displacement=setmetatable({},{__index=function(self,index) if tPath[index] and target[index] then return tPath[index]-target[index] end end,__newindex=function() return nil end})
- local checked=setmetatable({{x=1,y=1,z=1}},{__call=function(self,x,y,z) for k,v in pairs(self) do if v.x==x and v.y==y and v.z==z then return true end end return false end})
- local directions_mt={__index=function(self,index) if index=="x" or index=="y" or index=="z" then return 0 end end}
- local directions=setmetatable({{x=1,"r"},{x=-1,"l"},{y=1,"d"},{y=-1,"u"},{z=1,"f"},{z=-1,"b"}},{__call=function(self,direction)
- if direction then
- for k,v in pairs(self) do
- if v[1]==direction then
- return v
- end
- end
- local tDirs={}
- for k,v in pairs(self) do
- if rawget(v,direction) then
- table.insert(tDirs,v)
- end
- end
- return tDirs
- else
- local x
- local y
- local z
- for k,v in pairs(self) do
- if (displacement.x>0 and v.x<0) or (displacement.x<0 and v.x>0) then
- x=v
- elseif (displacement.y>0 and v.y<0) or (displacement.y<0 and v.y>0) then
- y=v
- elseif (displacement.z>0 and v.z<0) or (displacement.z<0 and v.z>0) then
- z=v
- end
- end
- return x,y,z
- end
- end})
- for k,v in pairs(directions) do setmetatable(v,directions_mt) end
- --[FUNCTIONS]
- local render=function()
- term.clear()
- for k,v in pairs(map[tPath.z]) do
- term.setCursorPos(1,k)
- print(({string.gsub(string.gsub(v,"0","."),"1","#")})[1])
- end
- print(tPath.x..", "..tPath.y..", "..tPath.z)
- term.setCursorPos(tPath.x,tPath.y)
- write("*")
- end
- local list=function(...)
- local tArgs={...}
- return coroutine.wrap(function()
- for i=1,#tArgs do
- coroutine.yield(tArgs[i])
- end
- end)
- end
- --[EXECUTION]
- while true do
- local x,y,z=directions()
- if x and map(tPath.x+x.x,tPath.y,tPath.z)=="0" and not checked(tPath.x+x.x,tPath.y,tPath.z) then
- tPath.x=tPath.x+x.x
- tPath[1]=tPath[1]..x[1]
- table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
- elseif y and map(tPath.x,tPath.y+y.y,tPath.z)=="0" and not checked(tPath.x,tPath.y+y.y,tPath.z) then
- tPath.y=tPath.y+y.y
- tPath[1]=tPath[1]..y[1]
- table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
- elseif z and map(tPath.x,tPath.y,tPath.z+z.z)=="0" and not checked(tPath.x,tPath.y,tPath.z+z.z) then
- tPath.z=tPath.z+z.z
- tPath[1]=tPath[1]..z[1]
- table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
- else
- local moved=false
- local axis,smallest
- for item in list("x","y","z") do
- if not smallest or displacement[item]*(displacement[item]<0 and -1 or 1)<smallest or (displacement[item]==smallest and math.random(1,2)==1) then
- axis=item
- smallest=displacement[item]*(displacement[item]<0 and -1 or 1)
- end
- end
- local dirs=directions(axis)
- for k,v in pairs(directions) do table.insert(dirs,v) end
- for k,v in ipairs(dirs) do
- if v~=x and v~=y and v~=z and map(tPath.x+v.x,tPath.y+v.y,tPath.z+v.z)=="0" and not checked(tPath.x+v.x,tPath.y+v.y,tPath.z+v.z) then
- moved=true
- tPath.x=tPath.x+v.x
- tPath.y=tPath.y+v.y
- tPath.z=tPath.z+v.z
- tPath[1]=tPath[1]..v[1]
- table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
- break
- end
- end
- if not moved then
- if tPath.x==1 and tPath.y==1 then
- return false
- end
- tPath.x=tPath.x-directions(string.sub(tPath[1],-1)).x
- tPath.y=tPath.y-directions(string.sub(tPath[1],-1)).y
- tPath.z=tPath.z-directions(string.sub(tPath[1],-1)).z
- tPath[1]=string.sub(tPath[1],1,-2)
- end
- end
- if tPath.x==target.x and tPath.y==target.y and tPath.z==target.z then
- return tPath[1]
- end
- --sleep(0.1)
- --render()
- end
- end
- function follow(str,dir)
- local res={}
- local dir=dir or "d"
- local tR={d="l",l="u",u="r",r="d"}
- for i=1,#str do
- local char=string.sub(str,i,i)
- if char=="f" then
- while not turtle.down() do end
- elseif char=="b" then
- while not turtle.up() do end
- elseif char==dir then
- while not turtle.forward() do end
- else
- while dir~=char do
- if turtle.turnRight() then
- dir=tR[dir]
- end
- end
- while not turtle.forward() do end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment