Hey everyone, besides refactoring our code extensively (as in here) I was also working at a new worker in the past weeks.
This worker is "The Smelter".
Before I talk more about his code some basic specs:
The Smelter is a worker quite similar to the cook. He basically requests ore and fuel to the colony (warehouse, or the player),
and when he receives this he will smelt the ore using the fuel.
Besides that, he's also able to deconstruct tools, weapons or armor the player doesn't need anymore.
In order to create a new Worker in Minecolonies you need the following classes:
Job - The job of the worker which tells you about his model, about his AI and store job-related information even to disk.
Block - The block the worker is connected with and which he uses to execute his job.
Building - The building which contains the block and stores information about the worker to disk as well (Which stays even after worker death).
AI - The actual AI of the worker which defines exactly what he does.
On the basis of that, I created the following classes.
And since we don't have a Model and texture yet, as you can see in the code we defined the current style to be the miner style until we have something more fitting.
Besides that, I had to request certain Building-styles to our community so the worker has a roof over his head and enough furnaces to actually work.
In the next step, I added a functionality to our request-system which allows each worker to request just "ore" and the warehouse will give him the first "ore" he can find.
Our coders did a really great job with the request system, which allows us to add quite generic classes of deliverable items and block which can be fulfilled by the deliveryman.
Then as a first step before implementing the AI, I had to define exactly what the AI has to do and I broke it down into the following basic tasks:
Which can be shortly described as:
- Getting fuel
- Smelting ore
- Gathering things from the chests
- Retrieving smelted things from the furnace
- Breaking down tools etc in raw materials
I was then, finally able to start with the AI itself.
First of all, I connected the different AI states and how they'll be called in the constructor of the AI class:
Besides that, I defined that the worker is allowed to pick up items and what his best working skills are (used for leveling).
The most interesting part here is probably "Start-Working" since that is basically the task which starts off most of the other tasks.
When he starts working he first always checks if there is anything he can get from the furnace (if any ore is ready) if that is not necessary he will check if he has ore to smelt and if necessary request it.
After that he will check the furnaces he has and will decide if he a) Will go to retrieve ore since it's ready now or b) start smelting ore since there is ore in a furnace but its missing fuel.
If there is no furnace with anything to do he will try to deconstruct tools, weapons or armor he finds around his inventory.
I hope you liked our new worker, we're pretty happy to have one more great addition to our colonies, see you the next time!
Posted on Utopian.io - Rewarding Open Source Contributors