Minecolonies new AI worker: the composter

in #utopian-io6 years ago

Hi everyone! To continue with my last contribution, I've made the AI worker that will use the Barrel Block: the Composter AI. This AI will come with his hut building just like the other workers, and will have some customization regarding the items it will use.

What's new:

In my last contribution, we added the Barrel Block, a block that will allow you to exchange biological waste items (saplings, seeds, rotten flesh or even excess food) for Compost, a new item that behaves like Bone Meal. This could be used by the player, but we want it to be fully automatic. For that, we added the Composter: a fully automatic AI that will ask for those materials and will take them into the barrel, and also collect the generated compost.

Also, some adjustments were made to the BarrelBlock.

First, we need to craft the block for the hut. For this you will need a building tool, 7 wooden planks, and a barrel block.

When we have it, we will place it where we want it (I recommend you to use the building tool for that), and ask our builder to build it.

When is done we want to hire a worker (deactivate automatic hiring to get the best worker for the job), with the maximum intelligence and charisma possible.

After we have our composter hired he will need some instructions (he will remind you and your officers once a day if you dont make it) regarding what items he should compost. That can be done through the GUI on his building (right click on the hut block and click on the arrow at the top right of the window). Select what items do you want him to use there, and is ready.

Once this is done, he will start using those items to make compost, and if he runs out of them, he will ask the delivery man for more through requests.

What we did

Like the last time I published here, I will cover only those changes that are really significant for the correct working of the addition to make the post a little more readable.

First, the classes we've worked with, and what are they for:

  • EntityAIWorkComposter: The heart and brain of our worker. Here he will decide what to do.
  • BlockHutComposter: The block for the building. The central hub of operations for everything.
  • JobComposter: This will let know the colony and the building what should be doing the citizen assigned to a building. In this case it references our AI. Also gives the entity its appereance
  • BuildingComposter: This will manage all the things referencing our building, such as the barrels we have, building level. It is also our way into out GUI.
  • WindowHutComposter: The GUI for the building. Here we will let the user select the items he wants the composter to use.

More classes were edited/added to fit this addition (such as the CompatibilityManager or the worker models), but going over them will make the post unnecessarily long. Full changes can be seen on the PR (link at the end of the document).

BlockHutComposter:

This class follows the default implementation of AbstractBlockHut, and the only overriden method is getName which returns the name of the block.

JobComposter:

Almost exactly the same implementation as AbstractJob, but rewriting methods like getModel to fit the new worker.

BuildingComposter:

Here we store the level of the building (each level will add a new Barrel to the building). We have to store the position of the barrels that are added to the building by the builder. For that we have a List of BlockPos that will be filled on the method registerBlockPosition that will be called by the builder while upgrading the building:

On the constructor of the building we set keepX, which will make the chest keep some items when the Delivery Man tries to empty the barrel. For this we use the isAllowedItem method, that will check if the item is on the list of items we let the worker use.

That list will be filled by the WindowHutComposter.

Finally, we have to store and read all this info in case the player logs of or unloads the chunck. For that we override writeToNBT and readFromNBT.

WriteToNBT, where we write down the barrel list and the allowed items list.

ReadFromNBT, where we read both of the lists from the NBT:

WindowHutComposter:

This is the class that will represent our GUI.

To let the player choose wich items to compost, we need first to retrieve all the items that can be used by the barrel. For that we added a method on the CompatibilityManager to discover and return all the compostable items (as they might change if other mods are present). We store those in a list on the constructor for later use.

Next, we will add a handler to the button clicks in wich we will check what icon is the button holding, and we will add or remove the item it represents on the list and change the icon on the button:

EntityAIWorkComposter:

With all the rest of the classes explained, we can go into EntityAIWorkComposter.

This class behaves like a state machine to tell the entity what to make. For that purpose we will define the different states our AI can take and what methods should trigger each state:

The entry state is IDLE, which transitions into START_WORKING. This state will call decideWhatToDo, in which the AI will check what needs to be done.

For this purpose we make the worker go to his building. When he arrives, we iterate through the barrels on the building 2 times: first checking if there are finished barrels needing to be harvested, and then checking if we can fill any barrel.

A barrel ready to be harvested will take the AI to go to the COMPOSTER_HARVEST state which triggers the harvestBarrels method, marking the blockPos of the barrel we intend to harvest as the currentTarget:

Here we will make our worker walk to the barrel (he will stop if he's close enough to interact with it), and we will make him hit the block to simulate physical interaction as the same time we retrieve teh compost from the TileEntity of the barrel (the amount will depend on the composter level). We also give some experience to the worker as he just finished a task.

After this the worker will try to decide what to do again. The other task that can be given to the AI is fill the empty barrels. For that we will get the AI into the COMPOSTER_FILL state when a barrel is detected to not be full. This state triggers the method fillBarrels:

First we need to check if the AI has a valid item to fill the barrel in his inventory. For that we will check with the building. If we have the needed item, we make the worker hold it on his hand and walk to the barrel that needs to be filled. Once he is there, we make him hit the barrel, again to simulate interaction, we add the item to the TileEntity of the barrel. We give him experience again and increment the actions until dumping (set to 1 so we maintain a clean inventory).

If the worker doesn´t have the needed item on his inventory, we will make it go to the GATHERING_REQUIRED_MATERIALS state, which will trigger our getMaterials method:

Here we will make the worker go to his building block. When he arrives, we check first, if the list of valid items is not empty. If is empty, we message the officers and owner of the colony asking for them to configure it on the complain method.

If the list is not empty, we check if there are any valid items on the chest. If there are no valid items, we make the worker make a new request for the colony if there are no pending requests from that worker.

If we find valid items, we take them from the chest, and make the worker hold them (the last stack get from the chest) on his main hand.

Models

The models for the worker were generated by a third party tool. This tool generated a .java that includes the shape of the model and the animated parts.

But the .java s were corrupted as the tool didnt export them correctly and we had to fix them by hand, which was really tedious and took longer than expected.

Fixes made

After the original PR, we found that the AI could be easilly optimized adding some delays between actions, and changing the way the main loops on the decideWhatToDo method deal with the TileEntities (changes already present in the screenshots on this document).

Also, an imposible cast was detected while testing that caused the game to crash everytime the GUI from the composter was opened.

Links to the PRs:

Original PR: https://github.com/ldtteam/minecolonies/pull/2701

Fix PRs:
https://github.com/ldtteam/minecolonies/pull/2703
https://github.com/ldtteam/minecolonies/pull/2705


And that is all for this update! I hope you liked it and that you find the AI useful! See you in the next update!

Sort:  

Thanks for the contribution, @fireruner! Great post and a very interesting read and I honestly don't have much to remark other than that your commit messages could definitely be improved. Messages like "Fixing some issues", "Minor changes" ... aren't really useful, so keep an eye on that. Other than that: keep up the great work!

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks! Will take a look for the next commits :D

Congratulations @fireruner! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Hey @fireruner
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @fireruner! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

You got a First Reply

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @fireruner! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @fireruner! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @fireruner! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.031
BTC 61659.88
ETH 2579.99
USDT 1.00
SBD 2.55