Description
I have created a separated repository for this. I'll probably start from scratch but first I want to have a specifications sheet that defines everything, from API interface with nodes to performance tips&tricks and, obviously, overall system architecture. Please contribute there with your ideas, suggestions and requirements. Just create issues or, even better, pull requests are greatly accepted to help me write the specifications document. If you plan on contributing constantly I can give you access to the repository.
https://github.com/gdelazzari/tech_api
Thank you!
Hi everyone, I've been experimenting and sketching out a new energy distribution API with the aim to create something that, in the future, may become a common API (i.e. a mod that other mods will depend on) that will make energy distribution and item & fluid management uniform and easier for anyone willing to write mods with such requirements. For the moment I'm focusing just on power. Also, I'm not using metadata to pass energy through networks so the Technic mod would benefit from this in terms of performance (given the current implementation). I already talked with nora on IRC #minetest-technic and he told me to write down here my progress and my current results so everyone can possibly contribute ideas or, maybe, help with the code.
This is what I have so far.
The system uses a table (under the API namespace) to store the energy networks. A network is a collection of devices connected by a transporter (i.e. a wire - a transporter is just a more general concept of something that can move energy around). A device can be a user, a provider or a storage. I think the names are self-explanatory, but if you read the comment at the beginning of the Pastebin below you'll get more information.
This means that every time a player places something that is power-capable, the table must be updated. This is not so easy to manage (especially when you need to merge two networks because a wire connected them together or, vice-versa, you need to split them etc...) but the advantage is that in one minetest globalstep handler we can manage the power distribution for all the networks in the world. Also, I'm using callbacks to pass energy in/out from devices, so no metadata is used in the process (except if the machines have an internal buffer and need to save the energy they contain, or in the case of storage devices like batteries. Maybe there's another way of doing that, but I'm not aware of any at the moment).
From a "mod-that-uses-the-API" perspective, I want to keep the usage really simple. This is what I have so far (please note that everything is experimental and thrown there, there are a few things that could be implemented differently so that it's simpler to use the API, but I need feedback regarding this).
This is a Pastebin with an example API usage and some comments to explain what's going on: https://pastebin.com/smDzZ6Nq
As you may notice there are two callbacks for each device every tick of the server, which is not the best thing in terms of CPU usage. However I plan to find a better solution. nora (I still don't know your username on GitHub, sorry :D) on #minetest-technic has made a proposal for this, maybe he can report it below so it's here for reference.
If you have spare time you can look at the current API implementation (which, by the way, is a mess). It is also incomplete (still no wire removal thus no network split) and has a ton of bugs, especially on the power distribution algorithm that was written in something like 15 minutes just to test out if the power was somehow getting from a point to the other (however I promise your PC won't explode). If you want to try it on a test world sure you can, just /give yourself some "unitech:user", "unitech:provider", "unitech:storage", "unitech:wire" and, if you want to get fancy, some "unitech:solar_panels" (featuring textures stolen from Technic - don't hate me). They're an example of a solar panel implementation that uses the API. Every freaking thing is "logged" on the chat, so you should be able to understand what's going on internally.
Just download the ZIP file attached and install the "mod".
unitech.zip
The nodes are simply demo-devices represented by colored cubes with some text on them, just to let you know what you're doing. Please note that the API, ideally, would not define any node: other mods will do that. The advantage is that we can have a mod just providing solar panels, a mod providing fancy wires, another one providing super-useful machines etc... all working together seamlessly. The nodes I have implemented are just to test out the system.
All the implementation is in the script unitech/power/power.lua
If you notice strange power distribution (e.g. storages not charging correctly, power losses etc..) don't worry, it's just because of the sketchy algorithm - which is not the priority at the moment. The priorities at the moment, IMHO, are the following:
- defining whether or not this will be useful if I develop it completely
- in the case of positive response to the point above, defining the API (I mean how nodes are going to interface with the power system in terms of functions/callbacks/properties/etc)
- given that I'm writing this from scratch, define all the best practices to make this blazing fast and not overload the network or the servers CPU in the case of multiplayer worlds
I hope this is somehow going to be appreciated, I'll be glad to have some feedback along with ideas. If anyone wants to help me write this, they will be totally welcome :)
Thank you for your attention!