Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------
- -- public codes
- -------------------------
- DEBUG_MODE = false
- SCT_PING = "sctcs_ping"
- SCT_REPING = "turtle_reping"
- SCT_REMOTE = "sctcs_remote"
- SCT_FUEL_DATA = "sctcs_fuel_data"
- SCT_REMOTE_MAT = "sctcs_remo_mat"
- SCT_REMOTE_MCANSEL = "sctcs_remo_mcan"
- SCT_REMOTE_LINE = "sctcs_remo_line"
- SCT_REMOTE_LCANSEL = "sctcs_remo_lcan"
- SCT_PING_GO = "sctcs_pingo"
- REMOTE_STOP_CODE = "remote_stop"
- REMOTE_BUSY_CODE = "remote_busy"
- -------------------------
- -- public function
- -------------------------
- function Debug(str)
- if DEBUG_MODE then
- write(str)
- end
- end
- function exitloop()
- --write("asdfasgaghahahafh")
- --sleep(2)
- return true
- end
- function syousuu (num,level)
- local r = num * 10^level
- return (math.floor(r) / 10^level)
- end
- function stringsplit( str )
- local r = {}
- local x = 1
- for v in string.gmatch(str,"[^%s]+") do
- r[x] = v
- x = x+1
- end
- return r
- end
- function working( func , ... )
- local co = coroutine.create(func)
- local ok,filter = coroutine.resume(co, ...)
- local eve = {}
- while coroutine.status(co) ~= "dead" do
- eve = {os.pullEventRaw()}
- --------------------------------
- -- retuen to func
- --------------------------------
- if filter == nil or filter == eve[1] or filter[1] == "terminate" then
- local param
- ok,param = coroutine.resume( co, unpack(eve) )
- if not ok then
- error( param )
- else
- filter = param
- end
- end
- -------------------------------
- -- mess proccess
- -------------------------------
- if eve[1] == "modem_message" then
- local mess = stringsplit(eve[5]["message"])
- if mess[1] == REMOTE_STOP_CODE then
- return
- else
- rednet.send(eve[4], REMOTE_BUSY_CODE.." "..status())
- end
- end
- end
- end
- function keta(tnum,k)
- return syousuu( ( tnum % ( 10 ^ k ) ) / ( 10 ^ ( k - 1 ) ),0 )
- end
- function status()
- local n = os.getComputerLabel() or "-"
- local t
- local add = ""
- if turtle then
- t="(t"
- add = "F:"..tostring(math.floor(100.0/turtle.getFuelLimit()*turtle.getFuelLevel())).."%"
- else
- if pocket then
- t="(p"
- else
- t="(c"
- end
- end
- return n..t..os.getComputerID()..")"..add
- end
- ---------------------------------
- -- pocket computer programs
- ---------------------------------
- function pocketping ()
- rednet.broadcast(SCT_PING)
- return true
- end
- function stopcodeloop( tID )
- local cursol = 1
- local looping = true
- local stopcomlist = {{"stop",true},{"exit",false}}
- while looping do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("stop turtle c:".. cursol .."\n")
- for i, v in ipairs(stopcomlist) do
- -- Code here...
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- write(v[1].."\n")
- end
- local event,p1,p2,p3,p4,p5 = os.pullEvent()
- -- key controll
- if event == "key" then
- if p1 == 208 then -- up key
- cursol = cursol + 1
- if cursol > #stopcomlist then
- cursol = 1
- end
- end
- if p1 == 200 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #stopcomlist
- end
- end
- if p1 == 57 then -- space key
- if stopcomlist[cursol][2] then
- rednet.send(tID, REMOTE_STOP_CODE)
- end
- return
- end
- end
- end
- return
- end
- function remoteturtleloop( tID )
- local cursol = 1
- local looping = true
- local fueldata = -1
- while looping do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("remote turtle c:".. cursol .. " f:"..fueldata .. "%\n")
- for i, v in ipairs(turtleComlist) do
- -- Code here...
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- write(v[1].."\n")
- end
- local event,p1,p2,p3,p4,p5 = os.pullEvent()
- -- key controll
- if event == "key" then
- if p1 == 208 then -- up key
- cursol = cursol + 1
- if cursol > #turtleComlist then
- cursol = 1
- end
- end
- if p1 == 200 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #turtleComlist
- end
- end
- if p1 == 57 then -- space key
- if turtleComlist[cursol][2] == nil then
- rednet.send(tID, SCT_REMOTE .." ".. turtleComlist[cursol][1])
- else
- looping = not turtleComlist[cursol][2]( tID )
- end
- end
- end
- if event == "modem_message" then
- local mess = stringsplit(p4["message"])
- --remote
- if mess[1] == SCT_FUEL_DATA then
- if mess[2] == nil then
- fueldata = -1
- else
- fueldata = tostring(d)
- end
- end
- --busy
- if mess[1] == REMOTE_BUSY_CODE and p3 == tID then
- stopcodeloop( tID )
- end
- end
- end
- return false
- end
- function PCmode()
- local comlist = {"intarval","long","exit"}
- local config = {intarval = {0.1,0.5,1,2,3,5},
- long = {5,10,20,30,50,64},
- exit = {"exit"}}
- local confData = {intarval=2,long=2,exit=1}
- local looping = true
- local cursol = 1
- local timeID = os.startTimer(config["intarval"][confData["intarval"]])
- while looping do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("poket com mode c:".. cursol .. "\n")
- for i, v in ipairs(comlist) do
- if v == "intarval" then
- write("intarval ")
- end
- if v == "long" then
- write("long ")
- end
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- write(config[v][confData[v]])
- if v == "intarval" then
- write("s")
- end
- if v == "long" then
- write("m")
- end
- write("\n")
- end
- local eve = {os.pullEvent()}
- if eve[1] == "key" then
- local selectCode = comlist[cursol]
- Debug(selectCode)
- if eve[2] == 208 then -- up key
- cursol = cursol + 1
- if cursol > #comlist then
- cursol = 1
- end
- end
- if eve[2] == 200 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #comlist
- end
- end
- if eve[2] == 205 then -- left key
- confData[selectCode] = confData[selectCode] + 1
- if confData[selectCode] > #config[selectCode] then
- confData[selectCode] = 1
- end
- end
- if eve[2] == 203 then -- right key
- confData[selectCode] = confData[selectCode] - 1
- if confData[selectCode] < 1 then
- confData[selectCode] = #config[selectCode]
- end
- end
- if eve[2] == 57 then -- space key
- if config[selectCode][confData[selectCode]] == "exit" then
- looping = false
- end
- end
- end
- if eve[1] == "timer" and timeID == eve[2] then
- rednet.broadcast(SCT_PING_GO.." "..config["long"][confData["long"]])
- timeID = os.startTimer(config["intarval"][confData["intarval"]])
- end
- end
- end
- function pocketloop ()
- local cursol = 1
- local defaultComm = {{"PING",pocketping},{"PingControllMode",PCmode}}
- local commandlist = {unpack(defaultComm)}
- rednet.open("back")
- while true do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("poket com mode c:".. cursol .. "\n")
- for i, v in ipairs(commandlist) do
- -- Code here...
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- if v[4] == REMOTE_BUSY_CODE then
- write("BSY_")
- end
- write(v[1].."\n")
- end
- local event,p1,p2,p3,p4,p5 = os.pullEvent()
- -- key controll
- if event == "key" then
- if p1 == 208 then -- up key
- cursol = cursol + 1
- if cursol > #commandlist then
- cursol = 1
- end
- end
- if p1 == 200 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #commandlist
- end
- end
- if p1 == 57 then -- space key
- if commandlist[cursol][2](commandlist[cursol][3]) then
- commandlist = {unpack(defaultComm)}
- end
- end
- end
- -- network
- if event == "modem_message" then
- local mess = stringsplit(p4["message"])
- if mess[1] == SCT_REPING or mess[1] == REMOTE_BUSY_CODE then
- local check = true
- for i, v in ipairs(commandlist) do
- if v[3] == p3 then
- check = false
- end
- end
- if check then
- commandlist[#commandlist+1] = {mess[2].."-"..tostring(syousuu(p5,1)).."m",remoteturtleloop,p3,mess[1]}
- end
- end
- end
- end
- rednet.close(back)
- end
- ------------------------------
- -- turtle programs
- ------------------------------
- function puting ( side ) -- "","Down","Up"
- if 0 == turtle.getItemCount(1) then
- local i = 1
- repeat
- i = i + 1
- if i > 16 then
- sleep(1)
- i = 1
- end
- until turtle.getItemCount(i) > 0
- turtle.select(i)
- turtle.transferTo(1)
- end
- turtle.select(1)
- repeat
- turtle["place"..side]()
- until turtle["detect"..side]()
- end
- function fet ( side )
- local high = 1
- local unside = (side == "Down") and "Up" or "Down"
- turtle[string.lower(side)]()
- while not turtle["detect"..side]() do
- high = high + 1
- turtle[string.lower(side)]()
- end
- for i = 1 , high, 1 do
- turtle[string.lower(unside)]()
- puting()
- end
- end
- function turtlelineloop( hitStop, pingmove, side, preDig, long )
- local looping = true
- local tilong = 0
- while looping do
- if preDig == "ON" then
- turtle["dig"..side]()
- end
- puting(side)
- if pingmove == "ON" then
- local pwait = true
- while pwait do
- local eve = {os.pullEvent("modem_message")}
- local mess = stringsplit(eve[5]["message"])
- --Debug(eve[5]["message"].."//p5:"..eve[6].."|ms2:"..mess[2])
- if mess[1] == SCT_PING_GO then
- if tonumber(eve[6]) < tonumber(mess[2]) then
- pwait = false
- end
- end
- end
- end
- if turtle.detect() then
- if hitStop == "ON" then
- break
- end
- end
- repeat
- if turtle.detect() then
- turtle.dig()
- end
- until turtle.forward()
- tilong = tilong + 1
- if tilong >= tonumber(long) and tonumber(long) > 0 then
- looping = false
- end
- end
- end
- function turtlelineconfig( tID )
- local cursol = 1
- local Clist = {"HitStop","PingMove","UpDown","PreDig","long","Ready"}
- local Config = { HitStop = {"ON","OFF"},
- PingMove = {"ON","OFF"},
- UpDown = {"Down","Up"},
- PreDig = {"ON","OFF"},
- long = {},
- Ready = {"Cansel","OK"}}
- local CData = { HitStop = 2, PingMove = 2, PreDig = 1, long = 0, UpDown = 1, Ready = 1}
- local looping = true
- local sendCursol = 0
- if tID then
- rednet.send(tID, SCT_REMOTE .." LINE")
- end
- while looping do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("Line config c:".. cursol .. "\n")
- for i, v in ipairs(Clist) do
- if v == "long" then
- write(" ")
- if sendCursol ~= 0 and i == cursol then
- for i = 1, sendCursol -1 do
- write(" ")
- end
- write("A")
- end
- write("\n")
- end
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- if v == "long" then
- write("long : ")
- if CData["long"] == 0 and i ~= cursol then
- write("NoLimit\n")
- else
- if CData["long"] < 1000 then
- write("0")
- end
- if CData["long"] < 100 then
- write("0")
- end
- if CData["long"] < 10 then
- write("0")
- end
- write(CData["long"].."\n")
- end
- if v == "long" then
- write(" ")
- if sendCursol ~= 0 and i == cursol then
- for i = 1, sendCursol -1 do
- write(" ")
- end
- write("V")
- end
- write("\n")
- end
- else
- write(v.." : "..Config[v][CData[v]].."\n")
- end
- end
- local event,p1,p2,p3,p4 = os.pullEvent()
- -- key controll
- if event == "key" then
- local selectCode = Clist[cursol]
- write(selectCode)
- -------------- no long -----------
- if p1 == 208 and sendCursol == 0 then -- up key
- cursol = cursol + 1
- if cursol > #Clist then
- cursol = 1
- end
- end
- if p1 == 200 and sendCursol == 0 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #Clist
- end
- end
- if p1 == 205 and selectCode ~= "long" then -- left key
- CData[selectCode] = CData[selectCode] + 1
- if CData[selectCode] > #Config[selectCode] then
- CData[selectCode] = 1
- end
- end
- if p1 == 203 and selectCode ~= "long" then -- right key
- CData[selectCode] = CData[selectCode] - 1
- if CData[selectCode] < 1 then
- CData[selectCode] = #Config[selectCode]
- end
- end
- -------------- long --------------
- if p1 == 200 and sendCursol ~= 0 then -- up key
- if keta( CData[selectCode] , 5 - sendCursol ) == 9 then
- CData[selectCode] = CData[selectCode] - 9 * 10 ^ (4-sendCursol)
- else
- CData[selectCode] = CData[selectCode] + 10 ^ (4-sendCursol)
- end
- end
- if p1 == 208 and sendCursol ~= 0 then -- down key
- if keta( CData[selectCode] , 5 - sendCursol ) == 0 then
- CData[selectCode] = CData[selectCode] + 9 * 10 ^ (4-sendCursol)
- else
- CData[selectCode] = CData[selectCode] - 10 ^ (4-sendCursol)
- end
- end
- if p1 == 203 and selectCode == "long" then -- right key
- sendCursol = sendCursol - 1
- if sendCursol < 0 then
- sendCursol = 4
- end
- end
- if p1 == 205 and selectCode == "long" then -- left key
- sendCursol = sendCursol + 1
- if sendCursol > 4 then
- sendCursol = 0
- end
- end
- if p1 == 57 then -- space key
- if Config[selectCode][CData[selectCode]] == "OK" then
- if turtle then
- working(turtlelineloop,Config["HitStop"][CData["HitStop"]],Config["PingMove"][CData["PingMove"]]
- ,Config["UpDown"][CData["UpDown"]],Config["PreDig"][CData["PreDig"]],CData["long"])
- looping = false
- end
- if pocket and tID then
- rednet.send(tID,SCT_REMOTE_LINE.." "..Config["HitStop"][CData["HitStop"]].." "..Config["PingMove"][CData["PingMove"]].." "..Config["UpDown"][CData["UpDown"]].." "..Config["PreDig"][CData["PreDig"]].." "..CData["long"])
- end
- looping = false
- end
- if Config[selectCode][CData[selectCode]] == "Cansel" then
- if tID then
- rednet.send(tID,SCT_REMOTE_LCANSEL)
- end
- looping = false
- end
- if selectCode == "long" then
- cursol = #Clist
- sendCursol = 0
- end
- end
- end
- -- network
- if event == "modem_message" then
- local mess = stringsplit(p4["message"])
- -- reping
- if mess[1] == SCT_PING then
- rednet.send(p3,SCT_REPING)
- end
- --remote
- if mess[1] == SCT_REMOTE_MAT then
- working(turtlematloop,mess[2],mess[3],mess[4])
- looping = false
- end
- if mess[1] == SCT_REMOTE_LINE then
- working(turtlelineloop,mess[2],mess[3],mess[4],mess[5],mess[6])
- looping = false
- end
- if mess[1] == SCT_REMOTE then
- looping = false
- end
- if mess[1] == SCT_REMOTE_LCANSEL then
- looping = false
- end
- end
- end
- end
- function turtlematloop( mode, side, maxline )
- ---config
- if side == "" or side == nil then
- side = "Down"
- end
- if maxline == nil then
- maxline = 30
- end
- local rc = 0
- local move = 0
- local modefunc = (mode == "Mat") and fet or puting
- if not turtle["detect"..side]() then
- modefunc( side )
- end
- while rc < 4 do
- if turtle.detect() then
- --write("DF\n")
- turtle.turnLeft()
- rc = rc + 1
- move = 0
- else
- while not turtle.forward() do
- sleep(1)
- end
- if turtle["detect"..side]() then
- --write("DD\n")
- while not turtle.back() do
- sleep(1)
- end
- turtle.turnLeft()
- rc = rc + 1
- move = 0
- else
- --write(".")
- move = move + 1
- rc = 0
- modefunc( side )
- if move > tonumber( maxline ) then
- --write("MD\n")
- turtle.turnLeft()
- move = 0
- end
- end
- end
- end
- end
- function turtlematconfig( tID )
- local cursol = 1
- local Clist = {"MatFet","UpDown","Size","Ready"}
- local Config = { MatFet = {"MAT","FET"},
- UpDown = {"Down","Up"},
- Size = {30,31,32,33,34,35,36,37,38,39,40,
- 41,42,43,44,45,46,47,48,49,50,
- 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
- 20,21,22,23,24,25,26,27,28,29},
- Ready = {"Cansel","OK"}}
- local CData = { MatFet = 1, UpDown = 1, Size = 1 ,Ready = 1}
- local looping = true
- if tID then
- rednet.send(tID, SCT_REMOTE .." MAT")
- end
- while looping do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("mat config c:".. cursol .. "\n")
- for i, v in ipairs(Clist) do
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- write(v.." : "..Config[v][CData[v]].."\n")
- end
- local event,p1,p2,p3,p4 = os.pullEvent()
- -- key controll
- if event == "key" then
- local selectCode = Clist[cursol]
- write(selectCode)
- if p1 == 208 then -- up key
- cursol = cursol + 1
- if cursol > #Clist then
- cursol = 1
- end
- end
- if p1 == 200 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #Clist
- end
- end
- if p1 == 205 then -- left key
- CData[selectCode] = CData[selectCode] + 1
- if CData[selectCode] > #Config[selectCode] then
- CData[selectCode] = 1
- end
- end
- if p1 == 203 then -- right key
- CData[selectCode] = CData[selectCode] - 1
- if CData[selectCode] < 1 then
- CData[selectCode] = #Config[selectCode]
- end
- end
- if p1 == 57 then -- space key
- if Config[selectCode][CData[selectCode]] == "OK" then
- if turtle then
- working(turtlematloop,Config["MatFet"][CData["MatFet"]],Config["UpDown"][CData["UpDown"]],Config["Size"][CData["Size"]])
- looping = false
- end
- if pocket and tID then
- rednet.send(tID, SCT_REMOTE_MAT.." "..Config["MatFet"][CData["MatFet"]].." "..Config["UpDown"][CData["UpDown"]].." "..Config["Size"][CData["Size"]])
- end
- looping = false
- end
- if Config[selectCode][CData[selectCode]] == "Cansel" then
- if tID then
- rednet.send(tID,SCT_REMOTE_MCANSEL)
- end
- looping = false
- end
- end
- end
- -- network
- if event == "modem_message" then
- local mess = stringsplit(p4["message"])
- -- reping
- if mess[1] == SCT_PING then
- rednet.send(p3,SCT_REPING)
- end
- --remote
- if mess[1] == SCT_REMOTE_MAT then
- working(turtlematloop,mess[2],mess[3],mess[4])
- looping = false
- end
- if mess[1] == SCT_REMOTE then
- looping = false
- end
- if mess[1] == SCT_REMOTE_MCANSEL then
- looping = false
- end
- end
- end
- end
- function turtleloop()
- local cursol = 1
- rednet.open("right")
- while true do
- -- data write mode
- term.clear()
- term.setCursorPos(1, 1)
- write("turtle c:".. cursol .. "/f:"..tostring(math.floor(100.0/turtle.getFuelLimit()*turtle.getFuelLevel())).."\n")
- for i, v in ipairs(turtleComlist) do
- -- Code here...
- if i == cursol then
- write(">")
- else
- write(" ")
- end
- write(v[1].."\n")
- end
- local event,p1,p2,p3,p4 = os.pullEvent()
- -- key controll
- if event == "key" then
- if p1 == 208 then -- up key
- cursol = cursol + 1
- if cursol > #turtleComlist then
- cursol = 1
- end
- end
- if p1 == 200 then -- down key
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #turtleComlist
- end
- end
- if p1 == 57 then -- space key
- turtleComlist[cursol][2](turtleComlist[cursol][3])
- end
- end
- -- network
- if event == "modem_message" then
- local mess = stringsplit(p4["message"])
- -- reping
- if mess[1] == SCT_PING then
- rednet.send(p3,SCT_REPING .. " ".. status())
- end
- --remote
- if mess[1] == SCT_REMOTE then
- for i, v in ipairs(turtleComlist) do
- if mess[2] == v[1] then
- v[2](v[3])
- break
- end
- end
- rednet.send(p3,SCT_FUEL_DATA .." ".. tostring(math.floor(100.0/turtle.getFuelLimit()*turtle.getFuelLevel())))
- end
- if mess[1] == SCT_REMOTE_MAT then
- working(turtlematloop,mess[2],mess[3],mess[4])
- end
- --write("asdgahh")
- --sleep(10)
- end
- end
- rednet.close("right")
- end
- function pickup( side )
- side = side or ""
- write("pickup"..side)
- while turtle.getFuelLimit() > turtle.getFuelLevel() do
- write(".")
- if turtle["suck"..side]() then
- if not turtle.refuel() then
- turtle["drop"..side]()
- break
- end
- else
- break
- end
- end
- write("\n")
- end
- function turtlestartup()
- -------------------------------
- -- label check
- -------------------------------
- if nil == os.getComputerLabel() then
- os.setComputerLabel("bit")
- end
- -------------------------------
- -- fuel check
- -------------------------------
- pickup("")
- pickup("Down")
- pickup("Up")
- end
- -----------------------------------
- -- turtle command list
- -----------------------------------
- if turtle then
- turtleComlist = {{"UP",turtle.up},
- {"DOWN",turtle.down},
- {"FORWARD",turtle.forward},
- {"BACK",turtle.back},
- {"T_RIGHT",turtle.turnRight},
- {"T_LEFT",turtle.turnLeft},
- {"MAT",turtlematconfig},
- {"LINE",turtlelineconfig}}
- else
- ---- for pocket
- turtleComlist = {{"UP"},
- {"DOWN"},
- {"FORWARD"},
- {"BACK"},
- {"T_RIGHT"},
- {"T_LEFT"},
- {"MAT",turtlematconfig},
- {"LINE",turtlelineconfig},
- {"EXIT",exitloop}}
- end
- ---------------------------------
- -- main programs
- ---------------------------------
- write("this is autorun test2 ganeesya\n")
- if turtle then
- write("its trutle.\n")
- turtlestartup()
- turtleloop()
- else
- if pocket then
- write("its pocket.\n")
- pocketloop()
- else
- write("its computer.\n")
- end
- end
- --local file = fs.open("testfile","w")
- --
- --if file then
- -- file.write("testcase")
- -- file.close()
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement