aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/RangedAttackGoal.cpp
diff options
context:
space:
mode:
authordaoge <3523206925@qq.com>2026-03-03 03:04:10 +0800
committerGitHub <noreply@github.com>2026-03-03 03:04:10 +0800
commitb3feddfef372618c8a9d7a0abcaf18cfad866c18 (patch)
tree267761c3bb39241ba5c347bfbe2254d06686e287 /Minecraft.World/RangedAttackGoal.cpp
parent84c31a2331f7a0ec85b9d438992e244f60e5020f (diff)
feat: TU19 (Dec 2014) Features & Content (#155)
* try to resolve merge conflict * feat: TU19 (Dec 2014) Features & Content (#32) * December 2014 files * Working release build * Fix compilation issues * Add sound to Windows64Media * Add DLC content and force Tutorial DLC * Revert "Add DLC content and force Tutorial DLC" This reverts commit 97a43994725008e35fceb984d5549df9c8cea470. * Disable broken light packing * Disable breakpoint during DLC texture map load Allows DLC loading but the DLC textures are still broken * Fix post build not working * ... * fix vs2022 build * fix cmake build --------- Co-authored-by: Loki <lokirautio@gmail.com>
Diffstat (limited to 'Minecraft.World/RangedAttackGoal.cpp')
-rw-r--r--Minecraft.World/RangedAttackGoal.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/Minecraft.World/RangedAttackGoal.cpp b/Minecraft.World/RangedAttackGoal.cpp
new file mode 100644
index 00000000..2923e011
--- /dev/null
+++ b/Minecraft.World/RangedAttackGoal.cpp
@@ -0,0 +1,105 @@
+#include "stdafx.h"
+#include "net.minecraft.world.entity.ai.control.h"
+#include "net.minecraft.world.entity.ai.navigation.h"
+#include "net.minecraft.world.entity.ai.sensing.h"
+#include "net.minecraft.world.entity.h"
+#include "net.minecraft.world.entity.monster.h"
+#include "net.minecraft.world.phys.h"
+#include "RangedAttackGoal.h"
+
+void RangedAttackGoal::_init(RangedAttackMob *rangedMob, Mob *mob, double speedModifier, int attackIntervalMin, int attackIntervalMax, float attackRadius)
+{
+ //if (!(mob instanceof LivingEntity))
+ //{
+ //throw new IllegalArgumentException("ArrowAttackGoal requires Mob implements RangedAttackMob");
+ //}
+ rangedAttackMob = rangedMob;
+ this->mob = mob;
+ this->speedModifier = speedModifier;
+ this->attackIntervalMin = attackIntervalMin;
+ this->attackIntervalMax = attackIntervalMax;
+ this->attackRadius = attackRadius;
+ attackRadiusSqr = attackRadius * attackRadius;
+ setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
+
+ target = weak_ptr<LivingEntity>();
+ attackTime = -1;
+ seeTime = 0;
+}
+
+RangedAttackGoal::RangedAttackGoal(RangedAttackMob *rangedMob, Mob *mob, double speedModifier, int attackInterval, float attackRadius)
+{
+ _init(rangedMob, mob, speedModifier, attackInterval, attackInterval, attackRadius);
+}
+
+RangedAttackGoal::RangedAttackGoal(RangedAttackMob *rangedMob, Mob *mob, double speedModifier, int attackIntervalMin, int attackIntervalMax, float attackRadius)
+{
+ _init(rangedMob, mob, speedModifier, attackIntervalMin, attackIntervalMax, attackRadius);
+}
+
+bool RangedAttackGoal::canUse()
+{
+ shared_ptr<LivingEntity> bestTarget = mob->getTarget();
+ if (bestTarget == NULL) return false;
+ target = weak_ptr<LivingEntity>(bestTarget);
+ return true;
+}
+
+bool RangedAttackGoal::canContinueToUse()
+{
+ return canUse() || !mob->getNavigation()->isDone();
+}
+
+void RangedAttackGoal::stop()
+{
+ target = weak_ptr<LivingEntity>();
+ seeTime = 0;
+ attackTime = -1;
+}
+
+void RangedAttackGoal::tick()
+{
+ // 4J: It's possible the target has gone since canUse selected it, don't do tick if target is null
+ if (target.lock() == NULL) return;
+
+ double targetDistSqr = mob->distanceToSqr(target.lock()->x, target.lock()->bb->y0, target.lock()->z);
+ bool canSee = mob->getSensing()->canSee(target.lock());
+
+ if (canSee)
+ {
+ seeTime++;
+ }
+ else
+ {
+ seeTime = 0;
+ }
+
+ if (targetDistSqr > attackRadiusSqr || seeTime < 20)
+ {
+ mob->getNavigation()->moveTo(target.lock(), speedModifier);
+ }
+ else
+ {
+ mob->getNavigation()->stop();
+ }
+
+ mob->getLookControl()->setLookAt(target.lock(), 30, 30);
+
+ if (--attackTime == 0)
+ {
+ if (targetDistSqr > attackRadiusSqr || !canSee) return;
+
+ float dist = Mth::sqrt(targetDistSqr) / attackRadius;
+ float power = dist;
+ if (power < 0.1f) power = 0.1f;
+ if (power > 1) power = 1;
+
+ rangedAttackMob->performRangedAttack(target.lock(), power);
+ attackTime = Mth::floor(dist * (attackIntervalMax - attackIntervalMin) + attackIntervalMin);
+ }
+ else if (attackTime < 0)
+ {
+ float dist = Mth::sqrt(targetDistSqr) / attackRadius;
+ attackTime = Mth::floor(dist * (attackIntervalMax - attackIntervalMin) + attackIntervalMin);
+ }
+} \ No newline at end of file