[Mod] Extensible gridless craft system [simplecrafting_lib]

FaceDeer
Member
 
Posts: 267
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

[Mod] Extensible gridless craft system [simplecrafting_lib]

by FaceDeer » Sat May 13, 2017 03:19

This mod provides a library of functions that make it easy for other mods to work with a new type of crafting system that doesn't rely on the traditional "crafting grid". It essentially makes every crafting recipe "shapeless", depending solely on quantity of inputs and not requiring users to remember a specific arrangement to put them in. Any number of crafting "types" can be created, allowing mods to define specialist crafting stations with specific subsets of recipes, and arbitrary additional data can be associated with crafting recipes to allow mods to easily do other fancy things.

A set of highly controllable import functions are included that are capable of importing the grid-based crafting recipes of default Minetest, optionally removing them from the default crafting system in the process. This import system doesn't require any dependencies to be declared, it is capable of processing any recipe registered by any mod.

As this mod is just a library, it doesn't do anything when installed in a game by itself. It is intended to be a dependency of other mods that make use of its functions. That said, there are a couple of WIP mods that showcase some of its features. For example:

This fork of Qwertymine3's crafting table mod adds a crafting table to Minetest that imports all existing recipes and allows a user to craft them using the simplecrafting system. The crafting table's interface shows how this works:

Image

Raw materials are placed into the block of ten inventory slots on the left-hand side of the window. The arrangement of the raw materials is not important. The large 8*6 grid to the right of the window shows all possible products that could be made from the raw materials in the right. All the player has to do is take the possible product stack from the table and drop it into his own inventory and the raw materials required will be deducted from the raw material supply.

A more sophisticated and specialized example is the smelter in my WIP "workshops" mod:

Image

This is somewhat like a furnace, except it allows for multiple items to be placed in the "input" and "fuel" inventories. Possible outputs are shown in the grid of buttons to the lower right, and when fuel is present and an output is selected the smelter processes recipes similar to a furnace. This arrangement allows for a number of recipe types that the default furnace doesn't, however, such as realistic alloying reactions (tin + 3 copper -> 4 bronze) or smelting reactions that require flux.

The smelter's recipe import functions show off the flexibility of this system's legacy crafting recipe support. There are two import filters defined for the smelter:

  • The first imports only "cooking" recipes that produce metal ingots as outputs, and removes them from the default crafting system in the process. So a default furnace will still be able to cook bread, but will no longer be able to cook a copper lump into a copper ingot - you'll need the smelter for that.
  • The second analyzes all "normal" crafting recipes that require metal ingots as input materials and automatically creates and registers a smelter recipe that turns the output of that recipe back into metal ingots - but doesn't return any other components. So you can stick a steel pick into the smelter and melt it down into three iron ingots, but you don't get the sticks back. This filter doesn't remove anything from the default crafting system, it only creates new recipes from scratch. Any other mod that defines metal-consuming recipes will automatically have recycling recipes created in this manner with no need for specific action to support it.

Finally, simplecrafting_lib includes a basic crafting guide that can be displayed to the user with a single API call. It lists all of the possible outputs that can be made for a given crafting type in the upper grid, and when an output is selected it shows all possible combinations of inputs that can be used to craft it in the lower half. This screenshot shows a crafting guide for the crafting table:

Image

My goal with this mod is to not just use it as a foundation for my own work but to try to support other modders in making a more interesting and diverse set of crafting recipes in Minetest. An API guide can be found in "api.txt", let me know if there are any omissions or unclear features.

This mod is still a work in progress so there's already a couple of planned to-do items, including a simplecrafting_lib version of clear_craft and register_on_craft.

Links


Simplecrafting_lib is released under the MIT license.
 

User avatar
Wuzzy
Member
 
Posts: 3243
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by Wuzzy » Sat May 13, 2017 10:37

Questions:
- Does it support groups?
- Does it support replacements (example in MineClone 2: You need 3 milk buckets and other stuff to craft a cake. After you crafted the cake, the milk buckets are replaced by empty buckets)?
- Can it act as a compability layer to existing shaped recipes (e.g. can this library read in all the recipes of Minetest Game and make them accessible through this API)?
- If the previous answer is “Yes.”, is this done automatically?
My creations. I gladly take any bitcoins you have lying around: 17fsUywHxeMHKG41UFfu34F1rAxZcrVoqH
 

FaceDeer
Member
 
Posts: 267
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by FaceDeer » Sat May 13, 2017 14:59

