From acdcbf88296856e91b62d2431026372349f74c24 Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Sat, 7 Oct 2023 19:46:15 -0700 Subject: [PATCH] Retreating/losing a battle should teleport to the nearest anchor --- .../lunarcore/game/battle/BattleService.java | 12 +++++-- .../emu/lunarcore/game/player/Player.java | 17 +++------- .../java/emu/lunarcore/game/scene/Scene.java | 31 +++++++++++++++++++ .../game/scene/entity/EntityProp.java | 4 +++ 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/main/java/emu/lunarcore/game/battle/BattleService.java b/src/main/java/emu/lunarcore/game/battle/BattleService.java index 17c6c56..fb2cd9e 100644 --- a/src/main/java/emu/lunarcore/game/battle/BattleService.java +++ b/src/main/java/emu/lunarcore/game/battle/BattleService.java @@ -241,9 +241,15 @@ public class BattleService extends BaseGameService { // Teleport to anchor if player has lost/retreated. On official servers, the player party is teleported to the nearest anchor. if (teleportToAnchor) { - var anchor = player.getScene().getFloorInfo().getStartAnchorInfo(); - if (anchor != null) { - player.moveTo(anchor.clonePos()); + var anchorProp = player.getScene().getNearestSpring(); + if (anchorProp != null && anchorProp.getPropInfo() != null) { + var anchor = player.getScene().getFloorInfo().getAnchorInfo( + anchorProp.getPropInfo().getAnchorGroupID(), + anchorProp.getPropInfo().getAnchorID() + ); + if (anchor != null) { + player.moveTo(anchor.clonePos()); + } } } diff --git a/src/main/java/emu/lunarcore/game/player/Player.java b/src/main/java/emu/lunarcore/game/player/Player.java index 11bc7e6..58d6751 100644 --- a/src/main/java/emu/lunarcore/game/player/Player.java +++ b/src/main/java/emu/lunarcore/game/player/Player.java @@ -366,23 +366,16 @@ public class Player { public void onMove() { // Sanity if (this.getScene() == null) return; - - boolean anchorRange = false; - + // Check anchors. We can optimize this later. - for (EntityProp anchor : this.getScene().getHealingSprings()) { - long dist = getPos().getFast2dDist(anchor.getPos()); - if (dist > 25_000_000) continue; // 5000^2 - - anchorRange = true; - break; - } + EntityProp nearestAnchor = this.getScene().getNearestSpring(25_000_000); // 5000^2 + boolean isInRange = nearestAnchor != null; // Only heal if player isnt already in anchor range - if (anchorRange && anchorRange != this.inAnchorRange) { + if (isInRange && isInRange != this.inAnchorRange) { this.getCurrentLineup().heal(10000); } - this.inAnchorRange = anchorRange; + this.inAnchorRange = isInRange; } public void moveTo(int entryId, Position pos) { diff --git a/src/main/java/emu/lunarcore/game/scene/Scene.java b/src/main/java/emu/lunarcore/game/scene/Scene.java index 98b3b02..593bf3c 100644 --- a/src/main/java/emu/lunarcore/game/scene/Scene.java +++ b/src/main/java/emu/lunarcore/game/scene/Scene.java @@ -127,6 +127,7 @@ public class Scene { ); prop.setInstId(propInfo.getID()); prop.setGroupId(group.getId()); + prop.setPropInfo(propInfo); // Hacky fixes if (prop.getPropId() == 1003) { @@ -252,6 +253,36 @@ public class Scene { return true; } + /** + * Returns the nearest spring (Space Anchor) to the player in the scene + * @return + */ + public EntityProp getNearestSpring() { + return getNearestSpring(Long.MAX_VALUE); + } + + /** + * Returns the nearest spring (Space Anchor) to the player in the scene + * @param minDistSq Only checks springs in below this distance + * @return + */ + public EntityProp getNearestSpring(long minDistSq) { + EntityProp spring = null; + long springDist = 0; + + for (EntityProp prop : this.getHealingSprings()) { + long dist = getPlayer().getPos().getFast2dDist(prop.getPos()); + if (dist > minDistSq) continue; + + if (spring == null || dist < springDist) { + spring = prop; + springDist = dist; + } + } + + return spring; + } + // TODO public void fireTrigger(PropTriggerType type, int param1, int param2) { for (PropTrigger trigger : this.getTriggers()) { diff --git a/src/main/java/emu/lunarcore/game/scene/entity/EntityProp.java b/src/main/java/emu/lunarcore/game/scene/entity/EntityProp.java index 782a7f0..914e7ff 100644 --- a/src/main/java/emu/lunarcore/game/scene/entity/EntityProp.java +++ b/src/main/java/emu/lunarcore/game/scene/entity/EntityProp.java @@ -1,5 +1,6 @@ package emu.lunarcore.game.scene.entity; +import emu.lunarcore.data.config.PropInfo; import emu.lunarcore.data.excel.PropExcel; import emu.lunarcore.game.enums.PropState; import emu.lunarcore.game.scene.Scene; @@ -23,6 +24,9 @@ public class EntityProp implements GameEntity { private final Position pos; private final Position rot; + @Setter + private PropInfo propInfo; + public EntityProp(Scene scene, PropExcel excel, Position pos) { this.scene = scene; this.excel = excel;