aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/PathfinderMob.cpp
blob: 840a6020832a42b3034f38eb6595259cfe8e3033 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "stdafx.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.entity.ai.attributes.h"
#include "net.minecraft.world.entity.ai.goal.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.monster.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.pathfinder.h"
#include "net.minecraft.world.phys.h"
#include "SharedConstants.h"
#include "PathfinderMob.h"

AttributeModifier *PathfinderMob::SPEED_MODIFIER_FLEEING = (new AttributeModifier(eModifierId_MOB_FLEEING, 2.0f, AttributeModifier::OPERATION_MULTIPLY_TOTAL))->setSerialize(false);

PathfinderMob::PathfinderMob(Level *level) : Mob( level )
{
	path = nullptr;
	attackTarget = nullptr;
	holdGround = false;
	fleeTime = 0;

	restrictRadius = -1;
	restrictCenter = new Pos(0, 0, 0);
	addedLeashRestrictionGoal = false;
	leashRestrictionGoal = new MoveTowardsRestrictionGoal(this, 1.0f);
}

bool PathfinderMob::shouldHoldGround()
{
	return false;
}

PathfinderMob::~PathfinderMob()
{
	delete path;
	delete restrictCenter;
	delete leashRestrictionGoal;
}

void PathfinderMob::serverAiStep()
{
	if (fleeTime > 0)
	{
		if (--fleeTime == 0)
		{
			AttributeInstance *speed = getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED);
			speed->removeModifier(SPEED_MODIFIER_FLEEING);
		}
	}
	holdGround = shouldHoldGround();
	float maxDist = 16;

	if (attackTarget == nullptr)
	{
		attackTarget = findAttackTarget();
		if (attackTarget != nullptr)
		{
			setPath(level->findPath(shared_from_this(), attackTarget, maxDist, true, false, false, true)); // 4J - changed to setPath from path =
		}
	}
	else
	{
		if (attackTarget->isAlive())
		{
			float d = attackTarget->distanceTo(shared_from_this());
			if (canSee(attackTarget))
			{
				checkHurtTarget(attackTarget, d);
			}
		}
		else
		{
			attackTarget = nullptr;
		}
	}

	/*
	* if (holdGround) { xxa = 0; yya = 0; jumping = false; return; }
	*/

	// 4J - a few changes here so that we can call findRandomStrollLocation for a sub-set of things that it normally wouldn't be in the java game.
	// This is so that we can have entities wander around a little, in order that we can measure how far they wander and then determine (if they wander too far) that
	// they aren't enclosed. We don't want the extra network overhead of just having Everything wandering round all the time, so have put a management system in place
	// that selects a subset of entities which have had their flag set through the considerForExtraWandering method so that these can keep doing random strolling.

	if (!holdGround && (attackTarget != nullptr && (path == nullptr || random->nextInt(20) == 0)))
	{
		setPath(level->findPath(shared_from_this(), attackTarget, maxDist, true, false, false, true));// 4J - changed to setPath from path =
	}
	else if (!holdGround && ((path == nullptr && (random->nextInt(180) == 0) || fleeTime > 0) || (random->nextInt(120) == 0 || fleeTime > 0)))
	{
		if(noActionTime < SharedConstants::TICKS_PER_SECOND * 5) 
		{
			findRandomStrollLocation();
		}
	}
	else if (!holdGround && (path == nullptr ) )
	{
		if( ( noActionTime >= SharedConstants::TICKS_PER_SECOND * 5 ) && isExtraWanderingEnabled() )
		{
			// This entity wouldn't normally be randomly strolling. However, if our management system says that it should do, then do. Don't
			// bother waiting for random conditions to be met before picking a direction though as the point here is to see if it is possible to
			// stroll out of a given area and so waiting around is just wasting time
			findRandomStrollLocation(getWanderingQuadrant());
		}
	}

	// Consider this for extra strolling if it is protected against despawning. We aren't interested in ones that aren't protected as the whole point of this
	// extra wandering is to potentially transition from protected to not protected.
	considerForExtraWandering( isDespawnProtected() );

	int yFloor = Mth::floor(bb->y0 + 0.5f);

	bool inWater = isInWater();
	bool inLava = isInLava();
	xRot = 0;
	if (path == nullptr || random->nextInt(100) == 0)
	{
		this->Mob::serverAiStep();
		setPath(nullptr);// 4J - changed to setPath from path =
		return;
	}

	Vec3 *target = path->currentPos(shared_from_this());
	double r = bbWidth * 2;
	while (target != nullptr && target->distanceToSqr(x, target->y, z) < r * r)
	{
		path->next();
		if (path->isDone())
		{
			target = nullptr;
			setPath(nullptr); // 4J - changed to setPath from path =
		}
		else target = path->currentPos(shared_from_this());
	}

	jumping = false;
	if (target != nullptr)
	{
		double xd = target->x - x;
		double zd = target->z - z;
		double yd = target->y - yFloor;
		float yRotD = static_cast<float>(atan2(zd, xd) * 180 / PI) - 90;
		float rotDiff = Mth::wrapDegrees(yRotD - yRot);
		yya = static_cast<float>(getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED)->getValue());
		if (rotDiff > MAX_TURN)
		{
			rotDiff = MAX_TURN;
		}
		if (rotDiff < -MAX_TURN)
		{
			rotDiff = -MAX_TURN;
		}
		yRot += rotDiff;

		if (holdGround)
		{
			if (attackTarget != nullptr)
			{
				double xd2 = attackTarget->x - x;
				double zd2 = attackTarget->z - z;

				float oldyRot = yRot;
				yRot = static_cast<float>(atan2(zd2, xd2) * 180 / PI) - 90;

				rotDiff = ((oldyRot - yRot) + 90) * PI / 180;
				xxa = -Mth::sin(rotDiff) * yya * 1.0f;
				yya = Mth::cos(rotDiff) * yya * 1.0f;
			}
		}
		if (yd > 0)
		{
			jumping = true;
		}
	}

	if (attackTarget != nullptr)
	{
		lookAt(attackTarget, 30, 30);
	}

	if (horizontalCollision && !isPathFinding()) jumping = true;
	if (random->nextFloat() < 0.8f && (inWater || inLava)) jumping = true;
}

