aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/BreedGoal.cpp
blob: 10aa6844b8848f906173b338e312941b11b95a87 (plain)
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
#include "stdafx.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.phys.h"
#include "BasicTypeContainers.h"
#include "BreedGoal.h"
#include "ExperienceOrb.h"

#include "GenericStats.h"

BreedGoal::BreedGoal(Animal *animal, double speedModifier)
{
	partner = weak_ptr<Animal>();
	loveTime = 0;

	this->animal = animal;
	this->level = animal->level;
	this->speedModifier = speedModifier;
	setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
}

bool BreedGoal::canUse()
{
	if (!animal->isInLove()) return false;
	partner = weak_ptr<Animal>(getFreePartner());
	return partner.lock() != nullptr;
}

bool BreedGoal::canContinueToUse()
{
	return partner.lock() != nullptr && partner.lock()->isAlive() && partner.lock()->isInLove() && loveTime < 20 * 3;
}

void BreedGoal::stop()
{
	partner = weak_ptr<Animal>();
	loveTime = 0;
}

void BreedGoal::tick()
{
	animal->getLookControl()->setLookAt(partner.lock(), 10, animal->getMaxHeadXRot());
	animal->getNavigation()->moveTo(partner.lock(), speedModifier);
	++loveTime;
	if (loveTime >= 20 * 3 && animal->distanceToSqr(partner.lock()) < 3*3) breed();
}

shared_ptr<Animal> BreedGoal::getFreePartner()
{
	float r = 8;
	vector<shared_ptr<Entity> > *others = level->getEntitiesOfClass(typeid(*animal), animal->bb->grow(r, r, r));
	double dist = Double::MAX_VALUE;
	shared_ptr<Animal> partner = nullptr;
	if ( others )
	{
		for ( auto& it : *others )
		{
			shared_ptr<Animal> p = dynamic_pointer_cast<Animal>(it);
			if ( p && animal->canMate(p) && animal->distanceToSqr(p) < dist)
			{
				partner = p;
				dist = animal->distanceToSqr(p);
			}
		}
		delete others;
	}
	return partner;
}

void BreedGoal::breed()
{
	shared_ptr<AgableMob> offspring = animal->getBreedOffspring(partner.lock());
	animal->setDespawnProtected();
	partner.lock()->setDespawnProtected();
	if (offspring == nullptr)
	{
		// This will be nullptr if we've hit our limits for spawning any particular type of animal... reset things as normally as we can, without actually producing any offspring
		animal->resetLove();
		partner.lock()->resetLove();
		return;
	}

	shared_ptr<Player> loveCause = animal->getLoveCause();
	if (loveCause == nullptr && partner.lock()->getLoveCause() != nullptr)
	{
		loveCause = partner.lock()->getLoveCause();
	}

	if (loveCause != nullptr)
	{
		// Record mob bred stat.
		loveCause->awardStat(GenericStats::breedEntity(offspring->GetType()),GenericStats::param_breedEntity(offspring->GetType()));

		if (animal->GetType() == eTYPE_COW)
		{
			//loveCause->awardStat(Achievements.breedCow);
		}
	}

	animal->setAge(5 * 60 * 20);
	partner.lock()->setAge(5 * 60 * 20);
	animal->resetLove();
	partner.lock()->resetLove();
	offspring->setAge(AgableMob::BABY_START_AGE);
	offspring->moveTo(animal->x, animal->y, animal->z, 0, 0);
	offspring->setDespawnProtected();
	level->addEntity(offspring);

	Random *random = animal->getRandom();
	for (int i = 0; i < 7; i++)
	{
		double xa = random->nextGaussian() * 0.02;
		double ya = random->nextGaussian() * 0.02;
		double za = random->nextGaussian() * 0.02;
		level->addParticle(eParticleType_heart, animal->x + random->nextFloat() * animal->bbWidth * 2 - animal->bbWidth, animal->y + .5f + random->nextFloat() * animal->bbHeight, animal->z + random->nextFloat()
			* animal->bbWidth * 2 - animal->bbWidth, xa, ya, za);
	}
	// 4J-PB - Fix for 106869- Customer Encountered: TU12: Content: Gameplay: Breeding animals does not give any Experience Orbs.
	level->addEntity(std::make_shared<ExperienceOrb>(level, animal->x, animal->y, animal->z, random->nextInt(7) + 1));
}