Advertisement
Ganeesya

sctcs_0_1_140919

Sep 24th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.17 KB | None | 0 0
  1. -------------------------
  2. -- public codes
  3. -------------------------
  4. DEBUG_MODE = false
  5.  
  6. SCT_PING = "sctcs_ping"
  7. SCT_REPING = "turtle_reping"
  8. SCT_REMOTE = "sctcs_remote"
  9. SCT_FUEL_DATA = "sctcs_fuel_data"
  10. SCT_REMOTE_MAT = "sctcs_remo_mat"
  11. SCT_REMOTE_MCANSEL = "sctcs_remo_mcan"
  12. SCT_REMOTE_LINE = "sctcs_remo_line"
  13. SCT_REMOTE_LCANSEL = "sctcs_remo_lcan"
  14. SCT_PING_GO = "sctcs_pingo"
  15.  
  16. REMOTE_STOP_CODE = "remote_stop"
  17. REMOTE_BUSY_CODE = "remote_busy"
  18.  
  19. -------------------------
  20. -- public function
  21. -------------------------
  22. function Debug(str)
  23.     if DEBUG_MODE then
  24.         write(str)
  25.     end
  26. end
  27.  
  28. function exitloop()
  29.     --write("asdfasgaghahahafh")
  30.     --sleep(2)
  31.     return true
  32. end
  33.  
  34. function syousuu (num,level)
  35.     local r = num * 10^level
  36.     return (math.floor(r) / 10^level)
  37. end
  38.  
  39. function stringsplit( str )
  40.     local r = {}
  41.     local x = 1
  42.     for v in string.gmatch(str,"[^%s]+") do
  43.         r[x] = v
  44.         x = x+1
  45.     end
  46.     return r
  47. end
  48.  
  49. function working( func , ... )
  50.     local co = coroutine.create(func)
  51.     local ok,filter = coroutine.resume(co, ...)
  52.     local eve = {}
  53.  
  54.     while coroutine.status(co) ~= "dead" do
  55.  
  56.         eve = {os.pullEventRaw()}
  57.         --------------------------------
  58.         -- retuen to func
  59.         --------------------------------
  60.         if filter == nil or filter == eve[1] or filter[1] == "terminate" then
  61.  
  62.             local param
  63.             ok,param = coroutine.resume( co, unpack(eve) )
  64.             if not ok then
  65.                 error( param )
  66.             else
  67.                 filter = param
  68.             end
  69.         end
  70.  
  71.         -------------------------------
  72.         -- mess proccess
  73.         -------------------------------
  74.         if eve[1] == "modem_message" then
  75.             local mess = stringsplit(eve[5]["message"])
  76.  
  77.             if mess[1] == REMOTE_STOP_CODE  then
  78.                 return
  79.             else
  80.                 rednet.send(eve[4], REMOTE_BUSY_CODE.." "..status())
  81.             end
  82.  
  83.         end
  84.  
  85.     end
  86. end
  87.  
  88. function keta(tnum,k)
  89.     return syousuu( ( tnum % ( 10 ^ k ) ) / ( 10 ^ ( k - 1 ) ),0 )
  90. end
  91.  
  92. function status()
  93.     local n = os.getComputerLabel() or "-"
  94.     local t
  95.     local add = ""
  96.     if turtle then
  97.         t="(t"
  98.         add = "F:"..tostring(math.floor(100.0/turtle.getFuelLimit()*turtle.getFuelLevel())).."%"
  99.     else
  100.         if pocket then
  101.             t="(p"
  102.         else
  103.             t="(c"
  104.         end
  105.     end
  106.     return n..t..os.getComputerID()..")"..add
  107. end
  108.  
  109. ---------------------------------
  110. -- pocket computer programs
  111. ---------------------------------
  112. function pocketping ()
  113.     rednet.broadcast(SCT_PING)
  114.     return true
  115. end
  116.  
  117. function stopcodeloop( tID )   
  118.     local cursol = 1
  119.     local looping = true
  120.     local stopcomlist = {{"stop",true},{"exit",false}}
  121.     while looping do
  122.         -- data write mode
  123.         term.clear()
  124.         term.setCursorPos(1, 1)
  125.         write("stop turtle  c:".. cursol .."\n")
  126.         for i, v in ipairs(stopcomlist) do
  127.             -- Code here...
  128.             if i == cursol then
  129.                 write(">")
  130.             else
  131.                 write(" ")
  132.             end
  133.             write(v[1].."\n")
  134.         end
  135.  
  136.         local event,p1,p2,p3,p4,p5 = os.pullEvent()
  137.         -- key controll
  138.         if event == "key" then
  139.             if p1 == 208 then -- up key
  140.                 cursol = cursol + 1
  141.                 if cursol > #stopcomlist then
  142.                     cursol = 1
  143.                 end
  144.             end
  145.             if p1 == 200 then -- down key
  146.                 cursol = cursol - 1
  147.                 if cursol < 1 then
  148.                     cursol = #stopcomlist
  149.                 end
  150.             end    
  151.             if p1 == 57 then -- space key
  152.                 if stopcomlist[cursol][2] then
  153.                     rednet.send(tID, REMOTE_STOP_CODE)
  154.                 end
  155.                 return
  156.             end    
  157.         end
  158.     end
  159.     return
  160. end
  161.  
  162. function remoteturtleloop( tID )
  163.     local cursol = 1
  164.     local looping = true
  165.     local fueldata = -1
  166.     while looping do
  167.         -- data write mode
  168.         term.clear()
  169.         term.setCursorPos(1, 1)
  170.         write("remote turtle  c:".. cursol .. " f:"..fueldata .. "%\n")
  171.         for i, v in ipairs(turtleComlist) do
  172.             -- Code here...
  173.             if i == cursol then
  174.                 write(">")
  175.             else
  176.                 write(" ")
  177.             end
  178.             write(v[1].."\n")
  179.         end
  180.  
  181.         local event,p1,p2,p3,p4,p5 = os.pullEvent()
  182.         -- key controll
  183.         if event == "key" then
  184.             if p1 == 208 then -- up key
  185.                 cursol = cursol + 1
  186.                 if cursol > #turtleComlist then
  187.                     cursol = 1
  188.                 end
  189.             end
  190.             if p1 == 200 then -- down key
  191.                 cursol = cursol - 1
  192.                 if cursol < 1 then
  193.                     cursol = #turtleComlist
  194.                 end
  195.             end    
  196.             if p1 == 57 then -- space key
  197.                 if turtleComlist[cursol][2] == nil then
  198.                     rednet.send(tID, SCT_REMOTE .." ".. turtleComlist[cursol][1])
  199.                 else
  200.                     looping = not turtleComlist[cursol][2]( tID )
  201.                 end
  202.             end    
  203.         end
  204.  
  205.  
  206.         if event == "modem_message" then
  207.             local mess = stringsplit(p4["message"])
  208.             --remote
  209.             if mess[1] == SCT_FUEL_DATA then
  210.                 if mess[2] == nil then
  211.                     fueldata = -1
  212.                 else
  213.                     fueldata = tostring(d)
  214.                 end
  215.             end
  216.  
  217.             --busy
  218.             if mess[1] == REMOTE_BUSY_CODE and p3 == tID then
  219.                 stopcodeloop( tID )
  220.             end
  221.         end
  222.     end
  223.     return false
  224. end
  225.  
  226. function PCmode()
  227.     local comlist = {"intarval","long","exit"}
  228.     local config = {intarval = {0.1,0.5,1,2,3,5},
  229.                     long = {5,10,20,30,50,64},
  230.                     exit = {"exit"}}
  231.     local confData = {intarval=2,long=2,exit=1}
  232.     local looping = true
  233.     local cursol = 1
  234.  
  235.     local timeID = os.startTimer(config["intarval"][confData["intarval"]])
  236.  
  237.     while looping do
  238.         -- data write mode
  239.         term.clear()
  240.         term.setCursorPos(1, 1)
  241.         write("poket com mode  c:".. cursol .. "\n")
  242.         for i, v in ipairs(comlist) do
  243.             if v == "intarval" then
  244.                 write("intarval ")
  245.             end
  246.             if v == "long" then
  247.                 write("long     ")
  248.             end
  249.             if i == cursol then
  250.                 write(">")
  251.             else
  252.                 write(" ")
  253.             end
  254.             write(config[v][confData[v]])
  255.             if v == "intarval" then
  256.                 write("s")
  257.             end
  258.             if v == "long" then
  259.                 write("m")
  260.             end
  261.             write("\n")
  262.         end
  263.  
  264.         local eve = {os.pullEvent()}
  265.  
  266.         if eve[1] == "key" then
  267.  
  268.             local selectCode = comlist[cursol]
  269.             Debug(selectCode)
  270.             if eve[2] == 208 then -- up key
  271.                 cursol = cursol + 1
  272.                 if cursol > #comlist then
  273.                     cursol = 1
  274.                 end
  275.             end
  276.             if eve[2] == 200 then -- down key
  277.                 cursol = cursol - 1
  278.                 if cursol < 1 then
  279.                     cursol = #comlist
  280.                 end
  281.             end    
  282.             if eve[2] == 205 then -- left key
  283.                 confData[selectCode] = confData[selectCode] + 1
  284.                 if confData[selectCode] > #config[selectCode] then
  285.                     confData[selectCode] = 1
  286.                 end
  287.             end
  288.             if eve[2] == 203 then -- right key
  289.                 confData[selectCode] = confData[selectCode] - 1
  290.                 if confData[selectCode] < 1 then
  291.                     confData[selectCode] = #config[selectCode]
  292.                 end
  293.             end    
  294.             if eve[2] == 57 then -- space key
  295.                 if config[selectCode][confData[selectCode]] == "exit" then
  296.                     looping = false
  297.                 end
  298.             end
  299.  
  300.         end
  301.  
  302.         if eve[1] == "timer" and timeID == eve[2] then
  303.             rednet.broadcast(SCT_PING_GO.." "..config["long"][confData["long"]])
  304.             timeID = os.startTimer(config["intarval"][confData["intarval"]])
  305.         end
  306.  
  307.     end
  308. end
  309.  
  310. function pocketloop ()
  311.     local cursol = 1
  312.     local defaultComm = {{"PING",pocketping},{"PingControllMode",PCmode}}
  313.     local commandlist = {unpack(defaultComm)}
  314.     rednet.open("back")
  315.  
  316.  
  317.     while true do
  318.         -- data write mode
  319.         term.clear()
  320.         term.setCursorPos(1, 1)
  321.         write("poket com mode  c:".. cursol .. "\n")
  322.         for i, v in ipairs(commandlist) do
  323.             -- Code here...
  324.             if i == cursol then
  325.                 write(">")
  326.             else
  327.                 write(" ")
  328.             end
  329.             if v[4] == REMOTE_BUSY_CODE then
  330.                 write("BSY_")
  331.             end
  332.             write(v[1].."\n")
  333.         end
  334.  
  335.         local event,p1,p2,p3,p4,p5 = os.pullEvent()
  336.         -- key controll
  337.         if event == "key" then
  338.             if p1 == 208 then -- up key
  339.                 cursol = cursol + 1
  340.                 if cursol > #commandlist then
  341.                     cursol = 1
  342.                 end
  343.             end
  344.             if p1 == 200 then -- down key
  345.                 cursol = cursol - 1
  346.                 if cursol < 1 then
  347.                     cursol = #commandlist
  348.                 end
  349.             end    
  350.             if p1 == 57 then -- space key
  351.                 if commandlist[cursol][2](commandlist[cursol][3]) then
  352.                     commandlist = {unpack(defaultComm)}
  353.                 end
  354.             end    
  355.         end
  356.  
  357.         -- network
  358.         if event == "modem_message" then
  359.             local mess = stringsplit(p4["message"])
  360.             if mess[1] == SCT_REPING or mess[1] == REMOTE_BUSY_CODE then
  361.                 local check = true
  362.                 for i, v in ipairs(commandlist) do
  363.                     if v[3] == p3 then
  364.                         check = false
  365.                     end
  366.                 end
  367.                 if check then
  368.                     commandlist[#commandlist+1] = {mess[2].."-"..tostring(syousuu(p5,1)).."m",remoteturtleloop,p3,mess[1]}
  369.                 end
  370.             end
  371.         end
  372.  
  373.     end
  374.  
  375.     rednet.close(back)
  376. end
  377.  
  378. ------------------------------
  379. -- turtle programs
  380. ------------------------------
  381. function puting ( side ) -- "","Down","Up"
  382.     if 0 == turtle.getItemCount(1) then
  383.         local i = 1
  384.         repeat
  385.             i = i + 1
  386.             if i > 16 then
  387.                 sleep(1)
  388.                 i = 1
  389.             end
  390.         until turtle.getItemCount(i) > 0
  391.         turtle.select(i)
  392.         turtle.transferTo(1)
  393.     end
  394.     turtle.select(1)
  395.     repeat
  396.         turtle["place"..side]()
  397.     until turtle["detect"..side]()
  398. end
  399.  
  400. function fet ( side )
  401.     local high = 1
  402.     local unside = (side == "Down") and "Up" or "Down"
  403.  
  404.     turtle[string.lower(side)]()
  405.     while not turtle["detect"..side]() do
  406.         high = high + 1
  407.         turtle[string.lower(side)]()
  408.     end
  409.     for i = 1 , high, 1 do
  410.         turtle[string.lower(unside)]()
  411.         puting()
  412.     end
  413. end
  414.  
  415. function turtlelineloop( hitStop, pingmove, side, preDig, long )
  416.     local looping = true
  417.     local tilong = 0
  418.  
  419.     while looping do
  420.         if preDig == "ON" then
  421.             turtle["dig"..side]()
  422.         end
  423.         puting(side)
  424.  
  425.         if pingmove == "ON" then
  426.             local pwait = true
  427.             while pwait do
  428.                 local eve = {os.pullEvent("modem_message")}
  429.                 local mess = stringsplit(eve[5]["message"])
  430.                 --Debug(eve[5]["message"].."//p5:"..eve[6].."|ms2:"..mess[2])
  431.                 if mess[1] == SCT_PING_GO then
  432.                     if tonumber(eve[6]) < tonumber(mess[2]) then
  433.                         pwait = false
  434.                     end
  435.                 end
  436.             end
  437.         end
  438.  
  439.         if turtle.detect() then
  440.             if hitStop == "ON" then
  441.                 break
  442.             end
  443.         end
  444.  
  445.         repeat
  446.             if turtle.detect() then
  447.                 turtle.dig()
  448.             end
  449.         until turtle.forward()
  450.  
  451.         tilong = tilong + 1
  452.         if tilong >= tonumber(long) and tonumber(long) > 0 then
  453.             looping = false
  454.         end
  455.     end
  456.  
  457. end
  458.  
  459. function turtlelineconfig( tID )
  460.     local cursol = 1
  461.     local Clist = {"HitStop","PingMove","UpDown","PreDig","long","Ready"}
  462.     local Config = { HitStop = {"ON","OFF"},
  463.         PingMove = {"ON","OFF"},
  464.         UpDown = {"Down","Up"},
  465.         PreDig = {"ON","OFF"},
  466.         long = {},
  467.         Ready = {"Cansel","OK"}}
  468.     local CData = { HitStop = 2, PingMove = 2, PreDig = 1, long = 0, UpDown = 1, Ready = 1}
  469.     local looping = true
  470.     local sendCursol = 0
  471.  
  472.     if tID then
  473.         rednet.send(tID, SCT_REMOTE .." LINE")
  474.     end
  475.  
  476.     while looping do
  477.         -- data write mode
  478.         term.clear()
  479.         term.setCursorPos(1, 1)
  480.         write("Line config  c:".. cursol .. "\n")
  481.         for i, v in ipairs(Clist) do
  482.             if v == "long" then
  483.                 write("        ")
  484.                 if sendCursol ~= 0 and i == cursol then
  485.                     for i = 1, sendCursol -1 do
  486.                         write(" ")
  487.                     end
  488.                     write("A")
  489.                 end
  490.                 write("\n")
  491.             end
  492.  
  493.             if i == cursol then
  494.                 write(">")
  495.             else
  496.                 write(" ")
  497.             end
  498.  
  499.             if v == "long" then
  500.                 write("long : ")
  501.                 if CData["long"] == 0 and i ~= cursol then
  502.                     write("NoLimit\n")
  503.                 else
  504.                     if CData["long"] < 1000 then
  505.                         write("0")
  506.                     end
  507.                     if CData["long"] < 100 then
  508.                         write("0")
  509.                     end
  510.                     if CData["long"] < 10 then
  511.                         write("0")
  512.                     end
  513.                     write(CData["long"].."\n")
  514.                 end
  515.  
  516.                 if v == "long" then
  517.                     write("        ")
  518.                     if sendCursol ~= 0 and i == cursol then
  519.                         for i = 1, sendCursol -1 do
  520.                             write(" ")
  521.                         end
  522.                         write("V")
  523.                     end
  524.                     write("\n")
  525.                 end
  526.             else
  527.                 write(v.." : "..Config[v][CData[v]].."\n")
  528.             end
  529.         end
  530.  
  531.         local event,p1,p2,p3,p4 = os.pullEvent()
  532.  
  533.         -- key controll
  534.         if event == "key" then
  535.             local selectCode = Clist[cursol]
  536.             write(selectCode)
  537.  
  538.             -------------- no long -----------         
  539.             if p1 == 208 and sendCursol == 0 then -- up key
  540.                 cursol = cursol + 1
  541.                 if cursol > #Clist then
  542.                     cursol = 1
  543.                 end
  544.             end
  545.             if p1 == 200 and sendCursol == 0 then -- down key
  546.                 cursol = cursol - 1
  547.                 if cursol < 1 then
  548.                     cursol = #Clist
  549.                 end
  550.             end    
  551.             if p1 == 205 and selectCode ~= "long" then -- left key
  552.                 CData[selectCode] = CData[selectCode] + 1
  553.                 if CData[selectCode] > #Config[selectCode] then
  554.                     CData[selectCode] = 1
  555.                 end
  556.             end
  557.             if p1 == 203 and selectCode ~= "long" then -- right key
  558.                 CData[selectCode] = CData[selectCode] - 1
  559.                 if CData[selectCode] < 1 then
  560.                     CData[selectCode] = #Config[selectCode]
  561.                 end
  562.             end    
  563.  
  564.             -------------- long --------------
  565.             if p1 == 200 and sendCursol ~= 0 then -- up key
  566.                 if keta( CData[selectCode] , 5 - sendCursol ) == 9 then
  567.                     CData[selectCode] = CData[selectCode] - 9 * 10 ^ (4-sendCursol)
  568.                 else
  569.                     CData[selectCode] = CData[selectCode] + 10 ^ (4-sendCursol)
  570.                 end
  571.             end
  572.             if p1 == 208 and sendCursol ~= 0 then -- down key
  573.                 if keta( CData[selectCode] , 5 - sendCursol ) == 0 then
  574.                     CData[selectCode] = CData[selectCode] + 9 * 10 ^ (4-sendCursol)
  575.                 else
  576.                     CData[selectCode] = CData[selectCode] - 10 ^ (4-sendCursol)
  577.                 end
  578.             end    
  579.             if p1 == 203 and selectCode == "long" then -- right key
  580.                 sendCursol = sendCursol - 1
  581.                 if sendCursol < 0 then
  582.                     sendCursol = 4
  583.                 end
  584.             end
  585.             if p1 == 205 and selectCode == "long" then -- left key
  586.                 sendCursol = sendCursol + 1
  587.                 if sendCursol > 4 then
  588.                     sendCursol = 0
  589.                 end
  590.             end    
  591.  
  592.  
  593.             if p1 == 57 then -- space key
  594.                 if Config[selectCode][CData[selectCode]] == "OK" then
  595.                     if turtle then
  596.                         working(turtlelineloop,Config["HitStop"][CData["HitStop"]],Config["PingMove"][CData["PingMove"]]
  597.                                               ,Config["UpDown"][CData["UpDown"]],Config["PreDig"][CData["PreDig"]],CData["long"])
  598.                         looping = false
  599.                     end
  600.                     if pocket and tID then
  601.                         rednet.send(tID,SCT_REMOTE_LINE.." "..Config["HitStop"][CData["HitStop"]].." "..Config["PingMove"][CData["PingMove"]].." "..Config["UpDown"][CData["UpDown"]].." "..Config["PreDig"][CData["PreDig"]].." "..CData["long"])
  602.                     end
  603.                     looping = false
  604.                 end
  605.                 if Config[selectCode][CData[selectCode]] == "Cansel" then
  606.                     if tID then
  607.                         rednet.send(tID,SCT_REMOTE_LCANSEL)
  608.                     end
  609.                     looping = false
  610.                 end
  611.  
  612.                 if selectCode == "long" then
  613.                     cursol = #Clist
  614.                     sendCursol = 0
  615.                 end
  616.  
  617.             end    
  618.         end
  619.  
  620.         -- network
  621.         if event == "modem_message" then
  622.             local mess = stringsplit(p4["message"])
  623.             -- reping
  624.             if mess[1] == SCT_PING then
  625.                 rednet.send(p3,SCT_REPING)
  626.             end
  627.  
  628.             --remote
  629.             if mess[1] == SCT_REMOTE_MAT then
  630.                 working(turtlematloop,mess[2],mess[3],mess[4])
  631.                 looping = false
  632.             end
  633.             if mess[1] == SCT_REMOTE_LINE then
  634.                 working(turtlelineloop,mess[2],mess[3],mess[4],mess[5],mess[6])
  635.                 looping = false
  636.             end
  637.  
  638.             if mess[1] == SCT_REMOTE then
  639.                 looping = false
  640.             end
  641.  
  642.             if mess[1] == SCT_REMOTE_LCANSEL then
  643.                 looping = false
  644.             end
  645.         end
  646.     end
  647. end
  648.  
  649. function turtlematloop( mode, side, maxline )
  650.     ---config
  651.     if side == "" or side == nil then
  652.         side = "Down"
  653.     end
  654.     if maxline == nil then
  655.         maxline = 30
  656.     end
  657.     local rc = 0
  658.     local move = 0
  659.     local modefunc = (mode == "Mat") and fet or puting
  660.  
  661.     if not turtle["detect"..side]() then
  662.         modefunc( side )
  663.     end
  664.  
  665.     while rc < 4 do
  666.         if turtle.detect() then
  667.             --write("DF\n")
  668.             turtle.turnLeft()
  669.             rc = rc + 1
  670.             move = 0
  671.         else
  672.             while not turtle.forward() do
  673.                 sleep(1)
  674.             end
  675.             if turtle["detect"..side]() then
  676.                 --write("DD\n")
  677.                 while not turtle.back() do
  678.                     sleep(1)
  679.                 end
  680.                 turtle.turnLeft()
  681.                 rc = rc + 1
  682.                 move = 0
  683.             else
  684.                 --write(".")
  685.                 move = move + 1
  686.                 rc = 0
  687.                 modefunc( side )
  688.                 if move > tonumber( maxline ) then
  689.                     --write("MD\n")
  690.                     turtle.turnLeft()
  691.                     move = 0
  692.                 end
  693.             end
  694.         end
  695.     end
  696. end
  697.  
  698. function turtlematconfig( tID )
  699.     local cursol = 1
  700.     local Clist = {"MatFet","UpDown","Size","Ready"}
  701.     local Config = { MatFet = {"MAT","FET"},
  702.         UpDown = {"Down","Up"},
  703.         Size = {30,31,32,33,34,35,36,37,38,39,40,
  704.             41,42,43,44,45,46,47,48,49,50,
  705.             5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
  706.             20,21,22,23,24,25,26,27,28,29},
  707.         Ready = {"Cansel","OK"}}
  708.     local CData = { MatFet = 1, UpDown = 1, Size = 1 ,Ready = 1}
  709.     local looping = true
  710.  
  711.     if tID then
  712.         rednet.send(tID, SCT_REMOTE .." MAT")
  713.     end
  714.  
  715.     while looping do
  716.         -- data write mode
  717.         term.clear()
  718.         term.setCursorPos(1, 1)
  719.         write("mat config  c:".. cursol .. "\n")
  720.         for i, v in ipairs(Clist) do
  721.             if i == cursol then
  722.                 write(">")
  723.             else
  724.                 write(" ")
  725.             end
  726.             write(v.." : "..Config[v][CData[v]].."\n")
  727.         end
  728.  
  729.         local event,p1,p2,p3,p4 = os.pullEvent()
  730.  
  731.         -- key controll
  732.         if event == "key" then
  733.             local selectCode = Clist[cursol]
  734.             write(selectCode)
  735.             if p1 == 208 then -- up key
  736.                 cursol = cursol + 1
  737.                 if cursol > #Clist then
  738.                     cursol = 1
  739.                 end
  740.             end
  741.             if p1 == 200 then -- down key
  742.                 cursol = cursol - 1
  743.                 if cursol < 1 then
  744.                     cursol = #Clist
  745.                 end
  746.             end    
  747.             if p1 == 205 then -- left key
  748.                 CData[selectCode] = CData[selectCode] + 1
  749.                 if CData[selectCode] > #Config[selectCode] then
  750.                     CData[selectCode] = 1
  751.                 end
  752.             end
  753.             if p1 == 203 then -- right key
  754.                 CData[selectCode] = CData[selectCode] - 1
  755.                 if CData[selectCode] < 1 then
  756.                     CData[selectCode] = #Config[selectCode]
  757.                 end
  758.             end    
  759.             if p1 == 57 then -- space key
  760.  
  761.                 if Config[selectCode][CData[selectCode]] == "OK" then
  762.                     if turtle then
  763.                         working(turtlematloop,Config["MatFet"][CData["MatFet"]],Config["UpDown"][CData["UpDown"]],Config["Size"][CData["Size"]])
  764.                         looping = false
  765.                     end
  766.                     if pocket and tID then
  767.                         rednet.send(tID, SCT_REMOTE_MAT.." "..Config["MatFet"][CData["MatFet"]].." "..Config["UpDown"][CData["UpDown"]].." "..Config["Size"][CData["Size"]])
  768.                     end
  769.                     looping = false
  770.                 end
  771.                 if Config[selectCode][CData[selectCode]] == "Cansel" then
  772.                     if tID then
  773.                         rednet.send(tID,SCT_REMOTE_MCANSEL)
  774.                     end
  775.                     looping = false
  776.                 end
  777.  
  778.             end    
  779.         end
  780.  
  781.         -- network
  782.         if event == "modem_message" then
  783.             local mess = stringsplit(p4["message"])
  784.             -- reping
  785.             if mess[1] == SCT_PING then
  786.                 rednet.send(p3,SCT_REPING)
  787.             end
  788.  
  789.             --remote
  790.             if mess[1] == SCT_REMOTE_MAT then
  791.                 working(turtlematloop,mess[2],mess[3],mess[4])
  792.                 looping = false
  793.             end
  794.  
  795.             if mess[1] == SCT_REMOTE then
  796.                 looping = false
  797.             end
  798.             if mess[1] == SCT_REMOTE_MCANSEL then
  799.                 looping = false
  800.             end
  801.         end
  802.     end
  803. end
  804.  
  805. function turtleloop()
  806.     local cursol = 1
  807.     rednet.open("right")
  808.  
  809.     while true do
  810.         -- data write mode
  811.         term.clear()
  812.         term.setCursorPos(1, 1)
  813.         write("turtle  c:".. cursol .. "/f:"..tostring(math.floor(100.0/turtle.getFuelLimit()*turtle.getFuelLevel())).."\n")
  814.         for i, v in ipairs(turtleComlist) do
  815.             -- Code here...
  816.             if i == cursol then
  817.                 write(">")
  818.             else
  819.                 write(" ")
  820.             end
  821.             write(v[1].."\n")
  822.         end
  823.  
  824.         local event,p1,p2,p3,p4 = os.pullEvent()
  825.  
  826.         -- key controll
  827.         if event == "key" then
  828.             if p1 == 208 then -- up key
  829.                 cursol = cursol + 1
  830.                 if cursol > #turtleComlist then
  831.                     cursol = 1
  832.                 end
  833.             end
  834.             if p1 == 200 then -- down key
  835.                 cursol = cursol - 1
  836.                 if cursol < 1 then
  837.                     cursol = #turtleComlist
  838.                 end
  839.             end    
  840.             if p1 == 57 then -- space key
  841.                 turtleComlist[cursol][2](turtleComlist[cursol][3])
  842.             end    
  843.         end
  844.  
  845.         -- network
  846.         if event == "modem_message" then
  847.             local mess = stringsplit(p4["message"])
  848.             -- reping
  849.             if mess[1] == SCT_PING then
  850.                 rednet.send(p3,SCT_REPING .. " ".. status())
  851.             end
  852.  
  853.             --remote
  854.             if mess[1] == SCT_REMOTE then
  855.                 for i, v in ipairs(turtleComlist) do
  856.                     if mess[2] == v[1] then
  857.                         v[2](v[3])
  858.                         break
  859.                     end
  860.                 end
  861.  
  862.                 rednet.send(p3,SCT_FUEL_DATA .." ".. tostring(math.floor(100.0/turtle.getFuelLimit()*turtle.getFuelLevel())))
  863.             end
  864.  
  865.             if mess[1] == SCT_REMOTE_MAT then
  866.                 working(turtlematloop,mess[2],mess[3],mess[4])
  867.             end
  868.  
  869.             --write("asdgahh")
  870.             --sleep(10)
  871.         end
  872.     end
  873.  
  874.     rednet.close("right")
  875. end
  876.  
  877. function pickup( side )
  878.     side = side or ""
  879.     write("pickup"..side)
  880.     while turtle.getFuelLimit() > turtle.getFuelLevel() do
  881.         write(".")
  882.         if turtle["suck"..side]() then
  883.             if not turtle.refuel() then
  884.                     turtle["drop"..side]()
  885.                 break
  886.             end
  887.         else
  888.             break
  889.         end
  890.     end
  891.     write("\n")
  892. end
  893.  
  894. function turtlestartup()
  895.     -------------------------------
  896.     -- label check
  897.     -------------------------------
  898.     if nil == os.getComputerLabel() then
  899.         os.setComputerLabel("bit")
  900.     end
  901.  
  902.     -------------------------------
  903.     -- fuel check
  904.     -------------------------------
  905.     pickup("")
  906.     pickup("Down")
  907.     pickup("Up")
  908. end
  909.  
  910. -----------------------------------
  911. -- turtle command list
  912. -----------------------------------
  913. if turtle then
  914.     turtleComlist = {{"UP",turtle.up},
  915.                     {"DOWN",turtle.down},
  916.                     {"FORWARD",turtle.forward},
  917.                     {"BACK",turtle.back},
  918.                     {"T_RIGHT",turtle.turnRight},
  919.                     {"T_LEFT",turtle.turnLeft},
  920.                     {"MAT",turtlematconfig},
  921.                     {"LINE",turtlelineconfig}}
  922. else
  923.     ---- for pocket
  924.     turtleComlist = {{"UP"},
  925.                     {"DOWN"},
  926.                     {"FORWARD"},
  927.                     {"BACK"},
  928.                     {"T_RIGHT"},
  929.                     {"T_LEFT"},
  930.                     {"MAT",turtlematconfig},
  931.                     {"LINE",turtlelineconfig},
  932.                     {"EXIT",exitloop}}
  933. end
  934.  
  935. ---------------------------------
  936. -- main programs
  937. ---------------------------------
  938. write("this is autorun test2 ganeesya\n")
  939. if turtle then
  940.     write("its trutle.\n")
  941.     turtlestartup()
  942.     turtleloop()
  943. else
  944.     if pocket then
  945.         write("its pocket.\n")
  946.         pocketloop()
  947.     else
  948.         write("its computer.\n")
  949.     end
  950. end
  951.  
  952.  
  953. --local file = fs.open("testfile","w")
  954. --
  955. --if file then
  956. --  file.write("testcase")
  957. --  file.close()
  958. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement