aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Ghast.cpp
blob: f0817791cdc738598b7a0a893c15f163a8f19f68 (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
#include "stdafx.h"
#include "net.minecraft.world.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.ai.attributes.h"
#include "net.minecraft.world.entity.projectile.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.entity.monster.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.damagesource.h"
#include "net.minecraft.stats.h"
#include "Ghast.h"
#include "..\Minecraft.Client\Textures.h"
#include "LevelEvent.h"
#include "SoundTypes.h"



void Ghast::_init()
{
	explosionPower = 1;
	floatDuration = 0;
	target = nullptr;
	retargetTime = 0;
	oCharge = 0;
	charge = 0;

	xTarget = 0.0f;
	yTarget = 0.0f;
	zTarget = 0.0f;
}

Ghast::Ghast(Level *level) : FlyingMob( 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());

	_init();

	setSize(4, 4);
	fireImmune = true;
	xpReward = Enemy::XP_REWARD_MEDIUM;
}

bool Ghast::isCharging()
{
	return entityData->getByte(DATA_IS_CHARGING) != 0;
}

bool Ghast::hurt(DamageSource *source, float dmg)
{
	if (isInvulnerable()) return false;
	if (source->getMsgId() == ChatPacket::e_ChatDeathFireball)
	{
		if ( (source->getEntity() != NULL) && source->getEntity()->instanceof(eTYPE_PLAYER) )
		{
			// reflected fireball, kill the ghast
			FlyingMob::hurt(source, 1000);
			dynamic_pointer_cast<Player>(source->getEntity())->awardStat(GenericStats::ghast(), GenericStats::param_ghast());
			return true;
		}
	}

	return FlyingMob::hurt(source, dmg);
}

void Ghast::defineSynchedData() 
{
	FlyingMob::defineSynchedData();

	entityData->define(DATA_IS_CHARGING, (byte) 0);
}

void Ghast::registerAttributes()
{
	FlyingMob::registerAttributes();

	getAttribute(SharedMonsterAttributes::MAX_HEALTH)->setBaseValue(10);
}

void Ghast::serverAiStep() 
{
	if (!level->isClientSide && level->difficulty == Difficulty::PEACEFUL) remove();
	checkDespawn();

	oCharge = charge;
	double xd = xTarget - x;
	double yd = yTarget - y;
	double zd = zTarget - z;

	double dd = xd * xd + yd * yd + zd * zd;

	if (dd < 1 * 1 || dd > 60 * 60)
	{
		xTarget = x + (random->nextFloat() * 2 - 1) * 16;
		yTarget = y + (random->nextFloat() * 2 - 1) * 16;
		zTarget = z + (random->nextFloat() * 2 - 1) * 16;
	}

	if (floatDuration-- <= 0)
	{
		floatDuration += random->nextInt(5) + 2;

		dd = sqrt(dd);

		if (canReach(xTarget, yTarget, zTarget, dd)) 
		{
			this->xd += xd / dd * 0.1;
			this->yd += yd / dd * 0.1;
			this->zd += zd / dd * 0.1;
		}
		else
		{
			xTarget = x;
			yTarget = y;
			zTarget = z;
		}
	}

	if (target != NULL && target->removed) target = nullptr;
	if (target == NULL || retargetTime-- <= 0)
	{
		target = level->getNearestAttackablePlayer(shared_from_this(), 100);
		if (target != NULL) 
		{
			retargetTime = 20;
		}
	}

	double maxDist = 64.0f;
	if (target != NULL && target->distanceToSqr(shared_from_this()) < maxDist * maxDist) 
	{
		double xdd = target->x - x;
		double ydd = (target->bb->y0 + target->bbHeight / 2) - (y + bbHeight / 2);
		double zdd = target->z - z;
		yBodyRot = yRot = -(float) atan2(xdd, zdd) * 180 / PI;

		if (canSee(target))
		{
			if (charge == 10)
			{
				// 4J - change brought forward from 1.2.3
				level->levelEvent(nullptr, LevelEvent::SOUND_GHAST_WARNING, (int) x, (int) y, (int) z, 0);
			}
			charge++;
			if (charge == 20)
			{
				// 4J - change brought forward from 1.2.3
				level->levelEvent(nullptr, LevelEvent::SOUND_GHAST_FIREBALL, (int) x, (int) y, (int) z, 0);
				shared_ptr<LargeFireball> ie = shared_ptr<LargeFireball>( new LargeFireball(level, dynamic_pointer_cast<Mob>( shared_from_this() ), xdd, ydd, zdd) );
				ie->explosionPower = explosionPower;
				double d = 4;
				Vec3 *v = getViewVector(1);
				ie->x = x + v->x * d;
				ie->y = y + bbHeight / 2 + 0.5f;
				ie->z = z + v->z * d;
				level->addEntity(ie);
				charge = -40;
			}
		} 
		else 
		{
			if (charge > 0) charge--;
		}
	}
	else
	{
		yBodyRot = yRot = -(float) atan2(this->xd, this->zd) * 180 / PI;
		if (charge > 0) charge--;
	}

	if (!level->isClientSide) 
	{
		byte old = entityData->getByte(DATA_IS_CHARGING);
		byte current = (byte) (charge > 10 ? 1 : 0);
		if (old != current)
		{
			entityData->set(DATA_IS_CHARGING, current);
		}
	}
}

bool Ghast::canReach(double xt, double yt, double zt, double dist) 
{
	double xd = (xTarget - x) / dist;
	double yd = (yTarget - y) / dist;
	double zd = (zTarget - z) / dist;

	AABB *bb = this->bb->copy();
	for (int d = 1; d < dist; d++)
	{
		bb->move(xd, yd, zd);
		if (!level->getCubes( shared_from_this(), bb)->empty()) return false;
	}

	return true;
}

int Ghast::getAmbientSound()
{
	return eSoundType_MOB_GHAST_MOAN;
}

int Ghast::getHurtSound()
{
	return eSoundType_MOB_GHAST_SCREAM;
}

int Ghast::getDeathSound()
{
	return eSoundType_MOB_GHAST_DEATH;
}

int Ghast::getDeathLoot() 
{
	return Item::gunpowder_Id;
}

void Ghast::dropDeathLoot(bool wasKilledByPlayer, int playerBonusLevel)
{
	int count = random->nextInt(2) + random->nextInt(1 + playerBonusLevel);
	for (int i = 0; i < count; i++)
	{
		spawnAtLocation(Item::ghastTear_Id, 1);
	}
	count = random->nextInt(3) + random->nextInt(1 + playerBonusLevel);
	for (int i = 0; i < count; i++)
	{
		spawnAtLocation(Item::gunpowder_Id, 1);
	}
}

float Ghast::getSoundVolume()
{
	return 0.4f;//10; 4J-PB - changing due to customer demands
}

bool Ghast::canSpawn()
{
	return (random->nextInt(20) == 0 && FlyingMob::canSpawn() && level->difficulty > Difficulty::PEACEFUL);
}

int Ghast::getMaxSpawnClusterSize()
{
	return 1;
}
void Ghast::addAdditonalSaveData(CompoundTag *tag)
{
	FlyingMob::addAdditonalSaveData(tag);
	tag->putInt(L"ExplosionPower", explosionPower);
}

void Ghast::readAdditionalSaveData(CompoundTag *tag)
{
	FlyingMob::readAdditionalSaveData(tag);
	if (tag->contains(L"ExplosionPower")) explosionPower = tag->getInt(L"ExplosionPower");
}