This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ksbd's profile picture

[Lua][Question] Loop/Function miscounts

Started by ksbd, 02 January 2013 - 04:18 AM
ksbd #1
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:


-- 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!
remiX #2
Posted 02 January 2013 - 05:50 AM
tonumber() the co-ordinates:


term.write("X = ")
  x = tonumber(read())
term.write("Y = ")
  y = tonumber(read())
term.write("Z = ")
  z = tonumber(read())
ksbd #3
Posted 02 January 2013 - 06:26 AM
I think I understand this now. According to the wiki, this converts a string to a 'real' number. What I don't understand however, is how this fixes my code?
In my mind, if it was previously a string because the user enters in the variable, why did it work all the time except when attacking? If you could elaborate on this at all it would be greatly appreciated, but if not, thank you nevertheless!

~Ben
remiX #4
Posted 02 January 2013 - 06:46 AM
I'm also baffled to how it works, but I just always tonumber arguments that must be numbers.

But does it all work fine now?
ksbd #5
Posted 02 January 2013 - 07:22 AM
It does, thank you. That's been giving me trouble for quite some time. I'll be sure to remember it.
ChunLing #6
Posted 02 January 2013 - 04:03 PM
Most arithmetic operators and the numeric for loop automatically attempt to convert a string to number value. But it's a good idea to always tonumber values that you definitely need to be numbers, because if you use them as indexes or for equivalence then that won't be done automatically.