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

Merging slot contents TransferTo doesnt work

Started by TyCamden, 20 November 2014 - 05:19 PM
TyCamden #1
Posted 20 November 2014 - 06:19 PM
When my turtle mines, it uses cobble in slot 15 to fill various holes in walls. When it is done and comes back to the top, it has (for an example) 40 cobble in slot 15 and 10 cobble in slot 1.

I want it to top off slot 15 by merging the two, so that it would have 50 cobble in slot 15 and none in slot 1.

I wrote code to do this using turtle.transferTo but it didnt work, and I realized that this command only works if the destination slot is empty.

Any ideas or commands that would help me accomplish this.

PS: Here is the section of code that didnt work, but I know why…


				cobb = turtle.getItemCount(15)
				if cobb < 64 then
					  for i = 1, 14 do
						  ctslt = turtle.getItemCount(i)
						  if ctslt ~= 0 then
							 wut = turtle.getItemDetail(i)
							 if wut.name == "cobblestone" then
								turtle.select(i)
								turtle.transferTo(15)
								end
						  end
					  end
				end
KingofGamesYami #2
Posted 20 November 2014 - 06:25 PM
ComputerCraft Wiki said:
If there are fewer than quantity items in the selected slot or only room for fewer items in the destination slot, transfers as many as possible and returns true. If none can be transferred, returns false.
TyCamden #3
Posted 20 November 2014 - 07:46 PM
Any thought why my code won't work then. I designed it to "top off" slot 15 with any cobble I dig out that hits slots 1-14.
KingofGamesYami #4
Posted 20 November 2014 - 07:51 PM
I honestly don't know. It looks like that portion works, post the full code.
TyCamden #5
Posted 20 November 2014 - 08:12 PM
full code:


-- before running program, be sure...
--    coal is in slot 16
--    cobble in slot 15
--    put an actual chest ABOVE the turtle that will be on the mission (empty) (for the loot)
--    put an actual chest BEHIND the turtle that will be on the mission (with more coal in it)
ypos = 0 -- This tracks how far down turtle is from its starting position
facing = 0 -- This tracks direction turtle is facing 0=original-front 1=left 2=right 3=back
mission = 1 -- Current status of mission.
			   -- 0 means mission completed
			   -- 1 means operational mission, still running.
			   -- 2 means that it is time to drop off stuff then return to where u left off
			   -- 3 means that it is time to refuel
