Jump to content




Mapping height


6 replies to this topic

#1 Nivm200

  • Members
  • 5 posts

Posted 28 May 2016 - 05:40 PM

Hello,
Im attempting my first proper computercraft program, I want to map the height of X blocks (3 for now, it will be a set area later) with respect to where the turtle started at. so at every moment the turtle should know its Y position relative to its starting point. ie whenever the turtle goes down, the currect location will be -1, if it goes up, then 1.
it will work by making the turtle go up to a certain Y point and then dropping it until it reaches the floor, recording the value of Y, and going up again and moving forward, then repeat.

after that, the y position will be stored in a matrix (its 3x3 for now because im planning to make it scan a 3x3 area soon.) and be printed at the end of the program with colours.
I can retrieve and print the colours of the matrix fine, but for the love of god the turtle just breaking my head. it keep going up one block every time it goes forward, and I cant figure out why. any help is appriciated.

pastebin : http://pastebin.com/pQUrw0Nq

things to note :
maxY is the maximum y distance the turtle will go away from the origin y point. (not ready yet )
cy is the current y potision of the turtle.
Im really not sure whats going on there, and Im brekaing my head over this for over 3 hours now, whats basically is happening is that if the turtle is placed above a block, it will detect it, then go up an extra block from its origin, and go down to detect the next block, and then repeat that, which will give me a false +1 y block value every time...
*Im not sure if its fit to be in ask a pro, but hey, why not give it a shot?

code:
ypos = {}
maxY = 3
cy = 0

function doStuff(xx)
  for ymax = 1, maxY do
    if turtle.detectDown() then
	  ypos[xx] = {cy, cy, cy}
	  for cy = cy, 0 do
	    turtle.up()
	    cy = cy +1
	    if cy == 0 then
		  turtle.forward()
		  cy = cy -1
	    end
	  end
    else
	  turtle.down()
	  cy = cy -1
    end
  end
end

function getColour(int)
  if int <= -5 then return colors.blue end
  if int == -4 then return colors.purple end
  if int == -3 then return colors.magenta end
  if int == -2 then return colors.cyan end
  if int == -1 then return colors.lightBlue end
  if int == 0  then return colors.pink end
  if int == 1  then return colors.yellow end
  if int == 2  then return colors.lime end
  if int == 3  then return colors.green end
  if int == 4  then return colors.brown end
  if int >= 5  then return colors.red end
  return colors.green
end

term.clear()
term.setCursorPos(0,0)
for xx = 1, 3 do
  doStuff(xx)
end
for xx = 1, 3 do
  for zz = 1, 3 do
    term.setBackgroundColor(getColour(ypos[xx][zz]))
    term.setCursorPos(xx,zz)
    print (" ")
  end
end


#2 The_Cat

  • Members
  • 119 posts

Posted 29 May 2016 - 01:10 AM

It might be because when you are calling the "doStruff()" function within it you have a for loop which contains both the turtle.up() and the turtle.forward()? Is this right?

or maybe try putting the turtle.up and turtle.forward both in a if statement:
for example:
if cy ~= 0 then
    turtle.up()
    cy=cy+1
else
    turtle.forward()
    cy=cy-1
end
I'm not 100% on what this doStuff is actually doing. Hope this somewhat helps.

Another note:

So are you just trying to keep track of the y position? if so you could try doing something like this:
Make a function for up and down within which you change the y value.
for example:
function moveUp() --# To move up, adds one to the cy, you will want to do the same for down
while not turtle.up() do --# Not full proof to bedrock
  if turtle.detectUp() then
   turtle.digUp()
  else
   turtle.attackUp()
  end
end
cy = cy + 1
end
with a function like so you could just call it and it will add one or minus one.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 29 May 2016 - 01:17 AM

          for cy = cy, 0 do
            turtle.up()
            cy = cy +1
            if cy == 0 then
                  turtle.forward()
                  cy = cy -1
            end
          end

The above bit of code has a scope issue in it.

When you define the counter variables for a "for" loop, they're localised to the loop. Not only that, but on each repetition, the counter value will be set to what the loop thinks it should be set to, regardless of any changes you make within the loop. For eg:

local x = 3

for x = 1, 10 do
  print(x)
  x = x + 20
end

print(x)

The "x" counter for the loop is localised to it automatically, overriding the "x" declared before the loop. Every time the loop repeats that second local "x" is set to whatever the loop thinks it should be set to, so the +20 bit is also effectively doing nothing. The code prints one to ten, and then since the loop is over and the second "x" variable no longer exists, the final line prints the value of the first "x": three.

#4 Nivm200

  • Members
  • 5 posts

Posted 29 May 2016 - 11:24 AM

I have everything ready, but I cant figure out how to store the y values in a matrix.
heres the code:
cy = 0
ax = 2
az = 2
ymax = 40

ylocs = {}
----- functions
function getColour(int)
  if int <= -10 then return colors.blue end
  if int == -9 then return colors.purple end
  if int == -8 then return colors.magenta end
  if int == -7 then return colors.cyan end
  if int == -6 then return colors.lightBlue end
  if int == -5  then return colors.pink end
  if int == -4  then return colors.yellow end
  if int == -3  then return colors.lime end
  if int == -2  then return colors.green end
  if int == -1  then return colors.brown end
  if int == 0  then return colors.red end
  return colors.green
