Skip to content

Commit b4e8a71

Browse files
committed
Add skill extension properties
1 parent 8c2f086 commit b4e8a71

File tree

5 files changed

+161
-103
lines changed

5 files changed

+161
-103
lines changed

game/plugin/api/src/org/apollo/game/plugins/api/player.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

game/plugin/api/src/player.kt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.apollo.game.plugin.api
2+
3+
import org.apollo.game.model.entity.Player
4+
import org.apollo.game.model.entity.Skill
5+
import org.apollo.game.model.entity.SkillSet
6+
7+
val Player.attack: SkillProxy get() = SkillProxy(skillSet, Skill.ATTACK)
8+
val Player.defence: SkillProxy get() = SkillProxy(skillSet, Skill.DEFENCE)
9+
val Player.strength: SkillProxy get() = SkillProxy(skillSet, Skill.STRENGTH)
10+
val Player.hitpoints: SkillProxy get() = SkillProxy(skillSet, Skill.HITPOINTS)
11+
val Player.ranged: SkillProxy get() = SkillProxy(skillSet, Skill.RANGED)
12+
val Player.prayer: SkillProxy get() = SkillProxy(skillSet, Skill.PRAYER)
13+
val Player.magic: SkillProxy get() = SkillProxy(skillSet, Skill.MAGIC)
14+
val Player.cooking: SkillProxy get() = SkillProxy(skillSet, Skill.COOKING)
15+
val Player.woodcutting: SkillProxy get() = SkillProxy(skillSet, Skill.WOODCUTTING)
16+
val Player.fletching: SkillProxy get() = SkillProxy(skillSet, Skill.FLETCHING)
17+
val Player.fishing: SkillProxy get() = SkillProxy(skillSet, Skill.FISHING)
18+
val Player.firemaking: SkillProxy get() = SkillProxy(skillSet, Skill.FIREMAKING)
19+
val Player.crafting: SkillProxy get() = SkillProxy(skillSet, Skill.CRAFTING)
20+
val Player.smithing: SkillProxy get() = SkillProxy(skillSet, Skill.SMITHING)
21+
val Player.mining: SkillProxy get() = SkillProxy(skillSet, Skill.MINING)
22+
val Player.herblore: SkillProxy get() = SkillProxy(skillSet, Skill.HERBLORE)
23+
val Player.agility: SkillProxy get() = SkillProxy(skillSet, Skill.AGILITY)
24+
val Player.thieving: SkillProxy get() = SkillProxy(skillSet, Skill.THIEVING)
25+
val Player.slayer: SkillProxy get() = SkillProxy(skillSet, Skill.SLAYER)
26+
val Player.farming: SkillProxy get() = SkillProxy(skillSet, Skill.FARMING)
27+
val Player.runecraft: SkillProxy get() = SkillProxy(skillSet, Skill.RUNECRAFT)
28+
29+
/**
30+
* A proxy class to allow
31+
*/
32+
class SkillProxy(val skills: SkillSet, val skill: Int) {
33+
34+
/**
35+
* The maximum level of this skill.
36+
*/
37+
val maximum = skills.getMaximumLevel(skill)
38+
39+
/**
40+
* The current level of this skill.
41+
*/
42+
val current = skills.getCurrentLevel(skill)
43+
44+
val experience = ExperienceProxy()
45+
46+
/**
47+
* A proxy class to make [experience] (effectively) write-only.
48+
*/
49+
inner class ExperienceProxy {
50+
51+
operator fun plusAssign(amount: Int) = skills.addExperience(skill, amount.toDouble())
52+
53+
operator fun plusAssign(amount: Double) = skills.addExperience(skill, amount)
54+
55+
}
56+
57+
/**
58+
* Boosts the current level of this skill by [amount], if possible (i.e. if `current + amount <= maximum + amount`).
59+
*/
60+
fun boost(amount: Int) {
61+
val new = Math.min(current + amount, maximum + amount)
62+
skills.setCurrentLevel(skill, new)
63+
}
64+
65+
/**
66+
* Drains the current level of this skill by [amount], if possible (i.e. if `current - amount >= 0`).
67+
*/
68+
fun drain(amount: Int) {
69+
val new = Math.max(current - amount, 0)
70+
skills.setCurrentLevel(skill, new)
71+
}
72+
73+
/**
74+
* Restores the current level of this skill by [amount], if possible (i.e. if `current + amount < maximum`).
75+
*/
76+
fun restore(amount: Int) {
77+
val new = Math.max(current + amount, maximum)
78+
skills.setCurrentLevel(skill, new)
79+
}
80+
81+
}

