Advertisement
MatthewJ217

Remote Control (Turtle)

Oct 24th, 2022 (edited)
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. -- Interpret rednet signals as movement commands
  2.  
  3. local id, message;
  4.  
  5. function selectAndSendSlot()
  6.     turtle.select(turtle.getSelectedSlot()%16+1)
  7.     local block = turtle.getItemDetail();
  8.     if block then return block.name; else return "Empty slot"; end
  9. end
  10.  
  11. function deselectAndSendSlot()
  12.     turtle.select(turtle.getSelectedSlot()%16+1)
  13.     local block = turtle.getItemDetail();
  14.     if block then return block.name; else return "Empty slot"; end
  15. end
  16.  
  17. function observe()
  18.     local isBlock, blockData = turtle.inspect();
  19.     local str = "";
  20.     if not isBlock then str = str.."Not a block\n"; end
  21.     if blockData.name then str = str..blockData.name.."\n"; end
  22.     if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
  23.     return str;
  24. end
  25.  
  26. function observeUp()
  27.     local isBlock, blockData = turtle.inspectUp();
  28.     local str = "";
  29.     if not isBlock then str = str.."Not a block\n"; end
  30.     if blockData.name then str = str..blockData.name.."\n"; end
  31.     if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
  32.     return str;
  33. end
  34.  
  35. function observeDown()
  36.     local isBlock, blockData = turtle.inspectDown();
  37.     local str = "";
  38.     if not isBlock then str = str.."Not a block\n"; end
  39.     if blockData.name then str = str..blockData.name.."\n"; end
  40.     if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
  41.     return str;
  42. end
  43.    
  44.  
  45. local moves = {
  46. [32] = turtle.up,
  47. [340] = turtle.down,
  48. [87] = turtle.forward,
  49. [83] = turtle.back,
  50. [65] = turtle.turnLeft,
  51. [68] = turtle.turnRight,
  52. [82] = turtle.digUp,
  53. [70] = turtle.dig,
  54. [67] = turtle.digDown,
  55. [84] = turtle.placeUp,
  56. [71] = turtle.place,
  57. [86] = turtle.placeDown,
  58. [90] = turtle.refuel,
  59. [49] = selectAndSendSlot,
  60. [50] = deselectAndSendSlot,
  61. [89] = observeUp,
  62. [72] = observe,
  63. [66] = observeDown,
  64. [85] = turtle.suckUp,
  65. [74] = turtle.suck,
  66. [78] = turtle.suckDown,
  67. [73] = turtle.dropUp,
  68. [75] = turtle.drop,
  69. [77] = turtle.dropDown,
  70. [88] = turtle.equipRight,
  71. [79] = turtle.attackUp,
  72. [76] = turtle.attack,
  73. [188] = turtle.attackDown
  74. };
  75.  
  76. peripheral.find("modem", rednet.open);
  77. while true do
  78.     id, message = rednet.receive();
  79.     if moves[tonumber(message)] then
  80.         local msg = moves[tonumber(message)]();
  81.         if tostring and tostring(msg) ~= "true" then rednet.send(id, msg); else rednet.send(id, ""); end
  82.     else
  83.         rednet.send(id, "Button not mapped");
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement