aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Creeper.cpp
blob: 0cd6b53448722907357363e4b89fd844631bdd83 (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
#include "stdafx.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.item.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.animal.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.entity.monster.h"
#include "net.minecraft.world.damagesource.h"
#include "GeneralStat.h"
#include "Skeleton.h"
#include "Creeper.h"
#include "Arrow.h"
#include "..\Minecraft.Client\Textures.h"
#include "SoundTypes.h"



void Creeper::_init()
{
	swell = 0;
	oldSwell = 0;
	maxSwell = 30;
	explosionRadius = 3;
}

Creeper::Creeper(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());

	_init();

	goalSelector.addGoal(1, new FloatGoal(this));
	goalSelector.addGoal(2, new SwellGoal(this));
	goalSelector.addGoal(3, new AvoidPlayerGoal(this, typeid(Ocelot), 6, 1.0, 1.2));
	goalSelector.addGoal(4, new MeleeAttackGoal(this, 1.0, false));
	goalSelector.addGoal(5, new RandomStrollGoal(this, 0.8));
	goalSelector.addGoal(6, new LookAtPlayerGoal(this, typeid(Player), 8));
	goalSelector.addGoal(6, new RandomLookAroundGoal(this));

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

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

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

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

int Creeper::getMaxFallDistance()
{
	if (getTarget() == nullptr) return 3;
	// As long as they survive the fall they should try.
	return 3 + static_cast<int>(getHealth() - 1);
}

void Creeper::causeFallDamage(float distance)
{
	Monster::causeFallDamage(distance);

	swell += distance * 1.5f;
	if (swell > maxSwell - 5) swell = maxSwell - 5;
}

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

	entityData->define(DATA_SWELL_DIR, static_cast<byte>(-1));
	entityData->define(DATA_IS_POWERED, static_cast<byte>(0));
}

void Creeper::addAdditonalSaveData(CompoundTag *entityTag)
{
	Monster::addAdditonalSaveData(entityTag);
	if (entityData->getByte(DATA_IS_POWERED) == 1) entityTag->putBoolean(L"powered", true);
	entityTag->putShort(L"Fuse", static_cast<short>(maxSwell));
	entityTag->putByte(L"ExplosionRadius", static_cast<byte>(explosionRadius));
}

void Creeper::readAdditionalSaveData(CompoundTag *tag)
{
	Monster::readAdditionalSaveData(tag);
	entityData->set(DATA_IS_POWERED, static_cast<byte>(tag->getBoolean(L"powered") ? 1 : 0));
	if (tag->contains(L"Fuse")) maxSwell = tag->getShort(L"Fuse");
	if (tag->contains(L"ExplosionRadius")) explosionRadius = tag->getByte(L"ExplosionRadius");
}

void Creeper::tick()
{
	oldSwell = swell;
	if (isAlive())
	{
		int swellDir = getSwellDir();
		if (swellDir > 0 && swell == 0)
		{
			playSound(eSoundType_RANDOM_FUSE, 1, 0.5f);
		}
		swell += swellDir;
		if (swell < 0) swell = 0;
		if (swell >= maxSwell)
		{
			swell = maxSwell;
			if (!level->isClientSide)
			{
				bool destroyBlocks = level->getGameRules()->getBoolean(GameRules::RULE_MOBGRIEFING);
				if (isPowered()) level->explode(shared_from_this(), x, y, z, explosionRadius * 2, destroyBlocks);
				else level->explode(shared_from_this(), x, y, z, explosionRadius, destroyBlocks);
				remove();
			}
		}
	}
	Monster::tick();
}

int Creeper::getHurtSound()
{
	return eSoundType_MOB_CREEPER_HURT;
}

int Creeper::getDeathSound()
{
	return eSoundType_MOB_CREEPER_DEATH;
}

void Creeper::die(DamageSource *source)
{
	Monster::die(source);

	if ( source->getEntity() != nullptr && source->getEntity()->instanceof(eTYPE_SKELETON) )
	{
		int recordId = Item::record_01_Id + random->nextInt(Item::record_12_Id - Item::record_01_Id + 1);
		spawnAtLocation(recordId, 1);
	}

	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());
		player->awardStat(GenericStats::archer(), GenericStats::param_archer());
	}
}

bool Creeper::doHurtTarget(shared_ptr<Entity> target)
{
	return true;
}

bool Creeper::isPowered()
{
	return entityData->getByte(DATA_IS_POWERED) == 1;
}

float Creeper::getSwelling(float a)
{
	return (oldSwell + (swell - oldSwell) * a) / (maxSwell - 2);
}

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

int Creeper::getSwellDir()
{
	return (int) static_cast<char>(entityData->getByte(DATA_SWELL_DIR));
}

void Creeper::setSwellDir(int dir)
{
	entityData->set(DATA_SWELL_DIR, static_cast<byte>(dir));
}

void Creeper::thunderHit(const LightningBolt *lightningBolt) 
{
	Monster::thunderHit(lightningBolt);
	entityData->set(DATA_IS_POWERED, static_cast<byte>(1));
}