Cuberite
A lightweight, fast and extensible game server for Minecraft
Villager.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "PassiveMonster.h"
5 #include "../Blocks/ChunkInterface.h"
6 #include "../Inventory.h"
7 
8 
9 
10 
11 class cVillager:
12  public cPassiveMonster
13 {
15 
16 public:
17 
19  {
20  vtFarmer = 0,
22  vtPriest = 2,
24  vtButcher = 4,
25  vtGeneric = 5,
26  vtMax
27  } ;
28 
29  cVillager(eVillagerType VillagerType);
30 
32 
33 
35 
36  // cEntity overrides
37  virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
38  virtual void Tick (std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
39  virtual void KilledBy (TakeDamageInfo & a_TDI) override;
40 
41  // cVillager functions
43  cItemGrid & GetInventory(void) { return m_Inventory; }
44  const cItemGrid & GetInventory(void) const { return m_Inventory; }
45 
47  bool IsBlockFarmable(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
48 
50  bool IsHarvestable(Vector3i a_CropsPos);
51 
53  bool IsPlantable(Vector3i a_CropsPos);
54 
55  // Farmer functions
57  {
61  } ;
62 
63  static const int FARMER_RANDOM_TICK_SPEED = 5;
65  static constexpr double FARMER_SPECIAL_ACTION_CHANCE = 0.1;
67  static constexpr Vector3i FARMER_SCAN_CROPS_DIST {15, 1, 15};
68 
70  void TickFarmer();
71 
73  void ScanAreaForWork();
74 
77 
80 
82  void CheckForNearbyCrops();
83 
85  bool CanPlantCrops();
86 
88  bool IsIdling()
89  {
90  return m_FarmerAction == faIdling;
91  }
92 
93  // Get and set functions.
94  int GetVilType(void) const { return m_Type; }
96 
97 private:
98 
100  int m_Type;
104 
105 } ;
106 
107 
108 
109 
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
#define CLASS_PROTODEF(classname)
Definition: Entity.h:13
Definition: Chunk.h:36
Definition: Entity.h:76
cPassiveMonster(const AString &a_ConfigName, eMonsterType a_MobType, const AString &a_SoundHurt, const AString &a_SoundDeath, const AString &a_SoundAmbient, float a_Width, float a_Height)
void TickFarmer()
Tick function for farmers.
Definition: Villager.cpp:100
bool IsPlantable(Vector3i a_CropsPos)
Returns true if seeds can be planted at a given location.
Definition: Villager.cpp:396
eFarmerAction m_FarmerAction
Definition: Villager.h:101
Vector3i m_CropsPos
Definition: Villager.h:102
eFarmerAction
Definition: Villager.h:57
@ faIdling
Definition: Villager.h:58
@ faHarvesting
Definition: Villager.h:60
@ faPlanting
Definition: Villager.h:59
static eVillagerType GetRandomProfession()
Returns a random Profession.
Definition: Villager.cpp:405
static constexpr Vector3i FARMER_SCAN_CROPS_DIST
This distance from the Villager makes for a 31x3x31 area.
Definition: Villager.h:67
void HandleFarmerTryHarvestCrops()
Looks if the farmer has reached it's destination, and if it's still crops and the destination is clos...
Definition: Villager.cpp:211
eFarmerAction GetFarmerAction(void) const
Definition: Villager.h:95
int m_Type
Definition: Villager.h:100
int GetVilType(void) const
Definition: Villager.h:94
bool IsHarvestable(Vector3i a_CropsPos)
Returns true if the block at the given location is a fully grown up crop.
Definition: Villager.cpp:387
void ScanAreaForWork()
Searches in a 31x3x31 area to harvest crops or spaces to plant crops.
Definition: Villager.cpp:165
const cItemGrid & GetInventory(void) const
Definition: Villager.h:44
static const int FARMER_RANDOM_TICK_SPEED
Definition: Villager.h:63
int m_ActionCountDown
Definition: Villager.h:99
bool IsBlockFarmable(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Returns true if the given blocktype are: crops, potatoes or carrots and they have full grown up.
Definition: Villager.cpp:363
static constexpr double FARMER_SPECIAL_ACTION_CHANCE
With 10% chance, it takes about 20 seconds to find a spot.
Definition: Villager.h:65
bool CanPlantCrops()
Returns whether the farmer has crops in his inventory to plant.
Definition: Villager.cpp:351
cItemGrid m_Inventory
Definition: Villager.h:103
eVillagerType
Definition: Villager.h:19
@ vtPriest
Definition: Villager.h:22
@ vtFarmer
Definition: Villager.h:20
@ vtGeneric
Definition: Villager.h:25
@ vtBlacksmith
Definition: Villager.h:23
@ vtLibrarian
Definition: Villager.h:21
@ vtButcher
Definition: Villager.h:24
bool IsIdling()
Returns whether the farmer is not working.
Definition: Villager.h:88
void CheckForNearbyCrops()
Checking for harvesting or planting nearby crops.
Definition: Villager.cpp:237
void HandleFarmerTryPlaceCrops()
Looks if the farmer has reached it's destination, and if it's still non obstructed farmland and the d...
Definition: Villager.cpp:271
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
When hit by someone, run away.
Definition: Villager.cpp:27
cItemGrid & GetInventory(void)
Returns the villager hidden inventory (8 slots).
Definition: Villager.h:43
cVillager(eVillagerType VillagerType)
Definition: Villager.cpp:14
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Villager.cpp:55
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Villager.cpp:78