Guest User

BAD CODE

a guest
Aug 2nd, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.64 KB | None | 0 0
  1. --[[
  2.         Turtle Command
  3.         By
  4.         Big SHiny Toys
  5. ]]--
  6.  
  7. -- start of programs
  8.  
  9. local tProgs = {
  10.     manCon = function()
  11.         while true do
  12.             local e,e1,e2,e3,e4,e5 = coroutine.yield()
  13.             if e == "char" and ManualControlstatus then
  14.                 if e1 == "w" then
  15.                     move("F")
  16.                 end
  17.                 if e1 == "s" then
  18.                     move("B")
  19.                 end
  20.                 if e1 == "a" then
  21.                     move("L")
  22.                 end
  23.                 if e1 == "d" then
  24.                     move("R")
  25.                 end
  26.                 if e1 == "q" then
  27.                     move("U")
  28.                 end
  29.                 if e1 == "e" then
  30.                     move("D")
  31.                 end
  32.             end
  33.         end
  34.     end,
  35.     location = function()
  36.         while true do
  37.             local x,x1 = coroutine.yield()
  38.             if locationS then
  39.                 if not menuStatus then
  40.                     -- term.clear()
  41.                     term.setCursorPos(1,1)
  42.                     print("LOC X:"..turX.." Y:"..turY.." Z:"..turZ.." Compas: "..compas[face].." "..face)
  43.                     if gpsX then
  44.                         print("GPS X:"..gpsX.." Y:"..gpsY.." Z:"..gpsZ)
  45.                     else
  46.                         print("gps unavalible")
  47.                     end
  48.                 end
  49.                 if x == "redstone" then
  50.                     term.setCursorPos(1,4)
  51.                     print("Redstone signal")
  52.                 end
  53.             end
  54.         end
  55.     end,
  56.     gps = function()
  57.         local pastX,pastY,pastZ,pastV = turX,turY,turZ,face
  58.         local w,w1,w2,w3,w4,w5
  59.         while true do
  60.             w,w1,w2,w3,w4,w5 = coroutine.yield()
  61.             -- if w == "char" and w1 == "t" then break end
  62.             if pastX == turX and pastY == turY and pastZ == turZ and pastV == face then
  63.             else
  64.                 gpsX,gpsY,gpsZ = gps.locate(1)
  65.                 pastX,pastY,pastZ,pastV = turX,turY,turZ,face
  66.             end
  67.         end
  68.     end,
  69.     menu = function()
  70.         while true do
  71.             coroutine.yield()
  72.             if menuStatus then
  73.                 local a = menu("return","Mineing "..tostring(running).." ","Test "..tostring(test),"Quit")
  74.                 if a == 1 then
  75.                     menuStatus = false
  76.                 end
  77.                 if a == 2 then
  78.                     if running == true then running = false
  79.                     elseif running == false then running = true
  80.                     end
  81.                 end
  82.                 if a == 3 then
  83.                     if test == true then test = false
  84.                     elseif test == false then test = true
  85.                     end
  86.                 end
  87.                 if a == 4 then
  88.                     shutdown = true
  89.                     menuStatus = false
  90.                 end
  91.             end
  92.         end
  93.     end,
  94.     wifiControll = function()
  95.         while true do
  96.             local a,a1,a2,a3,a4,a5 = coroutine.yield()
  97.             if a == "rednet_message" and wifiControll then
  98.                 local x = string.sub(a2,1,4) -- TCMD
  99.                 if x == "TCMD" then
  100.                     move(string.sub(a2,5,5))
  101.                     rednet.send(a1,textutils.serialize({turX,turY,turZ,compas[face]}))
  102.                 end
  103.             end
  104.             term.setCursorPos(1,4)
  105.             print(tostring(a).." "..tostring(a1))
  106.         end
  107.     end,
  108.     tunneler = function()
  109.         while true do
  110.             coroutine.yield()
  111.             if running then break end
  112.         end
  113.         local starting = {turX,turY,turZ,face}
  114.         local startW,startL,startH = 10,10,10
  115.         running = true
  116.         wifiControll = false
  117.         while true do
  118.             turtle.dig()
  119.             move("F")
  120.             move("R")
  121.             for i = 1,startL do
  122.                 for o = 1,math.floor(startH/3) do
  123.                     for p = 1,startW do
  124.                         turtle.dig()
  125.                         turtle.digUp()
  126.                         turtle.digDown()
  127.                         move("F")
  128.                     end
  129.                     move("R")
  130.                     move("R")
  131.                     turtle.digDown()
  132.                     for q = 1,3 do
  133.                         turtle.digUp()
  134.                         move("U")
  135.                     end
  136.                 end
  137.                 for w = 1,startH do
  138.                     turtle.digDown()
  139.                     move("D")
  140.                 end
  141.                 for w = 1,startW do
  142.                     turtle.dig()
  143.                     move("F")
  144.                 end
  145.                 move("R")
  146.                 turtle.dig()
  147.                 move("F")
  148.                 move("R")
  149.                 turtle.dig()
  150.             end
  151.         end
  152.         wifiControll = true
  153.     end,
  154.     test = function()
  155.         --[[moveADV("H",4)
  156.         while true do
  157.             sleep(2)
  158.             for i = 1,4 do
  159.                 moveADV("H",i)
  160.                 sleep(1)
  161.             end
  162.             for i = 4,1,-1 do
  163.                 moveADV("H",i)
  164.                 sleep(1)
  165.             end
  166.         end
  167.         ]]--
  168.         while true do
  169.             coroutine.yield()
  170.             if test then
  171.             goto(-24,-12,2)
  172.             goto(-18,-12,2)
  173.             goto(-18,-16,2)
  174.             goto(-24,-16,2)
  175.             end
  176.         end
  177.     end,
  178.     console = function()
  179.         while true do
  180.             coroutine.yield()
  181.             if consoleRUN then
  182.                 term.clear()
  183.                 term.setCursorPos(1,5)
  184.                 local sInput = readADV()
  185.                 if string.sub(sInput,1,3) == "run" then
  186.                     coroutine.yield("run",string.sub(sInput,5,#sInput))
  187.                 elseif string.sub(sInput,1,4) == "kill" then
  188.                     coroutine.yield("kill",string.sub(sInput,6,#sInput))
  189.                 end
  190.                 consoleRUN = false
  191.             end
  192.         end
  193.     end
  194. }
  195.  
  196. -- end of programs
  197.  
  198. -- globals
  199. gpsX,gpsY,gpsZ = nil,nil,nil
  200. turX,turY,turZ = 0,0,0
  201. compas = {"n","e","s","w"}
  202. moves = {"U","D","L","R","F","B"}
  203. face = 1 -- 1 north 2 east 3 south 4 west
  204. routines = {}
  205. routinNames = {}
  206. menuStatus = false
  207. ManualControlstatus = true
  208. shutdown = false
  209. wifiControll = true
  210. running = true
  211. test = false
  212. consoleRUN = true
  213. locationS = true
  214. -- local vars
  215. local i = 1
  216. local e,e1,e2,e3,e4,e5
  217. -- functions
  218.  
  219. function menu(...)
  220.  
  221.     local sel = 1
  222.     local list = {...}
  223.     local offX,offY = 1,1 -- change this to what ever off set you want
  224.     local curX,curY = term.getCursorPos()
  225.     local tX,tY
  226.    
  227.     while true do
  228.         if sel > #list then sel = #list end
  229.         if sel < 1 then sel = 1 end
  230.         for i = 1,#list do
  231.             term.setCursorPos(offX,offY+i-1)
  232.             if sel == i then
  233.                 print("["..list[i].."]")
  234.             else
  235.                 print(" "..list[i].." ")
  236.             end
  237.         end
  238.         while true do
  239.             tX,tY = term.getCursorPos()
  240.             local e,e1,e2,e3,e4,e5 = os.pullEvent()
  241.             term.setCursorPos(tX,tY)
  242.             if e == "key" then
  243.                 if e1 == 200 then -- up key
  244.                     sel = sel-1
  245.                     break
  246.                 end
  247.                 if e1 == 208 then -- down key
  248.                     sel = sel+1
  249.                     break
  250.                 end
  251.                 if e1 == 28 then
  252.                     term.setCursorPos(curX,curY)
  253.                     return sel
  254.                 end
  255.             end
  256.         end
  257.     end
  258. end
  259.  
  260. function goto(thisX,thisY,thisZ)
  261.     moveADV("X",thisX)
  262.     moveADV("Y",nil,thisY)
  263.     moveADV("Z",nil,nil,thisZ)
  264. end
  265.  
  266. function moveADV(ins,rep,tempY,tempZ) -- advanced instructions
  267.     if ins == "H" then
  268.         if rep > 4 or rep < 1 then return false end
  269.         if rep == face then return true end
  270.         if rep == face - 2 or rep == face + 2 then
  271.             move("R")
  272.             move("R")
  273.         else
  274.             local temp = rep-1
  275.             for p = 1,4 do
  276.                 temp = temp+1
  277.                 if temp > 4 then
  278.                     temp = 1
  279.                 end
  280.                 if face == temp then
  281.                     if p == 1 or p == 4 then
  282.                         move("R")
  283.                     end
  284.                     if p == 2 or p == 3 then
  285.                         move("L")
  286.                     end
  287.                 end
  288.             end
  289.         end
  290.     end
  291.     if ins == "X" then
  292.         if rep == turX then return true end
  293.         if rep > turX then
  294.             moveADV("H",2)
  295.         elseif rep < turX then
  296.             moveADV("H",4)
  297.         end
  298.         while rep ~= turX do
  299.             move("F")
  300.         end
  301.     end
  302.     if ins == "Y" then
  303.         if tempY == turY then return true end
  304.         if tempY > turY then
  305.             moveADV("H",1)
  306.         elseif tempY < turY then
  307.             moveADV("H",3)
  308.         end
  309.         while tempY ~= turY do
  310.             move("F")
  311.         end
  312.     end
  313.     if ins == "Z" then
  314.         if tempZ == turZ then return true end
  315.         if tempZ > turZ then
  316.             while tempZ ~= turZ do
  317.                 move("U")
  318.             end
  319.         elseif tempZ < turZ then
  320.             while tempZ ~= turZ do
  321.                 move("D")
  322.             end
  323.         end
  324.     end
  325. end
  326.  
  327. function move(ins,rep) -- low levle functions
  328.     if not ins and not rep then
  329.         return false,"error no move specified"
  330.     elseif not rep then
  331.         rep = 1
  332.     end
  333.     for i=1,rep do
  334.         if ins == "U" then -- up move
  335.             if turtle.up() then
  336.                 turZ = turZ+1
  337.             else
  338.                 return false
  339.             end
  340.         end
  341.         if ins == "D" then -- down move
  342.             if turtle.down() then
  343.                 turZ = turZ-1
  344.             else
  345.                 return false
  346.             end
  347.         end
  348.         if ins == "L" then -- left turn
  349.             if turtle.turnLeft() then
  350.                 face = face - 1
  351.                 if face < 1 then
  352.                     face = 4   
  353.                 end
  354.             else
  355.                 return false
  356.             end
  357.         end
  358.         if ins == "R" then -- right turn
  359.             if turtle.turnRight() then
  360.                 face = face + 1
  361.                 if face > 4 then
  362.                     face = 1   
  363.                 end
  364.             else
  365.                 return false
  366.             end
  367.         end
  368.         if ins == "F" then -- forward move
  369.             if turtle.forward() then
  370.                 if face == 1 then
  371.                     turY = turY+1
  372.                 end
  373.                 if face == 2 then
  374.                     turX = turX+1
  375.                 end
  376.                 if face == 3 then
  377.                     turY = turY-1
  378.                 end
  379.                 if face == 4 then
  380.                     turX = turX-1
  381.                 end
  382.             else
  383.                 return false
  384.             end
  385.         end
  386.         if ins == "B" then -- back move
  387.             if turtle.back() then
  388.                 if face == 1 then
  389.                     turY = turY-1
  390.                 end
  391.                 if face == 2 then
  392.                     turX = turX-1
  393.                 end
  394.                 if face == 3 then
  395.                     turY = turY+1
  396.                 end
  397.                 if face == 4 then
  398.                     turX = turX+1
  399.                 end
  400.             else
  401.                 return false
  402.             end
  403.         end
  404.     end
  405.     return true
  406. end
  407.  
  408. function run(fFun)
  409.     if fFun == nil then
  410.         return false
  411.     else
  412.         local alreadyRunning = false
  413.         if tProgs[fFun] ~= nil then
  414.             for i = 1, #routinNames do
  415.                 if fFun == routinNames[i] then
  416.                     alreadyRunning = true
  417.                     break
  418.                 end
  419.             end
  420.             if not alreadyRunning then
  421.                 routines[#routines+1] = coroutine.create(tProgs[fFun])
  422.                 routinNames[#routinNames+1] = fFun
  423.                 return true
  424.             end
  425.         end
  426.     end
  427.     return false
  428. end
  429. function kill(fFun)
  430.     if fFun == nil then
  431.         return false
  432.     else
  433.         for i = 1,#routinNames do
  434.             if routinNames[i] == fFun then
  435.                 table.remove(routines,i)
  436.                 table.remove(routinNames,i)
  437.             end
  438.         end
  439.     end
  440. end
  441. local function fExit()
  442.     while face ~= 1 do
  443.         move("R")
  444.     end
  445.     error()
  446. end
  447. function readADV( _sReplaceChar, _tHistory )   
  448.     term.setCursorBlink( true )
  449.  
  450.     local sLine = ""
  451.     local nHistoryPos = nil
  452.     local nPos = 0
  453.     if _sReplaceChar then
  454.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  455.     end
  456.    
  457.     local w, h = term.getSize()
  458.     local sx, sy = term.getCursorPos() 
  459.     local function redraw()
  460.         local nScroll = 0
  461.         if sx + nPos >= w then
  462.             nScroll = (sx + nPos) - w
  463.         end
  464.            
  465.         term.setCursorPos( sx, sy )
  466.         term.write( string.rep(" ", w - sx + 1) )
  467.         term.setCursorPos( sx, sy )
  468.         if _sReplaceChar then
  469.             term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  470.         else
  471.             term.write( string.sub( sLine, nScroll + 1 ) )
  472.         end
  473.         term.setCursorPos( sx + nPos - nScroll, sy )
  474.     end
  475.    
  476.     while true do
  477.         local sEvent, param = os.pullEvent()
  478.         if sEvent == "char" then
  479.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  480.             nPos = nPos + 1
  481.             redraw()
  482.            
  483.         elseif sEvent == "key" then
  484.             if param == 28 then
  485.                 -- Enter
  486.                 break
  487.                
  488.             elseif param == 203 then
  489.                 -- Left
  490.                 if nPos > 0 then
  491.                     nPos = nPos - 1
  492.                     redraw()
  493.                 end
  494.                
  495.             elseif param == 205 then
  496.                 -- Right               
  497.                 if nPos < string.len(sLine) then
  498.                     nPos = nPos + 1
  499.                     redraw()
  500.                 end
  501.            
  502.             elseif param == 200 or param == 208 then
  503.                 -- Up or down
  504.                 if _tHistory then
  505.                     if param == 200 then
  506.                         -- Up
  507.                         if nHistoryPos == nil then
  508.                             if #_tHistory > 0 then
  509.                                 nHistoryPos = #_tHistory
  510.                             end
  511.                         elseif nHistoryPos > 1 then
  512.                             nHistoryPos = nHistoryPos - 1
  513.                         end
  514.                     else
  515.                         -- Down
  516.                         if nHistoryPos == #_tHistory then
  517.                             nHistoryPos = nil
  518.                         elseif nHistoryPos ~= nil then
  519.                             nHistoryPos = nHistoryPos + 1
  520.                         end                    
  521.                     end
  522.                    
  523.                     if nHistoryPos then
  524.                         sLine = _tHistory[nHistoryPos]
  525.                         nPos = string.len( sLine )
  526.                     else
  527.                         sLine = ""
  528.                         nPos = 0
  529.                     end
  530.                     redraw()
  531.                 end
  532.             elseif param == 14 then
  533.                 -- Backspace
  534.                 if nPos > 0 then
  535.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  536.                     nPos = nPos - 1                
  537.                     redraw()
  538.                 end
  539.             end
  540.         else
  541.             redraw()
  542.         end
  543.     end
  544.     term.setCursorBlink( false )
  545.     term.setCursorPos( w + 1, sy )
  546.     return sLine
  547. end
  548.  
  549. -- end of functions
  550.  
  551.  
  552.  
  553. -- start up sequence
  554.  
  555. rednet.open("right")
  556. gpsX,gpsY,gpsZ = gps.locate(1)
  557. if gpsX then
  558.     turX,turY,turZ = gpsX,gpsY,gpsZ
  559. else
  560.     turX,turY,turZ = 0,0,0
  561. end
  562. gpsX,gpsY,gpsZ = nil,nil,nil
  563.  
  564. --run("manCon")
  565. --run("location")
  566. run("gps")
  567. run("menu")
  568. run("wifiControll")
  569. --run("tunneler")
  570. --run("test")
  571. run("console")
  572.  
  573. -- main loop
  574.  
  575. while true do
  576.     e,e1,e2,e3,e4,e5 = os.pullEvent()
  577.     term.clear()
  578.     term.setCursorPos(1,1)
  579.     if shutdown then
  580.         fExit()
  581.     end
  582.     if e == "key" and e1 == 199 then
  583.         if consoleRUN then
  584.             consoleRUN = false
  585.         else
  586.             consoleRUN = true
  587.         end
  588.     end
  589.     i = 1
  590.     while true do
  591.         if i == #routines+1 then
  592.             break
  593.         end
  594.         if coroutine.status(routines[i]) == "deadened" then
  595.             table.remove(routines,i)
  596.             table.remove(routinNames,i)
  597.         else
  598.             local status,request,request1 = coroutine.resume(routines[i],e,e1,e2,e3,e4,e5)
  599.             if status and request then
  600.                 term.setCursorPos(1,6)
  601.                 term.clearLine()
  602.                 print(tostring(request).." "..tostring(request1))
  603.                 if request == "kill" then
  604.                     kill(request1)
  605.                 elseif request == "run" then
  606.                     run(request1)
  607.                 end
  608.             end
  609.             i = i+1
  610.         end
  611.     end
  612. end
  613.  
  614.  
  615.  
  616. --[[
  617. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  618. move("U")
  619. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  620. move("D")
  621. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  622. move("F")
  623. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  624. move("L")
  625. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  626. move("F")
  627. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  628.  
  629.  
  630. turX,turY,turZ = gps.locate(2)
  631. local c1,c2,c3
  632.  
  633. local tab = {}
  634.  
  635. for i = 1,500 do
  636.     local special,speical2 =  moves[math.random(1,6)],math.random(1,6)
  637.     write("Ins:"..special.." For:"..speical2.." Res:")
  638.     move(special,speical2)
  639.     print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  640.     c1,c2,c3 = gps.locate(2)
  641.     print("X"..c1.." Y:"..c2.." Z:"..c3)
  642.     table.insert(tab,"Ins: "..special.." For: "..speical2.." Res: tur: X:"..turX.." Y:"..turY.." Z:"..turZ.." Face: "..compas[face].." GPS: X:"..c1.." Y:"..c2.." Z:"..c3)
  643. end
  644.     while face ~= 1 do
  645.         move("R")
  646.     end
  647. print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
  648. c1,c2,c3 = gps.locate(2)
  649. print("X"..c1.." Y:"..c2.." Z:"..c3)
  650. table.insert(tab,"LAST".." Res: tur: X:"..turX.." Y:"..turY.." Z:"..turZ.." Face: "..compas[face].." GPS: X:"..c1.." Y:"..c2.." Z:"..c3)
  651. print("adding to file")
  652. if fs.exists("log") then
  653.     file = io.open("log","a")
  654. else
  655.     file = io.open("log","w")
  656. end
  657.     file:write("\n".."== start of log ==".."\n")
  658. for i = 1,#tab do
  659.     file:write(tab[i].."\n")
  660. end
  661. file:close()
  662. print("file add compleet")
  663. ]]--
Advertisement
Add Comment
Please, Sign In to add comment