Move drop calculations to their own game service

This commit is contained in:
Melledy
2023-11-11 19:44:15 -08:00
parent 28e7c22d38
commit 012219de6a
4 changed files with 63 additions and 44 deletions

View File

@@ -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() {

View File

@@ -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

View File

@@ -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);
}
}
}
}

View File

@@ -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);