Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local select_screen = "fd5b599c-0519-4bb2-aeb1-5cec9c7bdfc9"
- local robot_modem_adr = ""
- -- buttonBg, buttonFg, pressedButtonBg, pressedButtonFg
- local colors = {0x00accf, 0xFFC700, 0x008EAB, 0xFFC700 }
- local port = 50
- local component = require("component")
- local event = require("event")
- local keyboard = require("keyboard")
- local gpu = component.gpu
- local modem = component.modem
- local w, h = gpu.getResolution()
- local buttons = {}
- local event_debug = false
- local screen_debug = false
- local selected = ""
- --[[
- # Debug functions
- > eventDebug(args...): void
- > screenDebug(args...): void
- --]]
- function eventDebug(eventName, arg1, arg2, arg3, arg4, arg5)
- if eventName ~= nil then
- print(eventName)
- end
- if arg1 ~= nil then
- print(arg1)
- end
- if arg2 ~= nil then
- print(arg2)
- end
- if arg3 ~= nil then
- print(arg3)
- end
- if arg4 ~= nil then
- print(arg4)
- end
- if arg5 ~= nil then
- print(arg5)
- end
- end
- function screenDebug()
- for i=1, 200 do
- gpu.set(1, i, tostring(i))
- gpu.set(i, 1, tostring(i), true)
- end
- end
- --[[
- # Button functions
- all functions around buttons
- > createButton(
- xPosition: int, yPosition: int, width: int, height: int,
- displayedText: string, internName: string,
- frontColor: color, backgroundColor: color
- ): boolean
- returns success
- > getButton(
- internName: string
- ): array
- returns array with button information
- > removeButton(
- internName: string
- ): boolean
- returns success
- > checkButton(
- screenAddress: string,
- xPosition: int, yPosition: int,
- button: int,
- playerName: string
- ) : string
- returns internName or nil
- > drawButtons(): void
- --]]
- function createButton(x1, y1, width, height, txt, name, fColor, bColor)
- buttons[#buttons+1] = {x1, y1, width, height, txt, name, fColor, bColor}
- end
- function getButton(name)
- for button in buttons do
- if button[6] == name then
- return button
- end
- end
- end
- function removeButton(name)
- local no = 0
- for i=1, #buttons do
- if buttons[i][6] == name then
- no = i
- end
- end
- local newButtons = {}
- for i=1, #buttons do
- if i ~= no then
- newButtons[#newButtons+1] = buttons[i]
- end
- end
- buttons = newButtons
- return (no ~= 0)
- end
- function checkButton(screenAdr, x, y, button, playerName)
- for i=1,#buttons do
- if x >= buttons[i][1] and x <= buttons[i][1] + buttons[i][3]
- and y >= buttons[i][2] and y <= buttons[i][2] + buttons[i][4] then
- print(buttons[i][6])
- return buttons[i][6]
- end
- end
- return nil
- end
- function drawButtons(clear)
- switchScreen(select_screen)
- local aw, ah = gpu.getResolution()
- if aw ~= 150 or ah ~= 20 then
- gpu.setResolution(150,20)
- end
- if clear ~= nil and clear == true then
- gpu.fill(0,0,w,h," ")
- end
- for i=1, #buttons do
- gpu.setBackground(buttons[i][8], true)
- gpu.setForeground(buttons[i][7], true)
- gpu.fill(buttons[i][1], buttons[i][2], buttons[i][3], buttons[i][4], " ")
- gpu.set(buttons[i][1] + ((buttons[i][3]-#buttons[i][5])/2), buttons[i][2] + (buttons[i][4]/2), buttons[i][5])
- end
- if screen_debug == true then
- screenDebug()
- end
- switchScreenBack()
- end
- --[[
- # Screen functions
- all functions around screens
- > switchScreen(
- oldScreenAddress: string, newScreenAddress: string
- ): void
- > scwitchScreenBack(): void
- --]]
- local oldScreenAdr = ""
- function switchScreen(new)
- if gpu.getScreen() ~= new then
- oldScreenAdr = gpu.getScreen()
- gpu.bind(new)
- w, h = gpu.getResolution()
- end
- end
- function switchScreenBack()
- switchScreen(oldScreenAdr)
- end
- --[[
- # GUI functions
- all functions around the gui
- > createSelectGUI(): void
- > removeSelectGUI(): void
- > createAmountGUI(): void
- > removeAmountGUI(): void
- --]]
- function createSelectGUI()
- createButton(10, 5, 20, 3, "Minicio", "minicio", 1, 2)
- createButton(10, 10, 20, 3, "Coal", "coal", 1, 2)
- createButton(10, 15, 20, 3, "Iron", "iron", 1, 2)
- createButton(40, 5, 20, 3, "Redstone", "redstone", 1, 2)
- createButton(40, 10, 20, 3, "Gold", "gold", 1, 2)
- createButton(40, 15, 20, 3, "Fluix", "fluix", 1, 2)
- createButton(70, 5, 20, 3, "Diamond", "diamond", 1, 2)
- createButton(70, 10, 20, 3, "Certus Quartz", "certusquartz", 1, 2)
- createButton(70, 15, 20, 3, "Quartz", "quartz", 1, 2)
- end
- function removeSelectGUI()
- removeButton("minicio")
- removeButton("coal")
- removeButton("iron")
- removeButton("redstone")
- removeButton("gold")
- removeButton("fluix")
- removeButton("diamond")
- removeButton("certusquartz")
- removeButton("quartz")
- end
- function createAmountGUI()
- createButton(116, 5, 9, 3, "1", "num_1", 3, 2)
- createButton(126, 5, 9, 3, "2", "num_2", 3, 2)
- createButton(136, 5, 9, 3, "3", "num_3", 3, 2)
- createButton(116, 9, 9, 3, "4", "num_4", 3, 2)
- createButton(126, 9, 9, 3, "5", "num_5", 3, 2)
- createButton(136, 9, 9, 3, "6", "num_6", 3, 2)
- createButton(116, 13, 9, 3, "7", "num_7", 3, 2)
- createButton(126, 13, 9, 3, "8", "num_8", 3, 2)
- createButton(136, 13, 9, 3, "9", "num_9", 3, 2)
- createButton(116, 17, 9, 3, "C", "num_c", 3, 2)
- createButton(126, 17, 9, 3, "0", "num_0", 3, 2)
- createButton(136, 17, 9, 3, "E", "num_e", 3, 2)
- end
- function removeAmountGUI()
- removeButton("num_1")
- removeButton("num_2")
- removeButton("num_3")
- removeButton("num_4")
- removeButton("num_5")
- removeButton("num_6")
- removeButton("num_7")
- removeButton("num_8")
- removeButton("num_9")
- removeButton("num_c")
- removeButton("num_0")
- removeButton("num_e")
- end
- --[[
- # Network functions
- all functions around communication
- > sendStatusRequest(): void
- > isRobotAvailable(): boolean
- > sendingQueue(): void
- > messageHandler(receiver, sender, port, distance, msg): void
- > addInstruction(): void
- > sendInstructions(): void
- --]]
- local robot_availability = false
- local task_Description = ""
- local messagesToSend = {}
- function sendStatusRequest()
- local success_1 = modem.send(robot_modem_adr, 50, "getStatus")
- local success_2 = modem.send(robot_modem_adr, 50, "getTask")
- if success_1 == false or success_2 == false then
- print("ERROR: UNABLE to COMMUNICATE with the robot")
- os.exit()
- end
- end
- function isRobotAvailable()
- return robot_availability
- end
- function sendingQueue()
- if isRobotAvailable == true and #messagesToSend > 0 then
- modem.send(robot_modem_adr, 50, messagesToSend[1])
- local help = {}
- for i=2,#messagesToSend do
- help[#help+1] = messagesToSend[i]
- end
- messagesToSend = help
- sendStatusRequest()
- end
- end
- function messageHandler(local receiver, local sender, local port, local distance, local msg)
- if sender ~= robot_modem_adr or port ~= 50 then
- return nil
- end
- if msg == "available" then
- robot_availability = true
- elseif msg == "unavailable" then
- robot_availability = false
- end
- end
- --[[
- # main script
- --]]
- for i=1, #colors do
- gpu.setPaletteColor(i,colors[i])
- end
- if modem.open(50) == false then
- print("ERROR: UNABLE to OPEN PORT!")
- os.exit()
- end
- createSelectGUI()
- createAmountGUI()
- drawButtons(true)
- while true do
- eventName, arg1, arg2, arg3, arg4, arg5 = event.pull();
- if event_debug == true then
- eventDebug(eventName, arg1, arg2, arg3, arg4, arg5)
- end
- if eventName == "touch" and arg1 == select_screen then
- checkButton(arg1, arg2, arg3, arg4, arg5)
- drawButtons()
- elseif eventName == "key_down" then
- if arg2 == 116 and arg4 == "jamawie" then
- break
- elseif arg2 == 101 and arg4 == "jamawie" then
- if event_debug == true then
- event_debug = false
- else
- event_debug = true
- end
- elseif arg2 == 115 and arg4 == "jamawie" then
- if screen_debug == true then
- screen_debug = false
- else
- screen_debug = true
- end
- drawButtons()
- end
- end
- end
- event.listen("touch", checkButton)
- event.listen("modem_message", messageHandler)
- event.timer(10, sendStatusRequest, math.huge)
- event.timer(10, sendingQueue, math.huge)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement