Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Interpret rednet signals as movement commands
- local id, message;
- local currActKey = 0; -- This marks the last key pressed for an action
- local actionUsed = false;
- function selectAndSendSlot()
- turtle.select(turtle.getSelectedSlot()%16+1)
- local block = turtle.getItemDetail();
- if block then return block.name; else return "Empty slot"; end
- end
- function deselectAndSendSlot()
- turtle.select(turtle.getSelectedSlot()%16+1)
- local block = turtle.getItemDetail();
- if block then return block.name; else return "Empty slot"; end
- end
- function observe()
- local isBlock, blockData = turtle.inspect();
- local str = "";
- if not isBlock then str = str.."Not a block\n"; end
- if blockData.name then str = str..blockData.name.."\n"; end
- if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
- return str;
- end
- function observeUp()
- local isBlock, blockData = turtle.inspectUp();
- local str = "";
- if not isBlock then str = str.."Not a block\n"; end
- if blockData.name then str = str..blockData.name.."\n"; end
- if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
- return str;
- end
- function observeDown()
- local isBlock, blockData = turtle.inspectDown();
- local str = "";
- if not isBlock then str = str.."Not a block\n"; end
- if blockData.name then str = str..blockData.name.."\n"; end
- if blockData.state then str = str..textutils.serializeJSON(blockData.state, true).."\n"; end
- return str;
- end
- local directionKeys = {
- [87] = 1, --forward
- [32] = 2, --up
- [340] = 3, --down
- [83] = 4, --back
- [65] = 5, --left
- [68] = 6, --right
- };
- local actions = {
- [0] = {"Move", turtle.forward, turtle.up, turtle.down, turtle.back, turtle.turnLeft, turtle.turnRight},
- [85] = {"Dig (forward/up/down)", turtle.dig, turtle.digUp, turtle.digDown}, -- U
- [73] = {"Place block (forward/up/down)", turtle.place, turtle.placeUp, turtle.placeDown}, -- I
- [82] = {"Refuel (Non-directional)", turtle.refuel}, -- R
- [57] = {"Change selected slot (forward/back)", selectAndSendSlot, deselectAndSendSlot}, -- 9
- [80] = {"Get block details (forward/up/down)", observe, observeUp, observeDown}, -- P
- [79] = {"Pick up item (forward/up/down)", turtle.suck, turtle.suckUp, turtle.suckDown}, -- O
- [75] = {"Drop selected item (forward/up/down)", turtle.drop, turtle.dropUp, turtle.dropDown}, -- K
- [56] = {"Equip selected item (left/right)",turtle.equipRight, turtle.equipLeft}, -- 8
- [74] = {"Attack (forward/up/down)", turtle.attack, turtle.attackUp, turtle.attackDown}, -- J
- }
- function runAction(n, dir)
- if actions[n] then
- if #actions[n] == 2 then return actions[n][2](); end
- if #actions[n] == 3 then return actions[n][({2, 2, 3, 3, 3, 2})[dir]](); end
- if #actions[n] == 4 then return actions[n][({2, 3, 4, 4, 3, 2})[dir]](); end
- if #actions[n] == 7 then return actions[n][dir+1](); end
- else
- return "Key not mapped to an action (redundancy check)";
- end
- end
- peripheral.find("modem", rednet.open);
- print("Reciever started - Connect to ID", os.computerID());
- while true do
- id, message = rednet.receive();
- cmdKey = tonumber(message);
- if directionKeys[cmdKey] ~= nil then -- Do the current action
- local msg = tostring(runAction(currActKey, directionKeys[cmdKey]));
- actionUsed = true;
- if msg ~= "true" then
- rednet.send(id, msg);
- else
- rednet.send(id, "");
- end
- else
- if cmdKey == 0 then -- Reset the selected action
- if not actionUsed and currActKey ~= 0 then
- rednet.send(id, ("%s is mapped to %s"):format((keys.getName(currActKey)):upper(), actions[currActKey][1]));
- else
- rednet.send(id, "");
- end
- currActKey = 0;
- actionUsed = false;
- else -- Select an action
- if actions[cmdKey] then
- currActKey = cmdKey;
- actionUsed = false;
- rednet.send(id, "");
- else
- currActKey = 0;
- rednet.send(id, "Key not mapped to an action");
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement