aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Animal.cpp
blob: 31de7d759b86224787bc20957ea5009593403380 (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "stdafx.h"

#include "com.mojang.nbt.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.storage.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.projectile.h"
#include "net.minecraft.world.damagesource.h"
#include "net.minecraft.world.entity.monster.h"
#include "net.minecraft.world.entity.ai.attributes.h"
#include "Random.h"
#include "Animal.h"

Animal::Animal(Level *level) : AgableMob( level )
{
//	inLove = 0;										// 4J removed - now synched data
	loveTime = 0;
	loveCause = shared_ptr<Player>();

	setDespawnProtected();
}

void Animal::defineSynchedData()
{
	AgableMob::defineSynchedData();

	entityData->define(DATA_IN_LOVE, (int)0);		// 4J added
}

void Animal::serverAiMobStep()
{
	if (getAge() != 0) setInLoveValue(0);
	AgableMob::serverAiMobStep();
}


void Animal::aiStep()
{
	AgableMob::aiStep();

	if (getAge() != 0) setInLoveValue(0);

	if (getInLoveValue() > 0)
	{
		setInLoveValue(getInLoveValue()-1);
		if (getInLoveValue() % 10 == 0)
		{
			double xa = random->nextGaussian() * 0.02;
			double ya = random->nextGaussian() * 0.02;
			double za = random->nextGaussian() * 0.02;
			level->addParticle(eParticleType_heart, x + random->nextFloat() * bbWidth * 2 - bbWidth, y + .5f + random->nextFloat() * bbHeight, z + random->nextFloat() * bbWidth * 2 - bbWidth, xa, ya, za);
		}
	}
	else
	{
		loveTime = 0;
	}

	updateDespawnProtectedState();		// 4J added
}

void Animal::checkHurtTarget(shared_ptr<Entity> target, float d)
{
	// 4J-JEV: Changed from dynamic cast to use eINSTANCEOF
	if ( target->instanceof(eTYPE_PLAYER) )
	{
		if (d < 3)
		{
			double xd = target->x - x;
			double zd = target->z - z;
			yRot = (float) (atan2(zd, xd) * 180 / PI) - 90;

			holdGround = true;
		}

		shared_ptr<Player> p = dynamic_pointer_cast<Player>(target);
		if (p->getSelectedItem() == NULL || !isFood(p->getSelectedItem()))
		{
			attackTarget = nullptr;
		}

	}
	// 4J-JEV: Changed from dynamic cast to use eINSTANCEOF
	else if ( target->instanceof(eTYPE_ANIMAL) )
	{
		shared_ptr<Animal> a = dynamic_pointer_cast<Animal>(target);
		if (getAge() > 0 && a->getAge() < 0)
		{
			if (d < 2.5)
			{
				holdGround = true;
			}
		}
		else if (getInLoveValue() > 0 && a->getInLoveValue() > 0)
		{
			if (a->attackTarget == NULL) a->attackTarget = shared_from_this();

			if (a->attackTarget == shared_from_this() && d < 3.5)
			{
				a->setInLoveValue(a->getInLoveValue()+1);
				setInLoveValue(getInLoveValue()+1);
				loveTime++;
				if (loveTime % 4 == 0)
				{
					level->addParticle(eParticleType_heart, x + random->nextFloat() * bbWidth * 2 - bbWidth, y + .5f + random->nextFloat() * bbHeight, z + random->nextFloat() * bbWidth * 2 - bbWidth, 0, 0, 0);
				}

				if (loveTime == 20 * 3) breedWith(a);
			}
			else loveTime = 0;
		}
		else
		{
			loveTime = 0;
			attackTarget = nullptr;
		}

	}
}


void Animal::breedWith(shared_ptr<Animal> target)
{
	shared_ptr<AgableMob> offspring = getBreedOffspring(target);

	setInLoveValue(0);
	loveTime = 0;
	attackTarget = nullptr;
	target->attackTarget = nullptr;
	target->loveTime = 0;
	target->setInLoveValue(0);

	// 4J - we have offspring of NULL returned when we have hit our limits of spawning any particular type of animal. In these cases try and do everything we can apart from actually
	// spawning the entity.
	if (offspring != NULL)
	{
		// Only want to set the age to this +ve value if something is actually spawned, as during this period the animal will attempt to follow offspring and ignore players.
		setAge(5 * 60 * 20);
		target->setAge(5 * 60 * 20);

		offspring->setAge(-20 * 60 * 20);
		offspring->moveTo(x, y, z, yRot, xRot);
		offspring->setDespawnProtected();
		for (int i = 0; i < 7; i++)
		{
			double xa = random->nextGaussian() * 0.02;
			double ya = random->nextGaussian() * 0.02;
			double za = random->nextGaussian() * 0.02;
			level->addParticle(eParticleType_heart, x + random->nextFloat() * bbWidth * 2 - bbWidth, y + .5f + random->nextFloat() * bbHeight, z + random->nextFloat() * bbWidth * 2 - bbWidth, xa, ya, za);
		}
		level->addEntity(offspring);

		level->addEntity( shared_ptr<ExperienceOrb>( new ExperienceOrb(level, x, y, z, random->nextInt(4) + 1) ) );
	}

	setDespawnProtected();
}

float Animal::getWalkTargetValue(int x, int y, int z)
{
	if (level->getTile(x, y - 1, z) == Tile::grass_Id) return 10;
	return level->getBrightness(x, y, z) - 0.5f;
}

bool Animal::hurt(DamageSource *dmgSource, float dmg)
{
	if (isInvulnerable()) return false;
	if (dynamic_cast<EntityDamageSource *>(dmgSource) != NULL)
	{
		shared_ptr<Entity> source = dmgSource->getDirectEntity();

		// 4J-JEV: Changed from dynamic cast to use eINSTANCEOF
		if ( source->instanceof(eTYPE_PLAYER) && !dynamic_pointer_cast<Player>(source)->isAllowedToAttackAnimals() )
		{
			return false;
		}

		if ( (source != NULL) && source->instanceof(eTYPE_ARROW) )
		{
			shared_ptr<Arrow> arrow = dynamic_pointer_cast<Arrow>(source);

			// 4J: Check that the arrow's owner can attack animals (dispenser arrows are not owned)
			if (arrow->owner != NULL && arrow->owner->instanceof(eTYPE_PLAYER) && !dynamic_pointer_cast<Player>(arrow->owner)->isAllowedToAttackAnimals() )
			{
				return false;
			}
		}
	}

	fleeTime = 20 * 3;

	if (!useNewAi())
	{
		AttributeInstance *speed = getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED);
		if (speed->getModifier(eModifierId_MOB_FLEEING) == NULL)
		{
			speed->addModifier(new AttributeModifier(*Animal::SPEED_MODIFIER_FLEEING));
		}
	}

	attackTarget = nullptr;
	setInLoveValue(0);

	return AgableMob::hurt(dmgSource, dmg);
}

void Animal::addAdditonalSaveData(CompoundTag *tag)
{
	AgableMob::addAdditonalSaveData(tag);
	tag->putInt(L"InLove", getInLoveValue());
}

void Animal::readAdditionalSaveData(CompoundTag *tag)
{
	AgableMob::readAdditionalSaveData(tag);
	setInLoveValue(tag->getInt(L"InLove"));
	setDespawnProtected();
}

shared_ptr<Entity> Animal::findAttackTarget()
{
	if (fleeTime > 0) return nullptr;

	float r = 8;
	if (getInLoveValue() > 0)
	{
		vector<shared_ptr<Entity> > *others = level->getEntitiesOfClass(typeid(*this), bb->grow(r, r, r));
		//for (int i = 0; i < others->size(); i++)
		for(AUTO_VAR(it, others->begin()); it != others->end(); ++it)
		{
			shared_ptr<Animal> p = dynamic_pointer_cast<Animal>(*it);
			if (p != shared_from_this() && p->getInLoveValue() > 0)
			{
				delete others;
				return p;
			}
		}
		delete others;
	}
	else
	{
		if (getAge() == 0)
		{
			vector<shared_ptr<Entity> > *players = level->getEntitiesOfClass(typeid(Player), bb->grow(r, r, r));
			//for (int i = 0; i < players.size(); i++)
			for(AUTO_VAR(it, players->begin()); it != players->end(); ++it)
			{
				setDespawnProtected();

				shared_ptr<Player> p = dynamic_pointer_cast<Player>(*it);
				if (p->getSelectedItem() != NULL && this->isFood(p->getSelectedItem()))
				{
					delete players;
					return p;
				}
			}
			delete players;
		}
		else if (getAge() > 0)
		{
			vector<shared_ptr<Entity> > *others = level->getEntitiesOfClass(typeid(*this), bb->grow(r, r, r));
			//for (int i = 0; i < others.size(); i++)			
			for(AUTO_VAR(it, others->begin()); it != others->end(); ++it)
			{
				shared_ptr<Animal> p = dynamic_pointer_cast<Animal>(*it);
				if (p != shared_from_this() && p->getAge() < 0)
				{
					delete others;
					return p;
				}
			}
			delete others;
		}
	}
	return nullptr;
}

