Skip to content

Commit a5817d7

Browse files
committed
Refactoring
1 parent f4ed375 commit a5817d7

32 files changed

+283
-94
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,16 @@ public Player findNearestPlayer(Locatable base, String name) {
570570
return findNearest(EntityType.PLAYER, base, player -> player.getUsername().equalsIgnoreCase(name));
571571
}
572572

573+
/**
574+
* Finds the nearest player.
575+
*
576+
* @param base The origin of the search.
577+
* @return The nearest player, or {@code null} if no player is online.
578+
*/
579+
public Player findNearestPlayer(Locatable base) {
580+
return findNearest(EntityType.PLAYER, base, player -> true);
581+
}
582+
573583
/**
574584
* Computes the list of players visible to {@code player}.
575585
* <p>

src/main/java/io/luna/game/model/item/Item.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public DynamicItem asDynamic() {
339339
* @return The item definition (as stored in {@link ItemDefinition#ALL}).
340340
*/
341341
public ItemDefinition getItemDef() {
342-
return ItemDefinition.ALL.retrieve(id);
342+
return ItemDefinition.ALL.get(id).orElse(null);
343343
}
344344

345345
/**
@@ -351,6 +351,6 @@ public ItemDefinition getItemDef() {
351351
* @return The equipment definition for this item.
352352
*/
353353
public EquipmentDefinition getEquipDef() {
354-
return EquipmentDefinition.ALL.retrieve(id);
354+
return EquipmentDefinition.ALL.get(id).orElse(null);
355355
}
356356
}

src/main/java/io/luna/game/model/item/ItemContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ public final void set(int index, Item item) {
10931093
* @return The item at that slot, or null if empty.
10941094
*/
10951095
public final Item get(int index) {
1096-
checkArgument(index >= 0 && index < capacity, "Index out of bounds!");
1096+
checkArgument(index >= 0 && index < capacity, "Index " + index + " out of bounds!");
10971097
return items[index];
10981098
}
10991099

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public WalkingQueue(Mob mob) {
8282
public void process() {
8383
// Ignore processing and clear queue for stationary/immobilized NPCs, and locked mobs.
8484
boolean isNpc = mob instanceof Npc;
85-
if ((isNpc && mob.asNpc().isStationary()) || (isNpc && mob.getCombat().isImmobilized()) || locked) {
85+
boolean isNpcOrBot = isNpc || mob.asPlr().isBot();
86+
if ((isNpc && mob.asNpc().isStationary()) || (isNpcOrBot && mob.getCombat().isImmobilized()) || locked) {
8687
clear();
8788
mob.setWalkingDirection(Direction.NONE);
8889
mob.setRunningDirection(Direction.NONE);

src/main/java/io/luna/game/model/mob/bot/Bot.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Set;
3131
import java.util.concurrent.CompletableFuture;
3232
import java.util.concurrent.ThreadLocalRandom;
33+
import java.util.function.Predicate;
3334

3435
import static java.util.Objects.requireNonNull;
3536

@@ -274,11 +275,11 @@ public CompletableFuture<PlayerData> login() {
274275
if (data != null) {
275276
temporary = false;
276277
}
278+
loadData(data);
279+
if (data == null) {
280+
position = spawnPosition;
281+
}
277282
if (world.getPlayers().add(this)) {
278-
loadData(data);
279-
if (data == null) {
280-
setPosition(spawnPosition);
281-
}
282283
world.getBots().add(this);
283284
setState(EntityState.ACTIVE);
284285
log("I'm alive!");
@@ -342,15 +343,25 @@ public void randomizeSkills() {
342343
? ThreadLocalRandom.current().nextInt(1, 100)
343344
: ThreadLocalRandom.current().nextInt(50, 100);
344345
skill.setStaticLevel(level);
346+
skill.setLevel(level);
345347
}
346348
}
347349

348350
/**
349351
* Randomizes the bot’s currently equipped items.
350352
*/
351353
public void randomizeEquipment() {
354+
randomizeEquipment(def -> true);
355+
}
356+
357+
/**
358+
* Randomizes the bot’s currently equipped items, only factoring in filtered items.
359+
*
360+
* @param filter The filter to apply.
361+
*/
362+
public void randomizeEquipment(Predicate<EquipmentDefinition> filter) {
352363
ArrayListMultimap<Integer, EquipmentDefinition> eligible = ArrayListMultimap.create();
353-
EquipmentDefinition.ALL.lookupAll(def -> def.meetsAllRequirements(this))
364+
EquipmentDefinition.ALL.lookupAll(def -> def.meetsAllRequirements(this) && filter.test(def))
354365
.forEach(def -> eligible.put(def.getIndex(), def));
355366

356367
for (int index = 0; index < getEquipment().capacity(); index++) {
@@ -361,6 +372,16 @@ public void randomizeEquipment() {
361372
}
362373
}
363374

375+
/**
376+
* Maxes the bot’s skill levels.
377+
*/
378+
public void maxSkills() {
379+
for (Skill skill : skills) {
380+
skill.setStaticLevel(99);
381+
skill.setLevel(99);
382+
}
383+
}
384+
364385
/**
365386
* @return {@code true} if this bot is temporary (non-persistent).
366387
*/

src/main/java/io/luna/game/model/mob/bot/BotMovementStack.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import io.luna.game.model.Locatable;
77
import io.luna.game.model.mob.WalkingNavigator;
88
import io.luna.game.model.mob.WalkingQueue;
9+
import io.luna.game.model.mob.interact.InteractionPolicy;
10+
import io.luna.game.model.mob.interact.InteractionType;
911
import io.luna.game.task.Task;
1012

1113
import java.util.concurrent.CompletableFuture;
@@ -201,7 +203,10 @@ public void cancel() {
201203
*/
202204
private BotMovementRequest createRequest(Locatable target) {
203205
bot.log("Generating new movement path to " + target + ".");
204-
return new BotMovementRequest(bot.getNavigator().walk(target, true), target);
206+
// TODO Redo nicely?
207+
return new BotMovementRequest(target instanceof Entity ?
208+
bot.getNavigator().walkUntilReached((Entity) target, new InteractionPolicy(InteractionType.SIZE, ((Entity) target).size()), true) :
209+
bot.getNavigator().walk(target, true), target);
205210
}
206211

207212
/**

src/main/java/io/luna/game/model/mob/bot/io/BotOutputMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ public boolean sendInventoryItemClick(int option, int index, int itemId) {
325325
opcode = 203;
326326
break;
327327
case 2:
328+
msg.putShort(3214, ValueType.ADD);
328329
msg.putShort(itemId);
329330
msg.putShort(index, ValueType.ADD);
330-
msg.putShort(3214, ValueType.ADD);
331331
opcode = 24;
332332
break;
333333
case 3:

src/main/java/io/luna/game/model/mob/combat/attack/CombatAttack.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public CombatAttack(T attacker, Mob victim, InteractionPolicy interactionPolicy,
102102
*/
103103
public final void apply() {
104104
// todo onAttack is redundant, do anything within calculate damage?
105+
// todo maybe just only pass the builder until attack is ready to run?
105106
nextDamage = onAttack(calculateDamage(victim));
106107
if (nextDamage == null) {
107108
attacker.getCombat().setTarget(null);
@@ -136,7 +137,7 @@ public CombatDamage onAttack(CombatDamage damage) {
136137
*
137138
* @param damage The damage that was applied to the victim.
138139
*/
139-
public void onAttackArrived(CombatDamage damage) {
140+
public void onDamageApplied(CombatDamage damage) {
140141

141142
}
142143

src/main/java/io/luna/game/model/mob/combat/attack/MeleeCombatAttack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class MeleeCombatAttack<T extends Mob> extends CombatAttack<T> {
3838
*/
3939
public MeleeCombatAttack(T attacker, Mob victim, int animationId, int range, int delay) {
4040
super(attacker, victim, new InteractionPolicy(InteractionType.SIZE, range), delay);
41-
animation = new Animation(animationId, AnimationPriority.HIGH);
41+
animation = animationId == -1 ? null : new Animation(animationId, AnimationPriority.HIGH);
4242
}
4343

4444
@Override

src/main/java/io/luna/game/model/mob/combat/damage/CombatDamageAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public boolean run() {
7474
// Apply the hit.
7575
if (damage != null) {
7676
damage.apply();
77-
source.onAttackArrived(damage);
77+
source.onDamageApplied(damage);
7878
}
7979

8080
// Determine whether the victim should retaliate.

0 commit comments

Comments
 (0)