tPlatform.lua

206 lines · 6.0 KB

Open raw

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

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/Kaikaku/tPlatform.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 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() while not turtle.forward() do end end
58local function gb() while not turtle.back() do end end
59local function gu() while not turtle.up() do end end
60local function gd() while not turtle.down() do end end
61local function gl() while not turtle.turnLeft() do end end
62local function gr() while not turtle.turnRight() do end end
63local function df() turtle.dig() end
64local function du() turtle.digUp() end
65local function dd() turtle.digDown() end
66local function pf() mats() while not turtle.place() do end end
67local function pu() mats() while not turtle.placeUp() do end end
68local function pd() mats() while not turtle.placeDown() do end end
69local function sf() turtle.suck() end
70local function su() turtle.suckUp() end
71local function sd() turtle.suckDown() end
72local function Df() turtle.drop() end
73local function Du() turtle.dropUp() end
74local function Dd() turtle.dropDown() end
75local function ss(s) turtle.select(s) end
76
77local function askForInputText(textt)
78 local at=""
79 -- check prompting texts
80 if textt==nil then textt="Enter text:" end
81
82 -- ask for input
83 write(textt)
84 at=read()
85 return at
86end
87
88
89local function checkFuel()
90 local tmp=turtle.getFuelLevel()
91 return tmp
92end
93
94------------------------------------------------------------------------------
95-- main ----------------------------------------------------------------------
96------------------------------------------------------------------------------
97
98-- step 0 usage hints
99term.clear() term.setCursorPos(1,1)
100print("+-------------------------------------+")
101print("| tPlatform v1.10, by Kaikaku |")
102print("+-------------------------------------+")
103print("| Put in building materials in any |")
104print("| slot(s) and press enter. |")
105print("| Platform size: Enter x and y to |")
106print("| determine size. Either when asked |")
107print("| by program or with function call, |")
108print("| e.g., tPlatform 5 10 |")
109print("| If turtle runs out of materials it |")
110print("| waits until resupplied. |")
111print("+-------------------------------------+")
112
113-- step 1 get input
114ss(1)
115if blnAskForParameters then
116 askForInputText("Put in materials + press enter!")
117 -- step 1.1 get x
118 write("Enter depth x (default&min=3):")
119 userX=read()
120 if userX==nil or userX=="" then userX=3 end
121 userX=tonumber(userX) -- no error check yet
122 if userX<3 then userX=3 end
123
124 -- step 1.2 get y
125 write("Enter width y (default&min=1):")
126 userY=read()
127 if userY==nil or userY=="" then userY=3 end
128 userY=tonumber(userY) -- no error check yet
129 --if userY<2 then userY=2 end
130end
131userX=math.floor(userX)
132userY=math.floor(userY)
133
134-- check fuel level
135local cMinFuel=(userX)*(userY+1)+1
136turtleOk, turtleVal = pcall(checkFuel)
137if turtleVal<cMinFuel then
138term.clear() term.setCursorPos(1,1)
139print("+-------------------------------------+")
140print("| tPlatform v1.10, by Kaikaku |")
141print("+-------------------------------------+")
142print("| Please refuel turtle, it needs a |")
143print("| minimum of about ",cMinFuel," fuel units.")
144print("| Tip: Put some fuel (e.g. coal) in |")
145print("| slot 1 and enter: refuel all. |")
146print("| This will consume all(!) fuel |")
147print("| items in the turtle's inventory|")
148print("+-------------------------------------+")
149return
150end
151
152-- step 2 loopy loops ;)
153print("Let's build something nice:")
154-- step 2.1 go to start position
155-- & if odd number go back
156if userY%2==1 then
157 -- odd number of rows
158 for i=1,userX,1 do gf() end
159 blnDirectionX=false
160else
161 -- even number of rows
162 gf() gf() gl() gl()
163 blnDirectionX=true
164end
165
166-- step 2.2 build it
167for iY=1,userY,1 do
168 for iX=1,userX-1 do
169 if iX==1 then
170 if iY~=1 then
171 if blnDirectionX then
172 gl()
173 else
174 gr()
175 end
176 end
177 gb() pf()
178 elseif iX==userX-1 then
179 if iY~=userY then
180 if blnDirectionX then
181 -- right turn
182 gr()
183 else
184 -- left turn
185 gl()
186 end
187 end
188 gb() pf()
189 else -- in between start and end
190 gb() pf()
191 end
192
193 end
194 blnDirectionX=not blnDirectionX
195end
196-- go back within 1st row
197gr()
198for i=1,userY-1,1 do gb() pf() end
199gl() gb() pf()
200
201print("Done. Looks nice to me ;)")
202os.sleep(0.4)
203print("***************************************")
204print("* Check out YouTube for more videos *")
205print("* and turtle programs by Kaikaku :) *")
206print("***************************************")