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

Program error.

Started by detsuo04, 13 January 2013 - 09:48 PM
detsuo04 #1
Posted 13 January 2013 - 10:48 PM
Ok so i started messing with computer craft today and this is my result so far. However Im getting an error. string miner :93: "=" expected, or something to that effect.
The first chunk just deals with its initial startup and communication. all that is tested and works.
i have gone over the code several times but im not sure whats wrong. If anyone could give it a look i would apreciate it.
Thanks for the help.


rednet.open("right")
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")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
td = turtle.dig
tm = turtle.move
tc = turtle.detect
--===========fuel=============--
turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
  turtle.getFuelLevel()
    if f < z + 10 then
    rn (base , "Insuficient Coal, Returning.")
    home()
    else
    end
  else
  end
--=======return code==========--
function home()
  tcUp(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
  end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
    turtle.select(i)
    if turtle.compare() then
    return false
    else
    return true
    end
  end
--============walls===========--
function walls()
  for j = 1,4 do
    if value() then
    td()
    rn (base, "ORE!")
	  else
	  turtle.turn("right")
	  end
end
--=============home===========--
function home()
tmUp(z)
rn (base , "Done, ready for pickup")
os.shutdown

--===========Dig Code=========--
function mine()
fuel()
  tcDown()
   if true then
   tdDown()
   tcDown()
    if true then
    rn (base , "I hit bedrock, Returning")
    home()
	  else
	  tmDown()
	  walls()
	  z = z + 1
	  else
	   tmDown()
	   walls()
	   z = z + 1
	   end
   end
   --===========start=========--
   while true do
   mine()
   end
  
crazyguymgd #2
Posted 13 January 2013 - 10:59 PM
you're missing ends to a lot of your functions. The one's I found were to value(), walls(), home(), and mine().
Also, turtle.turnRight() not turtle.turn("right")
In your home() function you use os.shutdown instead of os.shutdown()…
and maybe some other things. try going through your code and seeing if you can find any more problems :P/> debugging code is like a game, the more problems you find, the more points you get. so far, I'm winning.
detsuo04 #3
Posted 13 January 2013 - 11:04 PM
HA ok ill fix the errors and get back to you.

actually im a little fuzzy on what denotes the need for end

im not sure how many to put
crazyguymgd #4
Posted 13 January 2013 - 11:12 PM
examples:


function foo()
  ... 
end

if condition then
   ...
end

if condition then
   if condition2 then
      ...
   end
else
  ...
end

while something do
  ...
end

for x = 1, n do
  ...
end

and so on
detsuo04 #5
Posted 13 January 2013 - 11:21 PM
ok so i added the ends and fixed the errors you mentioned now im getting a eof error at 56? which i suspect has something to do with that else end pile there. but im not sure whats wrong with it

rednet.open("right")
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")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
td = turtle.dig
tm = turtle.move
tc = turtle.detect
--===========fuel=============--
turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
  turtle.getFuelLevel()
    if f < z + 10 then
    rn (base , "Insuficient Coal, Returning.")
    home()
    end
    else
    end
  else
end
--=======return code==========--
function home()
  tcUp(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
    turtle.select(i)
    if turtle.compare() then
    return false
    end
    else
    return true
    end
end
--============walls===========--
function walls()
  for j = 1,4 do
    if value() then
    td()
    rn (base, "ORE!")
    end
	  else
	  turtle.turnright()
	  end
  end
end
--=============home===========--
function home()
tmUp(z)
rn (base , "Done, ready for pickup")
os.shutdown()
end

--===========Dig Code=========--
function mine()
fuel()
  tcDown()
   if true then
   tdDown()
   tcDown()
    if true then
    rn (base , "I hit bedrock, Returning")
    home()
    end
	  else
	  tmDown()
	  walls()
	  z == z + 1
	  end
	  else
	   tmDown()
	   walls()
	   z == z + 1
	   end
   end
   --===========start=========--
   while true do
   mine()
   end
  
crazyguymgd #6
Posted 13 January 2013 - 11:29 PM
well the extra else's aren't necessary so did you try just getting rid of them? also, there are a couple of places where you have


if condition then
   ...
end
else
  ...
end

that first end isn't necessary. so you can find those and get rid of them too.
detsuo04 #7
Posted 13 January 2013 - 11:38 PM
just to clerify

if condition then
if condition then
….
end
else
end

right?
crazyguymgd #8
Posted 13 January 2013 - 11:42 PM
so nested ifs?


if condition then
  if condition then
    -- do something
  end
else
    -- do something else
end

but you only need that else if you're actually going to do something else. otherwise just an end.
if that's not clear i'm sure there's a wikipedia page that explains ifs and elses better than I can. or someone else on here.
detsuo04 #9
Posted 13 January 2013 - 11:58 PM
awsome you are mega helpful. ok so i have finished the edits and the program returns no errors. however it ends without doing anything. kindof confused on this since its giving me no feed back. right now im looking over my dig function for a reason
crazyguymgd #10
Posted 14 January 2013 - 12:01 AM
it often helps to put some debugging print statements in this situation. also, make sure you're actually calling the main function after all these edits. sometimes that could get missed.
theoriginalbit #11
Posted 14 January 2013 - 12:01 AM
Conditionals are definitely a very important part of programming that everyone needs to know… that being said here goes…

Explaination of if's
SpoilerIf statements
An if statement is a way of performing particular instructions given a certain condition… these conditions can be a single boolean (true/false) variable or a set of statements that produce a boolean… basic Lua syntax of an if statement is:

if <condition> then
  -- instructions to perform when condition is true
end
[code]

An if statement's conditional can then be furthered using boolean logic ( not, and, or ) using not, and, or all can effect the condition.
code example
[code]
if ( active and running ) or not waiting then
end
where active, running and waiting are all boolean variables

A not effects in this way

not true = false
not false = true

An and effects in this way

true and true = true
true and false = false
false and true = false
false and false = false

An or effects in this way

true or true = true
true or false = true
false or true  = true
false or false = false

As shown in the coding example you can also use brackets to make sure that those statements are evaluated first. For examples using

if not ( running and listening ) then
would only do the actions if running AND listening are false

Ifelse statements

Ifelse statements work in the exact same way as with the if statements except they provide one advantage, if we wished to do something based on a condition, without ifelse we would do this

if <condition> then
  -- what to do when condition is true
end

if not <condition> then
  -- what to do when condition is not true
end

however with ifelse statements we can do the following


if <condition> then
  -- what to do when condition is true
else
  -- what to do when condition is false
end

now ifelse statements can also be extended when dealing with variable equality as follows

if input == "this" then

elseif input == "that" then

elseif input == "the other" then

end
we can also end the above code with an aditional else, just incase it doesn't match anything



if input == "this" then

elseif input == "that" then

elseif input == "the other" then

else

end

Off the top of my head I think I have covered everything, I hope I'm not forgetting anything and that it all makes sense, it made sense in my head ;)/>

EDIT: Oops just remembered something… editing


Pro tip:
SpoilerLets say that we want to assign a variable x to 10 if a boolean is true or 20 if it is false we can use a (what is referred to in other languages) ternary operator…
A ternary operator works in this way

<condition> and <value if true> or <value if false>
so for the example above we would do the following

x = running and 10 or 20


now lets say we want to assign to the variable x the value 20 ONLY if the variable y has no value (is nil) we can do the following

x = y or 20
ok now I think thats it ;)/>
Edited on 13 January 2013 - 11:07 PM
detsuo04 #12
Posted 14 January 2013 - 12:15 AM
frustration is hitting an all time high. i copy the program from my computer to a disk. then run it to the turtle and copy from disk to turtle No such file.
so i go back and try the copy again. file already exists. i have gone back and forth ten times with different disks trying to preform this simple operation that i have done 100 times already. BLARG coding is killer

Edit ok i got the disk to work again. not sure what happened there

code still doing nothing though. i am going to spend some time and go over it again.

Here it is as it stands incase somebody catches something i missed.. Once agfain thanks for all your help guys

rednet.open("right")
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")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
td = turtle.dig
tm = turtle.move
tc = turtle.detect
z = 0
--===========fuel=============--
function fuel()
print("1")
turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
  turtle.getFuelLevel()
    if f < z + 10 then
    rn (base , "Insuficient Coal, Returning.")
    home()
	 else
	 end
end
end
--=======return code==========--
function home()
  tcUp(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
    turtle.select(i)
    if turtle.compare() then
    return false
    else
    return true
    end
end
--============walls===========--
function walls()
  for j = 1,4 do
    if value() then
    td()
    rn (base, "ORE!")
	  else
	  turtle.turnright()
	  end
  end
end
--=============home===========--
function home()
tmUp(z)
rn (base , "Done, ready for pickup")
os.shutdown()
end

--===========Dig Code=========--
function mine()
print("1")
fuel()
print("2")
  tcDown()
   if true then
   tdDown()
   tcDown()
   print("3")
    if true then
    rn (base , "I hit bedrock, Returning")
    home()
	  else
	  tmDown()
	  print("4")
	  walls()
	  print("5")
	  z = z + 1
	  end
	  else
	   tmDown()
	   walls()
	   z = z + 1
	   end
   end
   --===========start=========--
   while true do
   mine()
   end
end  
theoriginalbit #13
Posted 14 January 2013 - 12:16 AM
are you typing in the copy correct? i.e from the disk to the root of the turtle…
crazyguymgd #14
Posted 14 January 2013 - 12:23 AM
your while true do mine() end needs to be outside of the mine() function.
theoriginalbit #15
Posted 14 January 2013 - 12:24 AM
your while true do mine() end needs to be outside of the mine() function.
As a side note here, you should never call a function recursively unless you have some way for it to return back down the function tree… or else you will get a stack overflow error as your program has run out of space…
detsuo04 #16
Posted 14 January 2013 - 12:30 AM
i moved some ends around to acomodate that now its

end
end
end
start
while true do mine()
end

though still dosnt move or return errors

not sure what you mean.
you mean i shouldnt loop the function mine() ?
theoriginalbit #17
Posted 14 January 2013 - 12:32 AM
if true then else will NEVER do the else… true can never be false…
detsuo04 #18
Posted 14 January 2013 - 12:34 AM
huh? where did i do that?
theoriginalbit #19
Posted 14 January 2013 - 12:41 AM
ok I've gone and commented your code with all the mistakes that you have made ( hope i didn't miss any )

http://pastebin.com/eCHabxgj
crazyguymgd #20
Posted 14 January 2013 - 12:45 AM
ok I've gone and commented your code with all the mistakes that you have made ( hope i didn't miss any )

