summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Behaviors/BehaviorWanderer.cpp
blob: 56316bd9f55d3bacbf3a041d7d0b79700c602d74 (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
#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules
#include "BehaviorWanderer.h"
#include "../Monster.h"
#include "../../Chunk.h"
#include "../../World.h"

cBehaviorWanderer::cBehaviorWanderer() : m_IdleInterval(0)
{

}





void cBehaviorWanderer::AttachToMonster(cMonster & a_Parent)
{
	LOGD("mobDebug - Behavior Wanderer: Attach");
	m_Parent = &a_Parent;
	m_Parent->AttachTickBehavior(this);
}





bool cBehaviorWanderer::IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	// Wandering behavior always happily accepts control.
	// It should therefore be the last one attached to a monster.
	UNUSED(a_Dt);
	UNUSED(a_Chunk);
	return true;
}



bool cBehaviorWanderer::ControlStarting(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	UNUSED(a_Dt);
	UNUSED(a_Chunk);
	m_Parent->GetPathFinder().SetDontCare(true); // We don't care we're we are going when
	// wandering. If a path is not found, the pathfinder just modifies our destination.
	return true;
}


bool cBehaviorWanderer::ControlEnding(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	UNUSED(a_Dt);
	UNUSED(a_Chunk);
	m_Parent->GetPathFinder().SetDontCare(false);
	return true;
}


void cBehaviorWanderer::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	if (m_Parent->IsPathFinderActivated())
	{
		return;  // Still getting there
	}

	m_IdleInterval += a_Dt;

	if (m_IdleInterval > std::chrono::seconds(1))
	{
		// At this interval the results are predictable
		int rem = m_Parent->GetWorld()->GetTickRandomNumber(6) + 1;
		m_IdleInterval -= std::chrono::seconds(1);  // So nothing gets dropped when the server hangs for a few seconds

		Vector3d Dist;
		Dist.x = static_cast<double>(m_Parent->GetWorld()->GetTickRandomNumber(10)) - 5.0;
		Dist.z = static_cast<double>(m_Parent->GetWorld()->GetTickRandomNumber(10)) - 5.0;

		if ((Dist.SqrLength() > 2)  && (rem >= 3))
		{

			Vector3d Destination(m_Parent->GetPosX() + Dist.x, m_Parent->GetPosition().y, m_Parent->GetPosZ() + Dist.z);

			cChunk * Chunk = a_Chunk.GetNeighborChunk(static_cast<int>(Destination.x), static_cast<int>(Destination.z));
			if ((Chunk == nullptr) || !Chunk->IsValid())
			{
				return;
			}

			BLOCKTYPE BlockType;
			NIBBLETYPE BlockMeta;
			int RelX = static_cast<int>(Destination.x) - Chunk->GetPosX() * cChunkDef::Width;
			int RelZ = static_cast<int>(Destination.z) - Chunk->GetPosZ() * cChunkDef::Width;
			int YBelowUs = static_cast<int>(Destination.y) - 1;
			if (YBelowUs >= 0)
			{
				Chunk->GetBlockTypeMeta(RelX, YBelowUs, RelZ, BlockType, BlockMeta);
				if (BlockType != E_BLOCK_STATIONARY_WATER)  // Idle mobs shouldn't enter water on purpose
				{
					m_Parent->MoveToPosition(Destination);
				}
			}
		}
	}
}