-
-
Save nbness2/5cf8f94139c1cf4a7b078e932ced380b to your computer and use it in GitHub Desktop.
DropTables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Author nbness2 | |
*/ | |
class ItemPair: RandomTable<Int> { | |
constructor(itemIds: Array<Int>, amountRange: Pair<Int, Int>): super(itemIds) { | |
if (itemIds.isEmpty()) | |
throw ArrayStoreException("Cannot initialize ItemPair with empty itemid array") | |
this.amountRange = RandomRange(minOf(amountRange.first, amountRange.second), maxOf(amountRange.first, amountRange.second), true) | |
} | |
private val amountRange: RandomRange<Int> | |
private val itemAmount: Int | |
get(): Int = this.amountRange.pick() | |
private val itemId: Int | |
get(): Int = this.pick() | |
val newItem: Item | |
get(): Item = Item(this.itemId, this.itemAmount) | |
constructor(itemId: Int, amountRange: Pair<Int, Int>): this(arrayOf(itemId), amountRange) | |
constructor(itemId: Int, itemAmount: Int): this(itemId, itemAmount to itemAmount) | |
constructor(itemIds: Array<Int>, itemAmount: Int): this(itemIds, itemAmount to itemAmount) | |
constructor(itemIds: IntArray, itemAmount: Int): this(itemIds.toTypedArray(), itemAmount to itemAmount) | |
constructor(itemIds: IntArray, amountRange: Pair<Int, Int>): this(itemIds.toTypedArray(), amountRange) | |
} | |
class Bracket(val bracketName: String, val announceDrop: Boolean, vararg items: ItemPair) : RandomTable<ItemPair>(items as Array<ItemPair>) { | |
override fun toString(): String = "Bracket{$bracketName}" | |
fun pickItem(): Item { // No modifier because this inherits RandomTable which is not weight-biased. | |
return this.pick().newItem | |
} | |
} | |
private inline fun Array<out Pair<Int, Bracket>>.toArrays(): Pair<IntArray, Array<Bracket>> { | |
return this.map{p -> p.first}.toIntArray() to this.map{p -> p.second}.toTypedArray() | |
} | |
class DropTable: WeightedTable<Bracket> { | |
val tableName: String | |
constructor(tableName: String, vararg items: Pair<Int, Bracket>): super(items.toArrays().second, items.toArrays().first){ | |
this.tableName = tableName | |
} | |
fun pick(pickAmount: Int, modifier: Int=0): Any { | |
if (pickAmount <= 1) return this.pick( modifier) | |
return this.pickMap(pickAmount, modifier) | |
} | |
fun pickItem(modifier: Int=0): Item { return this.pick(modifier).pickItem() } | |
override fun toString(): String = "DropTable{$tableName}" | |
} | |
public infix fun Int.amt(other: Int): ItemPair = ItemPair(this, other) | |
public infix fun Int.amt(other: Pair<Int, Int>): ItemPair = ItemPair(this, other) | |
public infix fun IntArray.amt(other: Int): ItemPair = ItemPair(this, other) | |
public infix fun IntArray.amt(other: Pair<Int, Int>): ItemPair = ItemPair(this, other) | |
public infix fun IntProgression.amt(other: Int): ItemPair = ItemPair(this.toArray(), other) | |
public infix fun IntProgression.amt(other: Pair<Int, Int>): ItemPair = ItemPair(this.toArray(), other) | |
public fun IntProgression.toArray(): IntArray = this.map{i -> i}.toIntArray() | |
fun main(args: Array<String>) { | |
val testItemPair = (636 .. 654 step 2) amt 1 | |
for (run in 0 .. 100) | |
println(testItemPair.newItem) | |
val meleeWeapons = | |
DropTable( | |
"Melee Weapons", | |
104 to Bracket("Common", false, | |
// Bronze weapons | |
intArrayOf(1205, 1237, 1265, 1277, 1291, 1307, 1321, 1337, 1351, 1375, 1422, 3095, 3190) amt 1 | |
), | |
86 to Bracket("Common", false, | |
// Iron weapons | |
intArrayOf(1203, 1239, 1267, 1279, 1293, 1309, 1323, 1335, 1349, 1363, 1420, 3096, 3192) amt 1 | |
), | |
71 to Bracket("Common", false, | |
// Steel weapons | |
intArrayOf(1207, 1241, 1269, 1281, 1295, 1311, 1325, 1339, 1353, 1365, 1424, 3097, 3194) amt 1 | |
), | |
57 to Bracket("Common", false, | |
// Black weapons | |
intArrayOf(1217, 1283, 1297, 1313, 1327, 1341, 1361, 1367, 1426, 3098, 3196, 4580) amt 1 | |
), | |
44 to Bracket("Common", false, | |
// Mithril weapons | |
intArrayOf(1209, 1243, 1273, 1285, 1299, 1315, 1329, 1343, 1355, 1369, 1428, 3099, 3198) amt 1 | |
), | |
32 to Bracket("Uncommon", false, | |
// Adamant weapons | |
intArrayOf(1211, 1245, 1271, 1287, 1301, 1317, 1331, 1357, 1371, 1430, 3100, 3200) amt 1 | |
), | |
21 to Bracket("Uncommon", false, | |
// Rune weapons | |
intArrayOf(1213, 1247, 1275, 1289, 1303, 1319, 1333, 1347, 1359, 1373, 1432,3101, 3202) amt 1 | |
), | |
10 to Bracket("Uncommon", false, | |
// Dragon weapons (no pickaxe or axe) | |
intArrayOf(1215, 1249, 1305, 1377, 1434, 3204, 4587, 7158) amt 1 | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment