mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-17 05:54:05 +09:00
@@ -7,6 +7,9 @@ import java.util.HashSet
|
||||
/**
|
||||
* Created by minjaesong on 16-02-15.
|
||||
*/
|
||||
|
||||
typealias FactionID = Int
|
||||
|
||||
class Faction(name: String) : Comparable<Faction> {
|
||||
|
||||
var factionName: String = name
|
||||
@@ -14,7 +17,7 @@ class Faction(name: String) : Comparable<Faction> {
|
||||
lateinit var factionNeutral: HashSet<String>
|
||||
lateinit var factionHostile: HashSet<String>
|
||||
lateinit var factionFearful: HashSet<String>
|
||||
var referenceID: Long = generateUniqueID()
|
||||
var referenceID: FactionID = generateUniqueID()
|
||||
|
||||
init {
|
||||
factionAmicable = HashSet<String>()
|
||||
@@ -59,10 +62,11 @@ class Faction(name: String) : Comparable<Faction> {
|
||||
factionFearful.remove(faction)
|
||||
}
|
||||
|
||||
private fun generateUniqueID(): Long {
|
||||
var ret: Long
|
||||
/** Valid range: -2147483648..-1 (all the negative number) */
|
||||
private fun generateUniqueID(): Int {
|
||||
var ret: Int
|
||||
do {
|
||||
ret = HQRNG().nextLong().or(0x80000000L).and(0xFFFFFFFFL) // guaranteed to be 2147483648..4294967295
|
||||
ret = HQRNG().nextInt(2147483647).plus(1).unaryMinus()
|
||||
} while (FactionCodex.hasFaction(ret)) // check for collision
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.*
|
||||
object FactionCodex {
|
||||
val factionContainer = ArrayList<Faction>()
|
||||
|
||||
fun hasFaction(ID: Long): Boolean =
|
||||
fun hasFaction(ID: FactionID): Boolean =
|
||||
if (factionContainer.size == 0)
|
||||
false
|
||||
else
|
||||
@@ -22,7 +22,7 @@ object FactionCodex {
|
||||
insertionSortLastElem(factionContainer) // we can do this as we are only adding single actor
|
||||
}
|
||||
|
||||
fun getFactionByID(ID: Long): Faction {
|
||||
fun getFactionByID(ID: FactionID): Faction {
|
||||
if (factionContainer.size == 0) throw IllegalArgumentException("Faction with ID $ID does not exist.")
|
||||
|
||||
val index = factionContainer.binarySearch(ID)
|
||||
@@ -45,7 +45,7 @@ object FactionCodex {
|
||||
arr[j + 1] = x
|
||||
}
|
||||
|
||||
private fun ArrayList<Faction>.binarySearch(ID: Long): Int {
|
||||
private fun ArrayList<Faction>.binarySearch(ID: FactionID): Int {
|
||||
// code from collections/Collections.kt
|
||||
var low = 0
|
||||
var high = factionContainer.size - 1
|
||||
|
||||
Reference in New Issue
Block a user