Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --computercraft
- --laser miner controller with monitor
- --all mining lasers are dealt with as peripherals
- --text scale
- --changing this will cause touch interface to fail for some reason --TODO
- MON_SCALE = 1.0
- --min offset
- MIN_OFFSET = 2
- --max offset
- MAX_OFFSET = 250
- function DetectPeripheral(name)
- local sides = {"top" , "bottom" , "front" , "left" , "right" , "back"}
- for i = 1, 6 do
- if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
- print("found "..tostring(name)..": "..tostring(sides[i]))
- return sides[i]
- end
- end
- return nil
- end
- screen = peripheral.wrap(DetectPeripheral("monitor"))
- screen_width, screen_height = screen.getSize()
- lazors = {}
- states = {}
- step = 12
- xOffset = 2
- yOffset = 4
- offset = MIN_OFFSET
- start_all_position = {x = 1, y = screen_height}
- stop_all_position = {x = screen_width - 7, y = screen_height}
- increment_position = {x = math.floor(screen_width/2 + 4), y = screen_height}
- decrement_position = {x = math.floor(screen_width/2 - 4), y = screen_height}
- screen.setTextScale(MON_SCALE)
- -- math floor, KOSTYL against lua coordinats rounding system
- function resetColorsScreen()
- screen.setBackgroundColor(colors.black)
- screen.setTextColor(colors.white)
- end
- function increment_offset()
- if offset <= MAX_OFFSET then
- offset = offset + 1
- end
- end
- function decrement_offset()
- if offset > MIN_OFFSET then
- offset = offset - 1
- end
- end
- function start(laserNUM)
- print("Calling laser "..lazors[laserNUM].." for start")
- setOffset(laserNUM, offset)
- m.callRemote(lazors[laserNUM], "mine")
- end
- function startAll()
- for i =1 ,#lazors do
- start(i)
- end
- end
- function stop(laserNUM)
- print("Calling laser "..lazors[laserNUM].." for stop")
- m.callRemote(lazors[laserNUM], "stop")
- end
- function stopAll()
- for i = 1 ,#lazors do
- stop(i)
- end
- end
- function setOffset(laserNUM, offset)
- m.callRemote(lazors[laserNUM], "offset", offset)
- end
- function drawstatus()
- screen.clear()
- ----drawing static elements
- --drawing list of catgories
- local categories = {"State", "Control", "Energy", "Layer", "Val/Min"}
- for i=1,5 do
- screen.setCursorPos(-10 + step*i, 2)
- screen.write(categories[i])
- end
- --drawing static buttons
- screen.setBackgroundColor(colors.cyan)
- screen.setCursorPos(start_all_position["x"] ,start_all_position["y"])
- screen.write("Start All")
- screen.setCursorPos(stop_all_position["x"] , stop_all_position["y"])
- screen.write("Stop All")
- resetColorsScreen()
- screen.setBackgroundColor(colors.green)
- screen.setCursorPos(increment_position["x"], increment_position["y"])
- screen.write("+")
- screen.setCursorPos(math.floor(screen_width/2) - 1, screen_height)
- resetColorsScreen()
- screen.write(" "..offset.." ")
- screen.setBackgroundColor(colors.red)
- screen.setCursorPos(decrement_position.x, decrement_position.y)
- screen.write("-")
- resetColorsScreen()
- ----drawing dynamic elements
- --iterating throught miner array
- for a = 1,#lazors do
- state, energy, height, luxuries, mined = m.callRemote(lazors[a], "state")
- -- mState, mEnergy, mLayer, mValuablesMined, mValuablesInLayer = m.callRemote(lasersT[i], "state")
- if state == "not mining" then
- states[a] = "Start"
- else
- states[a] = "Stop"
- end
- local temp = {state, states[a], energy, height, tostring(luxuries.."/"..mined)}
- --printing for current miner on monitor
- for b = 1, 5 do
- screen.setCursorPos(-10 + step * b, a + yOffset)
- --color for on/off status
- if b == 1 then
- if temp[b] == "not mining" then
- screen.setBackgroundColor(colors.red)
- screen.write(temp[b])
- resetColorsScreen()
- else
- screen.setBackgroundColor(colors.green)
- screen.write(temp[b])
- resetColorsScreen()
- end
- elseif b == 2 then
- screen.setBackgroundColor(colors.cyan)
- screen.write(temp[b])
- resetColorsScreen()
- else
- screen.write(temp[b])
- end
- end
- end
- sleep(1)
- return 0
- end
- --controll from player
- function touch_control()
- --print("Enter Touch service")
- local laserIndex = nil
- local xMax = xOffset + step * 2
- local yMax = yOffset + table.maxn(lazors)
- --pull touch event
- local event, side, xPos, yPos = os.pullEvent("monitor_touch")
- --miners controll
- print("Pulled touch event on X: ".. xPos.." Y: "..yPos)
- print(decrement_position.x.." "..decrement_position.y)
- if xPos >= xOffset and xPos <= xMax and yPos >= yOffset and yPos <= yMax then
- print("Button hitted, executing code")
- laserIndex = yPos - yOffset
- if states[laserIndex] == "Start" then
- print("Starting the miner with index"..laserIndex)
- start(laserIndex)
- else
- print("Stopping the miner with index"..laserIndex)
- stop(laserIndex)
- end
- elseif xPos <= 10 and yPos == screen_height then
- startAll()
- elseif xPos >= stop_all_position.x and yPos == stop_all_position.y then
- stopAll()
- elseif xPos == increment_position.x and yPos == increment_position.y then
- increment_offset()
- elseif xPos == decrement_position.x and yPos == decrement_position.y then
- decrement_offset()
- print("Function ended, exiting")
- return 0
- end
- end
- --execution start
- --KOSTYL for not detectiong any lasers after PRYGEE jump
- sleep(1)
- m = peripheral.wrap(DetectPeripheral("modem"))
- clients = m.getNamesRemote()
- --filter lasers from other peripherals
- for i = 1, table.maxn(clients) do
- if m.getTypeRemote(clients[i]) then
- table.insert(lazors, clients[i])
- end
- end
- --mainloop
- while true do
- parallel.waitForAny(drawstatus, touch_control)
- end
Advertisement
Add Comment
Please, Sign In to add comment