Cuberite
A lightweight, fast and extensible game server for Minecraft
Pig.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "Pig.h"
5 #include "../Entities/Player.h"
6 #include "../World.h"
7 
8 
9 
10 
11 
12 cPig::cPig(void) :
13  Super("Pig", mtPig, "entity.pig.hurt", "entity.pig.death", "entity.pig.ambient", 0.9f, 0.9f),
14  m_bIsSaddled(false)
15 {
16 }
17 
18 
19 
20 
21 
22 void cPig::GetDrops(cItems & a_Drops, cEntity * a_Killer)
23 {
24  if (IsBaby())
25  {
26  return; // Babies don't drop items
27  }
28 
29  unsigned int LootingLevel = 0;
30  if (a_Killer != nullptr)
31  {
33  }
34  AddRandomDropItem(a_Drops, 1, 3 + LootingLevel, IsOnFire() ? E_ITEM_COOKED_PORKCHOP : E_ITEM_RAW_PORKCHOP);
35  if (m_bIsSaddled)
36  {
37  a_Drops.emplace_back(E_ITEM_SADDLE, static_cast<char>(1));
38  }
39 }
40 
41 
42 
43 
44 
45 void cPig::OnRightClicked(cPlayer & a_Player)
46 {
47  Super::OnRightClicked(a_Player);
48 
49  if (m_bIsSaddled)
50  {
51  if (m_Attachee != nullptr)
52  {
53  if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
54  {
55  // This player is already sitting in, they want out.
56  a_Player.Detach();
57  return;
58  }
59 
60  if (m_Attachee->IsPlayer())
61  {
62  // Another player is already sitting in here, cannot attach
63  return;
64  }
65 
66  // Detach whatever is sitting in this pig now:
67  m_Attachee->Detach();
68  }
69 
70  // Attach the player to this pig:
71  a_Player.AttachTo(*this);
72  }
73  else if (a_Player.GetEquippedItem().m_ItemType == E_ITEM_SADDLE)
74  {
75  if (!a_Player.IsGameModeCreative())
76  {
78  }
79 
80  // Set saddle state & broadcast metadata
81  m_bIsSaddled = true;
83  }
84 }
85 
86 
87 
88 
89 
90 void cPig::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
91 {
92  Super::Tick(a_Dt, a_Chunk);
93  if (!IsTicking())
94  {
95  // The base class tick destroyed us
96  return;
97  }
98 
99  // If the attachee player is holding a carrot-on-stick, let them drive this pig:
100  if (m_bIsSaddled && (m_Attachee != nullptr))
101  {
103  {
105  }
106  }
107 }
108 
109 
110 
111 
112 
114 {
115  if (!Super::DoTakeDamage(a_TDI))
116  {
117  return false;
118  }
119 
120  if (a_TDI.DamageType == dtLightning)
121  {
122  Destroy();
124  return true;
125  }
126  return true;
127 }
128 
129 
130 
131 
@ E_ITEM_RAW_PORKCHOP
Definition: BlockType.h:363
@ E_ITEM_COOKED_PORKCHOP
Definition: BlockType.h:364
@ E_ITEM_SADDLE
Definition: BlockType.h:373
@ E_ITEM_CARROT_ON_STICK
Definition: BlockType.h:444
@ dtLightning
Definition: Defines.h:248
@ mtPig
Definition: MonsterTypes.h:47
@ mtZombiePigman
Definition: MonsterTypes.h:85
Definition: Chunk.h:36
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
eDamageType DamageType
Definition: Entity.h:61
Definition: Entity.h:76
bool IsPlayer(void) const
Definition: Entity.h:160
cEntity * m_Attachee
The entity which is attached to this entity (rider), nullptr if none.
Definition: Entity.h:591
void Detach(void)
Detaches from the currently attached entity, if any.
Definition: Entity.cpp:2052
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
double GetPosX(void) const
Definition: Entity.h:195
cWorld * m_World
Definition: Entity.h:624
double GetPosZ(void) const
Definition: Entity.h:197
UInt32 GetUniqueID(void) const
Definition: Entity.h:253
void Destroy()
Destroys the entity, schedules it for memory freeing and broadcasts the DestroyEntity packet.
Definition: Entity.cpp:243
virtual bool IsOnFire(void) const
Definition: Entity.h:489
double GetPosY(void) const
Definition: Entity.h:196
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:333
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2267
void AttachTo(cEntity &a_AttachTo)
Attaches to the specified entity; detaches from any previous one first.
Definition: Entity.cpp:2027
Definition: Player.h:29
const cItem & GetEquippedItem(void) const
Definition: Player.h:162
bool IsGameModeCreative(void) const
Returns true if the player is in Creative mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1025
cInventory & GetInventory(void)
Definition: Player.h:156
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
cEnchantments m_Enchantments
Definition: Item.h:166
short m_ItemType
Definition: Item.h:163
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
virtual void MoveToPosition(const Vector3d &a_Position)
Engage pathfinder and tell it to calculate a path to a given position, and move the mob accordingly.
Definition: Monster.cpp:245
void AddRandomDropItem(cItems &a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth=0)
Adds a random number of a_Item between a_Min and a_Max to itemdrops a_Drops.
Definition: Monster.cpp:1518
bool IsBaby(void) const
Definition: Monster.h:156
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
When hit by someone, run away.
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
When hit by someone, run away.
Definition: Pig.cpp:113
bool m_bIsSaddled
Definition: Pig.h:37
cPig()
Definition: Pig.cpp:12
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Pig.cpp:22
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Pig.cpp:90
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Pig.cpp:45
virtual UInt32 SpawnMob(double a_PosX, double a_PosY, double a_PosZ, eMonsterType a_MonsterType, bool a_Baby=false) override
Spawns a mob of the specified type.
Definition: World.cpp:2897
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override