Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MultiMiner v4 by DrPepper
- -- made for tweaked.cc and WarpDrive Minecraft mods
- -- settings (peristent)
- local defaultSettings = {
- minerRefreshSeconds = 0.25,
- inactiveMinerRefreshSeconds = 5,
- silkTouch = false,
- onlyOres = true,
- laserOffset = 8,
- fontSize = 5, -- times 10
- redstoneOut = false
- }
- -- program state
- local isQuitting = false
- local startMining = false
- local stopMining = false
- local isActive = false
- local laserCnt = 0
- local laserStartingH = 0
- local laserSettingsChanged = true
- local energyUnits = nil
- local showPositions = false
- local lasers = {}
- local settings = {}
- local monitor = nil
- -- functions
- local BoolToRedGreen, FixOffset, IncFontSize, RedstoneOutput, AttachNewLaser
- local Init, MinerLoop, InputLoop, RedrawLoop, RefreshLoop, Shutdown, ReadSettings, WriteSettings
- local DrawAll, DrawHeader, DrawTable, DrawFooter
- function BoolToRedGreen(value)
- if value then
- return colors.green
- else
- return colors.red
- end
- end
- function FixOffset(laserOffsetInput)
- if laserOffsetInput > 255 then
- return 255
- elseif laserOffsetInput < 0 then
- return 0
- else
- return laserOffsetInput
- end
- end
- function IncFontSize(currentSize)
- if currentSize >= 50 or currentSize < 5 then
- return 5
- else
- return currentSize + 5
- end
- end
- function RedstoneOutput(emit)
- for _, side in pairs(redstone.getSides()) do
- rs.setOutput(side, emit)
- end
- end
- function ReadSettings()
- local file = fs.open("multiminer.cfg","r")
- if file ~= nil then
- settings = textutils.unserialize(file.readAll())
- end
- for k,v in pairs (defaultSettings) do
- if settings[k] == nil then
- settings[k] = defaultSettings[k]
- end
- end
- for k,v in pairs (settings) do
- print("Settings: " .. tostring(k) .. " : " .. tostring(v))
- end
- end
- function WriteSettings()
- local file = fs.open("multiminer.cfg","w")
- file.write(textutils.serialize(settings))
- file.close()
- end
- function AttachNewLaser(side)
- laserCnt = laserCnt + 1
- lasers[laserCnt] = {}
- lasers[laserCnt].peripheral = peripheral.wrap(side)
- lasers[laserCnt].side = side
- print("Wrapped warpdriveMiningLaser on " .. side .. " side")
- if energyUnits == nil then
- energyUnits = lasers[laserCnt].peripheral.energyDisplayUnits()
- end
- local lx, ly, lz = lasers[laserCnt].peripheral.getLocalPosition()
- lasers[laserCnt].x = lx
- lasers[laserCnt].y = ly
- lasers[laserCnt].z = lz
- end
- function Init()
- if not term.isColor() then
- print("Advanced computer required. Go find some gold.")
- end
- ReadSettings()
- WriteSettings()
- local sides = peripheral.getNames()
- for _, side in pairs(sides) do
- if peripheral.getType(side) == "warpdriveMiningLaser" then
- AttachNewLaser(side)
- elseif monitor == nil and peripheral.getType(side) == "monitor" then
- print("Wrapping monitor on " .. side .. " side")
- monitor = peripheral.wrap(side)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setTextScale(settings.fontSize / 10.0)
- monitor.setPaletteColor(colors.lightGray, 0.4, 0.4, 0.4)
- end
- end
- sleep(1)
- term.clear()
- term.setPaletteColor(colors.lightGray, 0.4, 0.4, 0.4)
- os.queueEvent("redraw")
- end
- function Shutdown()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setPaletteColor(colors.lightGray, term.nativePaletteColor(colors.lightGray))
- term.setCursorPos(1,1)
- RedstoneOutput(false)
- end
- function RefreshLoop()
- while not isQuitting do
- os.queueEvent("refreshMiners")
- sleep(settings.inactiveMinerRefreshSeconds)
- end
- end
- function MinerLoop()
- local timerId = nil
- while not isQuitting do
- local redrawNeeded = false
- local ev, p2 = os.pullEvent()
- if ev == "peripheral_detach" then
- for _,v in pairs (lasers) do
- if v.side == p2 then
- v.peripheral = nil
- v.status = "DETACHED"
- redrawNeeded = true
- end
- end
- end
- if ev == "peripheral" and peripheral.getType(p2) == "warpdriveMiningLaser" then
- local existingLaser = false
- for _,v in pairs (lasers) do
- if v.side == p2 then
- v.peripheral = peripheral.wrap(p2)
- existingLaser = true
- end
- end
- if not existingLaser then
- AttachNewLaser(p2)
- end
- redrawNeeded = true
- end
- if (ev == "timer" and p2 == timerId) or ev == "refreshMiners" then
- RedstoneOutput(settings.redstoneOut and (isActive or startMining))
- if not laserSettingsChanged then
- if startMining or isActive then
- timerId = os.startTimer(settings.minerRefreshSeconds)
- end
- end
- isActive = false
- for k,v in pairs (lasers) do
- if v.peripheral ~= nil then
- if laserSettingsChanged or startMining then
- v.peripheral.offset(settings.laserOffset)
- v.peripheral.onlyOres(settings.onlyOres)
- v.peripheral.silktouch(settings.silkTouch)
- end
- if startMining then
- v.peripheral.enable(true)
- redrawNeeded = true
- end
- if stopMining then
- v.peripheral.enable(false)
- redrawNeeded = true
- end
- local r1, r2, r3, r4, r5, r6 = v.peripheral.state()
- if v.status ~=r1 or
- v.isActive ~= r2 or
- v.energy ~= r3 or
- v.currentLayer ~= r4 or
- v.mined ~= r5 or
- v.total ~= r6 then
- v.status, v.isActive, v.energy, v.currentLayer, v.mined, v.total = r1, r2, r3, r4, r5, r6
- redrawNeeded = true
- end
- v.offset = v.peripheral.offset()
- isActive = isActive or r2
- end
- end
- if isActive then
- startMining = false
- else
- stopMining = false
- end
- laserSettingsChanged = false
- end
- if redrawNeeded then
- os.queueEvent("redraw")
- end
- end
- end
- function InputLoop()
- while not isQuitting do
- local event, p2, p3, p4, p5 = os.pullEvent("key")
- if keys.getName(p2) == "q" then
- isQuitting = true
- elseif keys.getName(p2) == "s" then
- stopMining = true
- os.queueEvent("refreshMiners")
- elseif keys.getName(p2) == "m" then
- startMining = true
- os.queueEvent("refreshMiners")
- elseif keys.getName(p2) == "o" then
- settings.onlyOres = not settings.onlyOres
- laserSettingsChanged = true
- os.queueEvent("refreshMiners")
- elseif keys.getName(p2) == "t" then
- settings.silkTouch = not settings.silkTouch
- laserSettingsChanged = true
- os.queueEvent("refreshMiners")
- elseif keys.getName(p2) == "down" then
- settings.laserOffset = FixOffset(settings.laserOffset + 1)
- laserSettingsChanged = true
- elseif keys.getName(p2) == "up" then
- settings.laserOffset = FixOffset(settings.laserOffset - 1)
- laserSettingsChanged = true
- elseif keys.getName(p2) == "right" then
- settings.laserOffset = FixOffset(settings.laserOffset - 10)
- laserSettingsChanged = true
- elseif keys.getName(p2) == "left" then
- settings.laserOffset = FixOffset(settings.laserOffset + 10)
- laserSettingsChanged = true
- elseif keys.getName(p2) == "p" then
- showPositions = not showPositions
- elseif keys.getName(p2) == "f" then
- if monitor ~= nil then
- settings.fontSize = IncFontSize(settings.fontSize)
- monitor.setTextScale(settings.fontSize / 10.0)
- end
- elseif keys.getName(p2) == "r" then
- settings.redstoneOut = not settings.redstoneOut
- end
- WriteSettings()
- os.queueEvent("redraw")
- end
- end
- function RedrawLoop()
- while not isQuitting do
- local event = os.pullEvent("redraw")
- DrawAll()
- end
- end
- function DrawAll()
- DrawHeader(term)
- DrawHeader(monitor)
- DrawTable(term)
- DrawTable(monitor)
- DrawFooter(term)
- end
- function DrawHeader(drawDevice)
- if drawDevice == nil then
- return
- end
- local maxX, maxY = drawDevice.getSize()
- local str = "MultiMiner v4"
- drawDevice.setCursorPos(math.ceil((maxX - string.len(str)) / 2 + 1), 1)
- drawDevice.setTextColor(colors.orange)
- drawDevice.setBackgroundColor(colors.black)
- drawDevice.clearLine()
- drawDevice.write(str)
- drawDevice.setTextColor(colors.black)
- drawDevice.setBackgroundColor(colors.orange)
- drawDevice.setCursorPos(1, 2)
- drawDevice.clearLine()
- laserStartingH = 0
- for k,v in pairs (lasers) do
- if v.y ~= nil then
- local newH = v.y - settings.laserOffset - 1
- if newH > laserStartingH then
- laserStartingH = newH
- end
- end
- end
- drawDevice.write("Starts at height: " .. laserStartingH)
- if (settings.redstoneOut) then
- if (isActive) then
- drawDevice.setTextColor(colors.red)
- else
- drawDevice.setTextColor(colors.black)
- end
- drawDevice.write(" R")
- end
- drawDevice.setCursorPos(maxX - 25, 2)
- drawDevice.setTextColor(BoolToRedGreen(settings.onlyOres))
- drawDevice.write("\7")
- drawDevice.setTextColor(colors.black)
- drawDevice.write(" Only ores")
- drawDevice.setCursorPos(maxX - 11, 2)
- drawDevice.setTextColor(BoolToRedGreen(settings.silkTouch))
- drawDevice.write("\7")
- drawDevice.setTextColor(colors.black)
- drawDevice.write(" Silk touch")
- if isQuitting then
- drawDevice.setCursorPos(1,1)
- drawDevice.setTextColor(colors.white)
- drawDevice.setBackgroundColor(colors.black)
- drawDevice.clearLine()
- drawDevice.write("Quitting...")
- end
- end
- --###################################################
- --# Status Energy Hei Min/Tot
- --99 o Status message 123456 EU 999 123/456
- function DrawTable(drawDevice)
- if drawDevice == nil then
- return
- end
- local maxX, maxY = drawDevice.getSize()
- drawDevice.setCursorPos(1, 3)
- drawDevice.setTextColor(colors.black)
- drawDevice.setBackgroundColor(colors.orange)
- drawDevice.clearLine()
- drawDevice.write("# Status")
- local xAlign = maxX - 21
- if xAlign < 5 then xAlign = 5 end
- drawDevice.setCursorPos(xAlign, 3)
- drawDevice.write(string.format(" %-9.9s %-3.3s %-3.3s/%-3.3s", "Energy", "Height", "Mined", "Total"))
- drawDevice.setCursorPos(1, 4)
- for k,v in pairs (lasers) do
- local cx, cy = drawDevice.getCursorPos()
- if cy % 2 == 1 then
- drawDevice.setBackgroundColor(colors.gray)
- else
- drawDevice.setBackgroundColor(colors.lightGray)
- end
- drawDevice.setTextColor(colors.white)
- drawDevice.clearLine()
- drawDevice.write(string.format("%2d", tonumber(k)))
- drawDevice.setTextColor(BoolToRedGreen(v.isActive))
- drawDevice.write(" \7 ")
- drawDevice.setTextColor(colors.white)
- local status = tostring(v.status)
- if showPositions then
- status = tostring(v.x) .. "; " .. tostring(v.y) .. "; " .. tostring(v.z)
- end
- drawDevice.write(status)
- drawDevice.setCursorPos(xAlign, cy)
- drawDevice.write(string.format(" %-6s %-2.2s %3s %3.3s/%-3.3s",
- tostring(v.energy),
- energyUnits,
- tostring(v.currentLayer),
- tostring(v.mined),
- tostring(v.total)))
- drawDevice.setCursorPos(1, cy + 1)
- end
- end
- function DrawFooter(drawDevice)
- if drawDevice == nil then
- return
- end
- local maxX, maxY = drawDevice.getSize()
- drawDevice.setTextColor(colors.white)
- drawDevice.setBackgroundColor(colors.gray)
- drawDevice.setCursorPos(1, maxY-2)
- drawDevice.clearLine()
- drawDevice.blit( "[P] Toggle position [F] Font size [R] Redstone",
- "01000000000000000000000100000000000000010000000000",
- "77777777777777777777777777777777777777777777777777" )
- drawDevice.setBackgroundColor(colors.gray)
- drawDevice.setCursorPos(1, maxY-1)
- drawDevice.clearLine()
- drawDevice.blit( "[O] Only ores [T] Silk touch [\24\25\26\27] Offset:",
- "01000000000000000100000000000000001111000000000",
- "77777777777777777777777777777777777777777777777" )
- drawDevice.setBackgroundColor(colors.black)
- drawDevice.write(string.format(" %3s", settings.laserOffset))
- drawDevice.setBackgroundColor(colors.gray)
- drawDevice.setCursorPos(1, maxY)
- drawDevice.clearLine()
- drawDevice.blit( "[M] Mine [S] Stop [Q] Exit",
- "010000000000100000000001000000",
- "777777777777777777777777777777" )
- end
- Init()
- parallel.waitForAny(MinerLoop, RedrawLoop, InputLoop, RefreshLoop)
- Shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement