Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem1 = 6565
- modem2 = 6566
- modem3 = 6567
- modem4 = 6568
- local gatherChannel = 52523 --This channel will be *heavily* spammed; so be cautious of which channel you use...
- local fuelLevelGreenMin = 50000
- local fuelLevelYellowMin = 15000
- local modems = { peripheral.find("modem") }
- if #modems == 0 then error("No modems found") end
- local modem = modems[1]
- local map = {}
- local screen = "map"
- local viewTarget = ""
- local requestTarget = ""
- local requestCount = 1
- local maxRequestCount = 100
- local mapButtons = {}
- local buttons = {}
- local w,h = term.getSize()
- local offsetX = 0 --Offsets used to automatically offset the picture; making it fit on the screen.
- local offsetZ = 0
- local requestButtons = {}
- local updatesEnabled = true
- function updateVisuals()
- if not updatesEnabled then return end
- if screen == "map" then
- local totalX = 0
- local totalZ = 0
- local count = 0
- for i,v in pairs(map) do
- if v.location ~= nil then
- totalX = totalX + v.location.x
- totalZ = totalZ + v.location.z
- count = count + 1
- end
- end
- local midpointX = math.floor(totalX/count)
- local midpointZ = math.floor(totalZ/count)
- local topLeftX = math.floor(midpointX - (w/2))
- local topLeftZ = math.floor(midpointZ - (h/2))
- offsetX = 0
- offsetY = 0
- for i,v in pairs(map) do
- local x = v.location.x-topLeftX
- local z = v.location.z-topLeftZ
- if x < 1 then
- offsetX = offsetX + math.abs(x) + 1
- end
- if z < 1 then
- offsetY = offsetY + math.abs(z) + 1
- end
- end
- term.setBackgroundColor(colors.white)
- term.clear()
- mapButtons = {}
- for i,v in pairs(map) do
- if v.location ~= nil then
- local x = v.location.x-topLeftX+offsetX
- local z = v.location.z-topLeftZ+offsetY
- if mapButtons[x] == nil then mapButtons[x] = {} end
- mapButtons[x][z] = i
- term.setCursorPos(x,z)
- term.setTextColor(colors.black)
- if v.fuel ~= nil and v.fuel >= fuelLevelGreenMin then
- term.setBackgroundColor(colors.green)
- elseif v.fuel ~= nil and v.fuel >= fuelLevelYellowMin then
- term.setBackgroundColor(colors.yellow)
- elseif v.fuel ~= nil then
- term.setBackgroundColor(colors.red)
- else
- term.setBackgroundColor(colors.white)
- end
- if v.age ~= nil and v.age >= 5 then
- term.write("?")
- elseif v.type == "hopper" then
- term.write("H")
- elseif v.type == "stocker" then
- term.write("S")
- elseif v.type == "row" then
- term.write("R")
- end
- end
- end
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.write("Request")
- elseif screen == "view" then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.write("Back")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- local y = 1
- for i,v in pairs(map[viewTarget]) do
- term.setCursorPos(1,y+2)
- if type(v) == "string" or type(v) == "number" then
- term.write(i:sub(1,1):upper() .. i:sub(2):lower() .. ": " .. v)
- y = y + 1
- end
- end
- if map[viewTarget].location ~= nil then
- local loc = map[viewTarget].location
- term.setCursorPos(1,y+4)
- term.write("Location:")
- term.setCursorPos(2,y+5)
- term.write("X: "..loc.x)
- term.setCursorPos(2,y+6)
- term.write("Y: "..loc.y)
- term.setCursorPos(2,y+7)
- term.write("Z: "..loc.z)
- end
- if map[viewTarget].inventory ~= nil then
- local leftCornerX = w-21
- local leftCornerY = h-15
- term.setCursorPos(leftCornerX+2,leftCornerY-1)
- term.write("Inventory")
- local inv = map[viewTarget].inventory
- --probably a way more efficient way to do this but... y'know...
- term.setCursorPos(leftCornerX,leftCornerY)
- term.setTextColor(colors.lightGray)
- term.write("+--+--+--+--+")
- for dif = 0,3,1 do
- term.setCursorPos(leftCornerX,leftCornerY+1+(dif*2))
- term.setTextColor(colors.lightGray)
- term.write("|")
- for xslot = 0,3,1 do
- local item = inv[(dif*4)+xslot+1]
- term.setTextColor(colors.black)
- if item ~= nil and item.count ~= nil then
- if item.count > 9 then
- term.write(""..math.floor(item.count))
- else
- term.write(" "..math.floor(item.count))
- end
- else
- term.write(" ")
- end
- term.setTextColor(colors.lightGray)
- term.write("|")
- end
- term.setCursorPos(leftCornerX,leftCornerY+2+(dif*2))
- term.setTextColor(colors.lightGray)
- term.write("+--+--+--+--+")
- end
- end
- elseif screen == "requestMenu" then
- requestButtons = {}
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.green)
- term.write("Map")
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- local align = "left"
- local ny = 4
- for i,v in pairs(map)do
- if v.type == "row" then
- if align == "left" then
- term.setCursorPos(5,ny)
- term.write(" "..v.item:sub(11,11):upper()..v.item:sub(12):gsub("_"," ").." ")
- term.setCursorPos(5,ny+1)
- local countStr = ""..v.count
- local totl = #v.item-10
- local btn = {}
- btn.minX = 5
- btn.minY = ny
- btn.maxX = #v.item-1
- btn.maxY = ny+1
- btn.item = v.item
- table.insert(requestButtons,btn)
- term.write(string.rep(" ",math.floor(((totl-#countStr)/2)+2))..countStr..string.rep(" ",math.ceil(((totl-#countStr)/2)+2)))
- align = "right"
- elseif align == "right" then
- term.setCursorPos(w-5-#v.item+10,ny) -- w-(offset)-(length of item name)+(length of minecraft:)
- term.write(" "..v.item:sub(11,11):upper()..v.item:sub(12):gsub("_"," ").." ")
- term.setCursorPos(w-5-#v.item+10,ny+1)
- local countStr = ""..v.count
- local totl = #v.item-10
- local btn = {}
- btn.minX = w-5-#v.item+10
- btn.minY = ny
- btn.maxX = w-3
- btn.maxY = ny+1
- btn.item = v.item
- table.insert(requestButtons,btn)
- term.write(string.rep(" ",math.floor(((totl-#countStr)/2)+2))..countStr..string.rep(" ",math.ceil(((totl-#countStr)/2)+2)))
- align = "left"
- ny = ny + 3
- end
- end
- end
- elseif screen == "request" then
- local item = nil
- for i,v in pairs(map) do
- if v.item == requestTarget then
- item = v
- break
- end
- end
- if item == nil then
- term.clear()
- term.setCursorPos(1,1)
- term.write("Failure")
- updatesEnabled = false
- sleep(3)
- updatesEnabled = true
- screen="map"
- end
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.write("Back")
- local beautyname = item.item:sub(11,11):upper()..item.item:sub(12):gsub("_"," ")
- local amount = "Maximum: "..item.count
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(math.floor((w/2)-(#beautyname/2)),3)
- term.write(beautyname)
- term.setCursorPos(math.floor((w/2)-(#amount/2)),4)
- term.write(amount)
- maxRequestCount = item.count
- local cnt = requestCount..""
- term.setCursorPos(math.floor(w/2)-10,6)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write("-")
- term.setCursorPos(math.floor((w/2)-(#cnt/2)),6)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write(cnt)
- term.setCursorPos(math.floor(w/2)+10,6)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write("+")
- term.setCursorPos(math.floor(w/2)-11,8)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write("-16")
- term.setCursorPos(math.floor(w/2)+9,8)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write("+16")
- term.setCursorPos(math.floor((w/2)-3),10)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write("Request")
- end
- end
- function buttonListener()
- while true do
- local e,side,x,y = os.pullEvent("monitor_touch")
- if screen == "map" then
- if x >= 1 and x <= 7 and y == 1 then
- screen = "requestMenu"
- updateVisuals()
- else
- if mapButtons[x] ~= nil and mapButtons[x][y] ~= nil and map[mapButtons[x][y]] ~= nil then
- viewTarget = mapButtons[x][y]
- screen = "view"
- updateVisuals()
- end
- end
- elseif screen == "view" then
- if x >= 1 and x <= 4 and y == 1 then
- screen = "map"
- viewTarget = ""
- updateVisuals()
- end
- elseif screen == "requestMenu" then
- if x >= 1 and x <= 3 and y == 1 then
- screen = "map"
- updateVisuals()
- else
- for i,v in pairs(requestButtons)do
- if v.minX <= x and v.maxX >= x and v.minY <= y and v.maxY >= y then
- requestTarget = v.item
- screen = "request"
- updateVisuals()
- break
- end
- end
- end
- elseif screen == "request" then
- if x >= 1 and x <= 4 and y == 1 then
- screen = "requestMenu"
- updateVisuals()
- elseif x == math.floor(w/2)-10 and y == 6 then
- if requestCount > 1 then
- requestCount = requestCount - 1
- updateVisuals()
- end
- elseif x == math.floor(w/2)+10 and y == 6 then
- if requestCount < maxRequestCount then
- requestCount = requestCount + 1
- updateVisuals()
- end
- elseif x >= math.floor(w/2)-11 and x <= math.floor(w/2)-9 and y == 8 then
- if requestCount-16 > 0 then
- requestCount = requestCount - 16
- updateVisuals()
- else
- requestCount = 1
- updateVisuals()
- end
- elseif x >= math.floor(w/2)+9 and x <= math.floor(w/2)+11 and y == 8 then
- if requestCount+16 <= maxRequestCount then
- requestCount = requestCount + 16
- updateVisuals()
- else
- requestCount = maxRequestCount
- updateVisuals()
- end
- elseif x >= math.floor((w/2)-3) and x <= math.floor((w/2)+3) and y == 10 then
- updatesEnabled = false
- os.startTimer(2)
- modem.open(gatherChannel+1)
- modem.transmit(modem3,gatherChannel+1,{name=requestTarget,count=requestCount})
- while true do
- local e,side,chnl,rChnl,msg,dist = os.pullEvent()
- if e == "modem_message" and chnl == gatherChannel+1 and type(msg) == "table" and type(msg.type) == "string" and msg.type == "requestResponse" then
- if type(msg.success) == "boolean" and msg.success then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.clear()
- local response = "Success!"
- term.setCursorPos(math.floor((w/2)-(#response/2)),4)
- term.write(response)
- screen = "map"
- updateVisuals()
- modem.close(gatherChannel+1)
- break
- else
- if type(msg.error) == "string" then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.clear()
- local response = "Failure: "..msg.error
- term.setCursorPos(math.floor((w/2)-(#response/2)),4)
- term.write(response)
- screen = "map"
- updateVisuals()
- modem.close(gatherChannel+1)
- break
- else
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.clear()
- local response = "Failure: Unknown"
- term.setCursorPos(math.floor((w/2)-(#response/2)),4)
- term.write(response)
- screen = "map"
- updateVisuals()
- modem.close(gatherChannel+1)
- break
- end
- end
- elseif e == "timer" then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.clear()
- local response = "Failure: Timed out"
- term.setCursorPos(math.floor((w/2)-(#response/2)),4)
- term.write(response)
- screen = "map"
- updateVisuals()
- modem.close(gatherChannel+1)
- break
- end
- end
- updatesEnabled = true
- end
- end
- end
- end
- function updateData()
- while true do
- for i,v in pairs(map) do
- if v.age == nil then
- v.age = 1
- else
- v.age = v.age + 1
- end
- end
- modem.transmit(modem1,gatherChannel,"")
- sleep(.5)
- updateVisuals()
- sleep(1.5)
- end
- end
- function gatherData()
- while true do
- local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
- if chnl == gatherChannel and type(msg) == "table" and type(msg.type) == "string" then
- for i,v in pairs(msg) do
- if type(v) == "boolean" then
- if v then
- msg[i] = "true"
- else
- msg[i] = "false"
- end
- end
- end
- msg.distance = dist
- msg.age = 0
- if msg.type == "infoHopperResponse" and msg.channel ~= nil then
- msg.type = "hopper"
- map["hopperChnl"..msg.channel] = msg
- elseif msg.type == "infoStockerResponse" then
- msg.type = "stocker"
- map["stocker"] = msg
- elseif msg.type == "infoRowResponse" and msg.item ~= nil then
- msg.type = "row"
- map["rowItem"..msg.item] = msg
- end
- end
- end
- end
- modem.open(gatherChannel)
- parallel.waitForAll(updateData,gatherData,buttonListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement