Skip to content

Commit cfd3585

Browse files
committed
fix: optimize some math operation
1 parent 7a32102 commit cfd3585

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

api/src/main/java/kr/toxicity/model/api/bone/BoneTagRegistry.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package kr.toxicity.model.api.bone;
88

99
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
1011

1112
import java.util.*;
1213

@@ -35,12 +36,23 @@ public void addTag(@NotNull BoneTag tag) {
3536
}
3637

3738
/**
38-
* Gets bone tag by tag name
39+
* Gets a bone tag by its name wrapped in an Optional.
3940
* @param tag tag name
40-
* @return optional tag
41+
* @return bone tag
42+
* @since 1.15.2
4143
*/
4244
public @NotNull Optional<BoneTag> byTagName(@NotNull String tag) {
43-
return Optional.ofNullable(byName.get(tag));
45+
return Optional.ofNullable(byTagNameOrNull(tag));
46+
}
47+
48+
/**
49+
* Gets a bone tag by its name.
50+
* @param tag tag name
51+
* @return bone tag or null
52+
* @since 2.0.2
53+
*/
54+
public @Nullable BoneTag byTagNameOrNull(@NotNull String tag) {
55+
return byName.get(tag);
4456
}
4557

4658
/**
@@ -50,16 +62,17 @@ public void addTag(@NotNull BoneTag tag) {
5062
*/
5163
public @NotNull BoneName parse(@NotNull String rawName) {
5264
rawName = rawName.toLowerCase(Locale.ROOT);
53-
var tagArray = List.of(rawName.split("_"));
54-
if (tagArray.size() < 2) return new BoneName(Collections.emptySet(), rawName, rawName);
55-
var maxSize = tagArray.size() - 1;
65+
var tagArray = rawName.split("_");
66+
if (tagArray.length < 2) return new BoneName(Collections.emptySet(), rawName, rawName);
67+
var tagList = List.of(tagArray);
68+
var maxSize = tagList.size() - 1;
5669
var set = new HashSet<BoneTag>(maxSize);
57-
for (String s : tagArray) {
58-
var tag = byTagName(s).orElse(null);
70+
for (String s : tagList) {
71+
var tag = byTagNameOrNull(s);
5972
if (tag != null && set.size() < maxSize) {
6073
set.add(tag);
61-
} else return new BoneName(Collections.unmodifiableSet(set), String.join("_", tagArray.subList(set.size(), tagArray.size())), rawName);
74+
} else return new BoneName(Collections.unmodifiableSet(set), String.join("_", tagList.subList(set.size(), tagList.size())), rawName);
6275
}
63-
return new BoneName(Collections.unmodifiableSet(set), String.join("_", tagArray.subList(set.size(), tagArray.size())), rawName);
76+
return new BoneName(Collections.unmodifiableSet(set), String.join("_", tagList.subList(set.size(), tagList.size())), rawName);
6477
}
6578
}

api/src/main/java/kr/toxicity/model/api/platform/PlatformLocation.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import org.jetbrains.annotations.NotNull;
1010

11+
import static java.lang.Math.*;
12+
1113
/**
1214
* Represents a location in the underlying platform.
1315
* <p>
@@ -77,4 +79,29 @@ public interface PlatformLocation extends PlatformRegionHolder {
7779
* @since 2.0.0
7880
*/
7981
@NotNull PlatformLocation add(double x, double y, double z);
82+
83+
/**
84+
* Calculates the distance between this location and another location.
85+
*
86+
* @param other the other location
87+
* @return the distance
88+
* @since 2.0.2
89+
*/
90+
default double distance(@NotNull PlatformLocation other) {
91+
return sqrt(distanceSquared(other));
92+
}
93+
94+
/**
95+
* Calculates the squared distance between this location and another location.
96+
*
97+
* @param other the other location
98+
* @return the squared distance
99+
* @since 2.0.2
100+
*/
101+
default double distanceSquared(@NotNull PlatformLocation other) {
102+
var x = x() - other.x();
103+
var y = y() - other.y();
104+
var z = z() - other.z();
105+
return fma(x, x, fma(y, y, z * z));
106+
}
80107
}

api/src/main/java/kr/toxicity/model/api/util/EntityUtil.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ public static boolean canSee(@NotNull PlatformLocation player, @NotNull Platform
7979
if (!manager.sightTrace()) return true;
8080
else if (!player.world().equals(target.world())) return false;
8181

82-
var d = distance(player, target);
82+
var d = player.distance(target);
8383
if (d > manager.maxSight()) return false;
8484
else if (d <= manager.minSight()) return true;
8585

86-
var t = PI - abs(atan(d)) * 2;
86+
var t = fma(-abs(atan(d)), 2, PI);
8787
var ty = t + Y_RENDER_THRESHOLD;
8888
var tz = t + X_RENDER_THRESHOLD;
8989
return isInDegree(player, target, ty, tz);
@@ -99,14 +99,10 @@ public static boolean canSee(@NotNull PlatformLocation player, @NotNull Platform
9999
*/
100100
public static boolean isCustomNameVisible(@NotNull PlatformLocation player, @NotNull PlatformLocation target) {
101101
if (!player.world().equals(target.world())) return false;
102-
if (distance(player, target) > 5) return false;
102+
if (player.distanceSquared(target) > 25) return false; // 5 ^ 2
103103
return isInPoint(player, target);
104104
}
105105

106-
private static double distance(@NotNull PlatformLocation a, @NotNull PlatformLocation b) {
107-
return sqrt(pow(a.x() - b.x(), 2) + pow(a.z() - b.z(), 2));
108-
}
109-
110106
/**
111107
* Checks if a target entity is directly in the player's crosshair (point of view).
112108
*
@@ -129,6 +125,6 @@ private static boolean isInDegree(@NotNull PlatformLocation player, @NotNull Pla
129125

130126
var ry = abs(atan2(dy, sqrt(MathUtil.fma(dz, dz, dx * dx))) - playerPitch);
131127
var rz = abs(atan2(-dx, dz) - playerYaw);
132-
return (ry <= ty || ry >= PI * 2 - ty) && (rz <= tz || rz >= PI * 2 - tz);
128+
return (ry <= ty || ry >= TAU - ty) && (rz <= tz || rz >= TAU - tz);
133129
}
134130
}

0 commit comments

Comments
 (0)