Skip to content

Commit 2d9a674

Browse files
committed
Merge remote-tracking branch 'refs/remotes/upstream/master'
# Conflicts: # src/main/kotlin/world/player/item/banking/regularBank/openBank.kts
2 parents 717b06e + 96d70f7 commit 2d9a674

File tree

15 files changed

+364
-270
lines changed

15 files changed

+364
-270
lines changed

src/main/java/io/luna/game/model/Entity.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.luna.game.model.mob.Mob;
99
import io.luna.game.model.mob.MobList;
1010
import io.luna.game.model.mob.Player;
11+
import io.luna.game.model.mob.attr.AttributeMap;
1112
import io.luna.game.plugin.PluginManager;
1213
import io.luna.game.GameService;
1314
import org.apache.logging.log4j.LogManager;
@@ -24,7 +25,7 @@
2425
* @author lare96
2526
*/
2627
public abstract class Entity {
27-
// todo interactable conditions
28+
2829
/**
2930
* A {@link Comparator} that sorts {@link Entity} types by closest to furthest distance from the base entity.
3031
*/
@@ -95,6 +96,11 @@ public int compare(Entity o1, Entity o2) {
9596
*/
9697
protected volatile ChunkRepository chunkRepository;
9798

99+
/**
100+
* The attribute map.
101+
*/
102+
private AttributeMap attributes;
103+
98104
/**
99105
* Creates a new {@link Entity}.
100106
*
@@ -312,6 +318,17 @@ private void removeCurrentChunk() {
312318
}
313319
}
314320

321+
/**
322+
* @return The attribute map.
323+
*/
324+
public final AttributeMap getAttributes() {
325+
if(attributes == null) {
326+
// Lazy initialization is necessary, otherwise way too much memory will be used.
327+
attributes = new AttributeMap();
328+
}
329+
return attributes;
330+
}
331+
315332
/**
316333
* @return The context instance.
317334
*/

src/main/java/io/luna/game/model/mob/Mob.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.luna.game.model.Position;
1010
import io.luna.game.model.mob.MobDeathTask.NpcDeathTask;
1111
import io.luna.game.model.mob.MobDeathTask.PlayerDeathTask;
12-
import io.luna.game.model.mob.attr.AttributeMap;
1312
import io.luna.game.model.mob.block.Animation;
1413
import io.luna.game.model.mob.block.Graphic;
1514
import io.luna.game.model.mob.block.Hit;
@@ -33,11 +32,6 @@
3332
*/
3433
public abstract class Mob extends Entity {
3534

36-
/**
37-
* The attribute map.
38-
*/
39-
protected final AttributeMap attributes = new AttributeMap();
40-
4135
/**
4236
* The update flag set.
4337
*/
@@ -555,13 +549,6 @@ public void onTeleport(Position newPosition) {
555549

556550
}
557551

558-
/**
559-
* @return The attribute map.
560-
*/
561-
public final AttributeMap getAttributes() {
562-
return attributes;
563-
}
564-
565552
/**
566553
* @return The update flag set.
567554
*/

src/main/java/io/luna/game/model/mob/WalkingQueue.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010
import io.luna.game.model.collision.CollisionManager;
1111
import io.luna.game.model.path.AStarPathfindingAlgorithm;
1212
import io.luna.game.model.path.EuclideanHeuristic;
13+
import io.luna.game.model.path.PathfindingAlgorithm;
1314
import io.luna.game.model.path.SimplePathfindingAlgorithm;
1415
import io.luna.util.RandomUtils;
1516
import org.apache.logging.log4j.LogManager;
1617
import org.apache.logging.log4j.Logger;
1718

18-
import java.util.ArrayDeque;
19-
import java.util.Arrays;
20-
import java.util.Deque;
21-
import java.util.Objects;
22-
import java.util.Optional;
23-
import java.util.Queue;
19+
import java.util.*;
2420

2521
/**
2622
* A model representing an implementation of the walking queue.
@@ -124,16 +120,7 @@ public int getY() {
124120
* The collision manager.
125121
*/
126122
private final CollisionManager collisionManager;
127-
128-
/**
129-
* The simple pathfinder.
130-
*/
131-
private final SimplePathfindingAlgorithm simplePathfinder;
132-
133-
/**
134-
* The A* pathfinder.
135-
*/
136-
private final AStarPathfindingAlgorithm astarPathfinder;
123+
private final PathfindingAlgorithm pathfindingAlgorithm;
137124

138125
/**
139126
* The mob.
@@ -163,8 +150,11 @@ public int getY() {
163150
public WalkingQueue(Mob mob) {
164151
this.mob = mob;
165152
collisionManager = mob.getWorld().getCollisionManager();
166-
simplePathfinder = new SimplePathfindingAlgorithm(collisionManager);
167-
astarPathfinder = new AStarPathfindingAlgorithm(collisionManager, new EuclideanHeuristic());
153+
if (mob instanceof Player) {
154+
this.pathfindingAlgorithm = new AStarPathfindingAlgorithm(collisionManager, new EuclideanHeuristic());
155+
} else {
156+
this.pathfindingAlgorithm = new SimplePathfindingAlgorithm(collisionManager);
157+
}
168158
}
169159

170160
/**
@@ -265,7 +255,7 @@ public void walk(Position destination) {
265255
*/
266256
public void walkUntilReached(Entity target) {
267257
Deque<Step> newPath = new ArrayDeque<>();
268-
Deque<Position> path = astarPathfinder.find(mob.getPosition(), target.getPosition());
258+
Deque<Position> path = pathfindingAlgorithm.find(mob.getPosition(), target.getPosition());
269259
Position lastPosition = mob.getPosition();
270260
for (; ; ) {
271261
Position nextPosition = path.poll();
@@ -507,8 +497,7 @@ private void incrementRunEnergy() {
507497
* @return The path.
508498
*/
509499
private Deque<Step> findPath(Position target) {
510-
Deque<Position> positionPath = mob.getType() == EntityType.PLAYER ? astarPathfinder.find(mob.getPosition(), target) :
511-
simplePathfinder.find(mob.getPosition(), target);
500+
Deque<Position> positionPath = pathfindingAlgorithm.find(mob.getPosition(), target);
512501
Deque<Step> stepPath = new ArrayDeque<>(positionPath.size());
513502
for (; ; ) {
514503
// TODO remove step class?

src/main/java/io/luna/game/model/mob/attr/AttributeMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final class AttributeMap {
3434
/**
3535
* A map that holds attribute key and value pairs.
3636
*/
37-
private final Map<Attribute<?>, Object> attributes = new IdentityHashMap<>(64);
37+
private final Map<Attribute<?>, Object> attributes = new IdentityHashMap<>(12);
3838

3939
/**
4040
* The last accessed key.

src/main/java/io/luna/game/model/path/PathfindingAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @author Major
1717
*/
18-
abstract class PathfindingAlgorithm {
18+
public abstract class PathfindingAlgorithm {
1919

2020
//todo concurrency for long paths?
2121
protected final CollisionManager collisionManager;

src/main/kotlin/api/attr/AttributeDelegate.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package api.attr
22

3+
import io.luna.game.model.Entity
34
import io.luna.game.model.mob.Player
45
import io.luna.game.model.mob.attr.Attribute
56
import kotlin.reflect.KProperty
@@ -14,14 +15,14 @@ class AttributeDelegate<T : Any?>(val attr: Attribute<T>) {
1415
/**
1516
* Retrieve the attribute value.
1617
*/
17-
operator fun getValue(player: Player, property: KProperty<*>): T =
18-
player.attributes[attr]
18+
operator fun getValue(entity: Entity, property: KProperty<*>): T =
19+
entity.attributes[attr]
1920

2021
/**
2122
* Set the attribute value.
2223
*/
23-
operator fun setValue(player: Player, property: KProperty<*>, value: T) {
24-
player.attributes[attr] = value
24+
operator fun setValue(entity: Entity, property: KProperty<*>, value: T) {
25+
entity.attributes[attr] = value
2526
}
2627

2728
/**
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package api.attr
22

3+
import io.luna.game.model.Entity
34
import io.luna.game.model.mob.Player
45
import io.luna.game.model.mob.attr.Attribute
56
import kotlin.reflect.KProperty
67

78
/**
89
* An extension property that adds a getter delegate to [Attribute].
910
*/
10-
operator fun <T> Attribute<T>.getValue(plr: Player, property: KProperty<*>): T = plr.attributes[this]
11+
operator fun <T> Attribute<T>.getValue(entity: Entity, property: KProperty<*>): T = entity.attributes[this]
1112

1213
/**
1314
* An extension property that adds a setter delegate to [Attribute].
1415
*/
15-
operator fun <T> Attribute<T>.setValue(plr: Player, property: KProperty<*>, value: T) {
16-
plr.attributes[this] = value
16+
operator fun <T> Attribute<T>.setValue(entity: Entity, property: KProperty<*>, value: T) {
17+
entity.attributes[this] = value
1718
}

src/main/kotlin/world/player/item/banking/regularBank/Banking.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package world.player.item.banking.regularBank
22

33
import api.predef.*
4+
import io.luna.game.model.def.*
45

56
/**
67
* Holds utility functions related to banking.
78
*/
89
object Banking {
910

11+
/**
12+
* ID of the default banker npc.
13+
*/
14+
val DEFAULT_BANKER: Int = 494
15+
1016
/**
1117
* The mutable set of banking objects.
1218
*/
@@ -17,6 +23,16 @@ object Banking {
1723
*/
1824
val bankingObjects: Set<Int> = loadObjects
1925

26+
/**
27+
* Mutable set of banking npc ids.
28+
*/
29+
private val loadBankingNpcs = mutableSetOf<Int>()
30+
31+
/**
32+
* Immutable set of banker npc ids.
33+
*/
34+
val bankingNpcs: Set<Int> = loadBankingNpcs
35+
2036
/**
2137
* Loads all banking objects based on definitions from the cache.
2238
*/
@@ -30,4 +46,13 @@ object Banking {
3046
}
3147
}
3248
}
49+
50+
/**
51+
* Loads all banking npcs based on definitions from the cache.
52+
*/
53+
internal fun loadBankingNpcs() {
54+
NpcDefinition.ALL
55+
.filter { npcDefinition -> npcDefinition?.name?.contains("Banker", ignoreCase = true) ?: false }
56+
.forEach({ npcDefinition -> loadBankingNpcs.add(npcDefinition.id)})
57+
}
3358
}

src/main/kotlin/world/player/item/banking/regularBank/openBank.kts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@ package world.player.item.banking.regularBank
22

33
import api.predef.*
44
import io.luna.game.event.impl.ServerStateChangedEvent.ServerLaunchEvent
5-
import io.luna.game.model.def.NpcDefinition
65
import io.luna.game.model.mob.*
76

8-
val DEFAULT_BANKER = 494
9-
10-
val bankerIds = mutableListOf<Int>()
11-
127
on(ServerLaunchEvent::class) {
13-
// Find all banker npcs and store their ids
14-
NpcDefinition.ALL
15-
.filter { npcDefinition -> npcDefinition?.name?.contains("Banker", ignoreCase = true) ?: false }
16-
.forEach({ npcDefinition -> bankerIds.add(npcDefinition.id)})
8+
// Load all banking npcs and make them initialize dialogue
9+
Banking.loadBankingNpcs()
10+
for (id in Banking.bankingNpcs) {
11+
npc1(id, {bankerDialogue(plr, id)})
12+
}
1713

1814
// Load all banking objects, make them open the bank.
1915
Banking.loadBankingObjects()
2016
for (id in Banking.bankingObjects) {
2117
object1(id) {
2218
val npc: Npc? = plr.localNpcs.firstOrNull { npc ->
2319
npc.position.isWithinDistance(gameObject.position, 1)
24-
&& bankerIds.contains(npc.id)
20+
&& Banking.bankingNpcs.contains(npc.id)
2521
}
2622

2723
if (npc != null) {
2824
bankerDialogue(plr, npc.id)
2925
} else {
30-
bankerDialogue(plr, DEFAULT_BANKER) // use default banker when no npc nearby
26+
bankerDialogue(plr, Banking.DEFAULT_BANKER) // use default banker when no npc nearby
3127
}
3228
}
3329
if (objectDef(id).actions.contains("Use-quickly")) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package world.player.skill
2+
3+
import api.predef.*
4+
import kotlin.math.floor
5+
6+
/**
7+
* A utility class containing functions related to skills.
8+
*/
9+
object Skills {
10+
11+
12+
/* https://github.com/luna-rs/luna/issues/422#issuecomment-2980912667 */
13+
14+
/**
15+
* Calculates if an action is successful based on the [low] and [high] rates.
16+
* [level] is the effective current level in the skill to check. This takes into account any
17+
* bonuses.
18+
* [maxLevel] is the maximum level in the skill. This is 99 for all skills in OSRS/317.
19+
*/
20+
fun success(low: Int, high: Int, level: Int, maxLevel: Int = 99): Boolean {
21+
val rate = successRate(low, high, level, maxLevel)
22+
return rate > rand().nextDouble()
23+
}
24+
25+
/**
26+
* Calculates the success rate for an action from the [low] and [high] rates.
27+
* [level] is the effective current level in the skill to check. This takes into account any
28+
* bonuses.
29+
* [maxLevel] is the maximum level in the skill. This is 99 for all skills in OSRS/317.
30+
*/
31+
fun successRate(low: Int, high: Int, level: Int, maxLevel: Int = 99): Double {
32+
val lowRate = (low * (maxLevel - level)) / (maxLevel - 1.0)
33+
val highRate = (high * (level - 1)) / (maxLevel - 1.0)
34+
return (1.0 + floor(lowRate + highRate + 0.5)) / 256.0
35+
}
36+
}

0 commit comments

Comments
 (0)