tPlatform_xyz.lua

228 lines · 6.6 KB

Open raw

{program="tPlatform",version="1.10",date="2016-02-28"}

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/tPlatform/tPlatform_xyz.lua
1--{program="tPlatform",version="1.10",date="2016-02-28"}
2---------------------------------------
3-- tPlatform by Kaikaku
4-- 2016-02-28, v1.10 checks fuel
5-- 2015-03-28, v1.03 select slot 1
6-- 2015-03-21, v1.02 code tidied up
7-- 2013-11-09, v1.01 more compact
8-- 2013-11-02, v1.00 initial
9---------------------------------------
10
11---------------------------------------
12---- ASSUMPTIONS/PRECONDITIONS --------
13---------------------------------------
14-- Turtle movement:
15-- - building space must be empty
16
17---------------------------------------
18---- PARAMETERS -----------------------
19---------------------------------------
20local cSleepTime=10
21
22---------------------------------------
23---- VARIABLES ------------------------
24---------------------------------------
25local userX=3 userY=1
26local userZ=1
27local blnAskForParameters=true
28local blnDirectionX=true
29local currentSlot=1
30
31---------------------------------------
32---- tArgs ----------------------------
33---------------------------------------
34local tArgs = {...}
35if #tArgs == 3 then -- no error check
36 blnAskForParameters=false
37 userX=tonumber(tArgs[1])
38 userY=tonumber(tArgs[2])
39 userZ=tonumber(tArgs[3])
40elseif #tArgs == 2 then -- no error check
41 blnAskForParameters=false
42 userX=tonumber(tArgs[1])
43 userY=tonumber(tArgs[2])
44end
45
46---------------------------------------
47-- basic functions for turtle control -
48---------------------------------------
49local function mats()
50 if turtle.getItemCount(currentSlot)==0 then
51 currentSlot=currentSlot+1
52 if currentSlot>16 then
53 currentSlot=1
54 print("Out of materials, please restock!")
55 print(" Sleeping for "..cSleepTime.." sec ...")
56 os.sleep(cSleepTime)
57 end
58 turtle.select(currentSlot)
59 mats()
60 end
61end
62
63local function gf() while not turtle.forward() do end end
64local function gb() while not turtle.back() do end end
65local function gu() while not turtle.up() do end end
66local function gd() while not turtle.down() do end end
67local function gl() while not turtle.turnLeft() do end end
68local function gr() while not turtle.turnRight() do end end
69local function df() turtle.dig() end
70local function du() turtle.digUp() end
71local function dd() turtle.digDown() end
72local function pf() mats() while not turtle.place() do end end
73local function pu() mats() while not turtle.placeUp() do end end
74local function pd() mats() while not turtle.placeDown() do end end
75local function sf() turtle.suck() end
76local function su() turtle.suckUp() end
77local function sd() turtle.suckDown() end
78local function Df() turtle.drop() end
79local function Du() turtle.dropUp() end
80local function Dd() turtle.dropDown() end
81local function ss(s) turtle.select(s) end
82
83local function askForInputText(textt)
84 local at=""
85 -- check prompting texts
86 if textt==nil then textt="Enter text:" end
87
88 -- ask for input
89 write(textt)
90 at=read()
91 return at
92end
93
94
95local function checkFuel()
96 local tmp=turtle.getFuelLevel()
97 return tmp
98end
99
100------------------------------------------------------------------------------
101-- main ----------------------------------------------------------------------
102------------------------------------------------------------------------------
103
104-- step 0 usage hints
105term.clear() term.setCursorPos(1,1)
106print("+-------------------------------------+")
107print("| tPlatform v1.10, by Kaikaku |")
108print("+-------------------------------------+")
109print("| Put in building materials in any |")
110print("| slot(s) and press enter. |")
111print("| Platform size: Enter x and y to |")
112print("| determine size, and optional z |")
113print("| height. Either when asked |")
114print("| by program or with function call, |")
115print("| e.g., tPlatform 5 10 3 |")
116print("| If turtle runs out of materials it |")
117print("| waits until resupplied. |")
118print("+-------------------------------------+")
119
120-- step 1 get input
121ss(1)
122if blnAskForParameters then
123 askForInputText("Put in materials + press enter!")
124 -- step 1.1 get x
125 write("Enter depth x (default&min=3):")
126 userX=read()
127 if userX==nil or userX=="" then userX=3 end
128 userX=tonumber(userX) -- no error check yet
129 if userX<3 then userX=3 end
130
131 -- step 1.2 get y
132 write("Enter width y (default&min=1):")
133 userY=read()
134 if userY==nil or userY=="" then userY=1 end
135 userY=tonumber(userY) -- no error check yet
136 --if userY<2 then userY=2 end
137 -- step 1.3 get z
138 write("Enter height z (default&min=1):")
139 userZ=read()
140 if userZ==nil or userZ=="" then userZ=1 end
141 userZ=tonumber(userZ)
142 if userZ<1 then userZ=1 end
143end
144userX=math.floor(userX)
145userY=math.floor(userY)
146userZ=math.floor(userZ)
147
148-- check fuel level
149local cMinFuel=(userX)*(userY+1)+1
150-- account for z layers and vertical travel (up and down between layers)
151cMinFuel = userZ * cMinFuel + 2*(userZ-1)
152turtleOk, turtleVal = pcall(checkFuel)
153if turtleVal<cMinFuel then
154term.clear() term.setCursorPos(1,1)
155print("+-------------------------------------+")
156print("| tPlatform v1.10, by Kaikaku |")
157print("+-------------------------------------+")
158print("| Please refuel turtle, it needs a |")
159print("| minimum of about ",cMinFuel," fuel units.")
160print("| Tip: Put some fuel (e.g. coal) in |")
161print("| slot 1 and enter: refuel all. |")
162print("| This will consume all(!) fuel |")
163print("| items in the turtle's inventory|")
164print("+-------------------------------------+")
165return
166end
167
168-- step 2 loopy loops ;)
169print("Let's build something nice:")
170for iZ=1,userZ,1 do
171 if iZ>1 then gu() end
172-- step 2.1 go to start position
173-- & if odd number go back
174if userY%2==1 then
175 -- odd number of rows
176 for i=1,userX,1 do gf() end
177 blnDirectionX=false
178else
179 -- even number of rows
180 gf() gf() gl() gl()
181 blnDirectionX=true
182end
183
184-- step 2.2 build it
185for iY=1,userY,1 do
186 for iX=1,userX-1 do
187 if iX==1 then
188 if iY~=1 then
189 if blnDirectionX then
190 gl()
191 else
192 gr()
193 end
194 end
195 gb() pf()
196 elseif iX==userX-1 then
197 if iY~=userY then
198 if blnDirectionX then
199 -- right turn
200 gr()
201 else
202 -- left turn
203 gl()
204 end
205 end
206 gb() pf()
207 else -- in between start and end
208 gb() pf()
209 end
210
211 end
212 blnDirectionX=not blnDirectionX
213end
214-- go back within 1st row
215gr()
216for i=1,userY-1,1 do gb() pf() end
217gl() gb() pf()
218end
219
220-- return to original height
221for i=1,userZ-1,1 do gd() end
222
223print("Done. Looks nice to me ;)")
224os.sleep(0.4)
225print("***************************************")
226print("* Check out YouTube for more videos *")
227print("* and turtle programs by Kaikaku :) *")
228print("***************************************")