Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sides={"top","bottom","left","right","front","back"}
- local receiver=nil
- local availableClients={}
- local thisId=os.getComputerID()
- local connectedID=nil
- local listOptions={}
- local selectedOption=1
- local screenX,screenY=term.getSize()
- local APIs={
- ["turtle"]=turtle,
- ["redstone"]=redstone,
- ["term"]=term,
- ["io"]=io,
- ["os"]=os,
- ["rednet"]=rednet,
- }
- local commands={
- ["w"]={{"turtle","forward"},},
- ["a"]={{"turtle","turnLeft"},{"turtle","forward"},},
- ["s"]={{"turtle","back"},},
- ["d"]={{"turtle","turnRight"},{"turtle","forward"},},
- ["up"]={{"turtle","up"},},
- ["down"]={{"turtle","down"},},
- ["refuel"]={{"turtle","refuel"},},
- ["attack"]={{"turtle","attack"},},
- ["attackUp"]={{"turtle","attackUp"},},
- ["attackDown"]={{"turtle","attackDown"},},
- ["place"]={{"turtle","place"},},
- ["placeUp"]={{"turtle","placeUp"},},
- ["placeDown"]={{"turtle","placeDown"},},
- ["dig"]={{"turtle","dig"},},
- ["digDown"]={{"turtle","digDown"},},
- ["digUp"]={{"turtle","digUp"},},
- ["dig"]={{"turtle","dig"},},
- ["breakUp"]={{"turtle","breakUp"},},
- ["breakDown"]={{"turtle","breakDown"},},
- ["drop"]={{"turtle","drop"},},
- ["dropUp"]={{"turtle","dropUp"},},
- ["dropDown"]={{"turtle","dropDown"},},
- ["refuel"]={{"turtle","refuel"},},
- }
- term.clear()
- for id,side in ipairs(sides) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- function drawOptionList(x,y,title)
- for i=x,screenX do
- for p=y,#listOptions do
- term.setCursorPos(i,p+1)
- term.write(" ")
- end
- end
- term.setCursorPos(x+3,y)
- term.write(title)
- for id,data in pairs(listOptions) do
- if selectedOption==id then
- term.setCursorPos(x,y+id)
- term.write(">")
- end
- term.setCursorPos(x+3,y+id)
- term.write(data)
- end
- end
- function addCommandGUI()
- term.clear()
- term.setCursorPos(1,1)
- term.write("New command : ")
- local command=read()
- term.setCursorPos(1,2)
- print("How many functions do you want to call when using this command?")
- term.setCursorPos(1,5)
- term.write("Count : ")
- local count=read()
- local allFunctions={}
- for i=1,tonumber(count) do
- term.clear()
- term.setCursorPos(1,1)
- print("What API is the function in you want to call")
- term.setCursorPos(1,3)
- term.write("API : ")
- local API=read()
- term.setCursorPos(1,5)
- print("What is the function you want to call")
- term.setCursorPos(1,7)
- term.write("Function : ")
- local toCallFunction=read()
- allFunctions[#allFunctions+1]={API,toCallFunction}
- end
- newFunctionString='["'..command..'"]={'
- for id,functionInfo in ipairs(allFunctions) do
- thisFunctionString='{"'..functionInfo[1]..'","'..functionInfo[2]..'"},'
- newFunctionString=newFunctionString..thisFunctionString
- end
- newFunctionString=newFunctionString.."},"
- addCommandToTable(newFunctionString,command,allFunctions)
- return newFunctionString,command,allFunctions
- end
- function addCommandToTable(newFunctionString,command,commandTable)
- commands[command]=commandTable
- local currentPath=shell.getRunningProgram()
- local thisFile=fs.open(currentPath,"r")
- local fullFile=thisFile.readAll()
- thisFile.close()
- local newFile=string.gsub(fullFile,'local commands={',"local commands={\n "..newFunctionString,1)
- local thisFile=fs.open(currentPath,"w")
- thisFile.write(newFile)
- thisFile.close()
- end
- function findTransmitter()
- term.clear()
- term.setCursorPos(1,1)
- term.write("Pinging for a transmitter..")
- rednet.broadcast(thisId)
- repeat
- timer=os.startTimer(3)
- local event,id,message=os.pullEvent()
- if event=="rednet_message" then
- if message=="Link" then
- connectedID=id
- term.setCursorPos(1,1)
- term.write("Found connection : "..connectedID)
- elseif message=="Possible link" then
- repeat
- possibleLinkID=id
- local event,id,message=os.pullEvent("rednet_message")
- if event=="rednet_message" and id==possibleLinkID and message=="Link" then
- connectedID=id
- elseif event=="rednet_message" and id==possibleLinkID and message=="Quit link" then
- possibleLinkID=nil
- end
- until connectedID~=nil or possibleLinkID==nil
- end
- elseif event=="timer" then
- rednet.broadcast(thisId)
- end
- until connectedID~=nil
- term.clear()
- term.setCursorPos(1,1)
- term.write("Found connection : "..connectedID)
- receiveCommands()
- end
- function findReceiver()
- repeat
- local event,id,message=os.pullEvent()
- if event=="rednet_message" then
- idExists=false
- for __,data in ipairs(listOptions) do
- if data==id then
- idExists=true
- end
- end
- if idExists==false then
- listOptions[#listOptions+1]=tonumber(id)
- drawOptionList(2,1,"Possible links")
- rednet.send(id,"Possible link")
- else
- rednet.send(id,"Possible link")
- end
- elseif event=="key" then
- if id==208 then --down
- if selectedOption+1<=#listOptions then
- selectedOption=selectedOption+1
- drawOptionList(2,1,"Possible links")
- else
- selectedOption=1
- drawOptionList(2,1,"Possible links")
- end
- elseif id==200 then --up
- if selectedOption-1>=1 then
- selectedOption=selectedOption-1
- drawOptionList(2,1,"Possible links")
- else
- selectedOption=#listOptions
- drawOptionList(2,1,"Possible links")
- end
- elseif id==28 then --enter
- for __,id in pairs(listOptions) do
- rednet.send(id,"Quit link")
- end
- connectedID=listOptions[selectedOption]
- rednet.send(connectedID,"Link")
- end
- elseif event=="redstone" then
- error("Redstone signal received")
- end
- until connectedID~=nil
- sendCommands()
- end
- function receiveCommands()
- repeat
- local event,id,message=os.pullEvent()
- if event=="rednet_message" and id==connectedID then
- term.clear()
- term.setCursorPos(1,1)
- term.write("Received message : "..message)
- if string.find(message,"newCMD")==nil then
- command=splitString(message," ",1)
- interval=tonumber(splitString(message," ",2))
- if interval==nil then
- interval=1
- end
- term.setCursorPos(1,2)
- term.write(command.." X "..interval)
- term.setCursorPos(1,3)
- functionsData=commands[command]
- end
- if string.find(message,"newCMD")~=nil then
- newCMDLine=splitString(message,"~",2)
- newCommand=splitString(message,"~",3)
- newFunctionTable=splitString(message,"~",4)
- newCommandFunction=textutils.unserialize(newFunctionTable)
- addCommandToTable(newCMDLine,newCommand,newCommandFunction)
- elseif command=="stop" then
- rednet.send(id,"true")
- elseif functionsData==nil then
- rednet.send(id,"false")
- else
- term.setCursorPos(1,3)
- for i=1,interval do
- for id,functionData in ipairs(functionsData) do
- if i==1 then
- print(functionData[1]..","..functionData[2])
- end
- local API=APIs[functionData[1]]
- local toCallFunction=API[functionData[2]]
- toCallFunction()
- end
- end
- rednet.send(id,"true")
- end
- term.setCursorPos(1,3)
- end
- until message=="stop"
- end
- function sendCommands()
- repeat
- term.clear()
- term.setCursorPos(3,1)
- term.write("Command : ")
- input=read()
- if input=="help" then
- term.setCursorPos(3,2)
- for id,data in pairs(commands) do
- term.write(id)
- x,y=term.getCursorPos()
- term.setCursorPos(15,y)
- for __,functionInfo in ipairs(data) do
- term.write(" : "..functionInfo[1].."."..functionInfo[2])
- end
- x,y=term.getCursorPos()
- term.setCursorPos(3,y+1)
- x,y=term.getCursorPos()
- if y>=screenY then
- term.setCursorPos(3,screenY)
- term.write("Press any key to continue..")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(3,1)
- end
- sleep(0.1)
- end
- x,y=term.getCursorPos()
- term.setCursorPos(3,y)
- term.write("new command")
- x,y=term.getCursorPos()
- term.setCursorPos(15,y)
- term.write(" : ".."Allows you to add a new command.")
- x,y=term.getCursorPos()
- term.setCursorPos(3,screenY)
- term.write("Press any key to continue..")
- os.pullEvent("key")
- sleep(0.1)
- elseif input=="new command" then
- local newFunctionString,command,functionTable=addCommandGUI()
- rednet.send(connectedID,"newCMD~"..newFunctionString.."~"..command.."~"..textutils.serialise(functionTable))
- else
- rednet.send(connectedID,input)
- repeat
- timer=os.startTimer(5)
- event,id,message=os.pullEvent()
- if message=="false" then
- term.setCursorPos(3,2)
- term.write("Incorrect command.")
- sleep(0.4)
- end
- until id==connectedID or id==timer
- end
- until input=="stop"
- end
- function splitString(sourceString,splittingChar,index)
- results={}
- currentWord=""
- splittingChar=string.sub(splittingChar,1,1)
- for i=1,string.len(sourceString) do
- letter=string.sub(sourceString,i,i)
- if letter==splittingChar then
- results[#results+1]=currentWord
- currentWord=""
- else
- currentWord=currentWord..letter
- end
- end
- results[#results+1]=currentWord
- return results[index]
- end
- if turtle==nil then
- receiver=false
- findReceiver()
- else
- receiver=true
- findTransmitter()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement