#include "stdafx.h" #include "net.minecraft.world.level.h" #include "net.minecraft.world.level.tile.h" #include "net.minecraft.world.entity.h" #include "net.minecraft.world.entity.player.h" #include "net.minecraft.world.phys.h" #include "net.minecraft.world.item.h" #include "net.minecraft.world.damagesource.h" #include "net.minecraft.world.item.enchantment.h" #include "net.minecraft.network.packet.h" #include "../Minecraft.Client/ServerPlayer.h" #include "../Minecraft.Client/PlayerConnection.h" #include "com.mojang.nbt.h" #include "Arrow.h" #include "net.minecraft.world.effect.h" #include "net.minecraft.world.item.alchemy.h" #include "MobEffectInstance.h" // 4J : WESTY : Added for other award, kill creeper with arrow. #include "net.minecraft.world.entity.monster.h" #include "net.minecraft.stats.h" #include "SoundTypes.h" // base damage, multiplied with velocity const double Arrow::ARROW_BASE_DAMAGE = 2.0f; // 4J - added common ctor code. void Arrow::_init() { // 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(); xTile = -1; yTile = -1; zTile = -1; lastTile = 0; lastData = 0; inGround = false; pickup = PICKUP_DISALLOWED; shakeTime = 0; flightTime = 0; owner = nullptr; life = 0; baseDamage = ARROW_BASE_DAMAGE; inGroundTime = 0; customEffectId = -1; customEffectDuration = 0; customEffectAmplifier = 0; knockback = 0; } Arrow::Arrow(Level *level) : Entity( level ) { _init(); viewScale = 10; setSize(0.5f, 0.5f); } Arrow::Arrow(Level *level, shared_ptr mob, shared_ptr target, float power, float uncertainty) : Entity( level ) { _init(); viewScale = 10; owner = mob; if ( mob->instanceof(eTYPE_PLAYER) ) pickup = PICKUP_ALLOWED; y = mob->y + mob->getHeadHeight() - 0.1f; double xd = target->x - mob->x; double yd = (target->y + target->getHeadHeight() - 0.7f) - y; double zd = target->z - mob->z; double sd = sqrt(xd * xd + zd * zd); if (sd < 0.0000001) return; float yRot = static_cast(atan2(zd, xd) * 180 / PI) - 90; float xRot = static_cast(-(atan2(yd, sd) * 180 / PI)); double xdn = xd / sd; double zdn = zd / sd; moveTo(mob->x + xdn, y, mob->z + zdn, yRot, xRot); heightOffset = 0; float yo = static_cast(sd) * 0.2f; shoot(xd, yd + yo, zd, power, uncertainty); } Arrow::Arrow(Level *level, double x, double y, double z) : Entity( level ) { _init(); viewScale = 10; setSize(0.5f, 0.5f); setPos(x, y, z); heightOffset = 0; } Arrow::Arrow(Level *level, shared_ptr mob, float power) : Entity( level ) { _init(); viewScale = 10; owner = mob; if ( mob->instanceof(eTYPE_PLAYER) ) pickup = PICKUP_ALLOWED; setSize(0.5f, 0.5f); moveTo(mob->x, mob->y + mob->getHeadHeight(), mob->z, mob->yRot, mob->xRot); x -= Mth::cos(yRot / 180 * PI) * 0.16f; y -= 0.1f; z -= Mth::sin(yRot / 180 * PI) * 0.16f; setPos(x, y, z); heightOffset = 0; xd = -Mth::sin(yRot / 180 * PI) * Mth::cos(xRot / 180 * PI); zd = Mth::cos(yRot / 180 * PI) * Mth::cos(xRot / 180 * PI); yd = -Mth::sin(xRot / 180 * PI); shoot(xd, yd, zd, power * 1.5f, 1); } void Arrow::defineSynchedData() { entityData->define(ID_FLAGS, static_cast(0)); entityData->define(ID_ARROW_TYPE, static_cast(TYPE_NORMAL)); entityData->define(ID_POTION, 0); } void Arrow::shoot(double xd, double yd, double zd, float pow, float uncertainty) { float dist = static_cast(sqrt(xd * xd + yd * yd + zd * zd)); xd /= dist; yd /= dist; zd /= dist; xd += (random->nextGaussian() * (random->nextBoolean() ? -1 : 1)) * 0.0075f * uncertainty; yd += (random->nextGaussian() * (random->nextBoolean() ? -1 : 1)) * 0.0075f * uncertainty; zd += (random->nextGaussian() * (random->nextBoolean() ? -1 : 1)) * 0.0075f * uncertainty; xd *= pow; yd *= pow; zd *= pow; this->xd = xd; this->yd = yd; this->zd = zd; double sd = sqrt(xd * xd + zd * zd); yRotO = yRot = static_cast(atan2(xd, zd) * 180 / PI); xRotO = xRot = static_cast(atan2(yd, sd) * 180 / PI); life = 0; } void Arrow::lerpTo(double x, double y, double z, float yRot, float xRot, int steps) { setPos(x, y, z); setRot(yRot, xRot); } void Arrow::lerpMotion(double xd, double yd, double zd) { this->xd = xd; this->yd = yd; this->zd = zd; if (xRotO == 0 && yRotO == 0) { double sd = sqrt(xd * xd + zd * zd); yRotO = yRot = static_cast(atan2(xd, zd) * 180 / PI); xRotO = xRot = static_cast(atan2(yd, sd) * 180 / PI); xRotO = xRot; yRotO = yRot; app.DebugPrintf("%f %f : 0x%x\n",xRot,yRot,&yRot); moveTo(x, y, z, yRot, xRot); life = 0; } } void Arrow::tick() { Entity::tick(); if (level->isClientSide) { int arrowType = getArrowType(); if (arrowType == TYPE_SPECTRAL && !inGround) { level->addParticle(eParticleType_instantSpell, x, y, z, 0, 0, 0); } else if (arrowType == TYPE_TIPPED) { int col = PotionBrewing::getColorValue(getPotionValue(), false); int count = inGround ? ((inGroundTime++ % 5) == 0 ? 1 : 0) : 2; for (int i = 0; i < count; i++) { level->addParticle(eParticleType_mobSpell, x + (random->nextDouble() - 0.5) * bbWidth, y + random->nextDouble() * bbHeight, z + (random->nextDouble() - 0.5) * bbWidth, ((col >> 16) & 0xff) / 255.0, ((col >> 8) & 0xff) / 255.0, (col & 0xff) / 255.0); } } } if (xRotO == 0 && yRotO == 0) { double sd = sqrt(xd * xd + zd * zd); yRotO = yRot = static_cast(atan2(xd, zd) * 180 / PI); xRotO = xRot = static_cast(atan2(yd, sd) * 180 / PI); } { int t = level->getTile(xTile, yTile, zTile); if (t > 0) { Tile::tiles[t]->updateShape(level, xTile, yTile, zTile); AABB *aabb = Tile::tiles[t]->getAABB(level, xTile, yTile, zTile); if (aabb != nullptr && aabb->contains(Vec3::newTemp(x, y, z))) { inGround = true; } } } if (shakeTime > 0) shakeTime--; if (inGround) { int tile = level->getTile(xTile, yTile, zTile); int data = level->getData(xTile, yTile, zTile); if (tile != lastTile || data != lastData) { inGround = false; xd *= random->nextFloat() * 0.2f; yd *= random->nextFloat() * 0.2f; zd *= random->nextFloat() * 0.2f; life = 0; flightTime = 0; return; } else { life++; if (life == 20 * 60) remove(); return; } } else { flightTime++; } Vec3 *from = Vec3::newTemp(x, y, z); Vec3 *to = Vec3::newTemp(x + xd, y + yd, z + zd); HitResult *res = level->clip(from, to, false, true); from = Vec3::newTemp(x, y, z); to = Vec3::newTemp(x + xd, y + yd, z + zd); if (res != nullptr) { to = Vec3::newTemp(res->pos->x, res->pos->y, res->pos->z); } shared_ptr hitEntity = nullptr; vector > *objects = level->getEntities(shared_from_this(), this->bb->expand(xd, yd, zd)->grow(1, 1, 1)); double nearest = 0; for ( auto& e : *objects ) { if (!e->isPickable() || (e == owner && flightTime < 5)) continue; float rr = 0.3f; AABB *bb = e->bb->grow(rr, rr, rr); HitResult *p = bb->clip(from, to); if (p != nullptr) { double dd = from->distanceTo(p->pos); if (dd < nearest || nearest == 0) { hitEntity = e; nearest = dd; } delete p; } } if (hitEntity != nullptr) { delete res; res = new HitResult(hitEntity); } if ( (res != nullptr) && (res->entity != nullptr) && res->entity->instanceof(eTYPE_PLAYER)) { shared_ptr player = dynamic_pointer_cast(res->entity); // 4J: Check for owner being null if ( player->abilities.invulnerable || ((owner != nullptr) && (owner->instanceof(eTYPE_PLAYER) && !dynamic_pointer_cast(owner)->canHarmPlayer(player)))) { res = nullptr; } } if (res != nullptr) { if (res->entity != nullptr) { float pow = Mth::sqrt(xd * xd + yd * yd + zd * zd); int dmg = (int) Mth::ceil(static_cast(pow * baseDamage)); if(isCritArrow()) dmg += random->nextInt(dmg / 2 + 2); DamageSource *damageSource = nullptr; if (owner == nullptr) { damageSource = DamageSource::arrow(dynamic_pointer_cast(shared_from_this()), shared_from_this()); } else { damageSource = DamageSource::arrow(dynamic_pointer_cast(shared_from_this()), owner); } if(!res->entity->isInvulnerable()) { if (isOnFire() && res->entity->GetType() != eTYPE_ENDERMAN) { res->entity->setOnFire(5); } } if(res->entity->hurt(damageSource, dmg)) { // Firx for #67839 - Customer Encountered: Bows enchanted with "Flame" still set things on fire if pvp/attack animals is turned off // 4J Stu - We should not set the entity on fire unless we can cause some damage (this doesn't necessarily mean that the arrow hit lowered their health) // set targets on fire first because we want cooked // pork/chicken/steak if (res->entity->instanceof(eTYPE_LIVINGENTITY)) { shared_ptr mob = dynamic_pointer_cast(res->entity); if (!level->isClientSide) { mob->setArrowCount(mob->getArrowCount() + 1); applyArrowEffects(mob); } if (knockback > 0) { float pushLen = sqrt(xd * xd + zd * zd); if (pushLen > 0) { res->entity->push(xd * knockback * .6f / pushLen, 0.1, zd * knockback * .6f / pushLen); } } if (owner != nullptr) { ThornsEnchantment::doThornsAfterAttack(owner, mob, random); } if (owner != nullptr && res->entity != owner && owner->GetType() == eTYPE_SERVERPLAYER) { dynamic_pointer_cast(owner)->connection->send(std::make_shared(GameEventPacket::SUCCESSFUL_BOW_HIT, 0)); } } // 4J : WESTY : For award, need to track if creeper was killed by arrow from the player. if (owner != nullptr && owner->instanceof(eTYPE_PLAYER) // arrow owner is a player && !res->entity->isAlive() // target is now dead && (res->entity->GetType() == eTYPE_CREEPER)) // target is a creeper { dynamic_pointer_cast(owner)->awardStat( GenericStats::arrowKillCreeper(), GenericStats::param_arrowKillCreeper() ); } playSound( eSoundType_RANDOM_BOW_HIT, 1.0f, 1.2f / (random->nextFloat() * 0.2f + 0.9f)); if (res->entity->GetType() != eTYPE_ENDERDRAGON) remove(); } else { xd *= -0.1f; yd *= -0.1f; zd *= -0.1f; yRot += 180; yRotO += 180; flightTime = 0; } delete damageSource; } else { xTile = res->x; yTile = res->y; zTile = res->z; lastTile = level->getTile(xTile, yTile, zTile); lastData = level->getData(xTile, yTile, zTile); xd = static_cast(res->pos->x - x); yd = static_cast(res->pos->y - y); zd = static_cast(res->pos->z - z); float dd = static_cast(sqrt(xd * xd + yd * yd + zd * zd)); // 4J added check - zero dd here was creating NaNs if( dd > 0.0001f ) { x -= (xd / dd) * 0.05f; y -= (yd / dd) * 0.05f; z -= (zd / dd) * 0.05f; } playSound(eSoundType_RANDOM_BOW_HIT, 1.0f, 1.2f / (random->nextFloat() * 0.2f + 0.9f)); inGround = true; shakeTime = 7; setCritArrow(false); if (lastTile != 0) { Tile::tiles[lastTile]->entityInside(level, xTile, yTile, zTile, shared_from_this() ); } } } delete res; if(isCritArrow()) { for (int i = 0; i < 4; i++) { level->addParticle(eParticleType_crit, x + xd * i / 4.0f, y + yd * i / 4.0f, z + zd * i / 4.0f, -xd, -yd + 0.2, -zd); } } x += xd; y += yd; z += zd; double sd = sqrt(xd * xd + zd * zd); yRot = static_cast(atan2(xd, zd) * 180 / PI); xRot = static_cast(atan2(yd, sd) * 180 / PI); while (xRot - xRotO < -180) xRotO -= 360; while (xRot - xRotO >= 180) xRotO += 360; while (yRot - yRotO < -180) yRotO -= 360; while (yRot - yRotO >= 180) yRotO += 360; xRot = xRotO + (xRot - xRotO) * 0.2f; yRot = yRotO + (yRot - yRotO) * 0.2f; float inertia = 0.99f; float gravity = 0.05f; if (isInWater()) { for (int i = 0; i < 4; i++) { float s = 1 / 4.0f; level->addParticle(eParticleType_bubble, x - xd * s, y - yd * s, z - zd * s, xd, yd, zd); } inertia = 0.80f; } xd *= inertia; yd *= inertia; zd *= inertia; yd -= gravity; setPos(x, y, z); checkInsideTiles(); } void Arrow::addAdditonalSaveData(CompoundTag *tag) { tag->putShort(L"xTile", static_cast(xTile)); tag->putShort(L"yTile", static_cast(yTile)); tag->putShort(L"zTile", static_cast(zTile)); tag->putByte(L"inTile", static_cast(lastTile)); tag->putByte(L"inData", static_cast(lastData)); tag->putByte(L"shake", static_cast(shakeTime)); tag->putByte(L"inGround", static_cast(inGround ? 1 : 0)); tag->putByte(L"pickup", static_cast(pickup)); tag->putDouble(L"damage", baseDamage); tag->putByte(L"ArrowType", static_cast(getArrowType())); tag->putInt(L"Potion", getPotionValue()); if (customEffectId >= 0) { tag->putInt(L"CustomEffect", customEffectId); tag->putInt(L"CustomEffectDuration", customEffectDuration); tag->putInt(L"CustomEffectAmplifier", customEffectAmplifier); } } void Arrow::readAdditionalSaveData(CompoundTag *tag) { xTile = tag->getShort(L"xTile"); yTile = tag->getShort(L"yTile"); zTile = tag->getShort(L"zTile"); lastTile = tag->getByte(L"inTile") & 0xff; lastData = tag->getByte(L"inData") & 0xff; shakeTime = tag->getByte(L"shake") & 0xff; inGround = tag->getByte(L"inGround") == 1; if (tag->contains(L"damage")) { baseDamage = tag->getDouble(L"damage"); } if (tag->contains(L"ArrowType")) { setArrowType(tag->getByte(L"ArrowType"), tag->getInt(L"Potion")); if (tag->contains(L"CustomEffect")) setCustomEffect(tag->getInt(L"CustomEffect"), tag->getInt(L"CustomEffectDuration"), tag->getInt(L"CustomEffectAmplifier")); } if (tag->contains(L"pickup")) { pickup = tag->getByte(L"pickup"); } else if (tag->contains(L"player")) { pickup = tag->getBoolean(L"player") ? PICKUP_ALLOWED : PICKUP_DISALLOWED; } } void Arrow::playerTouch(shared_ptr player) { if (level->isClientSide || !inGround || shakeTime > 0) return; bool bRemove = pickup == PICKUP_ALLOWED || (pickup == PICKUP_CREATIVE_ONLY && player->abilities.instabuild); if (pickup == PICKUP_ALLOWED) { if (!player->inventory->add(getPickupItem())) { bRemove = false; } } if (bRemove) { playSound(eSoundType_RANDOM_POP, 0.2f, ((random->nextFloat() - random->nextFloat()) * 0.7f + 1.0f) * 2.0f); player->take(shared_from_this(), 1); remove(); } } bool Arrow::makeStepSound() { return false; } float Arrow::getShadowHeightOffs() { return 0; } void Arrow::setBaseDamage(double baseDamage) { this->baseDamage = baseDamage; } double Arrow::getBaseDamage() { return baseDamage; } void Arrow::setKnockback(int knockback) { this->knockback = knockback; } bool Arrow::isAttackable() { return false; } void Arrow::setCritArrow(bool critArrow) { byte flags = entityData->getByte(ID_FLAGS); if (critArrow) { entityData->set(ID_FLAGS, static_cast(flags | FLAG_CRIT)); } else { entityData->set(ID_FLAGS, static_cast(flags & ~FLAG_CRIT)); } } bool Arrow::isCritArrow() { byte flags = entityData->getByte(ID_FLAGS); return (flags & FLAG_CRIT) != 0; } void Arrow::setArrowType(int type, int potionValue) { entityData->set(ID_ARROW_TYPE, static_cast(type)); entityData->set(ID_POTION, type == TYPE_TIPPED ? potionValue : 0); } int Arrow::getArrowType() { return entityData->getByte(ID_ARROW_TYPE); } int Arrow::getPotionValue() { return entityData->getInteger(ID_POTION); } bool Arrow::isArrowItem(int itemId) { return itemId == Item::arrow_Id || itemId == Item::spectral_arrow_Id || itemId == Item::tipped_arrow_Id; } void Arrow::setFromItem(shared_ptr item) { if (item == nullptr || item->id == Item::arrow_Id) { setArrowType(TYPE_NORMAL); } else if (item->id == Item::spectral_arrow_Id) { setArrowType(TYPE_SPECTRAL); } else if (item->id == Item::tipped_arrow_Id) { setArrowType(TYPE_TIPPED, item->getAuxValue()); } } shared_ptr Arrow::getPickupItem() { switch (getArrowType()) { case TYPE_SPECTRAL: return std::make_shared(Item::spectral_arrow, 1); case TYPE_TIPPED: return std::make_shared(Item::tipped_arrow_Id, 1, getPotionValue()); default: return std::make_shared(Item::arrow, 1); } } void Arrow::applyArrowEffects(shared_ptr mob) { int arrowType = getArrowType(); if (arrowType == TYPE_SPECTRAL) { mob->addEffect(new MobEffectInstance(MobEffect::glowing->id, SPECTRAL_GLOW_DURATION, 0)); } else if (arrowType == TYPE_TIPPED) { if (customEffectId >= 0) { mob->addEffect(new MobEffectInstance(customEffectId, customEffectDuration, customEffectAmplifier)); return; } vector *effects = PotionBrewing::getEffects(getPotionValue(), false); if (effects == nullptr) return; for (auto &effect : *effects) { MobEffect *mobEffect = MobEffect::effects[effect->getId()]; if (mobEffect != nullptr) { if (mobEffect->isInstantenous()) { mobEffect->applyInstantenousEffect(dynamic_pointer_cast(owner), mob, effect->getAmplifier(), 1.0); } else { mob->addEffect(new MobEffectInstance(effect->getId(), max(effect->getDuration() / 8, 1), effect->getAmplifier())); } } delete effect; } delete effects; } } void Arrow::setCustomEffect(int effectId, int duration, int amplifier) { customEffectId = effectId; customEffectDuration = duration; customEffectAmplifier = amplifier; }