void PathfinderMob::findRandomStrollLocation(int quadrant/*=-1*/)	// 4J - added quadrant
{
	bool hasBest = false;
	int xBest = -1;
	int yBest = -1;
	int zBest = -1;
	float best = -99999;
	for (int i = 0; i < 10; i++)
	{
		// 4J - added quadrant parameter to this method so that the caller can request that only stroll locations in one quadrant be found. If -1 is passed then
		// behaviour is the same as the java game
		int xt, zt;
		int yt = Mth::floor(y + random->nextInt(7) - 3);
		if( quadrant == -1 )
		{
			xt = Mth::floor(x + random->nextInt(13) - 6);
			zt = Mth::floor(z + random->nextInt(13) - 6);
		}
		else
		{
			int sx = ( ( quadrant & 1 ) ? -1 : 1 );
			int sz = ( ( quadrant & 2 ) ? -1 : 1 );
			xt = Mth::floor(x + random->nextInt(7) * sx);
			zt = Mth::floor(z + random->nextInt(7) * sz);
		}
		float value = getWalkTargetValue(xt, yt, zt);
		if (value > best)
		{
			best = value;
			xBest = xt;
			yBest = yt;
			zBest = zt;
			hasBest = true;
		}
	}
	if (hasBest)
	{
		setPath(level->findPath(shared_from_this(), xBest, yBest, zBest, 10, true, false, false, true)); // 4J - changed to setPath from path =
	}
}

void PathfinderMob::checkHurtTarget(shared_ptr<Entity> target, float d)
{
}

float PathfinderMob::getWalkTargetValue(int x, int y, int z)
{
	return 0;
}

shared_ptr<Entity> PathfinderMob::findAttackTarget()
{
	return shared_ptr<Entity>();
}


