Post: [PI] Drop System
04-08-2015, 10:40 PM #1
Stunz
Former Staff
(adsbygoogle = window.adsbygoogle || []).push({});
Within your NPCHandler:
    
public static NPCDrop npcDropList[][] = new NPCDrop[NPCHandler.maxListedNPCs][];


Still within your NPCHandler class search:
    
public void intialize()


Within that method add this:

    
/*
* When public static void main within the Server class is read so will this configuration file.
*/

loadNPCDrops("./Data/CFG/npc_drops.cfg");


Do not forget to create that (npc_drops.cfg) file!

Add this boolean within your NPCHandler class.

    
/**
*
* @param FileName
* @return NPC drops
* @start Used for relaying a console print out of intialization times.
* @dropsCount Used for relaying a console print out of the number of drops.
* @ & used to separate drops amounts of the same type of item by amount. Example chickens: drop 5, 10 or 15 feathers, not a range. TODO finish
* @ : used to separate drops using a range. Example goblins: drop 1-24 coins.
* @ / used to indicate multiple NPC's that have the same drops. Example cows: 2310/1768/1767/1766/955/397/81 are all the ID's for cows, which share a common droptable.
*/

public boolean loadNPCDrops(final String FileName) {
long start = System.currentTimeMillis();
int dropsCount = 0;
int npcCount = 0;
String line = "";
String token = "";
String token2 = "";
String token2_2 = "";
String[] token3 = new String[10];
boolean EndOfFile = false;
BufferedReader characterfile = null;
try {
characterfile = new BufferedReader(new FileReader("./" + FileName));
} catch (final FileNotFoundException fileex) {
Misc.println(FileName + ": file not found.");
return false;
}
try {
line = characterfile.readLine();
} catch (final IOException ioexception) {
Misc.println(FileName + ": error loading file.");
return false;
}
while (EndOfFile == false && line != null) {
line = line.trim();
final int spot = line.indexOf("=");
if (spot > -1) {
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token2 = token2.trim();
token2_2 = token2.replaceAll("\t\t", "\t");
while (token2_2.indexOf("\t\t") > -1) {
token2_2 = token2_2.replaceAll("\t\t", "\t");
}
token3 = token2_2.split("\t");
if (token.equals("drop")) {
String[] slotTokens = token3[0].trim().split("/");
for (String slotToken : slotTokens) {
if (slotToken.length() > 0) {
int slot = Integer.parseInt(slotToken.trim());
List<NPCDrop> newNpcDrops = new LinkedList<NPCDrop>();
for (int i = 1; i <= token3.length - 1; i++) {
String[] token4 = token3[i].trim().split(":");
NPCDrop npcDrop = null;
if (token4.length == 3) {
String amounts = token4[1].trim();
if (amounts.indexOf("&") > -1) {
String[] token5 = amounts.split("&");
for (String amount : token5) {
amount = amount.trim();
if (amount.length() > 0) {
npcDrop = new NPCDrop(slot, Integer.parseInt(token4[0].trim()), Integer.parseInt(amount), token4[2].trim());
if (npcDrop != null) {
newNpcDrops.add(npcDrop);
}
}
}
} else {
npcDrop = new NPCDrop(slot, Integer.parseInt(token4[0].trim()), Integer.parseInt(token4[1].trim()), token4[2].trim());
if (npcDrop != null) {
newNpcDrops.add(npcDrop);
}
}
}
if (token4.length == 4) {
npcDrop = new NPCDrop(slot, Integer.parseInt(token4[0].trim()), Integer.parseInt(token4[1].trim()), Integer.parseInt(token4[2].trim()), token4[2].trim());
if (npcDrop != null) {
newNpcDrops.add(npcDrop);
}
}
}
npcDropList[slot] = newNpcDrops.toArray(new NPCDrop[0]);
dropsCount += npcDropList[slot].length;
npcCount += 1;
}
}
}
} else if (line.equals("[ENDOFDROPLIST]")) {
try {
characterfile.close();
} catch (final IOException ioexception) {
}
System.out.println(" Loaded " + dropsCount + " npc drops for " + npcCount + " npcs in " + (System.currentTimeMillis() - start) + "ms.");
return true;
}
try {
line = characterfile.readLine();
} catch (final IOException ioexception1) {
EndOfFile = true;
}
}
try {
characterfile.close();
} catch (final IOException ioexception) {
}
return false;
}


