Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local jarMaxAmount = 64
- local wifi = peripheral.wrap("bottom")
- if not wifi then wifi = peripheral.wrap("back") end
- local channel = {45,46,47}
- --if #args > 0 then channel = tonumber(args[1]) end
- for i=1,3 do
- channel[i] = #args >= i and tonumber(args[i]) or channel[i]
- print("Opening wifi channel ",channel[i])
- wifi.open(channel[i])
- end
- print("Press and hold CTRL R to reboot if GUI doesn't show")
- --wifi.transmit(channel,channel,"hello turtle")
- local function oldprintRecipe(recipe)
- print(math.floor(recipe[1])," ",recipe[14])
- for i=2,13 do
- term.write(math.floor(recipe[i]).." ")
- if (i-1) % 4 == 0 then print("") end
- end
- end
- local function printRecipe(recipe)
- print(math.floor(recipe[1][1]),":",math.floor(recipe[1][2]) or 0," ",recipe[14][1])
- for i=2,13 do
- term.write(math.floor(recipe[i][1])..":"..(recipe[i][2] and math.floor(recipe[i][2]) or 0).." ")
- if (i-1) % 4 == 0 then print("") end
- end
- --print("")
- end
- local function printAllRecipes(recipes)
- local counter = 0
- for _,recipe in pairs(recipes) do
- --if counter > 0 and counter % 4 == 0 then print("press a key to continue") local x,y=term.getCursorPos() os.pullEvent("key") term.setCursorPos(x,y) print(" ") term.setCursorPos(x,y) end
- if counter > 0 and counter % 4 == 0 then print("press a key to continue") local _,key = os.pullEvent("key") end
- printRecipe(recipe)
- counter = counter + 1
- end
- term.clearLine()
- end
- -- http://lua-users.org/wiki/SplitJoin
- local function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local jars = {}
- local sumJars = {}
- local function callJarHead(num)
- wifi.transmit(channel[num],channel[num],"")
- local _,_,_,_,message,_ = os.pullEvent("modem_message")
- return message
- end
- local function getJars()
- sumJars=callJarHead(2)
- for essentia,amount in pairs(callJarHead(3)) do
- sumJars[essentia] = sumJars[essentia] and sumJars[essentia]+amount or amount
- end
- jars[1]=callJarHead(2)
- jars[2]=callJarHead(3)
- end
- local function printMissingEssentia()
- for essentia,amount in pairs(sumJars) do
- if amount < jarMaxAmount * 2 then print(essentia," ",amount) end
- end
- end
- if #args > 3 then
- local input = nil
- repeat
- input = read()
- if input ~= "bye" and input ~= "exit" then
- local command = split(input,"|")
- if command[1] == "getJars" then getJars() printMissingEssentia()
- else
- wifi.transmit(channel[1],channel[1],{["id"]=command[1],["info"]=command[2]})
- if command[1] ~= "reboot" then
- local _,_,_,_,message,_ = os.pullEvent("modem_message")
- if message.id == "altar" then printRecipe(message.result)
- elseif message.id == "recipes" then printAllRecipes(message.result)
- else print(message.id," ",message.result) end
- end
- end
- end
- until input == "bye" or input == "exit"
- else
- -- GUI
- local function padStr(str, len)
- if #str > len then do return string.sub(str,1,len) end
- else do return str .. string.rep(' ', len - #str) end
- end
- end
- local termX, termY = term.getSize()
- local wind = window.create(term.current(), 1, 1, termX, termY, false)
- local windPosX, windPosY = wind.getPosition()
- local bgColor = colors.white
- local fgColor = colors.blue
- local altFgColor = colors.cyan
- -- empty, > 10, > 32, > 50, full
- local jarAlert = {0,10,32,50,64}
- local jarColor = {colors.red, colors.orange, colors.yellow, colors.lime, colors.green}
- local button = {}
- local function colorJars(amount)
- local color
- if amount == jarAlert[5] then color = jarColor[5]
- elseif amount > jarAlert[4] then color = jarColor[4]
- elseif amount > jarAlert[3] then color = jarColor[3]
- elseif amount > jarAlert[2] then color = jarColor[2]
- else color = jarColor[1]
- end
- return color
- end
- local jarWind = window.create(wind,1,4,termX,2,false)
- local jarWindPosX, jarWindPosY = jarWind.getPosition()
- jarWindPosX = jarWindPosX + windPosX - 1
- jarWindPosY = jarWindPosY + windPosY - 1
- local function drawJars()
- jarWind.setCursorPos(1,1)
- local counter = 1
- for name,amount in pairs(jars[1]) do
- if counter > termX then break end
- jarWind.setBackgroundColor(colorJars(amount))
- jarWind.write(" ")
- counter = counter + 1
- end
- jarWind.setCursorPos(1,2)
- counter = 1
- for name,amount in pairs(jars[2]) do
- if counter > termX then break end
- jarWind.setBackgroundColor(colorJars(amount))
- jarWind.write(" ")
- counter = counter + 1
- end
- wind.setCursorPos(1,6)
- end
- local function redrawJars()
- jarWind.setVisible(false)
- getJars()
- drawJars()
- jarWind.setVisible(true)
- --wind.setCursorPos(1,7)
- --wind.write("refreshed")
- end
- altarWind = window.create(wind,1,7,termX,6,false)
- local function getAltar()
- wifi.transmit(channel[1],channel[1],{id="altar",info=""})
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- return message.result
- end
- local function drawRecipe(w,recipe)
- if #recipe < 14 then return false end
- w.setBackgroundColor(bgColor)
- w.setTextColor(fgColor)
- w.clear()
- w.setCursorPos(11,1)
- w.write(recipe[14][1] and recipe[14][1] or " ")
- -- local map = {{1,1}, {21,2}, {31,2}, {31,3}, {31,4}, {31,5}, {21,5}, {11,5}, {1,5}, {1,4}, {1,3}, {1,2}, {11,2}}
- local map = {{1,1}, {27,2}, {40,2}, {40,3}, {40,4}, {40,5}, {27,5}, {14,5}, {1,5}, {1,4}, {1,3}, {1,2}, {14,2}}
- for i=1,13 do
- w.setCursorPos(map[i][1]>1 and map[i][1]-1 or map[i][1],map[i][2])
- if map[i][1] > 1 then w.write(" ") end
- --w.write(math.floor(recipe[i][1])..(recipe[i][2] and (":"..math.floor(recipe[i][2])) or 0))
- w.write(recipe[i][3] and ((recipe[i][3]==" " or recipe[i][3]=="") and "-" or recipe[i][3]) or (math.floor(recipe[i][1])..(recipe[i][2] and (":"..math.floor(recipe[i][2])) or 0)))
- end
- end
- local function drawAltar()
- local altar = getAltar()
- drawRecipe(altarWind,altar)
- end
- local function redrawAltar()
- drawAltar()
- end
- local function redrawJarsNAltar()
- redrawJars()
- redrawAltar()
- end
- local rWind = window.create(wind,1,13,termX,7,false)
- local rWindPosX, rWindPosY = rWind.getPosition()
- local function getRecipes()
- wifi.transmit(channel[1],channel[1],{id="recipes",info=""})
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- return message.result
- end
- local function getRecipeNames()
- local recipes = getRecipes()
- local names = {}
- for _,recipe in pairs(recipes) do
- if #recipe >= 14 then
- names[#names+1] = recipe[14][1] and recipe[14][1] or (type(recipe[14]) ~= "table" and recipe[14] or "-")
- end
- end
- return names
- end
- local function loadR(name)
- wifi.transmit(channel[1],channel[1],{["id"]="loadR",["info"]=name})
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- redrawAltar()
- end
- local function drawRecipes()
- local names = getRecipeNames()
- rWind.setCursorPos(1,1)
- rWind.setBackgroundColor(bgColor)
- rWind.setTextColor(altFgColor)
- rWind.clear()
- for i=1,#names do
- rWind.setCursorPos(1,i)
- rWind.write(names[i])
- button[i+rWindPosY-1] = {{["x"]=0,["func"]=loadR,["data"]=names[i]}}
- end
- end
- local function redrawRecipes()
- drawRecipes()
- end
- local function refresh()
- redrawJarsNAltar()
- redrawRecipes()
- end
- local function askForName()
- altarWind.setBackgroundColor(fgColor)
- altarWind.setTextColor(bgColor)
- altarWind.clear()
- altarWind.setCursorPos(1,2)
- altarWind.write("Please enter a name to store the current recipe")
- altarWind.setCursorPos(1,3)
- local input = read()
- redrawAltar()
- return input
- --(input and #input > 0) and input or false
- end
- local function addR()
- local name = askForName()
- if name then
- wifi.transmit(channel[1],channel[1],{["id"]="add",["info"]=name})
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- end
- end
- local function prepareWindow()
- wind.setVisible(false)
- wind.setBackgroundColor(bgColor)
- wind.setTextColor(fgColor)
- wind.clear()
- wind.setCursorPos(1,1)
- wind.write("Infusion Altar Control Center")
- wind.setCursorPos(1,3)
- -- wind.write(termX.." "..termY)
- -- wind.setCursorPos(1,1)
- -- wind.setBackgroundColor(bgColor)
- -- wind.setTextColor(fgColor)
- wind.write(padStr("Essentia Storage",termX))
- wind.setTextColor(altFgColor)
- wind.setCursorPos(termX-10,3)
- wind.write("Refresh")
- local _,ypos = wind.getCursorPos()
- button[ypos + windPosY - 1] = {{["x"]=0,["func"]=refresh,["data"]=""}}
- wind.setCursorPos(termX-10,6)
- wind.write("Add Recipe")
- _,ypos = wind.getCursorPos()
- button[ypos + windPosY - 1] = {}
- for i=0,9 do
- table.insert(button[ypos + windPosY - 1],{["x"]=termX-10+i,["func"]=addR,["data"]=""})
- end
- wind.setTextColor(fgColor)
- wind.setCursorPos(1,4)
- drawJars()
- drawAltar()
- drawRecipes()
- wind.setVisible(true)
- jarWind.setVisible(true)
- altarWind.setVisible(true)
- rWind.setVisible(true)
- end
- local function drawUserInterface()
- wind.redraw()
- end
- getJars()
- prepareWindow()
- while true do
- drawUserInterface()
- local event, mousebutton, xPos, yPos = os.pullEvent()
- if event == "mouse_click" and mousebutton == 1 then
- if button[yPos] then
- for _,b in pairs(button[yPos]) do
- if b.x == 0 or b.x == xPos then
- b.func(b.data)
- break
- end
- end
- end
- end
- end
- --wifi.transmit(channel[1],channel[1],{id="altar",info=""})
- --local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- --print(message.id," ",message.result[1])
- end
Advertisement
Add Comment
Please, Sign In to add comment