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

feat: crude drop tables with sample #164

Open
wants to merge 1 commit into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ MEN.forEach { man ->
on_npc_option(npc = man, option = "talk-to") {
player.queue { chat(this) }
}

npc_drop_table(npc = man) {
val map = HashMap<Int, Int>()
map[Items.BONES] = 1
if(world.random(1..10) == 4) {
map[Items.COINS_995] = 100
}
map
}
}

suspend fun chat(it: QueueTask) {
Expand Down
19 changes: 15 additions & 4 deletions game/src/main/kotlin/gg/rsmod/game/action/NpcDeathAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gg.rsmod.game.action
import gg.rsmod.game.fs.def.AnimDef
import gg.rsmod.game.model.LockState
import gg.rsmod.game.model.attr.KILLER_ATTR
import gg.rsmod.game.model.entity.GroundItem
import gg.rsmod.game.model.entity.Npc
import gg.rsmod.game.model.entity.Player
import gg.rsmod.game.model.queue.QueueTask
Expand Down Expand Up @@ -34,12 +35,16 @@ object NpcDeathAction {
val world = npc.world
val deathAnimation = npc.combatDef.deathAnimation
val respawnDelay = npc.combatDef.respawnDelay
val killer = npc.damageMap.getMostDamage()

npc.damageMap.getMostDamage()?.let { killer ->
if (killer is Player) {
world.getService(LoggerService::class.java, searchSubclasses = true)?.logNpcKill(killer, npc)
npc.stopMovement()
npc.lock()

npc.damageMap.getMostDamage()?.let { _killer ->
if (_killer is Player) {
world.getService(LoggerService::class.java, searchSubclasses = true)?.logNpcKill(_killer, npc)
}
npc.attr[KILLER_ATTR] = WeakReference(killer)
npc.attr[KILLER_ATTR] = WeakReference(_killer)
}

world.plugins.executeNpcPreDeath(npc)
Expand All @@ -56,6 +61,12 @@ object NpcDeathAction {

world.plugins.executeNpcDeath(npc)

val drops = world.plugins.executeNpcDropTable(killer, npc.id)

drops?.forEach { id, amount ->
world.spawn(GroundItem(id, amount, npc.tile, killer as? Player))
}

if (npc.respawns) {
npc.invisible = true
npc.reset()
Expand Down
2 changes: 2 additions & 0 deletions game/src/main/kotlin/gg/rsmod/game/plugin/KotlinPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ abstract class KotlinPlugin(private val r: PluginRepository, val world: World, v
*/
fun on_item_on_npc(item: Int, npc: Int, plugin: Plugin.() -> Unit) = r.bindItemOnNpc(npc = npc, item = item, plugin = plugin)

fun npc_drop_table(npc: Int, plugin: (Plugin).() -> HashMap<Int, Int>?) = r.bindNpcDropTable(npc = npc, plugin = plugin)

companion object {
private val METADATA_PATH = Paths.get("./plugins", "configs")
}
Expand Down
21 changes: 21 additions & 0 deletions game/src/main/kotlin/gg/rsmod/game/plugin/PluginRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ class PluginRepository(val world: World) {
*/
private val npcDeathPlugins = Int2ObjectOpenHashMap<Plugin.() -> Unit>()

/**
* A list of plugin that will be invoked when an npc dies
* and invokes its drop table
*/
private val npcDropsPlugins = Int2ObjectOpenHashMap<Plugin.() -> HashMap<Int, Int>?>()

/**
* A map of plugins that occur when an [Event] is triggered.
*/
Expand Down Expand Up @@ -1253,5 +1259,20 @@ class PluginRepository(val world: World) {
}
}

fun bindNpcDropTable(npc: Int, plugin: Plugin.() -> HashMap<Int, Int>?) {
if (npcDropsPlugins.containsKey(npc)) {
val error = java.lang.IllegalStateException("Npc drop table is already bound to a plugin: npc=$npc")
logger.error(error) {}
throw error
}
npcDropsPlugins[npc] = plugin
pluginCount++
}

fun executeNpcDropTable(p: Pawn?, npc: Int): HashMap<Int, Int>? {
val plugin = npcDropsPlugins[npc] ?: return null
return p?.executePlugin(plugin)
}

companion object : KLogging()
}