View difference between Paste ID: VaKu3qug and fwazv1HB
SHOW: | | - or go back to the newest paste.
1
local homeX, homeY, homeZ -- Coordinates of turtle home station
2
homeX = 420
3
homeY = 65
4
homeZ = -104
5
local direction
6
local timeout = 5 -- The timeout of the gps 
7
local safeHeight = 35 --The height that is safe to dig at
8
9
10
function go(dist, dig)
11
    dig = (dig == true)
12
    dist = dist or 1
13
    if(dist <= 0) then return end
14
    for i = 1, dist do
15
        while(not turtle.forward()) do
16
            turtle.attack()
17
            if(turtle.detect()) then
18
                if(dig) then
19
                    turtle.dig()
20
                else
21
                    local x,y,z = gps.locate(timeout, true)
22
                    if(safeHeight<y) then
23
                        goDown(y-safeHeight)
24
                        turtle.dig()
25
                    else
26
                        turtle.dig()
27
                    end
28
                end
29
            end
30
        end
31
    end        
32
end
33
34
function goDown(dist, dig)
35
    dig = true -- Until I find a way to make sure it does go home if it can't go down
36
    dist = dist or 1
37
    if(dist<=0) then return end
38
    for i = 1, dist do
39
        while(not turtle.down()) do
40
            turtle.attackDown()
41
            if(dig) then
42
                turtle.digDown()
43
            else
44
                return false
45
            end
46
        end
47
    end
48
end
49
50
function goUp(dist)
51
    dist = dist or 1
52
    if(dist<=0)then return end
53
    for i = 1, dist do
54
        while(not turtle.up()) do
55
            turtle.up()
56
            turtle.digUp()
57
        end
58
    end
59
end
60
61
function getDirection(dig, times)
62
    dig = dig ~= false
63
    times = times or 1
64
    local loc1 = vector.new(gps.locate(timeout, false))
65
    if(not turtle.forward()) then
66
        if(turtle.detect()) then
67
            local succ, block = turtle.inspect()
68-
            if(block.name == "dirt" or block.name == "cobblestone" or block.name == "stone" or block.name == "grass" or dig) then
68+
            if(block.name == "minecraft:dirt" or block.name == "minecraft:cobblestone" or block.name == "minecraft:stone" or block.name == "minecraft:sand" or block.name == "minecraft:sandstone" or block.name == "minecraft:clay" or block.name == "minecraft:gravel" or block.name == "minecraft:yellow_flower" or block.name == "minecraft:red_flower" or block.name == "minecraft:granite" or block.name == "minecraft:wood" or block.name == "minecraft:double_plant" or block.name == "minecraft:leaves" or block.name == "minecraft:moss_stone" or block.name == "minecraft:grass" or dig) then
69
                turtle.dig()
70
                turtle.forward()
71
            else
72
                if(times<4) then
73
                    turnLeft(false)
74
                    local tmp = getDirection(dig, times+1)
75
                    turnRight(false)
76
                    return (tmp+1)%4
77
                end
78
                return false
79
            end
80
        end
81
    end
82
    local loc2 = vector.new(gps.locate(timeout, false))
83
    heading = loc2-loc1
84
    if(not turtle.back()) then
85
        turtle.turnLeft()
86
        turtle.turnLeft()
87
        while(not turtle.forward()) do
88
            turtle.dig()
89
            turtle.attack()
90
        end
91
        turtle.turnRight()
92
        turtle.turnRight()
93
    end
94
    return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))-1
95
end
96
97
function turnLeft(followDir)
98
    followDir = followDir ~= false
99
    turtle.turnLeft()
100
    if(followDir) then
101
        direction = (direction-1)%4
102
    end
103
end
104
105
function turnRight(followDir)
106
    followDir = followDir ~= false
107
    turtle.turnRight()
108
    if(followDir) then
109
        direction = (direction+1)%4
110
    end
111
end
112
113
function turnTo(dir)
114
    if(dir >= 0 and dir <= 3) then
115
        local turns = (dir-direction)%4
116
        if    (turns == 1 or turns == -3) then
117
            turnRight()
118
        elseif(turns == 2) then
119
            turnRight()
120
            turnRight()
121
        elseif(turns == -1 or turns == 3) then
122
            turnLeft()
123
        elseif(turns == -2) then
124
            turnLeft()
125
            turnLeft()
126
        end
127
    end
128
    if(dir ~= direction) then 
129
        print((dir-direction)%4)
130
        print("Problem in turnTo()") 
131-
        os.sleep(10)
131+
132
end
133
134
function goHome()
135
    local x,y,z = gps.locate(5, false)
136
    
137
--    if(y > safeHeight) then
138
  --      goDown(y-safeHeight)
139
    --end
140
    direction = getDirection(y<=safeHeight)
141
    print("Got direction: "..direction)
142
    if(x>homeX) then
143-
    os.sleep(5)
143+
144
    elseif(x<homeX) then
145
        turnTo(2)
146
    end
147
    go(math.abs(x-homeX))
148
    if(z>homeZ) then
149
        turnTo(1)
150
    elseif(z<homeZ) then
151
        turnTo(3)
152
    end
153
    go(math.abs(z-homeZ))
154
    if(y>homeY) then
155
        goDown(y-homeY)
156
    elseif(y<homeY) then
157
        goUp(homeY-y)
158
    end
159
    if(vector.new(x,y,z)-vector.new(homeX,homeY,homeZ) == 0) then
160
        print("Error in going home equation")
161
        print("Current position: ("..x..","..y..","..z..")")
162
    end
163
end
164
165
os.sleep(3)
166
goHome()