while mission ~= 0 do
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 mission = 3
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		    turtle.select(16) -- choose slot 16 on turtle
		    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		    turtle.select(1) -- choose slot 1 on turtle (default?)
		    mission = 1
		 else
			 -- coal slot is empty so go back and get more fuel into slot 16
			 yrem = ypos -- remember ypos before returning for fuel
			 facrem = facing -- remember facing before returning for fuel
			 if ypos == 0 then -- you are back at start
			    -- get turtle facing the chest (backwards from original direction
			    if facing == 0 then -- facing forward
				   turtle.turnLeft() -- turn left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 1 then -- facing left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 2 then -- facing right
				   turtle.turnRight() -- turn right
				   facing = 3
			    end
			    -- see if fuel chest is empty or not and get more fuel
			    turtle.select(16) -- choose slot 16 on turtle
			    turtle.suck() -- suck fuel out of chest into slot 16 if possible
			    turtle.turnRight() -- turning turtle back towards facing the front
				   facing = 1
			    turtle.turnRight() -- finishing turning turtle to facing the front
				   facing = 0
			    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
			    turtle.select(1) -- choose slot 1 on turtle (default?)
			    FuelLeft = turtle.getFuelLevel() + 1
			    if FuelLeft < 65 then -- still less than 65 fuel after refueling attempt
				   print "Not enough fuel to continue mission."
			    else
				    -- RETURN TO POSITION PRIOR TO NEEDING FUEL IF NECESSARY
				    while yrem ~= ypos do
					   turtle.down()
					   ypos = ypos - 1
				    end
				    while facrem ~= facing do
						  turtle.turnRight()
						  if facing == 0 then -- 0=original-front 1=left 2=right 3=back
							 facing = 2
						  else
							  if facing == 1 then
								 facing = 0
							  else
								  if facing == 2 then
									 facing = 3
								  else
									  -- facing = 3
									  facing = 1
								  end
							  end
						  end
				    end
				    mission = 1
			    end
			 else
				 -- move back up to start
				 while ypos<0 and ypos~=0 do
					   turtle.up()
					   ypos = ypos + 1
				 end
			 end
		 end
	  else  -- if there is NOT less than 65 fuel in the turtle...
		    if turtle.down() then -- it was able to move down
			   ypos = ypos - 1
		    else -- it cannot move down
			    -- find out what is below it and act accordingly
			    local bSuccess, tData = turtle.inspectDown()
			    if bSuccess and tData.name == "minecraft:chest" then
				   turtle.suckDown()
				   turtle.select(15)
				   turtle.placeDown()
				   turtle.select(1)
				   mission = 2 -- drop off all new stuff after grabbing a rare chest
			    else
				    if bSuccess and tData.name == "minecraft:bedrock" then -- bottom of the world
					   mission = 0
					   while ypos ~= 0 do
							 turtle.up()
							 ypos = ypos + 1
							 turtle.select(15)
							 turtle.placeDown()
							 turtle.select(1)
					   end
				    else
					    turtle.digDown()
					    turtle.down()
					    ypos = ypos - 1
					    -- turtle.select(15) Decided not to fill hole above turtle when going down
					    -- turtle.placeUp()
					    -- turtle.select(1)
				    end
			    end
		    end
		    if mission~=2 and mission~=0 then
			   -- seal any holes around this location (main concern: lava)
			   for i = 1, 4 do
				   if turtle.detect()=="false" then
					  -- there is a hole
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
					  turtle.turnLeft()
				   end
			   end
			   -- look around for goodies
			   flag = 0
			   for i = 1, 4 do
				   local bSuccess, tData = turtle.inspect()
				   if bSuccess and tData.name == "minecraft:stone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:cobblestone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:dirt" then
					  flag = 1
				   end
				   -- if the flag is still zero, it may be valuable
				   --  and just get rid of gravel
				   if flag == 0 then
					  turtle.dig()
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
				   end
				   turtle.turnLeft()
			   end
		    else
			    -- mission = 2
			    --    2 means that it is time to drop off stuff then return to where u left off
			    -- there is a chest ABOVE the turtle start position for the loot
			    yrem = ypos -- remember ypos before returning for fuel
			    while ypos ~= 0 do
					  turtle.up()
					  ypos = ypos + 1
			    end
			    -- top off slot 15 (cobble) to 64 items - if possible from goodies collected
			    -- DOES NOT WORK BECAUSE TRANSFERTO COMMAND ONLY WORKS TO EMPTY SLOT (does not merge)
			    cobb = turtle.getItemCount(15)
			    if cobb < 64 then
					  for i = 1, 14 do
						  ctslt = turtle.getItemCount(i)
						  if ctslt ~= 0 then
							 wut = turtle.getItemDetail(i)
							 if wut.name == "cobblestone" then
							    turtle.select(i)
							    turtle.transferTo(15)
							    end
						  end
					  end
			    end
			    turtle.select(1)
			    -- dump everything in slots 1-14 in the goodies chest
			    for i = 1, 14 do
				    turtle.select(i)
				    turtle.dropUp()
			    end
			    turtle.select(1)
			    while yrem ~= ypos do
					  turtle.down()
					  ypos = ypos - 1
			    end
		    end
	  end
end
Dragon53535 #6
Posted 20 November 2014 - 10:48 PM
Your problem is simple. The name of the item is not "cobblestone". Turtle.getItemDetail() gets the parent mod it's from, so for CC it would be "computercraft:turtle"

Just change your

if wut.name == "cobblestone" then
to

if wut.name == "minecraft:cobblestone" then
Lyqyd #7
Posted 20 November 2014 - 10:59 PM
That would not cause the program to exhibit the described behavior. OP states that it does work if slot 15 is empty.
Dragon53535 #8
Posted 20 November 2014 - 11:27 PM
I just tested it. It does work if you specify correctly what the item you're looking for is.

Spoiler







Edit: Added image for destination slot being empty for each test.
Edited on 21 November 2014 - 03:17 AM
Lyqyd #9
Posted 21 November 2014 - 12:56 AM
I don't see a test with the existing code where the destination slot is empty.
valithor #10
Posted 21 November 2014 - 01:22 AM
I don't see a test with the existing code where the destination slot is empty.

Would that test even be nescesary? It is obvious that it will move items from slots 1-14 to slot 15 if slot 15 has less than 64 items (empty = 0 which is less than 64). There is honestly no reason it should not work.

That would not cause the program to exhibit the described behavior. OP states that it does work if slot 15 is empty.

He actually said that he realized that the command would only work if the slot was empty. Although this is wrong he made this assumption using a method which he did not say. He never said that it worked when the slot is empty, but does not work when the slot has items. He likely assumed that it can not move to a slot with items in it, because when he tried it in this program it did not work (while the problem was actually the if statement check for item name).


Dragon's fix, fixed the problem that the OP was experiencing.
Edited on 21 November 2014 - 12:25 AM
TyCamden #11
Posted 21 November 2014 - 01:33 PM
Thank you Dragon53535, that did it !

Sometimes you just need an extra pair of eyes on the problem :P/>
TyCamden #12
Posted 21 November 2014 - 08:38 PM
Here is the final version of the program (with a few final bug corrections):


-- before running program, be sure...
--    coal is in slot 16
--    cobble in slot 15
--    put an actual chest ABOVE the turtle that will be on the mission (empty) (for the loot)
--    put an actual chest BEHIND the turtle that will be on the mission (with more coal in it)
ypos = 0 -- This tracks how far down turtle is from its starting position
facing = 0 -- This tracks direction turtle is facing 0=original-front 1=left 2=right 3=back
mission = 1 -- Current status of mission.
			   -- 0 means mission completed
			   -- 1 means operational mission, still running.
			   -- 2 means that it is time to drop off stuff then return to where u left off
			   -- 3 means that it is time to refuel
while mission ~= 0 do
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 mission = 3
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		    turtle.select(16) -- choose slot 16 on turtle
		    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		    turtle.select(1) -- choose slot 1 on turtle (default?)
		    mission = 1
		 else
			 -- coal slot is empty so go back and get more fuel into slot 16
			 yrem = ypos -- remember ypos before returning for fuel
			 facrem = facing -- remember facing before returning for fuel
			 if ypos == 0 then -- you are back at start
			    -- get turtle facing the chest (backwards from original direction
			    if facing == 0 then -- facing forward
				   turtle.turnLeft() -- turn left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 1 then -- facing left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 2 then -- facing right
				   turtle.turnRight() -- turn right
				   facing = 3
			    end
			    -- see if fuel chest is empty or not and get more fuel
			    turtle.select(16) -- choose slot 16 on turtle
			    turtle.suck() -- suck fuel out of chest into slot 16 if possible
			    turtle.turnRight() -- turning turtle back towards facing the front
				   facing = 1
			    turtle.turnRight() -- finishing turning turtle to facing the front
				   facing = 0
			    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
			    turtle.select(1) -- choose slot 1 on turtle (default?)
			    FuelLeft = turtle.getFuelLevel() + 1
			    if FuelLeft < 65 then -- still less than 65 fuel after refueling attempt
				   print "Not enough fuel to continue mission."
			    else
				    -- RETURN TO POSITION PRIOR TO NEEDING FUEL IF NECESSARY
				    while yrem ~= ypos do
					   turtle.down()
					   ypos = ypos - 1
				    end
				    while facrem ~= facing do
						  turtle.turnRight()
						  if facing == 0 then -- 0=original-front 1=left 2=right 3=back
							 facing = 2
						  else
							  if facing == 1 then
								 facing = 0
							  else
								  if facing == 2 then
									 facing = 3
								  else
									  -- facing = 3
									  facing = 1
								  end
							  end
						  end
				    end
				    mission = 1
			    end
			 else
				 -- move back up to start
				 while ypos<0 and ypos~=0 do
					   turtle.up()
					   ypos = ypos + 1
				 end
			 end
		 end
	  else  -- if there is NOT less than 65 fuel in the turtle...
		    if turtle.down() then -- it was able to move down
			   ypos = ypos - 1
		    else -- it cannot move down
			    -- find out what is below it and act accordingly
			    local bSuccess, tData = turtle.inspectDown()
			    if bSuccess and tData.name == "minecraft:chest" then
				   turtle.suckDown()
				   turtle.select(15)
				   turtle.placeDown()
				   turtle.select(1)
				   mission = 2 -- drop off all new stuff after grabbing a rare chest
			    else
				    if bSuccess and tData.name == "minecraft:bedrock" then -- bottom of the world
					   mission = 0
					   while ypos ~= 0 do
							 turtle.up()
							 ypos = ypos + 1
							 -- fill below turtle with cobble IF there is cobble in slot 15
							 cobcnt = turtle.getItemCount(15)
							 if cobcnt <1 then
							    for i = 1, 14 do
								    sltcnt = turtle.getItemCount(i)
								    if sltcnt > 0 then
									   wut = turtle.getItemDetail(i)
									   if wut.name == "minecraft:cobblestone" then
										  turtle.select(i)
										  turtle.transferTo(15)
										  turtle.select(1)
									   end
								    end
							    end
							 end
							 turtle.select(15)
							 turtle.placeDown()
							 turtle.select(1)
					   end
				    else
					    turtle.digDown()
					    turtle.down()
					    ypos = ypos - 1
				    end
			    end
		    end
		    if mission~=2 and mission~=0 then
			   -- seal any holes around this location (main concern: lava)
			   for i = 1, 4 do
				   if turtle.detect()=="false" then
					  -- there is a hole
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
					  turtle.turnLeft()
				   end
			   end
			   -- look around for goodies
			   flag = 0
			   for i = 1, 4 do
				   local bSuccess, tData = turtle.inspect()
				   if bSuccess and tData.name == "minecraft:stone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:cobblestone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:dirt" then
					  flag = 1
				   end
				   -- if the flag is still zero, it may be valuable
				   --  and just get rid of gravel
				   if flag == 0 then
					  turtle.dig()
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
				   end
				   turtle.turnLeft()
				   flag = 0
			   end
		    else
			    -- mission = 2 (or 0)
			    --    2 means that it is time to drop off stuff then return to where u left off
			    -- there is a chest ABOVE the turtle start position for the loot
			    yrem = ypos -- remember ypos before returning for fuel
			    while ypos ~= 0 do
					  turtle.up()
					  ypos = ypos + 1
			    end
			    -- top off slot 15 (cobble) to 64 items - if possible from goodies collected
			    cobb = turtle.getItemCount(15)
			    if cobb < 64 then
					  for i = 1, 14 do
						  ctslt = turtle.getItemCount(i)
						  if ctslt ~= 0 then
							 wut = turtle.getItemDetail(i)
							 if wut.name == "minecraft:cobblestone" then
							    turtle.select(i)
							    turtle.transferTo(15)
							    end
						  end
					  end
			    end
			    turtle.select(1)
			    -- see if there is a chest above the turtle
			    local bSuccess, tData = turtle.inspectUp()
			    if bSuccess and tData.name == "minecraft:chest" then
				   -- dump everything in slots 1-14 in the goodies chest
				   for i = 1, 14 do
					   turtle.select(i)
					   turtle.dropUp()
				   end
			    end
			    turtle.select(1)
			    while yrem ~= ypos do
					  turtle.down()
					  ypos = ypos - 1
			    end
			    if mission ~= 0 then -- if mission is not complete
				   -- mission = 2 -- it just finished dropping and returning
				   mission = 1
			    end
		    end
	  end
end