Posted 16 January 2013 - 01:44 AM
So i made these two programs as a bit of practice for my first timid steps into the world of LUA.
the first is a basic mining well program that checks the mineshaft walls for value
and the second is for making underground rooms of veriable size. i have yet to add a place light function but i porbably will in the future
The reason im posting this is because i was curious about the community oppenion. maybee i could get a few pointers.
as they are both of these work but im open to some ideas on how to improve my "technique"
the first is a basic mining well program that checks the mineshaft walls for value
and the second is for making underground rooms of veriable size. i have yet to add a place light function but i porbably will in the future
The reason im posting this is because i was curious about the community oppenion. maybee i could get a few pointers.
as they are both of these work but im open to some ideas on how to improve my "technique"
Spoiler
rednet.open("right")
print("what is the id# of the base PC?: ")
base = tonumber(read())
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
rn (base , "Connection established")
sleep(1)
rn (base , "getting fuel level")
sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
sleep(1)
rn (base , "Current fuel level is "..f.." " )
sleep(1)
if f < 100 then
turtle.getItemCount(16)
rn (base , "current coal stack is")
turtle.select(16)
turtle.refuel(a)
else
rn (base , "Fuel levels are acceptable")
sleep(1)
end
sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
sleep(1)
rn (base, "I have "..fx.." Coal")
sleep(1)
if fx < 64 then
rn (base, "DANGER! Coal is low")
else
rn (base, "Coal storage is full")
sleep(1)
end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
z = 0
--=======return code==========--
function home()
n = 0
while n ~= z do
turtle.up()
n = n + 1
end
rn (base , "Mining Operation finished")
rn (base , "ready for Pickup")
sleep (2)
rn (base , "x")
os.shutdown()
end
--============value===========--
function value()
ts = turtle.select
tc = turtle.compare
if not turtle.detect() then
return false
else
ts(1)
b = turtle.compare()
ts(2)
n = turtle.compare()
ts(3)
m = turtle.compare()
if m or n or b then
return false
else
return true
end
end
end
--============walls===========--
function walls()
for j = 1,4 do
print("value function")
if value() then
turtle.dig()
rn (base, "ore collected at "..z.." ")
turtle.turnRight()
else
turtle.turnRight()
end
end
end
--===========Dig Code=========--
z = 0
while true do
print("2")
f = turtle.getFuelLevel()
if f < z + 10 then
rn (base , "Danger! fuel levels low.")
turtle.select(16)
turtle.refuel()
z = turtle.getFuelLevel()
if f < z + 10 then
rn (base , "Insuficient Coal, Returning.")
home()
end
elseif turtle.detectDown() then
turtle.digDown()
turtle.detectDown()
print("3")
if turtle.detectDown() then
rn (base , "I hit bedrock, Returning")
print("Your home function")
home()
break -- to break out of the while loop
end
else
turtle.down()
rn (base , "fuel levels are "..f.." ")
z = z + 1
rn (base , "Nothing on "..z.." moving down")
print("your walls function")
walls()
end
end
--==========mine room=========--
--== set up rednet peramiters==--
print("what is the Id of base pc?")
a = tonumber(read())
rednet.open ("right")
rn = rednet.send
rn ( a , "Connection established")
--== request room dimintions ==--
print("what is x (forward)")
x = tonumber(read())
print("what is y (right)")
y = tonumber(read())
print("what is z (up)")
z = tonumber(read())
print ("ok is "..x..", " ..y..", "..z.."?")
b = read()
if b ~= "yes" then
os.shutdown()
end
--===== check fuel =====--
f = turtle.getFuelLevel()
f2 = (x*y*z)
rn ( a , "program requires "..f2.." fuel")
sleep (1)
rn( a , "turtle has "..f.." fuel")
if f < f2 then
rn (a , "inceficient fuel, refueling.")
i = 1
turtle.select (1)
for i = 1 , 15 do
turtle.select(i)
j = turtle.getItemCount(i)
turtle.refuel(j)
i = i + 1
end
f = turtle.getFuelLevel()
rn ( a , "fuel level is now "..f.." units")
if f < f2 then
os.shutdown()
end
end
f = turtle.getFuelLevel()
if f < f2 then
rn (a , "No fuel available")
os.shutdown()
end
--=== fuel function ===--
function fuel()
f = turtle.getFuelLevel()
rn ( a , "Fuel is "..f.." units")
if f < 100 then
i = 1
for i = 1 , 15 do
j = turtle.getItemCount(i)
turtle.refuel(j)
i = i + 1
end
f = turtle.getFuelLevel()
if f < 100 then
rn ( a , "refuel failed, add coal")
print ("do i have coal now")
k = read()
if k == yes then
fuel()
end
end
end
end
--=== veriable setup ===--
x2 = 1
z2 = 1
y2 = 1
--==== function x loop ====--
function xloop()
while x2 ~= x do -- mine x distance forward --
turtle.dig()
while turtle.detect() do
turtle.dig()
sleep(.5)
end
turtle.forward()
x2 = x2 + 1
end
while x2 ~= 1 do -- return to x = 1 --
turtle.back()
x2 = x2 - 1
end
rn ( a , "finished x on z= "..z2.." y= "..y2.." ")
fuel()
i = 1
for i = 1 , 13 do
turtle.select(i)
for m = 14 , 16 do
if turtle.compareTo(m) then
p = turtle.getItemCount(i)
turtle.drop(p)
end
end
end
end
--====function z loop===--
function zloop()
xloop()
while z2 ~= z do
turtle.digUp()
turtle.up()
xloop()
z2 = z2 + 1
end
while z2 ~= 1 do
turtle.down()
z2 = z2 - 1
end
rn ( a , "finished z on y = "..y2.." ")
end
--====== y loop =======--
zloop()
while y2 ~= y do
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnLeft()
zloop()
y2 = y2 + 1
end