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

Need help with Platform maker

Started by Acuena, 04 June 2013 - 04:11 PM
Acuena #1
Posted 04 June 2013 - 06:11 PM
Hi all!

Been trying to make a program that makes a platform. This is the code I have tried with:


local args = {...}
orient=false
if  #args~=2 then
print("Usage: platform length width")
  return
end
length = tonumber(args[1])
width = tonumber(args[2])
for xx = 1,width do
  for x = 1, length-1 do
  turtle.placeDown()
  turtle.forward()
  end
if orient == false then
  orient = true
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
else
  orient = false
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end
end

The problem I have is it refuses to place any block, even when it has blocks in the selected slot.
And the movment I havent got to work either.
I can't get it to work.
I even tested using "turtle.placeDown()" in the LUA prompt, but I only get false as a result, even if I used turtle.refuel before.
Lyqyd #2
Posted 04 June 2013 - 07:44 PM
Split into new topic.
Bomb Bloke #3
Posted 04 June 2013 - 08:56 PM
for x = 1, length-1 do

This loop will never execute unless "length" is set to 2 or more.

I suspect the turtle's inability to place blocks is due to some other plugin on the server. Anything that uses permissions to limit what players can do in certain areas (eg PermissionsEX) may mess up turtles.

Furthermore, this:

if orient == false then
  orient = true
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
else

Should read as this:

if orient == false then
  orient = true
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
else
Acuena #4
Posted 05 June 2013 - 07:28 AM
Never thougt about permission for [ComputerCraft], now they can place blocks :)/>
Had to op [ComputerCraft].

Now, after alot of trial and error, I got the platforming code to work.
Here is the code if someone want to take a look at it:

local args = {...}
orient=false
if  #args~=2 then -- Check so it has both arguments
print("Usage: platform length width")
return
end
length = tonumber(args[1])
width = tonumber(args[2])

for xx = 1,width do
for x = 1, length do
  turtle.forward()
  turtle.placeDown() 
end
if orient == false then -- Should turn right
  orient = true
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
  
else -- Should turn left
  orient = false
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end
end

I am planing to add a few things:
  • Check that is has blocks to build with selected, if not move to next slot.
  • A check if there is something in the way, should not be that hard.
  • Ability to refuel from slot 1, if there is coal there.
Any suggestion on what more to add?
Acuena #5
Posted 05 June 2013 - 12:55 PM
I have been working on the function that checks if it is something in the way and keeps trying. But I can get it to work :(/>

This is the code I have triied, it returns: platform:14: attemt to call nil
Here is the code:


local args = {...}
orient=false
if  #args~=2 then -- Check so it has both arguments
print("Usage: platform length width")
return
end
length = tonumber(args[1])
width = tonumber(args[2])

for xx = 1,width do
for x = 1, length do
  turtle.forward()
  turtle.placeDown() 
end
if orient == false then -- Should turn right
  orient = true
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
  
else -- Should turn left
  orient = false
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end
end
function moveforward()
while not turtle.forward() do
turtle.forward()
end
end
Bomb Bloke #6
Posted 05 June 2013 - 07:51 PM
You must declare functions BEFORE you attempt to call them. Move your "moveforward()" function to the top of your code. Mind you, you're not actually attempting to call it here…

The line "while not turtle.forward() do" will itself attempt to move the turtle forward. You could just make it "while not turtle.forward() do end", or you may wish to put your turtle.detect() statement in there and act on it if true.
Acuena #7
Posted 06 June 2013 - 09:07 AM
  • Check that is has blocks to build with selected, if not move to next slot.
  • A check if there is something in the way, should not be that hard. <Done: Thanks Bomb Bloke for pointing out that functions should be declard on top of the code.
  • Ability to refuel from slot 1, if there is coal there. <Done: Thanks to Bomb Bloke for pointing out that I don't need 2 turtleforward() in the while function.

The only part I need to find out now is how to check if there is any block in the selected slot, and if not select the next slot until it reaces slot 16, and if it goes empty, stop.

A quick search turned this function up: turtle.getItemCount
That I belive should do the trick :)/>
Acuena #8
Posted 06 June 2013 - 04:06 PM
Have got all parts working now, but I have problem with the path the turtle goes.

I made a quick image wich I hope will show a bit how I want the turtle to go and how it goes now.
Spoiler

The enchantment table which is labeld "S", is where the turtle stands when it starts. The green line represents the path I want it to take and the red line the path I don't want it to take, which it does now. The tnt blocks are one block higher than the rest of the block so the turtle will bump into them if it follows the red path, and such the turtle just stops because of a check (se code). I do not have any clue on why it takes the red path, as far as I can see it should take the green one. Any help would be appriciated.

The command to make the platform above would be: platform 3 4

Here is the code I currently have:

Spoiler

-- Variables
curslot = 2 -- Holds current slot

-- Functions

function moveforward()
while not turtle.forward() do
checkfuel()
end
end

function checkfuel()
if turtle.getFuelLevel() < 1 then
   turtle.select(1)
   if not turtle.refuel(1) then
	print("No fuel in slot 1, awaiting fuel")
	while not turtle.refuel(1) do
	end
	print("Successfully refueld, continuing")
   end
  turtle.select(curslot)
end
end

function turnright()
turtle.turnRight()
moveforward()
turtle.turnRight()
end

function turnleft()
turtle.turnLeft()
moveforward()
turtle.turnLeft()
end

function checkblockcount()
print("Checking total blocks") -- Just for debugg
  if turtle.getItemCount(curslot) == 0 then
selnextslot()
end
end

function selnextslot()
curslot = curslot + 1
print("Changed curslot")
if curslot > 16 then
print("Out of blocks, terminating")
error()
end
turtle.select(curslot)
end

function place() -- Proberly should change order of this one
turtle.placeDown()
checkblockcount()
end

-- Code

turtle.select(curslot)
local args = {...}
orient=false
if  #args~=2 then -- Check so it has both arguments
print("Usage: platform length width")
return
end
length = tonumber(args[1])
width = tonumber(args[2])
for xx = 1,width do
for x = 1, length do
  moveforward()
  place()
end
if orient == false then -- Should turn right
  turnright()
  place()
else -- Should turn left
   turnleft()
end
orient = not orient
end

Again, any help would be greatly appriciated.
Bomb Bloke #9
Posted 06 June 2013 - 08:26 PM
Each time turnright() or turnleft() is called, the turtle ends up in the other lane. You're specifying a "length" of three, so after entering each lane it goes three extra spaces: hence it'll always end up travelling through a total of four spaces in each lane.

It sounds like what you want to do is make your "for x = 1, length do" loop instead read as "for x = 1, length-1 do" (to account for the extra space covered by the turning functions). You'll also need to add a single "moveforward()" call above the two for loops to account for the fact that you want it to travel one extra space in just the first lane.

Oh, and make it "place()" regardless of which direction it's turning.