Refactor mining to use async actions

This commit is contained in:
Gary Tierney
2017-09-17 03:10:15 +01:00
parent a54485e263
commit 0dc6879bdc
7 changed files with 221 additions and 233 deletions
+14
View File
@@ -0,0 +1,14 @@
plugin {
name = "mining-skill"
packageName = "org.apollo.game.plugin.skills.mining"
authors = [
"Graham",
"Mikey`",
"WH:II:DOW",
"Requa",
"Clifton",
"tlf30"
]
dependencies = ["api"]
}
+14
View File
@@ -0,0 +1,14 @@
name = "mining-skill"
package = "org.apollo.game.plugin.skills.mining"
authors = [
"Graham",
"Mikey`",
"WH:II:DOW",
"Requa",
"Clifton",
"tlf30"
]
[config]
srcDir = "src/"
testDir = "test/"
+12
View File
@@ -0,0 +1,12 @@
package org.apollo.game.plugin.skills.mining
enum class Gem(val id: Int, val chance: Int) {
UNCUT_SAPPHIRE(1623, 0),
UNCUT_EMERALD(1605, 0),
UNCUT_RUBY(1619, 0),
UNCUT_DIAMOND(1617, 0)
}
val GEMS = Gem.values()
fun lookupGem(id: Int): Gem? = GEMS.find { it.id == id }
@@ -0,0 +1,174 @@
import org.apollo.game.action.AsyncDistancedAction
import org.apollo.game.action.DistancedAction
import org.apollo.game.message.impl.ObjectActionMessage
import org.apollo.game.model.Position
import org.apollo.game.model.World
import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.Skill
import org.apollo.game.model.entity.obj.GameObject
import org.apollo.game.plugin.skills.mining.Ore
import org.apollo.game.plugin.skills.mining.PICKAXES
import org.apollo.game.plugin.skills.mining.Pickaxe
import org.apollo.game.plugin.skills.mining.lookupOreRock
import org.apollo.game.plugins.api.Definitions
import org.apollo.game.plugins.api.mining
import org.apollo.game.plugins.api.skills
import org.apollo.net.message.Message
import java.util.*
class MiningTarget(val objectId: Int, val position: Position, val ore: Ore) {
fun getObject(world: World): Optional<GameObject> {
val region = world.regionRepository.fromPosition(position)
val obj = region.findObject(position, objectId)
return obj
}
fun isSuccessful(mob: Player): Boolean {
val offset = if (ore.chanceOffset) 1 else 0
val percent = (ore.chance * mob.skills.mining.currentLevel + offset) * 100
return rand(100) < percent;
}
}
class MiningAction(player: Player, val tool: Pickaxe, val target: MiningTarget) : AsyncDistancedAction<Player>(
PULSES,
true,
player,
target.position,
ORE_SIZE
) {
companion object {
private val PULSES = 0
private val ORE_SIZE = 1;
fun pickaxeFor(player: Player): Pickaxe? {
return PICKAXES
.filter { it.level <= player.skills.mining.currentLevel }
.filter { player.equipment.contains(it.id) || player.inventory.contains(it.id) }
.sortedByDescending { it.level }
.firstOrNull()
}
/**
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
*/
fun start(message: ObjectActionMessage, player: Player, ore: Ore) {
val pickaxe = pickaxeFor(player)
if (pickaxe != null) {
val action = MiningAction(player, pickaxe, MiningTarget(message.id, message.position, ore))
player.startAction(action)
} else {
player.sendMessage("You do not have a pickaxe for which you have the level to use.")
}
message.terminate()
}
}
override fun start() {
mob.turnTo(position)
val level = mob.skills.mining.currentLevel
if (level < target.ore.level) {
mob.sendMessage("You do not have the required level to mine this rock.")
stop()
}
}
override suspend fun executeActionAsync() {
mob.sendMessage("You swing your pick at the rock.")
mob.playAnimation(tool.animation)
wait(tool.pulses)
val obj = target.getObject(mob.world)
if (!obj.isPresent) {
stop()
return
}
if (target.isSuccessful(mob)) {
if (mob.inventory.freeSlots() == 0) {
mob.inventory.forceCapacityExceeded()
stop()
return
}
if (mob.inventory.add(target.ore.id)) {
val oreName = Definitions.item(target.ore.id)?.name?.toLowerCase()
mob.sendMessage("You manage to mine some $oreName")
mob.skills.addExperience(Skill.MINING, target.ore.exp)
mob.world.expireObject(obj.get(), target.ore.objects[target.objectId]!!, target.ore.respawn)
stop()
return
}
}
}
}
class ExpiredProspectingAction : DistancedAction<Player> {
constructor(mob: Player, position: Position) : super(PROSPECT_PULSES, true, mob, position, ORE_SIZE)
companion object {
private val PROSPECT_PULSES = 0
private val ORE_SIZE = 1;
}
override fun executeAction() {
mob.sendMessage("There is currently no ore available in this rock.")
stop();
}
}
class ProspectingAction(val m: Player, val p: Position, val ore: Ore) : AsyncDistancedAction<Player>(PROSPECT_PULSES, true, m, p, ORE_SIZE) {
companion object {
private val PROSPECT_PULSES = 3
private val ORE_SIZE = 1;
/**
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
*/
fun start(message: ObjectActionMessage, player: Player, ore: Ore) {
val action = ProspectingAction(player, message.position, ore)
player.startAction(action)
message.terminate()
}
}
suspend override fun executeActionAsync() {
mob.sendMessage("You examine the rock for ores...")
mob.turnTo(position)
wait()
val oreName = Definitions.item(ore.id)?.name?.toLowerCase()
mob.sendMessage("This rock contains $oreName")
stop()
}
}
on { ObjectActionMessage::class }
.where { option == 1 }
.then {
val ore = lookupOreRock(id)
if (ore != null) {
MiningAction.start(this, it, ore)
}
}
on { ObjectActionMessage::class }
.where { option == 2 }
.then {
var ore = lookupOreRock(id)
if (ore != null) {
ProspectingAction.start(this, it, ore)
}
}
+155
View File
@@ -0,0 +1,155 @@
package org.apollo.game.plugin.skills.mining
import com.google.common.collect.Maps.asMap
/*
Thanks to Mikey` <http://www.rune-server.org/members/mikey%60/> for helping
to find some of the item/object IDs, minimum levels and experiences.
Thanks to Clifton <http://www.rune-server.org/members/clifton/> for helping
to find some of the expired object IDs.
*/
val CLAY_OBJECTS = mapOf(
2108 to 450,
2109 to 451,
14904 to 14896,
14905 to 14897
)
val COPPER_OBJECTS = mapOf(
11960 to 11555,
11961 to 11556,
11962 to 11557,
11936 to 11552,
11937 to 11553,
11938 to 11554,
2090 to 450,
2091 to 451,
14906 to 14898,
14907 to 14899,
14856 to 14832,
14857 to 14833,
14858 to 14834
)
val TIN_OBJECTS = mapOf(
11597 to 11555,
11958 to 11556,
11959 to 11557,
11933 to 11552,
11934 to 11553,
11935 to 11554,
2094 to 450,
2095 to 451,
14092 to 14894,
14903 to 14895
)
val IRON_OBJECTS = mapOf(
11954 to 11555,
11955 to 11556,
11956 to 11557,
2092 to 450,
2093 to 451,
14900 to 14892,
14901 to 14893,
14913 to 14915,
14914 to 14916
)
val COAL_OBJECTS = mapOf(
11963 to 11555,
11964 to 11556,
11965 to 11557,
11930 to 11552,
11931 to 11553,
11932 to 11554,
2096 to 450,
2097 to 451,
14850 to 14832,
14851 to 14833,
14852 to 14834
)
val SILVER_OBJECTS = mapOf (
11948 to 11555,
11949 to 11556,
11950 to 11557,
2100 to 450,
2101 to 451
)
val GOLD_OBJECTS = mapOf(
11951 to 11555,
11952 to 11556,
11953 to 11557,
2098 to 450,
2099 to 451
)
val MITHRIL_OBJECTS = mapOf(
11945 to 11555,
11946 to 11556,
11947 to 11557,
11942 to 11552,
11943 to 11553,
11944 to 11554,
2102 to 450,
2103 to 451,
14853 to 14832,
14854 to 14833,
14855 to 14834
)
val ADAMANT_OBJECTS = mapOf(
11939 to 11552,
11940 to 11553,
11941 to 11554,
2104 to 450,
2105 to 451,
14862 to 14832,
14863 to 14833,
14864 to 14834
)
val RUNITE_OBJECTS = mapOf(
2106 to 450,
2107 to 451,
14859 to 14832,
14860 to 14833,
14861 to 14834
)
/**
* Chance values thanks to: http://runescape.wikia.com/wiki/Talk:Mining#Mining_success_rate_formula
* Respawn times and xp thanks to: http://oldschoolrunescape.wikia.com/wiki/
*/
enum class Ore(val id: Int, val objects: Map<Int, Int>, val level: Int, val exp: Double, val respawn: Int, val chance: Double, val chanceOffset: Boolean) {
CLAY(434, CLAY_OBJECTS, 1, 5.0, 1, 0.0085, true),
COPPER(436, COPPER_OBJECTS, 1, 17.5, 4, 0.0085, true),
TIN(438, TIN_OBJECTS, 1, 17.5, 4, 0.0085, true),
IRON(440, IRON_OBJECTS, 15, 35.0, 9, 0.0085, true),
COAL(453, COAL_OBJECTS, 30, 50.0, 50, 0.004, false),
GOLD(444, GOLD_OBJECTS, 40, 65.0, 100, 0.003, false),
SILVER(442, SILVER_OBJECTS, 20, 40.0, 100, 0.0085, false),
MITHRIL(447, MITHRIL_OBJECTS, 55, 80.0, 200, 0.002, false),
ADAMANT(449, ADAMANT_OBJECTS, 70, 95.0, 800, 0.001, false),
RUNITE(451, RUNITE_OBJECTS, 85, 125.0, 1200, 0.0008, false)
}
val ORES = enumValues<Ore>()
fun lookupOre(id: Int): Ore? = ORES.find { it.id == id }
fun lookupOreRock(id: Int): Ore? {
for (ore in enumValues<Ore>()) {
for (rock in ore.objects) {
if (rock.key == id) {
return ore
}
}
}
return null
}
+23
View File
@@ -0,0 +1,23 @@
package org.apollo.game.plugin.skills.mining
import org.apollo.game.model.Animation;
enum class Pickaxe(val id: Int, val level: Int, val animation: Animation, val pulses: Int) {
RUNE(1275, 41, Animation(624), 3),
ADAMANT(1271, 31, Animation(628), 4),
MITHRIL(1273, 21, Animation(629), 5),
STEEL(1269, 1, Animation(627), 6),
ITRON(1267, 1, Animation(626), 7),
BRONZE(1265, 1, Animation(625), 8)
}
val PICKAXES = Pickaxe.values()
fun getPickaxes(): Array<Pickaxe> {
return PICKAXES
}
fun lookupPickaxe(id: Int): Pickaxe? = PICKAXES.find { it.id == id }