http://pastebin.com/eCHabxgj

I was just getting into doing this as well…
crazyguymgd #21
Posted 14 January 2013 - 12:50 AM
one thing I also noticed when I ran this on a turtle is tmDown() doesn't actually work. This is because the command to move down is just turtle.down(). And the reason it seems to do nothing when you run it is because you have a ton of sleep(1)s in there. just wait 15 seconds and you'll start getting errors that will help you find all the problems.
theoriginalbit #22
Posted 14 January 2013 - 01:04 AM
When I get back to my computer I will write up a 'refactored' program with a bunch of fixes and improvements…
detsuo04 #23
Posted 14 January 2013 - 01:14 AM
so i fixed all those problems eccept the never initialised problem, Im not sure what it means. though i tested it and it works. anyway
at line 114 it has a nil error but the only thing there is turtle.down()
if there is a block below the turtle it gives a nil error also but at line 107 another turtle.down()

rednet.open("right")
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")
  os.reboot()
  else
	rn (base, "Coal storage is full")
	sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
z = 0
--===========fuel=============--
function fuel()
print("1")
f = turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
f =  turtle.getFuelLevel()
	if f < z + 10 then
	rn (base , "Insuficient Coal, Returning.")
	home()
	 end
end
end
--=======return code==========--
function home()
  turtle.Up(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
	 turtle.select(i)
	 if turtle.compare() then
		 return false
		 end
end
end
--============walls===========--
function walls()
  for j = 1,4 do
	if value() then
	turtle.dig()
	rn (base, "ORE!")
	  else
	  turtle.turnright()
	  end
  end
end
--=============home===========--
function home()
turtle.Up(z)
rn (base , "Done, ready for pickup")
os.shutdown()
end

--===========Dig Code=========--
function mine()
print("1")
fuel()
print("2")
  turtle.detectDown()
   if turtle.detectDown() == true then
   turtle.digDown()
   turtle.detectDown()
   print("3")
	if turtle.detectDown() == true then
	rn (base , "I hit bedrock, Returning")
	home()
	  else
	  turtle.Down()
	 print("4")
	  walls()
	  print("5")
	  z = z + 1
	  end
	  else
	   turtle.Down()
	   walls()
	   z = z + 1
	   end
   end

   --===========start=========--
   while true do
   mine()
   end
theoriginalbit #24
Posted 14 January 2013 - 01:16 AM
not initialised means that you aren't telling it what the variable base is… it thinks its nil… but you need a number to send a rednet message…
detsuo04 #25
Posted 14 January 2013 - 01:20 AM
so rednet.send <reciever name> <msg> isn't right?

the monitor displays the message though. anyway im getting nils off of turtle.down now. i was so sure it was right :(/>

one thing I also noticed when I ran this on a turtle is tmDown() doesn't actually work. This is because the command to move down is just turtle.down(). And the reason it seems to do nothing when you run it is because you have a ton of sleep(1)s in there. just wait 15 seconds and you'll start getting errors that will help you find all the problems.

the program dosn't stall thoug it just terminates and back the os screen
theoriginalbit #26
Posted 14 January 2013 - 01:28 AM
so rednet.send <reciever name> <msg> isn't right?

From the wiki http://computercraft.info/wiki/Rednet_(API)

"rednet.send(receiverID, message) — Sends a message to the computer using the opened sides. The receiver ID is the ID (number, not string) of the computer you're sending the message to. ID as nil will do the same as a broadcast."
detsuo04 #27
Posted 14 January 2013 - 01:37 AM
Oh nifty, i looked at that earlier but i miss understood it to mean computer lable. fixed now with a base = 13
detsuo04 #28
Posted 14 January 2013 - 06:38 AM
daily bump?
i have poured over the code and as far as i can tel the code is good and turtle.down() is a command it should accept with no reason to return nil but i get a nil error on it in two places. :(/> any ideas would be awsome.
theoriginalbit #29
Posted 14 January 2013 - 11:52 AM
has your code changed? and are your trying to run this ON a turtle? ( some people try to run it on a computer not realising that the computer doesn't know about the turtle api )
crazyguymgd #30
Posted 14 January 2013 - 11:57 AM
turtle movements:

turtle.up()
turtle.down()
turtle.turnRight()
turtle.turnLeft()

notice where everything is capitalized. You often try to use turtle.Down() which wont work. Neither with turtle.turnright() and so on.
crazyguymgd #31
Posted 14 January 2013 - 12:00 PM
I also saw a point where you use turtle.Up(z + 2). These don't take parameters. To get this effect, you would do:


for i = 1, z+2 do
  turtle.up()
end
theoriginalbit #32
Posted 14 January 2013 - 12:01 PM
I also saw a point where you use turtle.Up(z + 2). These don't take parameters. To get this effect, you would do:


for i = 1, z+2 do
  turtle.up()
end
Could have been trying to use my "pro tip" without actually fully implementing it :P/>
detsuo04 #33
Posted 14 January 2013 - 12:04 PM
ill check my downs.

i am running it on a turtle yes. i have been fiddling for hours now and i think i have narrowed down to one last error before i have a working program.

Im using print # to find my error point and im getting a phantom end

I have been messing with it a bit so its a bit of a jumbled mess now. but for some reason the code returns 2222222 ect forever.

my nested ifs are to blame i think but i am having trouble narrowing down the problem.

EDIT: ok my downs where capped so yay thats one problem down. thanks for the help still the phantom end though

while true do
print("2")
f = turtle.getFuelLevel()
  if f < z + 10 then
   rn (base , "Danger! fuel levels low.")
   turtle.select(16)
   turtle.refuel(all)
   f =  turtle.getFuelLevel()
   if f < z + 10 then
   rn (base , "Insuficient Coal, Returning.")
   home()
   else
	if turtle.detectDown() == true then
	turtle.digDown()
	turtle.detectDown()
	print("3")
	 if turtle.detectDown() == true then
	 rn (base , "I hit bedrock, Returning")
	 home()
	  else
		turtle.Down()
		walls()
		z = z + 1
	end
   end
  end
  end
theoriginalbit #34
Posted 14 January 2013 - 12:05 PM
this

while true do
print("2")
is the reason it prints 222222222 continuous… its an infinite loop and at the top you have print( "2" )


EDIT: More comments on how to fix it


while true do
  f = turtle.getFuelLevel()
  if f < z + 10 then
    rn (base , "Danger! fuel levels low.")
    turtle.select(16)
    turtle.refuel(all) -- refuel( all ) wouldn't work... use refuel() to 'refuel all'
    f =  turtle.getFuelLevel()
    if f < z + 10 then
      rn (base , "Insuficient Coal, Returning.")
      home()
    else
      if turtle.detectDown() == true then -- == true is not needed here
        turtle.digDown()
        turtle.detectDown() -- this doesn't do anything... not needed
        print("3") -- another spamming print
        if turtle.detectDown() == true then -- == true is not needed here
          rn (base , "I hit bedrock, Returning")
          home()
        else
          turtle.down() -- still have a capital d
          walls()
          z = z + 1
        end
      end
    end
  end
end
Edited on 14 January 2013 - 11:10 AM
detsuo04 #35
Posted 14 January 2013 - 12:08 PM
i was hoping the "while true do" would effect the rest of the code. does it only work on the next line?

if i take out the while true do it just returns 2 then ends the program. so there is some reason it is shutting off after print 2
crazyguymgd #36
Posted 14 January 2013 - 12:10 PM
Did you read TheOriginalBITs tips on ifs? Because you didn't seem to follow them…
theoriginalbit #37
Posted 14 January 2013 - 12:11 PM
Did you read TheOriginalBITs tips on ifs? Because you didn't seem to follow them…
*says in the cheering mannor* here, here… I've noticed that too… it seems like he did a selective read…
detsuo04 #38
Posted 14 January 2013 - 12:15 PM
It's not so much that i didn't read im just having trouble wrapping my brain around it. i also scrammbled alot of my if stuff around when i was blindly tinkering to fix errors

if condition true then
check condition 2 if condition true then
do this
if condition 1 not true do this
if condition 2 not true do ths
theoriginalbit #39
Posted 14 January 2013 - 12:18 PM
if condition true then
check condition 2 if condition true then
do this
if condition 1 not true do this
if condition 2 not true do ths

Not quite

if condition 1 then
  if condition 2 then
   do this
  else not condition 2 then
  end
else not condition 1 then
end

EDIT: And sorry I did miss nested ifs in the tips/explanation didnt i….
Edited on 14 January 2013 - 11:19 AM
crazyguymgd #40
Posted 14 January 2013 - 12:28 PM
I rode a turtle all the way to bedrock with this 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()
      print("your walls function")
      --walls()
      z = z + 1
  end
end 
detsuo04 #41
Posted 14 January 2013 - 12:35 PM
OH SNAP i think its working. will report back if it returns :D/>


ok so sadly it dosnt fully work.

it goes down to bed rock mining the block below it each stem. it also looks at the walls around it each step but it ignores all and just goes to bedrock.
when it hits bedrock it fails to return. still looking over the code
detsuo04 #42
Posted 14 January 2013 - 01:20 PM
ok now i have fiddle and cleaned the best i can. now its telling me that i need to end the value() function but it sais that even if i add 20 ends to it.
also sais something is wrong with my else statment in value() eof expected?
And it sais do expected on lines where there already is a do statment


rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
base = 13
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")
  os.reboot()
  else
	rn (base, "Coal storage is full")
	--sleep(2)
	  end
--sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
z = 0
--=======return code==========--
function home()
for n = 1, z +2 do
  turtle.up()
  end
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false then
  return false
end
   else
	for i = 1 , 3
	if turtle.compareTo(i) == true then
	return false
	end
	 else
	  return true
	  end
end
--============walls===========--
function walls()
  j = 1
  for j = 1,4 do
	if value() == true then do
	turtle.dig()
	rn (base, "ORE!")
	end
	  else
	  turtle.turnRight()
	  j = j + 1
	  end
end
--=============home===========--
function home()
for i = 1 , z do
turtle.up()
rn (base , "Done, ready for pickup")
os.shutdown()
end
end
--===========Dig Code=========--
while true do
print("2")
f = turtle.getFuelLevel()
  if f < z + 10 then
  do
   rn (base , "Danger! fuel levels low.")
   turtle.select(16)
   turtle.refuel(all)
   f =  turtle.getFuelLevel()
   if f < z + 10 then
   do
   rn (base , "Insuficient Coal, Returning.")
   home()
   end
   end
   end
  
   else
	if turtle.detectDown() == true then
	do
	turtle.digDown()
	turtle.detectDown()
	print("3")
	 if turtle.detectDown() == true then
	 do
	 rn (base , "I hit bedrock, Returning")
	 home()
	 end
	 end
	 end
	  else
	  do
		turtle.down()
		rn (base , "Nothing on level "..z.."next")
		walls()
		z = z + 1
	end
   end
  end
end

crazyguymgd #43
Posted 14 January 2013 - 01:24 PM
Here's your value function with proper indentation. As you can see, you're missing key ends (and you have unnecessary ends)


function value()
  if turtle.detect() == false then
	return false
  end
  else
	for i = 1 , 3
	  if turtle.compareTo(i) == true then
		return false
	  end
	  else
		return true
	  end
	end
And here's the correct way to use the if's, else's and ends


function value()
  if turtle.detect() == false then
	return false
  else
	for i = 1 , 3
	  if turtle.compareTo(i) == true then
		return false
	  else
		return true
	  end
	end
  end
end
theoriginalbit #44
Posted 14 January 2013 - 01:29 PM
Again with all the '== true' and '== false' … while its not a 'bug' or an 'issue' with the code, it is bad coding practise to do this … a function that returns a boolean or a boolean variable do not need an equality check in an if statement… if you want to check if a boolean variable/return value is true then just type the variable/return value … if you want to check if its false then add a not before the variable/return value … saying turtle.detect() == true is like making your while loop this, while true == true do … its pointless, true will always equal true, just like not false will always equal true, and false will always equal false, and not true will always equal false … does this make sense?
detsuo04 #45
Posted 14 January 2013 - 01:32 PM
OMG you are my hero it kept asking me for ends so i think at one point in a blind rage i just "Here take all the ends you can handle mua hahahaha !!! take them untill you explode!!!"

yes this section of code resolved.

ill continue pooring over the
if argument then do error need more dos thing
Again with all the '== true' and '== false' …
Oh ok i see what that means now. using if argument then basicly means if argument is true then i understand


EDIT: ok so it still wants there to be a do after each then. if i put a do there it sais i need more ends. when i add the ends it wants more do's


function value()
  if not turtle.detect() then
		return false
		
  else
		for i = 1 , 3
		  if turtle.compareTo(i) then
				return false
			  
		  else
				return true
		  end
		end
  end
end
crazyguymgd #46
Posted 14 January 2013 - 01:54 PM
sorry, I missed that do,

for i = 1, 3 do
  if turtle.compare..........
end
detsuo04 #47
Posted 14 January 2013 - 01:55 PM
i should have noticed that. thank you
crazyguymgd #48
Posted 14 January 2013 - 02:01 PM
yup yup
theoriginalbit #49
Posted 14 January 2013 - 02:11 PM
Again with all the '== true' and '== false' …
Oh ok i see what that means now. using if argument then basicly means if argument is true then i understand
Yes thats it, but thats ONLY if the argument either is a boolean variable or is a function returning a boolean or a combo of both ( remember and/or in the tips )
if you have say some input variable and you want to check the user types hello you have to do this

someVariable = read()

if someVariable == "hello" then
this is because someVariable is not a boolean but is a string, so we need to test equality

Pro tip:
SpoilerIn Lua nil is also considered false
running the following code would print the number 7, this is because we are saying while available[index] ~= nil do ( ~= is the opposite of equality == )

local avaible = { "A", "Set", "Of", "Data", "In A", "Table" }
local index = 1

while avaible[index] do
  index = index + 1
end

print( index )
crazyguymgd #50
Posted 14 January 2013 - 02:13 PM
I keep wanting to like your posts…
dam facebook
theoriginalbit #51
Posted 14 January 2013 - 02:15 PM
I keep wanting to like your posts…
dam facebook
haha yeh, I wish there was a like, and a rep +1 instead of just rep +1… coz sometimes I like what someone says and agree with it, but its not worth a rep up….
detsuo04 #52
Posted 14 January 2013 - 02:56 PM
I was going backwards there for a bit and hit my loop 222222 problem but i fixed that. now im cycling properly however
im having a problem where the value function only compares slot 1 despite having for i = 1 , 3 do

function value()
  if not turtle.detect() then
  return false
	 else
	    for i = 1 , 3 do
	    print("comparing "..i.." now")
	    turtle.select(i)
		  if turtle.compare() then		 
			    return false
				  else
				  return true
				
				  end
		  end
	    end
end
crazyguymgd #53
Posted 14 January 2013 - 03:01 PM
when you return, that means stop the function right there. So here's what I would do:


function value()
  if not turtle.detect() then
    return false
  else
    for i = 1 , 3 do
    print("comparing "..i.." now")
    turtle.select(i)
      if not turtle.compare() then
        return true
      end
    end
    return false
  end
end

As you can see, it will return true if it finds something at i, and returns false only if the for loop exits, and it hasn't returned true yet.
However, this might cause a problem if there is something at 1 and 2 because it'll return true at 1, then skip 2 because it returned.
detsuo04 #54
Posted 14 January 2013 - 03:06 PM
Awsome. i had just noticed in my program that i was returning false to to the walls function which would accept false as the argument. it wouldnt wate for value() to check slot 2 or 3

Edit:

Ok i just danced a jig when lil hal grabed the wood planking of my floore and proclaimed triumphantly "ORE!"

edit 2:

though he still refuses to return up
theoriginalbit #55
Posted 14 January 2013 - 03:07 PM
Here is what I would do:

function value()
  if not turtle.detect() then
	return false
  end

  for i = 1 , 3 do
	print("comparing "..i.." now")
	turtle.select(i)
	  if turtle.compare() then -- I'm assuming this was meant to be when it finds something
		return true
	  end
	end
  end

  return false
end
Its not any different logically, but it has a slightly better flow, then also if you decide to add anything else in later you wont forget to put return false in at the end of the function…

also just a question, shouldn't the commented line, be how I have it typed there
Edited on 14 January 2013 - 02:14 PM
crazyguymgd #56
Posted 14 January 2013 - 03:11 PM
Here is what I would do:

function value()
  if not turtle.detect() then
	return false
  end

  for i = 1 , 3 do
	print("comparing "..i.." now")
	turtle.select(i)
	  if turtle.compare() then -- I'm assuming this was meant to be when it finds something
		return true
	  end
	end
  end

  return false
end
Its not any different logically, but it has a slightly better flow, then also if you decide to add anything else in later you wont forget to put return false in at the end of the function…

also just a question, shouldn't

How is it not different logically? Originally, if the compare with the block at slot 1 was false, he would return false and never check slot 2 or 3. This checks slots 2 and 3 if 1 fails. Which is differentt
theoriginalbit #57
Posted 14 January 2013 - 03:13 PM
How is it not different logically? Originally, if the compare with the block at slot 1 was false, he would return false and never check slot 2 or 3. This checks slots 2 and 3 if 1 fails. Which is differentt

Oops that change to the code was a last minute edit, forgot that I said that ;)/> :P/>
crazyguymgd #58
Posted 14 January 2013 - 03:18 PM
Ahh and yes it should be if turtle.compare() then…. without the not. thanks for the catch
detsuo04 #59
Posted 14 January 2013 - 03:32 PM
well in slots 1 2 and 3 i have dirt gravel and stone respectivly. so it checks that its not thoes before it mines it.

i just tested my program and it worked 100%
he mines down checking all his sides as he goes looking for not stone dirt or gravel, when he finds it he mines that.
later i plan on adding a location ticker setup like i did with the z axis so he can venure away from the mine shaft to get an entire veign of ore.
here is the finished code.

rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
base = 13
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")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    --sleep(2)
	  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")
  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.." ")
	  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 , "Nothing on "..z.." moving down")
	  print("your walls function")
	  walls()
	  z = z + 1
  end
end

crazyguymgd #60
Posted 14 January 2013 - 03:35 PM
congrats!
theoriginalbit #61
Posted 14 January 2013 - 03:35 PM
ahhh ok… i suggest inverting the returns in value() then… have it return true, indicating it can mine it, and false indicating it can't… then when using value() do
if value() then dig() else somethingElse() end
detsuo04 #62
Posted 14 January 2013 - 03:37 PM
wait i take it back. it almost works. for some reasone it skips an ore occationaly
theoriginalbit #63
Posted 14 January 2013 - 03:39 PM
same type of ore or random?
detsuo04 #64
Posted 14 January 2013 - 03:42 PM
it would have to be random. since the program only identifies slot 1 2 and 3 of its inv which has dirt stone and gravel.
im thinking its skiping a compare one one or more of its turns.

edit
it skips ores on its back and left side reletive to starting orientation.
theoriginalbit #65
Posted 14 January 2013 - 03:46 PM
hmmm not too sure… I'll have a look in a minute…

also what with your random


do

end

inside your while loop, you don't need to localise that code… you can remove that do end statement…
crazyguymgd #66
Posted 14 January 2013 - 03:56 PM
In your walls function:



function walls()
  for j = 1,4 do
    print("value function")
    if value() then
      turtle.dig()
      rn (base, "ore collected at "..z.." ")
    else
      turtle.turnRight()
    end
  end
end
Lets say there is an ore when j = 1, the value function will return true, you'll dig the ore, sit still, then j increments to 2. Then value will return false, you'll turn right, and j increments to 3. say there is another ore here (we're only on the second wall at this point). value will return true, you'll dig, and then j will increment to 4. Value returns false, you turn right, but now the for loop is done, walls() returns and you never actually check the back or left wall.
Thats a scenario I imagined that could cause this problem. There are probably plenty more.
To solve this, turn right when value() returns true and when it returns false. So you will at least always check every wall.
detsuo04 #67
Posted 14 January 2013 - 03:57 PM
hmmm not too sure… I'll have a look in a minute…

also what with your random


do

end

inside your while loop, you don't need to localise that code… you can remove that do end statement…
you meen here?

edit: i added a turn right command after it collects ore so that should fix that. thanks

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
theoriginalbit #68
Posted 14 January 2013 - 04:02 PM
you meen here?

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
the do is required for the while… but when I looked at your completed code post before a wild 'do end' has appeared….
crazyguymgd #69
Posted 14 January 2013 - 04:08 PM
edit: i added a turn right command after it collects ore so that should fix that. thanks

ok let me know how that goes
crazyguymgd #70
Posted 14 January 2013 - 04:09 PM
the do is required for the while… but when I looked at your completed code post before a wild 'do end' has appeared….

I'm having a hard time not photoshopping an image of this pokemon battle…
detsuo04 #71
Posted 14 January 2013 - 04:24 PM
lol missingno attacks

it worked all opperations are good. still have to check the out of fuel operation though.
detsuo04 #72
Posted 15 January 2013 - 01:25 AM
The results of my hard labor

Lil Cal and Lil Hal both running my program in tandom.

Thanks for all the help guys!!!
theoriginalbit #73
Posted 15 January 2013 - 01:47 AM
The results of my hard labor

Lil Cal and Lil Hal both running my program in tandom.

Thanks for all the help guys!!!

VERY cool :)/> No problems…

An extra coding challenge if you dare:
SpoilerModify your code to get it so you have one computer monitor that displays the following info for BOTH turtles; the fuel level, their current y position, what they are doing (searching for ores, mining ores, etc), and maybe a short message from them… all within a tabled gui or the such… :)/> just a suggestion, but hey gotta keep up the challenge some how ;)/> :P/>
crazyguymgd #74
Posted 15 January 2013 - 01:55 AM
Nice job :)/>
remiX #75
Posted 15 January 2013 - 03:09 AM
An extra coding challenge if you dare:
SpoilerModify your code to get it so you have one computer monitor that displays the following info for BOTH turtles; the fuel level, their current y position, what they are doing (searching for ores, mining ores, etc), and maybe a short message from them… all within a tabled gui or the such… :)/>/> just a suggestion, but hey gotta keep up the challenge some how ;)/>/> :P/>/>

