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

[Lua][Questions] Little guidance completing platform lighting program

Started by SavageCore, 18 December 2012 - 02:13 PM
SavageCore #1
Posted 18 December 2012 - 03:13 PM
Hello

First thing I have attempted to code in Lua and first time playing with turtles! Awesome so far.

Anyway I am trying to write a program for the turtle which will given a set width and length place a torch every 8 blocks.

My testing has been a 20x20 area. In the first row the space between each torch varies and the 3rd 'column' the same happens.

This is the code:


local torches = turtle.getItemCount(2)
local tArgs = {...}
local width = tonumber(tArgs[1])
local length = tonumber(tArgs[2])
local turnRight = true
local curSlot = 16
function checkFuel(curSlot)
  if turtle.getFuelLevel() < 1 and turtle.getFuelLevel() ~= "unlimited" then
	turtle.select(1)
	if turtle.refuel(1) then
	  print("Refueling...")
	  sleep(1)
	  return true
	else
	  return false
	end
	turtle.select(curSlot)
  return nil
  end
  return true
end
-- forward function partially taken from SethBling's video
-- http://www.youtube.com/watch?v=YiNFZNBAF5w
function forward(curSlot)
  for i=1,10 do
	if turtle.detect() then
	  turtle.dig()
	end
	if turtle.forward() then
	  return true
	end
  end
  return nil
end
function prevBlockSlot(curSlot)
  for slot=curSlot,2,-1 do
	turtle.select(slot)
	if (turtle.compareTo(2) and turtle.getItemCount(slot) > 0) or turtle.getItemCount(slot) > 0 then
	  return slot
	end
  end
  return false
end
function placeDown(curSlot)
  if not turtle.compareTo(2) or turtle.getItemCount(curSlot) < 1 then
	--if not prevBlockSlot(curSlot) then
	  --return false
	--end
	curSlot = prevBlockSlot(curSlot)
  end
  turtle.turnRight()
  turtle.turnRight()
  turtle.place()
  turtle.turnLeft()
  turtle.turnLeft()
  return curSlot
end
if width == nil or length == nil then
  print("1. Place turtle in left corner")
  print("2. Slot 1: Coal")
  print("3. Slot 2-16: Torches")
  print("4. Type 'torch <width> <length>'")
else
  for slot=3,16 do
	turtle.select(slot)
	if turtle.compareTo(2) then
	  torches = torches + turtle.getItemCount(slot)
	end
  end
  turtle.select(curSlot)
  widthStr = tostring(width)
  lengthStr = tostring(length)
  print("Lighting up a " .. widthStr .. "x" .. lengthStr .. " platform")
  local columns = 1
  for j = 1, width, 1 do
	for i = 1, length, 1 do
	  if i < length then
		if not checkFuel(curSlot) then
		  print("Bleep. Bloop. Empty fuel cell.")
		  while not checkFuel(curSlot) do
			sleep(1)
		  end
		print("Bing. All juiced up!")
		end
		forward()
	  end
	  if (i % 9 == 0 and i > 0) or (i == 1 and columns == 1) then
		if columns % 9 == 0 or columns == 1 then
		  if not placeDown(curSlot) then
			print("Bleep. Bloop. I lost the torches! Sorry :(/>/>")
			while not placeDown(curSlot) do
			  sleep(1)
			end
		  print("Bing. I found the torches! :D/>/>")
		  turtle.select(curSlot)
		  placeDown(curSlot)
		  end
		end
	  end
	end
	if j < width then
	  if turnRight == true then
		turtle.turnRight()
		forward()
		turtle.turnRight()
		turnRight = false
		columns = columns + 1
	  else
		turtle.turnLeft()
		turtle.forward()
		turtle.turnLeft()
		turnRight = true
		columns = columns + 1
	  end
	end
  end
print("I am done master")
end

It could be horrific and I may be looking at this the wrong way but any help would be much appreciated. If needs be I can cut out the code that waits for more coal/torches to make it a little cleaner?

Thanks
ChunLing #2
Posted 18 December 2012 - 10:30 PM
Your checkFuel function didn't work like you apparently thought it did. Here's a revised version:
function checkFuel(curSlot)
    local fuelL = turtle.getFuelLevel()
    if fuelL ~= "unlimited" and fuelL < 1 then --reversed the order so that unlimited fuel doesn't cause error
	    turtle.select(1)
	    if turtle.refuel(1) then
		  print("Refueling...")
	    else
		  fuelL = nil
	    end
	    turtle.select(curSlot)
    end
    return fuelL
end
This returns nil if fuel is needed but refueling failed. Otherwise it returns the fuel level before refueling.

I'm almost sure prevBlockSlot is wrong. I'm not sure what it's supposed to do, but what what it does is this:
function prevBlockSlot(curSlot)
    for slot=curSlot,2,-1 do
        turtle.select(slot)
        if turtle.getItemCount(slot) > 0 then -- this is functionally equivalent to what you had
            return slot
        end
    end
    return false
end
That is, starting at curSlot, it checks till it finds a non-empty slot and returns that slot. Perhaps you wanted to find the highest slot with torches? I don't know.

Then you have your movement algorithm, which doesn't look very efficient. You also do a lot of things you don't really need to, like converting length and width into strings to use them in print. Concatenating a string with a number is perfectly acceptable. But if this works then I say you're doing okay.
SavageCore #3
Posted 19 December 2012 - 12:02 AM
Thanks, I'll get to testing these.

prevBlockSlot() recursively loops round each inventory slot to find the first non empty slot starting from 16 counting down.

My main issue with movement is I'd like to place a torch at the start but that either requires turtle.up, placeDown(), forward(), down() or turtle.forward() then look behind and place()? Which then causes my loop to be off by one? Meaning the first torch is off in placement.