Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- turtle = 13
- co = {0, 0, 0}
- sty = co[2] - 20
- direction = 1
- selected = 1
- last = 1
- tm = 1
- start = {0, 0, 0}
- returning = false
- function save()
- local fp = fs.open("/save", "w")
- fp.write(textutils.serialize(co))
- fp.close()
- local fp2 = fs.open("/direction", "w")
- fp2.write(textutils.serialize({direction}))
- fp2.close()
- print("Saved coordinates: ("..co[1]..", "..co[2]..", "..co[3]..")")
- end
- function sep(list, by)
- local newlist = {}
- while true do
- local on = nil
- local done = false
- for i = 1, #list do
- if done == false then
- if string.sub(list, i, i) == by then
- on = i
- done = true
- end
- end
- end
- if on then
- table.insert(newlist, string.sub(list, 1, on - 1))
- list = string.sub(list, on + 1)
- else
- break
- end
- end
- table.insert(newlist, list)
- return newlist
- end
- function clear()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(1)
- end
- function ports()
- rednet.open("back")
- rednet.open("top")
- rednet.open("front")
- rednet.open("bottom")
- rednet.open("left")
- rednet.open("right")
- end
- function rec()
- local idr, msgr, elr = nil
- function lop()
- local id, msg = rednet.receive()
- if id == turtle then
- msg = textutils.unserialize(msg)
- idr = id
- msgr = msg
- else
- lop()
- end
- end
- lop()
- repeat sleep(0) until idr and msgr
- return idr, msgr
- end
- function down()
- rednet.send(turtle, textutils.serialize({"down"}))
- local id, ms = rec()
- if ms[1] == true then
- co[2] = co[2] - 1
- end
- return ms
- end
- function up()
- rednet.send(turtle, textutils.serialize({"up"}))
- local id, ms = rec()
- if ms[1] == true then
- co[2] = co[2] + 1
- end
- return ms
- end
- function back()
- rednet.send(turtle, textutils.serialize({"back"}))
- local id, ms = rec()
- if ms[1] == true then
- if direction == 1 then
- co[1] = co[1] - 1
- elseif direction == 2 then
- co[3] = co[3] + 1
- elseif direction == 3 then
- co[1] = co[1] + 1
- elseif direction == 4 then
- co[3] = co[3] - 1
- end
- end
- return ms
- end
- function forward()
- rednet.send(turtle, textutils.serialize({"forward"}))
- local id, ms = rec()
- if ms[1] == true then
- if direction == 1 then
- co[1] = co[1] + 1
- elseif direction == 2 then
- co[3] = co[3] - 1
- elseif direction == 3 then
- co[1] = co[1] - 1
- elseif direction == 4 then
- co[3] = co[3] + 1
- end
- end
- return ms
- end
- function left()
- rednet.send(turtle, textutils.serialize({"turnLeft"}))
- local id, ms = rec()
- if ms[1] == true then
- if direction == 1 then
- direction = 2
- elseif direction == 2 then
- direction = 3
- elseif direction == 3 then
- direction = 4
- elseif direction == 4 then
- direction = 1
- end
- end
- return ms
- end
- function right()
- rednet.send(turtle, textutils.serialize({"turnRight"}))
- local id, ms = rec()
- if ms[1] == true then
- if direction == 1 then
- direction = 4
- elseif direction == 2 then
- direction = 1
- elseif direction == 3 then
- direction = 2
- elseif direction == 4 then
- direction = 3
- end
- end
- return ms
- end
- function select(num)
- rednet.send(turtle, textutils.serialize({"select", num}))
- local id, ms = rec()
- if ms[1] == true then
- selected = num
- end
- return ms
- end
- function icount(num)
- rednet.send(turtle, textutils.serialize({"getItemCount", num}))
- local id, ms = rec()
- return ms
- end
- function ispace(num)
- rednet.send(turtle, textutils.serialize({"getItemSpace", num}))
- local id, ms = rec()
- return ms
- end
- function dig()
- rednet.send(turtle, textutils.serialize({"dig"}))
- local id, ms = rec()
- return ms
- end
- function digup()
- rednet.send(turtle, textutils.serialize({"digUp"}))
- local id, ms = rec()
- return ms
- end
- function digdown()
- rednet.send(turtle, textutils.serialize({"digDown"}))
- local id, ms = rec()
- return ms
- end
- function place()
- rednet.send(turtle, textutils.serialize({"place"}))
- local id, ms = rec()
- return ms
- end
- function placeup()
- rednet.send(turtle, textutils.serialize({"placeUp"}))
- local id, ms = rec()
- return ms
- end
- function placedown()
- rednet.send(turtle, textutils.serialize({"placeDown"}))
- local id, ms = rec()
- return ms
- end
- function detect()
- rednet.send(turtle, textutils.serialize({"detect"}))
- local id, ms = rec()
- return ms
- end
- function detectup()
- rednet.send(turtle, textutils.serialize({"detectUp"}))
- local id, ms = rec()
- return ms
- end
- function detectdown()
- rednet.send(turtle, textutils.serialize({"detectDown"}))
- local id, ms = rec()
- return ms
- end
- function compare()
- rednet.send(turtle, textutils.serialize({"compare"}))
- local id, ms = rec()
- return ms
- end
- function compareup()
- rednet.send(turtle, textutils.serialize({"compareUp"}))
- local id, ms = rec()
- return ms
- end
- function comparedown()
- rednet.send(turtle, textutils.serialize({"compareDown"}))
- local id, ms = rec()
- return ms
- end
- function compareto(num)
- rednet.send(turtle, textutils.serialize({"compareTo", num}))
- local id, ms = rec()
- return ms
- end
- function transferto(num)
- rednet.send(turtle, textutils.serialize({"transferTo", num}))
- local id, ms = rec()
- return ms
- end
- function drop(num)
- rednet.send(turtle, textutils.serialize({"drop", num}))
- local id, ms = rec()
- return ms
- end
- function dropup(num)
- rednet.send(turtle, textutils.serialize({"dropUp", num}))
- local id, ms = rec()
- return ms
- end
- function dropdown(num)
- rednet.send(turtle, textutils.serialize({"dropDown", num}))
- local id, ms = rec()
- return ms
- end
- function suck()
- rednet.send(turtle, textutils.serialize({"suck"}))
- local id, ms = rec()
- return ms
- end
- function suckup()
- rednet.send(turtle, textutils.serialize({"suckUp"}))
- local id, ms = rec()
- return ms
- end
- function suckdown()
- rednet.send(turtle, textutils.serialize({"suckDown"}))
- local id, ms = rec()
- return ms
- end
- function dp()
- rednet.send(turtle, textutils.serialize({"dp"}))
- rec()
- end
- function ran()
- tm = tm + 1
- local moving = nil
- if math.random(1, 100) < 10 then
- if last == 1 then
- last = 2
- else
- if co[2] < sty then
- last = 1
- end
- end
- moving = true
- end
- if math.random(1, 100) < 8 then
- if math.random(1, 2) == 1 then
- left()
- else
- right()
- end
- end
- if moving then
- if last == 1 then
- digup()
- up()
- else
- digdown()
- down()
- end
- end
- digup()
- dig()
- forward()
- digup()
- if tm == 31 then
- dp()
- tm = 0
- end
- save()
- end
- function retx()
- local togo = 0
- if co[1] < start[1] then
- togo = direction - 1
- elseif co[1] > start[1] then
- togo = direction - 3
- end
- if togo > 0 then
- for i = 1, togo do
- right()
- end
- elseif togo < 0 then
- for i = 1, togo*-1 do
- left()
- end
- end
- if co[1] < start[1] then
- for i = 1, start[1] - co[1] do
- function lolz()
- dig()
- local d = forward()
- if d[1] == false then
- lolz()
- end
- end
- lolz()
- end
- elseif co[1] > start[1] then
- for i = 1, co[1] - start[1] do
- function lolz()
- dig()
- local d = forward()
- if d[1] == false then
- lolz()
- end
- end
- lolz()
- end
- end
- end
- function retz()
- local togo = 0
- if co[3] < start[3] then
- togo = direction - 4
- elseif co[3] > start[3] then
- togo = direction - 2
- end
- if togo > 0 then
- for i = 1, togo do
- right()
- end
- elseif togo < 0 then
- for i = 1, togo*-1 do
- left()
- end
- end
- if co[3] < start[3] then
- for i = 1, start[3] - co[3] do
- function lolz()
- dig()
- local d = forward()
- if d[1] == false then
- lolz()
- end
- end
- lolz()
- end
- elseif co[3] > start[3] then
- for i = 1, co[3] - start[3] do
- function lolz()
- dig()
- local d = forward()
- if d[1] == false then
- lolz()
- end
- end
- lolz()
- end
- end
- end
- function rety()
- for i = 1, start[2] - co[2] do
- function lolz()
- digup()
- local d = up()
- if d[1] == false then
- lolz()
- end
- end
- lolz()
- end
- end
- function ret()
- retx()
- retz()
- rety()
- end
- function drawlist(title, list, num)
- clear()
- print(title.."\n")
- for i,v in pairs(list) do
- if i == num then
- print("> "..v)
- else
- print(" "..v)
- end
- end
- end
- function getKey()
- local rt = nil
- function lop()
- local e, a1 = os.pullEvent("key")
- if a1 == 200 then
- rt = "up"
- elseif a1 == 208 then
- rt = "down"
- elseif a1 == 28 then
- rt = "enter"
- else
- lop()
- end
- end
- lop()
- repeat sleep(0) until rt ~= nil
- return rt
- end
- function ch(title, list)
- clear()
- local ind = 1
- drawlist(title, list, ind)
- local rt = nil
- cycle = function()
- local ke = getKey()
- if ke == "up" then
- if ind > 1 then
- ind = ind - 1
- else
- ind = #list
- end
- drawlist(title, list, ind)
- cycle()
- elseif ke == "down" then
- if ind < #list then
- ind = ind + 1
- else
- ind = 1
- end
- drawlist(title, list, ind)
- cycle()
- elseif ke == "enter" then
- rt = ind
- end
- end
- cycle()
- repeat sleep(0) until rt ~= nil
- return ind
- end
- function main()
- clear()
- ports()
- print("Turtle server running on "..os.getComputerID())
- if not fs.exists("/save") then
- write("Enter coordinate X: ")
- local X = read()
- write("Enter coordinate Y: ")
- local Y = read()
- write("Enter coordinate Z: ")
- local Z = read()
- sty = tonumber(Y) - 20
- co = {tonumber(X), tonumber(Y), tonumber(Z)}
- start = co
- local fb = fs.open("/start", "w")
- fb.write(textutils.serialize(co))
- fb.close()
- else
- local fd = fs.open("/save", "r")
- co = textutils.unserialize(fd.readAll())
- fd.close()
- local fd2 = fs.open("/direction", "r")
- direction = textutils.unserialize(fd2.readAll())[1]
- fd2.close()
- local fd3 = fs.open("/start", "r")
- start = textutils.unserialize(fd3.readAll())
- fd3.close()
- end
- local ar = ch("What would you like to do?", {"Make turtle mine", "Make turtle come back"})
- if ar == 1 then
- for i = 1, 20 do
- digdown()
- down()
- end
- dp()
- while true do
- if returning == true then break end
- ran()
- end
- else
- ret()
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement