Advertisement
fiveriverflow

FutureRoute

Aug 27th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.62 KB | None | 0 0
  1. package gg.rsmod.game.model.path
  2.  
  3. import com.google.common.util.concurrent.ThreadFactoryBuilder
  4. import gg.rsmod.game.model.MovementQueue
  5. import mu.KLogging
  6. import java.util.concurrent.Executors
  7.  
  8. /**
  9.  * Represents a [Route] that will be returned some time in the future.
  10.  *
  11.  * @param strategy the [PathFindingStrategy] that will be used to get the path
  12.  * for our route.
  13.  *
  14.  * @param stepType the type of [MovementQueue.StepType] that will be used for
  15.  * the route. Since the route will be handled in the future, there's no guarantee
  16.  * the pawn using it will still have the same state for its [MovementQueue.StepType],
  17.  * which is why we store it on our [FutureRoute].
  18.  *
  19.  * @param detectCollision if true, the [MovementQueue.Step]s of the route will
  20.  * take collision detection into account.
  21.  *
  22.  * @author Tom <rspsmods@gmail.com>
  23.  */
  24. class FutureRoute private constructor(val strategy: PathFindingStrategy, val stepType: MovementQueue.StepType, val detectCollision: Boolean) {
  25.  
  26.     /**
  27.      * This flag lets us know if the [FutureRoute.route] has finished.
  28.      */
  29.     @Volatile var completed = false
  30.  
  31.     lateinit var route: Route
  32.  
  33.     companion object : KLogging() {
  34.  
  35.         /**
  36.          * Future routes are handled on a separate thread. This should be defined
  37.          * else-where, but for now we'll keep it here!
  38.          */
  39.         private val executor = Executors.newSingleThreadExecutor(ThreadFactoryBuilder().setNameFormat("pathfinding-thread").setUncaughtExceptionHandler { t, e -> logger.error("Error with thread $t", e) }.build())
  40.  
  41.         /**
  42.          * Creates a [FutureRoute] with the given parameters and executes it on
  43.          * our [executor] service. When the [Route] is returned, [FutureRoute.completed]
  44.          * is set to true.
  45.          *
  46.          * @param strategy the [PathFindingStrategy] that will be used to get
  47.          * the path for our route.
  48.          *
  49.          * @param request the properties for our path to take.
  50.          *
  51.          * @param stepType the type of [MovementQueue.StepType] that will be
  52.          * used for the route.
  53.          *
  54.          * @param detectCollision if true, the [MovementQueue.Step]s in the
  55.          * route will detect collision.
  56.          */
  57.         fun of(strategy: PathFindingStrategy, request: PathRequest, stepType: MovementQueue.StepType, detectCollision: Boolean): FutureRoute {
  58.             val future = FutureRoute(strategy, stepType, detectCollision)
  59.             executor.execute {
  60.                 future.route = strategy.calculateRoute(request)
  61.                 future.completed = true
  62.             }
  63.             return future
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement