mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 16:49:11 +00:00
Refactor mining to use async actions
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
name = "mining-skill"
|
||||
package = "org.apollo.game.plugin.skills.mining"
|
||||
authors = [
|
||||
"Graham",
|
||||
"Mikey`",
|
||||
"WH:II:DOW",
|
||||
"Requa",
|
||||
"Clifton",
|
||||
"tlf30"
|
||||
]
|
||||
|
||||
[config]
|
||||
srcDir = "src/"
|
||||
testDir = "test/"
|
||||
@@ -1,12 +0,0 @@
|
||||
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 }
|
||||
@@ -1,200 +0,0 @@
|
||||
import org.apollo.cache.def.ItemDefinition
|
||||
import org.apollo.cache.def.ObjectDefinition
|
||||
import org.apollo.game.action.DistancedAction
|
||||
import org.apollo.game.message.impl.ObjectActionMessage
|
||||
import org.apollo.game.model.Animation
|
||||
import org.apollo.game.model.Position
|
||||
import org.apollo.game.model.entity.Entity
|
||||
import org.apollo.game.model.entity.EquipmentConstants
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.model.entity.Skill
|
||||
import org.apollo.game.model.entity.obj.StaticGameObject
|
||||
import org.apollo.game.plugin.skills.mining.*
|
||||
import org.apollo.game.scheduling.ScheduledTask
|
||||
import org.apollo.game.scheduling.Scheduler
|
||||
import java.util.*
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class MiningAction(val player: Player, val objectID: Int, val p: Position, val ore: Ore) : DistancedAction<Player>(PULSES, true, player, p, ORE_SIZE) {
|
||||
|
||||
companion object {
|
||||
private val PULSES = 0
|
||||
private val ORE_SIZE = 1;
|
||||
}
|
||||
|
||||
private var counter: Int = 0
|
||||
private var started: Boolean = false
|
||||
private val rand: Random = Random()
|
||||
|
||||
override fun executeAction() {
|
||||
val level = mob.skillSet.getSkill(Skill.MINING).currentLevel
|
||||
val pickaxe = findPickaxe()
|
||||
|
||||
|
||||
//check that our pick can mine the ore
|
||||
if (pickaxe == null || level < pickaxe.level) {
|
||||
mob.sendMessage("You do not have a pickaxe for which you have the level to use.")
|
||||
stop()
|
||||
return
|
||||
}
|
||||
|
||||
//check that we can mine the ore
|
||||
if (level < ore.level) {
|
||||
mob.sendMessage("You do not have the required level to mine this rock.")
|
||||
stop()
|
||||
return
|
||||
}
|
||||
|
||||
//start the process of mining
|
||||
if (started) {
|
||||
if (counter == 0) {
|
||||
if (!miningSuccessful(ore.chance, ore.chanceOffset, level)) {
|
||||
mine(pickaxe)
|
||||
return //We did not mine the ore... Keep going
|
||||
}
|
||||
//Check inv capacity
|
||||
if (mob.inventory.freeSlots() == 0) {
|
||||
mob.inventory.forceCapacityExceeded()
|
||||
stop()
|
||||
return
|
||||
}
|
||||
if (mob.inventory.add(ore.id)) {
|
||||
//TODO: Use lookup from utils once it has a lookup function for IDs
|
||||
val oreName = ItemDefinition.lookup(ore.id).name.toLowerCase();
|
||||
mob.sendMessage("You managed to mine some " + oreName + ".")
|
||||
mob.skillSet.addExperience(Skill.MINING, ore.exp)
|
||||
//Expire ore
|
||||
var rockEntity: StaticGameObject? = null
|
||||
val region = mob.world.regionRepository.fromPosition(position)
|
||||
val entities = region.getEntities(position)
|
||||
for (entity: Entity in entities) {
|
||||
if (entity is StaticGameObject) {
|
||||
if (entity.id == objectID) {
|
||||
rockEntity = entity
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rockEntity == null) { //Mining entity not found at location...
|
||||
stop()
|
||||
return
|
||||
}
|
||||
//Get ID of exipred ore
|
||||
val expiredObjectID = ore.objects.get(objectID);
|
||||
val expiredRockEntity = StaticGameObject(mob.world, expiredObjectID!!, position, rockEntity!!.type, rockEntity!!.orientation)
|
||||
//add task to remove normal ore and replace with depleted
|
||||
mob.world.schedule(object: ScheduledTask(0, true) {
|
||||
override fun execute() {
|
||||
//Replace normal ore with expired ore
|
||||
region.removeEntity(rockEntity);
|
||||
region.addEntity(expiredRockEntity)
|
||||
this.stop() //Makes task run once
|
||||
}
|
||||
})
|
||||
//add task to respawn normal ore
|
||||
mob.world.schedule(object: ScheduledTask(ore.respawn, false) {
|
||||
override fun execute() {
|
||||
//Replace expired ore with normal ore
|
||||
region.removeEntity(expiredRockEntity)
|
||||
region.addEntity(rockEntity);
|
||||
this.stop() //Makes task run once
|
||||
}
|
||||
})
|
||||
} //If we did not add to inv, the action will still stop
|
||||
stop(); //Force this action to stop after we are done
|
||||
}
|
||||
counter -= 1
|
||||
} else {
|
||||
started = true
|
||||
mine(pickaxe)
|
||||
}
|
||||
}
|
||||
|
||||
private fun findPickaxe(): Pickaxe? {
|
||||
for (pick in getPickaxes()) {
|
||||
if (pick!!.level > mob.skillSet.getSkill(Skill.MINING).currentLevel) {
|
||||
continue;
|
||||
}
|
||||
val weaponSlot = mob.equipment.get(EquipmentConstants.WEAPON)
|
||||
if (weaponSlot != null && weaponSlot.id == pick.id) {
|
||||
return pick;
|
||||
} else if (mob.inventory.contains(pick.id)) {
|
||||
return pick;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private fun mine(pickaxe: Pickaxe) {
|
||||
mob.sendMessage("You swing your pick at the rock.")
|
||||
mob.playAnimation(pickaxe.animation)
|
||||
counter = pickaxe.pulses
|
||||
mob.turnTo(position)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the chance of mining being successful.
|
||||
* Algorithm comes from: http://runescape.wikia.com/wiki/Talk:Mining#Mining_success_rate_formula
|
||||
*/
|
||||
private fun miningSuccessful(oreChance: Double, oreChanceOffset: Boolean, playerLevel: Int): Boolean {
|
||||
val percent: Double
|
||||
if (oreChanceOffset) {
|
||||
percent = (oreChance * playerLevel + 1) * 100
|
||||
} else {
|
||||
percent = (oreChance * playerLevel) * 100
|
||||
}
|
||||
return rand.nextInt(100) < percent;
|
||||
}
|
||||
}
|
||||
|
||||
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) : DistancedAction<Player>(PROSPECT_PULSES, true, m, p, ORE_SIZE) {
|
||||
|
||||
companion object {
|
||||
private val PROSPECT_PULSES = 3
|
||||
private val ORE_SIZE = 1;
|
||||
}
|
||||
|
||||
var started = false;
|
||||
|
||||
override fun executeAction() {
|
||||
if (started) {
|
||||
val oreName = ItemDefinition.lookup(ore.id).name.toLowerCase()
|
||||
mob.sendMessage("This rock contains " + oreName + ".")
|
||||
stop();
|
||||
} else {
|
||||
started = true
|
||||
mob.sendMessage("You examine the rock for ores...")
|
||||
mob.turnTo(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
on {ObjectActionMessage::class}
|
||||
.where {option == 1}
|
||||
.then {
|
||||
if (lookupOreRock(id) != null) {
|
||||
it.startAction(MiningAction(it, id, this.position, lookupOreRock(id)!!))
|
||||
}
|
||||
}
|
||||
|
||||
on {ObjectActionMessage::class}
|
||||
.where {option == 2}
|
||||
.then {
|
||||
if (lookupOreRock(id) != null) {
|
||||
it.startAction(ProspectingAction(it, this.position, lookupOreRock(id)!!))
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
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.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 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 = Ore.values()
|
||||
|
||||
fun lookupOre(id: Int): Ore? = ORES.find { it.id == id }
|
||||
|
||||
fun lookupOreRock(id: Int): Ore? {
|
||||
for (ore in ORES) {
|
||||
for (rock in ore.objects) {
|
||||
if (rock.key == id) {
|
||||
return ore
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
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 }
|
||||
|
||||
Reference in New Issue
Block a user