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.* ;
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
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 < > ( ) ;
2022-04-30 09:41:17 -04:00
2023-04-06 20:07:24 -05:00
private LootManager ( ) { }
// 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-04-06 20:07:24 -05:00
public static void GenerateMobLoot ( Mob mob ) {
//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-04-06 20:10:42 -05:00
for ( BootySetEntry bse : NPCManager . _bootySetMap . get ( mob . getMobBase ( ) . bootySet ) ) {
2023-04-06 20:07:24 -05:00
//check if chance roll is good
if ( ThreadLocalRandom . current ( ) . nextInt ( 100 ) < = ( bse . dropChance * multiplier ) ) {
//early exit, failed to hit minimum chance roll
continue ;
}
if ( bse . bootyType . equals ( " GOLD " ) ) {
//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 ) ;
}
2023-04-07 11:22:14 -05:00
} else if ( bse . bootyType . equals ( " LOOT " ) ) {
2023-04-06 20:07:24 -05:00
//iterate the booty tables and add items to mob inventory
Item toAdd = getGenTableItem ( bse . lootTable , mob ) ;
if ( toAdd ! = null ) {
mob . getCharItemManager ( ) . addItemToInventory ( toAdd ) ;
}
if ( inHotzone ) {
Item toAddHZ = getGenTableItem ( bse . lootTable + 1 , mob ) ;
mob . getCharItemManager ( ) . addItemToInventory ( toAddHZ ) ;
}
2023-04-07 11:22:14 -05:00
} else if ( bse . bootyType . equals ( " ITEM " ) ) {
if ( Item . getItem ( bse . itemBase ) ! = null ) {
mob . getCharItemManager ( ) . addItemToInventory ( Item . getItem ( bse . itemBase ) ) ;
}
2023-04-06 20:07:24 -05:00
}
}
//lastly, check mobs inventory for godly or disc runes to send a server announcement
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 ) ;
}
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 20:07:24 -05:00
public static Item getGenTableItem ( int genTableID , Mob mob ) {
Item outItem ;
int minRollRange = mob . getParentZone ( ) . minLvl + mob . getLevel ( ) ;
int maxRollRange = ( mob . getParentZone ( ) . minLvl + mob . getLevel ( ) + mob . getParentZone ( ) . maxLvl ) * 2 ;
GenTableRow selectedRow = generalItemTables . get ( genTableID ) . getRowForRange ( new Random ( ) . nextInt ( 100 ) ) ;
int itemUUID = itemTables . get ( selectedRow . itemTableID ) . getRowForRange ( new Random ( ) . nextInt ( maxRollRange ) + minRollRange ) . cacheID ;
if ( itemUUID = = 0 ) {
return null ;
}
ModTypeTable prefixTable = modTypeTables . get ( selectedRow . pModTable ) ;
ModTypeTable suffixTable = modTypeTables . get ( selectedRow . sModTable ) ;
ModTable prefixModTable = modTables . get ( prefixTable . getRowForRange ( 100 ) . modTableID ) ;
ModTable suffixModTable = modTables . get ( suffixTable . getRowForRange ( 100 ) . modTableID ) ;
ModTableRow prefixMod = prefixModTable . getRowForRange ( new Random ( ) . nextInt ( maxRollRange ) + minRollRange ) ;
ModTableRow suffixMod = suffixModTable . getRowForRange ( new Random ( ) . nextInt ( maxRollRange ) + minRollRange ) ;
outItem = Item . getItem ( itemUUID ) ;
if ( prefixMod . action . length ( ) > 0 ) {
outItem . addPermanentEnchantment ( prefixMod . action , prefixMod . level ) ;
}
if ( suffixMod . action . length ( ) > 0 ) {
outItem . addPermanentEnchantment ( suffixMod . action , suffixMod . level ) ;
}
return outItem ;
2022-04-30 09:41:17 -04:00
}
2023-04-06 21:06:23 -05:00
public static void AddGenTableRow ( int tableID , GenTableRow row ) {
2023-04-06 20:07:24 -05:00
if ( ! generalItemTables . containsKey ( tableID ) ) {
//create the new table
2023-04-06 21:06:23 -05:00
GenTable gt = new GenTable ( ) ;
gt . rows . add ( row ) ;
generalItemTables . put ( tableID , gt ) ;
2023-04-06 20:07:24 -05:00
} else {
//add row to existing table
GenTable toAdd = generalItemTables . get ( tableID ) ;
toAdd . rows . add ( row ) ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 21:06:23 -05:00
public static void AddItemTableRow ( int tableID , ItemTableRow row ) {
2023-04-06 20:07:24 -05:00
if ( ! itemTables . containsKey ( tableID ) ) {
//create the new table
2023-04-06 21:06:23 -05:00
ItemTable it = new ItemTable ( ) ;
it . rows . add ( row ) ;
itemTables . put ( tableID , it ) ;
2023-04-06 20:07:24 -05:00
} else {
//add row to existing table
ItemTable toAdd = itemTables . get ( tableID ) ;
toAdd . rows . add ( row ) ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 21:06:23 -05:00
public static void AddModTypeTableRow ( int tableID , ModTypeTableRow row ) {
2023-04-06 20:07:24 -05:00
if ( ! modTypeTables . containsKey ( tableID ) ) {
//create the new table
2023-04-06 21:06:23 -05:00
ModTypeTable mtt = new ModTypeTable ( ) ;
mtt . rows . add ( row ) ;
modTypeTables . put ( tableID , mtt ) ;
2023-04-06 20:07:24 -05:00
} else {
//add row to existing table
ModTypeTable toAdd = modTypeTables . get ( tableID ) ;
toAdd . rows . add ( row ) ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 21:06:23 -05:00
public static void AddModTableRow ( int tableID , ModTableRow row ) {
2023-04-06 20:07:24 -05:00
if ( ! itemTables . containsKey ( tableID ) ) {
//create the new table
2023-04-06 21:06:23 -05:00
ModTable mt = new ModTable ( ) ;
mt . rows . add ( row ) ;
modTables . put ( tableID , mt ) ;
2023-04-06 20:07:24 -05:00
} else {
//add row to existing table
ModTable toAdd = modTables . get ( tableID ) ;
toAdd . rows . add ( row ) ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 20:07:24 -05:00
public static class GenTable {
public ArrayList < GenTableRow > rows = new ArrayList < GenTableRow > ( ) ;
public GenTableRow getRowForRange ( int roll ) {
GenTableRow outRow = null ;
for ( GenTableRow iteration : this . rows ) {
if ( iteration . minRoll > = roll & & iteration . maxRoll < = roll ) {
outRow = iteration ;
}
}
return outRow ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 20:07:24 -05:00
public static class ItemTable {
public ArrayList < ItemTableRow > rows = new ArrayList < ItemTableRow > ( ) ;
public ItemTableRow getRowForRange ( int roll ) {
if ( roll > 320 ) {
roll = 320 ;
}
ItemTableRow outRow = null ;
for ( ItemTableRow iteration : this . rows ) {
if ( iteration . minRoll > = roll & & iteration . maxRoll < = roll ) {
outRow = iteration ;
}
}
return outRow ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 20:07:24 -05:00
public static class ModTypeTable {
public ArrayList < ModTypeTableRow > rows = new ArrayList < ModTypeTableRow > ( ) ;
public ModTypeTableRow getRowForRange ( int roll ) {
ModTypeTableRow outRow = null ;
for ( ModTypeTableRow iteration : this . rows ) {
if ( iteration . minRoll > = roll & & iteration . maxRoll < = roll ) {
outRow = iteration ;
}
}
return outRow ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 20:07:24 -05:00
public static class ModTable {
public ArrayList < ModTableRow > rows = new ArrayList < ModTableRow > ( ) ;
public ModTableRow getRowForRange ( int roll ) {
if ( roll > 320 ) {
roll = 320 ;
}
ModTableRow outRow = null ;
for ( ModTableRow iteration : this . rows ) {
if ( iteration . minRoll > = roll & & iteration . maxRoll < = roll ) {
outRow = iteration ;
}
}
return outRow ;
}
2022-04-30 09:41:17 -04:00
}
2023-04-06 20:07:24 -05:00
public static class GenTableRow {
public int minRoll ;
public int maxRoll ;
public int itemTableID ;
public int pModTable ;
public int sModTable ;
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-04-06 20:07:24 -05:00
public static class ItemTableRow {
public int minRoll ;
public int maxRoll ;
public int cacheID ;
public ItemTableRow ( ResultSet rs ) throws SQLException {
this . minRoll = rs . getInt ( " minRoll " ) ;
this . maxRoll = rs . getInt ( " maxRoll " ) ;
this . cacheID = rs . getInt ( " itemBaseUUID " ) ;
}
}
public static class ModTypeTableRow {
public int minRoll ;
public int maxRoll ;
public int modTableID ;
public ModTypeTableRow ( ResultSet rs ) throws SQLException {
this . minRoll = rs . getInt ( " minRoll " ) ;
this . maxRoll = rs . getInt ( " maxRoll " ) ;
this . modTableID = rs . getInt ( " subTableID " ) ;
}
}
public static class ModTableRow {
public int minRoll ;
public int maxRoll ;
public String action ;
public int level ;
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
}
}