SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ | |
| 2 | HPWebcamAble presents... | |
| 3 | ControlMe | |
| 4 | ||
| 5 | --=== Description ===-- | |
| 6 | This program lets you control a turtle from a pocket computer! | |
| 7 | ||
| 8 | This version of the program is for the TURTLE | |
| 9 | Get the Pocket Computer version here: pastebin.com/mTGccYbM | |
| 10 | ||
| 11 | ||
| 12 | --=== Installation ===-- | |
| 13 | Pastebin Code: zjn3E5LS | |
| 14 | ||
| 15 | To download a file from pastebin, run this command in a computer: | |
| 16 | pastebin get <code> <file name> | |
| 17 | ||
| 18 | ||
| 19 | --=== Update History ===-- | |
| 20 | The pastebin will always have the most recent version | |
| 21 | ||
| 22 | |1.0| | |
| 23 | -Release | |
| 24 | ]] | |
| 25 | ||
| 26 | --=== Variables ===-- | |
| 27 | local args = {...}
| |
| 28 | local protocalName = "controlme" | |
| 29 | local computerID | |
| 30 | local w,h = term.getSize() | |
| 31 | ||
| 32 | ||
| 33 | --=== Functions ===-- | |
| 34 | local function color(text,back) | |
| 35 | local temp = text and term.setTextColor(text) or nil | |
| 36 | temp = back and term.setBackgroundColor(back) or nil | |
| 37 | end | |
| 38 | ||
| 39 | local function printC(text,y) | |
| 40 | if type(text) ~= "string" or type(y) ~= "number" then error("expected string,number, got "..type(text)..","..type(y),2) end
| |
| 41 | local lenght = #text | |
| 42 | local start = math.floor((w-lenght)/2)+1 | |
| 43 | term.setCursorPos(start,y) | |
| 44 | term.write(text) | |
| 45 | return start,start+lenght | |
| 46 | end | |
| 47 | ||
| 48 | local function checkMessage(event,skipTableCheck,skipIDCheck) | |
| 49 | return event[4] == protocalName and (skipTableCheck or type(event[3]) == "table") and (skipIDCheck or event[2] == computerID) | |
| 50 | end | |
| 51 | ||
| 52 | local function message(message) | |
| 53 | rednet.send(computerID,message,protocalName) | |
| 54 | end | |
| 55 | ||
| 56 | local function broadcast(message) | |
| 57 | rednet.broadcast(message,protocalName) | |
| 58 | end | |
| 59 | ||
| 60 | local function getModem() | |
| 61 | for a,b in pairs(rs.getSides()) do | |
| 62 | if peripheral.getType(b) == "modem" and peripheral.call(b,"isWireless") then | |
| 63 | return b | |
| 64 | end | |
| 65 | end | |
| 66 | end | |
| 67 | ||
| 68 | local function getFreeSlot() | |
| 69 | for i = 1, 16 do | |
| 70 | local result = turtle.getItemCount(i) | |
| 71 | if result == 0 then | |
| 72 | return i | |
| 73 | end | |
| 74 | end | |
| 75 | return false | |
| 76 | end | |
| 77 | ||
| 78 | local function getTool(side) | |
| 79 | if peripheral.getType(side) then | |
| 80 | return peripheral.getType(side) | |
| 81 | end | |
| 82 | local slot = getFreeSlot() | |
| 83 | local curSlot = turtle.getSelectedSlot() | |
| 84 | local equipSide = "equip"..string.sub(side,1,1):upper()..string.sub(side,2) | |
| 85 | if slot then | |
| 86 | turtle.select(slot) | |
| 87 | turtle[equipSide]() | |
| 88 | local item = turtle.getItemDetail() | |
| 89 | turtle[equipSide]() | |
| 90 | turtle.select(curSlot) | |
| 91 | return (item~=nil and item.name or "none") | |
| 92 | end | |
| 93 | return "failed_no_slot" | |
| 94 | end | |
| 95 | ||
| 96 | --=== Program ===-- | |
| 97 | if not turtle then | |
| 98 | printError("This program requires a turtle!")
| |
| 99 | return | |
| 100 | elseif args[1] == "help" then | |
| 101 | print("ControlMe")
| |
| 102 | print("By HPWebcamAble")
| |
| 103 | print("")
| |
| 104 | print("Use this program to remotly control your turtle!")
| |
| 105 | print("Start this program on the turtle and the corresponding program on a pockect computer and control away!")
| |
| 106 | end | |
| 107 | ||
| 108 | do | |
| 109 | local modemSide = getModem() | |
| 110 | if modemSide then | |
| 111 | rednet.open(modemSide) | |
| 112 | else | |
| 113 | printError("This program requires a wireless modem")
| |
| 114 | print("Place one in the turtle's inventory and use the 'equip' program")
| |
| 115 | return | |
| 116 | end | |
| 117 | end | |
| 118 | color(colors.white,colors.black) | |
| 119 | term.clear() | |
| 120 | printC("Waiting for connection...",5)
| |
| 121 | printC("My ID is "..os.getComputerID(),6)
| |
| 122 | ||
| 123 | while true do | |
| 124 | local event = {os.pullEventRaw()}
| |
| 125 | if event[1] == "terminate" then | |
| 126 | term.clear() term.setCursorPos(1,1) print("Program terminated")
| |
| 127 | return | |
| 128 | elseif event[1] == "rednet_message" then | |
| 129 | if checkMessage(event,true,true) then | |
| 130 | if event[3] == "turtle_locate" then | |
| 131 | broadcast("turtle_here")
| |
| 132 | elseif type(event[3]) == "table" and event[3].action == "connect" and event[3].id == os.getComputerID() then | |
| 133 | computerID = event[2] | |
| 134 | message("connected")
| |
| 135 | break | |
| 136 | end | |
| 137 | end | |
| 138 | end | |
| 139 | end | |
| 140 | ||
| 141 | term.clear() | |
| 142 | printC("ControlMe is running!",5)
| |
| 143 | printC("Connected to ID "..computerID,6)
| |
| 144 | ||
| 145 | local function heartbeat() | |
| 146 | ||
| 147 | local timer = os.startTimer(5) | |
| 148 | while true do | |
| 149 | local event = {os.pullEvent()}
| |
| 150 | if event[1] == "rednet_message" then | |
| 151 | if checkMessage(event,true) then | |
| 152 | if event[3] == "ping" then | |
| 153 | message("pong")
| |
| 154 | timer = os.startTimer(5) | |
| 155 | end | |
| 156 | end | |
| 157 | elseif event[1] == "timer" and event[2] == timer then | |
| 158 | color(colors.white,colors.black) term.setCursorPos(1,1) term.clear() print("Lost contact with Pocket Computer!")
| |
| 159 | error("FORCEQUIT")
| |
| 160 | end | |
| 161 | end | |
| 162 | ||
| 163 | end | |
| 164 | ||
| 165 | local function main() | |
| 166 | ||
| 167 | while true do | |
| 168 | local event = {os.pullEvent()}
| |
| 169 | if event[1] == "rednet_message" and checkMessage(event) then | |
| 170 | local msg = event[3] | |
| 171 | if msg.action == "execute" then | |
| 172 | local func = loadstring("return "..msg.func)
| |
| 173 | local result = {pcall(func)}
| |
| 174 | message({action = "completed",result = select(2,unpack(result))})
| |
| 175 | elseif msg.action == "getLeftTool" then | |
| 176 | message({action="getLeftTool",result=getTool("left")})
| |
| 177 | elseif msg.action == "getRightTool" then | |
| 178 | message({action="getRightTool",result=getTool("right")})
| |
| 179 | end | |
| 180 | end | |
| 181 | end | |
| 182 | ||
| 183 | end | |
| 184 | ||
| 185 | local function run() | |
| 186 | parallel.waitForAny(main,heartbeat) | |
| 187 | end | |
| 188 | ||
| 189 | local state,err = pcall(run) | |
| 190 | ||
| 191 | if err and not err:find("Terminated") and not err:find("FORCEQUIT") then
| |
| 192 | color(colors.white,colors.black) | |
| 193 | term.clear() | |
| 194 | printC("An error occured:",1)
| |
| 195 | term.setCursorPos(1,3) | |
| 196 | print(err) | |
| 197 | elseif not err:find("FORCEQUIT") then
| |
| 198 | color(colors.white,colors.black) | |
| 199 | term.clear() | |
| 200 | term.setCursorPos(1,1) | |
| 201 | end |