bool PathfinderMob::canSpawn()
{
	int xt = Mth::floor(x);
	int yt = Mth::floor(bb->y0);
	int zt = Mth::floor(z);
	return this->Mob::canSpawn() && getWalkTargetValue(xt, yt, zt) >= 0;
}

bool PathfinderMob::isPathFinding()
{
	return path != nullptr;
}

void PathfinderMob::setPath(Path *path)
{
	delete this->path;
	this->path = path;
}

shared_ptr<Entity> PathfinderMob::getAttackTarget()
{
	return attackTarget;
}

void PathfinderMob::setAttackTarget(shared_ptr<Entity> attacker)
{
	attackTarget = attacker;
}

// might move to navigation, might make area
bool PathfinderMob::isWithinRestriction()
{
	return isWithinRestriction(Mth::floor(x), Mth::floor(y), Mth::floor(z));
}

bool PathfinderMob::isWithinRestriction(int x, int y, int z)
{
	if (restrictRadius == -1) return true;
	return restrictCenter->distSqr(x, y, z) < restrictRadius * restrictRadius;
}

void PathfinderMob::restrictTo(int x, int y, int z, int radius)
{
	restrictCenter->set(x, y, z);
	restrictRadius = radius;
}

Pos *PathfinderMob::getRestrictCenter()
{
	return restrictCenter;
}

float PathfinderMob::getRestrictRadius()
{
	return restrictRadius;
}

void PathfinderMob::clearRestriction()
{
	restrictRadius = -1;
}

bool PathfinderMob::hasRestriction()
{
	return restrictRadius != -1;
}

void PathfinderMob::tickLeash()
{
	Mob::tickLeash();

	if (isLeashed() && getLeashHolder() != nullptr && getLeashHolder()->level == this->level)
	{
		// soft restriction
		shared_ptr<Entity> leashHolder = getLeashHolder();
		restrictTo(static_cast<int>(leashHolder->x), static_cast<int>(leashHolder->y), static_cast<int>(leashHolder->z), 5);

		float _distanceTo = distanceTo(leashHolder);

		shared_ptr<TamableAnimal> tamabaleAnimal = shared_from_this()->instanceof(eTYPE_TAMABLE_ANIMAL) ? dynamic_pointer_cast<TamableAnimal>(shared_from_this()) : nullptr;
		if ( (tamabaleAnimal != nullptr) && tamabaleAnimal->isSitting() )
		{
			if (_distanceTo > 10)
			{
				dropLeash(true, true);
			}
			return;
		}

		if (!addedLeashRestrictionGoal)
		{
			goalSelector.addGoal(2, leashRestrictionGoal, false);
			getNavigation()->setAvoidWater(false);
			addedLeashRestrictionGoal = true;
		}

		onLeashDistance(_distanceTo);

		if (_distanceTo > 4)
		{
			// harder restriction
			getNavigation()->moveTo(leashHolder, 1.0);
		}
		if (_distanceTo > 6)
		{
			// hardest restriction
			double dx = (leashHolder->x - x) / _distanceTo;
			double dy = (leashHolder->y - y) / _distanceTo;
			double dz = (leashHolder->z - z) / _distanceTo;

			xd += dx * abs(dx) * .4;
			yd += dy * abs(dy) * .4;
			zd += dz * abs(dz) * .4;
		}
		if (_distanceTo > 10)
		{
			dropLeash(true, true);
		}

	}
	else if (!isLeashed() && addedLeashRestrictionGoal)
	{
		addedLeashRestrictionGoal = false;
		goalSelector.removeGoal(leashRestrictionGoal);
		getNavigation()->setAvoidWater(true);
		clearRestriction();
	}
}

void PathfinderMob::onLeashDistance(float distanceToLeashHolder)
{
}

bool PathfinderMob::couldWander()
{
	return (noActionTime < SharedConstants::TICKS_PER_SECOND * 5) || ( isExtraWanderingEnabled() );
}