Yeah, he could have the max sized monitor and split it into two which Lil Cal on top and Lil Hal on the bottom, or left and right.
detsuo04 #76
Posted 15 January 2013 - 08:32 AM
thats some cool stuff. i think i will however first i need to revuild my base deeper into the earth. thousands of spiders keep screaming at me through the roof and zombies keep knocking like sales guys….

im wrighting a program now to tunnel out a room with player imputed variabls for x y and z

edit:

and the code is done. there is a moment where lil hal mines x to the end and comes back then he goes up z and mines x again.. then oddly he skips his up z step and runs the middle x again. after that he comtinues up and finishes the program.

he re runs the x on z 2 for every y. weired. anyway here is the code.

fixed :P/>


rednet.open("right")
print("Room clearing program")
sleep(1)
print("What is my base id: ")
a = tonumber(read())
rn = rednet.send
rn (a , "connection established")
print ("what is the rooms length (forward)?")
x = tonumber(read())
print ("ok. Now the width (right)?")
y = tonumber(read())
print ("ok. And finaly the hight (up)?")
z = tonumber(read())
rn (a , "the room will be "..x.." long,")
rn (a , " "..y.." wide,")
rn (a , "and "..z.." tall.")
--======paramiters of room are set=======--
print ("Is the size right? ")
b = read()
  if b ~= "yes" then
  os.shutdown()
  end
	f = turtle.getFuelLevel()
	rn (a , "fuel is "..f.." units.")
	need = (x*y*z) + x + y + z
	rn (a , "assignment needs "..need.." fuel")
	if f < need then
	  rn (a , "incificient fuel for assignment")
	  os.shutdown()
	end
  x2 = 1
  y2 = 0
  z2 = 1
  done = 0
  l = 0
while l == 0 do
  while y2 ~= y do
	while z2 ~= z do
	  while x2 ~= x do
		turtle.dig()
		turtle.forward()
		x2 = x2 +1
	  end
		while x2 ~= 1 do
		  turtle.back()
		  x2 = x2 - 1
		end
	  if z2 ~= z then
		turtle.digUp()
		turtle.up()
		z2 = z2 + 1
	  end
	  while x2 ~= x and z2 == z do
		turtle.dig()
		turtle.forward()
		x2 = x2 + 1
	  end
	  while x2 ~= 1 and z2 == z do
		turtle.back()
		x2 = x2 - 1
	  end
	end

	while z2 ~= 1 do
	  turtle.down()
	  z2 = z2 - 1
	end
  
	if y2 ~= y and l == 0 then
	  turtle.turnRight()
	  turtle.dig()
	  turtle.forward()
	  turtle.turnLeft()
	  y2 = y2 + 1
	end
  end
  l = 1
end