Skip to content

Instantly share code, notes, and snippets.

@jlandahl
Last active August 29, 2015 14:13
Show Gist options
  • Save jlandahl/4014fd7fd7ab95a5f512 to your computer and use it in GitHub Desktop.
Save jlandahl/4014fd7fd7ab95a5f512 to your computer and use it in GitHub Desktop.
EVE routing sample
object EveRouting extends App {
import EveMap.{SolarSystem, SolarSystemJump, loadSystems, loadJumps}
import scalax.collection.immutable.Graph
import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._
import scalax.collection.edge.Implicits._
// Load the EVE map
val systems: List[SolarSystem] = loadSystems
val jumps: List[SolarSystemJump] = loadJumps
// System lookups
val byId: Map[Int, SolarSystem] = systems.map(s => s.solarSystemId -> s).toMap
val byName: Map[String, SolarSystem] = systems.map(s => s.name -> byId(s.solarSystemId)).toMap
// Convenience variables
val jita = byName("Jita")
val amarr = byName("Amarr")
val dodixie = byName("Dodixie")
val thera = byName("Thera")
// Convert the jumps into graph edges
val mapEdges = jumps.map(jump => byId(jump.fromSolarSystemId) ~> byId(jump.toSolarSystemId))
// Convenience builder for Thera edges
def theraEdge(name: String) = List(thera ~> byName(name), byName(name) ~> thera)
// Setup a few of the current Thera entrances
val theraEdges = List("Ana", "Altrinur", "Jaswelu", "Adeel", "Trosquesere", "Anjedin").flatMap(theraEdge)
// Build the graph
val g = Graph.from(systems, mapEdges ++ theraEdges)
// Convenience - lookup node in the graph by SolarSystem
def n(s: SolarSystem): g.NodeT = g get s
type WeightingFunction = g.EdgeT => Double
val preferHighSec: WeightingFunction =
edge => {
val system = edge.nodes.tail.head.value
if (system.roundedSecurity <= 0.0)
10000.0 + (1.0 - system.security)
else if (system.roundedSecurity < 0.5)
5000.0 + (1.0 - system.security)
else
1.0 + (1.0 - system.security)
}
val fastest: WeightingFunction =
_ => 1.0
// String representation of a path
def pathString(path: g.Path): String =
path.nodes.map(_.value.name).mkString(" -> ")
// Calculate shortest path between two systems (if any), returning the string representation
def shortestPath(from: SolarSystem, to: SolarSystem, weighting: WeightingFunction = fastest): Option[String] =
n(from).shortestPathTo(n(to), weighting).map(pathString)
// print some routes
println(shortestPath(jita, amarr))
// Some(Jita -> Perimeter -> Urlen -> Sirppala -> Inaro -> Kaaputenen -> Niarja -> Madirmilire -> Ashab -> Amarr)
println(shortestPath(jita, dodixie))
// Some(Jita -> Niyabainen -> Tunttaras -> Nourvukaiken -> Tama -> Sujarento -> Onatoh -> Tannolen -> Tierijev -> Chantrousse -> Ourapheh -> Botane -> Dodixie)
println(shortestPath(jita, dodixie, preferHighSec))
// Some(Jita -> Perimeter -> Urlen -> Sirppala -> Anttiri -> Juunigaishi -> Uedama -> Sivala -> Hatakani -> Kassigainen -> Algogille -> Renyn -> Grinacanne -> Erme -> Botane -> Dodixie)
println(shortestPath(thera, jita))
// Some(Thera -> Anjedin -> Pimebeka -> Tash-Murkon Prime -> Bhizheba -> Romi -> Madirmilire -> Niarja -> Kaaputenen -> Inaro -> Sirppala -> Urlen -> Perimeter -> Jita)
println(shortestPath(thera, amarr))
// Some(Thera -> Anjedin -> Pimebeka -> Tash-Murkon Prime -> Bhizheba -> Amarr)
println(shortestPath(thera, dodixie))
// Some(Thera -> Trosquesere -> Nausschie -> Erme -> Botane -> Dodixie)
println(shortestPath(byName("Villore"), byName("Ana"), preferHighSec))
// Some(Villore -> Erme -> Grinacanne -> Renyn -> Algogille -> Kassigainen -> Hatakani -> Sivala -> Uedama -> Ikao -> Kamio -> Kaaputenen -> Niarja -> Madirmilire -> Ashab -> Amarr -> Sarum Prime -> Hama -> Bagodan -> Barira -> Yuhelia -> Maiah -> Jaswelu -> Ana)
println(shortestPath(byName("Villore"), byName("Ana")))
// Some(Villore -> Erme -> Nausschie -> Trosquesere -> Thera -> Ana)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment