Advertisement
theTANCO

CC Turtle Controller

Jun 24th, 2020 (edited)
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.32 KB | None | 0 0
  1. -- v1.0
  2. local blocks = {
  3.   front = {turtle.inspect()},
  4.   above = {turtle.inspectUp()},
  5.   below = {turtle.inspectDown()},
  6.   item = turtle.getItemDetail()
  7. }
  8. local keysPressed = {
  9.   up = false,
  10.   down = false,
  11. }
  12. local displayText = {}
  13. local xSize, ySize = term.getSize()
  14. local page = 1
  15. local scrollPos = 0
  16.  
  17. function display()
  18.   term.clear()
  19.   term.setCursorPos(1,1)
  20.   if page == 1 then
  21.     displayText = {
  22.       [ 1] = "Action Commands       Software Commands",
  23.       [ 2] = " Delete = Dig        Left = Last Page  ",
  24.       [ 3] = " Insert = Place/Use Right = Next Page  ",
  25.       [ 4] = "      Z = Attack     PgUp = Scroll Up  ",
  26.       [ 5] = "      R = Refuel   PgDown = Scroll Down",
  27.       [ 6] = "      V = Vein Miner    U = Update Data",
  28.       [ 7] = "",
  29.       [ 8] = "Movement Commands    Inventory Commands",
  30.       [ 9] = "    W = Forward  Num8 = Inv. Slot Up   ",
  31.       [10] = "    A = Left     Num4 = Inv. Slot Left ",
  32.       [11] = "    S = Back     Num2 = Inv. Slot Down ",
  33.       [12] = "    D = Right    Num6 = Inv. Slot Right",
  34.       [13] = "Space = Up          Q = Drop Item      ",
  35.       [14] = "Shift = Down        E = Pick Up Item   ",
  36.       [15] = "   Up = Face Up     [ = Equip Left     ",
  37.       [16] = " Down = Face Down   ] = Equip Right    ",
  38.     }
  39.     if term.isColor() then
  40.       table.insert(displayText, 17, "         Scroll Wheel = Cycle Inv. Slot")
  41.       table.insert(displayText, 7, "R Click = Place/Use")
  42.       table.insert(displayText, 7, "L Click = Dig                          ")
  43.     end
  44.     if turtle.craft then table.insert(displayText, 7, "     C = Craft                         ") end
  45.  
  46.     for a = 1, ySize-2 do
  47.       if displayText[a+scrollPos] == "Movement Commands    Inventory Commands"
  48.       or displayText[a+scrollPos] == "Action Commands       Software Commands"
  49.       or displayText[a+scrollPos] == "Program List Commands" then
  50.         term.setTextColor(colors.lightBlue)
  51.       else
  52.         term.setTextColor(colors.white)
  53.       end
  54.       print(displayText[a+scrollPos])
  55.     end
  56.     term.setTextColor(colors.yellow)
  57.     term.setCursorPos(1, ySize-1)
  58.     print("                        Right = Inspect")
  59.   elseif page == 2 then
  60.     local text = ""
  61.     term.setTextColor(colors.lightBlue)
  62.     if keysPressed.up then
  63.       print("Inspecting Block Above:")
  64.       if blocks.above[1] then
  65.         text = "Name: "..blocks.above[2].name
  66.         text = text.."\nState:"..string.sub(textutils.serialize(blocks.above[2].state), 2, string.len(textutils.serialize(blocks.above[2].state))-2)
  67.         text = text.."\nTags:\n"..string.sub(textutils.serialize(blocks.above[2].tags), 2, string.len(textutils.serialize(blocks.above[2].tags))-2)
  68.       else text = blocks.above[2].."\n" end
  69.     elseif keysPressed.down then
  70.       print("Inspecting Block Below:")
  71.       if blocks.below[1] then
  72.         text = "Name: "..blocks.below[2].name
  73.         text = text.."\nState:"..string.sub(textutils.serialize(blocks.below[2].state), 2, string.len(textutils.serialize(blocks.below[2].state))-2)
  74.         text = text.."\nTags:"..string.sub(textutils.serialize(blocks.below[2].tags), 2, string.len(textutils.serialize(blocks.below[2].tags))-2)
  75.       else text = blocks.below[2].."\n" end
  76.     else
  77.       print("Inspecting Block In Front:")
  78.       if blocks.front[1] then
  79.         text = "Name: "..blocks.front[2].name
  80.         text = text.."\nState:"..string.sub(textutils.serialize(blocks.front[2].state), 2, string.len(textutils.serialize(blocks.front[2].state))-1)
  81.         text = text.."Tags:"..string.sub(textutils.serialize(blocks.front[2].tags), 2, string.len(textutils.serialize(blocks.front[2].tags))-1)
  82.       else text = blocks.front[2].."\n" end
  83.     end
  84.     term.setTextColor(colors.white)
  85.     textByLines(text)
  86.     term.setTextColor(colors.yellow)
  87.     term.setCursorPos(1, ySize-1)
  88.     print("Left = Commands       Right = Item Info")
  89.   elseif page == 3 then
  90.     local text = ""
  91.     term.setTextColor(colors.lightBlue)
  92.     print("Inspecting Selected Item:")
  93.     if blocks.item == nil then text = "No item in the selected slot.\n"
  94.     else
  95.       text = "Name: "..blocks.item.name
  96.       text = text.."\nCount: "..blocks.item.count
  97.       text = text.."\nTags:"..string.sub(textutils.serialize(blocks.item.tags), 2, string.len(textutils.serialize(blocks.item.tags))-1)
  98.     end
  99.     term.setTextColor(colors.white)
  100.     textByLines(text)
  101.     term.setTextColor(colors.yellow)
  102.     term.setCursorPos(1, ySize-1)
  103.     print("Left = Inspect                         ")
  104.   end
  105.   write("T = Exit  F: ")
  106.   if keysPressed.up then write("Up")
  107.   elseif keysPressed.down then write("Down")
  108.   else write("Front") end
  109.   term.setCursorPos(xSize-16+(5-string.len(turtle.getFuelLevel())),13)
  110.   write("Fuel: "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
  111. end
  112.  
  113. function textByLines(text)
  114.   displayText = {}
  115.   local a, lastSpace = 1, 1
  116.   repeat
  117.     if string.sub(text, 1, 2) == "  " then text = string.sub(text, 3, string.len(text)) end
  118.     if string.sub(text, a, a) == " " or string.sub(text, a, a) == "\n" then lastSpace = a end
  119.     if string.sub(text, a, a) == "\n" or a == xSize then
  120.       if lastSpace > 1 then
  121.         displayText[#displayText+1] = string.sub(text, 1, lastSpace-1)
  122.         text = string.sub(text, lastSpace+1, string.len(text))
  123.       else
  124.         displayText[#displayText+1] = string.sub(text, 1, a)
  125.         text = string.sub(text, a+1, string.len(text))
  126.       end
  127.       a, lastSpace = 1, 1
  128.     else a = a + 1
  129.     end
  130.   until string.len(text) == 0
  131.  
  132.   for a = 1, ySize-3 do
  133.     if a+scrollPos > #displayText then print("")
  134.     else print(displayText[a+scrollPos]) end
  135.   end
  136. end
  137.  
  138. function updateData()
  139.   blocks.front = {turtle.inspect()}
  140.   blocks.above = {turtle.inspectUp()}
  141.   blocks.below = {turtle.inspectDown()}
  142.   blocks.item = turtle.getItemDetail()
  143. end
  144.  
  145. while true do
  146.   display()
  147.   local event = {os.pullEvent()}
  148.   if event[1] == "key" then
  149.     os.pullEvent("key_up")
  150.     -- Movement Commands
  151.     if event[2] == keys.w then
  152.       turtle.forward()
  153.       updateData()
  154.     elseif event[2] == keys.a then
  155.       turtle.turnLeft()
  156.       blocks.front = {turtle.inspect()}
  157.     elseif event[2] == keys.s then
  158.       turtle.back()
  159.       updateData()
  160.     elseif event[2] == keys.d then
  161.       turtle.turnRight()
  162.       blocks.front = {turtle.inspect()}
  163.     elseif event[2] == keys.space then
  164.       turtle.up()
  165.       updateData()
  166.     elseif event[2] == keys.leftShift then
  167.       turtle.down()
  168.       updateData()
  169.     elseif event[2] == keys.up then
  170.       if keysPressed.down then keysPressed.down = false
  171.       else keysPressed.up = true end
  172.     elseif event[2] == keys.down then
  173.       if keysPressed.up then keysPressed.up = false
  174.       else keysPressed.down = true end
  175.     -- Inventory Management Commands
  176.     elseif event[2] == keys.numPad8 then
  177.       local newSlot = turtle.getSelectedSlot() - 4
  178.       if newSlot < 1 then newSlot = newSlot + 16 end
  179.       turtle.select(newSlot)
  180.       blocks.item = turtle.getItemDetail()
  181.     elseif event[2] == keys.numPad4 then
  182.       local newSlot = turtle.getSelectedSlot() - 1
  183.       if newSlot%4 == 0 then newSlot = newSlot + 4 end
  184.       turtle.select(newSlot)
  185.       blocks.item = turtle.getItemDetail()
  186.     elseif event[2] == keys.numPad2 then
  187.       local newSlot = turtle.getSelectedSlot() + 4
  188.       if newSlot > 16 then newSlot = newSlot - 16 end
  189.       turtle.select(newSlot)
  190.       blocks.item = turtle.getItemDetail()
  191.     elseif event[2] == keys.numPad6 then
  192.       local newSlot = turtle.getSelectedSlot() + 1
  193.       if newSlot%4 == 1 then newSlot = newSlot - 4 end
  194.       turtle.select(newSlot)
  195.       blocks.item = turtle.getItemDetail()
  196.     elseif event[2] == keys.q then
  197.       if keysPressed.up then turtle.dropUp()
  198.       elseif keysPressed.down then turtle.dropDown()
  199.       else turtle.drop() end
  200.       blocks.item = turtle.getItemDetail()
  201.     elseif event[2] == keys.e then
  202.       if keysPressed.up then turtle.suckUp()
  203.       elseif keysPressed.down then turtle.suckDown()
  204.       else turtle.suck() end
  205.       blocks.item = turtle.getItemDetail()
  206.     elseif event[2] == keys.leftBracket then
  207.       turtle.equipLeft()
  208.       blocks.item = turtle.getItemDetail()
  209.     elseif event[2] == keys.rightBracket then
  210.       turtle.equipRight()
  211.       blocks.item = turtle.getItemDetail()
  212.     -- Action Commands
  213.     elseif event[2] == keys.delete then
  214.       if keysPressed.up then
  215.         turtle.digUp()
  216.         blocks.above = {turtle.inspectUp()}
  217.       elseif keysPressed.down then
  218.         turtle.digDown()
  219.         blocks.below = {turtle.inspectDown()}
  220.       else
  221.         turtle.dig()
  222.         blocks.front = {turtle.inspect()}
  223.       end
  224.       blocks.item = turtle.getItemDetail()
  225.     elseif event[2] == keys.insert then
  226.       if keysPressed.up then
  227.         turtle.placeUp()
  228.         blocks.above = {turtle.inspectUp()}
  229.       elseif keysPressed.down then
  230.         turtle.placeDown()
  231.         blocks.below = {turtle.inspectDown()}
  232.       else
  233.         turtle.place()
  234.         blocks.front = {turtle.inspect()}
  235.       end
  236.       blocks.item = turtle.getItemDetail()
  237.     elseif event[2] == keys.z then
  238.       if keysPressed.up then turtle.attackUp()
  239.       elseif keysPressed.down then turtle.attackDown()
  240.       else turtle.attack() end
  241.     elseif event[2] == keys.r then
  242.       turtle.refuel(1)
  243.       blocks.item = turtle.getItemDetail()
  244.     elseif event[2] == keys.v then
  245.       term.setTextColor(colors.white)
  246.       shell.run("VeinMiner.lua")
  247.       updateData()
  248.     elseif event[2] == keys.c then
  249.       if turtle.craft then
  250.         turtle.craft(1)
  251.         blocks.item = turtle.getItemDetail()
  252.       end
  253.     -- Software Commands
  254.     elseif event[2] == keys.u then
  255.       updateData()
  256.     elseif event[2] == keys.t then
  257.       term.clear()
  258.       term.setCursorPos(1,1)
  259.       error()
  260.     elseif event[2] == keys.right then
  261.       if page < 3 then page = page + 1 end
  262.       scrollPos, displayText = 0, {}
  263.     elseif event[2] == keys.left then
  264.       if page > 1 then page = page - 1 end
  265.       scrollPos, displayText = 0, {}
  266.     elseif event[2] == keys.pageUp and scrollPos > 0 then
  267.       scrollPos = scrollPos - 1
  268.     elseif event[2] == keys.pageDown and (page == 1 and scrollPos+ySize-2 < #displayText) or (page > 1 and scrollPos+ySize-3 < #displayText) then
  269.       scrollPos = scrollPos + 1
  270.     end
  271.   elseif event[1] == "mouse_click" then
  272.     os.pullEvent("mouse_up")
  273.     if event[2] == 1 then
  274.       if keysPressed.up then
  275.         turtle.digUp()
  276.         blocks.above = {turtle.inspectUp()}
  277.       elseif keysPressed.down then
  278.         turtle.digDown()
  279.         blocks.below = {turtle.inspectDown()}
  280.       else
  281.         turtle.dig()
  282.         blocks.front = {turtle.inspect()}
  283.       end
  284.     elseif event[2] == 2 then
  285.       if keysPressed.up then
  286.         turtle.placeUp()
  287.         blocks.above = {turtle.inspectUp()}
  288.       elseif keysPressed.down then
  289.         turtle.placeDown()
  290.         blocks.below = {turtle.inspectDown()}
  291.       else
  292.         turtle.place()
  293.         blocks.front = {turtle.inspect()}
  294.       end
  295.     end
  296.   elseif event[1] == "mouse_scroll" then
  297.     local newSlot = turtle.getSelectedSlot() + event[2]
  298.     if newSlot < 1 then newSlot = newSlot + 16 end
  299.     if newSlot > 16 then newSlot = newSlot - 16 end
  300.     turtle.select(newSlot)
  301.     blocks.item = turtle.getItemDetail()
  302.   elseif event[1] == "turtle_inventory" then
  303.     blocks.item = turtle.getItemDetail()
  304.   end
  305. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement