Posted 18 March 2013 - 04:41 PM
Well I've written this code to do the following:
- Move a massive frame machine to mine in a clock wise direction around an island and slowly get wider and wider.
- Be able to handle chunk unloading/loading (e.g. the computer reseting when the chunk gets unloaded)
- Be fully automatic
Now it has taken a lot of my thought power to work out a lot of the numbers and calculations in this code and I didn't have away to test bits of code until the entire thing was done. Now my first problem is the Save() function I believe, its throwing the error "startup:100: attempt to call nil. I don't see the problem, help? Also if you notice any other problems yell them out to since it taken me a few nights to put this code together.
Also on pastebin which maybe easier to read
http://pastebin.com/JSDeBiS0
- Move a massive frame machine to mine in a clock wise direction around an island and slowly get wider and wider.
- Be able to handle chunk unloading/loading (e.g. the computer reseting when the chunk gets unloaded)
- Be fully automatic
Now it has taken a lot of my thought power to work out a lot of the numbers and calculations in this code and I didn't have away to test bits of code until the entire thing was done. Now my first problem is the Save() function I believe, its throwing the error "startup:100: attempt to call nil. I don't see the problem, help? Also if you notice any other problems yell them out to since it taken me a few nights to put this code together.
Also on pastebin which maybe easier to read
http://pastebin.com/JSDeBiS0
Spoiler
local vars = {}
local Pos = vector.new(0,0,0) --Base Position
local Side = "bottom"
local TargetPos = vector.new(0,0,0) -- Keep Y = 0
local MaxWest = -90
local MaxNorth = -55
local MaxEast = 10
local MaxSouth = 10
local ND = 0
local SD = 0
local Depth = 60
local Direction = "West"
local Drilling = true
if fs.exists("vars") then
local handle_0 = assert(fs.open("vars", "r"), "This maybe a little fucked.")
local input = handle_0.readAll()
handle_0.close()
vars = textutils.unserialize(input)
Pos = vars.pos
TargetPos = vars.targetpos
MaxWest = vars.maxwest
MaxNorth = vars.maxnorth
MaxEast = vars.maxeast
MaxSouth = vars.maxsouth
Direction = vars.direction
Drilling = vars.drilling
end
local function North(int)
print("North: "..int)
for I=1,int do
Pos = Pos:sub(vector.new(0,0,1))
redstone.setBundledOutput(Side,0)
sleep(1.5)
redstone.setBundledOutput(Side,8)
sleep(1.5)
Save()
end
end
local function East(int)
print("East: "..int)
for I=1,int do
Pos = Pos:add(vector.new(1,0,0))
redstone.setBundledOutput(Side,0)
sleep(1.5)
redstone.setBundledOutput(Side,1)
sleep(1.5)
Save()
end
end
local function South(int)
print("South: "..int)
for I=1,int do
Pos = Pos:sub(vector.new(0,0,1))
redstone.setBundledOutput(Side,0)
sleep(1.5)
redstone.setBundledOutput(Side,4)
sleep(1.5)
Save()
end
end
local function West(int)
print("West: "..int)
for I=1,int do
Pos = Pos:sub(vector.new(1,0,0))
redstone.setBundledOutput(Side,0)
sleep(1.5)
redstone.setBundledOutput(Side,2)
sleep(1.5)
Save()
end
end
local function Up(int)
print("Up: "..int)
for I=1,int do
Pos = Pos:add(vector.new(0,1,0))
redstone.setBundledOutput(Side,0)
sleep(1.5)
redstone.setBundledOutput(Side,16)
sleep(1.5)
Save()
end
end
local function Down(int)
print("Down: "..int)
for I=1,int do
Pos = Pos:sub(vector.new(0,1,0))
redstone.setBundledOutput(Side,0)
sleep(1.5)
redstone.setBundledOutput(Side,32)
sleep(1.5)
Save()
if Pos.y == -6 then --ADJUSTABLE
redstone.setOutput("top",true)
end
end
redstone.setOutput("top",false)
end
local function Save()
vars.pos = Pos
vars.targetpos = TargetPos
vars.maxwest = MaxWest
vars.maxnorth = MaxNorth
vars.maxeast = MaxEast
vars.maxsouth = MaxSouth
vars.direction = Direction
vars.drilling = Drilling
local output = textutils.serialize(vars)
local handle_1 = assert(fs.open("vars", "w"), "Shits fucked.")
handle_1.write(output)
handle_1.close()
end
local function Check()
if redstone.getInput("front") then
return true
else
return false
end
end
local function MoveToPos(vec)
local x = vec.x
local y = vec.y
local z = vec.z
if y > Pos.y then
Up(y - Pos.y)
elseif y < Pos.y then
Down(Pos.y - y)
end
if x > Pos.x then
East(x - Pos.x)
elseif x < Pos.x then
West(Pos.x - x)
end
if z > Pos.z then
South(z - Pos.z)
elseif z < Pos.z then
North(Pos.z - z)
end
end
local function Mine()
Drilling = true
Save()
Down(Depth)
Drilling = false
Save()
sleep(1.5)
end
if Drilling then
Down(Depth+Pos.y)
Drilling = false
Save()
end
if ((Pos.x ~= TargetPos.x) or (Pos.z ~= TargetPos.z)) and not Drilling then
MoveToPos(TargetPos)
sleep(1.5)
Mine()
end
while true do
if Check() then
if Check() and (Direction == "West") then
while Pos.x > MaxWest do
TargetPos.x = TargetPos.x - 10
Save()
MoveToPos(TargetPos)
sleep(1.5)
Mine()
end
MaxWest = MaxWest - 10
Direction = "North"
Save()
end
if Check() and (Direction == "North") then
while Pos.z > MaxNorth do
ND = Pos.z - MaxNorth
if ND > 10 then
ND = 10
end
TargetPos.z = TargetPos.z - ND
Save()
MoveToPos(TargetPos)
sleep(1.5)
Mine()
end
MaxNorth = MaxNorth - 10
Direction = "East"
Save()
end
if Check() and (Direction == "East") then
while Pos.x < MaxEast do
TargetPos.x = TargetPos.x + 10
Save()
MoveToPos(TargetPos)
sleep(1.5)
Mine()
end
MaxEast = MaxEast + 10
Direction = "South"
Save()
end
if Check() and (Direction == "South") then
while Pos.z < MaxSouth do
SD = MaxSouth - Pos.z
if SD > 10 then
SD = 10
end
TargetPos.z = TargetPos.z + SD
Save()
MoveToPos(TargetPos)
sleep(1.5)
Mine()
end
MaxSouth = MaxSouth + 10
Direction = "West"
Save()
end
else
sleep(10)
end
end