Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- HPWebcamAble presents...
- ControlMe
- --=== Description ===--
- This program lets you control a turtle from a pocket computer!
- This version of the program is for the TURTLE
- Get the Pocket Computer version here: pastebin.com/mTGccYbM
- --=== Installation ===--
- Pastebin Code: zjn3E5LS
- To download a file from pastebin, run this command in a computer:
- pastebin get <code> <file name>
- --=== Update History ===--
- The pastebin will always have the most recent version
- |1.0|
- -Release
- ]]
- --=== Variables ===--
- local args = {...}
- local protocalName = "controlme"
- local computerID
- local w,h = term.getSize()
- --=== Functions ===--
- local function color(text,back)
- local temp = text and term.setTextColor(text) or nil
- temp = back and term.setBackgroundColor(back) or nil
- end
- local function printC(text,y)
- if type(text) ~= "string" or type(y) ~= "number" then error("expected string,number, got "..type(text)..","..type(y),2) end
- local lenght = #text
- local start = math.floor((w-lenght)/2)+1
- term.setCursorPos(start,y)
- term.write(text)
- return start,start+lenght
- end
- local function checkMessage(event,skipTableCheck,skipIDCheck)
- return event[4] == protocalName and (skipTableCheck or type(event[3]) == "table") and (skipIDCheck or event[2] == computerID)
- end
- local function message(message)
- rednet.send(computerID,message,protocalName)
- end
- local function broadcast(message)
- rednet.broadcast(message,protocalName)
- end
- local function getModem()
- for a,b in pairs(rs.getSides()) do
- if peripheral.getType(b) == "modem" and peripheral.call(b,"isWireless") then
- return b
- end
- end
- end
- local function getFreeSlot()
- for i = 1, 16 do
- local result = turtle.getItemCount(i)
- if result == 0 then
- return i
- end
- end
- return false
- end
- local function getTool(side)
- if peripheral.getType(side) then
- return peripheral.getType(side)
- end
- local slot = getFreeSlot()
- local curSlot = turtle.getSelectedSlot()
- local equipSide = "equip"..string.sub(side,1,1):upper()..string.sub(side,2)
- if slot then
- turtle.select(slot)
- turtle[equipSide]()
- local item = turtle.getItemDetail()
- turtle[equipSide]()
- turtle.select(curSlot)
- return (item~=nil and item.name or "none")
- end
- return "failed_no_slot"
- end
- --=== Program ===--
- if not turtle then
- printError("This program requires a turtle!")
- return
- elseif args[1] == "help" then
- print("ControlMe")
- print("By HPWebcamAble")
- print("")
- print("Use this program to remotly control your turtle!")
- print("Start this program on the turtle and the corresponding program on a pockect computer and control away!")
- end
- do
- local modemSide = getModem()
- if modemSide then
- rednet.open(modemSide)
- else
- printError("This program requires a wireless modem")
- print("Place one in the turtle's inventory and use the 'equip' program")
- return
- end
- end
- color(colors.white,colors.black)
- term.clear()
- printC("Waiting for connection...",5)
- printC("My ID is "..os.getComputerID(),6)
- while true do
- local event = {os.pullEventRaw()}
- if event[1] == "terminate" then
- term.clear() term.setCursorPos(1,1) print("Program terminated")
- return
- elseif event[1] == "rednet_message" then
- if checkMessage(event,true,true) then
- if event[3] == "turtle_locate" then
- broadcast("turtle_here")
- elseif type(event[3]) == "table" and event[3].action == "connect" and event[3].id == os.getComputerID() then
- computerID = event[2]
- message("connected")
- break
- end
- end
- end
- end
- term.clear()
- printC("ControlMe is running!",5)
- printC("Connected to ID "..computerID,6)
- local function heartbeat()
- local timer = os.startTimer(5)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "rednet_message" then
- if checkMessage(event,true) then
- if event[3] == "ping" then
- message("pong")
- timer = os.startTimer(5)
- end
- end
- elseif event[1] == "timer" and event[2] == timer then
- color(colors.white,colors.black) term.setCursorPos(1,1) term.clear() print("Lost contact with Pocket Computer!")
- error("FORCEQUIT")
- end
- end
- end
- local function main()
- while true do
- local event = {os.pullEvent()}
- if event[1] == "rednet_message" and checkMessage(event) then
- local msg = event[3]
- if msg.action == "execute" then
- local func = loadstring("return "..msg.func)
- local result = {pcall(func)}
- message({action = "completed",result = select(2,unpack(result))})
- elseif msg.action == "getLeftTool" then
- message({action="getLeftTool",result=getTool("left")})
- elseif msg.action == "getRightTool" then
- message({action="getRightTool",result=getTool("right")})
- end
- end
- end
- end
- local function run()
- parallel.waitForAny(main,heartbeat)
- end
- local state,err = pcall(run)
- if err and not err:find("Terminated") and not err:find("FORCEQUIT") then
- color(colors.white,colors.black)
- term.clear()
- printC("An error occured:",1)
- term.setCursorPos(1,3)
- print(err)
- elseif not err:find("FORCEQUIT") then
- color(colors.white,colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement