Posted 02 January 2013 - 05:18 AM
This is the 2nd topic I've posted for this program, I hope I'm not breaking any rules, sorry if I am though.
Anyway, I've been attempting to create a program that remembers it's position (just to learn Lua, not for any particular reason) whilst branch-mining. The issue is, with the function I've made (I got help on here for it, but wanted to try and write my own) works fine when just moving froward without obstructions and when moving forward after digging through blocks, but it miscounts by 1 if it has to attack a mob (Even if it has to attack multiple mobs, overall it only ever miscounts by 1)
eg: I've been testing by telling it to move 20 blocks. I type in it's current co-ords, it moves or digs through by 20 and gives me it's final co-ords correctly eg: X = 50 to X = 70. But if I place 3 mobs in its path and tell it to do the task again, it will miscount by 1. eg: X = 50 to X = 69. I can't see what I've done, or how to correct it, so here I am.
Here's the code:
Any input would be greatly appreciated!
Anyway, I've been attempting to create a program that remembers it's position (just to learn Lua, not for any particular reason) whilst branch-mining. The issue is, with the function I've made (I got help on here for it, but wanted to try and write my own) works fine when just moving froward without obstructions and when moving forward after digging through blocks, but it miscounts by 1 if it has to attack a mob (Even if it has to attack multiple mobs, overall it only ever miscounts by 1)
eg: I've been testing by telling it to move 20 blocks. I type in it's current co-ords, it moves or digs through by 20 and gives me it's final co-ords correctly eg: X = 50 to X = 70. But if I place 3 mobs in its path and tell it to do the task again, it will miscount by 1. eg: X = 50 to X = 69. I can't see what I've done, or how to correct it, so here I am.
Here's the code:
-- Variables:
local f = ""
local x = ""
local y = ""
local z = ""
local cvar = ""
-- functions:
function sweep()
term.clear()
term.setCursorPos(1,1)
end
function tleft()
turtle.turnLeft()
if f == 1 or f == 2 or f == 3 then
f=f-1
else
f=3
end
end
function tright()
turtle.turnRight()
if f == 0 or f == 1 or f == 2 then
f=f+1
else
f=0
end
end
function frwd()
digdet()
if f == 0 then
z=z+1
elseif f == 1 then
x=x-1
elseif f == 2 then
z=z-1
else
x=x+1
end
end
function up()
turtle.up()
y=y+1
end
function down()
turtle.down()
y=y-1
end
function digdet()
if turtle.detect() then
turtle.dig()
turtle.forward()
elseif turtle.getFuelLevel() == 0 then
turtle.refuel(all)
else
while not turtle.forward() do
turtle.attack()
end
end
end
-- MAIN PROGRAM:
-- Set Home:
print("Please state whether the turtle is facing N,E,S,W:")
term.write("...")
f=read()
if f == "N" then
f=2
elseif f == "E" then
f=3
elseif f == "S" then
f=0
elseif f == "W" then
f=1
else
f="You entered an incorrect variable!"
os.reboot()
end
print("Facing: "..f)
sweep()
print("Please enter the X, Y & Z coordinates:")
print("...")
term.write("X = ")
x = read()
term.write("Y = ")
y = read()
term.write("Z = ")
z = read()
sweep()
print("F: "..f)
print("X: "..x)
print("Y: "..y)
print("Z: "..z)
term.write("Is this correct? y/n: ")
cvar = read()
if cvar == "n" then
os.reboot()
elseif cvar == "y" then
sweep()
print("Continuing...")
else
print("Incorrect input!")
os.reboot()
end
-- Set Ammount:
for i=1,20 do
frwd()
end
print("F = "..f)
print("X = "..x)
print("Y = "..y)
print("Z = "..z)
Any input would be greatly appreciated!