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

[Turtle] Auto refuel via Enderchest.

Started by JoePwnz, 06 April 2014 - 08:14 PM
JoePwnz #1
Posted 06 April 2014 - 10:14 PM
Dear Community,
im quite new to the Lua coding, and never did coding before thats why im asking you guys..
i just made a Farming turtle and got it running with a refuel command n stuff.. just a basic code
for refuelling, with if fuel is less then 10 = refuel and i want to automate it with enderchest..
what i want to do is something like

when the turtle checks for fuel there should be a situation where the turtle runs out of the selected slot
and detects it.. Then it should place a enderchest above take a bit coal out, pick up the chest and continue with Farming

Sorry for my bad english tho im from Germany :)/>

Greets from JoePwnz!

I would love to hear a answer!
CometWolf #2
Posted 06 April 2014 - 10:36 PM
This is pretty simple, just tell the turtle to select the enderchest slot with turtle.select(), then place it using turtle.place(). Once the chest is in place use turtle.suck() to suck items out of it, then use turtle.refuel to use those items for refueling. Idk how good you need this functionality wise, but you might want to check the block you wanna place it in using turtle.detect() beforehand to confirm that the turtle can actually place it and such.

Here's a function from my own api for just such a thing. Keep in mind that it uses openP to acess specific slots of the enderchest though. You can't do that with vanilla CC.
Spoiler

function enderRestock(enderSlot,tTurtleSlots,tEnderSlots)
  --puts down an ender chest from the slot (enderSlot), then attempts to restock the
  --turtle slots in the table tTurtleSlots with the chest slots in tEnderSlots
  turtle.select(enderSlot)
  local _dir,chestDir = findSpace(true)
  --locate open space for ender chest
  if chestDir then
	local pushDir = tNumHeading[dirStandardize("-"..chestDir)]
	local tTurtleSlotsCopy = {}
	--create local copy of turtle slots table
	for i=1,#tTurtleSlots do
	  tTurtleSlotsCopy[i] = tTurtleSlots[i]
	end
	local tEnderSlotsCopy = {}
	--create local copy of ender chest slots table
	for i=1,#tEnderSlots do
	  tEnderSlotsCopy[i] = tEnderSlots[i]
	end
	local selected
	local amount = 0 -- amout of items pushed from chest
	if peripheral.getType(chestDir) == "ender_chest" then -- confirms ender chest peripheral
	  local chest = peripheral.wrap(chestDir)
	  local chestFunc = chest.pushItemIntoSlot or chest.pushIntoSlot
	  for iTurtle=1,#tTurtleSlotsCopy do
		local turtleCount = turtle.getItemCount(tTurtleSlotsCopy[iTurtle])
		if turtleCount < 64 then -- only restocks the slot if there is space
		  local iEnder = 1
		  while iEnder <= #tEnderSlotsCopy do --dynamic for loop
			local stack = peripheral.call(chestDir,"getStackInSlot",tEnderSlotsCopy[iEnder]) or {["qty"] = 0}
			local removed = false
			if stack.qty > 0 then -- items in ender chest slot
			  local pushed = chestFunc(pushDir,tEnderSlotsCopy[iEnder],64,tTurtleSlotsCopy[iTurtle])
			  turtleCount = turtleCount+pushed
			  amount = amount+pushed
			  if pushed == stack.qty then
				remove = true
			  end
			  if turtleCount >= 64 then --ends loop if turtle slot is full
				break
			  end
			else -- no items in ender chest slot, slot is removed
			  remove = true
			end
			if remove then
			  table.remove(tEnderSlotsCopy,iEnder)
			else
			  iEnder = iEnder+1
			end
		  end
		end
	  end
	end
	dig(chestDir,false,true) -- retrieves the ender chest
	if amount > 0 then
	  return amount
	else
	  return false --no items pushed
	end
  end
  return false --chest placement failed
end
This is simply provided as an example though, and won't work without my api.
Edited on 06 April 2014 - 08:36 PM
JoePwnz #3
Posted 06 April 2014 - 11:30 PM
Thanks for the answer!
Well i just want to add a bit more of coding because i want to add a else function..
i got at the moment this little function (was just trying to add a else function but i dont know how) :

