From 012219de6aed27c80b4864b4a85f546f8cda934d Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Sat, 11 Nov 2023 19:44:15 -0800 Subject: [PATCH] Move drop calculations to their own game service --- .../emu/lunarcore/game/battle/Battle.java | 43 -------------- .../lunarcore/game/battle/BattleService.java | 2 +- .../emu/lunarcore/game/drops/DropService.java | 59 +++++++++++++++++++ .../emu/lunarcore/server/game/GameServer.java | 3 + 4 files changed, 63 insertions(+), 44 deletions(-) create mode 100644 src/main/java/emu/lunarcore/game/drops/DropService.java diff --git a/src/main/java/emu/lunarcore/game/battle/Battle.java b/src/main/java/emu/lunarcore/game/battle/Battle.java index 00f989f..96b81ff 100644 --- a/src/main/java/emu/lunarcore/game/battle/Battle.java +++ b/src/main/java/emu/lunarcore/game/battle/Battle.java @@ -116,49 +116,6 @@ public class Battle { this.buffs.clear(); } - // Drops - - public void calculateDrops() { - // TODO this isnt the right way drops are calculated on the official server... but its good enough for now - if (this.getNpcMonsters().size() == 0) { - return; - } - - var dropMap = new Int2IntOpenHashMap(); - - // Get drops from monsters - for (EntityMonster monster : this.getNpcMonsters()) { - var dropExcel = GameData.getMonsterDropExcel(monster.getExcel().getId(), monster.getWorldLevel()); - if (dropExcel == null || dropExcel.getDisplayItemList() == null) { - continue; - } - - for (ItemParam param : dropExcel.getDisplayItemList()) { - int id = param.getId(); - int count = Utils.randomRange(0, 3); - - if (id == 2) { - count = dropExcel.getAvatarExpReward(); - } - - dropMap.put(id, count + dropMap.get(id)); - } - } - - for (var entry : dropMap.int2IntEntrySet()) { - if (entry.getIntValue() <= 0) { - continue; - } - - // Create item and add it to player - GameItem item = new GameItem(entry.getIntKey(), entry.getIntValue()); - - if (getPlayer().getInventory().addItem(item)) { - this.getDrops().add(item); - } - } - } - // Serialization public SceneBattleInfo toProto() { diff --git a/src/main/java/emu/lunarcore/game/battle/BattleService.java b/src/main/java/emu/lunarcore/game/battle/BattleService.java index adf39c8..b2e7963 100644 --- a/src/main/java/emu/lunarcore/game/battle/BattleService.java +++ b/src/main/java/emu/lunarcore/game/battle/BattleService.java @@ -211,7 +211,7 @@ public class BattleService extends BaseGameService { player.getScene().removeEntity(monster); } // Drops - battle.calculateDrops(); + getServer().getDropService().calculateDrops(battle); } case BATTLE_END_LOSE -> { // Set avatar hp to 20% if the player's party is downed diff --git a/src/main/java/emu/lunarcore/game/drops/DropService.java b/src/main/java/emu/lunarcore/game/drops/DropService.java new file mode 100644 index 0000000..04dab06 --- /dev/null +++ b/src/main/java/emu/lunarcore/game/drops/DropService.java @@ -0,0 +1,59 @@ +package emu.lunarcore.game.drops; + +import emu.lunarcore.data.GameData; +import emu.lunarcore.data.common.ItemParam; +import emu.lunarcore.game.battle.Battle; +import emu.lunarcore.game.inventory.GameItem; +import emu.lunarcore.game.scene.entity.EntityMonster; +import emu.lunarcore.server.game.BaseGameService; +import emu.lunarcore.server.game.GameServer; +import emu.lunarcore.util.Utils; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; + +public class DropService extends BaseGameService { + + public DropService(GameServer server) { + super(server); + } + + public void calculateDrops(Battle battle) { + // TODO this isnt the right way drops are calculated on the official server... but its good enough for now + if (battle.getNpcMonsters().size() == 0) { + return; + } + + var dropMap = new Int2IntOpenHashMap(); + + // Get drops from monsters + for (EntityMonster monster : battle.getNpcMonsters()) { + var dropExcel = GameData.getMonsterDropExcel(monster.getExcel().getId(), monster.getWorldLevel()); + if (dropExcel == null || dropExcel.getDisplayItemList() == null) { + continue; + } + + for (ItemParam param : dropExcel.getDisplayItemList()) { + int id = param.getId(); + int count = Utils.randomRange(0, 3); + + if (id == 2) { + count = dropExcel.getAvatarExpReward(); + } + + dropMap.put(id, count + dropMap.get(id)); + } + } + + for (var entry : dropMap.int2IntEntrySet()) { + if (entry.getIntValue() <= 0) { + continue; + } + + // Create item and add it to player + GameItem item = new GameItem(entry.getIntKey(), entry.getIntValue()); + + if (battle.getPlayer().getInventory().addItem(item)) { + battle.getDrops().add(item); + } + } + } +} diff --git a/src/main/java/emu/lunarcore/server/game/GameServer.java b/src/main/java/emu/lunarcore/server/game/GameServer.java index 0753921..de24006 100644 --- a/src/main/java/emu/lunarcore/server/game/GameServer.java +++ b/src/main/java/emu/lunarcore/server/game/GameServer.java @@ -7,6 +7,7 @@ import java.util.List; import emu.lunarcore.Config.GameServerConfig; import emu.lunarcore.LunarCore; import emu.lunarcore.game.battle.BattleService; +import emu.lunarcore.game.drops.DropService; import emu.lunarcore.game.gacha.GachaService; import emu.lunarcore.game.inventory.InventoryService; import emu.lunarcore.game.player.Player; @@ -27,6 +28,7 @@ public class GameServer extends KcpServer { // Managers @Getter private final BattleService battleService; + @Getter private final DropService dropService; @Getter private final InventoryService inventoryService; @Getter private final GachaService gachaService; @@ -41,6 +43,7 @@ public class GameServer extends KcpServer { // Setup managers this.battleService = new BattleService(this); + this.dropService = new DropService(this); this.inventoryService = new InventoryService(this); this.gachaService = new GachaService(this);