MultiSellChoose.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.clientpackets;
  16. import java.util.logging.Logger;
  17. import javolution.util.FastList;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.datatables.ItemTable;
  20. import net.sf.l2j.gameserver.model.L2Augmentation;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2Multisell;
  23. import net.sf.l2j.gameserver.model.PcInventory;
  24. import net.sf.l2j.gameserver.model.L2Multisell.MultiSellEntry;
  25. import net.sf.l2j.gameserver.model.L2Multisell.MultiSellIngredient;
  26. import net.sf.l2j.gameserver.model.L2Multisell.MultiSellListContainer;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  29. import net.sf.l2j.gameserver.network.SystemMessageId;
  30. import net.sf.l2j.gameserver.serverpackets.ItemList;
  31. import net.sf.l2j.gameserver.serverpackets.PledgeShowInfoUpdate;
  32. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  33. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  34. import net.sf.l2j.gameserver.templates.L2Armor;
  35. import net.sf.l2j.gameserver.templates.L2Item;
  36. import net.sf.l2j.gameserver.templates.L2Weapon;
  37. public class MultiSellChoose extends L2GameClientPacket
  38. {
  39. private static final String _C__A7_MULTISELLCHOOSE = "[C] A7 MultiSellChoose";
  40. private static Logger _log = Logger.getLogger(MultiSellChoose.class.getName());
  41. private int _listId;
  42. private int _entryId;
  43. private int _amount;
  44. private int _enchantment;
  45. private int _transactionTax; // local handling of taxation
  46. @Override
  47. protected void readImpl()
  48. {
  49. _listId = readD();
  50. _entryId = readD();
  51. _amount = readD();
  52. // _enchantment = readH(); // <---commented this line because it did NOT work!
  53. _enchantment = _entryId % 100000;
  54. _entryId = _entryId / 100000;
  55. _transactionTax = 0; // initialize tax amount to 0...
  56. }
  57. @Override
  58. public void runImpl()
  59. {
  60. if(_amount < 1 || _amount > 5000)
  61. return;
  62. MultiSellListContainer list = L2Multisell.getInstance().getList(_listId);
  63. if(list == null) return;
  64. L2PcInstance player = getClient().getActiveChar();
  65. if(player == null) return;
  66. for(MultiSellEntry entry : list.getEntries())
  67. {
  68. if(entry.getEntryId() == _entryId)
  69. {
  70. doExchange(player,entry,list.getApplyTaxes(), list.getMaintainEnchantment(), _enchantment);
  71. return;
  72. }
  73. }
  74. }
  75. private void doExchange(L2PcInstance player, MultiSellEntry templateEntry, boolean applyTaxes, boolean maintainEnchantment, int enchantment)
  76. {
  77. PcInventory inv = player.getInventory();
  78. // given the template entry and information about maintaining enchantment and applying taxes
  79. // re-create the instance of the entry that will be used for this exchange
  80. // i.e. change the enchantment level of select ingredient/products and adena amount appropriately.
  81. L2NpcInstance merchant = (player.getTarget() instanceof L2NpcInstance)? (L2NpcInstance) player.getTarget() : null;
  82. if (merchant == null) return;
  83. MultiSellEntry entry = prepareEntry(merchant, templateEntry, applyTaxes, maintainEnchantment, enchantment);
  84. // Generate a list of distinct ingredients and counts in order to check if the correct item-counts
  85. // are possessed by the player
  86. FastList<MultiSellIngredient> _ingredientsList = new FastList<MultiSellIngredient>();
  87. boolean newIng = true;
  88. for(MultiSellIngredient e: entry.getIngredients())
  89. {
  90. newIng = true;
  91. // at this point, the template has already been modified so that enchantments are properly included
  92. // whenever they need to be applied. Uniqueness of items is thus judged by item id AND enchantment level
  93. for(MultiSellIngredient ex: _ingredientsList)
  94. {
  95. // if the item was already added in the list, merely increment the count
  96. // this happens if 1 list entry has the same ingredient twice (example 2 swords = 1 dual)
  97. if( (ex.getItemId() == e.getItemId()) && (ex.getEnchantmentLevel() == e.getEnchantmentLevel()) )
  98. {
  99. if ((double)ex.getItemCount() + e.getItemCount() > Integer.MAX_VALUE) {
  100. player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  101. _ingredientsList.clear();
  102. _ingredientsList = null;
  103. return;
  104. }
  105. ex.setItemCount(ex.getItemCount() + e.getItemCount());
  106. newIng = false;
  107. }
  108. }
  109. if(newIng)
  110. {
  111. // if it's a new ingredient, just store its info directly (item id, count, enchantment)
  112. _ingredientsList.add(L2Multisell.getInstance().new MultiSellIngredient(e));
  113. }
  114. }
  115. // now check if the player has sufficient items in the inventory to cover the ingredients' expences
  116. for(MultiSellIngredient e : _ingredientsList)
  117. {
  118. if((double)e.getItemCount() * _amount > Integer.MAX_VALUE )
  119. {
  120. player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  121. _ingredientsList.clear();
  122. _ingredientsList = null;
  123. return;
  124. }
  125. if(e.getItemId() !=65336)
  126. {
  127. // if this is not a list that maintains enchantment, check the count of all items that have the given id.
  128. // otherwise, check only the count of items with exactly the needed enchantment level
  129. if( inv.getInventoryItemCount(e.getItemId(), maintainEnchantment? e.getEnchantmentLevel() : -1) < ((Config.ALT_BLACKSMITH_USE_RECIPES || !e.getMantainIngredient()) ? (e.getItemCount() * _amount) : e.getItemCount()) )
  130. {
  131. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  132. _ingredientsList.clear();
  133. _ingredientsList = null;
  134. return;
  135. }
  136. }
  137. else
  138. {
  139. if(player.getClan() == null)
  140. {
  141. player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_A_CLAN_MEMBER));
  142. return;
  143. }
  144. if(!player.isClanLeader())
  145. {
  146. player.sendPacket(new SystemMessage(SystemMessageId.ONLY_THE_CLAN_LEADER_IS_ENABLED));
  147. return;
  148. }
  149. if(player.getClan().getReputationScore() < e.getItemCount())
  150. {
  151. player.sendPacket(new SystemMessage(SystemMessageId.THE_CLAN_REPUTATION_SCORE_IS_TOO_LOW));
  152. return;
  153. }
  154. }
  155. }
  156. _ingredientsList.clear();
  157. _ingredientsList = null;
  158. FastList<L2Augmentation> augmentation = new FastList<L2Augmentation>();
  159. /** All ok, remove items and add final product */
  160. for(MultiSellIngredient e : entry.getIngredients())
  161. {
  162. if(e.getItemId()!=65336)
  163. {
  164. L2ItemInstance itemToTake = inv.getItemByItemId(e.getItemId()); // initialize and initial guess for the item to take.
  165. if (itemToTake == null)
  166. { //this is a cheat, transaction will be aborted and if any items already tanken will not be returned back to inventory!
  167. _log.severe("Character: " + player.getName() + " is trying to cheat in multisell, merchatnt id:" + merchant.getNpcId());
  168. return;
  169. }
  170. if (Config.ALT_BLACKSMITH_USE_RECIPES || !e.getMantainIngredient())
  171. {
  172. // if it's a stackable item, just reduce the amount from the first (only) instance that is found in the inventory
  173. if (itemToTake.isStackable())
  174. {
  175. if (!player.destroyItem("Multisell", itemToTake.getObjectId(), (e.getItemCount() * _amount), player.getTarget(), true))
  176. return;
  177. }
  178. else
  179. {
  180. // for non-stackable items, one of two scenaria are possible:
  181. // a) list maintains enchantment: get the instances that exactly match the requested enchantment level
  182. // b) list does not maintain enchantment: get the instances with the LOWEST enchantment level
  183. // a) if enchantment is maintained, then get a list of items that exactly match this enchantment
  184. if (maintainEnchantment)
  185. {
  186. // loop through this list and remove (one by one) each item until the required amount is taken.
  187. L2ItemInstance[] inventoryContents = inv.getAllItemsByItemId(e.getItemId(), e.getEnchantmentLevel());
  188. for (int i = 0; i < (e.getItemCount() * _amount); i++)
  189. {
  190. if (inventoryContents[i].isAugmented())
  191. augmentation.add(inventoryContents[i].getAugmentation());
  192. if (!player.destroyItem("Multisell", inventoryContents[i].getObjectId(), 1, player.getTarget(), true))
  193. return;
  194. }
  195. }
  196. else // b) enchantment is not maintained. Get the instances with the LOWEST enchantment level
  197. {
  198. /* NOTE: There are 2 ways to achieve the above goal.
  199. * 1) Get all items that have the correct itemId, loop through them until the lowest enchantment
  200. * level is found. Repeat all this for the next item until proper count of items is reached.
  201. * 2) Get all items that have the correct itemId, sort them once based on enchantment level,
  202. * and get the range of items that is necessary.
  203. * Method 1 is faster for a small number of items to be exchanged.
  204. * Method 2 is faster for large amounts.
  205. *
  206. * EXPLANATION:
  207. * Worst case scenario for algorithm 1 will make it run in a number of cycles given by:
  208. * m*(2n-m+1)/2 where m is the number of items to be exchanged and n is the total
  209. * number of inventory items that have a matching id.
  210. * With algorithm 2 (sort), sorting takes n*log(n) time and the choice is done in a single cycle
  211. * for case b (just grab the m first items) or in linear time for case a (find the beginning of items
  212. * with correct enchantment, index x, and take all items from x to x+m).
  213. * Basically, whenever m > log(n) we have: m*(2n-m+1)/2 = (2nm-m*m+m)/2 >
  214. * (2nlogn-logn*logn+logn)/2 = nlog(n) - log(n*n) + log(n) = nlog(n) + log(n/n*n) =
  215. * nlog(n) + log(1/n) = nlog(n) - log(n) = (n-1)log(n)
  216. * So for m < log(n) then m*(2n-m+1)/2 > (n-1)log(n) and m*(2n-m+1)/2 > nlog(n)
  217. *
  218. * IDEALLY:
  219. * In order to best optimize the performance, choose which algorithm to run, based on whether 2^m > n
  220. * if ( (2<<(e.getItemCount() * _amount)) < inventoryContents.length )
  221. * // do Algorithm 1, no sorting
  222. * else
  223. * // do Algorithm 2, sorting
  224. *
  225. * CURRENT IMPLEMENTATION:
  226. * In general, it is going to be very rare for a person to do a massive exchange of non-stackable items
  227. * For this reason, we assume that algorithm 1 will always suffice and we keep things simple.
  228. * If, in the future, it becomes necessary that we optimize, the above discussion should make it clear
  229. * what optimization exactly is necessary (based on the comments under "IDEALLY").
  230. */
  231. // choice 1. Small number of items exchanged. No sorting.
  232. for (int i = 1; i <= (e.getItemCount() * _amount); i++)
  233. {
  234. L2ItemInstance[] inventoryContents = inv.getAllItemsByItemId(e.getItemId());
  235. itemToTake = inventoryContents[0];
  236. // get item with the LOWEST enchantment level from the inventory...
  237. // +0 is lowest by default...
  238. if (itemToTake.getEnchantLevel() > 0)
  239. {
  240. for (int j = 0; j < inventoryContents.length; j++)
  241. {
  242. if (inventoryContents[j].getEnchantLevel() < itemToTake.getEnchantLevel())
  243. {
  244. itemToTake = inventoryContents[j];
  245. // nothing will have enchantment less than 0. If a zero-enchanted
  246. // item is found, just take it
  247. if (itemToTake.getEnchantLevel() == 0)
  248. break;
  249. }
  250. }
  251. }
  252. if (!player.destroyItem("Multisell", itemToTake.getObjectId(), 1, player.getTarget(), true))
  253. return;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. else
  260. {
  261. int repCost = player.getClan().getReputationScore() - e.getItemCount();
  262. player.getClan().setReputationScore(repCost, true);
  263. SystemMessage smsg = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  264. smsg.addNumber(e.getItemCount());
  265. player.sendPacket(smsg);
  266. player.getClan().broadcastToOnlineMembers(new PledgeShowInfoUpdate(player.getClan()));
  267. }
  268. }
  269. // Generate the appropriate items
  270. for(MultiSellIngredient e : entry.getProducts())
  271. {
  272. if (ItemTable.getInstance().createDummyItem(e.getItemId()).isStackable())
  273. {
  274. inv.addItem("Multisell", e.getItemId(), (e.getItemCount() * _amount), player, player.getTarget());
  275. } else
  276. {
  277. L2ItemInstance product = null;
  278. for (int i = 0; i < (e.getItemCount() * _amount); i++)
  279. {
  280. product = inv.addItem("Multisell", e.getItemId(), 1, player, player.getTarget());
  281. if (maintainEnchantment)
  282. {
  283. if (i < augmentation.size())
  284. {
  285. product.setAugmentation(new L2Augmentation(product, augmentation.get(i).getAugmentationId(), augmentation.get(i).getSkill(), true));
  286. }
  287. product.setEnchantLevel(e.getEnchantmentLevel());
  288. }
  289. }
  290. }
  291. // msg part
  292. SystemMessage sm;
  293. if (e.getItemCount() * _amount > 1)
  294. {
  295. sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  296. sm.addItemName(e.getItemId());
  297. sm.addNumber(e.getItemCount() * _amount);
  298. player.sendPacket(sm);
  299. sm = null;
  300. }
  301. else
  302. {
  303. if(maintainEnchantment && _enchantment > 0)
  304. {
  305. sm = new SystemMessage(SystemMessageId.ACQUIRED);
  306. sm.addNumber(_enchantment);
  307. sm.addItemName(e.getItemId());
  308. }
  309. else
  310. {
  311. sm = new SystemMessage(SystemMessageId.EARNED_ITEM);
  312. sm.addItemName(e.getItemId());
  313. }
  314. player.sendPacket(sm);
  315. sm = null;
  316. }
  317. }
  318. player.sendPacket(new ItemList(player, false));
  319. StatusUpdate su = new StatusUpdate(player.getObjectId());
  320. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  321. player.sendPacket(su);
  322. su = null;
  323. // finally, give the tax to the castle...
  324. if (merchant != null && merchant.getIsInTown() && merchant.getCastle().getOwnerId() > 0)
  325. merchant.getCastle().addToTreasury(_transactionTax * _amount);
  326. }
  327. // Regarding taxation, the following appears to be the case:
  328. // a) The count of aa remains unchanged (taxes do not affect aa directly).
  329. // b) 5/6 of the amount of aa is taxed by the normal tax rate.
  330. // c) the resulting taxes are added as normal adena value.
  331. // d) normal adena are taxed fully.
  332. // e) Items other than adena and ancient adena are not taxed even when the list is taxable.
  333. // example: If the template has an item worth 120aa, and the tax is 10%,
  334. // then from 120aa, take 5/6 so that is 100aa, apply the 10% tax in adena (10a)
  335. // so the final price will be 120aa and 10a!
  336. private MultiSellEntry prepareEntry(L2NpcInstance merchant, MultiSellEntry templateEntry, boolean applyTaxes, boolean maintainEnchantment, int enchantLevel)
  337. {
  338. MultiSellEntry newEntry = L2Multisell.getInstance().new MultiSellEntry();
  339. newEntry.setEntryId(templateEntry.getEntryId());
  340. int totalAdenaCount = 0;
  341. for (MultiSellIngredient ing : templateEntry.getIngredients())
  342. {
  343. // load the ingredient from the template
  344. MultiSellIngredient newIngredient = L2Multisell.getInstance().new MultiSellIngredient(ing);
  345. if (newIngredient.getItemId() == 57 && newIngredient.isTaxIngredient())
  346. {
  347. double taxRate = 0.0;
  348. if (applyTaxes)
  349. {
  350. if (merchant != null && merchant.getIsInTown())
  351. taxRate = merchant.getCastle().getTaxRate();
  352. }
  353. _transactionTax = (int)Math.round(newIngredient.getItemCount()*taxRate);
  354. totalAdenaCount += _transactionTax;
  355. continue; // do not yet add this adena amount to the list as non-taxIngredient adena might be entered later (order not guaranteed)
  356. }
  357. else if (ing.getItemId() == 57) // && !ing.isTaxIngredient()
  358. {
  359. totalAdenaCount += newIngredient.getItemCount();
  360. continue; // do not yet add this adena amount to the list as taxIngredient adena might be entered later (order not guaranteed)
  361. }
  362. // if it is an armor/weapon, modify the enchantment level appropriately, if necessary
  363. else if (maintainEnchantment)
  364. {
  365. L2Item tempItem = ItemTable.getInstance().createDummyItem(newIngredient.getItemId()).getItem();
  366. if ((tempItem instanceof L2Armor) || (tempItem instanceof L2Weapon))
  367. newIngredient.setEnchantmentLevel(enchantLevel);
  368. }
  369. // finally, add this ingredient to the entry
  370. newEntry.addIngredient(newIngredient);
  371. }
  372. // Next add the adena amount, if any
  373. if (totalAdenaCount > 0)
  374. newEntry.addIngredient(L2Multisell.getInstance().new MultiSellIngredient(57, totalAdenaCount, false, false));
  375. // Now modify the enchantment level of products, if necessary
  376. for (MultiSellIngredient ing : templateEntry.getProducts())
  377. {
  378. // load the ingredient from the template
  379. MultiSellIngredient newIngredient = L2Multisell.getInstance().new MultiSellIngredient(ing);
  380. if (maintainEnchantment)
  381. {
  382. // if it is an armor/weapon, modify the enchantment level appropriately
  383. // (note, if maintain enchantment is "false" this modification will result to a +0)
  384. L2Item tempItem = ItemTable.getInstance().createDummyItem(newIngredient.getItemId()).getItem();
  385. if ((tempItem instanceof L2Armor) || (tempItem instanceof L2Weapon))
  386. newIngredient.setEnchantmentLevel(enchantLevel);
  387. }
  388. newEntry.addProduct(newIngredient);
  389. }
  390. return newEntry;
  391. }
  392. @Override
  393. public String getType()
  394. {
  395. return _C__A7_MULTISELLCHOOSE;
  396. }
  397. }