View difference between Paste ID: Y6AFr7k4 and qLthLak5
SHOW: | | - or go back to the newest paste.
1
--
2
--GPS Deploy by neonerZ v1.1
3
--http://youtube.com/neonerz
4
--
5
--This script is originally from BigSHinyToys from ComputerCraft.info
6
--Original link can be found here:
7
--http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/page__p__28333#entry28333
8
--Edited by neonerZ
9
--
10
-- Modifications included:
11
--
12
-- happydude11209: Line 209 - added logic to check if the server is set to no fuel mode
13
-- http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-10/page__view__findpost__p__123411
14
--
15
-- kittykiller: Line 110 - Bug in the locate code if you were using an existing GPS system to deploy a new one
16
-- http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-10/page__view__findpost__p__80323
17
--
18
-- Mad_Professor: Line 296 - Bug calling computers, monitors. Making people think they needed to load 4 monitors
19
-- http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-10/page__view__findpost__p__150291
20
--
21
--
22
--
23
-- In order to use this script you need to setup
24
-- either a mining turtle, or a standard turtle
25
-- Mining Turtle: Slot 1 - Fuel
26
--				  Slot 2 - At least 4 computers
27
--				  Slot 3 - At least 4 modems
28
--				  Slot 4 - At lease 1 disk drive
29
--				  Slot 5 - 1 Disk
30
-- Standard Turtle: Slot 1 - Fuel
31
--				  	Slot 2 - At least 4 computers
32
--				  	Slot 3 - At least 4 modems
33
--				  	Slot 4 - At lease 4 disk drives
34
--				 	Slot 5 - 1 Disk		
35
--
36
-- (mining turtles will be able to reuse the same
37
--	disk drive, where-as a standard turtle will leave
38
--  them and deploy a separate disk drive for each
39
--	GPS host)
40
--
41
-- Default Usage: Place the turtle where you want it
42
--		deployed facing the SOUTH or 0 direction.
43
--		Fill the turtle with the required materials
44
--		above. Then use the command
45
--
46
--		gps-deploy x y z
47
--
48
--		Where x, y, z is the *absolute* positions of the deploment
49
--		turtle. By default the turtle will deploy the
50
--		the GPS array at around y = 254. Add a fourth
51
--		value to specify the height offset.
52
--		IMPORTANT: It's crucial you use your absolute coordinates,
53
--		 (the ones inside the parentheses next to your realitive coordinates)
54
--		 For Example: If F3 shows X = -6.43534 (-7) or Z = -15.542 (-16)
55
--		 you'd use -7 and -16 respectively. If you use -6 and -15 all coordinates
56
--		 that go past 0 in the opposite direction will be off by 1.)
57
--
58
--		Example: gps-deploy 1 1 1 20
59
--
60
--		Would assume the turtle's start position is 
61
--		x=1, y=1, z=1 and will deploy the satelite 
62
--		array at y= 21
63
--
64
--neonerZ added features
65
--	Smart Refilling: Fuel should go in slot 1.
66
--		If not enough fuel is available script
67
--		will prompt user for more fuel and wait 30. 
68
--		Script will estimate how much fuel is needed
69
--		and try to take only that much (in coal)
70
--	Item Detection: Script will check slots 2-5
71
--		for the correct quantity of items. It does
72
--		*not* check if items are valid
73
--	GPS Host Coordinates: The script now requires
74
--		you to enter in the coordinates of the 
75
--		turtle before launching. This will set
76
--		the GPS host computers to the correct
77
--		coordinates.
78
--	Satelite Array Location: The script allows
79
--		the user to set an offset for the placement
80
--		of the four satelites 
81
82
-- How heigh up should the satellite be deployed?
83
-- This is the default value if you don't pass a
84
-- value to the script. There is a check down below
85
-- to make sure the user entered values isn't > 254
86
height = 255
87
88
-- need to enable rednet first incase using locate
89
rednet.open( "right" )
90
91
local function printUsage()
92
	print("")
93
	print( "Usages:" )
94
	print( "gps-deploy <x> <y> <z> [height]" )
95
	print( "Example: gps-deploy 1 1 1 20")
96
	print( "would deploy the satelite to y=21")
97
	print( "gps-deploy locate [height]")
98
	print( "if you have working GPS use this")
99
	print( "to find out the coords over GPS")
100
end
101
102
-- confirm default height isn't set above 254
103
-- Check to see if a minimum of 3 values were
104
-- passed to the script
105
local tArgs = { ... }
106
107
if tArgs[1] == "locate" then
108
	print ("")
109
	print ("Locating GPS signal...")
110
	xcord, ycord, zcord = gps.locate(5, false)
111
	if xcord==nil then
112
		print("")
113
		print ("No GPS signal detected, please rerun manually")
114
		return
115
	end
116
	if tArgs[2] == nil then
117
		height = tonumber(height)
118
	else
119
		height = tonumber(tArgs[2])
120
	end
121
	print ("gps-deploy ",xcord," ",ycord," ",zcord," height: ",height)
122
	xcord = tonumber(xcord)
123
	ycord = tonumber(ycord)
124
	zcord = tonumber(zcord)
125
else
126
	if #tArgs <= 2 then
127
		printUsage()
128
		return
129
	else
130
		xcord = tonumber(tArgs[1])
131
		ycord = tonumber(tArgs[2])
132
		zcord = tonumber(tArgs[3])	
133
		if tArgs[4] == nil then
134
			height = tonumber(height)
135
		else
136
			if tonumber(tArgs[4]) > 254 then
137
				height = tonumber(height)
138
			else
139
				height = tonumber(tArgs[4])
140
			end
141
		end
142
	end
143
144
end
145
146
if height > ycord and height < 256 then
147
	height = height-ycord
148
end
149
150
if height > 255 then
151
	height = 255
152
end
153
154
-- check if the script is running on a turtle
155
-- (original code)
156
if not turtle then
157
	print("")
158
	print("Error: not a turtle")
159
	return
160
end
161
162
-- move functions
163
-- (original code)
164
local mov = {}
165
166
mov.forward = function ()
167
	while not turtle.forward() do
168
		sleep(1)
169
	end
170
	return true
171
end
172
mov.back = function ()
173
	while not turtle.back() do
174
		sleep(1)
175
	end
176
	return true
177
end
178
mov.up = function ()
179
	while not turtle.up() do
180
		sleep(1)
181
	end
182
	return true
183
end
184
mov.down = function ()
185
	while not turtle.down() do
186
		sleep(1)
187
	end
188
	return true
189
end
190
mov.place = function ()
191
	while not turtle.place() do
192
		sleep(1)
193
	end
194
end
195
196
local base = nil
197
198
-- Check if we have enough fuel
199
-- we estimate the fuel usage ((height*2)+70) needed to
200
-- complete the deoployment and then see if we have enough
201
-- fuel loaded. If we don't, it checks the first slot for
202
-- available fuel and tries to fill up on it. If it doesn't
203
-- have enough fuel in there, it prompts the user for more 
204
-- fuel. It allows 30 seconds for the user to add  fuel
205
-- (trying to refuel and verify fuel level every second) 
206
-- and if it doesn't get it's fill within 30 seconds
207
-- it exits with a message to the user
208
-- neonerZ
209
if type(turtle.getFuelLevel()) == "string" then
210
        print("No-fuel mode")
211
else
212
	if turtle.getFuelLevel() < (tonumber(height)*2)+70 then
213
		while turtle.getFuelLevel() < (tonumber(height)*2)+70 do
214
			turtle.select(1)
215
			realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
216
			if realcoal>=64 then
217
				coal=64
218
			else
219
				coal=math.ceil(realcoal)
220
			end
221
			if turtle.refuel(tonumber(coal)) == false then
222
				fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
223
				print("")
224
				print("You ran out of fuel in slot 1")
225
				print("Please insert "..fuel.." fuel or "..realcoal.." coal to continue")
226
				print("Waiting 30 seconds for fuel or exiting")
227
				i=0
228
				while i<=30 do
229
					sleep(1)
230
					realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
231
					if realcoal>=64 then
232
						coal=64
233
					else
234
						coal=math.ceil(realcoal)
235
					end
236
					turtle.refuel(tonumber(coal))
237
					if turtle.getFuelLevel() >= (tonumber(height)*2)+70 then
238
						print("")
239
						print("Turtle Fueled")
240
						i=31
241
					end
242
					if i==30 then
243
						fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
244
						print("")
245
						print("Not enough fuel provided")
246
						print("Please provide "..fuel.." fuel or "..realcoal.." coal and try again")
247
						return
248
					end
249
					i=i+1
250
				end
251
			end
252
		end
253
	end
254
end
255
--	if fs.exists("custom") then
256
--		print("custom program detected")
257
--		print("please Enter base Station number")
258
--		local failFlag = false
259
--		repeat
260
--			if failFlag then
261
--				print("Error Not number")
262
--				print("try again")
263
--			end
264
--			write(" > ")
265
--			base = tonumber(read())
266
--			failFlag = true
267
--		until type(base) == "number"
268
--	end
269
--	print("Please Place 4 computers in slot two")
270
--	print("4 modems in slot three")
271
--	print("if mineing turtle then")
272
--	print("1 disk drive in slot four")
273
--	print("if not mineing then")
274
--	print("4 disk drive in slot four")
275
--	print("blank floopy disk in slot five")
276
--	print("press Enter key to continue")
277
--	while true do
278
--		local event , key = os.pullEvent("key")
279
--		if key == 28 then break end
280
--	end
281
--	print("Launching")
282
--end
283
284
-- check if the required quantity of items
285
-- are in the appropriate spots. I'm sure
286
-- there's a more elegant way of doing this.
287
-- I don't believe there's a way to check if
288
-- the items are correct without using compare
289
monitor=0
290
modem=0
291
diskdrive=0
292
disk=0
293
print("")
294
turtle.select(2)
295
if turtle.getItemCount(2) < 4 then
296
	print("Please place at least 4 computers into slot two")
