Jump to content




Trying to make a turtle autocrafter, Need some advice


  • You cannot reply to this topic
4 replies to this topic

#1 sjonky

  • Members
  • 59 posts
  • LocationNorway

Posted 25 January 2014 - 08:49 AM

Hello!

I've been trying to make an autocrafting turtle craft some 16k storage cells.
And instead of making it very static, i want to make it a bit more dynamic, so that i can easily change what i want to craft.
Right now im kind of stuck on how to solve a problem i've gotten. I've gotten to the stage where i can check if i got enough items for a thing, and get a table whith the missing stuff.
The problem is searching through multiple levels of recipes, to check if i got everything for the entire craft.

Example:
I start crafting a 16k storage drive, for that i need: 3 redstone, 2 glass, 3 iron and 1 storageBlock. And since storageBlock is a recipe ive got in my code i want to switch over to check that recipe and add to the numbers. So then i need: 4 glowstone, 3 glass, 1 advanced processor, 3 storage segments. Then again storage segments is a recipe in my code, then continue like that until i get to the bottom of everything.

Here is my current code: http://pastebin.com/udkRfK5T

So if anyone would give me some advice on how to efficently solve this i would be grateful. Also if there ways to make my current code more efficient. that would also be great.

Thanks

#2 sjonky

  • Members
  • 59 posts
  • LocationNorway

Posted 28 January 2014 - 02:57 PM

No advice/suggestions? Still haven't managed to get a Working "enough resources checker" got the crafting itself working like a charm though. But Still i would like it to know that it has all the required resources before it crafts the stuff

#3 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 28 January 2014 - 04:47 PM

It took me a while to wrap my head around the issue. I realize you can do this with other mods (not using a turtle), at the same time I completely respect wanting to figure out how to get a turtle to do it for you. 'nuf said on that.

As a courtesy to others who wish to reply, I am posting your code in the following spoiler:

Original Code

What you are trying to do is create some sort of database which, when queried properly, can tell you what recipes you could make based on your current inventory levels. In addition, there are hierarchical needs as well, e.g.: I want to make x which takes 1 crafted y and 3 headstone. Hmm. Do I have what I need to make the crafted y?

In your current code, you are limited to hard coded tables. I would recommend you come up with a structure you are happy with for an individual recipe and then store a table of recipes to a file, reading the file whenever that is necessary. Then you could write routines to add/edit/delete recipes from/to the file.

You may need to create several tables. One being a table of all ingredients you currently have. Another being a table with recipes. That way you can check the ingredients as you select a recipe. For each ingredient, you should also have a flag which you set to decide if the ingredient is elemental (cannot be crafted, must be supplied) or can be crafted with recipe id[x]. That way if it finds the ingredient, it is happy, but if it does not, it further checks to see if it has to request it or if the turtle can craft it itself.

Here is my concept of the algorithm:

Quote

I want to make Recipe ID "Thingy".

local availableInventoryTable {} -- -- structure is { {itemID,quantity}, {itemID,quantity} , etc }
local missingInventoryTable {} -- structure is { {itemID,quantity}, {itemID,quantity} , etc }
function checkIngredients(recipeID) -- returns table of need or false
  • Load table of requirements
    • requirement = {item, recipeID} where recipeID= nil if elemental
  • for each requirement, check inventory
    • found
      • deduct from available inventory and move to next requirement
    • not found
      • can I craft it? get recipeID
        • need = checkIngredients(recipeID) (this is a recursive call)
        • if need = false that means I can make it and I have updated availableInventoryTable
        • if need is a table, then update missingInventoryTable
      • I can't craft it, add requirement to need -- structure is { {itemID,quantity}, {itemID,quantity} , etc }
  • return false if we have ingredients or we can craft ingredients
  • return need if we are missing ingredients.
Useage:
currentInvetoryTable structure is { {itemD,quantity}, {itemD,quantity} , etc }
availableInventoryTable {}= currentInventoryTable
local need = checkIngredients(recipeID)
if need ==false then

I have everything

else

for k,v in pairs(need) do

missingInventoryTable.k = missingInventoryTable.k + v

end

do something to notify user of missing Inventory

end

This is all highly conceptual, and I can see that you have begun implementing portions of these ideas. However, consider what I am saying and see if that helps you.

As for your code itself, you follow decent indentation style, and your choice of naming functions and variables goes a long way towards making self-documenting code.

Also, just some advice: people on this site are willing to help -- but they all volunteer. Try not to get snippy, especially when you drop code which takes time to sort through.

Edited by surferpup, 28 January 2014 - 04:58 PM.


#4 sjonky

  • Members
  • 59 posts
  • LocationNorway

Posted 29 January 2014 - 04:15 PM

Thanks for a good answer :)

Yeah i know i can do this with other mods :P But well working turtle autocrafting is alot of fun to code. and it's very cheap compared to other ways of autocrafting.

If i understand everything in your answer i think i have most of it covered already in the code.

Although you do have some different approches which i would like to try out. which maybe can help with the problem. I do feel im really close to solving it though. It's probably a pretty good idea writing it down in "none code" as you have done. i usually dont do that.

Im thinking i should break down the gotRecipe into smaller parts, so that i can use some parts of it again.

Edited by sjonky, 29 January 2014 - 04:16 PM.


#5 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 29 January 2014 - 10:34 PM

Think about that recursive call to checkIngredients -- I believe you will find it is a key element of your solution. -- Oh. and if I helped you, vote my answer up :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users