View difference between Paste ID: uCaRQCuz and prusdZgu
SHOW: | | - or go back to the newest paste.
1
-- Heimdall116, University of Michigan, August 2014
2-
local function printUsage()
2+
	-- Stole several local functions from dan200's ComputerCraft 1.58 package.
3-
    print( "Usages:" )
3+
	local tArgs = { ... }
4-
    print( "pastebin put <filename>" )
4+
	if #tArgs ~= 1 then
5-
    print( "pastebin get <code> <filename>" )
5+
		print( "Usage: dome <radius>" )
6-
    print( "pastebin run <code> <arguments>" )
6+
		return
7-
end
7+
	end
8-
 
8+
9-
local tArgs = { ... }
9+
	-- Read in the radius
10-
if #tArgs < 2 then
10+
	local radius = tonumber( tArgs[1] )
11-
    printUsage()
11+
	if radius < 1 then
12-
    return
12+
		print( "Radius must be positive" )
13-
end
13+
		return
14-
 
14+
	end
15-
if not http then
15+
16-
    printError( "Pastebin requires http API" )
16+
	-- To be implemented later
17-
    printError( "Set enableAPI_http to true in ComputerCraft.cfg" )
17+
	local depth = 0
18-
    return
18+
	local collected = 0
19-
end
19+
	local facing = 1
20-
 
20+
	local xRel = 0
21-
local function get(paste)
21+
	local yRel = 0
22-
    write( "Connecting to pastebin.com... " )
22+
23-
    local response = http.get(
23+
	local function collect()
24-
        "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
24+
		collected = collected + 1
25-
    )
25+
		if math.fmod(collected, 25) == 0 then
26-
        
26+
			print( "Mined "..collected.." items." )
27-
    if response then
27+
		end
28-
        print( "Success." )
28+
	end
29-
        
29+
30-
        local sResponse = response.readAll()
30+
	local function tryDig()
31-
        response.close()
31+
		while turtle.detect() do
32-
        return sResponse
32+
			if turtle.dig() then
33-
    else
33+
				collect()
34-
        printError( "Failed." )
34+
				sleep(0.5)
35-
    end
35+
			else
36-
end
36+
				return false
37-
 
37+
			end
38-
local sCommand = tArgs[1]
38+
		end
39-
if sCommand == "put" then
39+
		return true
40-
    -- Upload a file to pastebin.com
40+
	end
41-
    -- Determine file to upload
41+
42-
    local sFile = tArgs[2]
42+
	local function tryDigUp()
43-
    local sPath = shell.resolve( sFile )
43+
		while turtle.detectUp() do
44-
    if not fs.exists( sPath ) or fs.isDir( sPath ) then
44+
			if turtle.digUp() then
45-
        print( "No such file" )
45+
				collect()
46-
        return
46+
				sleep(0.5)
47-
    end
47+
			else
48-
    
48+
				return false
49-
    -- Read in the file
49+
			end
50-
    local sName = fs.getName( sPath )
50+
		end
51-
    local file = fs.open( sPath, "r" )
51+
		return true
52-
    local sText = file.readAll()
52+
	end
53-
    file.close()
53+
54-
    
54+
	local function tryDigDown()
55-
    -- POST the contents to pastebin
55+
		while turtle.detectDown() do
56-
    write( "Connecting to pastebin.com... " )
56+
			if turtle.digDown() then
57-
    local key = "0ec2eb25b6166c0c27a394ae118ad829"
57+
				collect()
58-
    local response = http.post(
58+
				sleep(0.5)
59-
        "http://pastebin.com/api/api_post.php", 
59+
			else
60-
        "api_option=paste&"..
60+
				return false
61-
        "api_dev_key="..key.."&"..
61+
			end
62-
        "api_paste_format=lua&"..
62+
		end
63-
        "api_paste_name="..textutils.urlEncode(sName).."&"..
63+
		return true
64-
        "api_paste_code="..textutils.urlEncode(sText)
64+
	end
65-
    )
65+
66-
        
66+
	local function refuel()
67-
    if response then
67+
		local fuelLevel = turtle.getFuelLevel()
68-
        print( "Success." )
68+
		if fuelLevel == "unlimited" or fuelLevel > 0 then
69-
        
69+
			return
70-
        local sResponse = response.readAll()
70+
		end
71-
        response.close()
71+
		
72-
                
72+
		local function tryRefuel()
73-
        local sCode = string.match( sResponse, "[^/]+$" )
73+
			for n=1,16 do
74-
        print( "Uploaded as "..sResponse )
74+
				if turtle.getItemCount(n) > 0 then
75-
        print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
75+
					turtle.select(n)
76-
 
76+
					if turtle.refuel(1) then
77-
    else
77+
						turtle.select(1)
78-
        print( "Failed." )
78+
						return true
79-
    end
79+
					end
80-
    
80+
				end
81-
elseif sCommand == "get" then
81+
			end
82-
    -- Download a file from pastebin.com
82+
			turtle.select(1)
83-
    if #tArgs < 3 then
83+
			return false
84-
        printUsage()
84+
		end
85-
        return
85+
		