(here is a function)
if turtle.getFuelLevel() < 10 then
turtle.select(1)
turtle.refuel(1)
else
turtle.select(15)
turtle.placeUp()
turtle.suck(5)
turtle.digUp()
end
end
but im just to stupid to make one..
CometWolf #4
Posted 06 April 2014 - 11:41 PM
else what? seems to me like you've got it all in place, except you might want to attempt to refuel again after getting an item from the chest. Also im guessing you use slot 1 for fuel, so have the turtle select that slot prior to using suck, then reselect the enderChest slot before picking it up again.
JoePwnz #5
Posted 06 April 2014 - 11:55 PM
well the problem is that the turtle is just placing the chest and then it starts walking and doing its thing, but not picking it up again..
i thought when i add a "else" its gonna check if the code above works and if it doesnt, its gonna use the code below else.. the problem is, for me as a newbie its very hard to add functions because you have to search in the internet for codes and try to learn it from yourself.. im just trying to figure out how..i dont even want to add the entire code because you guys would get a heart attack :D/>
Edited on 06 April 2014 - 10:02 PM
CometWolf #6
Posted 06 April 2014 - 11:59 PM
You're using a mining turtle i presume? They're the only ones who could break an enderchest.
JoePwnz #7
Posted 07 April 2014 - 12:08 AM
Im using a Farming Turtle, but i used a Mining turtle now.. and nothing works with that..
Alright check out those 2 codes

!!this is a newbie code be aware!!

Regular Refill without enderchest: http://pastebin.com/jkeQQmu7

(doesnt work at all, tried with mining and farming turtle)My try with enderchest refill: http://pastebin.com/GiKQi0E1
Edited on 06 April 2014 - 10:15 PM
blipman17 #8
Posted 07 April 2014 - 07:49 PM
tried with mining and farming turtle)My try with enderchest refill: http://pastebin.com/GiKQi0E1

lets get through your function for refuelling

local function checkFuel()
  if turtle.getFuelLevel() < 10 then   -- if you are low on fuel then select slot 1 and refuel. this does work if you would have valid fuel there.
	  turtle.select(1)
	  turtle.refuel(1)
  else				-- if ou are not low on fuel then select slot 15, place an enderchest up, suck any item from the front and then dig up the enderchest, placing it back in slot 15.
	  turtle.select(15)
	  turtle.placeUp()
	  turtle.suck(5)
	  turtle.digUp()
  end
end

you might want to try this.

function checkFuel() --checks if there is enough fuel and refuels when neded.

	if turtle.getFuelLevel()<10 then  --if you are low on fuel

		print("refueling")
		turtle.select(15)  --select the enderchest
		turtle.placeUp()  --place the enderchest above you
		turtle.suckUp() --suck some fuel from the above enderchest into the current selected slot. (you cannot specify any amounth or from which slot.)
		turtle.refuell(1)  --refuel with one piece of fuel
		turtle.dropUp(63)  --dump the rest of the fuel back in the enderchest. (can be left out of the code.)
		turtle.digUp()  --digs up the enderchest above you and places it in your current selected iventory.

	else  --if you are not low on fuel.

		print("not low on fuel, carrying on with business. mining lasers n stuff.")

	end
end

You would need a mining turtle for this, because an enderchest is not a farming item. So it can't be picked up with a hoe. It might be better to have a chest with fuel on a set spot where the turtle will be a lot of time, and have him refuell over there.

Also, if you didn't know. Computercraft has an own Wiki which can be verry helpfull with build in functions.
http://computercraft.info/wiki/Category:APIs
especcially this one,
http://computercraft.info/wiki/Turtle_(API)
Edited on 07 April 2014 - 05:45 PM
JoePwnz #9
Posted 09 April 2014 - 12:22 AM
Thanks! i realy needed a guy who shows me what i did wrong, and tells me how to do it better! I had the problem i only thought about adding a else function because it needs to do something else when number 1 is not true… And i totaly forgot to add a code at my topic.. thanks for the help, and keep coding!
blipman17 #10
Posted 09 April 2014 - 05:16 PM
What i always try to do when I get stuck on some not working code is taking a piece of paper, start doing stuff manually from there and figure where I went wrong. It helps if you would first make a little draft from the steps you have to take. Remember, a computer is stupid. It is just way faster than you in doing stuff you want.