From fec64bb91c03c5e872a8f6fbc1a253f341373072 Mon Sep 17 00:00:00 2001 From: Persson-dev <66266021+Persson-dev@users.noreply.github.com> Date: Wed, 29 Dec 2021 20:28:41 +0100 Subject: Improved farmer AI & Fixed entity loading functions (#5351) * Allow villagers to pickup items * Add farmer villager harvesting * Use of auto keyword * Using for loop to check adjacent crops * Show particules when farmer harvest * Fix area comment * Move constants to header file * Removing unnecessary semicolon * Initialization of CropBlockType variable * Apply 12xx12 suggestion * Fixing area constant size * Refactor bounding box calculation, use vectors. * Add Api documentation * Update lua docs * Rework farmer ai * Fixing lua docs notes * Add missing capitalisation * Add villagers inventory save * Fixing loading entities from disk inconsistencies * Add farmer harvest animation * Fix beetroots grow state Co-authored-by: Alexander Harkness --- src/Mobs/Villager.h | 61 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 11 deletions(-) (limited to 'src/Mobs/Villager.h') diff --git a/src/Mobs/Villager.h b/src/Mobs/Villager.h index 4ac06765c..ef1c9e70f 100644 --- a/src/Mobs/Villager.h +++ b/src/Mobs/Villager.h @@ -3,6 +3,7 @@ #include "PassiveMonster.h" #include "../Blocks/ChunkInterface.h" +#include "../Inventory.h" @@ -38,30 +39,68 @@ public: virtual void KilledBy (TakeDamageInfo & a_TDI) override; // cVillager functions - /** return true if the given blocktype are: crops, potatoes or carrots. */ - bool IsBlockFarmable(BLOCKTYPE a_BlockType); + /** Returns the villager hidden inventory (8 slots). */ + cItemGrid & GetInventory(void) { return m_Inventory; } + const cItemGrid & GetInventory(void) const { return m_Inventory; } + + /** Returns true if the given blocktype are: crops, potatoes or carrots and they have full grown up. */ + bool IsBlockFarmable(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); + + /** Returns true if the block at the given location is a fully grown up crop. */ + bool IsHarvestable(Vector3i a_CropsPos); + + /** Returns true if seeds can be planted at a given location. */ + bool IsPlantable(Vector3i a_CropsPos); // Farmer functions - /** Searches in a 11x7x11 area for crops. If it found some it will navigate to them. */ - void HandleFarmerPrepareFarmCrops(); + enum eFarmerAction + { + faIdling, + faPlanting, + faHarvesting, + } ; + + static const int FARMER_RANDOM_TICK_SPEED = 5; + /** With 10% chance, it takes about 20 seconds to find a spot. */ + static constexpr double FARMER_SPECIAL_ACTION_CHANCE = 0.1; + /** This distance from the Villager makes for a 31x3x31 area. */ + static constexpr Vector3i FARMER_SCAN_CROPS_DIST {15, 1, 15}; - /** Looks if the farmer has reached it's destination, and if it's still crops and the destination is closer then 2 blocks it will harvest them. */ + /** Tick function for farmers. */ + void TickFarmer(); + + /** Searches in a 31x3x31 area to harvest crops or spaces to plant crops. If it found some it will navigate to them. */ + void ScanAreaForWork(); + + /** Looks if the farmer has reached it's destination, and if it's still crops and the destination is closer then 1 block it will harvest them. */ void HandleFarmerTryHarvestCrops(); - /** Replaces the crops he harvested. */ - void HandleFarmerPlaceCrops(); + /** Looks if the farmer has reached it's destination, and if it's still non obstructed farmland and the destination is closer then 1 block it will plant crops. */ + void HandleFarmerTryPlaceCrops(); + + /** Checking for harvesting or planting nearby crops */ + void CheckForNearbyCrops(); + + /** Returns whether the farmer has crops in his inventory to plant. */ + bool CanPlantCrops(); + + /** Returns whether the farmer is not working. */ + bool IsIdling() + { + return m_FarmerAction == faIdling; + } // Get and set functions. - int GetVilType(void) const { return m_Type; } - Vector3i GetCropsPos(void) const { return m_CropsPos; } - bool DoesHaveActionActivated(void) const { return m_VillagerAction; } + int GetVilType(void) const { return m_Type; } + eFarmerAction GetFarmerAction(void) const { return m_FarmerAction; } private: int m_ActionCountDown; int m_Type; - bool m_VillagerAction; + eFarmerAction m_FarmerAction; Vector3i m_CropsPos; + cItemGrid m_Inventory; } ; -- cgit v1.2.3