mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-19 15:04:05 +09:00
no idea
Former-commit-id: 9738d12e5a468b71142745fbf0ce02fcf1ca623c Former-commit-id: a26b80a1990996d9c05b0909128c210e0f897312
This commit is contained in:
@@ -7,17 +7,16 @@ import java.util.HashSet
|
||||
/**
|
||||
* Created by minjaesong on 16-02-15.
|
||||
*/
|
||||
class Faction(factionName: String) {
|
||||
class Faction(name: String) : Comparable<Faction> {
|
||||
|
||||
lateinit var factionName: String
|
||||
var factionName: String = name
|
||||
lateinit var factionAmicable: HashSet<String>
|
||||
lateinit var factionNeutral: HashSet<String>
|
||||
lateinit var factionHostile: HashSet<String>
|
||||
lateinit var factionFearful: HashSet<String>
|
||||
var factionID: Long = generateUniqueID()
|
||||
var referenceID: Long = generateUniqueID()
|
||||
|
||||
init {
|
||||
this.factionName = factionName
|
||||
factionAmicable = HashSet<String>()
|
||||
factionNeutral = HashSet<String>()
|
||||
factionHostile = HashSet<String>()
|
||||
@@ -60,8 +59,18 @@ class Faction(factionName: String) {
|
||||
factionFearful.remove(faction)
|
||||
}
|
||||
|
||||
fun generateUniqueID(): Long {
|
||||
fun Long.abs() = if (this < 0) -this else this
|
||||
return HQRNG().nextLong().abs() // set new ID
|
||||
private fun generateUniqueID(): Long {
|
||||
var ret: Long
|
||||
do {
|
||||
ret = HQRNG().nextLong().or(0x80000000L).and(0xFFFFFFFFL) // guaranteed to be 2147483648..4294967295
|
||||
} while (FactionCodex.hasFaction(ret)) // check for collision
|
||||
return ret
|
||||
}
|
||||
|
||||
override fun equals(other: Any?) = referenceID == (other as Faction).referenceID
|
||||
override fun hashCode() = (referenceID - 0x80000000L).toInt()
|
||||
override fun toString() = "Faction, ID: $referenceID ($factionName)"
|
||||
override fun compareTo(other: Faction): Int = (this.referenceID - other.referenceID).toInt().sign()
|
||||
|
||||
fun Int.sign(): Int = if (this > 0) 1 else if (this < 0) -1 else this
|
||||
}
|
||||
|
||||
66
src/net/torvald/terrarum/gameactors/faction/FactionCodex.kt
Normal file
66
src/net/torvald/terrarum/gameactors/faction/FactionCodex.kt
Normal file
@@ -0,0 +1,66 @@
|
||||
package net.torvald.terrarum.gameactors.faction
|
||||
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-05-09.
|
||||
*/
|
||||
object FactionCodex {
|
||||
val factionContainer = ArrayList<Faction>()
|
||||
|
||||
fun hasFaction(ID: Long): Boolean =
|
||||
if (factionContainer.size == 0)
|
||||
false
|
||||
else
|
||||
factionContainer.binarySearch(ID) >= 0
|
||||
|
||||
fun addFaction(faction: Faction) {
|
||||
if (hasFaction(faction.referenceID))
|
||||
throw RuntimeException("Faction with ID ${faction.referenceID} already exists.")
|
||||
factionContainer.add(faction)
|
||||
insertionSortLastElem(factionContainer) // we can do this as we are only adding single actor
|
||||
}
|
||||
|
||||
fun getFactionByID(ID: Long): Faction {
|
||||
if (factionContainer.size == 0) throw IllegalArgumentException("Faction with ID $ID does not exist.")
|
||||
|
||||
val index = factionContainer.binarySearch(ID)
|
||||
if (index < 0)
|
||||
throw IllegalArgumentException("Faction with ID $ID does not exist.")
|
||||
else
|
||||
return factionContainer[index]
|
||||
}
|
||||
|
||||
private fun insertionSortLastElem(arr: ArrayList<Faction>) {
|
||||
var x: Faction
|
||||
var j: Int
|
||||
var index: Int = arr.size - 1
|
||||
x = arr[index]
|
||||
j = index - 1
|
||||
while (j > 0 && arr[j] > x) {
|
||||
arr[j + 1] = arr[j]
|
||||
j -= 1
|
||||
}
|
||||
arr[j + 1] = x
|
||||
}
|
||||
|
||||
private fun ArrayList<Faction>.binarySearch(ID: Long): Int {
|
||||
// code from collections/Collections.kt
|
||||
var low = 0
|
||||
var high = factionContainer.size - 1
|
||||
|
||||
while (low <= high) {
|
||||
val mid = (low + high).ushr(1) // safe from overflows
|
||||
val midVal = get(mid)
|
||||
|
||||
if (ID > midVal.referenceID)
|
||||
low = mid + 1
|
||||
else if (ID < midVal.referenceID)
|
||||
high = mid - 1
|
||||
else
|
||||
return mid // key found
|
||||
}
|
||||
return -(low + 1) // key not found
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ object FactionFactory {
|
||||
val jsonObj = JsonFetcher.readJson(JSONPATH + filename)
|
||||
val factionObj = Faction(jsonObj.get("factionname").asString)
|
||||
|
||||
|
||||
jsonObj.get("factionamicable").asJsonArray.forEach { s -> factionObj.addFactionAmicable(s.asString) }
|
||||
jsonObj.get("factionneutral").asJsonArray.forEach { s -> factionObj.addFactionNeutral(s.asString) }
|
||||
jsonObj.get("factionhostile").asJsonArray.forEach { s -> factionObj.addFactionHostile(s.asString) }
|
||||
|
||||
Reference in New Issue
Block a user