Advertisement
MatthewJ217

Remote Control v2 (CC Turtle)

Jun 1st, 2023 (edited)
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- Interpret rednet signals as movement commands
  2.  
  3. local id, message;
  4. local currActKey = 0; -- This marks the last key pressed for an action
  5. local actionUsed = false;
  6.  
  7. function selectAndSendSlot()
  8.     turtle.select(turtle.getSelectedSlot()%16+1)
  9.     local block = turtle.getItemDetail();
  10.     if block then return block.name; else return "Empty slot"; end
  11. end
  12.  
  13. function deselectAndSendSlot()
  14.     turtle.select(turtle.getSelectedSlot()%16+1)
  15.     local block = turtle.getItemDetail();
  16.     if block then return block.name; else return "Empty slot"; end
  17. end
  18.  
  19. function observe()
  20.     local isBlock, blockData = turtle.inspect();
  21.     local str = "";
  22.     if not isBlock then str = str.."Not a block\n"; end
  23.     if blockData.name then str = str..blockData.name.."\n"; end
  24.     if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
  25.     return str;
  26. end
  27.  
  28. function observeUp()
  29.     local isBlock, blockData = turtle.inspectUp();
  30.     local str = "";
  31.     if not isBlock then str = str.."Not a block\n"; end
  32.     if blockData.name then str = str..blockData.name.."\n"; end
  33.     if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
  34.     return str;
  35. end
  36.  
  37. function observeDown()
  38.     local isBlock, blockData = turtle.inspectDown();
  39.     local str = "";
  40.     if not isBlock then str = str.."Not a block\n"; end
  41.     if blockData.name then str = str..blockData.name.."\n"; end
  42.     if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
  43.     return str;
  44. end
  45.    
  46. local directionKeys = {
  47. [87] = 1, --forward
  48. [32] = 2, --up
  49. [340] = 3, --down
  50. [83] = 4, --back
  51. [65] = 5, --left
  52. [68] = 6, --right
  53. };
  54.  
  55. local actions = {
  56. [0] = {"Move", turtle.forward, turtle.up, turtle.down, turtle.back, turtle.turnLeft, turtle.turnRight},
  57. [85] = {"Dig (forward/up/down)", turtle.dig, turtle.digUp, turtle.digDown}, -- U
  58. [73] = {"Place block (forward/up/down)", turtle.place, turtle.placeUp, turtle.placeDown}, -- I
  59. [82] = {"Refuel (Non-directional)", turtle.refuel}, -- R
  60. [57] = {"Change selected slot (forward/back)", selectAndSendSlot, deselectAndSendSlot}, -- 9
  61. [80] = {"Get block details (forward/up/down)", observe, observeUp, observeDown}, -- P
  62. [79] = {"Pick up item (forward/up/down)", turtle.suck, turtle.suckUp, turtle.suckDown}, -- O
  63. [75] = {"Drop selected item (forward/up/down)", turtle.drop, turtle.dropUp, turtle.dropDown}, -- K
  64. [56] = {"Equip selected item (left/right)",turtle.equipRight, turtle.equipLeft}, -- 8
  65. [74] = {"Attack (forward/up/down)", turtle.attack, turtle.attackUp, turtle.attackDown}, -- J
  66. }
  67.  
  68. function runAction(n, dir)
  69.     if actions[n] then
  70.         if #actions[n] == 2 then return actions[n][2](); end
  71.         if #actions[n] == 3 then return actions[n][({2, 2, 3, 3, 3, 2})[dir]](); end
  72.         if #actions[n] == 4 then return actions[n][({2, 3, 4, 4, 3, 2})[dir]](); end
  73.         if #actions[n] == 7 then return actions[n][dir+1](); end
  74.     else
  75.         return "Key not mapped to an action (redundancy check)";
  76.     end
  77. end
  78.  
  79. peripheral.find("modem", rednet.open);
  80. print("Reciever started - Connect to ID", os.computerID());
  81. while true do
  82.     id, message = rednet.receive();
  83.     cmdKey = tonumber(message);
  84.     if directionKeys[cmdKey] ~= nil then -- Do the current action
  85.         local msg = tostring(runAction(currActKey, directionKeys[cmdKey]));
  86.         actionUsed = true;
  87.         if msg ~= "true" then
  88.             rednet.send(id, msg);
  89.         else
  90.             rednet.send(id, "");
  91.         end
  92.     else
  93.         if cmdKey == 0 then -- Reset the selected action
  94.             if not actionUsed and currActKey ~= 0 then
  95.                 rednet.send(id, ("%s is mapped to %s"):format((keys.getName(currActKey)):upper(), actions[currActKey][1]));
  96.             else
  97.                 rednet.send(id, "");
  98.             end
  99.             currActKey = 0;
  100.             actionUsed = false;
  101.         else -- Select an action
  102.             if actions[cmdKey] then
  103.                 currActKey = cmdKey;
  104.                 actionUsed = false;
  105.                 rednet.send(id, "");
  106.             else
  107.                 currActKey = 0;
  108.                 rednet.send(id, "Key not mapped to an action");
  109.             end
  110.         end
  111.     end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement