summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Villager.cpp
blob: aa5409e4df4416e5e009edd5a91b62237c36e792 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

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

#include "Villager.h"
#include "../World.h"
#include "../BlockArea.h"
#include "../Blocks/BlockHandler.h"
#include "../BlockInServerPluginInterface.h"





cVillager::cVillager(eVillagerType VillagerType) :
	Super("Villager", mtVillager, "entity.villager.hurt", "entity.villager.death", "entity.villager.ambient", 0.6f, 1.95f),
	m_ActionCountDown(-1),
	m_Type(VillagerType),
	m_FarmerAction(faIdling),
	m_Inventory(8, 1)
{
}





bool cVillager::DoTakeDamage(TakeDamageInfo & a_TDI)
{
	if (!Super::DoTakeDamage(a_TDI))
	{
		return false;
	}

	if ((a_TDI.Attacker != nullptr) && a_TDI.Attacker->IsPlayer())
	{
		if (GetRandomProvider().RandBool(1.0 / 6.0))
		{
			m_World->BroadcastEntityAnimation(*this, EntityAnimation::VillagerShowsAnger);
		}
	}

	if (a_TDI.DamageType == dtLightning)
	{
		Destroy();
		m_World->SpawnMob(GetPosX(), GetPosY(), GetPosZ(), mtWitch, false);
		return true;
	}
	return true;
}





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

	switch (m_Type)
	{
		case vtFarmer:
		{
			TickFarmer();
			break;
		}
	}
}





void cVillager::KilledBy(TakeDamageInfo & a_TDI)
{
	Super::KilledBy(a_TDI);

	// TODO: 0% chance on Easy, 50% chance on Normal and 100% chance on Hard
	if (GetRandomProvider().RandBool(0.5) && (a_TDI.Attacker != nullptr) && (a_TDI.Attacker->IsMob()))
	{
		eMonsterType MonsterType = (static_cast<cMonster *>(a_TDI.Attacker)->GetMobType());
		if ((MonsterType == mtZombie) || (MonsterType == mtZombieVillager))
		{
			m_World->SpawnMob(GetPosX(), GetPosY(), GetPosZ(), mtZombieVillager, false);
		}
	}
}





////////////////////////////////////////////////////////////////////////////////
// Farmer functions:

void cVillager::TickFarmer()
{

	// Don't harvest crops if you must not
	if (!m_World->VillagersShouldHarvestCrops())
	{
		return;
	}

	// This is to prevent undefined behaviors
	if (m_FinalDestination.y <= 0)
	{
		return;
	}

	if (!IsIdling())
	{
		// Forcing the farmer to go to work spots.
		MoveToPosition(static_cast<Vector3d>(m_CropsPos) + Vector3d(0.5, 0, 0.5));

		// Forcing the farmer to look at the work spots.
		Vector3d Direction = (m_FinalDestination - (GetPosition() + Vector3d(0, 1.6, 0)));  // We get the direction from the eyes of the farmer to the work spot.
		Direction.Normalize();
		SetPitch(std::asin(-Direction.y) / M_PI * 180);
	}

	// Updating the timer
	if (m_ActionCountDown > -1)
	{
		m_ActionCountDown--;
	}

	// Searching for work in blocks where the farmer goes.
	if (IsHarvestable(m_FinalDestination.Floor()))
	{
		m_CropsPos = m_FinalDestination.Floor();
		m_FarmerAction = faHarvesting;
		HandleFarmerTryHarvestCrops();
		return;
	}
	else if (IsPlantable(m_FinalDestination.Floor()) && CanPlantCrops())
	{
		m_CropsPos = m_FinalDestination.Floor();
		m_FarmerAction = faPlanting;
		HandleFarmerTryPlaceCrops();
		return;
	}
	else
	{
		m_FarmerAction = faIdling;  // Returning to idling.
	}


	// Don't always try to do a special action. Each tick has 10% to do a special action.
	if (GetRandomProvider().RandBool(FARMER_SPECIAL_ACTION_CHANCE))
	{
		ScanAreaForWork();
	}

}





void cVillager::ScanAreaForWork()
{

	auto Pos = GetPosition().Floor();
	auto MinPos = Pos - FARMER_SCAN_CROPS_DIST;
	auto MaxPos = Pos + FARMER_SCAN_CROPS_DIST;

	// Read area to be checked for crops.
	cBlockArea Surrounding;
	Surrounding.Read(
		*m_World,
		MinPos, MaxPos
	);

	for (int I = 0; I < FARMER_RANDOM_TICK_SPEED; I++)
	{
		for (int Y = MinPos.y; Y <= MaxPos.y; Y++)
		{
			// Pick random coordinates and check for crops.
			Vector3i CandidatePos(MinPos.x + m_World->GetTickRandomNumber(MaxPos.x - MinPos.x - 1), Y, MinPos.z + m_World->GetTickRandomNumber(MaxPos.z - MinPos.z - 1));

			// A villager can harvest this.
			if (IsHarvestable(CandidatePos))
			{
				m_CropsPos = CandidatePos;
				m_FarmerAction = faHarvesting;
				MoveToPosition(static_cast<Vector3d>(m_CropsPos) + Vector3d(0.5, 0, 0.5));
				return;
			}
			// A villager can plant this.
			else if (IsPlantable(CandidatePos) && CanPlantCrops())
			{
				m_CropsPos = CandidatePos;
				m_FarmerAction = faHarvesting;
				MoveToPosition(static_cast<Vector3d>(m_CropsPos) + Vector3d(0.5, 0, 0.5));
				return;
			}

		}  // for Y
	}  // Repeat the proccess according to the random tick speed.
}





void cVillager::HandleFarmerTryHarvestCrops()
{
	if (m_ActionCountDown > 0)
	{
		// The farmer is still on cooldown
		return;
	}

	// Harvest the crops if it is closer than 1 block.
	if ((GetPosition() - m_CropsPos).Length() < 1)
	{
		// Check if the blocks didn't change while the villager was walking to the coordinates.
		if (IsHarvestable(m_CropsPos))
		{
			m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_BLOCK_BREAK, m_CropsPos, m_World->GetBlock(m_CropsPos));
			m_World->DropBlockAsPickups(m_CropsPos, this, nullptr);
			// Applying 0.5 second cooldown.
			m_ActionCountDown = 10;
		}
	}
}





void cVillager::CheckForNearbyCrops()
{

	// Search for adjacent crops

	constexpr std::array<Vector3i, 4> Directions = { Vector3i{0, 0, -1}, {0, 0, 1}, {1, 0, 0}, {-1, 0, 0} };

	for (Vector3i Direction : Directions)
	{
		if (IsHarvestable(m_CropsPos + Direction))
		{
			m_CropsPos += Direction;
			m_FarmerAction = faHarvesting;
			MoveToPosition(static_cast<Vector3d>(m_CropsPos) + Vector3d(0.5, 0, 0.5));
			return;
		}
		else if (IsPlantable(m_CropsPos + Direction) && CanPlantCrops())
		{
			m_CropsPos += Direction;
			m_FarmerAction = faPlanting;
			MoveToPosition(static_cast<Vector3d>(m_CropsPos) + Vector3d(0.5, 0, 0.5));
			return;
		}

	}

	// There is no more work to do around the previous crops.
	m_FarmerAction = faIdling;
}





void cVillager::HandleFarmerTryPlaceCrops()
{

	if ((GetPosition() - m_CropsPos).Length() > 1)
	{
		// The farmer is still to far from the final destination
		return;
	}

	if (m_ActionCountDown > 0)
	{
		// The farmer is still on cooldown
		return;
	}

	// Check if there is still farmland at the spot where the crops were.
	if (IsPlantable(m_CropsPos))
	{
		// Finding the item to use to plant a crop
		int TargetSlot = -1;
		BLOCKTYPE CropBlockType = E_BLOCK_AIR;

		for (int I = 0; I < m_Inventory.GetWidth() && TargetSlot < 0; I++)
		{
			const cItem & Slot = m_Inventory.GetSlot(I);
			switch (Slot.m_ItemType)
			{
				case E_ITEM_SEEDS:
				{
					TargetSlot = I;
					CropBlockType = E_BLOCK_CROPS;
					break;
				}

				case E_ITEM_BEETROOT_SEEDS:
				{
					TargetSlot = I;
					CropBlockType = E_BLOCK_BEETROOTS;
					break;
				}

				case E_ITEM_POTATO:
				{
					TargetSlot = I;
					CropBlockType = E_BLOCK_POTATOES;
					break;
				}

				case E_ITEM_CARROT:
				{
					TargetSlot = I;
					CropBlockType = E_BLOCK_CARROTS;
					break;
				}

				default:
				{
					break;
				}
			}
		}

		// Removing item from villager inventory
		m_Inventory.RemoveOneItem(TargetSlot);

		// Placing crop block
		m_World->SetBlock(m_CropsPos, CropBlockType, 0);

		// Applying 1 second cooldown
		m_ActionCountDown = 20;

		// Try to do the same with adjacent crops.
		CheckForNearbyCrops();
	}
}





bool cVillager::CanPlantCrops()
{
	return m_Inventory.HasItems(cItem(E_ITEM_SEEDS)) ||
		m_Inventory.HasItems(cItem(E_ITEM_BEETROOT_SEEDS)) ||
		m_Inventory.HasItems(cItem(E_ITEM_POTATO)) ||
		m_Inventory.HasItems(cItem(E_ITEM_CARROT));
}





bool cVillager::IsBlockFarmable(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	switch (a_BlockType)
	{
		case E_BLOCK_BEETROOTS:
		{
			// The crop must have fully grown up.
			return a_BlockMeta == 0x03;
		}
		case E_BLOCK_CROPS:
		case E_BLOCK_POTATOES:
		case E_BLOCK_CARROTS:
		{
			// The crop must have fully grown up.
			return a_BlockMeta == 0x07;
		}
		default: return false;
	}
}





bool cVillager::IsHarvestable(Vector3i a_CropsPos)
{
	return IsBlockFarmable(m_World->GetBlock(a_CropsPos), m_World->GetBlockMeta(a_CropsPos));
}





bool cVillager::IsPlantable(Vector3i a_CropsPos)
{
	return (m_World->GetBlock(a_CropsPos.addedY(-1)) == E_BLOCK_FARMLAND) && (m_World->GetBlock(a_CropsPos) == E_BLOCK_AIR);
}





cVillager::eVillagerType cVillager::GetRandomProfession()
{
	int Profession = GetRandomProvider().RandInt(cVillager::eVillagerType::vtMax - 1);

	return static_cast<cVillager::eVillagerType>(Profession);
}