Yes to all of the above questions, except the importation isn't quite automatic - you need to add a few lines of code to your mod to tell simplecrafting_lib how you want it to go about importing stuff.

For example, you could use this:

Code: Select all
simplecrafting_lib.register_recipe_import_filter(function(legacy_method, legacy_recipe)
   if legacy_method == "normal" then
      return "table", true
   end
end)

simplecrafting_lib.import_legacy_recipes()


to tell simplecrafting_lib to convert all "normal" recipes (the three types the default crafting system has are "normal", "cooking" and "fuel") into recipes belonging to the "table" crafting_type, and to clear each of those "normal" recipes from the default crafting system as it does so.

A more complicated example is in here, where I set up a "smelter" crafting_type using two filters to import metal-ingot-producing recipes from the furnace and also to create new recycling recipes for items that are made using metal ingots.

You don't need to worry about dependencies, simplecrafting_lib will be able to import recipes from all mods loaded both before and after simplecrafting_lib. It does this by redefining minetest.register_craft with its own code. You can register multiple import filters and you can call import_legacy_recipes() multiple times safely, so different mods should be able to use simplecrafting_lib to import default recipes into various crafting_types without stepping on each others' toes.
 

User avatar
Wuzzy
Member
 
Posts: 3243
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by Wuzzy » Sat May 13, 2017 18:20

This sounds very promising. I may take a deeper look into this when I need this.
My creations. I gladly take any bitcoins you have lying around: 17fsUywHxeMHKG41UFfu34F1rAxZcrVoqH
 

FaceDeer
Member
 
Posts: 267
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by FaceDeer » Sat May 13, 2017 18:56

Thanks. My goal with this mod is to try to account for all the weird bugs, edge cases, undocumented or misdocumented features, and quirks of the default crafting system and present a clean pure-LUA interface for other mods to make use of. So definitely let me know if you encounter any problems no matter how obscure.

Currently the only remaining bug I'm aware of is that if a mod registers two craft recipes using the native minetest.register_craft before simplecrafting_lib gets a chance to replace it with its own code where both of those crafts have identical inputs (ie, they conflict with each other) and the second craft to be registered has "replacements" defined for it and a previous call to import_legacy_recipes caused one of those crafts to be removed from the native crafting system then the legacy import code will not detect the "replacements" when importing the second crafting recipe.

That situation hasn't come up in any of my testing, fortunately - it's a pretty unlikely set of circumstances. I'll try to fix it eventually but I'm kind of hoping issue #4901 will get fixed instead so I don't have to (it's a symptom of the horrible replacements-detecting workaround I'm currently forced to use because of that bug).
 

FaceDeer
Member
 
Posts: 267
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by FaceDeer » Sat Sep 30, 2017 16:03

I've been doing a bit more work using simplecrafting_lib recently while working on my "workshops" mod, and I've started adding a new layer to simplecrafting_lib in the process; "templates."

Basically, I realized that although simplecrafting_lib was powerful it required a lot of work on the part of mods that made use of it, and that 90% of the time people would want to implement interfaces with the same basic features anyway. So I've created some utility functions that will create a set of customized functions that you can add to a node to turn it into a crafting station with all the bells and whistles provided for you up front.

The two types currently supported are "tables" and "multifurnaces". Tables are very simple, and are basically the code and formspec from the original [crafting] mod that all of this originally derives from. If you've got a crafting type that's populated with recipes you can just go:

Code: Select all
local table_functions = simplecrafting_lib.generate_table_functions(craft_type, table_def)


You'll get a table that contains functions called allow_metadata_inventory_move, allow_metadata_inventory_put, can_dig, on_construct, on_metadata_inventory_move, on_metadata_inventory_put, on_metadata_inventory_take, and on_receive_fields. These functions can be placed directly onto a node and then when you right-click on it you'll get a crafting formspec like the one pictured in the first comment.

"Multifurnace" is a bit more complicated, it has the signature

Code: Select all
simplecrafting_lib.generate_multifurnace_functions(craft_type, fuel_type, multifurnace_def)


And generates those methods and also "on_timer". It turns the node into a fuel-using crafting node that's similar in general concept to the default Minetest furnace, but that takes multiple input items and has the capacity for a lot more fuel. It's the second interface pictured above. It can support furnace recipes that take multiple inputs, which the default furnace's interface can't. I plan to also make a default-furnace-like template, but I didn't have the code quite ready-made for that - the [crafting] one is still too rough, I hadn't been planning on using it for [workshops].