bool Animal::canSpawn()
{
	int xt = Mth::floor(x);
	int yt = Mth::floor(bb->y0);
	int zt = Mth::floor(z);
	return level->getTile(xt, yt - 1, zt) == Tile::grass_Id && level->getDaytimeRawBrightness(xt, yt, zt) > 8 && AgableMob::canSpawn();
}

int Animal::getAmbientSoundInterval()
{
	return 20 * 6;
}

bool Animal::removeWhenFarAway()
{
	return !isDespawnProtected();	// 4J changed - was false
}

int Animal::getExperienceReward(shared_ptr<Player> killedBy)
{
	return 1 + level->random->nextInt(3);
}

bool Animal::isFood(shared_ptr<ItemInstance> itemInstance)
{
	return itemInstance->id == Item::wheat_Id;
}

bool Animal::mobInteract(shared_ptr<Player> player)
{
	shared_ptr<ItemInstance> item = player->inventory->getSelected();
	if (item != NULL && isFood(item) && getAge() == 0 && getInLoveValue() <= 0)
	{
		if (!player->abilities.instabuild)
		{
			item->count--;
			if (item->count <= 0)
			{
				player->inventory->setItem(player->inventory->selected, nullptr);
			}
		}
		

		// 4J-PB - If we can't produce another animal through breeding because of the spawn limits, display a message here
		if(!level->isClientSide)
		{
			switch(GetType())
			{
			case eTYPE_CHICKEN:
				if( !level->canCreateMore(eTYPE_CHICKEN, Level::eSpawnType_Breed) )
				{
					player->displayClientMessage(IDS_MAX_CHICKENS_BRED );
					return false;
				}					
				break;
			case eTYPE_WOLF:
				if( !level->canCreateMore(eTYPE_WOLF, Level::eSpawnType_Breed) )
				{
					player->displayClientMessage(IDS_MAX_WOLVES_BRED );
					return false;
				}					
				break;
			case eTYPE_MUSHROOMCOW:
				if( !level->canCreateMore(eTYPE_MUSHROOMCOW, Level::eSpawnType_Breed) )
				{
					player->displayClientMessage(IDS_MAX_MUSHROOMCOWS_BRED );
					return false;
				}					
				break;
			default:
				if((GetType() & eTYPE_ANIMALS_SPAWN_LIMIT_CHECK) == eTYPE_ANIMALS_SPAWN_LIMIT_CHECK)
				{
					if( !level->canCreateMore(GetType(), Level::eSpawnType_Breed) )
					{
						player->displayClientMessage(IDS_MAX_PIGS_SHEEP_COWS_CATS_BRED );

						return false;
					}
				}
				else if( instanceof(eTYPE_MONSTER) )
				{

				}
				break;
			}
			setInLove(player);
		}
		setInLove();

		return true;
	}
	return AgableMob::mobInteract(player);
}

