Skip to content

Commit b0dc794

Browse files
committed
Improved the data types of NpcPatrolMode slightly to more clearly represent intent
1 parent e7496db commit b0dc794

1 file changed

Lines changed: 44 additions & 47 deletions

File tree

src/main/java/com/projectswg/holocore/resources/support/npc/ai/NpcPatrolMode.kt

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,98 +38,96 @@ import kotlin.math.ceil
3838
* AI object that patrols the specified route
3939
*/
4040
class NpcPatrolMode(obj: AIObject, waypoints: List<ResolvedPatrolWaypoint>) : NpcMode(obj) {
41-
42-
private val waypoints: MutableList<NavigationPoint>
43-
41+
42+
private val waypoints: List<NavigationPoint>
43+
4444
init {
4545
var waypointBuilder = waypoints
4646
waypointBuilder = ArrayList(waypointBuilder)
47-
47+
4848
if (waypointBuilder.isNotEmpty() && waypointBuilder[0].patrolType == PatrolType.FLIP) {
4949
waypointBuilder.addAll(waypointBuilder.reversed())
5050
} else if (waypointBuilder.isNotEmpty() && waypointBuilder[0].patrolType == PatrolType.LOOP) {
5151
waypointBuilder.add(waypointBuilder[0])
5252
}
53-
53+
5454
if (waypointBuilder.isEmpty()) {
55-
this.waypoints = ArrayList<NavigationPoint>(128)
55+
this.waypoints = emptyList<NavigationPoint>()
5656
} else {
5757
this.waypoints = ArrayList<NavigationPoint>(128)
5858
for (i in 1 until waypointBuilder.size) {
5959
val source = waypointBuilder[i - 1]
6060
val destination = waypointBuilder[i]
6161
this.waypoints.addAll(NavigationPoint.from(source.parent, source.location, destination.parent, destination.location, walkSpeed))
62-
if (destination.delay > 0)
63-
this.waypoints.addAll(NavigationPoint.nop(this.waypoints[this.waypoints.size - 1], destination.delay.toInt() - 1))
62+
if (destination.delay > 0) this.waypoints.addAll(NavigationPoint.nop(this.waypoints[this.waypoints.size - 1], destination.delay.toInt() - 1))
6463
}
6564
this.waypoints.addAll(NavigationPoint.from(waypointBuilder[waypointBuilder.size - 1].parent, waypointBuilder[waypointBuilder.size - 1].location, waypointBuilder[0].parent, waypointBuilder[0].location, walkSpeed))
6665
}
6766
}
68-
67+
6968
override suspend fun onModeStart() {
7069
val route = calculateRouteOffset(calculateInitialRoutePoints())
71-
70+
7271
ai.moveVia(route, loop = true)
7372
}
74-
73+
7574
override suspend fun onModeLoop() {
7675
throw CancellationException() // No loop necessary
7776
}
7877

7978
private fun calculateInitialRoutePoints(): List<NavigationPoint> {
80-
val compiledWaypoints: MutableList<NavigationPoint>
81-
if (waypoints.isNotEmpty()) {
82-
var index = 0
83-
var closestDistance = waypoints[0].distanceTo(ai)
84-
for (i in 1 until waypoints.size) {
85-
if (waypoints[i].isNoOperation)
86-
continue
87-
88-
val distance = waypoints[i].distanceTo(ai)
89-
if (distance < closestDistance) {
90-
closestDistance = distance
91-
index = i
92-
}
93-
}
94-
if (closestDistance >= 2) {
95-
ai.activeMode = NpcNavigateMode(ai, waypoints[index])
96-
throw CancellationException()
97-
}
98-
compiledWaypoints = ArrayList(waypoints.size)
99-
for (i in index until waypoints.size) {
100-
compiledWaypoints.add(waypoints[i])
101-
}
102-
for (i in 0 until index) {
103-
compiledWaypoints.add(waypoints[i])
79+
if (waypoints.isEmpty()) return emptyList()
80+
81+
var index = 0
82+
var closestDistance = waypoints[0].distanceTo(ai)
83+
for (i in 1 until waypoints.size) {
84+
if (waypoints[i].isNoOperation) continue
85+
86+
val distance = waypoints[i].distanceTo(ai)
87+
if (distance < closestDistance) {
88+
closestDistance = distance
89+
index = i
10490
}
105-
} else {
106-
compiledWaypoints = waypoints
91+
}
92+
if (closestDistance >= 2) {
93+
ai.activeMode = NpcNavigateMode(ai, waypoints[index])
94+
throw CancellationException()
95+
}
96+
val compiledWaypoints = ArrayList<NavigationPoint>(waypoints.size)
97+
for (i in index until waypoints.size) {
98+
compiledWaypoints.add(waypoints[i])
99+
}
100+
for (i in 0 until index) {
101+
compiledWaypoints.add(waypoints[i])
107102
}
108103
return compiledWaypoints
109104
}
110-
105+
111106
private fun calculateRouteOffset(compiledWaypoints: List<NavigationPoint>): List<NavigationPoint> {
112107
val spawner = spawner ?: throw CancellationException()
113108
val spacing = 3.0
114109
val position = spawner.npcs.indexOf(ai)
115110
var offsetX = 0.0
116111
var offsetZ = 0.0
117112
assert(position != -1)
118-
113+
119114
when (spawner.patrolFormation) {
120-
NpcStaticSpawnLoader.PatrolFormation.NONE -> {}
115+
NpcStaticSpawnLoader.PatrolFormation.NONE -> {}
121116
NpcStaticSpawnLoader.PatrolFormation.COLUMN -> {
122117
offsetX = if (position % 2 == 0) 0.0 else spacing
123118
offsetZ = -(spacing * ceil((position - 1) / 2.0))
124119
}
125-
NpcStaticSpawnLoader.PatrolFormation.WEDGE -> {
120+
121+
NpcStaticSpawnLoader.PatrolFormation.WEDGE -> {
126122
offsetX = spacing * ceil(position / 2.0) * (if (position % 2 == 0) -1 else 1)
127123
offsetZ = -offsetX
128124
}
129-
NpcStaticSpawnLoader.PatrolFormation.LINE -> {
125+
126+
NpcStaticSpawnLoader.PatrolFormation.LINE -> {
130127
offsetX = spacing * ceil(position / 2.0) * (if (position % 2 == 0) -1 else 1)
131128
}
132-
NpcStaticSpawnLoader.PatrolFormation.BOX -> {
129+
130+
NpcStaticSpawnLoader.PatrolFormation.BOX -> {
133131
offsetX = when (position) {
134132
0, 1, 2 -> position * 3.0
135133
3 -> 0.0
@@ -143,11 +141,10 @@ class NpcPatrolMode(obj: AIObject, waypoints: List<ResolvedPatrolWaypoint>) : Np
143141
}
144142
}
145143
}
146-
144+
147145
val offsetWaypoints = ArrayList<NavigationPoint>(compiledWaypoints.size)
148-
for (wp in compiledWaypoints)
149-
offsetWaypoints.add(NavigationPoint(wp.parent, Location.builder(wp.location).translatePosition(offsetX, 0.0, offsetZ).build(), wp.speed))
146+
for (wp in compiledWaypoints) offsetWaypoints.add(NavigationPoint(wp.parent, Location.builder(wp.location).translatePosition(offsetX, 0.0, offsetZ).build(), wp.speed))
150147
return offsetWaypoints
151148
}
152-
149+
153150
}

0 commit comments

Comments
 (0)