297
	monitor=1
298
end
299
turtle.select(3)
300
if turtle.getItemCount(2) < 4 then
301
	print("Please place at least 4 modems into slot three")
302
	modem=1
303
end
304
turtle.select(4)
305
if turtle.getItemCount(2) < 1 then
306
	print("Please place 1 disk drive into slot four if a -mining turtle-")
307
	print("Please place 4 disk drives into slot four if a -standard turtle-")
308
	diskdrive=1
309
end
310
turtle.select(5)
311
if turtle.getItemCount(2) < 1 then
312
	print("Please place 1 disk into slot five")
313
	disk=1
314
end
315
316
if monitor == 1 or modem == 1 or diskdrive == 1 or disk == 1 then
317
	print("Please fix above issues to continue")
318
	return
319
end
320
321
-- calculate the coordinates of the 4 satelite arrays
322
323
newycord=tonumber(ycord)+tonumber(height)
324
325
if newycord > 255 then newycord = 255 end
326
327
toycordns=newycord
328
toycordwe=newycord-3
329
330
if toycordns >= 255 or toycordwe >= 255 then
331
	toycordns=255
332
	toycordwe=252
333
end
334
335
local set = {}
336
set[1] = {x = tonumber(xcord),z = tonumber(zcord)+3,y = tonumber(toycordns)}
337
set[2] = {x = tonumber(xcord)-3,z = tonumber(zcord),y = tonumber(toycordwe)}
338
set[3] = {x = tonumber(xcord),z = tonumber(zcord)-3,y = tonumber(toycordns)}
339
set[4] = {x = tonumber(xcord)+3,z = tonumber(zcord),y = tonumber(toycordwe)}
340
341
-- start the climb up to the correct ycord
342
while not turtle.up() do
343
	term.clear()
344
	term.setCursorPos(1,1)
345
	term.write("Please get off me")
346
end
347
if ycord+tonumber(height) >= 255 then
348
	while turtle.up() do -- sends it up till it hits max hight
349
	end
350
else
351
	for i = 3,tonumber(height) do
352
	turtle.up()
353
	end
354
end
355
356
-- once at the correct height, deploy GPS array
357
-- this is a mixture of my own code and the
358
-- original code
359
for a = 1,4 do
360
	--forward two
361
	for i = 1,2 do
362
		mov.forward()
363
	end
364
	turtle.select(2)
365
	mov.place()
366
	mov.back()
367
	turtle.select(3)
368
	mov.place()
369
	mov.down()
370
	mov.forward()
371
	turtle.select(4)
372
	mov.place()
373
	turtle.select(5)
374
	turtle.drop()
375
	-- make a custom disk that starts up the gps host application
376
	-- with the correct coordinates and copy it over. also makes 
377
	-- makes it a startup script so the computers will
378
	-- start back up properly after chunk unloading
379
	fs.delete("disk/startup")
380
	file = fs.open("disk/startup","w")
381
	file.write([[
382
fs.copy("disk/install","startup")
383
fs.delete("disk/startup")
384
if fs.exists("disk/custom") then
385
	fs.copy("disk/custom","custom")
386
	fs.delete("disk/custom")
387
end
388
print("sleep in 10")
389
sleep(10)
390
os.reboot()
391
]])
392
	file.close()
393
	if fs.exists("custom") then
394
		fs.copy("custom","disk/custom")
395
	end
396
		file = fs.open("disk/install","w")
397
		file.write([[
398
if fs.exists("custom") then
399
	shell.run("custom","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..","..(base or "nil")..[[)
400
else
401
	shell.run("gps","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..[[)
402
end
403
]])
404
		file.close()
405
	turtle.turnLeft()
406
	mov.forward()
407
	mov.up()
408
	turtle.turnRight()
409
	mov.forward()
410
	turtle.turnRight()
411
	peripheral.call("front","turnOn")
412
	mov.down()
413
	turtle.suck()
414
	turtle.select(3)
415
	turtle.dig()
416
	mov.up()
417
	-- reboot would be here
418
	turtle.turnRight()
419
	--back 3
420
	for i = 1,3 do
421
		mov.forward()
422
	end
423
	turtle.turnLeft()
424
	mov.forward()
425
	if a == 1 or a == 3 then
426
		for i = 1,3 do
427
		mov.down()
428
		end
429
	elseif a == 2 or a == 4 then
430
		for i = 1,3 do
431
		mov.up()
432
		end
433
	end
434
end
435
436
-- goes back down. this is the original code
437
-- might be worth editing to come down the same
438
-- amount of spaces it went up, but this should
439
-- do the job
440
while turtle.down() do 
441
end
442
turtle = tMove
443
print("")
444
print("Finished")