tPlatform_fixed.lua

256 lines · 7.0 KB

Open raw

{program="tPlatform",version="1.20",date="2025-08-23"}

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/tPlatform/tPlatform_fixed.lua
1--{program="tPlatform",version="1.20",date="2025-08-23"}
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 blnAskForParameters=true
27local blnDirectionX=true
28local currentSlot=1
29
30---------------------------------------
31---- tArgs ----------------------------
32---------------------------------------
33local tArgs = {...}
34if #tArgs == 2 then -- no error check
35 blnAskForParameters=false
36 userX=tonumber(tArgs[1])
37 userY=tonumber(tArgs[2])
38end
39
40---------------------------------------
41-- basic functions for turtle control -
42---------------------------------------
43local function mats()
44 if turtle.getItemCount(currentSlot)==0 then
45 currentSlot=currentSlot+1
46 if currentSlot>16 then
47 currentSlot=1
48 print("Out of materials, please restock!")
49 print(" Sleeping for "..cSleepTime.." sec ...")
50 os.sleep(cSleepTime)
51 end
52 turtle.select(currentSlot)
53 mats()
54 end
55end
56
57local function gf()
58 while not turtle.forward() do
59 turtle.attack()
60 turtle.dig()
61 os.sleep(0.2)
62 end
63end
64local function gb()
65 while not turtle.back() do
66 turtle.turnLeft(); turtle.turnLeft()
67 turtle.attack()
68 turtle.dig()
69 turtle.turnLeft(); turtle.turnLeft()
70 os.sleep(0.2)
71 end
72end
73local function gu()
74 while not turtle.up() do
75 turtle.attackUp()
76 turtle.digUp()
77 os.sleep(0.2)
78 end
79end
80local function gd()
81 while not turtle.down() do
82 turtle.attackDown()
83 turtle.digDown()
84 os.sleep(0.2)
85 end
86end
87local function gl() while not turtle.turnLeft() do end end
88local function gr() while not turtle.turnRight() do end end
89local function df() turtle.dig() end
90local function du() turtle.digUp() end
91local function dd() turtle.digDown() end
92local function pf() mats() while not turtle.place() do end end
93local function pu() mats() while not turtle.placeUp() do end end
94local function pd() mats() while not turtle.placeDown() do end end
95local function sf() turtle.suck() end
96local function su() turtle.suckUp() end
97local function sd() turtle.suckDown() end
98local function Df() turtle.drop() end
99local function Du() turtle.dropUp() end
100local function Dd() turtle.dropDown() end
101local function ss(s) turtle.select(s) end
102
103local function askForInputText(textt)
104 local at=""
105 -- check prompting texts
106 if textt==nil then textt="Enter text:" end
107
108 -- ask for input
109 write(textt)
110 at=read()
111 return at
112end
113
114
115local function checkFuel()
116 local tmp=turtle.getFuelLevel()
117 return tmp
118end
119
120-- Attempt to refuel up to a target fuel level using any fuel items in inventory
121local function autoRefuel(targetFuel)
122 local function tryConsumeFuelInSlots()
123 local consumedAny=false
124 for s=1,16 do
125 if turtle.getItemCount(s)>0 then
126 turtle.select(s)
127 if turtle.refuel(0) then
128 turtle.refuel()
129 consumedAny=true
130 if turtle.getFuelLevel()>=targetFuel then
131 return true
132 end
133 end
134 end
135 end
136 return consumedAny
137 end
138
139 if turtle.getFuelLevel()=="unlimited" then return true end
140
141 while turtle.getFuelLevel()<targetFuel do
142 local madeProgress = tryConsumeFuelInSlots()
143 if turtle.getFuelLevel()>=targetFuel then return true end
144 if not madeProgress then
145 print("Out of fuel. Insert coal/fuel. Waiting "..cSleepTime.."s ...")
146 os.sleep(cSleepTime)
147 end
148 end
149 return true
150end
151
152-- Place a block below the turtle if air; skip if already solid
153local function placeBlockDown()
154 if turtle.detectDown() then return true end
155
156 for s=1,16 do
157 if turtle.getItemCount(s)>0 then
158 turtle.select(s)
159 if not turtle.refuel(0) then
160 if turtle.placeDown() then
161 return true
162 end
163 end
164 end
165 end
166
167 print("No placeable blocks found. Waiting "..cSleepTime.."s ...")
168 os.sleep(cSleepTime)
169 return placeBlockDown()
170end
171
172------------------------------------------------------------------------------
173-- main ----------------------------------------------------------------------
174------------------------------------------------------------------------------
175
176-- step 0 usage hints
177term.clear() term.setCursorPos(1,1)
178print("+-------------------------------------+")
179print("| tPlatform v1.20, by Kaikaku |")
180print("+-------------------------------------+")
181print("| Put in building materials in any |")
182print("| slot(s) and press enter. |")
183print("| Platform size: Enter x and y to |")
184print("| determine size. Either when asked |")
185print("| by program or with function call, |")
186print("| e.g., tPlatform 5 10 |")
187print("| If turtle runs out of materials it |")
188print("| waits until resupplied. |")
189print("| Will auto-refuel from inventory. |")
190print("+-------------------------------------+")
191
192-- step 1 get input
193ss(1)
194if blnAskForParameters then
195 askForInputText("Put in materials + press enter!")
196 -- step 1.1 get x
197 write("Enter length x (default&min=3):")
198 userX=read()
199 if userX==nil or userX=="" then userX=3 end
200 userX=tonumber(userX) -- no error check yet
201 if userX<3 then userX=3 end
202
203 -- step 1.2 get y
204 write("Enter width y (default&min=1):")
205 userY=read()
206 if userY==nil or userY=="" then userY=1 end
207 userY=tonumber(userY) -- no error check yet
208 --if userY<2 then userY=2 end
209end
210userX=math.floor(userX)
211userY=math.floor(userY)
212
213-- check fuel level
214local cMinFuel=(userX)*(userY+1)+1
215turtleOk, turtleVal = pcall(checkFuel)
216autoRefuel(cMinFuel)
217if turtle.getFuelLevel()<cMinFuel then
218term.clear() term.setCursorPos(1,1)
219print("+-------------------------------------+")
220print("| tPlatform v1.20, by Kaikaku |")
221print("+-------------------------------------+")
222print("| Unable to reach required fuel level |")
223print("| minimum of about ",cMinFuel," units.")
224print("| Insert coal/fuel and restart. |")
225print("+-------------------------------------+")
226return
227end
228
229-- step 2 build (new)
230print("Let's build something nice:")
231placeBlockDown()
232for row=1,userY do
233 for col=2,userX do
234 gf()
235 placeBlockDown()
236 end
237 if row<userY then
238 if row%2==1 then
239 gr(); gf(); gr()
240 else
241 gl(); gf(); gl()
242 end
243 placeBlockDown()
244 end
245end
246
247print("Done. Looks nice to me ;)")
248os.sleep(0.4)
249print("***************************************")
250print("* Check out YouTube for more videos *")
251print("* and turtle programs by Kaikaku :) *")
252print("***************************************")
253return
254
255-- step 2 loopy loops ;
256-- ... existing code ...