summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Ocelot.cpp
blob: 329c175277b6c0bf5257fc01cd520fa6d39c37c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Ocelot.h"
#include "../World.h"
#include "../Entities/Player.h"
#include "../Items/ItemHandler.h"
#include "../BoundingBox.h"





// TODO: Ocelots should have a chance of spawning with two kittens
/*
	if (!IsBaby() && GetRandomProvider().RandBool(1.0 / 7.0))
	{
		m_World->SpawnMob(GetPosX(), GetPosY(), GetPosZ(), m_MobType, true);
		m_World->SpawnMob(GetPosX(), GetPosY(), GetPosZ(), m_MobType, true);
	}
*/

cOcelot::cOcelot(void) :
	Super("Ocelot", mtOcelot, "entity.cat.hurt", "entity.cat.death", "entity.cat.ambient", 0.6f, 0.7f),
	m_IsSitting(false),
	m_IsTame(false),
	m_IsBegging(false),
	m_CatType(ctWildOcelot),
	m_OwnerName("")
{
}





void cOcelot::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	Super::Tick(a_Dt, a_Chunk);
	if (!IsTicking())
	{
		// The base class tick destroyed us
		return;
	}

	if (!IsTame() && !IsBaby())
	{
		if (m_CheckPlayerTickCount == 23)
		{
			m_World->DoWithNearestPlayer(GetPosition(), 10, [&](cPlayer & a_Player) -> bool
			{
				cItems Items;
				GetBreedingItems(Items);
				if (Items.ContainsType(a_Player.GetEquippedItem().m_ItemType))
				{
					if (!IsBegging())
					{
						SetIsBegging(true);
						m_World->BroadcastEntityMetadata(*this);
					}

					MoveToPosition(a_Player.GetPosition());
				}
				else
				{
					if (IsBegging())
					{
						SetIsBegging(false);
						m_World->BroadcastEntityMetadata(*this);
					}
				}

				return true;
			}, true);
			m_CheckPlayerTickCount = 0;
		}
		else
		{
			m_CheckPlayerTickCount++;
		}
	}

	if (IsTame() && !IsSitting())
	{
		TickFollowPlayer();
	}
	else if (IsSitting())
	{
		StopMovingToPosition();
	}

	m_World->BroadcastEntityMetadata(*this);
}





void cOcelot::TickFollowPlayer()
{
	Vector3d OwnerPos;
	bool OwnerFlying = false;
	auto Callback = [&](cPlayer & a_Player)
	{
		OwnerPos = a_Player.GetPosition();
		OwnerFlying = a_Player.IsFlying();
		return true;
	};

	if (m_World->DoWithPlayerByUUID(m_OwnerUUID, Callback))
	{
		// The player is present in the world, follow him:
		double Distance = (OwnerPos - GetPosition()).Length();
		if (Distance > 12)
		{
			if (!OwnerFlying)
			{
				OwnerPos.y = FindFirstNonAirBlockPosition(OwnerPos.x, OwnerPos.z);
				TeleportToCoords(OwnerPos.x, OwnerPos.y, OwnerPos.z);
			}
		}
		if (Distance < 2)
		{
			StopMovingToPosition();
		}
		else
		{
			if (!OwnerFlying)
			{
				MoveToPosition(OwnerPos);
			}
		}
	}
}





void cOcelot::OnRightClicked(cPlayer & a_Player)
{
	if (!IsTame())
	{
		if (
			IsBegging() &&
			((a_Player.GetPosition() - GetPosition()).Length() <= 3)
		)
		{
			cItems Items;
			GetBreedingItems(Items);
			if (Items.ContainsType(a_Player.GetEquippedItem().m_ItemType))
			{
				if (!a_Player.IsGameModeCreative())
				{
					a_Player.GetInventory().RemoveOneEquippedItem();
				}

				auto & Random = GetRandomProvider();

				if (Random.RandBool(1.0 / 3.0))
				{
					// Taming succeeded
					SetIsBegging(false);

					SetMaxHealth(20);
					SetIsTame(true);
					SetOwner(a_Player.GetName(), a_Player.GetUUID());
					SetCatType(static_cast<eCatType>(Random.RandInt<int>(1, 3)));
					m_World->BroadcastEntityStatus(*this, esWolfTamed);
					m_World->BroadcastParticleEffect("heart", static_cast<Vector3f>(GetPosition()), Vector3f{}, 0, 5);
				}
				else
				{
					// Taming failed
					m_World->BroadcastEntityStatus(*this, esWolfTaming);
					m_World->BroadcastParticleEffect("smoke", static_cast<Vector3f>(GetPosition()), Vector3f{}, 0, 5);
				}
			}
		}
		else
		{
			Super::OnRightClicked(a_Player);
		}
	}
	else if (a_Player.GetUUID() == m_OwnerUUID)
	{
		Super::OnRightClicked(a_Player);
		SetIsSitting(!IsSitting());
	}
	m_World->BroadcastEntityMetadata(*this);
}





bool cOcelot::IsCatSittingOnBlock(cWorld * a_World, Vector3d a_BlockPosition)
{
	return a_World->ForEachEntityInBox(
		cBoundingBox(Vector3d(a_BlockPosition.x, a_BlockPosition.y + 1, a_BlockPosition.z), 1),
		[=](cEntity & a_Entity)
		{
			return (
				(a_Entity.GetEntityType() == cEntity::etMonster) &&
				(static_cast<cMonster &>(a_Entity).GetMobType() == eMonsterType::mtOcelot) &&
				(static_cast<cOcelot &>(a_Entity).IsSitting())
			);
		}
	);
}





bool cOcelot::DoTakeDamage(TakeDamageInfo & a_TDI)
{
	if (a_TDI.DamageType == dtFalling)
	{
		return false;
	}

	return Super::DoTakeDamage(a_TDI);
}