Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local name = string.gsub(string.lower(os.getComputerLabel()):gsub("%s+", ""), "%s+", "")
- --Set up monitors and modem
- local mon
- local modem
- if turtle.getFuelLevel() == 0 then
- error("Turtle out of fuel")
- end
- if peripheral.find("modem") then
- modem = peripheral.find("modem")
- else
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data and data.name == "computercraft:wireless_modem_advanced" then
- turtle.select(i)
- turtle.equipLeft()
- modem = peripheral.find("modem")
- break
- elseif i == 16 then
- error("Turtle missing ender modem in inventory")
- end
- end
- end
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data and data.name == "computercraft:monitor_advanced" and data.count >= 2 then
- if not turtle.inspectUp() then
- turtle.select(i)
- turtle.back()
- turtle.up()
- turtle.place()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- turtle.place()
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- turtle.down()
- turtle.forward()
- mon = peripheral.find("monitor")
- break
- else
- mon = peripheral.find("monitor")
- break
- end
- elseif i == 16 then
- error("Couldn't find 2 advanced monitors in inventory")
- end
- end
- modem.open(42)
- local function sendRequest(targetID,selectedIndex,fName)
- local table = {}
- table.targetID = targetID or 1
- table.selectedIndex = selectedIndex or 1
- table.name = fName or name
- modem.transmit(42, 42, table)
- end
- -- Display buttons and returns button list
- local function showButtons(branchList)
- local x,y = mon.getCursorPos()
- mon.setCursorPos(1,y+2)
- local bList = {}
- for i = 1, #branchList do
- if not branchList[i].targetID then
- error("showButtons: targetID "..i.." does not exist")
- elseif not branchList[i].label then
- error("showButtons: label "..i.." does not exist")
- end
- local X1 , Y1 = mon.getCursorPos()
- write( "["..i.."] "..branchList[i].label )
- local X2 , Y2 = mon.getCursorPos()
- table.insert( bList, { x1 = X1, y1 = Y1, x2 = X2, y2 = Y2, } )
- mon.setCursorPos(1,Y2+1)
- end
- return bList
- end
- --Wait for button press. Returns pressed button OR returns (false, nil) if unsuccessful
- local function bPress(buttonList)
- local event, side, xPos, yPos = os.pullEvent("monitor_touch")
- for i = 1, #buttonList do
- if buttonList[i].x1 <= xPos and xPos <= buttonList[i].x2 and buttonList[i].y1 <= yPos and yPos <= buttonList[i].y2 then
- return i
- end
- end
- return false, nil
- end
- local function say(string)
- local string = string or fill[1][2]
- local x,y = mon.getSize()
- local lines = require "cc.strings".wrap(string, x)
- for i = 1, #lines do
- term.setCursorPos(1, i)
- textutils.slowWrite(lines[i])
- end
- end
- local oldTerm = term.redirect(mon)
- term.clear()
- mon.setTextScale(0.5)
- term.setTextColor(1)
- term.setCursorPos(1,1)
- sendRequest(1,1,name)
- while true do
- --Send package request
- --Package structure looks like: package = { tName, color, targetIDs, bLabels, bResponses }
- local event, side, frequency, replyFrequency, package, distance = os.pullEvent("modem_message")
- if package == "missingName" then
- error("Turtle name: "..name.." was not found in npc list")
- elseif package == "missingTID" then
- error("Requested a missing target ID: "..targetID)
- end
- --List button options
- mon.setTextColor(1)
- local bList = showButtons(package.branches)
- --Get player button choice
- local bPressed = bPress(bList)
- while bPressed == false do
- bPressed = bPress(bList)
- end
- mon.clear()
- mon.setCursorPos(1,1)
- mon.setTextColor(package.color)
- --Respond to player
- say("'"..package.branches[bPressed].response.."'")
- targetID = package.branches[bPressed].targetID
- selectedIndex = package.branches[bPressed].index
- sendRequest(targetID,selectedIndex,name)
- end
- term.redirect(oldTerm)
- turtle.digUp()
- turtle.turnLeft()
- turtle.forward()
- turtle.digUp()
- turtle.back()
- turtle.turnRight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement