Refactor woodcutting to use coroutines

This commit is contained in:
Gary Tierney
2017-09-19 19:01:30 +01:00
parent 75a9b828b2
commit 2cd160feb3
@@ -1,5 +1,6 @@
import org.apollo.cache.def.ItemDefinition import org.apollo.cache.def.ItemDefinition
import org.apollo.game.GameConstants import org.apollo.game.GameConstants
import org.apollo.game.action.ActionBlock
import org.apollo.game.action.AsyncDistancedAction import org.apollo.game.action.AsyncDistancedAction
import org.apollo.game.message.impl.ObjectActionMessage import org.apollo.game.message.impl.ObjectActionMessage
import org.apollo.game.model.Position import org.apollo.game.model.Position
@@ -8,8 +9,7 @@ import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.Skill import org.apollo.game.model.entity.Skill
import org.apollo.game.model.entity.obj.GameObject import org.apollo.game.model.entity.obj.GameObject
import org.apollo.game.plugin.skills.woodcutting.* import org.apollo.game.plugin.skills.woodcutting.*
import org.apollo.game.plugins.api.woodcutting import org.apollo.game.plugins.api.*
import org.apollo.game.plugins.api.skills
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@@ -42,7 +42,7 @@ class WoodcuttingAction(val player: Player, val tool: Axe, val target: Woodcutti
* Find the highest level axe the player has * Find the highest level axe the player has
*/ */
private fun axeFor(player: Player): Axe? { private fun axeFor(player: Player): Axe? {
return getAxes() return Axe.getAxes()
.filter { it.level <= player.skills.woodcutting.currentLevel } .filter { it.level <= player.skills.woodcutting.currentLevel }
.filter { player.equipment.contains(it.id) || player.inventory.contains(it.id) } .filter { player.equipment.contains(it.id) || player.inventory.contains(it.id) }
.sortedByDescending { it.level } .sortedByDescending { it.level }
@@ -55,11 +55,11 @@ class WoodcuttingAction(val player: Player, val tool: Axe, val target: Woodcutti
fun start(message: ObjectActionMessage, player: Player, wood: Tree) { fun start(message: ObjectActionMessage, player: Player, wood: Tree) {
val axe = axeFor(player) val axe = axeFor(player)
if (axe != null) { if (axe != null) {
//Check that the player had room in their inventory
if (player.inventory.freeSlots() == 0) { if (player.inventory.freeSlots() == 0) {
player.inventory.forceCapacityExceeded() player.inventory.forceCapacityExceeded()
return return
} }
val action = WoodcuttingAction(player, axe, WoodcuttingTarget(message.id, message.position, wood)) val action = WoodcuttingAction(player, axe, WoodcuttingTarget(message.id, message.position, wood))
player.startAction(action) player.startAction(action)
} else { } else {
@@ -70,43 +70,39 @@ class WoodcuttingAction(val player: Player, val tool: Axe, val target: Woodcutti
} }
} }
override fun start() { override fun action(): ActionBlock = {
mob.turnTo(position) mob.turnTo(position)
val level = mob.skills.woodcutting.currentLevel val level = mob.skills.woodcutting.currentLevel
if (level < target.tree.level) { if (level < target.tree.level) {
mob.sendMessage("You do not have the required level to cut down this tree.") mob.sendMessage("You do not have the required level to cut down this tree.")
stop() stop()
} }
}
override suspend fun executeActionAsync() { while (isRunning) {
mob.sendMessage("You swing your axe at the tree.") mob.sendMessage("You swing your axe at the tree.")
mob.playAnimation(tool.animation) mob.playAnimation(tool.animation)
wait(tool.pulses) wait(tool.pulses)
//Check that the object exists in the world //Check that the object exists in the world
val obj = target.getObject(mob.world) val obj = target.getObject(mob.world)
if (!obj.isPresent) { if (!obj.isPresent) {
stop() stop()
return }
}
if (mob.inventory.add(target.tree.id)) { if (mob.inventory.add(target.tree.id)) {
//TODO: Use lookup from utils once it has a lookup function for IDs val logName = Definitions.item(target.tree.id)?.name?.toLowerCase();
val logName = ItemDefinition.lookup(target.tree.id).name.toLowerCase(); mob.sendMessage("You managed to cut some $logName.")
mob.sendMessage("You managed to cut some $logName.") mob.skills.addExperience(Skill.WOODCUTTING, target.tree.exp)
mob.skillSet.addExperience(Skill.WOODCUTTING, target.tree.exp) }
}
//Check if the tree was cut down. If it was, we stop the task. if (target.isCutDown(mob)) {
if (target.isCutDown(mob)) { //respawn time: http://runescape.wikia.com/wiki/Trees
//respawn time: http://runescape.wikia.com/wiki/Trees val respawn = TimeUnit.SECONDS.toMillis(MINIMUM_RESPAWN_TIME + rand(150)) / GameConstants.PULSE_DELAY
val respawn = TimeUnit.SECONDS.toMillis(MINIMUM_RESPAWN_TIME + rand(150)) / GameConstants.PULSE_DELAY mob.world.expireObject(obj.get(), target.tree.stump, respawn.toInt())
mob.world.expireObject(obj.get(), target.tree.stump, respawn.toInt()) stop()
stop() }
} }
} }
} }