game/plugin/skills/fishing/src/fishing.plugin.kts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
12
import Fishing_plugin.FishingAction
23
import org.apollo.game.action.ActionBlock
34
import org.apollo.game.action.AsyncDistancedAction
45
import org.apollo.game.message.impl.NpcActionMessage
56
import org.apollo.game.model.Position
67
import org.apollo.game.model.entity.Player
7-
import org.apollo.game.model.entity.Skill
8+
import org.apollo.game.plugin.api.fishing
89
import org.apollo.game.plugin.skills.fishing.FishingSpot
910
import org.apollo.game.plugin.skills.fishing.FishingTool
10-
import org.apollo.game.plugins.api.fishing
11-
import org.apollo.game.plugins.api.skills
1211
import java.util.Objects
1312
import java.util.Random
1413

@@ -62,7 +61,7 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
6261
mob.playAnimation(tool.animation)
6362
wait(FISHING_DELAY)
6463

65-
val level = mob.skills.fishing.currentLevel
64+
val level = mob.fishing.current
6665
val fish = option.sample(level)
6766

6867
if (successfulCatch(level, fish.level)) {
@@ -72,7 +71,7 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
7271

7372
mob.inventory.add(fish.id)
7473
mob.sendMessage("You catch a ${fish.formattedName}.")
75-
mob.skills.addExperience(Skill.FISHING, fish.experience)
74+
mob.fishing.experience += fish.experience
7675

7776
if (mob.inventory.freeSlots() == 0) {
7877
mob.inventory.forceCapacityExceeded()
@@ -91,7 +90,7 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
9190
* Verifies that the player can gather fish from the [FishingSpot] they clicked.
9291
*/
9392
private fun verify(): Boolean {
94-
if (mob.skills.fishing.currentLevel < option.level) {
93+
if (mob.fishing.current < option.level) {
9594
mob.sendMessage("You need a fishing level of ${option.level} to fish at this spot.")
9695
return false
9796
} else if (!hasTool(mob, tool)) {
@@ -113,7 +112,6 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
113112
if (javaClass != other?.javaClass) return false
114113

115114
other as FishingAction
116-
117115
return option == other.option && position == other.position && mob == other.mob
118116
}
119117

game/plugin/skills/mining/src/mining.plugin.kts

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,47 @@ import org.apollo.game.message.impl.ObjectActionMessage
55
import org.apollo.game.model.Position
66
import org.apollo.game.model.World
77
import org.apollo.game.model.entity.Player
8-
import org.apollo.game.model.entity.Skill
98
import org.apollo.game.model.entity.obj.GameObject
9+
import org.apollo.game.plugin.api.mining
1010
import org.apollo.game.plugin.skills.mining.Ore
1111
import org.apollo.game.plugin.skills.mining.PICKAXES
1212
import org.apollo.game.plugin.skills.mining.Pickaxe
1313
import org.apollo.game.plugin.skills.mining.lookupOreRock
14-
import org.apollo.game.plugins.api.Definitions
15-
import org.apollo.game.plugins.api.mining
16-
import org.apollo.game.plugins.api.skills
1714
import org.apollo.net.message.Message
18-
import java.util.*
15+
import java.util.Optional
1916

2017
class MiningTarget(val objectId: Int, val position: Position, val ore: Ore) {
18+
2119
fun getObject(world: World): Optional<GameObject> {
2220
val region = world.regionRepository.fromPosition(position)
23-
val obj = region.findObject(position, objectId)
24-
25-
return obj
21+
return region.findObject(position, objectId)
2622
}
2723

2824
fun isSuccessful(mob: Player): Boolean {
2925
val offset = if (ore.chanceOffset) 1 else 0
30-
val percent = (ore.chance * mob.skills.mining.currentLevel + offset) * 100
26+
val percent = (ore.chance * mob.mining.current + offset) * 100
3127

32-
return rand(100) < percent;
28+
return rand(100) < percent
3329
}
30+
3431
}
3532

36-
class MiningAction(player: Player, val tool: Pickaxe, val target: MiningTarget) : AsyncDistancedAction<Player>(
37-
PULSES,
38-
true,
39-
player,
40-
target.position,
41-
ORE_SIZE
42-
) {
33+
class MiningAction(
34+
player: Player,
35+
private val tool: Pickaxe,
36+
private val target: MiningTarget
37+
) : AsyncDistancedAction<Player>(PULSES, true, player, target.position, ORE_SIZE) {
4338

4439
companion object {
4540
private val PULSES = 0
46-
private val ORE_SIZE = 1;
41+
private val ORE_SIZE = 1
4742

4843
fun pickaxeFor(player: Player): Pickaxe? {
4944
return PICKAXES
50-
.filter { it.level <= player.skills.mining.currentLevel }
51-
.filter { player.equipment.contains(it.id) || player.inventory.contains(it.id) }
52-
.sortedByDescending { it.level }
53-
.firstOrNull()
45+
.filter { it.level <= player.mining.current }
46+
.filter { player.equipment.contains(it.id) || player.inventory.contains(it.id) }
47+
.sortedByDescending { it.level }
48+
.firstOrNull()
5449
}
5550

5651
/**
@@ -69,10 +64,10 @@ class MiningAction(player: Player, val tool: Pickaxe, val target: MiningTarget)
6964
}
7065
}
7166

72-
override fun action() : ActionBlock = {
67+
override fun action(): ActionBlock = {
7368
mob.turnTo(position)
7469

75-
val level = mob.skills.mining.currentLevel
70+
val level = mob.mining.current
7671
if (level < target.ore.level) {
7772
mob.sendMessage("You do not have the required level to mine this rock.")
7873
stop()
@@ -98,33 +93,40 @@ class MiningAction(player: Player, val tool: Pickaxe, val target: MiningTarget)
9893
val oreName = Definitions.item(target.ore.id)?.name?.toLowerCase()
9994
mob.sendMessage("You manage to mine some $oreName")
10095

101-
mob.skills.addExperience(Skill.MINING, target.ore.exp)
96+
mob.mining.experience += target.ore.exp
10297
mob.world.expireObject(obj.get(), target.ore.objects[target.objectId]!!, target.ore.respawn)
10398
stop()
10499
}
105100
}
106101
}
107102
}
108103

109-
class ExpiredProspectingAction : DistancedAction<Player> {
110-
111-
constructor(mob: Player, position: Position) : super(PROSPECT_PULSES, true, mob, position, ORE_SIZE)
104+
class ExpiredProspectingAction(
105+
mob: Player,
106+
position: Position
107+
) : DistancedAction<Player>(PROSPECT_PULSES, true, mob, position, ORE_SIZE) {
112108

113109
companion object {
114110
private val PROSPECT_PULSES = 0
115-
private val ORE_SIZE = 1;
111+
private val ORE_SIZE = 1
116112
}
117113

118114
override fun executeAction() {
119115
mob.sendMessage("There is currently no ore available in this rock.")
120-
stop();
116+
stop()
121117
}
118+
122119
}
123120

124-
class ProspectingAction(val m: Player, val p: Position, val ore: Ore) : AsyncDistancedAction<Player>(PROSPECT_PULSES, true, m, p, ORE_SIZE) {
121+
class ProspectingAction(
122+
player: Player,
123+
position: Position,
124+
private val ore: Ore
125+
) : AsyncDistancedAction<Player>(PROSPECT_PULSES, true, player, position, ORE_SIZE) {
126+
125127
companion object {
126128
private val PROSPECT_PULSES = 3
127-
private val ORE_SIZE = 1;
129+
private val ORE_SIZE = 1
128130

129131
/**
130132
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
@@ -135,9 +137,10 @@ class ProspectingAction(val m: Player, val p: Position, val ore: Ore) : AsyncDis
135137

136138
message.terminate()
137139
}
140+
138141
}
139142

140-
override fun action() : ActionBlock = {
143+
override fun action(): ActionBlock = {
141144
mob.sendMessage("You examine the rock for ores...")
142145
mob.turnTo(position)
143146

@@ -148,22 +151,23 @@ class ProspectingAction(val m: Player, val p: Position, val ore: Ore) : AsyncDis
148151

149152
stop()
150153
}
154+
151155
}
152156

153157
on { ObjectActionMessage::class }
154-
.where { option == 1 }
155-
.then {
156-
val ore = lookupOreRock(id)
157-
if (ore != null) {
158-
MiningAction.start(this, it, ore)
159-
}
158+
.where { option == 1 }
159+
.then {
160+
val ore = lookupOreRock(id)
161+
if (ore != null) {
162+
MiningAction.start(this, it, ore)
160163
}
164+
}
161165

162166
on { ObjectActionMessage::class }
163-
.where { option == 2 }
164-
.then {
165-
var ore = lookupOreRock(id)
166-
if (ore != null) {
167-
ProspectingAction.start(this, it, ore)
168-
}
167+
.where { option == 2 }
168+
.then {
169+
val ore = lookupOreRock(id)
170+
if (ore != null) { // TODO send expired action if rock is expired
171+
ProspectingAction.start(this, it, ore)
169172
}
173+
}

0 commit comments

Comments
 (0)