end

function moveUp()
  if cy ~= 0 then
    turtle.up()
    cy = cy + 1
  end
end

function moveDown()
  if not turtle.detectDown() then
    turtle.down()
    cy = cy - 1
  end
end

function doStuff(xx, zz)
  for yd = 1, ymax do
    if turtle.detectDown() then
	  if ylocs[xx] == nil then
	    ylocs [xx] = {}
	  end
	  table.insert(ylocs[xx],xx,zz)
	 -- ylocs[xx] = {[zz] = cy}
    else
	  moveDown()
    end
  end
  for yu = cy, 0 do
    moveUp()
  end
end
----- code
term.clear()
term.setCursorPos(1,1)
for zz = 1, az do
  for xx = 1, ax do
    turtle.forward()
    doStuff(xx,zz)
   -- turtle.forward()
  end
  turtle.turnRight()
  turtle.turnRight()
  for xx = 1, ax do
    turtle.forward()
  end
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end

print(ylocs)
for x2 = 1, ax do
  for z2 = 1, az do
    --print("works")
    --print(x2)
    --print("-")
    --print(z2)
    --print("---")
    --print(ylocs[1][1])
    --print(ylocs[1][2])
    term.setBackgroundColor(getColour(ylocs[x2][z2]))
    term.setCursorPos(z2, x2)
    term.print(" ")
  end
end
seem like theres a problem with the storing of the values in doStuff(), but I cant figure out how to do it... I can read the matrix, but not put stuff into it.

Edited by Nivm200, 29 May 2016 - 02:04 PM.


#5 Nivm200

  • Members
  • 5 posts

Posted 29 May 2016 - 02:02 PM

Sorry for double posting within 24 hours but I cannot edit the post for some reason.
I have everyting ready, the turtle goes to an area set by the code, scan all it, however I cant figure how to store the scanning correctly, funnily enough I can read scans though (checked via preset matrix.)
heres the code: http://pastebin.com/em8pE1ri
or
cy = 0
ax = 2
az = 2
ymax = 40

ylocs = {}
----- functions
function getColour(int)
  if int <= -10 then return colors.blue end
  if int == -9 then return colors.purple end
  if int == -8 then return colors.magenta end
  if int == -7 then return colors.cyan end
  if int == -6 then return colors.lightBlue end
  if int == -5  then return colors.pink end
  if int == -4  then return colors.yellow end
  if int == -3  then return colors.lime end
  if int == -2  then return colors.green end
  if int == -1  then return colors.brown end
  if int == 0  then return colors.red end
  return colors.green
end

function moveUp()
  if cy ~= 0 then
    turtle.up()
    cy = cy + 1
  end
end

function moveDown()
  if not turtle.detectDown() then
    turtle.down()
    cy = cy - 1
  end
end

function doStuff(xx, zz)
  for yd = 1, ymax do
    if turtle.detectDown() then
	  if ylocs[xx] == nil then
	    ylocs [xx] = {}
	  end
	  table.insert(ylocs[xx],xx,zz)
	 -- ylocs[xx] = {[zz] = cy}
    else
	  moveDown()
    end
  end
  for yu = cy, 0 do
    moveUp()
  end
end
----- code
term.clear()
term.setCursorPos(1,1)
for zz = 1, az do
  for xx = 1, ax do
    turtle.forward()
    doStuff(xx,zz)
   -- turtle.forward()
  end
  turtle.turnRight()
  turtle.turnRight()
  for xx = 1, ax do
    turtle.forward()
  end
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end

print(ylocs)
for x2 = 1, ax do
  for z2 = 1, az do
    --print("works")
    --print(x2)
    --print("-")
    --print(z2)
    --print("---")
    --print(ylocs[1][1])
    --print(ylocs[1][2])
    term.setBackgroundColor(getColour(ylocs[x2][z2]))
    term.setCursorPos(z2, x2)
    term.print(" ")
  end
end

the problem is that I cant store it in the method doStuff(), any help is appreciated.

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 29 May 2016 - 02:36 PM

Instead of:

table.insert(ylocs[xx],xx,zz)

... you meant:

table.insert(ylocs[xx],zz,cy)

... though I'd just do:

ylocs[xx][zz] = cy

Another thing, you're not performing any checks to confirm whether or not your movements are successful; if the turtle collides with a mob or something you'll run into problems.

As an example, a common sort of check might go like this:

local function moveDown()
  if not turtle.detectDown() then
    while not turtle.down() do turtle.attackDown() end
    cy = cy - 1
  end
end


#7 Nivm200

  • Members
  • 5 posts

Posted 29 May 2016 - 04:28 PM

Once again thanks a lot bomb bloke! Ive finished the program successfuly!
Im aware of the fact that it doesnt check for blocks or mobs blocking its way, but I intend it to be used at certain Y level (1-2 blocks above the highest level of the scanning area) and just scan the area below, if someone will miss use it, its their problem. I might however fool proof it later if Il be bored.
I will upload the code to the turtle programs section soon :) thanks a lot! here are some pics!

Area scanned approx.

Posted Image

Result of a 10x20 scan
Posted Image

The colours are pretty messed up but still work in progress :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users