Currently those "def" parameters on the generate functions are a table of the form:

Code: Select all
{
   show_guides = true,
   alphabetize_items = true,
}


"show guides" puts a Show Guide button on the formspec that shows a crafting guide, and alphabetize_items alphabetizes the ingredient lists by human-readable name rather than internal definition name.

This is just my first pass at adding these things to simplecrafting_lib. I want to add more customizability to these prepackaged methods going forward, so the API is a bit unstable right now. Let me know what other sorts of crafting stations might be desired and I'll see what I can do to support them, either by adding new templates or by adding customizability features to existing templates.

These templates are just here to make it a lot easier for modders to use [simplecrafting_lib] "out of the box." The rest of simplecrafting's API is still there, so if these templates don't suit your needs you can still roll your own just as easily, and since simplecrafting is licensed under the MIT license there should be no problem with copying bits of code from the templates' source files to do so.
 

FaceDeer
Member
 
Posts: 267
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by FaceDeer » Mon Oct 02, 2017 07:02

I've added a new optional feature to simplecrafting_lib that may be very labor-saving and useful under certain circumstances: disintermediation.

I am open to suggestions on a better name for this. :)

Basically, if there's a craft_type that's got a registered recipe that goes A + B => C and another registered recipe that goes C + D => E, simplecrafting_lib can now automatically add a new recipe to that craft_type that goes A + B + D => E. This allows the player to skip directly to the finished product if he's got all of the required materials. Due to the limitations of the legacy crafting system a lot of mods define intermediate crafting materials purely to prevent recipe collisions in the 3x3 crafting grid, something that simplecrafting_lib no longer needs to worry about.

As a concrete example: mesecons:silicon is made from default:sand and default:steel, and mesecons:solar_panels are made from mesecons:silicon. Disintermediation will allow a player to craft solar panels directly from sand and steel. Note that disintermediation will only do this if mesecons:silicon and mesecons:solar_panels are both registered to the same craft_type.

Image

The upper two recipes are defined by mesecons and the lower recipe is a result of disintermediation.

This mechanism is disabled by default. It can be enabled for a particular craft_type using the API method:
Code: Select all
simplecrafting_lib.set_disintermediation_cycles(craft_type, cycle_count)


Set cycle_count to some number greater than 0. The reason for multiple cycles is that adding a set of disintermediation recipes could potentially open up more possibilities for further disintermediation. 2 cycles should generally be enough for all but the most complicated webs of interrelated recipes.

Note that it is possible in some circumstances for disintermediation to go a bit awry and result in an exponential increase in recipes. This can happen when there are recipes that form "loops" wherein an item can be recycled back to its original components. To break loops like this there are flags you can set on individual recipes to exempt them from the disintermediation process. I go into some more detail about this in the api.txt document.
 

User avatar
texmex
Member
 
Posts: 1063
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by texmex » Mon May 28, 2018 10:50

I wonder if you would be interested in making a sfinv tab that features a formspec design with possible crafts in the upper section and the regular player inventory in the lower section, much like your crafting table mod. The upper section would sometimes need pagination and search but I figure that part can be taken from the creative formspec.

By implementing it as a sfinv the default crafting system can easily be replaced or enriched.
 

FaceDeer
Member
 
Posts: 267
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by FaceDeer » Mon May 28, 2018 13:45

Yeah, that's definitely the one remaining feature I need to consider this mod a "complete" crafting replacement system. I've been focused on my mapgen mods since getting back into modding but once that winds down simplecrafting is still one of the things I really want to get done so I'll be taking a look at that.
 

User avatar
texmex
Member
 
Posts: 1063
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by texmex » Mon May 28, 2018 19:50

That sounds terrific. I’ll be happy to take it for a spin and follow up with critique.
 

User avatar
texmex
Member
 
Posts: 1063
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

Re: [Mod] Extensible gridless craft system [simplecrafting_l

by texmex » Tue Jul 03, 2018 12:32

Any closing in on looking into the sfinv crafting tab? I'm thinking of a multiplayer game with that and with the crafting table. Since the crafting table has a "shared" inventory in that multiple player may add items to it, both the sfinv tab and the table has a purpose in that game. In my game, I will override the crafting table's name to "Shared crafting table" or similar to highlight its function.
 


Return to WIP Mods



Who is online

Users browsing this forum: No registered users and 6 guests