Add this below the above boolean.

    
/**
*
* @param value
* @return Percent Rarities
* Float used rather than a double.
*/

public float getRarity(String value) {
float dropRarity = 0.0f;
if (value.equalsIgnoreCase("ALWAYS")) {
dropRarity = 100.0f;
} else if (value.equalsIgnoreCase("VERY_COMMON")) {
dropRarity = 75.0f;
} else if (value.equalsIgnoreCase("COMMON")) {
int x = server.Misc.random(2, 50); // 1 in 2 through 50 kills will give this item
dropRarity = (float) (1 - (1 - (1/x))) * 100;
} else if (value.equalsIgnoreCase("UNCOMMON")) {
int x = server.Misc.random(51, 100); // 1 in 51 through 100 kills will give this item
dropRarity = (float) (1 - (1 - (1/x))) * 100;
} else if (value.equalsIgnoreCase("RARE")) {
int x = server.Misc.random(101, 512); // 1 in 101 through 512 kills will give this item
dropRarity = (float) (1 - (1 - (1/x))) * 100;
} else if (value.equalsIgnoreCase("VERY_RARE")) {
int x = server.Misc.random(513, 1024); // 1 in 513 through 1024 kills will give this item
dropRarity = (float) (1 - (1 - (1/x))) * 100;
} else if (value.equalsIgnoreCase("SUPER_RARE")) {
int x = server.Misc.random(1025, 4096); // 1 in 1025 through 4096 kills will give this item
dropRarity = (float) (1 - (1 - (1/x))) * 100;
} else if (value.equalsIgnoreCase("EXTREMELY_RARE")) {
int x = server.Misc.random(4097, 8192); // 1 in 4097 through 8192 kills will give this item
dropRarity = (float) (1 - (1 - (1/x))) * 100;
} else {
dropRarity = Float.parseFloat(value);
}
return dropRarity;
}


Finally, add:
    
class NPCDrop {
public int npcId;
public int itemId;
public int itemAmount = -1;
public int itemAmount2 = -1;
public String rarity;

public NPCDrop(int npcId, int itemId, int itemAmount, String percentRarity) {
this.npcId = npcId;
this.itemId = itemId;
this.itemAmount = itemAmount;
this.rarity = percentRarity;
}

public NPCDrop(int npcId, int itemId, int itemAmount, int itemAmount2, String percentRarity) {
this.npcId = npcId;
this.itemId = itemId;
this.itemAmount = itemAmount;
this.itemAmount2 = itemAmount2;
this.rarity = percentRarity;
}
}


Last, but not least, add these within your Misc class.

    
public static int random(final float range) {
return (int) (java.lang.Math.random() * (range + 1));
}

public static int random(int range1, int range2) { // 0 till range (range INCLUDED)
int keepGoing = 1;
int compare1 = 0;
while (keepGoing == 1) {
compare1 = random(range2);
if (compare1 >= range1) {
keepGoing = 0;
} else {
keepGoing = 1;
}
}
return compare1;
}


There you go!

But, just for the hell of it, here's my RareDropTable system as well.

NPCHandler class import this.

    
import server.game.npcs.RareDropTable.RareDrops;


Still within NPCHandler search this:

    
public void dropItems


Within that method add this.

    
if (npcDropList[NPCHandler.npcs[i].npcType] != null) {
for (NPCDrop drop : npcDropList[NPCHandler.npcs[i].npcType]) {
if (NPCHandler.npcs[i].npcType == drop.npcId) {
float dropRarity = getRarity(drop.rarity);
if (server.Misc.random(100.0f) <= dropRarity) {
if (drop.itemAmount2 != -1) {
int itemAmount = drop.itemAmount;
int itemAmount2 = drop.itemAmount2;
if (drop.itemAmount > drop.itemAmount2) {
itemAmount2 = drop.itemAmount;
itemAmount = drop.itemAmount2;
}
int amount = server.Misc.random(itemAmount, itemAmount2);
Server.itemHandler.createGroundItem(c, drop.itemId, NPCHandler.npcs[i].absX, NPCHandler.npcs[i].absY, amount, c.playerId);
} else {
Server.itemHandler.createGroundItem(c, drop.itemId, NPCHandler.npcs[i].absX, NPCHandler.npcs[i].absY, drop.itemAmount, c.playerId);
}
}
}
}
}
if (server.Misc.random(100.0f) <= 100 * (((float) npcList[NPCHandler.npcs[i].npcType].npcCombat) / 25000)) {
RareDrops drop = RareDrops.getRandomDrop(c);
if (!drop.equals(RareDrops.EMPTY)) {
c.sendMessage("You received a drop from the rare drop table.");
c.getItems().addItem(drop.getItemId(), drop.getAmount());
}
System.out.println(c.playerName2 + " received a drop from the rare drop table. " + drop.name());
}


Now create a new file called RareDropTable.java.


    
package //TODO

import server.model.players.Client;
import server.util.Misc;

public class RareDropTable {

/**
*
* @author Skiire
* @date Apr 10, 2012
* RareDrops enumeration used to store all rare drops and quantities.
* TODO: Fix the Misc.random() bug. Where users who receive a Misc.random() drop would set the drop amount until a server reset. This is not a huge issue as this is very rare and the chances of two of the same RareDrop in one server session is highly unlikely and hardly noticable.
*/

public enum RareDrops {

COINS(995, new int[] {250, 3000, 12000}),
UNCUT_SAPPHIRE(1623, 1),
UNCUT_EMERALD(1621, 1),
UNCUT_RUBY(1619, 1),
UNCUT_DIAMOND(1617, 1),
UNCUT_DRAGONSTONE(1631, 1),
DRAGONSTONE(1615, 1),
LOOP_HALF_OF_A_KEY(987, 1),
TOOTH_HALF_OF_A_KEY(985, 1),
ADAMANT_JAVELIN(829, 20),
RUNE_JAVELIN(830, 5),
RUNE_SPEAR(1247, 1),
DRAGON_SPEAR(1249, 1),
DRAGON_DAGGER(new int[] {1215, 1216}, new int[] {1, 50}),
RUNE_KITESHIELD(1201, 1),
SHIELD_LEFT_HALF(2366, 1),
DRAGON_MED_HELM(1149, 1),
RUNE_ARROW(892, new int[] {150, 200, 500}),
ADAMANT_BOLTS(9143, 200),
ONYX_BOLTS(9342, 150),
BATTLESTAFF(1392, 200),
AIR_ORB(573, 1000),
FIRE_ORB(569, 1000),
PURE_ESSENCE(7937, new int[] {500, 15000}),
NATURE_RUNE(561, new int[] {47, 62 + Misc.random(5)}),
SOUL_RUNE(566, 20),
BLOOD_RUNE(565, 50),
WATER_TALISMAN(1444, 1),
EARTH_TALISMAN(1441, 25 + Misc.random(10)),
FIRE_TALISMAN(1443, 25 + Misc.random(9)),
CHAOS_TALISMAN(1452, 1),
NATURE_TALISMAN(1462, 1),
RAW_SWORDFISH(372, new int[] {150 + Misc.random(100), 1000}),
RAW_SHARK(384, 250 + Misc.random(250)),
BIG_BONES(533, 150 + Misc.random(350)),
SILVER_ORE(443, 100),
COAL(454, 75 + Misc.random(75)),
ADAMANT_BAR(2362, 1450 + Misc.random(5550)),
RUNITE_ORE(452, 100),
RUNE_BAR(2364, new int[] {1, 50, 100}),
CLEAN_RANARR(258, 33),
CLEAN_TOADFLAX(2999, 25 + Misc.random(225)),
CLEAN_SNAPDRAGON(3001, 30 + Misc.random(90)),
CLEAN_TORSTOL(270, 10 + Misc.random(90)),
SARADOMIN_BREW(6686, new int[] {250, 2500}),
WATERMELON_SEED(5321, 3),
YEW_SEED(5315, 1 + Misc.random(49)),
MAGIC_SEED(5316, 1 + Misc.random(5)),
PALM_TREE_SEED(5289, 10),
SNAPDRAGON_SEED(5300, 1),
TORSTOL_SEED(5304, 1 + Misc.random(30)),
YEW_LOGS(1516, 100 + Misc.random(19900)),
EMPTY(0, 0);

/**
* The information that is being stored in the above enumeration.
*/

private int[] itemIds, variousAmounts;
private int itemId, singleAmount;

/**
*
* @param itemIds
* @param variousAmounts
*/

private RareDrops(int[] itemIds, int[] variousAmounts) {
this.itemIds = itemIds;
this.variousAmounts = variousAmounts;
}

/**
*
* @param itemId
* @param singleAmount
*/

private RareDrops(int itemId, int singleAmount) {
this.itemId = itemId;
this.singleAmount = singleAmount;
}

/**
*
* @param itemIds
* @param singleAmount
*/

private RareDrops(int[] itemIds, int singleAmount) {
this.itemIds = itemIds;
this.singleAmount = singleAmount;
}

/**
*
* @param itemId
* @param variousAmounts
*/

private RareDrops(int itemId, int[] variousAmounts) {
this.itemId = itemId;
this.variousAmounts = variousAmounts;
}

/**
*
* @return itemIds
*/

public int[] getItemIds() {
return this.itemIds;
}

/**
*
* @return variousAmounts
*/

public int[] getVariousAmounts() {
return this.variousAmounts;
}

/**
*
* @return itemId
*/

public int getItemId() {
return this.itemId;
}

/**
*
* @return singleAmount
*/

public int getSingleAmount() {
return this.singleAmount;
}

/**
*
* @return amount(s)
*/

public int getAmount() {
if (this.variousAmounts != null) {
int choice = server.Misc.random(this.variousAmounts.length);
choice = ((choice == this.variousAmounts.length) ? 0 : choice);
return this.variousAmounts[choice];
} else {
return this.singleAmount;
}
}

/**
*
* @param name
* @return RareDrops
*/

public static RareDrops getRareDrops(String name) {
for (RareDrops rd : RareDrops.values()) {
if (rd.name().equalsIgnoreCase(name)) {
return rd;
}
}
return null;
}

/**
*
* @param c
* @return RareDrops chance.
* Additional Ring of Wealth increase on getting an actual drop instead of an empty.
*
* Chance on getting rare drop is minimal. 50% chance of getting NO drop and instead an empty. 10% if wearing a ring of wealth.
*/

public static RareDrops getRandomDrop(Client c) {
float chanceForEmpty = 50.0f;
if (c.playerEquipment[c.playerRing] == 2572) {
chanceForEmpty = 10.0f;
}
if (server.Misc.random(100.0f) >= chanceForEmpty)
return RareDrops.EMPTY;
else
return values()[server.Misc.random(0, values().length)];
}
}
}


And... Because I'm feeling generous, here's a start to NPC drops for the #377 revision. Some NPC's are missing drops for those servers that are packing their 317 clients with this newer revision garbage, so you'll have to redo that. I tried my best to stick to pure #377 items!

P.S. This is your "npc_drops.cfg" Winky Winky

You must login or register to view this content.
05-02-2015, 01:54 AM #2
jerrmy12
At least I can fight
I haven't played rs since 2011, and I have no idea what this is.
05-02-2015, 08:10 PM #3
Stunz
Former Staff
Originally posted by jerrmy12 View Post
I haven't played rs since 2011, and I have no idea what this is.
It's a drop system for Project Insanity source base.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo