Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. package com.elvarg.game.entity.impl.npc;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Optional;
  7.  
  8. import com.elvarg.game.definition.NpcDropDefinition;
  9. import com.elvarg.game.definition.NpcDropDefinition.DropTable;
  10. import com.elvarg.game.definition.NpcDropDefinition.NPCDrop;
  11. import com.elvarg.game.definition.NpcDropDefinition.RDT;
  12. import com.elvarg.game.entity.impl.grounditem.ItemOnGroundManager;
  13. import com.elvarg.game.entity.impl.player.Player;
  14. import com.elvarg.game.model.Item;
  15. import com.elvarg.game.model.container.impl.Equipment;
  16. import com.elvarg.util.RandomGen;
  17. import com.sun.istack.internal.logging.Logger;
  18. import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
  19.  
  20. public class NPCDropGenerator {
  21.  
  22. /**
  23. * Attempts to start a new generator using
  24. * the given entities.
  25. * @param player
  26. * @param npc
  27. */
  28. public static void start(Player player, NPC npc) {
  29. Optional<NpcDropDefinition> def = NpcDropDefinition.get(npc.getId());
  30. if(def.isPresent()) {
  31. NPCDropGenerator gen = new NPCDropGenerator(player, def.get());
  32. for(Item item : gen.getDropList()) {
  33. ItemOnGroundManager.register(player, item, npc.getPosition());
  34. }
  35. }
  36. }
  37.  
  38. /**
  39. * The {@link Player} whose generating a drop.
  40. */
  41. private final Player player;
  42.  
  43. /**
  44. * The {@link NpcDropDefinition} this drop is for.
  45. */
  46. private final NpcDropDefinition def;
  47.  
  48. /**
  49. * Constructor
  50. * @param player
  51. * @param def
  52. */
  53. public NPCDropGenerator(Player player, NpcDropDefinition def) {
  54. this.player = player;
  55. this.def = def;
  56. }
  57.  
  58. /**
  59. * Generates a list of items from the drop definition
  60. * that will be dropped for a player.
  61. * @return
  62. */
  63. public List<Item> getDropList() {
  64. //The {@RandomGen} which will help us randomize drops..
  65. RandomGen random = new RandomGen();
  66.  
  67. //The list containing the {@link Item} that will be dropped for the player.
  68. List<Item> items = new LinkedList<>();
  69.  
  70. //The list containing the drop tables which we've gone through.
  71. List<DropTable> parsedTables = new ArrayList<DropTable>();
  72.  
  73. //Drop "always" items..
  74. if (def.getAlwaysDrops() != null) {
  75. for(NPCDrop drop : def.getAlwaysDrops()) {
  76. items.add(drop.toItem(random));
  77. }
  78. }
  79.  
  80. //Handle RDT.. If a drop is generated from RDT, no further items should be given.
  81. //There are 128 slots in the rdt, many empty. When a player is wearing ring of
  82. //wealth, the empty slots are not counted.
  83. if(def.getRdtChance() > 0
  84. && random.get().nextInt(def.getRdtChance()) == 0) {
  85. int rdtLength = NpcDropDefinition.RDT.values().length;
  86. int slots = wearingRingOfWealth() ? rdtLength : 128;
  87. int slot = random.get().nextInt(slots);
  88. if(slot < rdtLength) {
  89. RDT rdtDrop = RDT.values()[slot];
  90. if(random.get().nextInt(rdtDrop.getChance()) == 0) {
  91. items.add(new Item(rdtDrop.getItemId(), rdtDrop.getAmount()));
  92. return items;
  93. }
  94. }
  95. }
  96.  
  97. //Handle unique drops..
  98. //The amount of items the player will receive from the unique drop tables.
  99. //Note: A player cannot receive multiple drops from the same drop table.
  100. int rolls = 1 + random.get().nextInt(3);
  101. for(int i = 0; i < rolls; i++) {
  102. Optional<DropTable> table = Optional.empty();
  103.  
  104. //Check if we should access the special drop table..
  105. if(def.getSpecialDrops().length > 0 && !parsedTables.contains(DropTable.SPECIAL)) {
  106. NPCDrop drop = def.getSpecialDrops()[random.get().nextInt(def.getSpecialDrops().length)];
  107. if(random.get().nextInt(drop.getChance()) == 0) {
  108. items.add(drop.toItem(random));
  109. parsedTables.add(DropTable.SPECIAL);
  110. continue;
  111. }
  112. }
  113.  
  114. //If we didn't get a special drop, attempt to find a different table..
  115. if(!table.isPresent()) {
  116. double chance = random.get().nextDouble(100);
  117. if((table = getDropTable(chance)).isPresent()) {
  118. //Make sure we haven't already parsed this table.
  119. if(parsedTables.contains(table.get())) {
  120. continue;
  121. }
  122. //Get the items related to this drop table..
  123. Optional<NPCDrop[]> dropTableItems = Optional.empty();
  124. switch(table.get()) {
  125. case COMMON:
  126. dropTableItems = Optional.of(def.getCommonDrops());
  127. break;
  128. case UNCOMMON:
  129. dropTableItems = Optional.of(def.getUncommonDrops());
  130. break;
  131. case RARE:
  132. dropTableItems = Optional.of(def.getRareDrops());
  133. break;
  134. case VERY_RARE:
  135. dropTableItems = Optional.of(def.getVeryRareDrops());
  136. break;
  137. default:
  138. dropTableItems = Optional.of(def.getCommonDrops());
  139. break;
  140. }
  141. if(!dropTableItems.isPresent()) {
  142. continue;
  143. }
  144. NPCDrop npcDrop = dropTableItems.get()[random.get().nextInt(dropTableItems.get().length)];
  145.  
  146. //Add the drop to the drop list.
  147. items.add(npcDrop.toItem(random));
  148.  
  149. //Flag this table as visited..
  150. parsedTables.add(table.get());
  151. }
  152. }
  153. }
  154. return items;
  155. }
  156.  
  157. /**
  158. * Checks if the player is wearing a ring of wealth
  159. * which will increase the chances for getting a good
  160. * drop.
  161. * @return
  162. */
  163. public boolean wearingRingOfWealth() {
  164. return player.getEquipment().getItems()[Equipment.RING_SLOT].getId() == 2572;
  165. }
  166.  
  167. /**
  168. * Attempts to fetch the drop table for the given chance.
  169. * @param drop
  170. * @return
  171. */
  172. public Optional<DropTable> getDropTable(double chance) {
  173. Optional<DropTable> table = Optional.empty();
  174. //Fetch one of the ordinary drop tables
  175. //based on our chance.
  176. for(DropTable dropTable : DropTable.values()) {
  177. if(dropTable.getRandomRequired() >= 0) {
  178. if(chance <= dropTable.getRandomRequired()) {
  179. table = Optional.of(dropTable);
  180. }
  181. }
  182. }
  183. return table;
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement