Files
BattleBane/src/engine/loot/LootManager.java
T

370 lines
15 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.loot;
2023-04-06 20:07:24 -05:00
import engine.Enum;
import engine.gameManager.ConfigManager;
2022-04-30 09:41:17 -04:00
import engine.gameManager.DbManager;
2023-04-06 20:10:42 -05:00
import engine.gameManager.NPCManager;
2023-04-06 20:07:24 -05:00
import engine.gameManager.ZoneManager;
import engine.net.DispatchMessage;
import engine.net.client.msg.chat.ChatSystemMsg;
import engine.objects.*;
2023-07-15 09:23:48 -04:00
2023-04-06 20:07:24 -05:00
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
2022-04-30 09:41:17 -04:00
import java.util.HashMap;
2023-04-06 20:07:24 -05:00
import java.util.Random;
2022-04-30 09:41:17 -04:00
import java.util.concurrent.ThreadLocalRandom;
/**
* Class contains static methods for data from Magicbane's loot tables
*/
public class LootManager {
2023-04-06 20:07:24 -05:00
//new tables
2023-07-15 09:23:48 -04:00
private static final HashMap<Integer, GenTable> generalItemTables = new HashMap<>();
private static final HashMap<Integer, ItemTable> itemTables = new HashMap<>();
private static final HashMap<Integer, ModTypeTable> modTypeTables = new HashMap<>();
private static final HashMap<Integer, ModTable> modTables = new HashMap<>();
private LootManager() {
}
2022-04-30 09:41:17 -04:00
2023-04-06 20:07:24 -05:00
// Bootstrap routine to load loot data from database
public static void loadLootData() {
DbManager.LootQueries.LOAD_ALL_LOOTGROUPS();
DbManager.LootQueries.LOAD_ALL_LOOTTABLES();
DbManager.LootQueries.LOAD_ALL_MODGROUPS();
DbManager.LootQueries.LOAD_ALL_MODTABLES();
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static void GenerateMobLoot(Mob mob, boolean fromDeath) {
2023-04-06 20:07:24 -05:00
//determine if mob is in hotzone
boolean inHotzone = ZoneManager.inHotZone(mob.getLoc());
//get multiplier form config manager
float multiplier = Float.parseFloat(ConfigManager.MB_NORMAL_DROP_RATE.getValue());
if (inHotzone) {
//if mob is inside hotzone, use the hotzone gold multiplier form the config instead
multiplier = Float.parseFloat(ConfigManager.MB_HOTZONE_DROP_RATE.getValue());
}
//iterate the booty sets
2023-07-15 09:23:48 -04:00
if (mob.getMobBase().bootySet != 0 && NPCManager._bootySetMap.containsKey(mob.getMobBase().bootySet)) {
2023-04-15 11:52:19 -05:00
RunBootySet(NPCManager._bootySetMap.get(mob.getMobBase().bootySet), mob, multiplier, inHotzone, fromDeath);
2023-04-07 15:41:40 -05:00
}
2023-07-15 09:23:48 -04:00
if (mob.bootySet != 0) {
2023-04-15 11:52:19 -05:00
RunBootySet(NPCManager._bootySetMap.get(mob.bootySet), mob, multiplier, inHotzone, fromDeath);
2023-04-07 13:09:15 -05:00
}
2023-04-07 12:22:33 -05:00
//lastly, check mobs inventory for godly or disc runes to send a server announcement
2023-07-15 09:23:48 -04:00
if (!fromDeath) {
for (Item it : mob.getInventory()) {
ItemBase ib = it.getItemBase();
if (ib.isDiscRune() || ib.getName().toLowerCase().contains("of the gods")) {
ChatSystemMsg chatMsg = new ChatSystemMsg(null, mob.getName() + " in " + mob.getParentZone().getName() + " has found the " + ib.getName() + ". Are you tough enough to take it?");
chatMsg.setMessageType(10);
chatMsg.setChannel(Enum.ChatChannelType.SYSTEM.getChannelID());
DispatchMessage.dispatchMsgToAll(chatMsg);
}
2023-04-06 20:07:24 -05:00
}
2023-04-07 12:22:33 -05:00
}
}
2023-07-15 09:23:48 -04:00
2023-04-15 11:52:19 -05:00
private static void RunBootySet(ArrayList<BootySetEntry> entries, Mob mob, float multiplier, boolean inHotzone, boolean fromDeath) {
2023-07-15 09:23:48 -04:00
if (fromDeath) {
//do equipment here
2023-07-15 09:23:48 -04:00
if (mob.getEquip() != null) {
for (MobEquipment me : mob.getEquip().values()) {
if (me.getDropChance() == 0)
continue;
float equipmentRoll = ThreadLocalRandom.current().nextFloat();
float dropChance = me.getDropChance();
2023-07-15 09:23:48 -04:00
if (equipmentRoll < dropChance * multiplier) {
MobLoot ml = new MobLoot(mob, me.getItemBase(), false);
mob.getCharItemManager().addItemToInventory(ml);
}
}
}
return;
}
2023-04-07 13:09:15 -05:00
for (BootySetEntry bse : entries) {
int roll = ThreadLocalRandom.current().nextInt(100);
2023-04-07 13:09:15 -05:00
switch (bse.bootyType) {
2023-04-07 11:32:53 -05:00
case "GOLD":
if (roll > (bse.dropChance * multiplier)) {
2023-04-15 11:52:19 -05:00
//early exit, failed to hit minimum chance roll OR booty was generated from mob's death
2023-04-07 12:22:33 -05:00
break;
}
2023-04-07 11:32:53 -05:00
//determine and add gold to mob inventory
int gold = new Random().nextInt(bse.highGold - bse.lowGold) + bse.lowGold;
if (gold > 0) {
MobLoot goldAmount = new MobLoot(mob, (int) (gold * multiplier));
mob.getCharItemManager().addItemToInventory(goldAmount);
}
break;
case "LOOT":
if (roll > (bse.dropChance * multiplier)) {
2023-04-15 11:52:19 -05:00
//early exit, failed to hit minimum chance roll OR booty was generated from mob's death
2023-04-07 12:22:33 -05:00
break;
}
2023-04-07 11:32:53 -05:00
//iterate the booty tables and add items to mob inventory
2023-04-07 13:38:09 -05:00
MobLoot toAdd = getGenTableItem(bse.lootTable, mob);
2023-04-07 13:09:15 -05:00
if (toAdd != null) {
2023-04-07 11:32:53 -05:00
mob.getCharItemManager().addItemToInventory(toAdd);
}
if (inHotzone) {
2023-04-07 14:48:15 -05:00
int lootTableID = bse.lootTable + 1;
MobLoot toAddHZ = getGenTableItem(lootTableID, mob);
2023-04-07 13:09:15 -05:00
if (toAddHZ != null)
2023-04-07 11:32:53 -05:00
mob.getCharItemManager().addItemToInventory(toAddHZ);
2023-04-07 13:09:15 -05:00
2023-04-07 11:32:53 -05:00
}
break;
case "ITEM":
2023-04-07 13:09:15 -05:00
MobLoot disc = new MobLoot(mob, ItemBase.getItemBase(bse.itemBase), true);
2023-04-16 12:48:23 -05:00
if (disc != null && !fromDeath)
2023-04-07 11:32:53 -05:00
mob.getCharItemManager().addItemToInventory(disc);
2023-04-07 13:09:15 -05:00
2023-04-07 11:32:53 -05:00
break;
2023-04-06 20:07:24 -05:00
}
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static MobLoot getGenTableItem(int genTableID, Mob mob) {
if (genTableID == 0 || mob == null || generalItemTables.containsKey(genTableID) == false) {
2023-04-07 12:05:01 -05:00
return null;
}
2023-04-07 13:09:15 -05:00
MobLoot outItem;
int roll = new Random().nextInt(101);
2023-04-07 14:48:15 -05:00
GenTableRow selectedRow = generalItemTables.get(genTableID).getRowForRange(roll);
2023-07-15 09:23:48 -04:00
if (selectedRow == null) {
return null;
2023-04-07 13:09:15 -05:00
}
int itemTableId = selectedRow.itemTableID;
2023-04-08 14:19:47 -05:00
int minRollRange = mob.getLevel() + roll + mob.getParentZone().minLvl;
2023-04-08 21:49:17 -05:00
//add 20 to max roll range to make dwarven HA and Sage possible
int maxRollRange = (mob.getLevel() * 2) + roll + 20 + (mob.getParentZone().maxLvl * 2);
2023-07-15 09:23:48 -04:00
if (maxRollRange > 320) {
2023-04-08 14:19:47 -05:00
maxRollRange = 320;
}
2023-07-15 09:23:48 -04:00
int roll2 = new Random().nextInt(maxRollRange - minRollRange) + minRollRange;
2023-04-07 14:48:15 -05:00
ItemTableRow tableRow = itemTables.get(itemTableId).getRowForRange(roll2);
2023-07-15 09:23:48 -04:00
if (tableRow == null) {
2023-04-07 13:09:15 -05:00
return null;
}
int itemUUID = tableRow.cacheID;
2023-07-15 09:23:48 -04:00
if (itemUUID == 0) {
2023-04-06 20:07:24 -05:00
return null;
}
2023-07-15 09:23:48 -04:00
if (ItemBase.getItemBase(itemUUID).getType().ordinal() == Enum.ItemType.RESOURCE.ordinal()) {
2023-04-07 15:41:40 -05:00
int amount = ThreadLocalRandom.current().nextInt(tableRow.maxSpawn - tableRow.minSpawn) + tableRow.minSpawn;
2023-07-15 09:23:48 -04:00
return new MobLoot(mob, ItemBase.getItemBase(itemUUID), amount, false);
2023-04-06 20:07:24 -05:00
}
2023-04-07 15:41:40 -05:00
outItem = new MobLoot(mob, ItemBase.getItemBase(itemUUID), false);
Enum.ItemType outType = outItem.getItemBase().getType();
2023-07-15 09:23:48 -04:00
if (outType.ordinal() == Enum.ItemType.WEAPON.ordinal() || outType.ordinal() == Enum.ItemType.ARMOR.ordinal() || outType.ordinal() == Enum.ItemType.JEWELRY.ordinal()) {
if (outItem.getItemBase().isGlass() == false) {
ModTypeTable prefixTable = modTypeTables.get(selectedRow.pModTable);
ModTypeTable suffixTable = modTypeTables.get(selectedRow.sModTable);
if (modTables.get(prefixTable.getRowForRange(100).modTableID) != null) {
ModTable prefixModTable = modTables.get(prefixTable.getRowForRange(100).modTableID);
ModTableRow prefixMod = prefixModTable.getRowForRange(new Random().nextInt(maxRollRange) + minRollRange);
if (prefixMod != null && prefixMod.action.length() > 0) {
outItem.setPrefix(prefixMod.action);
outItem.addPermanentEnchantment(prefixMod.action, 0, prefixMod.level, true);
}
}
if (modTables.get(suffixTable.getRowForRange(100).modTableID) != null) {
ModTable suffixModTable = modTables.get(suffixTable.getRowForRange(100).modTableID);
ModTableRow suffixMod = suffixModTable.getRowForRange(new Random().nextInt(maxRollRange) + minRollRange);
if (suffixMod != null && suffixMod.action.length() > 0) {
outItem.setSuffix(suffixMod.action);
outItem.addPermanentEnchantment(suffixMod.action, 0, suffixMod.level, true);
}
}
}
2023-04-06 20:07:24 -05:00
}
return outItem;
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static void AddGenTableRow(int tableID, GenTableRow row) {
if (!generalItemTables.containsKey(tableID)) {
2023-04-06 20:07:24 -05:00
//create the new table
2023-04-06 21:06:23 -05:00
GenTable gt = new GenTable();
gt.rows.add(row);
2023-07-15 09:23:48 -04:00
generalItemTables.put(tableID, gt);
} else {
2023-04-06 20:07:24 -05:00
//add row to existing table
GenTable toAdd = generalItemTables.get(tableID);
toAdd.rows.add(row);
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static void AddItemTableRow(int tableID, ItemTableRow row) {
if (!itemTables.containsKey(tableID)) {
2023-04-06 20:07:24 -05:00
//create the new table
2023-04-06 21:06:23 -05:00
ItemTable it = new ItemTable();
it.rows.add(row);
2023-07-15 09:23:48 -04:00
itemTables.put(tableID, it);
} else {
2023-04-06 20:07:24 -05:00
//add row to existing table
ItemTable toAdd = itemTables.get(tableID);
toAdd.rows.add(row);
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static void AddModTypeTableRow(int tableID, ModTypeTableRow row) {
if (!modTypeTables.containsKey(tableID)) {
2023-04-06 20:07:24 -05:00
//create the new table
2023-04-06 21:06:23 -05:00
ModTypeTable mtt = new ModTypeTable();
mtt.rows.add(row);
2023-07-15 09:23:48 -04:00
modTypeTables.put(tableID, mtt);
} else {
2023-04-06 20:07:24 -05:00
//add row to existing table
ModTypeTable toAdd = modTypeTables.get(tableID);
toAdd.rows.add(row);
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static void AddModTableRow(int tableID, ModTableRow row) {
if (!modTables.containsKey(tableID)) {
2023-04-06 20:07:24 -05:00
//create the new table
2023-04-06 21:06:23 -05:00
ModTable mt = new ModTable();
mt.rows.add(row);
2023-07-15 09:23:48 -04:00
modTables.put(tableID, mt);
} else {
2023-04-06 20:07:24 -05:00
//add row to existing table
ModTable toAdd = modTables.get(tableID);
toAdd.rows.add(row);
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static class GenTable {
2023-04-06 20:07:24 -05:00
public ArrayList<GenTableRow> rows = new ArrayList<GenTableRow>();
2023-07-15 09:23:48 -04:00
public GenTableRow getRowForRange(int roll) {
2023-04-06 20:07:24 -05:00
GenTableRow outRow = null;
2023-07-15 09:23:48 -04:00
for (GenTableRow iteration : this.rows) {
if (roll >= iteration.minRoll && roll <= iteration.maxRoll) {
2023-04-06 20:07:24 -05:00
outRow = iteration;
}
}
return outRow;
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static class ItemTable {
2023-04-06 20:07:24 -05:00
public ArrayList<ItemTableRow> rows = new ArrayList<ItemTableRow>();
2023-07-15 09:23:48 -04:00
public ItemTableRow getRowForRange(int roll) {
if (roll > 320) {
2023-04-06 20:07:24 -05:00
roll = 320;
}
ItemTableRow outRow = null;
2023-07-15 09:23:48 -04:00
for (ItemTableRow iteration : this.rows) {
if (roll >= iteration.minRoll && roll <= iteration.maxRoll) {
2023-04-06 20:07:24 -05:00
outRow = iteration;
}
}
return outRow;
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static class ModTypeTable {
2023-04-06 20:07:24 -05:00
public ArrayList<ModTypeTableRow> rows = new ArrayList<ModTypeTableRow>();
2023-07-15 09:23:48 -04:00
public ModTypeTableRow getRowForRange(int roll) {
2023-04-06 20:07:24 -05:00
ModTypeTableRow outRow = null;
2023-07-15 09:23:48 -04:00
for (ModTypeTableRow iteration : this.rows) {
if (roll >= iteration.minRoll && roll <= iteration.maxRoll) {
2023-04-07 15:41:40 -05:00
return iteration;
2023-04-06 20:07:24 -05:00
}
}
return outRow;
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static class ModTable {
2023-04-06 20:07:24 -05:00
public ArrayList<ModTableRow> rows = new ArrayList<ModTableRow>();
2023-07-15 09:23:48 -04:00
public ModTableRow getRowForRange(int roll) {
if (roll > 320) {
2023-04-06 20:07:24 -05:00
roll = 320;
}
ModTableRow outRow = null;
2023-07-15 09:23:48 -04:00
for (ModTableRow iteration : this.rows) {
if (roll >= iteration.minRoll && roll <= iteration.maxRoll) {
2023-04-06 20:07:24 -05:00
outRow = iteration;
}
}
return outRow;
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static class GenTableRow {
2023-04-06 20:07:24 -05:00
public int minRoll;
public int maxRoll;
public int itemTableID;
public int pModTable;
public int sModTable;
2023-07-15 09:23:48 -04:00
2023-04-06 20:07:24 -05:00
public GenTableRow(ResultSet rs) throws SQLException {
this.minRoll = rs.getInt("minRoll");
this.maxRoll = rs.getInt("maxRoll");
this.itemTableID = rs.getInt("lootTableID");
this.pModTable = rs.getInt("pModTableID");
this.sModTable = rs.getInt("sModTableID");
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
public static class ItemTableRow {
2023-04-06 20:07:24 -05:00
public int minRoll;
public int maxRoll;
public int cacheID;
2023-04-07 15:41:40 -05:00
public int minSpawn;
public int maxSpawn;
2023-07-15 09:23:48 -04:00
2023-04-06 20:07:24 -05:00
public ItemTableRow(ResultSet rs) throws SQLException {
this.minRoll = rs.getInt("minRoll");
this.maxRoll = rs.getInt("maxRoll");
this.cacheID = rs.getInt("itemBaseUUID");
2023-04-07 15:41:40 -05:00
this.minSpawn = rs.getInt("minSpawn");
this.maxSpawn = rs.getInt("maxSpawn");
2023-04-06 20:07:24 -05:00
}
}
2023-07-15 09:23:48 -04:00
public static class ModTypeTableRow {
2023-04-06 20:07:24 -05:00
public int minRoll;
public int maxRoll;
public int modTableID;
2023-07-15 09:23:48 -04:00
2023-04-06 20:07:24 -05:00
public ModTypeTableRow(ResultSet rs) throws SQLException {
this.minRoll = rs.getInt("minRoll");
this.maxRoll = rs.getInt("maxRoll");
this.modTableID = rs.getInt("subTableID");
}
}
2023-07-15 09:23:48 -04:00
public static class ModTableRow {
2023-04-06 20:07:24 -05:00
public int minRoll;
public int maxRoll;
public String action;
public int level;
2023-07-15 09:23:48 -04:00
2023-04-06 20:07:24 -05:00
public ModTableRow(ResultSet rs) throws SQLException {
this.minRoll = rs.getInt("minRoll");
this.maxRoll = rs.getInt("maxRoll");
this.action = rs.getString("action");
this.level = rs.getInt("level");
}
2022-04-30 09:41:17 -04:00
}
}