aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/FollowParentGoal.cpp
blob: d8f3bad9e63bc3c64d13b72adfde3aac5ed797be (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
#include "stdafx.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.phys.h"
#include "BasicTypeContainers.h"
#include "FollowParentGoal.h"

FollowParentGoal::FollowParentGoal(Animal *animal, double speedModifier)
{
	timeToRecalcPath = 0;

	this->animal = animal;
	this->speedModifier = speedModifier;
}

bool FollowParentGoal::canUse()
{
	if (animal->getAge() >= 0) return false;

	vector<shared_ptr<Entity> > *parents = animal->level->getEntitiesOfClass(typeid(*animal), animal->bb->grow(8, 4, 8));

	shared_ptr<Animal> closest = nullptr;
	double closestDistSqr = Double::MAX_VALUE;
	for(auto& it : *parents)
	{
		shared_ptr<Animal> parent = dynamic_pointer_cast<Animal>(it);
		if (parent->getAge() < 0) continue;
		double distSqr = animal->distanceToSqr(parent);
		if (distSqr > closestDistSqr) continue;
		closestDistSqr = distSqr;
		closest = parent;
	}
	delete parents;

	if (closest == nullptr) return false;
	if (closestDistSqr < 3 * 3) return false;
	parent = weak_ptr<Animal>(closest);
	return true;
}

bool FollowParentGoal::canContinueToUse()
{
	if (parent.lock() == nullptr || !parent.lock()->isAlive()) return false;
	double distSqr = animal->distanceToSqr(parent.lock());
	if (distSqr < 3 * 3 || distSqr > 16 * 16) return false;
	return true;
}

void FollowParentGoal::start()
{
	timeToRecalcPath = 0;
}

void FollowParentGoal::stop()
{
	parent = weak_ptr<Animal>();
}

void FollowParentGoal::tick()
{
	if (--timeToRecalcPath > 0) return;
	timeToRecalcPath = 10;
	animal->getNavigation()->moveTo(parent.lock(), speedModifier);
}