gps-deploy.lua

457 lines · 12.0 KB

Open raw

added logic to check if the server is set to no fuel mode

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/Maengorn/gps-deploy.lua
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
86height = 255
87
88function findWirelessModem()
89 for _, side in ipairs(peripheral.getNames()) do
90 if peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then
91 return side
92 end
93 end
94 return nil
95end
96
97-- need to enable rednet first incase using locate
98local modemSide = findWirelessModem()
99if not modemSide then
100 error("No wireless modem found!")
101end
102rednet.open(modemSide)
103
104local function printUsage()
105 print("")
106 print( "Usages:" )
107 print( "gps-deploy <x> <y> <z> [height]" )
108 print( "Example: gps-deploy 1 1 1 20")
109 print( "would deploy the satelite to y=21")
110 print( "gps-deploy locate [height]")
111 print( "if you have working GPS use this")
112 print( "to find out the coords over GPS")
113end
114
115-- confirm default height isn't set above 254
116-- Check to see if a minimum of 3 values were
117-- passed to the script
118local tArgs = { ... }
119
120if tArgs[1] == "locate" then
121 print ("")
122 print ("Locating GPS signal...")
123 xcord, ycord, zcord = gps.locate(5, false)
124 if xcord==nil then
125 print("")
126 print ("No GPS signal detected, please rerun manually")
127 return
128 end
129 if tArgs[2] == nil then
130 height = tonumber(height)
131 else
132 height = tonumber(tArgs[2])
133 end
134 print ("gps-deploy ",xcord," ",ycord," ",zcord," height: ",height)
135 xcord = tonumber(xcord)
136 ycord = tonumber(ycord)
137 zcord = tonumber(zcord)
138else
139 if #tArgs <= 2 then
140 printUsage()
141 return
142 else
143 xcord = tonumber(tArgs[1])
144 ycord = tonumber(tArgs[2])
145 zcord = tonumber(tArgs[3])
146 if tArgs[4] == nil then
147 height = tonumber(height)
148 else
149 if tonumber(tArgs[4]) > 254 then
150 height = tonumber(height)
151 else
152 height = tonumber(tArgs[4])
153 end
154 end
155 end
156
157end
158
159if height > ycord and height < 256 then
160 height = height-ycord
161end
162
163if height > 255 then
164 height = 255
165end
166
167-- check if the script is running on a turtle
168-- (original code)
169if not turtle then
170 print("")
171 print("Error: not a turtle")
172 return
173end
174
175-- move functions
176-- (original code)
177local mov = {}
178
179mov.forward = function ()
180 while not turtle.forward() do
181 sleep(1)
182 end
183 return true
184end
185mov.back = function ()
186 while not turtle.back() do
187 sleep(1)
188 end
189 return true
190end
191mov.up = function ()
192 while not turtle.up() do
193 sleep(1)
194 end
195 return true
196end
197mov.down = function ()
198 while not turtle.down() do
199 sleep(1)
200 end
201 return true
202end
203mov.place = function ()
204 while not turtle.place() do
205 sleep(1)
206 end
207end
208
209local base = nil
210
211-- Check if we have enough fuel
212-- we estimate the fuel usage ((height*2)+70) needed to
213-- complete the deoployment and then see if we have enough
214-- fuel loaded. If we don't, it checks the first slot for
215-- available fuel and tries to fill up on it. If it doesn't
216-- have enough fuel in there, it prompts the user for more
217-- fuel. It allows 30 seconds for the user to add fuel
218-- (trying to refuel and verify fuel level every second)
219-- and if it doesn't get it's fill within 30 seconds
220-- it exits with a message to the user
221-- neonerZ
222if type(turtle.getFuelLevel()) == "string" then
223 print("No-fuel mode")
224else
225 if turtle.getFuelLevel() < (tonumber(height)*2)+70 then
226 while turtle.getFuelLevel() < (tonumber(height)*2)+70 do
227 turtle.select(1)
228 realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
229 if realcoal>=64 then
230 coal=64
231 else
232 coal=math.ceil(realcoal)
233 end
234 if turtle.refuel(tonumber(coal)) == false then
235 fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
236 print("")
237 print("You ran out of fuel in slot 1")
238 print("Please insert "..fuel.." fuel or "..realcoal.." coal to continue")
239 print("Waiting 30 seconds for fuel or exiting")
240 i=0
241 while i<=30 do
242 sleep(1)
243 realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
244 if realcoal>=64 then
245 coal=64
246 else
247 coal=math.ceil(realcoal)
248 end
249 turtle.refuel(tonumber(coal))
250 if turtle.getFuelLevel() >= (tonumber(height)*2)+70 then
251 print("")
252 print("Turtle Fueled")
253 i=31
254 end
255 if i==30 then
256 fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
257 print("")
258 print("Not enough fuel provided")
259 print("Please provide "..fuel.." fuel or "..realcoal.." coal and try again")
260 return
261 end
262 i=i+1
263 end
264 end
265 end
266 end
267end
268-- if fs.exists("custom") then
269-- print("custom program detected")
270-- print("please Enter base Station number")
271-- local failFlag = false
272-- repeat
273-- if failFlag then
274-- print("Error Not number")
275-- print("try again")
276-- end
277-- write(" > ")
278-- base = tonumber(read())
279-- failFlag = true
280-- until type(base) == "number"
281-- end
282-- print("Please Place 4 computers in slot two")
283-- print("4 modems in slot three")
284-- print("if mineing turtle then")
285-- print("1 disk drive in slot four")
286-- print("if not mineing then")
287-- print("4 disk drive in slot four")
288-- print("blank floopy disk in slot five")
289-- print("press Enter key to continue")
290-- while true do
291-- local event , key = os.pullEvent("key")
292-- if key == 28 then break end
293-- end
294-- print("Launching")
295--end
296
297-- check if the required quantity of items
298-- are in the appropriate spots. I'm sure
299-- there's a more elegant way of doing this.
300-- I don't believe there's a way to check if
301-- the items are correct without using compare
302monitor=0
303modem=0
304diskdrive=0
305disk=0
306print("")
307turtle.select(2)
308if turtle.getItemCount(2) < 4 then
309 print("Please place at least 4 computers into slot two")
310 monitor=1
311end
312turtle.select(3)
313if turtle.getItemCount(2) < 4 then
314 print("Please place at least 4 modems into slot three")
315 modem=1
316end
317turtle.select(4)
318if turtle.getItemCount(2) < 1 then
319 print("Please place 1 disk drive into slot four if a -mining turtle-")
320 print("Please place 4 disk drives into slot four if a -standard turtle-")
321 diskdrive=1
322end
323turtle.select(5)
324if turtle.getItemCount(2) < 1 then
325 print("Please place 1 disk into slot five")
326 disk=1
327end
328
329if monitor == 1 or modem == 1 or diskdrive == 1 or disk == 1 then
330 print("Please fix above issues to continue")
331 return
332end
333
334-- calculate the coordinates of the 4 satelite arrays
335
336newycord=tonumber(ycord)+tonumber(height)
337
338if newycord > 255 then newycord = 255 end
339
340toycordns=newycord
341toycordwe=newycord-3
342
343if toycordns >= 255 or toycordwe >= 255 then
344 toycordns=255
345 toycordwe=252
346end
347
348local set = {}
349set[1] = {x = tonumber(xcord),z = tonumber(zcord)+3,y = tonumber(toycordns)}
350set[2] = {x = tonumber(xcord)-3,z = tonumber(zcord),y = tonumber(toycordwe)}
351set[3] = {x = tonumber(xcord),z = tonumber(zcord)-3,y = tonumber(toycordns)}
352set[4] = {x = tonumber(xcord)+3,z = tonumber(zcord),y = tonumber(toycordwe)}
353
354-- start the climb up to the correct ycord
355while not turtle.up() do
356 term.clear()
357 term.setCursorPos(1,1)
358 term.write("Please get off me")
359end
360if ycord+tonumber(height) >= 255 then
361 while turtle.up() do -- sends it up till it hits max hight
362 end
363else
364 for i = 3,tonumber(height) do
365 turtle.up()
366 end
367end
368
369-- once at the correct height, deploy GPS array
370-- this is a mixture of my own code and the
371-- original code
372for a = 1,4 do
373 --forward two
374 for i = 1,2 do
375 mov.forward()
376 end
377 turtle.select(2)
378 mov.place()
379 mov.back()
380 turtle.select(3)
381 mov.place()
382 mov.down()
383 mov.forward()
384 turtle.select(4)
385 mov.place()
386 turtle.select(5)
387 turtle.drop()
388 -- make a custom disk that starts up the gps host application
389 -- with the correct coordinates and copy it over. also makes
390 -- makes it a startup script so the computers will
391 -- start back up properly after chunk unloading
392 fs.delete("disk/startup")
393 file = fs.open("disk/startup","w")
394 file.write([[
395fs.copy("disk/install","startup")
396fs.delete("disk/startup")
397if fs.exists("disk/custom") then
398 fs.copy("disk/custom","custom")
399 fs.delete("disk/custom")
400end
401print("sleep in 10")
402sleep(10)
403os.reboot()
404]])
405 file.close()
406 if fs.exists("custom") then
407 fs.copy("custom","disk/custom")
408 end
409 file = fs.open("disk/install","w")
410 file.write([[
411if fs.exists("custom") then
412 shell.run("custom","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..","..(base or "nil")..[[)
413else
414 shell.run("gps","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..[[)
415end
416]])
417 file.close()
418 turtle.turnLeft()
419 mov.forward()
420 mov.up()
421 turtle.turnRight()
422 mov.forward()
423 turtle.turnRight()
424 peripheral.call("front","turnOn")
425 mov.down()
426 turtle.suck()
427 turtle.select(3)
428 turtle.dig()
429 mov.up()
430 -- reboot would be here
431 turtle.turnRight()
432 --back 3
433 for i = 1,3 do
434 mov.forward()
435 end
436 turtle.turnLeft()
437 mov.forward()
438 if a == 1 or a == 3 then
439 for i = 1,3 do
440 mov.down()
441 end
442 elseif a == 2 or a == 4 then
443 for i = 1,3 do
444 mov.up()
445 end
446 end
447end
448
449-- goes back down. this is the original code
450-- might be worth editing to come down the same
451-- amount of spaces it went up, but this should
452-- do the job
453while turtle.down() do
454end
455turtle = tMove
456print("")
457print("Finished")