View difference between Paste ID: 0Bn7RYPa and fbyxVzSX
SHOW: | | - or go back to the newest paste.
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
---------------------------------------
20
local cSleepTime=10
21
22
---------------------------------------
23
---- VARIABLES ------------------------ 
24
---------------------------------------
25
local userX=3 userY=1
26
local blnAskForParameters=true
27
local blnDirectionX=true
28
local currentSlot=1
29
30
---------------------------------------
31
---- tArgs ----------------------------
32
---------------------------------------
33
local tArgs = {...}
34
if #tArgs == 2 then -- no error check
35
  blnAskForParameters=false
36
  userX=tonumber(tArgs[1])
37
  userY=tonumber(tArgs[2])
38
end
39
40
---------------------------------------
41
-- basic functions for turtle control -
42
---------------------------------------
43
local 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
55
end
56
57
local function gf()  while not turtle.forward()   do end end
58
local function gb()  while not turtle.back()      do end end
59
local function gu()  while not turtle.up()        do end end
60
local function gd()  while not turtle.down()      do end end
61
local function gl()  while not turtle.turnLeft()  do end end
62
local function gr()  while not turtle.turnRight() do end end
63
local function df()  turtle.dig()       end
64
local function du()  turtle.digUp()     end
65
local function dd()  turtle.digDown()   end
66
local function pf()  mats() while not turtle.place()     do end end
67
local function pu()  mats() while not turtle.placeUp()   do end end
68
local function pd()  mats() while not turtle.placeDown() do end end
69
local function sf()  turtle.suck()      end
70
local function su()  turtle.suckUp()    end
71
local function sd()  turtle.suckDown()  end
72
local function Df()  turtle.drop()      end
73
local function Du()  turtle.dropUp()    end
74
local function Dd()  turtle.dropDown()  end
75
local function ss(s) turtle.select(s)   end
76
77
local 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
86
end
87
88
89
local function checkFuel()
90
  local tmp=turtle.getFuelLevel()
91
  return tmp
92
end
93
94
------------------------------------------------------------------------------
95
-- main ----------------------------------------------------------------------
96
------------------------------------------------------------------------------
97
98
-- step 0 usage hints
99
term.clear() term.setCursorPos(1,1)
100
print("+-------------------------------------+")
101
print("| tPlatform v1.10, by Kaikaku         |")
102
print("+-------------------------------------+")
103
print("| Put in building materials in any    |")
104
print("|   slot(s) and press enter.          |")
105
print("| Platform size: Enter x and y to     |")
106
print("|   determine size. Either when asked |")
107
print("|   by program or with function call, |")
108
print("|   e.g., tPlatform 5 10              |")
109
print("| If turtle runs out of materials it  |")
110
print("|   waits until resupplied.           |")
111
print("+-------------------------------------+")
112
 
113
-- step 1 get input
114
ss(1)
115
if 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
130
end  
131
userX=math.floor(userX)
132
userY=math.floor(userY)
133
134
-- check fuel level
135
local cMinFuel=(userX)*(userY+1)+1
136
turtleOk, turtleVal = pcall(checkFuel)
137
if turtleVal<cMinFuel then
138
term.clear() term.setCursorPos(1,1)
139
print("+-------------------------------------+")
140
print("| tPlatform v1.10, by Kaikaku         |")
141
print("+-------------------------------------+")
142
print("| Please refuel turtle, it needs a    |")
143
print("| minimum of about ",cMinFuel," fuel units.")
144
print("| Tip: Put some fuel (e.g. coal) in   |")
145
print("|      slot 1 and enter: refuel all.  |")
146
print("|      This will consume all(!) fuel  |")
147
print("|      items in the turtle's inventory|")
148
print("+-------------------------------------+")
149
return
150
end
151
152
-- step 2 loopy loops ;)
153
print("Let's build something nice:")
154
-- step 2.1 go to start position 
155
--          & if odd number go back
156
if userY%2==1 then 
157
  -- odd number of rows
158
  for i=1,userX,1 do gf() end
159
  blnDirectionX=false
160
else
161
  -- even number of rows
162
  gf() gf() gl() gl()
163
  blnDirectionX=true
164
end	
165
166
-- step 2.2 build it
167
for 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
195
end
196
-- go back within 1st row
197
gr()
198
for i=1,userY-1,1 do gb() pf() end
199
gl() gb() pf()
200
  
201
print("Done. Looks nice to me ;)")
202
os.sleep(0.4)
203
print("***************************************")
204
print("* Check out YouTube for more videos   *")
205
print("* and turtle programs by Kaikaku :)   *")
206
print("***************************************")