// 4J added
int Animal::getInLoveValue()
{
	return entityData->getInteger(DATA_IN_LOVE);
}

void Animal::setInLoveValue(int value)
{
	entityData->set(DATA_IN_LOVE, value);
}

// 4J added
void Animal::setInLove(shared_ptr<Player> player)
{
	loveCause = player;
	setInLoveValue(20*30);
}

shared_ptr<Player> Animal::getLoveCause()
{
	return loveCause.lock();
}

void Animal::setInLove()
{
	entityData->set(DATA_IN_LOVE, 20 * 30);

	attackTarget = nullptr;
	level->broadcastEntityEvent(shared_from_this(), EntityEvent::IN_LOVE_HEARTS);
}

bool Animal::isInLove()
{
	return entityData->getInteger(DATA_IN_LOVE) > 0;
}

void Animal::resetLove() {
	entityData->set(DATA_IN_LOVE, 0);
}

bool Animal::canMate(shared_ptr<Animal> partner)
{
	if (partner == shared_from_this()) return false;
	if (typeid(*partner) != typeid(*this)) return false;
	return isInLove() && partner->isInLove();
}

void Animal::handleEntityEvent(byte id)
{
	if (id == EntityEvent::IN_LOVE_HEARTS)
	{
		for (int i = 0; i < 7; i++)
		{
			double xa = random->nextGaussian() * 0.02;
			double ya = random->nextGaussian() * 0.02;
			double za = random->nextGaussian() * 0.02;
			level->addParticle(eParticleType_heart, x + random->nextFloat() * bbWidth * 2 - bbWidth, y + .5f + random->nextFloat() * bbHeight, z + random->nextFloat() * bbWidth * 2 - bbWidth, xa, ya, za);
		}
	}
	else
	{
		AgableMob::handleEntityEvent(id);
	}
}

void Animal::updateDespawnProtectedState()
{
	if( level->isClientSide ) return;

	if( m_isDespawnProtected )
	{
		int xt = Mth::floor(x);
		int zt = Mth::floor(z);

		if ( xt > m_maxWanderX ) m_maxWanderX = xt;
		if ( xt < m_minWanderX ) m_minWanderX = xt;
		if ( zt > m_maxWanderZ ) m_maxWanderZ = zt;
		if ( zt < m_minWanderZ ) m_minWanderZ = zt;

		if( ( ( m_maxWanderX - m_minWanderX ) > MAX_WANDER_DISTANCE ) ||
			( ( m_maxWanderZ - m_minWanderZ ) > MAX_WANDER_DISTANCE ) )
		{
//			printf("Unprotecting : %d to %d, %d to %d\n", m_minWanderX, m_maxWanderX, m_minWanderZ, m_maxWanderZ );
			m_isDespawnProtected = false;
		}

/*
		if( isExtraWanderingEnabled() )
		{
			printf("%d: %d %d, %d\n",entityId,m_maxWanderX - m_minWanderX, m_maxWanderZ - m_minWanderZ, getWanderingQuadrant());
		}
		*/
	}
}

bool Animal::isDespawnProtected()
{
	return m_isDespawnProtected;
}

void Animal::setDespawnProtected()
{
	if( level && level->isClientSide ) return;

	int xt = Mth::floor(x);
	int zt = Mth::floor(z);

	m_minWanderX = xt;
	m_maxWanderX = xt;
	m_minWanderZ = zt;
	m_maxWanderZ = zt;

	m_isDespawnProtected = true;
}