aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/PlayGoal.cpp
blob: 72ee69d46adba43bbd701c28ad766e72fb39029a (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
#include "stdafx.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.ai.util.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.npc.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.phys.h"
#include "BasicTypeContainers.h"
#include "PlayGoal.h"

PlayGoal::PlayGoal(Villager *mob, double speedModifier)
{
	followFriend = weak_ptr<LivingEntity>();
	wantedX = wantedY = wantedZ = 0.0;
	playTime = 0;

	this->mob = mob;
	this->speedModifier = speedModifier;
	setRequiredControlFlags(Control::MoveControlFlag);
}

bool PlayGoal::canUse()
{
	if (mob->getAge() >= 0) return false;
	if (mob->getRandom()->nextInt(400) != 0) return false;

	vector<shared_ptr<Entity> > *children = mob->level->getEntitiesOfClass(typeid(Villager), mob->bb->grow(6, 3, 6));
	double closestDistSqr = Double::MAX_VALUE;
	for(auto& c : *children)
	{
		if (c.get() == mob) continue;
		shared_ptr<Villager> friendV = dynamic_pointer_cast<Villager>(c);
		if (friendV->isChasing()) continue;
		if (friendV->getAge() >= 0) continue;
		double distSqr = friendV->distanceToSqr(mob->shared_from_this());
		if (distSqr > closestDistSqr) continue;
		closestDistSqr = distSqr;
		followFriend = weak_ptr<LivingEntity>(friendV);
	}
	delete children;

	if (followFriend.lock() == NULL)
	{
		Vec3 *pos = RandomPos::getPos(dynamic_pointer_cast<PathfinderMob>(mob->shared_from_this()), 16, 3);
		if (pos == NULL) return false;
	}
	return true;
}

bool PlayGoal::canContinueToUse()
{
	return playTime > 0 && followFriend.lock() != NULL;
}

void PlayGoal::start()
{
	if (followFriend.lock() != NULL) mob->setChasing(true);
	playTime = 1000;
}

void PlayGoal::stop()
{
	mob->setChasing(false);
	followFriend = weak_ptr<LivingEntity>();
}

void PlayGoal::tick()
{
	--playTime;
	if (followFriend.lock() != NULL)
	{
		if (mob->distanceToSqr(followFriend.lock()) > 2 * 2) mob->getNavigation()->moveTo(followFriend.lock(), speedModifier);
	}
	else
	{
		if (mob->getNavigation()->isDone())
		{
			Vec3 *pos = RandomPos::getPos(dynamic_pointer_cast<PathfinderMob>(mob->shared_from_this()), 16, 3);
			if (pos == NULL) return;
			mob->getNavigation()->moveTo(pos->x, pos->y, pos->z, speedModifier);
		}
	}
}