aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Skeleton.cpp
blob: cc325b979d90f37208018b0fc61746fed9a3f86b (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
#include "stdafx.h"
#include "net.minecraft.world.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.dimension.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.item.enchantment.h"
#include "net.minecraft.world.effect.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.ai.attributes.h"
#include "net.minecraft.world.entity.ai.goal.h"
#include "net.minecraft.world.entity.ai.goal.target.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.projectile.h"
#include "net.minecraft.world.entity.item.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.entity.monster.h"
#include "net.minecraft.stats.h"
#include "net.minecraft.world.damagesource.h"
#include "SharedConstants.h"
#include "Skeleton.h"
#include "..\Minecraft.Client\Textures.h"
#include "SoundTypes.h"

Skeleton::Skeleton(Level *level) : Monster( level )
{
	// 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
	// the derived version of the function is called
	this->defineSynchedData();
	registerAttributes();
	setHealth(getMaxHealth());

	bowGoal = new RangedAttackGoal(this, this, 1.0, SharedConstants::TICKS_PER_SECOND * 1, SharedConstants::TICKS_PER_SECOND * 3, 15);
	meleeGoal = new MeleeAttackGoal(this, eTYPE_PLAYER, 1.2, false);

	goalSelector.addGoal(1, new FloatGoal(this));
	goalSelector.addGoal(2, new RestrictSunGoal(this));
	goalSelector.addGoal(3, new FleeSunGoal(this, 1.0));
	goalSelector.addGoal(5, new RandomStrollGoal(this, 1.0));
	goalSelector.addGoal(6, new LookAtPlayerGoal(this, typeid(Player), 8));
	goalSelector.addGoal(6, new RandomLookAroundGoal(this));

	targetSelector.addGoal(1, new HurtByTargetGoal(this, false));
	targetSelector.addGoal(2, new NearestAttackableTargetGoal(this, typeid(Player), 0, true));

	if (level != nullptr && !level->isClientSide) reassessWeaponGoal();
}

Skeleton::~Skeleton()
{
	delete bowGoal;
	delete meleeGoal;
}

void Skeleton::registerAttributes()
{
	Monster::registerAttributes();

	getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED)->setBaseValue(0.25f);
}

void Skeleton::defineSynchedData()
{
	Monster::defineSynchedData();

	entityData->define(DATA_TYPE_ID, static_cast<byte>(TYPE_DEFAULT));
}

bool Skeleton::useNewAi()
{
	return true;
}

int Skeleton::getAmbientSound() 
{
	return eSoundType_MOB_SKELETON_AMBIENT;
}

int Skeleton::getHurtSound()
{
	return eSoundType_MOB_SKELETON_HURT;
}

int Skeleton::getDeathSound() 
{
	return eSoundType_MOB_SKELETON_DEATH;
}

void Skeleton::playStepSound(int xt, int yt, int zt, int t)
{
	playSound(eSoundType_MOB_SKELETON_STEP, 0.15f, 1);
}

bool Skeleton::doHurtTarget(shared_ptr<Entity> target)
{
	if (Monster::doHurtTarget(target))
	{
		if ( (getSkeletonType() == TYPE_WITHER) && target->instanceof(eTYPE_LIVINGENTITY) )
		{
			dynamic_pointer_cast<LivingEntity>(target)->addEffect(new MobEffectInstance(MobEffect::wither->id, SharedConstants::TICKS_PER_SECOND * 10));
		}
		return true;
	}
	return false;
}

MobType Skeleton::getMobType() 
{
	return UNDEAD;
}

void Skeleton::aiStep()
{
	if (level->isDay() && !level->isClientSide)
	{
		float br = getBrightness(1);
		if (br > 0.5f && random->nextFloat() * 30 < (br - 0.4f) * 2 && level->canSeeSky(Mth::floor(x), static_cast<int>(floor(y + 0.5)), Mth::floor(z)))
		{
			bool burn = true;

			shared_ptr<ItemInstance> helmet = getCarried(SLOT_HELM);
			if (helmet != nullptr)
			{
				if (helmet->isDamageableItem())
				{
					helmet->setAuxValue(helmet->getDamageValue() + random->nextInt(2));
					if (helmet->getDamageValue() >= helmet->getMaxDamage())
					{
						breakItem(helmet);
						setEquippedSlot(SLOT_HELM, nullptr);
					}
				}

				burn = false;
			}

			if (burn)
			{
				setOnFire(8);
			}
		}
	}
	if (level->isClientSide)
	{
		if (getSkeletonType() == TYPE_WITHER)
		{
			setSize(0.6f * 1.2f, 1.8f * 1.3f);
		}
	}

	Monster::aiStep();
}

void Skeleton::rideTick()
{
	Monster::rideTick();

	if ( riding != nullptr && riding->instanceof(eTYPE_PATHFINDER_MOB) )
	{
		yBodyRot = dynamic_pointer_cast<PathfinderMob>(riding)->yBodyRot;
	}

}

void Skeleton::die(DamageSource *source)
{
	Monster::die(source);
	
	if ( source->getDirectEntity() != nullptr && source->getDirectEntity()->instanceof(eTYPE_ARROW) && source->getEntity() != nullptr && source->getEntity()->instanceof(eTYPE_PLAYER) )
	{
		shared_ptr<Player> player = dynamic_pointer_cast<Player>( source->getEntity() );

		double xd = player->x - x;
		double zd = player->z - z;
		if (xd * xd + zd * zd >= 50 * 50)
		{
			player->awardStat(GenericStats::snipeSkeleton(), GenericStats::param_snipeSkeleton());
		}
	}
}

int Skeleton::getDeathLoot() 
{
	return Item::arrow->id;
}

void Skeleton::dropDeathLoot(bool wasKilledByPlayer, int playerBonusLevel)
{
	if (getSkeletonType() == TYPE_WITHER)
	{
		// drop some arrows
		int count = random->nextInt(3 + playerBonusLevel) - 1;
		for (int i = 0; i < count; i++)
		{
			spawnAtLocation(Item::coal_Id, 1);
		}
	}
	else
	{
		// drop some arrows
		int count = random->nextInt(3 + playerBonusLevel);
		for (int i = 0; i < count; i++)
		{
			spawnAtLocation(Item::arrow_Id, 1);
		}
	}

	// and some bones
	int count = random->nextInt(3 + playerBonusLevel);
	for (int i = 0; i < count; i++)
	{
		spawnAtLocation(Item::bone->id, 1);
	}
}

void Skeleton::dropRareDeathLoot(int rareLootLevel)
{
	if (getSkeletonType() == TYPE_WITHER)
	{
		spawnAtLocation(std::make_shared<ItemInstance>(Item::skull_Id, 1, SkullTileEntity::TYPE_WITHER), 0);
	}
}

void Skeleton::populateDefaultEquipmentSlots()
{
	Monster::populateDefaultEquipmentSlots();

	setEquippedSlot(SLOT_WEAPON, std::make_shared<ItemInstance>(Item::bow));
}

MobGroupData *Skeleton::finalizeMobSpawn(MobGroupData *groupData, int extraData /*= 0*/) // 4J Added extraData param
{
	groupData = Monster::finalizeMobSpawn(groupData);

	if ( dynamic_cast<HellDimension *>(level->dimension) != nullptr && getRandom()->nextInt(5) > 0)
	{
		goalSelector.addGoal(4, meleeGoal, false);

		setSkeletonType(TYPE_WITHER);
		setEquippedSlot(SLOT_WEAPON, std::make_shared<ItemInstance>(Item::sword_stone));
		getAttribute(SharedMonsterAttributes::ATTACK_DAMAGE)->setBaseValue(4);
	}
	else
	{
		goalSelector.addGoal(4, bowGoal, false);

		populateDefaultEquipmentSlots();
		populateDefaultEquipmentEnchantments();
	}

	setCanPickUpLoot(random->nextFloat() < MAX_PICKUP_LOOT_CHANCE * level->getDifficulty(x, y, z));

	if (getCarried(SLOT_HELM) == nullptr)
	{
		if (Calendar::GetMonth() + 1 == 10 && Calendar::GetDayOfMonth() == 31 && random->nextFloat() < 0.25f)
		{
			// Halloween! OooOOo! 25% of all skeletons/zombies can wear pumpkins on their heads.
			setEquippedSlot(SLOT_HELM, std::make_shared<ItemInstance>(random->nextFloat() < 0.1f ? Tile::litPumpkin : Tile::pumpkin));
			dropChances[SLOT_HELM] = 0;
		}
	}
	return groupData;
}

void Skeleton::reassessWeaponGoal()
{
	goalSelector.removeGoal(meleeGoal);
	goalSelector.removeGoal(bowGoal);

	shared_ptr<ItemInstance> carried = getCarriedItem();

	if (carried != nullptr && carried->id == Item::bow_Id)
	{
		goalSelector.addGoal(4, bowGoal, false);
	}
	else
	{
		goalSelector.addGoal(4, meleeGoal, false);
	}
}

void Skeleton::performRangedAttack(shared_ptr<LivingEntity> target, float power)
{
	shared_ptr<Arrow> arrow = std::make_shared<Arrow>(level, dynamic_pointer_cast<LivingEntity>(shared_from_this()), target, 1.60f, 14 - (level->difficulty * 4));
	int damageBonus = EnchantmentHelper::getEnchantmentLevel(Enchantment::arrowBonus->id, getCarriedItem());
	int knockbackBonus = EnchantmentHelper::getEnchantmentLevel(Enchantment::arrowKnockback->id, getCarriedItem());

	arrow->setBaseDamage(power * 2.0f + (random->nextGaussian() * 0.25f + (level->difficulty * 0.11f)));

	if (damageBonus > 0)
	{
		arrow->setBaseDamage(arrow->getBaseDamage() + static_cast<double>(damageBonus) * .5 + .5);
	}
	if (knockbackBonus > 0)
	{
		arrow->setKnockback(knockbackBonus);
	}
	if (EnchantmentHelper::getEnchantmentLevel(Enchantment::arrowFire->id, getCarriedItem()) > 0 || getSkeletonType() == TYPE_WITHER)
	{
		arrow->setOnFire(100);
	}

	playSound(eSoundType_RANDOM_BOW, 1.0f, 1 / (getRandom()->nextFloat() * 0.4f + 0.8f));
	level->addEntity(arrow);
}

int Skeleton::getSkeletonType()
{
	return (int) entityData->getByte(DATA_TYPE_ID);
}

void Skeleton::setSkeletonType(int type)
{
	entityData->set(DATA_TYPE_ID, static_cast<byte>(type));

	fireImmune = type == TYPE_WITHER;
	if (type == TYPE_WITHER)
	{
		setSize(0.6f * 1.2f, 1.8f * 1.3f);
	}
	else
	{
		setSize(0.6f, 1.8f);
	}
}

void Skeleton::readAdditionalSaveData(CompoundTag *tag)
{
	Monster::readAdditionalSaveData(tag);

	if (tag->contains(L"SkeletonType"))
	{
		int value = tag->getByte(L"SkeletonType");
		setSkeletonType(value);
	}

	reassessWeaponGoal();
}

void Skeleton::addAdditonalSaveData(CompoundTag *entityTag)
{
	Monster::addAdditonalSaveData(entityTag);
	entityTag->putByte(L"SkeletonType", static_cast<byte>(getSkeletonType()));
}

void Skeleton::setEquippedSlot(int slot, shared_ptr<ItemInstance> item)
{
	Monster::setEquippedSlot(slot, item);

	if (!level->isClientSide && slot == SLOT_WEAPON)
	{
		reassessWeaponGoal();
	}
}

double Skeleton::getRidingHeight()
{
	return Monster::getRidingHeight() - .5;
}