aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/TemptGoal.cpp
blob: 40b3201caac6d90cf19cd1744641b19cf2ee3682 (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
#include "stdafx.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.level.h"
#include "TemptGoal.h"

TemptGoal::TemptGoal(PathfinderMob *mob, double speedModifier, int itemId, bool canScare)
{
	px = py = pz = pRotX = pRotY = 0.0;
	player = weak_ptr<Player>();
	calmDown = 0;
	_isRunning = false;
	oldAvoidWater = false;

	this->mob = mob;
	this->speedModifier = speedModifier;
	this->itemId = itemId;
	this->canScare = canScare;
	setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
}

bool TemptGoal::canUse()
{
	if (calmDown > 0)
	{
		--calmDown;
		return false;
	}
	player = weak_ptr<Player>(mob->level->getNearestPlayer(mob->shared_from_this(), 10));
	if (player.lock() == nullptr) return false;
	mob->setDespawnProtected();		// If we've got a nearby player, then consider this mob as something we'd miss if it despawned
	shared_ptr<ItemInstance> item = player.lock()->getSelectedItem();
	if (item == nullptr) return false;
	if (item->id != itemId) return false;
	return true;
}

bool TemptGoal::canContinueToUse()
{
	if (canScare)
	{
		if(player.lock() == nullptr) return false;
		if (mob->distanceToSqr(player.lock()) < 6 * 6)
		{
			if (player.lock()->distanceToSqr(px, py, pz) > 0.1 * 0.1) return false;
			if (abs(player.lock()->xRot - pRotX) > 5 || abs(player.lock()->yRot - pRotY) > 5) return false;
		}
		else
		{
			px = player.lock()->x;
			py = player.lock()->y;
			pz = player.lock()->z;
		}
		pRotX = player.lock()->xRot;
		pRotY = player.lock()->yRot;
	}
	return canUse();
}

void TemptGoal::start()
{
	px = player.lock()->x;
	py = player.lock()->y;
	pz = player.lock()->z;
	_isRunning = true;
	oldAvoidWater = mob->getNavigation()->getAvoidWater();
	mob->getNavigation()->setAvoidWater(false);
}

void TemptGoal::stop()
{
	player = weak_ptr<Player>();
	mob->getNavigation()->stop();
	calmDown = 100;
	_isRunning = false;
	mob->getNavigation()->setAvoidWater(oldAvoidWater);
}

void TemptGoal::tick()
{
	mob->getLookControl()->setLookAt(player.lock(), 30, mob->getMaxHeadXRot());
	if (mob->distanceToSqr(player.lock()) < 2.5 * 2.5) mob->getNavigation()->stop();
	else mob->getNavigation()->moveTo(player.lock(), speedModifier);
}

bool TemptGoal::isRunning()
{
	return _isRunning;
}