Skip to content

Commit d126950

Browse files
committed
Add firework terrain protection
Closes #74 Signed-off-by: Octol1ttle <[email protected]>
1 parent 94ce3ab commit d126950

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

src/main/kotlin/ru/octol1ttle/flightassistant/config/FAConfigScreen.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ object FAConfigScreen {
449449
binding(current::fireworkLockExplosive, defaults.fireworkLockExplosive)
450450
controller(tickBox())
451451
}
452+
rootOptions.register("firework.lock_obstacles") {
453+
setSafetyName()
454+
binding(current::fireworkLockObstacles, defaults.fireworkLockObstacles)
455+
controller(tickBox())
456+
}
452457
}
453458
}
454459

src/main/kotlin/ru/octol1ttle/flightassistant/config/options/SafetyOptions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class SafetyOptions {
7878
var fireworkExplosiveAlert: Boolean = true
7979
@SerialEntry
8080
var fireworkLockExplosive: Boolean = true
81+
@SerialEntry
82+
var fireworkLockObstacles: Boolean = true
8183

8284
internal fun setDisabled(): SafetyOptions {
8385
this.alertVolume = 0.0f
@@ -111,6 +113,7 @@ class SafetyOptions {
111113

112114
this.fireworkExplosiveAlert = false
113115
this.fireworkLockExplosive = false
116+
this.fireworkLockObstacles = false
114117
return this
115118
}
116119

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/FireworkComputer.kt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,20 @@ class FireworkComputer(computers: ComputerBus, private val mc: Minecraft) : Comp
3737
ThrustSourceRegistrationCallback.EVENT.register { it.accept(this) }
3838
InteractionEvent.RIGHT_CLICK_ITEM.register(InteractionEvent.RightClickItem { player, hand ->
3939
val stack: ItemStack = player.getItemInHand(hand)
40-
if (!player.level().isClientSide()) {
40+
if (player.level().isClientSide()) {
41+
val explosive = FAConfig.safety.fireworkLockExplosive && !isEmptyOrSafe(player, hand)
42+
val anyTerrainAhead = FAConfig.safety.fireworkLockObstacles && anyTerrainAhead()
43+
if (explosive || anyTerrainAhead) {
4144
//? if >=1.21.2 {
42-
/*return@RightClickItem net.minecraft.world.InteractionResult.PASS
45+
/*return@RightClickItem net.minecraft.world.InteractionResult.FAIL
4346
*///?} else
44-
return@RightClickItem dev.architectury.event.CompoundEventResult.pass()
45-
46-
}
47-
48-
if (FAConfig.safety.fireworkLockExplosive && !isEmptyOrSafe(player, hand)) {
49-
//? if >=1.21.2 {
50-
/*return@RightClickItem net.minecraft.world.InteractionResult.FAIL
51-
*///?} else
52-
return@RightClickItem dev.architectury.event.CompoundEventResult.interruptFalse(stack)
53-
}
47+
return@RightClickItem dev.architectury.event.CompoundEventResult.interruptFalse(stack)
48+
}
5449

55-
if (!waitingForResponse && stack.item is FireworkRocketItem) {
56-
lastActivationTime = FATickCounter.totalTicks
57-
waitingForResponse = true
50+
if (!waitingForResponse && stack.item is FireworkRocketItem) {
51+
lastActivationTime = FATickCounter.totalTicks
52+
waitingForResponse = true
53+
}
5854
}
5955

6056
//? if >=1.21.2 {
@@ -118,6 +114,12 @@ class FireworkComputer(computers: ComputerBus, private val mc: Minecraft) : Comp
118114
return stack.getTagElement("Fireworks")?.getList("Explosions", net.minecraft.nbt.Tag.TAG_COMPOUND.toInt())?.isEmpty() != false
119115
}
120116

117+
private fun anyTerrainAhead(): Boolean {
118+
val velocity = computers.data.player.forward.scale(FIREWORK_SPEED.toDouble())
119+
val end = computers.data.position.add(velocity.scale(computers.gpws.cautionThreshold))
120+
return computers.gpws.computeObstacleImpactTime(end, velocity) <= computers.gpws.warningThreshold
121+
}
122+
121123
private fun tryActivateFirework(player: Player) {
122124
if (FATickCounter.totalTicks < lastActivationTime + 10) {
123125
return
@@ -145,7 +147,7 @@ class FireworkComputer(computers: ComputerBus, private val mc: Minecraft) : Comp
145147
}
146148

147149
override fun tickThrust(currentThrust: Float) {
148-
if (currentThrust > computers.data.forwardVelocityPerSecond.length() / 30.0f) {
150+
if (currentThrust > computers.data.forwardVelocityPerSecond.length() / FIREWORK_SPEED && !anyTerrainAhead()) {
149151
tryActivateFirework(computers.data.player)
150152
}
151153
}
@@ -166,5 +168,7 @@ class FireworkComputer(computers: ComputerBus, private val mc: Minecraft) : Comp
166168

167169
companion object {
168170
val ID: ResourceLocation = FlightAssistant.id("firework")
171+
172+
private const val FIREWORK_SPEED: Float = 33.33f
169173
}
170174
}

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/safety/GroundProximityComputer.kt

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
3232
var obstacleImpactStatus: Status = Status.SAFE
3333
private set
3434

35+
private var anyBlocksAbove = false
36+
val safeThreshold: Double
37+
get() = if (anyBlocksAbove) 7.5 else 10.0
38+
val cautionThreshold: Double
39+
get() = if (anyBlocksAbove) 7.5 else 10.0
40+
val warningThreshold: Double
41+
get() = if (anyBlocksAbove) 7.5 else 10.0
42+
val recoverThreshold: Double
43+
get() = if (anyBlocksAbove) 7.5 else 10.0
44+
3545
var groundY: Double? = null
3646
val groundOrVoidY: Double
3747
get() = if (groundY == null || groundY == Double.MAX_VALUE) computers.data.voidY.toDouble()
@@ -54,19 +64,15 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
5464
return
5565
}
5666

57-
val anyBlocksAbove: Boolean = data.level.getHeight(Heightmap.Types.MOTION_BLOCKING, data.player.blockX, data.player.blockZ) > data.player.y
58-
val clearThreshold: Double = if (anyBlocksAbove) 7.5 else 10.0
59-
val cautionThreshold: Double = if (anyBlocksAbove) 3.0 else 7.5
60-
val warningThreshold: Double = if (anyBlocksAbove) 1.5 else 3.0
61-
val recoverThreshold = 0.75
67+
anyBlocksAbove = data.level.getHeight(Heightmap.Types.MOTION_BLOCKING, data.player.blockX, data.player.blockZ) > data.player.y
6268

6369
groundImpactTime = computeGroundImpactTime(data).throwIfNotInRange(0.0..Double.MAX_VALUE)
6470
groundImpactStatus =
6571
if (data.fallDistanceSafe) {
6672
Status.SAFE
6773
} else if (groundImpactStatus == Status.SAFE && (data.velocityPerSecond.y > -10 || groundImpactTime > cautionThreshold)) {
6874
Status.SAFE
69-
} else if (data.velocityPerSecond.y > -8.5 || groundImpactTime > clearThreshold) {
75+
} else if (data.velocityPerSecond.y > -8.5 || groundImpactTime > safeThreshold) {
7076
Status.SAFE
7177
} else if (groundImpactStatus >= Status.CAUTION && groundImpactTime > warningThreshold) {
7278
Status.CAUTION
@@ -76,15 +82,15 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
7682
Status.RECOVER
7783
}
7884

79-
obstacleImpactTime = computeObstacleImpactTime(data, clearThreshold).throwIfNotInRange(0.0..Double.MAX_VALUE)
85+
obstacleImpactTime = computeObstacleImpactTime(data, safeThreshold).throwIfNotInRange(0.0..Double.MAX_VALUE)
8086

8187
val damageOnCollision: Double = data.velocity.horizontalDistance() * 10 - 3
8288
obstacleImpactStatus =
8389
if (data.isInvulnerableTo(data.player.damageSources().flyIntoWall())) {
8490
Status.SAFE
8591
} else if (obstacleImpactStatus == Status.SAFE && (damageOnCollision < data.player.health * 0.5f || obstacleImpactTime > groundImpactTime * 1.1f || obstacleImpactTime > cautionThreshold)) {
8692
Status.SAFE
87-
} else if (damageOnCollision < data.player.health * 0.25f || obstacleImpactTime > groundImpactTime * 1.2f || obstacleImpactTime > clearThreshold) {
93+
} else if (damageOnCollision < data.player.health * 0.25f || obstacleImpactTime > groundImpactTime * 1.2f || obstacleImpactTime > safeThreshold) {
8894
Status.SAFE
8995
} else if (obstacleImpactStatus >= Status.CAUTION && obstacleImpactTime > warningThreshold) {
9096
Status.CAUTION
@@ -120,20 +126,24 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
120126

121127
private fun computeObstacleImpactTime(data: AirDataComputer, lookAheadTime: Double): Double {
122128
val end: Vec3 = data.position.add(data.forwardVelocityPerSecond.multiply(lookAheadTime, 0.0, lookAheadTime))
123-
val result: BlockHitResult = data.level.clip(
129+
return computeObstacleImpactTime(end, computers.data.forwardVelocityPerSecond)
130+
}
131+
132+
fun computeObstacleImpactTime(raycastEnd: Vec3, velocity: Vec3): Double {
133+
val result: BlockHitResult = computers.data.level.clip(
124134
ClipContext(
125-
data.position,
126-
end,
135+
computers.data.position,
136+
raycastEnd,
127137
ClipContext.Block.COLLIDER,
128138
ClipContext.Fluid.ANY,
129-
data.player
139+
computers.data.player
130140
)
131141
)
132142
if (result.type != HitResult.Type.BLOCK) {
133143
return Double.MAX_VALUE
134144
}
135145

136-
return data.position.distanceTo(result.location) / data.forwardVelocityPerSecond.length()
146+
return computers.data.position.distanceTo(result.location) / velocity.length()
137147
}
138148

139149
override fun <Response> handleQuery(query: ComputerQuery<Response>) {
@@ -195,6 +205,7 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
195205
groundImpactStatus = Status.SAFE
196206
obstacleImpactTime = Double.MAX_VALUE
197207
obstacleImpactStatus = Status.SAFE
208+
anyBlocksAbove = false
198209
groundY = null
199210
}
200211

src/main/resources/assets/flightassistant/lang/en_us.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ config.flightassistant:
224224
.: Fireworks
225225
explosive_alert: Alert when holding explosive fireworks
226226
lock_explosive: Lock explosive fireworks
227+
lock_obstacles: Lock fireworks near obstacles
227228

228229
menu.flightassistant:
229230
.: FlightAssistant setup screen

src/main/resources/assets/flightassistant/lang/ru_ru.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ config.flightassistant:
228228
.: Фейерверки
229229
explosive_alert: Предупреждать при взятии взрывоопасных фейерверков
230230
lock_explosive: Блокировать использование взрывоопасных фейерверков
231+
lock_obstacles: Блокировать использование фейерверков рядом с препятствиями
231232

232233
menu.flightassistant:
233234
.: Настройки FlightAssistant

0 commit comments

Comments
 (0)