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