86-
    end
86+
		if not tryRefuel() then
87-
 
87+
			print( "Add more fuel to continue." )
88-
    -- Determine file to download
88+
			while not tryRefuel() do
89-
    local sCode = tArgs[2]
89+
				sleep(1)
90-
    local sFile = tArgs[3]
90+
			end
91-
    local sPath = shell.resolve( sFile )
91+
			print( "Resuming Tunnel." )
92-
    if fs.exists( sPath ) then
92+
		end
93-
        print( "File already exists" )
93+
	end
94-
        return
94+
95-
    end
95+
	local function tryUp()
96-
    
96+
		refuel()
97-
    -- GET the contents from pastebin
97+
		while not turtle.up() do
98-
    local res = get(sCode)
98+
			if turtle.detectUp() then
99-
    if res then        
99+
				if not tryDigUp() then
100-
        local file = fs.open( sPath, "w" )
100+
					return false
101-
        file.write( res )
101+
				end
102-
        file.close()
102+
			elseif turtle.attackUp() then
103-
        
103+
				collect()
104-
        print( "Downloaded as "..sFile )
104+
			else
105-
    end 
105+
				sleep( 0.5 )
106-
elseif sCommand == "run" then
106+
			end
107-
    local sCode = tArgs[2]
107+
		end
108-
 
108+
		return true
109-
    local res = get(sCode)
109+
	end
110-
    if res then
110+
111-
        local func, err = loadstring(res)
111+
	local function tryDown()
112-
        if not func then
112+
		refuel()
113-
            printError( err )
113+
		while not turtle.down() do
114-
            return
114+
			if turtle.detectDown() then
115-
        end
115+
				if not tryDigDown() then
116-
        setfenv(func, getfenv())
116+
					return false
117-
        local success, msg = pcall(func, unpack(tArgs, 3))
117+
				end
118-
        if not success then
118+
			elseif turtle.attackDown() then
119-
            printError( msg )
119+
				collect()
120-
        end
120+
			else
121-
    end
121+
				sleep( 0.5 )
122-
else
122+
			end
123-
    printUsage()
123+
		end
124-
    return
124+
		return true
125-
end
125+
	end
126
127
	local function tryForward()
128
		refuel()
129
		while not turtle.forward() do
130
			if turtle.detect() then
131
				if not tryDig() then
132
					return false
133
				end
134
			elseif turtle.attack() then
135
				collect()
136
			else
137
				sleep( 0.5 )
138
			end
139
		end
140
		return true
141
	end
142
143
	local function round(q)
144
		if q == 0 then return 0 end
145
		b = math.abs(q)
146
		if q < 0 then a = -1 else a = 1 end
147
		
148
		if b-math.floor(b) < .5 then
149
			return math.floor(b) * a
150
		else
151
			return math.ceil(b) * a
152
		end
153
	end
154
155
	local function face(f)
156
		while f ~= facing do
157
			turtle.turnRight()
158
			if facing == 4 then
159
				facing = 1
160
			else
161
				facing = facing + 1
162
			end
163
		end
164
	end
165
166
	local function face1(n)
167
	-- Re-orient the turtle WRT original facing: 1=forward; 2=right; 3=back; 4=left
168
		if n-facing == 0 then return
169
		elseif n-facing > 0 and n-facing < 3 then
170
			while n-facing ~= 0 do
171
				turtle.turnRight()
172
				facing = facing + 1
173
				if facing == 5 then facing = 1 end
174
			end
175
		else
176
			turtle.turnLeft()
177
			facing = facing - 1
178
			if facing == 0 then facing = 4 end
179
		end
180
	end
181
182
	local function goTo(x2,y2)
183
		local dx = round(x2-xRel)
184
		local dy = round(y2-yRel)
185
		
186
		if dx == 0 then
187
			-- don't bother turning
188
		elseif dx > 0 then
189
			face(1)
190
		else
191
			face(3)
192
		end
193
		
194
		for n = 1,math.abs(dx) do
195
			tryDig()
196
			tryForward()
197
		end
198
		
199
		if dy == 0 then
200
			-- don't turn
201
		elseif dy > 0 then
202
			face(4)
203
		else
204
			face(2)
205
		end
206
		
207
		for n = 1,math.abs(dy) do
208
			tryDig()
209
			turtle.forward()
210
		end
211
		
212
		xRel = xRel + dx
213
		yRel = yRel + dy
214
	end
215
216
	print( "Building dome..." )
217
218
	for h=1,radius do
219
		-- r is the "local" radius for height "h"
220
		local r = round(radius * math.cos((h-1)/radius * math.pi/2))
221
		print('r='..r)
222
		tryDigUp()
223
		turtle.up()
224
		
225
		-- Plot circle in increments of angle theta
226
		local theta = math.atan(1/r)
227
		
228
		for c = 0,(2*math.pi / theta) do
229
			xNew = r - round(r*math.cos(c*theta))
230
			yNew = round(r*math.sin(c*theta))
231
			goTo(xNew,yNew)
232
			turtle.placeDown()
233
		end
234
	end
235
236
print( "Work complete..." )