L2Party.java 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 com.l2jserver.gameserver.model;
  16. import java.util.List;
  17. import java.util.NoSuchElementException;
  18. import java.util.concurrent.Future;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.GameTimeController;
  24. import com.l2jserver.gameserver.SevenSignsFestival;
  25. import com.l2jserver.gameserver.ThreadPoolManager;
  26. import com.l2jserver.gameserver.datatables.ItemTable;
  27. import com.l2jserver.gameserver.datatables.SkillTable;
  28. import com.l2jserver.gameserver.instancemanager.DuelManager;
  29. import com.l2jserver.gameserver.model.actor.L2Attackable;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.L2Playable;
  32. import com.l2jserver.gameserver.model.actor.L2Summon;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  36. import com.l2jserver.gameserver.model.entity.DimensionalRift;
  37. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  38. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  39. import com.l2jserver.gameserver.model.skills.L2Skill;
  40. import com.l2jserver.gameserver.model.stats.Stats;
  41. import com.l2jserver.gameserver.network.SystemMessageId;
  42. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  43. import com.l2jserver.gameserver.network.serverpackets.ExAskModifyPartyLooting;
  44. import com.l2jserver.gameserver.network.serverpackets.ExCloseMPCC;
  45. import com.l2jserver.gameserver.network.serverpackets.ExOpenMPCC;
  46. import com.l2jserver.gameserver.network.serverpackets.ExPartyPetWindowAdd;
  47. import com.l2jserver.gameserver.network.serverpackets.ExPartyPetWindowDelete;
  48. import com.l2jserver.gameserver.network.serverpackets.ExSetPartyLooting;
  49. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  50. import com.l2jserver.gameserver.network.serverpackets.PartyMemberPosition;
  51. import com.l2jserver.gameserver.network.serverpackets.PartySmallWindowAdd;
  52. import com.l2jserver.gameserver.network.serverpackets.PartySmallWindowAll;
  53. import com.l2jserver.gameserver.network.serverpackets.PartySmallWindowDelete;
  54. import com.l2jserver.gameserver.network.serverpackets.PartySmallWindowDeleteAll;
  55. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  56. import com.l2jserver.gameserver.util.Util;
  57. import com.l2jserver.util.Rnd;
  58. /**
  59. * This class ...
  60. * @author nuocnam
  61. * @version $Revision: 1.6.2.2.2.6 $ $Date: 2005/04/11 19:12:16 $
  62. */
  63. public class L2Party extends AbstractPlayerGroup
  64. {
  65. private static final Logger _log = Logger.getLogger(L2Party.class.getName());
  66. private static final double[] BONUS_EXP_SP =
  67. {
  68. 1,
  69. 1.10,
  70. 1.20,
  71. 1.30,
  72. 1.40,
  73. 1.50,
  74. 2.0,
  75. 2.10,
  76. 2.20
  77. };
  78. // TODO: JIV - unhardcode to some SysString enum (sysstring-e.dat)
  79. private static final int[] LOOT_SYSSTRINGS =
  80. {
  81. 487,
  82. 488,
  83. 798,
  84. 799,
  85. 800
  86. };
  87. private static final int PARTY_POSITION_BROADCAST = 12000;
  88. public static final byte ITEM_LOOTER = 0;
  89. public static final byte ITEM_RANDOM = 1;
  90. public static final byte ITEM_RANDOM_SPOIL = 2;
  91. public static final byte ITEM_ORDER = 3;
  92. public static final byte ITEM_ORDER_SPOIL = 4;
  93. private final FastList<L2PcInstance> _members;
  94. private boolean _pendingInvitation = false;
  95. private long _pendingInviteTimeout;
  96. private int _partyLvl = 0;
  97. private int _itemDistribution = 0;
  98. private int _itemLastLoot = 0;
  99. private L2CommandChannel _commandChannel = null;
  100. private DimensionalRift _dr;
  101. private byte _requestChangeLoot = -1;
  102. private List<Integer> _changeLootAnswers = null;
  103. protected long _requestChangeLootTimer = 0;
  104. private Future<?> _checkTask = null;
  105. private Future<?> _positionBroadcastTask = null;
  106. protected PartyMemberPosition _positionPacket;
  107. private boolean _disbanding = false;
  108. /**
  109. * The message type send to the party members.
  110. */
  111. public enum messageType
  112. {
  113. Expelled,
  114. Left,
  115. None,
  116. Disconnected
  117. }
  118. /**
  119. * constructor ensures party has always one member - leader
  120. * @param leader
  121. * @param itemDistribution
  122. */
  123. public L2Party(L2PcInstance leader, int itemDistribution)
  124. {
  125. _members = new FastList<L2PcInstance>().shared();
  126. _itemDistribution = itemDistribution;
  127. getMembers().add(leader);
  128. _partyLvl = leader.getLevel();
  129. }
  130. /**
  131. * Check if another player can start invitation process
  132. * @return boolean if party waits for invitation respond
  133. */
  134. public boolean getPendingInvitation()
  135. {
  136. return _pendingInvitation;
  137. }
  138. /**
  139. * set invitation process flag and store time for expiration happens when: player join party or player decline to join
  140. * @param val
  141. */
  142. public void setPendingInvitation(boolean val)
  143. {
  144. _pendingInvitation = val;
  145. _pendingInviteTimeout = GameTimeController.getGameTicks() + (L2PcInstance.REQUEST_TIMEOUT * GameTimeController.TICKS_PER_SECOND);
  146. }
  147. /**
  148. * Check if player invitation is expired
  149. * @return boolean if time is expired
  150. * @see com.l2jserver.gameserver.model.actor.instance.L2PcInstance#isRequestExpired()
  151. */
  152. public boolean isInvitationRequestExpired()
  153. {
  154. return !(_pendingInviteTimeout > GameTimeController.getGameTicks());
  155. }
  156. /**
  157. * returns all party members
  158. * @return
  159. * @deprecated
  160. */
  161. @Deprecated
  162. public final FastList<L2PcInstance> getPartyMembers()
  163. {
  164. return (FastList<L2PcInstance>) getMembers();
  165. }
  166. /**
  167. * get random member from party
  168. * @param ItemId
  169. * @param target
  170. * @return
  171. */
  172. private L2PcInstance getCheckedRandomMember(int ItemId, L2Character target)
  173. {
  174. List<L2PcInstance> availableMembers = new FastList<>();
  175. for (L2PcInstance member : getMembers())
  176. {
  177. if (member.getInventory().validateCapacityByItemId(ItemId) && Util.checkIfInRange(Config.ALT_PARTY_RANGE2, target, member, true))
  178. {
  179. availableMembers.add(member);
  180. }
  181. }
  182. if (!availableMembers.isEmpty())
  183. {
  184. return availableMembers.get(Rnd.get(availableMembers.size()));
  185. }
  186. return null;
  187. }
  188. /**
  189. * get next item looter
  190. * @param ItemId
  191. * @param target
  192. * @return
  193. */
  194. private L2PcInstance getCheckedNextLooter(int ItemId, L2Character target)
  195. {
  196. for (int i = 0; i < getMemberCount(); i++)
  197. {
  198. if (++_itemLastLoot >= getMemberCount())
  199. {
  200. _itemLastLoot = 0;
  201. }
  202. L2PcInstance member;
  203. try
  204. {
  205. member = getMembers().get(_itemLastLoot);
  206. if (member.getInventory().validateCapacityByItemId(ItemId) && Util.checkIfInRange(Config.ALT_PARTY_RANGE2, target, member, true))
  207. {
  208. return member;
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. // continue, take another member if this just logged off
  214. }
  215. }
  216. return null;
  217. }
  218. /**
  219. * get next item looter
  220. * @param player
  221. * @param ItemId
  222. * @param spoil
  223. * @param target
  224. * @return
  225. */
  226. private L2PcInstance getActualLooter(L2PcInstance player, int ItemId, boolean spoil, L2Character target)
  227. {
  228. L2PcInstance looter = player;
  229. switch (_itemDistribution)
  230. {
  231. case ITEM_RANDOM:
  232. if (!spoil)
  233. {
  234. looter = getCheckedRandomMember(ItemId, target);
  235. }
  236. break;
  237. case ITEM_RANDOM_SPOIL:
  238. looter = getCheckedRandomMember(ItemId, target);
  239. break;
  240. case ITEM_ORDER:
  241. if (!spoil)
  242. {
  243. looter = getCheckedNextLooter(ItemId, target);
  244. }
  245. break;
  246. case ITEM_ORDER_SPOIL:
  247. looter = getCheckedNextLooter(ItemId, target);
  248. break;
  249. }
  250. if (looter == null)
  251. {
  252. looter = player;
  253. }
  254. return looter;
  255. }
  256. /**
  257. * @param player the player to check.
  258. * @return {code true} if player is party leader.
  259. */
  260. public boolean isLeader(L2PcInstance player)
  261. {
  262. return getLeader().getObjectId() == player.getObjectId();
  263. }
  264. /**
  265. * @return the Object ID for the party leader to be used as a unique identifier of this party-
  266. * @deprecated use {@link #getLeaderObjectId()}
  267. */
  268. @Deprecated
  269. public int getPartyLeaderOID()
  270. {
  271. return getLeaderObjectId();
  272. }
  273. /**
  274. * Broadcasts packet to all party member.
  275. * @param packet the packet to be broadcasted.
  276. */
  277. @Deprecated
  278. public void broadcastToPartyMembers(L2GameServerPacket packet)
  279. {
  280. broadcastPacket(packet);
  281. }
  282. /**
  283. * Broadcasts UI update and User Info for new party leader.
  284. */
  285. public void broadcastToPartyMembersNewLeader()
  286. {
  287. for (L2PcInstance member : getMembers())
  288. {
  289. if (member != null)
  290. {
  291. member.sendPacket(PartySmallWindowDeleteAll.STATIC_PACKET);
  292. member.sendPacket(new PartySmallWindowAll(member, this));
  293. member.broadcastUserInfo();
  294. }
  295. }
  296. }
  297. @Deprecated
  298. public void broadcastCSToPartyMembers(CreatureSay msg, L2PcInstance broadcaster)
  299. {
  300. broadcastCreatureSay(msg, broadcaster);
  301. }
  302. /**
  303. * Send a Server->Client packet to all other L2PcInstance of the Party.<BR>
  304. * <BR>
  305. * @param player
  306. * @param msg
  307. */
  308. public void broadcastToPartyMembers(L2PcInstance player, L2GameServerPacket msg)
  309. {
  310. for (L2PcInstance member : getMembers())
  311. {
  312. if ((member != null) && (member.getObjectId() != player.getObjectId()))
  313. {
  314. member.sendPacket(msg);
  315. }
  316. }
  317. }
  318. /**
  319. * adds new member to party
  320. * @param player
  321. */
  322. public void addPartyMember(L2PcInstance player)
  323. {
  324. if (getMembers().contains(player))
  325. {
  326. return;
  327. }
  328. if (_requestChangeLoot != -1)
  329. {
  330. finishLootRequest(false); // cancel on invite
  331. }
  332. // sends new member party window for all members
  333. // we do all actions before adding member to a list, this speeds things up a little
  334. player.sendPacket(new PartySmallWindowAll(player, this));
  335. // sends pets/summons of party members
  336. for (L2PcInstance pMember : getMembers())
  337. {
  338. if ((pMember != null) && pMember.hasSummon())
  339. {
  340. player.sendPacket(new ExPartyPetWindowAdd(pMember.getSummon()));
  341. }
  342. }
  343. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.YOU_JOINED_S1_PARTY);
  344. msg.addString(getLeader().getName());
  345. player.sendPacket(msg);
  346. msg = SystemMessage.getSystemMessage(SystemMessageId.C1_JOINED_PARTY);
  347. msg.addString(player.getName());
  348. broadcastPacket(msg);
  349. broadcastPacket(new PartySmallWindowAdd(player, this));
  350. // send the position of all party members to the new party member
  351. // player.sendPacket(new PartyMemberPosition(this));
  352. // send the position of the new party member to all party members (except the new one - he knows his own position)
  353. // broadcastToPartyMembers(player, new PartyMemberPosition(this));
  354. // if member has pet/summon add it to other as well
  355. if (player.hasSummon())
  356. {
  357. broadcastPacket(new ExPartyPetWindowAdd(player.getSummon()));
  358. }
  359. // add player to party, adjust party level
  360. getMembers().add(player);
  361. if (player.getLevel() > _partyLvl)
  362. {
  363. _partyLvl = player.getLevel();
  364. }
  365. // update partySpelled
  366. L2Summon summon;
  367. for (L2PcInstance member : getMembers())
  368. {
  369. if (member != null)
  370. {
  371. member.updateEffectIcons(true); // update party icons only
  372. summon = member.getSummon();
  373. member.broadcastUserInfo();
  374. if (summon != null)
  375. {
  376. summon.updateEffectIcons();
  377. }
  378. }
  379. }
  380. if (isInDimensionalRift())
  381. {
  382. _dr.partyMemberInvited();
  383. }
  384. // open the CCInformationwindow
  385. if (isInCommandChannel())
  386. {
  387. player.sendPacket(ExOpenMPCC.STATIC_PACKET);
  388. }
  389. if (_positionBroadcastTask == null)
  390. {
  391. _positionBroadcastTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new PositionBroadcast(), PARTY_POSITION_BROADCAST / 2, PARTY_POSITION_BROADCAST);
  392. }
  393. }
  394. /**
  395. * Removes a party member using its name.
  396. * @param name player the player to be removed from the party.
  397. * @param type the message type {@link messageType}.
  398. */
  399. public void removePartyMember(String name, messageType type)
  400. {
  401. removePartyMember(getPlayerByName(name), type);
  402. }
  403. /**
  404. * Removes a party member instance.
  405. * @param player the player to be removed from the party.
  406. * @param type the message type {@link messageType}.
  407. */
  408. public void removePartyMember(L2PcInstance player, messageType type)
  409. {
  410. if (getMembers().contains(player))
  411. {
  412. final boolean isLeader = isLeader(player);
  413. if (!_disbanding)
  414. {
  415. if ((getMembers().size() == 2) || (isLeader && !Config.ALT_LEAVE_PARTY_LEADER && (type != messageType.Disconnected)))
  416. {
  417. disbandParty();
  418. return;
  419. }
  420. }
  421. getMembers().remove(player);
  422. recalculatePartyLevel();
  423. if (player.isFestivalParticipant())
  424. {
  425. SevenSignsFestival.getInstance().updateParticipants(player, this);
  426. }
  427. if (player.isInDuel())
  428. {
  429. DuelManager.getInstance().onRemoveFromParty(player);
  430. }
  431. try
  432. {
  433. if (player.getFusionSkill() != null)
  434. {
  435. player.abortCast();
  436. }
  437. for (L2Character character : player.getKnownList().getKnownCharacters())
  438. {
  439. if ((character.getFusionSkill() != null) && (character.getFusionSkill().getTarget() == player))
  440. {
  441. character.abortCast();
  442. }
  443. }
  444. }
  445. catch (Exception e)
  446. {
  447. _log.log(Level.WARNING, "", e);
  448. }
  449. SystemMessage msg;
  450. if (type == messageType.Expelled)
  451. {
  452. player.sendPacket(SystemMessageId.HAVE_BEEN_EXPELLED_FROM_PARTY);
  453. msg = SystemMessage.getSystemMessage(SystemMessageId.C1_WAS_EXPELLED_FROM_PARTY);
  454. msg.addString(player.getName());
  455. broadcastPacket(msg);
  456. }
  457. else if ((type == messageType.Left) || (type == messageType.Disconnected))
  458. {
  459. player.sendPacket(SystemMessageId.YOU_LEFT_PARTY);
  460. msg = SystemMessage.getSystemMessage(SystemMessageId.C1_LEFT_PARTY);
  461. msg.addString(player.getName());
  462. broadcastPacket(msg);
  463. }
  464. // UI update.
  465. player.sendPacket(PartySmallWindowDeleteAll.STATIC_PACKET);
  466. player.setParty(null);
  467. broadcastPacket(new PartySmallWindowDelete(player));
  468. if (player.hasSummon())
  469. {
  470. broadcastPacket(new ExPartyPetWindowDelete(player.getSummon()));
  471. }
  472. if (isInDimensionalRift())
  473. {
  474. _dr.partyMemberExited(player);
  475. }
  476. // Close the CCInfoWindow
  477. if (isInCommandChannel())
  478. {
  479. player.sendPacket(new ExCloseMPCC());
  480. }
  481. if (isLeader && (getMembers().size() > 1) && (Config.ALT_LEAVE_PARTY_LEADER || (type == messageType.Disconnected)))
  482. {
  483. msg = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_BECOME_A_PARTY_LEADER);
  484. msg.addString(getLeader().getName());
  485. broadcastPacket(msg);
  486. broadcastToPartyMembersNewLeader();
  487. }
  488. else if (getMembers().size() == 1)
  489. {
  490. if (isInCommandChannel())
  491. {
  492. // delete the whole command channel when the party who opened the channel is disbanded
  493. if (getCommandChannel().getLeader().getObjectId() == getLeader().getObjectId())
  494. {
  495. getCommandChannel().disbandChannel();
  496. }
  497. else
  498. {
  499. getCommandChannel().removeParty(this);
  500. }
  501. }
  502. if (getLeader() != null)
  503. {
  504. getLeader().setParty(null);
  505. if (getLeader().isInDuel())
  506. {
  507. DuelManager.getInstance().onRemoveFromParty(getLeader());
  508. }
  509. }
  510. if (_checkTask != null)
  511. {
  512. _checkTask.cancel(true);
  513. _checkTask = null;
  514. }
  515. if (_positionBroadcastTask != null)
  516. {
  517. _positionBroadcastTask.cancel(false);
  518. _positionBroadcastTask = null;
  519. }
  520. _members.clear();
  521. }
  522. }
  523. }
  524. /**
  525. * Disperse a party and sends a message to all its members.
  526. */
  527. public void disbandParty()
  528. {
  529. _disbanding = true;
  530. if (_members != null)
  531. {
  532. broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.PARTY_DISPERSED));
  533. for (L2PcInstance member : _members)
  534. {
  535. if (member != null)
  536. {
  537. removePartyMember(member, messageType.None);
  538. }
  539. }
  540. }
  541. }
  542. /**
  543. * Change party leader (used for string arguments)
  544. * @param name
  545. */
  546. public void changePartyLeader(String name)
  547. {
  548. L2PcInstance player = getPlayerByName(name);
  549. if ((player != null) && !player.isInDuel())
  550. {
  551. if (getMembers().contains(player))
  552. {
  553. if (isLeader(player))
  554. {
  555. player.sendPacket(SystemMessageId.YOU_CANNOT_TRANSFER_RIGHTS_TO_YOURSELF);
  556. }
  557. else
  558. {
  559. // Swap party members
  560. L2PcInstance temp;
  561. int p1 = getMembers().indexOf(player);
  562. temp = getLeader();
  563. getMembers().set(0, getMembers().get(p1));
  564. getMembers().set(p1, temp);
  565. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_BECOME_A_PARTY_LEADER);
  566. msg.addString(getLeader().getName());
  567. broadcastPacket(msg);
  568. broadcastToPartyMembersNewLeader();
  569. if (isInCommandChannel() && (temp.getObjectId() == _commandChannel.getLeader().getObjectId()))
  570. {
  571. _commandChannel.setChannelLeader(getLeader());
  572. msg = SystemMessage.getSystemMessage(SystemMessageId.COMMAND_CHANNEL_LEADER_NOW_C1);
  573. msg.addString(_commandChannel.getLeader().getName());
  574. _commandChannel.broadcastPacket(msg);
  575. }
  576. if (player.isInPartyMatchRoom())
  577. {
  578. PartyMatchRoom room = PartyMatchRoomList.getInstance().getPlayerRoom(player);
  579. room.changeLeader(player);
  580. }
  581. }
  582. }
  583. else
  584. {
  585. player.sendPacket(SystemMessageId.YOU_CAN_TRANSFER_RIGHTS_ONLY_TO_ANOTHER_PARTY_MEMBER);
  586. }
  587. }
  588. }
  589. /**
  590. * finds a player in the party by name
  591. * @param name
  592. * @return
  593. */
  594. private L2PcInstance getPlayerByName(String name)
  595. {
  596. for (L2PcInstance member : getMembers())
  597. {
  598. if (member.getName().equalsIgnoreCase(name))
  599. {
  600. return member;
  601. }
  602. }
  603. return null;
  604. }
  605. /**
  606. * distribute item(s) to party members
  607. * @param player
  608. * @param item
  609. */
  610. public void distributeItem(L2PcInstance player, L2ItemInstance item)
  611. {
  612. if (item.getItemId() == PcInventory.ADENA_ID)
  613. {
  614. distributeAdena(player, item.getCount(), player);
  615. ItemTable.getInstance().destroyItem("Party", item, player, null);
  616. return;
  617. }
  618. L2PcInstance target = getActualLooter(player, item.getItemId(), false, player);
  619. target.addItem("Party", item, player, true);
  620. // Send messages to other party members about reward
  621. if (item.getCount() > 1)
  622. {
  623. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.C1_OBTAINED_S3_S2);
  624. msg.addString(target.getName());
  625. msg.addItemName(item);
  626. msg.addItemNumber(item.getCount());
  627. broadcastToPartyMembers(target, msg);
  628. }
  629. else
  630. {
  631. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.C1_OBTAINED_S2);
  632. msg.addString(target.getName());
  633. msg.addItemName(item);
  634. broadcastToPartyMembers(target, msg);
  635. }
  636. }
  637. /**
  638. * distribute item(s) to party members
  639. * @param player
  640. * @param item
  641. * @param spoil
  642. * @param target
  643. */
  644. public void distributeItem(L2PcInstance player, L2Attackable.RewardItem item, boolean spoil, L2Attackable target)
  645. {
  646. if (item == null)
  647. {
  648. return;
  649. }
  650. if (item.getItemId() == PcInventory.ADENA_ID)
  651. {
  652. distributeAdena(player, item.getCount(), target);
  653. return;
  654. }
  655. L2PcInstance looter = getActualLooter(player, item.getItemId(), spoil, target);
  656. looter.addItem(spoil ? "Sweep" : "Party", item.getItemId(), item.getCount(), player, true);
  657. // Send messages to other aprty members about reward
  658. if (item.getCount() > 1)
  659. {
  660. SystemMessage msg = spoil ? SystemMessage.getSystemMessage(SystemMessageId.C1_SWEEPED_UP_S3_S2) : SystemMessage.getSystemMessage(SystemMessageId.C1_OBTAINED_S3_S2);
  661. msg.addString(looter.getName());
  662. msg.addItemName(item.getItemId());
  663. msg.addItemNumber(item.getCount());
  664. broadcastToPartyMembers(looter, msg);
  665. }
  666. else
  667. {
  668. SystemMessage msg = spoil ? SystemMessage.getSystemMessage(SystemMessageId.C1_SWEEPED_UP_S2) : SystemMessage.getSystemMessage(SystemMessageId.C1_OBTAINED_S2);
  669. msg.addString(looter.getName());
  670. msg.addItemName(item.getItemId());
  671. broadcastToPartyMembers(looter, msg);
  672. }
  673. }
  674. /**
  675. * distribute adena to party members
  676. * @param player
  677. * @param adena
  678. * @param target
  679. */
  680. public void distributeAdena(L2PcInstance player, long adena, L2Character target)
  681. {
  682. // Get all the party members
  683. List<L2PcInstance> membersList = getMembers();
  684. // Check the number of party members that must be rewarded
  685. // (The party member must be in range to receive its reward)
  686. List<L2PcInstance> ToReward = FastList.newInstance();
  687. for (L2PcInstance member : membersList)
  688. {
  689. if (!Util.checkIfInRange(Config.ALT_PARTY_RANGE2, target, member, true))
  690. {
  691. continue;
  692. }
  693. ToReward.add(member);
  694. }
  695. // Avoid null exceptions, if any
  696. if (ToReward.isEmpty())
  697. {
  698. return;
  699. }
  700. // Now we can actually distribute the adena reward
  701. // (Total adena splitted by the number of party members that are in range and must be rewarded)
  702. long count = adena / ToReward.size();
  703. for (L2PcInstance member : ToReward)
  704. {
  705. member.addAdena("Party", count, player, true);
  706. }
  707. FastList.recycle((FastList<?>) ToReward);
  708. }
  709. /**
  710. * Distribute Experience and SP rewards to L2PcInstance Party members in the known area of the last attacker.<BR>
  711. * <BR>
  712. * <B><U> Actions</U> :</B><BR>
  713. * <BR>
  714. * <li>Get the L2PcInstance owner of the L2ServitorInstance (if necessary)</li> <li>Calculate the Experience and SP reward distribution rate</li> <li>Add Experience and SP to the L2PcInstance</li><BR>
  715. * <BR>
  716. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T GIVE rewards to L2PetInstance</B></FONT><BR>
  717. * <BR>
  718. * Exception are L2PetInstances that leech from the owner's XP; they get the exp indirectly, via the owner's exp gain<BR>
  719. * @param xpReward The Experience reward to distribute
  720. * @param spReward The SP reward to distribute
  721. * @param rewardedMembers The list of L2PcInstance to reward
  722. * @param topLvl
  723. * @param partyDmg
  724. * @param target
  725. */
  726. public void distributeXpAndSp(long xpReward, int spReward, List<L2Playable> rewardedMembers, int topLvl, int partyDmg, L2Attackable target)
  727. {
  728. List<L2Playable> validMembers = getValidMembers(rewardedMembers, topLvl);
  729. float penalty;
  730. double sqLevel;
  731. double preCalculation;
  732. xpReward *= getExpBonus(validMembers.size());
  733. spReward *= getSpBonus(validMembers.size());
  734. double sqLevelSum = 0;
  735. for (L2Playable character : validMembers)
  736. {
  737. sqLevelSum += (character.getLevel() * character.getLevel());
  738. }
  739. final float vitalityPoints = (target.getVitalityPoints(partyDmg) * Config.RATE_PARTY_XP) / validMembers.size();
  740. final boolean useVitalityRate = target.useVitalityRate();
  741. // Go through the L2PcInstances and L2PetInstances (not L2ServitorInstances) that must be rewarded
  742. synchronized (rewardedMembers)
  743. {
  744. for (L2Character member : rewardedMembers)
  745. {
  746. if (member.isDead())
  747. {
  748. continue;
  749. }
  750. penalty = 0;
  751. // The servitor penalty
  752. if ((member.getSummon() != null) && member.getSummon().isServitor())
  753. {
  754. penalty = ((L2ServitorInstance) member.getSummon()).getExpPenalty();
  755. }
  756. // Pets that leech xp from the owner (like babypets) do not get rewarded directly
  757. if (member instanceof L2PetInstance)
  758. {
  759. if (((L2PetInstance) member).getPetLevelData().getOwnerExpTaken() > 0)
  760. {
  761. continue;
  762. }
  763. // TODO: This is a temporary fix while correct pet xp in party is figured out
  764. penalty = (float) 0.85;
  765. }
  766. // Calculate and add the EXP and SP reward to the member
  767. if (validMembers.contains(member))
  768. {
  769. sqLevel = member.getLevel() * member.getLevel();
  770. preCalculation = (sqLevel / sqLevelSum) * (1 - penalty);
  771. // Add the XP/SP points to the requested party member
  772. if (!member.isDead())
  773. {
  774. long addexp = Math.round(member.calcStat(Stats.EXPSP_RATE, xpReward * preCalculation, null, null));
  775. int addsp = (int) member.calcStat(Stats.EXPSP_RATE, spReward * preCalculation, null, null);
  776. if (member instanceof L2PcInstance)
  777. {
  778. if (((L2PcInstance) member).getSkillLevel(467) > 0)
  779. {
  780. L2Skill skill = SkillTable.getInstance().getInfo(467, ((L2PcInstance) member).getSkillLevel(467));
  781. if (skill.getExpNeeded() <= addexp)
  782. {
  783. ((L2PcInstance) member).absorbSoul(skill, target);
  784. }
  785. }
  786. ((L2PcInstance) member).addExpAndSp(addexp, addsp, useVitalityRate);
  787. if (addexp > 0)
  788. {
  789. ((L2PcInstance) member).updateVitalityPoints(vitalityPoints, true, false);
  790. }
  791. }
  792. else
  793. {
  794. member.addExpAndSp(addexp, addsp);
  795. }
  796. }
  797. }
  798. else
  799. {
  800. member.addExpAndSp(0, 0);
  801. }
  802. }
  803. }
  804. }
  805. /**
  806. * refresh party level
  807. */
  808. public void recalculatePartyLevel()
  809. {
  810. int newLevel = 0;
  811. for (L2PcInstance member : getMembers())
  812. {
  813. if (member == null)
  814. {
  815. getMembers().remove(member);
  816. continue;
  817. }
  818. if (member.getLevel() > newLevel)
  819. {
  820. newLevel = member.getLevel();
  821. }
  822. }
  823. _partyLvl = newLevel;
  824. }
  825. private List<L2Playable> getValidMembers(List<L2Playable> members, int topLvl)
  826. {
  827. List<L2Playable> validMembers = new FastList<>();
  828. // Fixed LevelDiff cutoff point
  829. if (Config.PARTY_XP_CUTOFF_METHOD.equalsIgnoreCase("level"))
  830. {
  831. for (L2Playable member : members)
  832. {
  833. if ((topLvl - member.getLevel()) <= Config.PARTY_XP_CUTOFF_LEVEL)
  834. {
  835. validMembers.add(member);
  836. }
  837. }
  838. }
  839. // Fixed MinPercentage cutoff point
  840. else if (Config.PARTY_XP_CUTOFF_METHOD.equalsIgnoreCase("percentage"))
  841. {
  842. int sqLevelSum = 0;
  843. for (L2Playable member : members)
  844. {
  845. sqLevelSum += (member.getLevel() * member.getLevel());
  846. }
  847. for (L2Playable member : members)
  848. {
  849. int sqLevel = member.getLevel() * member.getLevel();
  850. if ((sqLevel * 100) >= (sqLevelSum * Config.PARTY_XP_CUTOFF_PERCENT))
  851. {
  852. validMembers.add(member);
  853. }
  854. }
  855. }
  856. // Automatic cutoff method
  857. else if (Config.PARTY_XP_CUTOFF_METHOD.equalsIgnoreCase("auto"))
  858. {
  859. int sqLevelSum = 0;
  860. for (L2Playable member : members)
  861. {
  862. sqLevelSum += (member.getLevel() * member.getLevel());
  863. }
  864. int i = members.size() - 1;
  865. if (i < 1)
  866. {
  867. return members;
  868. }
  869. if (i >= BONUS_EXP_SP.length)
  870. {
  871. i = BONUS_EXP_SP.length - 1;
  872. }
  873. for (L2Playable member : members)
  874. {
  875. int sqLevel = member.getLevel() * member.getLevel();
  876. if (sqLevel >= (sqLevelSum / (members.size() * members.size())))
  877. {
  878. validMembers.add(member);
  879. }
  880. }
  881. }
  882. else if (Config.PARTY_XP_CUTOFF_METHOD.equalsIgnoreCase("none"))
  883. {
  884. validMembers.addAll(members);
  885. }
  886. return validMembers;
  887. }
  888. private double getBaseExpSpBonus(int membersCount)
  889. {
  890. int i = membersCount - 1;
  891. if (i < 1)
  892. {
  893. return 1;
  894. }
  895. if (i >= BONUS_EXP_SP.length)
  896. {
  897. i = BONUS_EXP_SP.length - 1;
  898. }
  899. return BONUS_EXP_SP[i];
  900. }
  901. private double getExpBonus(int membersCount)
  902. {
  903. if (membersCount < 2)
  904. {
  905. // not is a valid party
  906. return getBaseExpSpBonus(membersCount);
  907. }
  908. return getBaseExpSpBonus(membersCount) * Config.RATE_PARTY_XP;
  909. }
  910. private double getSpBonus(int membersCount)
  911. {
  912. if (membersCount < 2)
  913. {
  914. // not is a valid party
  915. return getBaseExpSpBonus(membersCount);
  916. }
  917. return getBaseExpSpBonus(membersCount) * Config.RATE_PARTY_SP;
  918. }
  919. @Override
  920. public int getLevel()
  921. {
  922. return _partyLvl;
  923. }
  924. public int getLootDistribution()
  925. {
  926. return _itemDistribution;
  927. }
  928. public boolean isInCommandChannel()
  929. {
  930. return _commandChannel != null;
  931. }
  932. public L2CommandChannel getCommandChannel()
  933. {
  934. return _commandChannel;
  935. }
  936. public void setCommandChannel(L2CommandChannel channel)
  937. {
  938. _commandChannel = channel;
  939. }
  940. public boolean isInDimensionalRift()
  941. {
  942. return _dr != null;
  943. }
  944. public void setDimensionalRift(DimensionalRift dr)
  945. {
  946. _dr = dr;
  947. }
  948. public DimensionalRift getDimensionalRift()
  949. {
  950. return _dr;
  951. }
  952. @Override
  953. public L2PcInstance getLeader()
  954. {
  955. try
  956. {
  957. return _members.getFirst();
  958. }
  959. catch (NoSuchElementException e)
  960. {
  961. return null;
  962. }
  963. }
  964. public void requestLootChange(byte type)
  965. {
  966. if (_requestChangeLoot != -1)
  967. {
  968. if (System.currentTimeMillis() > _requestChangeLootTimer)
  969. {
  970. finishLootRequest(false); // timeout 45sec, guess
  971. }
  972. else
  973. {
  974. return;
  975. }
  976. }
  977. _requestChangeLoot = type;
  978. int additionalTime = L2PcInstance.REQUEST_TIMEOUT * 3000;
  979. _requestChangeLootTimer = System.currentTimeMillis() + additionalTime;
  980. _changeLootAnswers = FastList.newInstance();
  981. _checkTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new ChangeLootCheck(), additionalTime + 1000, 5000);
  982. broadcastToPartyMembers(getLeader(), new ExAskModifyPartyLooting(getLeader().getName(), type));
  983. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.REQUESTING_APPROVAL_CHANGE_PARTY_LOOT_S1);
  984. sm.addSystemString(LOOT_SYSSTRINGS[type]);
  985. getLeader().sendPacket(sm);
  986. }
  987. public synchronized void answerLootChangeRequest(L2PcInstance member, boolean answer)
  988. {
  989. if (_requestChangeLoot == -1)
  990. {
  991. return;
  992. }
  993. if (_changeLootAnswers.contains(member.getObjectId()))
  994. {
  995. return;
  996. }
  997. if (!answer)
  998. {
  999. finishLootRequest(false);
  1000. return;
  1001. }
  1002. _changeLootAnswers.add(member.getObjectId());
  1003. if (_changeLootAnswers.size() >= (getMemberCount() - 1))
  1004. {
  1005. finishLootRequest(true);
  1006. }
  1007. }
  1008. protected synchronized void finishLootRequest(boolean success)
  1009. {
  1010. if (_requestChangeLoot == -1)
  1011. {
  1012. return;
  1013. }
  1014. if (_checkTask != null)
  1015. {
  1016. _checkTask.cancel(false);
  1017. _checkTask = null;
  1018. }
  1019. if (success)
  1020. {
  1021. broadcastPacket(new ExSetPartyLooting(1, _requestChangeLoot));
  1022. _itemDistribution = _requestChangeLoot;
  1023. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PARTY_LOOT_CHANGED_S1);
  1024. sm.addSystemString(LOOT_SYSSTRINGS[_requestChangeLoot]);
  1025. broadcastPacket(sm);
  1026. }
  1027. else
  1028. {
  1029. broadcastPacket(new ExSetPartyLooting(0, (byte) 0));
  1030. broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.PARTY_LOOT_CHANGE_CANCELLED));
  1031. }
  1032. _requestChangeLoot = -1;
  1033. FastList.recycle((FastList<?>) _changeLootAnswers);
  1034. _requestChangeLootTimer = 0;
  1035. }
  1036. protected class ChangeLootCheck implements Runnable
  1037. {
  1038. @Override
  1039. public void run()
  1040. {
  1041. if (System.currentTimeMillis() > _requestChangeLootTimer)
  1042. {
  1043. finishLootRequest(false);
  1044. }
  1045. }
  1046. }
  1047. protected class PositionBroadcast implements Runnable
  1048. {
  1049. @Override
  1050. public void run()
  1051. {
  1052. if (_positionPacket == null)
  1053. {
  1054. _positionPacket = new PartyMemberPosition(L2Party.this);
  1055. }
  1056. else
  1057. {
  1058. _positionPacket.reuse(L2Party.this);
  1059. }
  1060. broadcastPacket(_positionPacket);
  1061. }
  1062. }
  1063. /**
  1064. * @return reurns all party members
  1065. */
  1066. @Override
  1067. public List<L2PcInstance> getMembers()
  1068. {
  1069. return _members;
  1070. }
  1071. }