Advertisement
jig487

TerminalNonAPDev

Jul 22nd, 2021 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. local name = string.gsub(string.lower(os.getComputerLabel()):gsub("%s+", ""), "%s+", "")
  2.  
  3. --Set up monitors and modem
  4. local mon
  5. local modem
  6.  
  7. if turtle.getFuelLevel() == 0 then
  8.     error("Turtle out of fuel")
  9. end
  10.  
  11. if peripheral.find("modem") then
  12.     modem = peripheral.find("modem")
  13. else
  14.     for i = 1, 16 do
  15.         local data = turtle.getItemDetail(i)
  16.         if data and data.name == "computercraft:wireless_modem_advanced" then
  17.             turtle.select(i)
  18.             turtle.equipLeft()
  19.             modem = peripheral.find("modem")
  20.             break
  21.         elseif i == 16 then
  22.             error("Turtle missing ender modem in inventory")
  23.         end
  24.     end
  25. end
  26.  
  27. for i = 1, 16 do
  28.     local data = turtle.getItemDetail(i)
  29.     if data and data.name == "computercraft:monitor_advanced" and data.count >= 2 then
  30.         if not turtle.inspectUp() then
  31.             turtle.select(i)
  32.             turtle.back()    
  33.             turtle.up()
  34.             turtle.place()
  35.             turtle.turnLeft()
  36.             turtle.forward()
  37.             turtle.turnRight()
  38.             turtle.place()
  39.             turtle.turnRight()
  40.             turtle.forward()
  41.             turtle.turnLeft()
  42.             turtle.down()
  43.             turtle.forward()
  44.             mon = peripheral.find("monitor")
  45.             break
  46.         else
  47.             mon = peripheral.find("monitor")
  48.             break
  49.         end
  50.     elseif i == 16 then
  51.         error("Couldn't find 2 advanced monitors in inventory")
  52.     end
  53. end
  54.  
  55. modem.open(42)
  56.  
  57. local function sendRequest(targetID,selectedIndex,fName)
  58.     local table = {}
  59.     table.targetID = targetID or 1
  60.     table.selectedIndex = selectedIndex or 1
  61.     table.name = fName or name
  62.     modem.transmit(42, 42, table)
  63. end
  64.  
  65. -- Display buttons and returns button list
  66. local function showButtons(branchList)
  67.     local x,y = mon.getCursorPos()
  68.     mon.setCursorPos(1,y+2)
  69.  
  70.     local bList = {}
  71.     for i = 1, #branchList do
  72.  
  73.         if not branchList[i].targetID then
  74.             error("showButtons: targetID "..i.." does not exist")
  75.         elseif not branchList[i].label then
  76.             error("showButtons: label "..i.." does not exist")
  77.         end
  78.  
  79.         local X1 , Y1 = mon.getCursorPos()
  80.         write( "["..i.."] "..branchList[i].label )
  81.         local X2 , Y2 = mon.getCursorPos()
  82.         table.insert( bList, { x1 = X1, y1 = Y1, x2 = X2, y2 = Y2, } )
  83.         mon.setCursorPos(1,Y2+1)
  84.     end
  85.  
  86.     return bList
  87. end
  88.  
  89. --Wait for button press. Returns pressed button OR returns (false, nil) if unsuccessful
  90. local function bPress(buttonList)
  91.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  92.     for i = 1, #buttonList do
  93.         if buttonList[i].x1 <= xPos and xPos <= buttonList[i].x2 and buttonList[i].y1 <= yPos and yPos <= buttonList[i].y2 then
  94.             return i
  95.         end
  96.     end
  97.     return false, nil
  98. end
  99.  
  100. local function say(string)
  101.     local string = string or fill[1][2]
  102.     local x,y = mon.getSize()
  103.     local lines = require "cc.strings".wrap(string, x)
  104.     for i = 1, #lines do
  105.       term.setCursorPos(1, i)
  106.       textutils.slowWrite(lines[i])
  107.     end
  108. end
  109.  
  110. local oldTerm = term.redirect(mon)
  111.  
  112. term.clear()
  113. mon.setTextScale(0.5)
  114. term.setTextColor(1)
  115. term.setCursorPos(1,1)
  116.  
  117. sendRequest(1,1,name)
  118.  
  119. while true do
  120.     --Send package request
  121.     --Package structure looks like: package = { tName, color, targetIDs, bLabels, bResponses }
  122.     local event, side, frequency, replyFrequency, package, distance = os.pullEvent("modem_message")
  123.    
  124.  
  125.     if package == "missingName" then
  126.         error("Turtle name: "..name.." was not found in npc list")
  127.     elseif package == "missingTID" then
  128.         error("Requested a missing target ID: "..targetID)
  129.     end
  130.  
  131.     --List button options
  132.     mon.setTextColor(1)
  133.     local bList = showButtons(package.branches)
  134.  
  135.     --Get player button choice
  136.     local bPressed = bPress(bList)
  137.     while bPressed == false do
  138.         bPressed = bPress(bList)
  139.     end
  140.    
  141.     mon.clear()
  142.     mon.setCursorPos(1,1)                  
  143.     mon.setTextColor(package.color)
  144.    
  145.     --Respond to player
  146.     say("'"..package.branches[bPressed].response.."'")
  147.  
  148.     targetID = package.branches[bPressed].targetID
  149.     selectedIndex = package.branches[bPressed].index
  150.     sendRequest(targetID,selectedIndex,name)
  151. end
  152.  
  153. term.redirect(oldTerm)
  154.  
  155. turtle.digUp()
  156. turtle.turnLeft()
  157. turtle.forward()
  158. turtle.digUp()
  159. turtle.back()
  160. turtle.turnRight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement