From bad2346f3689edc863a87f5ccf9620a0f539f0b8 Mon Sep 17 00:00:00 2001 From: Xiaro <62033805+Xiaro@users.noreply.github.com> Date: Sat, 9 Jan 2021 18:08:16 -0500 Subject: [PATCH] [cleanup] CrystalAura damage calculations --- .../module/modules/combat/CombatSetting.kt | 68 ++++++++++--------- .../kami/module/modules/combat/CrystalAura.kt | 64 +++++++++-------- 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/combat/CombatSetting.kt b/src/main/java/me/zeroeightsix/kami/module/modules/combat/CombatSetting.kt index 36cb72bfe..7486bd0cd 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/combat/CombatSetting.kt +++ b/src/main/java/me/zeroeightsix/kami/module/modules/combat/CombatSetting.kt @@ -2,6 +2,7 @@ package me.zeroeightsix.kami.module.modules.combat import kotlinx.coroutines.Job import kotlinx.coroutines.launch +import me.zeroeightsix.kami.event.SafeClientEvent import me.zeroeightsix.kami.event.events.RenderOverlayEvent import me.zeroeightsix.kami.manager.managers.CombatManager import me.zeroeightsix.kami.module.Module @@ -91,10 +92,10 @@ object CombatSetting : Module( private var overrideRange = range.value private var paused = false private val resumeTimer = TickTimer(TimeUnit.SECONDS) - private val jobMap = hashMapOf<() -> Unit, Job?>( - { updateTarget() } to null, - { updatePlacingList() } to null, - { updateCrystalList() } to null + private val jobMap = hashMapOf<(SafeClientEvent) -> Unit, Job?>( + { it: SafeClientEvent -> it.updateTarget() } to null, + { it: SafeClientEvent -> it.updatePlacingList() } to null, + { it: SafeClientEvent -> it.updateCrystalList() } to null ) val pause @@ -127,7 +128,7 @@ object CombatSetting : Module( safeListener(5000) { for ((function, future) in jobMap) { if (future.isActiveOrFalse) continue // Skip if the previous thread isn't done - jobMap[function] = defaultScope.launch { function.invoke() } + jobMap[function] = defaultScope.launch { function(this@safeListener) } } if (pauseBaritone.value && !paused && isActive()) { @@ -140,7 +141,7 @@ object CombatSetting : Module( } } - private fun updateTarget() { + private fun SafeClientEvent.updateTarget() { with(CombatManager.getTopModule()) { overrideRange = if (this is KillAura) this.range.value else range.value } @@ -150,47 +151,48 @@ object CombatSetting : Module( } } - private fun updatePlacingList() { - if (CrystalAura.isDisabled && CrystalBasePlace.isDisabled && CrystalESP.isDisabled && mc.player.ticksExisted % 4 != 0) return + private fun SafeClientEvent.updatePlacingList() { + if (CrystalAura.isDisabled && CrystalBasePlace.isDisabled && CrystalESP.isDisabled && player.ticksExisted % 4 != 0) return - val eyePos = mc.player?.getPositionEyes(1f) ?: Vec3d.ZERO + val eyePos = player.getPositionEyes(1f) ?: Vec3d.ZERO val cacheList = ArrayList>>() val target = CombatManager.target val prediction = target?.let { getPrediction(it) } - for (pos in CrystalUtils.getPlacePos(target, mc.player, 8f)) { + for (pos in CrystalUtils.getPlacePos(target, player, 8f)) { val dist = eyePos.distanceTo(pos.toVec3dCenter(0.0, 0.5, 0.0)) val damage = target?.let { CrystalUtils.calcDamage(pos, it, prediction?.first, prediction?.second) } ?: 0.0f - val selfDamage = CrystalUtils.calcDamage(pos, mc.player) + val selfDamage = CrystalUtils.calcDamage(pos, player) cacheList.add(Pair(pos, Triple(damage, selfDamage, dist))) } - CombatManager.placeMap = linkedMapOf(*cacheList.sortedByDescending { triple -> triple.second.first }.toTypedArray()) + CombatManager.placeMap = LinkedHashMap>(cacheList.size).apply { + putAll(cacheList.sortedByDescending { triple -> triple.second.first }) + } } /* Crystal damage calculation */ - private fun updateCrystalList() { - if (CrystalAura.isDisabled && CrystalESP.isDisabled && mc.player.ticksExisted % 4 - 2 != 0) return + private fun SafeClientEvent.updateCrystalList() { + if (CrystalAura.isDisabled && CrystalESP.isDisabled && (player.ticksExisted - 2 )% 4 != 0) return - val entityList = ArrayList(mc.world.loadedEntityList) val cacheList = ArrayList>>() - val cacheMap = LinkedHashMap>() - val eyePos = mc.player.getPositionEyes(1f) + val eyePos = player.getPositionEyes(1f) val target = CombatManager.target val prediction = target?.let { getPrediction(it) } - for (entity in entityList) { + for (entity in world.loadedEntityList.toList()) { if (entity.isDead) continue if (entity !is EntityEnderCrystal) continue val dist = entity.distanceTo(eyePos) if (dist > 16.0f) continue val damage = if (target != null && prediction != null) CrystalUtils.calcDamage(entity, target, prediction.first, prediction.second) else 0.0f - val selfDamage = CrystalUtils.calcDamage(entity, mc.player) + val selfDamage = CrystalUtils.calcDamage(entity, player) cacheList.add(entity to Triple(damage, selfDamage, dist)) } - cacheMap.putAll(cacheList.sortedByDescending { pair -> pair.second.first }) - CombatManager.crystalMap = cacheMap + CombatManager.crystalMap = LinkedHashMap>(cacheList.size).apply { + putAll(cacheList.sortedByDescending { pair -> pair.second.first }) + } } fun getPrediction(entity: Entity) = CombatManager.target?.let { @@ -204,7 +206,7 @@ object CombatSetting : Module( /* End of crystal damage calculation */ /* Targeting */ - private fun getTargetList(): LinkedList { + private fun SafeClientEvent.getTargetList(): LinkedList { val targetList = LinkedList() for (entity in getCacheList()) { if (AntiBot.isBot(entity)) continue @@ -214,10 +216,10 @@ object CombatSetting : Module( || entity is AbstractHorse && entity.isTame)) continue if (!teammates.value - && mc.player.isOnSameTeam(entity)) continue + && player.isOnSameTeam(entity)) continue if (!shouldIgnoreWall() - && mc.player.canEntityBeSeen(entity) + && player.canEntityBeSeen(entity) && !EntityUtils.canEntityFeetBeSeen(entity) && EntityUtils.canEntityHitboxBeSeen(entity) == null) continue @@ -227,7 +229,7 @@ object CombatSetting : Module( return targetList } - private fun getCacheList(): LinkedList { + private fun SafeClientEvent.getCacheList(): LinkedList { val player = arrayOf(players.value, friends.value, sleeping.value) val mob = arrayOf(mobs.value, passive.value, neutral.value, hostile.value) val cacheList = LinkedList(EntityUtils.getTargetList(player, mob, invisible.value, overrideRange)) @@ -243,19 +245,19 @@ object CombatSetting : Module( else true } - private fun getTarget(listIn: LinkedList): EntityLivingBase? { + private fun SafeClientEvent.getTarget(listIn: LinkedList): EntityLivingBase? { val copiedList = LinkedList(listIn) return filterTargetList(copiedList) ?: CombatManager.target?.let { entity -> if (!entity.isDead && listIn.contains(entity)) entity else null } } - private fun filterTargetList(listIn: LinkedList): EntityLivingBase? { + private fun SafeClientEvent.filterTargetList(listIn: LinkedList): EntityLivingBase? { if (listIn.isEmpty()) return null return filterByPriority(filterByFilter(listIn)) } - private fun filterByFilter(listIn: LinkedList): LinkedList { + private fun SafeClientEvent.filterByFilter(listIn: LinkedList): LinkedList { when (filter.value) { TargetFilter.FOV -> { listIn.removeIf { RotationUtils.getRelativeRotation(it) > fov.value } @@ -265,8 +267,8 @@ object CombatSetting : Module( if (!mc.gameSettings.keyBindAttack.isKeyDown && !mc.gameSettings.keyBindUseItem.isKeyDown) { return LinkedList() } - val eyePos = mc.player.getPositionEyes(KamiTessellator.pTicks()) - val lookVec = mc.player.lookVec.scale(range.value.toDouble()) + val eyePos = player.getPositionEyes(KamiTessellator.pTicks()) + val lookVec = player.lookVec.scale(range.value.toDouble()) val sightEndPos = eyePos.add(lookVec) listIn.removeIf { it.entityBoundingBox.calculateIntercept(eyePos, sightEndPos) == null } } @@ -277,7 +279,7 @@ object CombatSetting : Module( return listIn } - private fun filterByPriority(listIn: LinkedList): EntityLivingBase? { + private fun SafeClientEvent.filterByPriority(listIn: LinkedList): EntityLivingBase? { if (listIn.isEmpty()) return null if (priority.value == TargetPriority.DAMAGE) filterByDamage(listIn) @@ -326,9 +328,9 @@ object CombatSetting : Module( return listIn.sortedBy { RotationUtils.getRelativeRotation(it) }[0] } - private fun filterByDistance(listIn: LinkedList): EntityLivingBase? { + private fun SafeClientEvent.filterByDistance(listIn: LinkedList): EntityLivingBase? { if (listIn.isEmpty()) return null - return listIn.sortedBy { it.getDistance(mc.player) }[0] + return listIn.sortedBy { it.getDistance(player) }[0] } /* End of targeting */ } diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/combat/CrystalAura.kt b/src/main/java/me/zeroeightsix/kami/module/modules/combat/CrystalAura.kt index b55022cb8..c1a1aa7b2 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/combat/CrystalAura.kt +++ b/src/main/java/me/zeroeightsix/kami/module/modules/combat/CrystalAura.kt @@ -73,7 +73,7 @@ object CrystalAura : Module( /* Force place */ private val bindForcePlace by setting("BindForcePlace", Bind(), { page == Page.FORCE_PLACE }) private val forcePlaceHealth by setting("ForcePlaceHealth", 6.0f, 0.0f..20.0f, 0.5f, { page == Page.FORCE_PLACE }) - private val forcePlaceArmorDura by setting("ForcePlaceArmorDura", 10, 0..50, 1, { page == Page.FORCE_PLACE }) + private val forcePlaceArmorDura by setting("ForcePlaceArmorDura", 5, 0..50, 1, { page == Page.FORCE_PLACE }) private val minDamageForcePlace by setting("MinDamageForcePlace", 1.5f, 0.0f..10.0f, 0.25f, { page == Page.FORCE_PLACE }) /* Place page one */ @@ -104,7 +104,7 @@ object CrystalAura : Module( /* Explode page two */ private val minDamageE by setting("MinDamageExplode", 6.0f, 0.0f..10.0f, 0.25f, { page == Page.EXPLODE_TWO }) private val maxSelfDamageE by setting("MaxSelfDamageExplode", 3.0f, 0.0f..10.0f, 0.25f, { page == Page.EXPLODE_TWO }) - private val swapDelay by setting("SwapDelay", 10, 1..50, 2, { page == Page.EXPLODE_TWO }) + private val swapDelay by setting("SwapDelay", 10, 1..50, 1, { page == Page.EXPLODE_TWO }) private val hitDelay by setting("HitDelay", 1, 1..10, 1, { page == Page.EXPLODE_TWO }) private val hitAttempts by setting("HitAttempts", 4, 0..8, 1, { page == Page.EXPLODE_TWO }) private val explodeRange by setting("ExplodeRange", 4.0f, 0.0f..5.0f, 0.25f, { page == Page.EXPLODE_TWO }) @@ -262,15 +262,15 @@ object CrystalAura : Module( } private fun SafeClientEvent.place() { - if (autoSwap && getHand() == null) { - InventoryUtils.getSlotsHotbar(426)?.get(0)?.let { - if (spoofHotbar) PlayerPacketManager.spoofHotbar(it) - else InventoryUtils.swapSlot(it) - } - } - getPlacingPos()?.let { pos -> getHand()?.let { hand -> + if (autoSwap && getHand() == null) { + InventoryUtils.getSlotsHotbar(426)?.get(0)?.let { + if (spoofHotbar) PlayerPacketManager.spoofHotbar(it) + else InventoryUtils.swapSlot(it) + } + } + placeTimer = 0 inactiveTicks = 0 lastLookAt = Vec3d(pos).add(0.5, placeOffset.toDouble(), 0.5) @@ -330,9 +330,9 @@ object CrystalAura : Module( } private fun SafeClientEvent.explode() { - if (!preExplode()) return - getExplodingCrystal()?.let { + if (!preExplode()) return + if (hitAttempts != 0 && it == lastCrystal) { hitCount++ if (hitCount >= hitAttempts) ignoredList.add(it) @@ -372,13 +372,11 @@ object CrystalAura : Module( /* End of main functions */ /* Placing */ - private fun SafeClientEvent.canPlace(): Boolean { - return doPlace + private fun SafeClientEvent.canPlace() = + doPlace && placeTimer > placeDelay && InventoryUtils.countItemAll(426) > 0 - && getPlacingPos() != null && countValidCrystal() < maxCrystal - } @Suppress("UnconditionalJumpStatementInLoop") // The linter is wrong here, it will continue until it's supposed to return private fun SafeClientEvent.getPlacingPos(): BlockPos? { @@ -421,36 +419,36 @@ object CrystalAura : Module( * @return True if passed placing damage check */ private fun checkDamagePlace(damage: Float, selfDamage: Float) = - (shouldFacePlace(damage) || damage >= minDamageP) && (selfDamage <= maxSelfDamageP) + (shouldFacePlace(damage) || damage >= minDamageP) && (selfDamage <= maxSelfDamageP) /* End of placing */ /* Exploding */ - private fun SafeClientEvent.canExplode() = - doExplode - && hitTimer > hitDelay - && getExplodingCrystal() != null + private fun canExplode() = doExplode && hitTimer > hitDelay - private fun SafeClientEvent.getExplodingCrystal() = - (crystalMap.entries.firstOrNull { (crystal, triple) -> + private fun SafeClientEvent.getExplodingCrystal(): EntityEnderCrystal? { + val filteredCrystal = crystalMap.entries.filter { (crystal, triple) -> !ignoredList.contains(crystal) && !crystal.isDead - && triple.third <= explodeRange && checkDamageExplode(triple.first, triple.second) + && checkYawSpeed(RotationUtils.getRotationToEntity(crystal).x) + } + + return (filteredCrystal.firstOrNull { (crystal, triple) -> + triple.third <= explodeRange && (player.canEntityBeSeen(crystal) || EntityUtils.canEntityFeetBeSeen(crystal)) - && checkYawSpeed(RotationUtils.getRotationToEntity(crystal).x) - } ?: crystalMap.entries.firstOrNull { (crystal, triple) -> - !ignoredList.contains(crystal) - && !crystal.isDead - && triple.third <= wallExplodeRange - && checkDamageExplode(triple.first, triple.second) - && EntityUtils.canEntityHitboxBeSeen(crystal) != null - && checkYawSpeed(RotationUtils.getRotationToEntity(crystal).x) + } ?: filteredCrystal.firstOrNull { (_, triple) -> + triple.third <= wallExplodeRange })?.key + } - private fun checkDamageExplode(damage: Float, selfDamage: Float) = (shouldFacePlace(damage) || shouldForceExplode() || damage >= minDamageE) && selfDamage <= maxSelfDamageE + private fun checkDamageExplode(damage: Float, selfDamage: Float) = + (shouldFacePlace(damage) || shouldForceExplode() || damage >= minDamageE) && selfDamage <= maxSelfDamageE - private fun shouldForceExplode() = autoForceExplode && placeMap.isNotEmpty() && placeMap.values.first().second > minDamageE + private fun shouldForceExplode() = autoForceExplode + && placeMap.values.any { + it.first > minDamage && it.second <= maxSelfDamage && it.third <= placeRange + } /* End of exploding */ /* General */