Posted 23 September 2013 - 10:30 PM
so im working on this program for redstone in motion to power a quarry or a digging machine just in general and i made a working program you tell it how many blocks to dig and which direction and press enter
here is the basic program
http://pastebin.com/nEBySJB3
that code works as intended
however after finishing this i decided to make functions to clean up the code make it look nicer and now it wont work properly it seems like count and distance are set to the exact same variable or something because it just see's it as done. if i change it to count >= distance it gives a strange error.
http://pastebin.com/0znM4b1c
any help would be greatly appreciated
here is the basic program
http://pastebin.com/nEBySJB3
local down = 0
local up = 1
local north = 2
local south = 3
local west = 4
local east = 5
if
fs.exists("count")
then
h = fs.open("count", "r")
count = h.readLine("count")
h.close()
h = fs.open("distance", "r")
distance = h.readLine("distance")
h.close()
h = fs.open("direction", "r")
direction = h.readLine("direction")
h.close()
else
while true do
print("How far shall i dig master?")
local distance = read()
distance = tonumber(distance)
if
type(distance) == "number"
then
h = fs.open("distance", "w")
h.write(distance)
h.close()
break
else
print("I am terribly sorry sir but that is not a number")
end
end
while true do
print("what direction")
direction = read()
if
(direction == "down") or
(direction == "up") or
(direction == "north") or
(direction == "south") or
(direction == "west") or
(direction == "east")
then
h = fs.open("direction", "w")
h.write(direction)
h.close()
print("You have 10 seconds to get off the rig")
sleep(10)
break
else
print("not a direction")
end
end
h = fs.open("count", "w")
h.write("0")
h.close()
count = 0
end
if
count == distance
then
print("quarry done")
fs.delete("count")
else
drive = peripheral.wrap("top")
drive.move(direction, false, false)
count = count + "1"
h = fs.open("count", "w")
h.write(count)
h.close()
end
that code works as intended
however after finishing this i decided to make functions to clean up the code make it look nicer and now it wont work properly it seems like count and distance are set to the exact same variable or something because it just see's it as done. if i change it to count >= distance it gives a strange error.
http://pastebin.com/0znM4b1c
--this is a simple redstone in motion
--quarry script and can be used
--for many purposes
--your startup file should read
-- first line
-- sleep(4)
--second line
-- shell.run("this script")
-- you can reduce the sleep time but
-- i dont recomend anything less then 2
-- these are for direction
local down = 0
local up = 1
local north = 2
local south = 3
local west = 4
local east = 5
-- function to read from file
function variableRead(fileName, variableName)
h = fs.open(fileName, "r")
variableName = h.readLine(fileName)
h.close()
end
--function to save to file
function variableSave(fileName, variable)
h = fs.open(fileName, "w")
h.write(variable)
h.close()
end
-- lets check if we are already doing something
if
fs.exists("count")
then
variableRead("count", count)
variableRead("distance", distance)
variableRead("direction", direction)
else
-- looks like we are not doing anything yet
-- what do we want to do
while true do
-- how many blocks to dig
print("How far shall i dig master?")
local distance = read()
distance = tonumber(distance)
if
type(distance) == "number"
then
variableSave("distance", distance)
break
else
-- a number was not entered
print("I am terribly sorry sir but that is not a number")
end
end
while true do
-- which direction we going
print("what direction")
direction = read()
if
-- list of choices
(direction == "down") or
(direction == "up") or
(direction == "north") or
(direction == "south") or
(direction == "west") or
(direction == "east")
then
variableSave("direction", direction)
print("You have 10 seconds to get off the rig")
sleep(10)
break
else
-- entered an incorrect direction
print("not a correct direction")
end
end
--create the count file then continue
variableSave("count", "0")
end
-- check count file to see how many
-- times we have moved in chosen
-- direction
if
count == distance
then
-- done with moving delete count file
-- to start over
print("quarry done")
fs.delete("count")
else
-- we are not done move in direction
-- then add one to count and start over
drive = peripheral.wrap("top")
drive.move(direction, false, false)
count = count + "1"
variableSave("count", count)
end
any help would be greatly appreciated