Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added firemaking plugin for Kotlin #349

Open
wants to merge 7 commits into
base: kotlin-experiments
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions game/plugin/skill/firemaking/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugin {
name = "firemaking_skill"
packageName = "org.apollo.game.plugin.skills.firemaking"
authors = [
"tlf30",
"lare96"
]
dependencies = [
"util:lookup",
]
}
11 changes: 11 additions & 0 deletions game/plugin/skill/firemaking/meta.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "firemaking_skill"
package = "org.apollo.game.plugin.skill.firemaking"
authors = [
"tlf30",
"lare96"
]
dependencies = [ "entity_lookup" ]

[config]
srcDir = "src/"
testDir = "test/"
22 changes: 22 additions & 0 deletions game/plugin/skill/firemaking/src/fireamaking_data.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.apollo.game.model.Animation

enum class Log(val id: Int, val level: Int, val xp: Double) {
NORMAL(1511, 1, 40.0),
ACHEY(2862, 1, 40.0),
OAK(1521, 15, 60.0),
WILLOW(1519, 30, 90.0),
TEAK(6333, 35, 105.0),
MAPLE(1517, 45, 135.0),
MAHOGANY(6332, 50, 157.5),
YEW(1515, 60, 202.5),
MAGIC(1513, 75, 303.8)
}

val TINDER_BOX = 590
val FIRE_OBJ = 2732
val ASH = 592
val LIGHT_ANIMATION = Animation(733)

val LOGS = Log.values()

fun lookupLog(id: Int): Log? = LOGS.find { it.id == id }
138 changes: 138 additions & 0 deletions game/plugin/skill/firemaking/src/firemaking.plugin.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import org.apollo.game.action.Action
import org.apollo.game.message.impl.ItemOnItemMessage
import org.apollo.game.model.Direction
import org.apollo.game.model.Item
import org.apollo.game.model.Position
import org.apollo.game.model.entity.*
import org.apollo.game.model.entity.obj.DynamicGameObject
import org.apollo.game.scheduling.ScheduledTask
import java.util.*
import kotlin.properties.Delegates


class FiremakingAction(val player: Player, val log: Log):Action<Player>(DELAY, true, player) {

private var started = false
private var groundLog: GroundItem by Delegates.notNull()
val rand = Random()

companion object {
private val DELAY = 0
}

override fun execute() {
mob.walkingQueue.clear()
//Check log level
if (log.level > player.skillSet.getSkill(Skill.FIREMAKING).currentLevel) {
player.sendMessage("You need a Firemaking level of " + log.level + " to light this log.")
stop()
return
}

//check if we have a tinderbox
if (!player.inventory.contains(TINDER_BOX)) {
player.sendMessage("You need a tinderbox in your inventory in order to light fires.")
stop()
return
}

if (!started) {
val region = player.world.regionRepository.fromPosition(player.position)
player.sendMessage("You attempt to light the logs.")
if (region.getEntities<Entity>(player.position, EntityType.DYNAMIC_OBJECT, EntityType.STATIC_OBJECT).isEmpty()) {
player.inventory.remove(log.id)
groundLog = GroundItem.dropped(player.world, player.position, Item(log.id), player)
player.world.spawn(groundLog)
} else {
player.sendMessage("You cannot light a fire here.")
stop()
return
}
started = true
}

//light the fire
player.playAnimation(LIGHT_ANIMATION)

if (successfulLight()) {
if (lightFire(Direction.WEST) ||
lightFire(Direction.EAST) ||
lightFire(Direction.NORTH) ||
lightFire(Direction.SOUTH) ||
lightFire(Direction.NONE)
) {
player.sendMessage("The fire catches and the logs begin to burn.")
player.skillSet.addExperience(Skill.FIREMAKING, log.xp)
} else {
player.sendMessage("You cannot light a fire here.")
}
stop()
}
}

override fun stop() {
super.stop()
player.stopAnimation()
}

fun successfulLight(): Boolean {
//TODO: This is from lare96, as he mentioned, we need to find the actual chance
val playerLevel = player.skillSet.getSkill(Skill.FIREMAKING).currentLevel
val lowChance = playerLevel - log.level + 5
if (lowChance > 30) {
return 30 > rand.nextInt(40)
} else {
return lowChance > rand.nextInt(40)
}
}


fun lightFire(direction: Direction): Boolean {
if (canLight(direction)) {
val fire = DynamicGameObject.createPublic(player.world, FIRE_OBJ, player.position, 10, 0)
player.walkingQueue.addFirstStep(player.position.step(1, direction))
val region = player.world.regionRepository.fromPosition(player.position)
region.removeEntity(groundLog)
player.world.spawn(fire)

//TODO: burn time = log level * 5 + rand(30). I think this is right can someone verify?

val burnTime = (log.level * 5 + rand.nextInt(30) * 1000) / 600 //convert to pulses
player.world.schedule(object: ScheduledTask(burnTime, false) {
override fun execute() {
region.removeEntity(fire)
player.world.spawn(GroundItem.create(player.world, fire.position, Item(ASH)))
this.stop()
}

})
return true
}
return false
}

fun canLight(direction: Direction): Boolean {
val region = player.world.regionRepository.fromPosition(player.position)
if (direction == Direction.NONE) {
return true
}
if (region.traversable(player.position, EntityType.PLAYER, direction)) {
return true
}
return false
}

}


on { ItemOnItemMessage::class }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Major- didn't you write something in Ruby to handle these ItemOnItem pairs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it because it wasn't very good, maybe we could look into something for kotlin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the ItemOnItemMessage not the correct way to do this?

Copy link
Member

@Major- Major- Sep 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is correct, we're just discussing how we can make it a bit nicer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok

.where { (id == TINDER_BOX && lookupLog(targetId) != null) || (targetId == TINDER_BOX && lookupLog(id) != null) }
.then {
if (id == TINDER_BOX) {
it.startAction(FiremakingAction(it, lookupLog(targetId)!!))
terminate()
} else {
it.startAction(FiremakingAction(it, lookupLog(id)!!))
terminate()
}
}