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

Rednet Crafting with Turtle. Help Plz!

Started by Marini21, 08 October 2012 - 06:00 PM
Marini21 #1
Posted 08 October 2012 - 08:00 PM
So im working in a wireless crafting system via rednet. The basics are that i send a message from my home computer to a main server in my basement, and this computer then sends it to the crafty turtle. All that part is solved out and it sends all the messages.
Please there are 3 main files, plz take a look at them. check the comments to know the specific part of the problem. :D/>/>
Thanks in advance for your help and support.

This is my crafty Turtle startup file.


function reFuel()
  if turtle.getFuelLevel()<50 then
	print("Refueling...")
	turtle.turnRight()
	turtle.turnRight()
	turtle.select(1)
	turtle.suck()
	turtle.drop(turtle.getItemCount()-2)
	turtle.refuel(2)
	turtle.turnLeft()
	turtle.turnLeft()
	turtle.select(1)
  end
end
function moveToChest(;)/>/>
  for i=1,b do
	turtle.forward()
  end
  turtle.turnRight()
end
function goBackHome(:D/>/>
  turtle.turnLeft()
  for j=1,b do
	turtle.back()
  end
  turtle.turnRight()
  turtle.drop()
  turtle.turnLeft()
end
function readName()
  rednet.open("right")
  while true do
	turtle.select(1)
	shell.run("clear")
	print("Waiting for Server Instructions")
	id,message = rednet.receive()			 -- REDNET MESSAGE from my server
	print(message)
	if message == recipe[name] then  
	  rednet.send(id,"Getting Required Items")
	  craftRecipe(recipe[name])
	  rednet.send(id,"Crafting Complete")
	else
	  rednet.send(id,"I Don't Know That Recipe")
	  sleep(2)
	end
  end
end
-- Crafting, dont worry about this, it's totally working
function craftRecipe(recipe)
  moveToChest(recipe.chest)
  turtle.select(1)
  for k,v in pairs(recipe.map) do
	turtle.select(k)
	if v then  
	  turtle.suck()
	  turtle.drop(turtle.getItemCount(k)-1)				
	end
  end
  turtle.craft()
  goBackHome(recipe.chest)
end
-- End of Basic Functions
-- My crafting system is based on tables
recipe={
  ["stick"] = { 
	count=2,
	chest=3,
	map={
	  [1]=false, [2]=false, [3]=false,
	  [5]=false, [6]=true, [7]=false,
	  [9]=false, [10]=true, [11]=false
	}
  },
  ["furnace"] = {
	count=8,
	chest=4,
	map={
	  [1]=true, [2]=true, [3]=true,
	  [5]=true, [6]=false, [7]=true,
	  [9]=true, [10]=true, [11]=true
	}
  }
  
}
-- End of Recipes List
reFuel()
readName()

This is my Main Server Startup File


rednet.open("left")
function centerText(text) -- Center text stuff, dont look at it
  local x,y = term.getSize()
  local x2,y2 = term.getCursorPos()
  term.setCursorPos(math.ceil((x/2)-(text:len()/2)),y2)
  write(text)
end
term.clear()
centerText("Master Server")
term.setCursorPos(1,3)
centerText("Waiting for Messages")
term.setCursorPos(1,5)
--Wait for rednet Messages...
while true do	-- It sends all the messages back and forth from home, server, turtle and viceversa.
  id,message=rednet.receive()
  print("From #"..id..": "..message)
  if id==9 then
	rednet.send(20,message)
  elseif id==20 then
	rednet.send(9,message)
  end
end

This is my Message program from home, just to test the recipe name…


rednet.send(22,"furnace")
id,message=rednet.receive()
print("From #"..id..": "..message)
OmegaVest #2
Posted 08 October 2012 - 08:06 PM
If I'm readin your post right (not that you ever actually tell us what the problem is, but I think I get it), what you need is the much-used but ever-hidden for loop for k, v in pairs do.

Basically, pairs allows you to grab the indices (k in each for iteration) of the table as well as their contents(v). So, you want

for k, v in pairs(recipe) do
   if k == message then
	  --Do stuff
   end
end
Marini21 #3
Posted 08 October 2012 - 08:51 PM
it worked, thank you so much, and sorry for not being clear in my explanation. first post and english is not my first language :D/>/>
Marini21 #4
Posted 08 October 2012 - 08:54 PM
EDIT: Now im getting an error in the crafting function in my turtle
it says trying to index a nil value with "recipe.chest"

function craftRecipe(recipe)
moveToChest(recipe.chest) – HERE
turtle.select(1)
for k,v in pairs(recipe.map) do
turtle.select(k)
if v then
turtle.suck()
turtle.drop(turtle.getItemCount(k)-1)
end
end
turtle.craft()
goBackHome(recipe.chest)
end
OmegaVest #5
Posted 08 October 2012 - 09:05 PM
Mmmmmmmmmmmmmmm…………..

No idea, lest the program doesn't like the x.y format for some unknown and probably unknowable reason. I don't know. If you haven't already, though, do try using the x["y"] format. If it works, then it's just CC being screwy again.
Marini21 #6
Posted 08 October 2012 - 09:07 PM
Mmmmmmmmmmmmmmm…………..

No idea, lest the program doesn't like the x.y format for some unknown and probably unknowable reason. I don't know. If you haven't already, though, do try using the x["y"] format. If it works, then it's just CC being screwy again.

Nope, same error using the x["y"] format or even with x[y]…
Marini21 #7
Posted 08 October 2012 - 11:06 PM
Can someone plz help me?
Marini21 #8
Posted 10 October 2012 - 01:16 AM
No one?