Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --endstop positions
- xEndstop, yEndstop = ...
- local w,h term.getSize()
- local fineBool = false
- local xInv = false
- local yInv = false
- --how big the platform is
- platformSize = 8
- --side definitions
- IO = "front"
- reverse = "left"
- toggleY = "back"
- toggleH = "right"
- rsIn = "bottom"
- --time required to move 1 block horozontally
- stepTime = 0.45
- --*************Functions*************
- --initializes mining platform connection
- function initPlatform()
- rsRoute(toggleY, "on")
- rsRoute(toggleH, "on")
- rsRoute(IO, "on")
- sleep(2)
- rsRoute(reverse, "on")
- sleep(4)
- clearRS()
- end
- --finds endstops
- function findExtent()
- back()
- right()
- fine()
- --find x endstop
- while rs.getAnalogInput(rsIn) ~= 3 do
- left()
- xEndstop = xEndstop + 1
- sleep(0.15)
- end
- for x=0,(xEndstop) do
- right()
- sleep(0.15)
- end
- --find y endstop
- while rs.getAnalogInput(rsIn) ~= 4 do
- forward()
- yEndstop = yEndstop + 1
- sleep(0.15)
- end
- for x=0,(yEndstop) do
- back()
- sleep(0.15)
- end
- fine()
- --debug
- --print(xEndstop)
- --print(yEndstop)
- end
- --sets colors for text and BG
- function setTheme(FG, BG)
- term.setBackgroundColor(BG)
- term.setTextColor(FG)
- end
- --sets redstone off
- function clearRS()
- rs.setAnalogOutput(IO,0)
- rs.setAnalogOutput(toggleH,0)
- rs.setAnalogOutput(toggleY,0)
- rs.setAnalogOutput(reverse,0)
- rs.setAnalogOutput(rsIn,0)
- end
- --draw functions
- function drawBtn(text, width, height, xpos, ypos)
- --positions are for the top left of the btn
- local linesAbove = math.floor((height-1)/2)
- local linesBelow = math.ceil((height-1)/2)
- local spaceLeft = math.floor((width-#text)/2)
- local spaceRight = math.ceil((width-#text)/2)
- --draw lines above text
- for line=0,linesAbove do
- term.setCursorPos(xpos,ypos+line)
- for character=1,width do
- write(" ")
- end
- end
- --draw lines below text
- for line=0,linesBelow do
- term.setCursorPos(xpos, ypos+linesAbove+line)
- for character=1,width do
- write(" ")
- end
- end
- --draw text line
- term.setCursorPos(xpos, ypos+linesAbove)
- for spaces=1,spaceLeft do
- write(" " )
- end
- write(text)
- for spaces=1,spaceRight do
- write(" ")
- end
- end
- --outputs the redstone signals
- function rsRoute(freq, command)
- if command=="on" then
- rs.setAnalogOutput(freq, 15)
- end
- if command=="off" then
- rs.setAnalogOutput(freq, 0)
- end
- if command=="pulse" then
- rs.setAnalogOutput(freq,15)
- sleep(0.1)
- rs.setAnalogOutput(freq, 0)
- end
- end
- --functions to translate commands into redstone
- function moveX(distance)
- rsRoute(toggleY, "off")
- rsRoute(toggleH, "off")
- if xInv then distance = -1*distance end
- if distance>0 then
- rsRoute(reverse, "off")
- elseif distance<0 then
- rsRoute(reverse, "on")
- distance = -1 * distance
- else
- sleep(0.1)
- end
- sleep(0.1)
- rsRoute(IO, "on")
- sleep(stepTime*distance)
- rsRoute(IO, "off")
- end
- function moveY(distance)
- rsRoute(toggleY, "on")
- rsRoute(toggleH, "off")
- if yInv then distance = -1*distance end
- if distance>0 then
- rsRoute(reverse, "off")
- elseif distance<0 then
- rsRoute(reverse, "on")
- distance = -1 * distance
- else
- sleep(0.1)
- end
- sleep(0.1)
- rsRoute(IO, "on")
- sleep(stepTime*distance)
- rsRoute(IO, "off")
- end
- function digToBR()
- clearRS()
- rsRoute(toggleY, "on")
- rsRoute(toggleH, "on")
- local atBottom = false
- local atTop = false
- rsRoute(IO,"on")
- sleep(2)
- while not atBottom do
- os.pullEvent("redstone")
- if rs.getAnalogInput(rsIn) == 1 then
- rsRoute(reverse, "on")
- atBottom = true
- elseif rs.getAnalogInput(rsIn) > 1 then
- --whatever else we need to do later
- else
- sleep(0.1)
- end
- end
- while not atTop do
- os.pullEvent("redstone")
- if rs.getAnalogInput(rsIn) == 2 then
- rsRoute(IO, "off")
- atTop = true
- elseif rs.getAnalogInput(rsIn) ~= 2 then
- --other stuff later maybe
- else
- sleep(0.1)
- end
- end
- end
- --functinos for the buttons
- function forward()
- --print("forward")
- if fineBool then moveY(-1) end
- if not fineBool then moveY(-4) end
- end
- function back()
- --print("back")
- if fineBool then moveY(1) end
- if not fineBool then moveY(4) end
- end
- function left()
- --print("left")
- if fineBool then moveX(1) end
- if not fineBool then moveX(4) end
- end
- function right()
- --print("right")
- if fineBool then moveX(-1) end
- if not fineBool then moveX(-4) end
- end
- function fine()
- fineBool = not fineBool
- if fineBool then
- setTheme(colors.black, colors.green)
- else
- setTheme(colors.black, colors.red)
- end
- drawBtn("FINE",6,3,21,4)
- end
- function invertX()
- xInv = not xInv
- if xInv then
- setTheme(colors.black, colors.green)
- else
- setTheme(colors.black, colors.red)
- end
- drawBtn("-X",3,3,4,14)
- end
- function invertY()
- yInv = not yInv
- if yInv then
- setTheme(colors.black, colors.green)
- else
- setTheme(colors.black, colors.red)
- end
- drawBtn("-Y",3,3,14,14)
- end
- function toBR()
- digToBR()
- end
- function mine()
- --init variables
- local xSteps = math.floor(xEndstop/platformSize)
- local ySteps = math.floor(yEndstop/platformSize)
- local backwards = false
- --turns off fine control
- if fineBool then fine() end
- --main loop
- for x=1,xSteps do
- for y=1,ySteps do
- digToBR()
- sleep(0.15)
- if backwards then
- for s=1,(platformSize/4) do back() sleep(0.15) end
- else
- for s=1,(platformSize/4) do forward() sleep(0.15) end
- end
- end
- sleep(0.15)
- digToBR()
- sleep(0.15)
- for s=1,(platformSize/4) do left() sleep(0.15) end
- backwards = not backwards
- sleep(10)
- end
- end
- --************MAIN*****************--
- --clear Term and redstone
- term.setBackgroundColor(colors.black)
- term.clear()
- clearRS()
- --draw momentary buttons
- setTheme(colors.black, colors.white)
- drawBtn("^",3,3,9,4)
- drawBtn("<",3,3,4,9)
- drawBtn(">",3,3,14,9)
- drawBtn("v",3,3,9,14)
- setTheme(colors.white, colors.black)
- --draw toggle buttons
- setTheme(colors.black, colors.red)
- drawBtn("FINE",6,3,21,4)
- drawBtn("-X",3,3,4,14)
- drawBtn("-Y",3,3,14,14)
- setTheme(colors.black, colors.green)
- drawBtn("2 BR",6,3,21,9)
- drawBtn("AREA",6,3,21,14)
- setTheme(colors.white, colors.black)
- --sets cursor at 1,1 for debug
- term.setCursorPos(1,1)
- --initialize
- initPlatform()
- if (xEndstop==yEndstop) and (xEndstop==0) then
- findExtent()
- end
- while true do
- local _,_,mX,mY = os.pullEvent("mouse_click")
- local x = 0
- local y = 0
- if (4 <= mX) and (mX <= 6) then
- x = 1
- elseif (9 <= mX) and (mX <= 11) then
- x = 2
- elseif (14 <= mX) and (mX <= 20) then
- x = 3
- elseif (21 <= mX) and (mX <= 26) then
- x = 4
- else
- x = 0
- end
- if (4 <= mY) and (mY <= 6) then
- y = 1
- elseif (9 <= mY) and (mY <= 11) then
- y = 2
- elseif (14 <= mY) and (mY <= 16) then
- y = 3
- else
- y = 0
- end
- if (x==1) and (y==2) then left() end
- if (x==1) and (y==3) then invertX() end
- if (x==3) and (y==3) then invertY() end
- if (x==2) and (y==1) then forward() end
- if (x==2) and (y==3) then back() end
- if (x==3) and (y==2) then right() end
- if (x==4) and (y==1) then fine() end
- if (x==4) and (y==2) then toBR() end
- if (x==4) and (y==3) then mine() end
- end
Add Comment
Please, Sign In to add comment