Advertisement
NanoBob

NanoTurtle 4.0

Dec 29th, 2014
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.98 KB | None | 0 0
  1. local sides={"top","bottom","left","right","front","back"}
  2. local receiver=nil
  3. local availableClients={}
  4. local thisId=os.getComputerID()
  5. local connectedID=nil
  6. local listOptions={}
  7. local selectedOption=1
  8. local screenX,screenY=term.getSize()
  9.  
  10. local APIs={
  11. ["turtle"]=turtle,
  12. ["redstone"]=redstone,
  13. ["term"]=term,
  14. ["io"]=io,
  15. ["os"]=os,
  16. ["rednet"]=rednet,
  17. }
  18.  
  19. local commands={
  20.     ["w"]={{"turtle","forward"},},
  21.     ["a"]={{"turtle","turnLeft"},{"turtle","forward"},},
  22.     ["s"]={{"turtle","back"},},
  23.     ["d"]={{"turtle","turnRight"},{"turtle","forward"},},
  24.     ["up"]={{"turtle","up"},},
  25.     ["down"]={{"turtle","down"},},
  26.     ["refuel"]={{"turtle","refuel"},},
  27.     ["attack"]={{"turtle","attack"},},
  28.     ["attackUp"]={{"turtle","attackUp"},},
  29.     ["attackDown"]={{"turtle","attackDown"},},
  30.     ["place"]={{"turtle","place"},},
  31.     ["placeUp"]={{"turtle","placeUp"},},
  32.     ["placeDown"]={{"turtle","placeDown"},},
  33.     ["dig"]={{"turtle","dig"},},
  34.     ["digDown"]={{"turtle","digDown"},},
  35.     ["digUp"]={{"turtle","digUp"},},
  36.     ["dig"]={{"turtle","dig"},},
  37.     ["breakUp"]={{"turtle","breakUp"},},
  38.     ["breakDown"]={{"turtle","breakDown"},},
  39.     ["drop"]={{"turtle","drop"},},
  40.     ["dropUp"]={{"turtle","dropUp"},},
  41.     ["dropDown"]={{"turtle","dropDown"},},
  42.     ["refuel"]={{"turtle","refuel"},},
  43. }
  44.  
  45. term.clear()
  46.  
  47. for id,side in ipairs(sides) do
  48.     if peripheral.getType(side) == "modem" then
  49.         rednet.open(side)
  50.     end
  51. end
  52.  
  53. function drawOptionList(x,y,title)
  54.     for i=x,screenX do
  55.         for p=y,#listOptions do
  56.             term.setCursorPos(i,p+1)
  57.             term.write(" ")
  58.         end
  59.     end
  60.     term.setCursorPos(x+3,y)
  61.     term.write(title)
  62.     for id,data in pairs(listOptions) do
  63.         if selectedOption==id then
  64.             term.setCursorPos(x,y+id)  
  65.             term.write(">")
  66.         end
  67.         term.setCursorPos(x+3,y+id)
  68.         term.write(data)
  69.     end
  70. end
  71.  
  72. function addCommandGUI()
  73.     term.clear()
  74.     term.setCursorPos(1,1)
  75.     term.write("New command : ")
  76.     local command=read()
  77.     term.setCursorPos(1,2)
  78.     print("How many functions do you want to call when using this command?")
  79.     term.setCursorPos(1,5)
  80.     term.write("Count : ")
  81.     local count=read()
  82.     local allFunctions={}
  83.     for i=1,tonumber(count) do
  84.         term.clear()
  85.         term.setCursorPos(1,1)
  86.         print("What API is the function in you want to call")
  87.         term.setCursorPos(1,3)
  88.         term.write("API : ")
  89.         local API=read()
  90.         term.setCursorPos(1,5)
  91.         print("What is the function you want to call")
  92.         term.setCursorPos(1,7)
  93.         term.write("Function : ")
  94.         local toCallFunction=read()
  95.         allFunctions[#allFunctions+1]={API,toCallFunction}
  96.     end
  97.     newFunctionString='["'..command..'"]={'
  98.     for id,functionInfo in ipairs(allFunctions) do
  99.         thisFunctionString='{"'..functionInfo[1]..'","'..functionInfo[2]..'"},'
  100.         newFunctionString=newFunctionString..thisFunctionString
  101.     end
  102.     newFunctionString=newFunctionString.."},"
  103.     addCommandToTable(newFunctionString,command,allFunctions)
  104.     return newFunctionString,command,allFunctions
  105. end
  106.  
  107. function addCommandToTable(newFunctionString,command,commandTable)
  108.     commands[command]=commandTable
  109.     local currentPath=shell.getRunningProgram()
  110.     local thisFile=fs.open(currentPath,"r")
  111.     local fullFile=thisFile.readAll()
  112.     thisFile.close()
  113.     local newFile=string.gsub(fullFile,'local commands={',"local commands={\n    "..newFunctionString,1)
  114.     local thisFile=fs.open(currentPath,"w")
  115.     thisFile.write(newFile)
  116.     thisFile.close()
  117. end
  118.  
  119. function findTransmitter()
  120.     term.clear()
  121.     term.setCursorPos(1,1)
  122.     term.write("Pinging for a transmitter..")
  123.     rednet.broadcast(thisId)
  124.     repeat
  125.         timer=os.startTimer(3)
  126.         local event,id,message=os.pullEvent()
  127.         if event=="rednet_message" then
  128.             if message=="Link" then
  129.                 connectedID=id
  130.                 term.setCursorPos(1,1)
  131.                 term.write("Found connection : "..connectedID)
  132.             elseif message=="Possible link" then
  133.                 repeat
  134.                     possibleLinkID=id
  135.                     local event,id,message=os.pullEvent("rednet_message")
  136.                     if event=="rednet_message" and id==possibleLinkID and message=="Link" then
  137.                         connectedID=id
  138.                     elseif event=="rednet_message" and id==possibleLinkID and message=="Quit link" then
  139.                         possibleLinkID=nil
  140.                     end                
  141.                 until connectedID~=nil or possibleLinkID==nil
  142.             end
  143.         elseif event=="timer" then
  144.             rednet.broadcast(thisId)
  145.         end
  146.     until connectedID~=nil
  147.     term.clear()
  148.     term.setCursorPos(1,1)
  149.     term.write("Found connection : "..connectedID)
  150.     receiveCommands()
  151. end
  152.  
  153. function findReceiver()
  154.     repeat
  155.         local event,id,message=os.pullEvent()
  156.         if event=="rednet_message" then
  157.             idExists=false
  158.             for __,data in ipairs(listOptions) do
  159.                 if data==id then
  160.                     idExists=true
  161.                 end
  162.             end
  163.             if idExists==false then
  164.                 listOptions[#listOptions+1]=tonumber(id)
  165.                 drawOptionList(2,1,"Possible links")
  166.                 rednet.send(id,"Possible link")
  167.             else
  168.                 rednet.send(id,"Possible link")        
  169.             end
  170.         elseif event=="key" then
  171.             if id==208 then --down
  172.                 if selectedOption+1<=#listOptions then
  173.                     selectedOption=selectedOption+1
  174.                     drawOptionList(2,1,"Possible links")
  175.                 else
  176.                     selectedOption=1
  177.                     drawOptionList(2,1,"Possible links")
  178.                 end
  179.             elseif id==200 then --up
  180.                 if selectedOption-1>=1 then
  181.                     selectedOption=selectedOption-1
  182.                     drawOptionList(2,1,"Possible links")
  183.                 else
  184.                     selectedOption=#listOptions
  185.                     drawOptionList(2,1,"Possible links")
  186.                 end
  187.             elseif id==28 then  --enter
  188.                 for __,id in pairs(listOptions) do
  189.                     rednet.send(id,"Quit link")                
  190.                 end
  191.                 connectedID=listOptions[selectedOption]
  192.                 rednet.send(connectedID,"Link")
  193.             end
  194.         elseif event=="redstone" then
  195.             error("Redstone signal received")
  196.         end
  197.     until connectedID~=nil
  198.     sendCommands()
  199. end
  200.  
  201. function receiveCommands()
  202.     repeat
  203.         local event,id,message=os.pullEvent()
  204.         if event=="rednet_message" and id==connectedID then
  205.             term.clear()
  206.             term.setCursorPos(1,1)
  207.             term.write("Received message : "..message)
  208.            
  209.             if string.find(message,"newCMD")==nil then
  210.                 command=splitString(message," ",1)
  211.                 interval=tonumber(splitString(message," ",2))
  212.                 if interval==nil then
  213.                     interval=1
  214.                 end
  215.                 term.setCursorPos(1,2)
  216.                 term.write(command.." X "..interval)
  217.                 term.setCursorPos(1,3)
  218.                 functionsData=commands[command]
  219.             end
  220.             if string.find(message,"newCMD")~=nil then
  221.                 newCMDLine=splitString(message,"~",2)
  222.                 newCommand=splitString(message,"~",3)
  223.                 newFunctionTable=splitString(message,"~",4)
  224.                 newCommandFunction=textutils.unserialize(newFunctionTable)
  225.                 addCommandToTable(newCMDLine,newCommand,newCommandFunction)
  226.             elseif command=="stop" then
  227.                 rednet.send(id,"true")
  228.             elseif functionsData==nil then
  229.                 rednet.send(id,"false")
  230.             else
  231.                 term.setCursorPos(1,3)
  232.                 for i=1,interval do
  233.                     for id,functionData in ipairs(functionsData) do
  234.                         if i==1 then
  235.                             print(functionData[1]..","..functionData[2])
  236.                         end
  237.                         local API=APIs[functionData[1]]
  238.                         local toCallFunction=API[functionData[2]]
  239.                         toCallFunction()
  240.                     end
  241.                 end
  242.                 rednet.send(id,"true")
  243.             end
  244.             term.setCursorPos(1,3)
  245.         end
  246.     until message=="stop"
  247. end
  248.  
  249. function sendCommands()
  250.     repeat
  251.         term.clear()
  252.         term.setCursorPos(3,1)
  253.         term.write("Command : ")
  254.         input=read()
  255.         if input=="help" then
  256.             term.setCursorPos(3,2)
  257.             for id,data in pairs(commands) do
  258.                 term.write(id)
  259.                 x,y=term.getCursorPos()
  260.                 term.setCursorPos(15,y)
  261.                 for __,functionInfo in ipairs(data) do
  262.                     term.write(" : "..functionInfo[1].."."..functionInfo[2])
  263.                 end
  264.                 x,y=term.getCursorPos()
  265.                 term.setCursorPos(3,y+1)
  266.                 x,y=term.getCursorPos()
  267.                 if y>=screenY then
  268.                     term.setCursorPos(3,screenY)
  269.                     term.write("Press any key to continue..")
  270.                     os.pullEvent("key")
  271.                     term.clear()
  272.                     term.setCursorPos(3,1)
  273.                 end
  274.                 sleep(0.1)
  275.             end
  276.             x,y=term.getCursorPos()
  277.             term.setCursorPos(3,y)
  278.             term.write("new command")
  279.             x,y=term.getCursorPos()
  280.             term.setCursorPos(15,y)
  281.             term.write(" : ".."Allows you to add a new command.")
  282.             x,y=term.getCursorPos()
  283.             term.setCursorPos(3,screenY)
  284.             term.write("Press any key to continue..")
  285.             os.pullEvent("key")
  286.             sleep(0.1)
  287.         elseif input=="new command" then
  288.             local newFunctionString,command,functionTable=addCommandGUI()
  289.             rednet.send(connectedID,"newCMD~"..newFunctionString.."~"..command.."~"..textutils.serialise(functionTable))
  290.        
  291.         else
  292.             rednet.send(connectedID,input)
  293.             repeat
  294.                 timer=os.startTimer(5)
  295.                 event,id,message=os.pullEvent()
  296.                 if message=="false" then
  297.                     term.setCursorPos(3,2) 
  298.                     term.write("Incorrect command.")
  299.                     sleep(0.4)
  300.                 end
  301.             until id==connectedID or id==timer
  302.         end
  303.     until input=="stop"
  304. end
  305.  
  306. function splitString(sourceString,splittingChar,index)
  307.     results={}
  308.     currentWord=""
  309.     splittingChar=string.sub(splittingChar,1,1)
  310.     for i=1,string.len(sourceString) do
  311.         letter=string.sub(sourceString,i,i)
  312.         if letter==splittingChar then
  313.             results[#results+1]=currentWord
  314.             currentWord=""
  315.         else
  316.             currentWord=currentWord..letter
  317.         end
  318.     end
  319.     results[#results+1]=currentWord
  320.     return results[index]
  321. end
  322.  
  323. if turtle==nil then
  324.     receiver=false
  325.     findReceiver()
  326. else
  327.     receiver=true
  328.     findTransmitter()
  329. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement