OlympiadGame.java 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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.model.olympiad;
  16. import java.util.Map;
  17. import java.util.logging.Logger;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.datatables.HeroSkillTable;
  20. import net.sf.l2j.gameserver.datatables.NpcTable;
  21. import net.sf.l2j.gameserver.datatables.SpawnTable;
  22. import net.sf.l2j.gameserver.model.L2ItemInstance;
  23. import net.sf.l2j.gameserver.model.L2Party;
  24. import net.sf.l2j.gameserver.model.L2Skill;
  25. import net.sf.l2j.gameserver.model.L2Spawn;
  26. import net.sf.l2j.gameserver.model.L2Summon;
  27. import net.sf.l2j.gameserver.model.L2World;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2CubicInstance;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  31. import net.sf.l2j.gameserver.model.itemcontainer.Inventory;
  32. import net.sf.l2j.gameserver.model.olympiad.Olympiad.COMP_TYPE;
  33. import net.sf.l2j.gameserver.network.SystemMessageId;
  34. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  35. import net.sf.l2j.gameserver.network.serverpackets.ExAutoSoulShot;
  36. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadMatchEnd;
  37. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadMode;
  38. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadSpelledInfo;
  39. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadUserInfo;
  40. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  41. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  42. import net.sf.l2j.gameserver.templates.StatsSet;
  43. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  44. import net.sf.l2j.util.L2FastList;
  45. /**
  46. *
  47. * @author GodKratos
  48. */
  49. class OlympiadGame
  50. {
  51. protected static final Logger _log = Logger.getLogger(OlympiadGame.class.getName());
  52. protected COMP_TYPE _type;
  53. protected boolean _aborted;
  54. protected boolean _gamestarted;
  55. protected boolean _playerOneDisconnected;
  56. protected boolean _playerTwoDisconnected;
  57. protected boolean _playerOneDefaulted;
  58. protected boolean _playerTwoDefaulted;
  59. protected String _playerOneName;
  60. protected String _playerTwoName;
  61. protected int _playerOneID = 0;
  62. protected int _playerTwoID = 0;
  63. protected static final int OLY_BUFFER = 36402;
  64. protected static final int OLY_MANAGER = 31688;
  65. private static final String POINTS = "olympiad_points";
  66. private static final String COMP_DONE = "competitions_done";
  67. private static final String COMP_WON = "competitions_won";
  68. private static final String COMP_LOST = "competitions_lost";
  69. private static final String COMP_DRAWN = "competitions_drawn";
  70. protected static boolean _battleStarted;
  71. public int _damageP1 = 0;
  72. public int _damageP2 = 0;
  73. public L2PcInstance _playerOne;
  74. public L2PcInstance _playerTwo;
  75. public L2Spawn _spawnOne;
  76. public L2Spawn _spawnTwo;
  77. protected L2FastList<L2PcInstance> _players;
  78. private int[] _stadiumPort;
  79. private int x1, y1, z1, x2, y2, z2;
  80. public int _stadiumID;
  81. private SystemMessage _sm;
  82. private SystemMessage _sm2;
  83. private SystemMessage _sm3;
  84. protected OlympiadGame(int id, COMP_TYPE type, L2FastList<L2PcInstance> list)
  85. {
  86. _aborted = false;
  87. _gamestarted = false;
  88. _stadiumID = id;
  89. _playerOneDisconnected = false;
  90. _playerTwoDisconnected = false;
  91. _type = type;
  92. _stadiumPort = OlympiadManager.STADIUMS[id].getCoordinates();
  93. if (list != null)
  94. {
  95. _players = list;
  96. _playerOne = list.get(0);
  97. _playerTwo = list.get(1);
  98. try
  99. {
  100. _playerOneName = _playerOne.getName();
  101. _playerTwoName = _playerTwo.getName();
  102. _playerOne.setOlympiadGameId(id);
  103. _playerTwo.setOlympiadGameId(id);
  104. _playerOneID = _playerOne.getObjectId();
  105. _playerTwoID = _playerTwo.getObjectId();
  106. }
  107. catch (Exception e)
  108. {
  109. _aborted = true;
  110. clearPlayers();
  111. }
  112. if (Config.DEBUG)
  113. _log.info("Olympiad System: Game - " + id + ": "
  114. + _playerOne.getName() + " Vs " + _playerTwo.getName());
  115. }
  116. else
  117. {
  118. _aborted = true;
  119. clearPlayers();
  120. return;
  121. }
  122. }
  123. public boolean isAborted()
  124. {
  125. return _aborted;
  126. }
  127. protected void clearPlayers()
  128. {
  129. _playerOne = null;
  130. _playerTwo = null;
  131. _players = null;
  132. _playerOneName = "";
  133. _playerTwoName = "";
  134. _playerOneID = 0;
  135. _playerTwoID = 0;
  136. }
  137. protected void handleDisconnect(L2PcInstance player)
  138. {
  139. if (_gamestarted)
  140. {
  141. if (player == _playerOne)
  142. _playerOneDisconnected = true;
  143. else if (player == _playerTwo)
  144. _playerTwoDisconnected = true;
  145. }
  146. }
  147. public L2Spawn SpawnBuffer(int xPos, int yPos, int zPos, int npcId)
  148. {
  149. L2NpcTemplate template;
  150. template = NpcTable.getInstance().getTemplate(npcId);
  151. try
  152. {
  153. L2Spawn spawn = new L2Spawn(template);
  154. spawn.setLocx(xPos);
  155. spawn.setLocy(yPos);
  156. spawn.setLocz(zPos);
  157. spawn.setAmount(1);
  158. spawn.setHeading(0);
  159. spawn.setRespawnDelay(1);
  160. SpawnTable.getInstance().addNewSpawn(spawn, false);
  161. spawn.init();
  162. return spawn;
  163. }
  164. catch (Exception e)
  165. {
  166. return null;
  167. }
  168. }
  169. protected void removals()
  170. {
  171. if (_aborted)
  172. return;
  173. if (_playerOne == null || _playerTwo == null)
  174. return;
  175. if (_playerOneDisconnected || _playerTwoDisconnected)
  176. return;
  177. for (L2PcInstance player : _players)
  178. {
  179. try
  180. {
  181. // Remove Clan Skills
  182. if (player.getClan() != null)
  183. {
  184. for (L2Skill skill : player.getClan().getAllSkills())
  185. player.removeSkill(skill, false);
  186. }
  187. // Abort casting if player casting
  188. if (player.isCastingNow())
  189. {
  190. player.abortCast();
  191. }
  192. // Force the character to be visible
  193. player.getAppearance().setVisible();
  194. // Remove Hero Skills
  195. if (player.isHero())
  196. {
  197. for (L2Skill skill : HeroSkillTable.getHeroSkills())
  198. player.removeSkill(skill, false);
  199. }
  200. // Heal Player fully
  201. player.setCurrentCp(player.getMaxCp());
  202. player.setCurrentHp(player.getMaxHp());
  203. player.setCurrentMp(player.getMaxMp());
  204. // Remove Buffs
  205. player.stopAllEffects();
  206. // Remove Summon's Buffs
  207. if (player.getPet() != null)
  208. {
  209. L2Summon summon = player.getPet();
  210. summon.stopAllEffects();
  211. if (summon instanceof L2PetInstance)
  212. summon.unSummon(player);
  213. }
  214. if (player.getCubics() != null)
  215. {
  216. boolean removed = false;
  217. for (L2CubicInstance cubic : player.getCubics().values())
  218. {
  219. if (cubic.givenByOther())
  220. {
  221. cubic.stopAction();
  222. player.delCubic(cubic.getId());
  223. removed = true;
  224. }
  225. }
  226. if (removed)
  227. player.broadcastUserInfo();
  228. }
  229. // Remove player from his party
  230. if (player.getParty() != null)
  231. {
  232. L2Party party = player.getParty();
  233. party.removePartyMember(player);
  234. }
  235. // Remove over enchanted items and hero weapons
  236. for (int i = 0; i < Inventory.PAPERDOLL_TOTALSLOTS; i++)
  237. {
  238. L2ItemInstance equippedItem = player.getInventory().getPaperdollItem(i);
  239. if (equippedItem != null && (equippedItem.isHeroItem() || equippedItem.isOlyRestrictedItem()))
  240. {
  241. player.getInventory().unEquipItemInSlotAndRecord(i);
  242. if (equippedItem.isWear())
  243. continue;
  244. SystemMessage sm = null;
  245. if (equippedItem.getEnchantLevel() > 0)
  246. {
  247. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  248. sm.addNumber(equippedItem.getEnchantLevel());
  249. sm.addItemName(equippedItem);
  250. }
  251. else
  252. {
  253. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  254. sm.addItemName(equippedItem);
  255. }
  256. player.sendPacket(sm);
  257. }
  258. }
  259. // Remove shot automation
  260. Map<Integer, Integer> activeSoulShots = player.getAutoSoulShot();
  261. for (int itemId : activeSoulShots.values())
  262. {
  263. player.removeAutoSoulShot(itemId);
  264. ExAutoSoulShot atk = new ExAutoSoulShot(itemId, 0);
  265. player.sendPacket(atk);
  266. }
  267. player.sendSkillList();
  268. }
  269. catch (Exception e)
  270. {
  271. }
  272. }
  273. }
  274. protected boolean portPlayersToArena()
  275. {
  276. boolean _playerOneCrash = (_playerOne == null || _playerOneDisconnected);
  277. boolean _playerTwoCrash = (_playerTwo == null || _playerTwoDisconnected);
  278. if (_playerOneCrash || _playerTwoCrash || _aborted)
  279. {
  280. _playerOne = null;
  281. _playerTwo = null;
  282. _aborted = true;
  283. return false;
  284. }
  285. try
  286. {
  287. x1 = _playerOne.getX();
  288. y1 = _playerOne.getY();
  289. z1 = _playerOne.getZ();
  290. x2 = _playerTwo.getX();
  291. y2 = _playerTwo.getY();
  292. z2 = _playerTwo.getZ();
  293. if (_playerOne.isSitting())
  294. _playerOne.standUp();
  295. if (_playerTwo.isSitting())
  296. _playerTwo.standUp();
  297. _playerOne.setTarget(null);
  298. _playerTwo.setTarget(null);
  299. _gamestarted = true;
  300. _playerOne.teleToLocation(_stadiumPort[0] + 1200, _stadiumPort[1], _stadiumPort[2], false);
  301. _playerTwo.teleToLocation(_stadiumPort[0] - 1200, _stadiumPort[1], _stadiumPort[2], false);
  302. _playerOne.sendPacket(new ExOlympiadMode(2));
  303. _playerTwo.sendPacket(new ExOlympiadMode(2));
  304. _spawnOne = SpawnBuffer(_stadiumPort[0] + 1100, _stadiumPort[1], _stadiumPort[2], OLY_BUFFER);
  305. _spawnTwo = SpawnBuffer(_stadiumPort[0] - 1100, _stadiumPort[1], _stadiumPort[2], OLY_BUFFER);
  306. _playerOne.setIsInOlympiadMode(true);
  307. _playerOne.setIsOlympiadStart(false);
  308. _playerOne.setOlympiadSide(1);
  309. _playerOne.olyBuff = 5;
  310. _playerTwo.setIsInOlympiadMode(true);
  311. _playerTwo.setIsOlympiadStart(false);
  312. _playerTwo.setOlympiadSide(2);
  313. _playerTwo.olyBuff = 5;
  314. }
  315. catch (NullPointerException e)
  316. {
  317. return false;
  318. }
  319. return true;
  320. }
  321. protected void sendMessageToPlayers(boolean toBattleBegin, int nsecond)
  322. {
  323. if (!toBattleBegin)
  324. _sm = new SystemMessage(SystemMessageId.YOU_WILL_ENTER_THE_OLYMPIAD_STADIUM_IN_S1_SECOND_S);
  325. else
  326. _sm = new SystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  327. _sm.addNumber(nsecond);
  328. try
  329. {
  330. for (L2PcInstance player : _players)
  331. player.sendPacket(_sm);
  332. }
  333. catch (Exception e)
  334. {
  335. }
  336. }
  337. protected void portPlayersBack()
  338. {
  339. if (_playerOne != null)
  340. _playerOne.teleToLocation(x1, y1, z1, true);
  341. if (_playerTwo != null)
  342. _playerTwo.teleToLocation(x2, y2, z2, true);
  343. }
  344. protected void PlayersStatusBack()
  345. {
  346. for (L2PcInstance player : _players)
  347. {
  348. try
  349. {
  350. if (player.isDead() == true)
  351. {
  352. player.setIsDead(false);
  353. }
  354. player.getStatus().startHpMpRegeneration();
  355. player.setCurrentCp(player.getMaxCp());
  356. player.setCurrentHp(player.getMaxHp());
  357. player.setCurrentMp(player.getMaxMp());
  358. player.setIsInOlympiadMode(false);
  359. player.setIsOlympiadStart(false);
  360. player.setOlympiadSide(-1);
  361. player.setOlympiadGameId(-1);
  362. player.sendPacket(new ExOlympiadMode(0));
  363. // Add Clan Skills
  364. if (player.getClan() != null)
  365. {
  366. for (L2Skill skill : player.getClan().getAllSkills())
  367. {
  368. if (skill.getMinPledgeClass() <= player.getPledgeClass())
  369. player.addSkill(skill, false);
  370. }
  371. }
  372. // Add Hero Skills
  373. if (player.isHero())
  374. {
  375. for (L2Skill skill : HeroSkillTable.getHeroSkills())
  376. player.addSkill(skill, false);
  377. }
  378. player.sendSkillList();
  379. }
  380. catch (Exception e)
  381. {
  382. }
  383. }
  384. }
  385. protected boolean haveWinner()
  386. {
  387. if (_aborted || _playerOne == null || _playerTwo == null ||
  388. _playerOneDisconnected || _playerTwoDisconnected)
  389. {
  390. return true;
  391. }
  392. double playerOneHp = 0;
  393. try
  394. {
  395. if (_playerOne != null && _playerOne.getOlympiadGameId() != -1)
  396. {
  397. playerOneHp = _playerOne.getCurrentHp();
  398. }
  399. }
  400. catch (Exception e)
  401. {
  402. playerOneHp = 0;
  403. }
  404. double playerTwoHp = 0;
  405. try
  406. {
  407. if (_playerTwo != null && _playerTwo.getOlympiadGameId() != -1)
  408. {
  409. playerTwoHp = _playerTwo.getCurrentHp();
  410. }
  411. }
  412. catch (Exception e)
  413. {
  414. playerTwoHp = 0;
  415. }
  416. if (playerTwoHp <= 0 || playerOneHp <= 0)
  417. {
  418. return true;
  419. }
  420. return false;
  421. }
  422. protected void validateWinner()
  423. {
  424. if (_aborted)
  425. return;
  426. boolean _pOneCrash = (_playerOne == null || _playerOneDisconnected);
  427. boolean _pTwoCrash = (_playerTwo == null || _playerTwoDisconnected);
  428. int _div;
  429. int _gpreward;
  430. String classed;
  431. switch (_type)
  432. {
  433. case NON_CLASSED:
  434. _div = 5;
  435. _gpreward = Config.ALT_OLY_NONCLASSED_RITEM_C;
  436. classed = "no";
  437. break;
  438. default:
  439. _div = 3;
  440. _gpreward = Config.ALT_OLY_CLASSED_RITEM_C;
  441. classed = "yes";
  442. break;
  443. }
  444. StatsSet playerOneStat = Olympiad.getNobleStats(_playerOneID);
  445. StatsSet playerTwoStat = Olympiad.getNobleStats(_playerTwoID);
  446. int playerOnePlayed = playerOneStat.getInteger(COMP_DONE);
  447. int playerTwoPlayed = playerTwoStat.getInteger(COMP_DONE);
  448. int playerOneWon = playerOneStat.getInteger(COMP_WON);
  449. int playerTwoWon = playerTwoStat.getInteger(COMP_WON);
  450. int playerOneLost = playerOneStat.getInteger(COMP_LOST);
  451. int playerTwoLost = playerTwoStat.getInteger(COMP_LOST);
  452. int playerOneDrawn = playerOneStat.getInteger(COMP_DRAWN);
  453. int playerTwoDrawn = playerTwoStat.getInteger(COMP_DRAWN);
  454. int playerOnePoints = playerOneStat.getInteger(POINTS);
  455. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  456. int pointDiff = Math.min(playerOnePoints, playerTwoPoints) / _div;
  457. // Check for if a player defaulted before battle started
  458. if (_playerOneDefaulted || _playerTwoDefaulted)
  459. {
  460. if (_playerOneDefaulted)
  461. {
  462. int lostPoints = playerOnePoints / 3;
  463. playerOneStat.set(POINTS, playerOnePoints - lostPoints);
  464. Olympiad.updateNobleStats(_playerOneID, playerOneStat);
  465. SystemMessage sm = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  466. sm.addString(_playerOneName);
  467. sm.addNumber(lostPoints);
  468. broadcastMessage(sm, false);
  469. if (Config.DEBUG)
  470. _log.info("Olympia Result: " + _playerOneName + " lost " + lostPoints + " points for defaulting");
  471. Olympiad.logResult(_playerOneName,_playerTwoName,0D,0D,0,0,_playerOneName+" default",lostPoints,classed);
  472. }
  473. if (_playerTwoDefaulted)
  474. {
  475. int lostPoints = playerTwoPoints / 3;
  476. playerTwoStat.set(POINTS, playerTwoPoints - lostPoints);
  477. Olympiad.updateNobleStats(_playerTwoID, playerTwoStat);
  478. SystemMessage sm = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  479. sm.addString(_playerTwoName);
  480. sm.addNumber(lostPoints);
  481. broadcastMessage(sm, false);
  482. if (Config.DEBUG)
  483. _log.info("Olympia Result: " + _playerTwoName + " lost " + lostPoints + " points for defaulting");
  484. Olympiad.logResult(_playerOneName,_playerTwoName,0D,0D,0,0,_playerTwoName+" default",lostPoints,classed);
  485. }
  486. return;
  487. }
  488. // Create results for players if a player crashed
  489. if (_pOneCrash || _pTwoCrash)
  490. {
  491. if (_pOneCrash && !_pTwoCrash)
  492. {
  493. try
  494. {
  495. playerOneStat.set(POINTS, playerOnePoints - pointDiff);
  496. playerOneStat.set(COMP_LOST, playerOneLost + 1);
  497. if (Config.DEBUG)
  498. _log.info("Olympia Result: " + _playerOneName + " vs " + _playerTwoName + " ... "
  499. + _playerOneName + " lost " + pointDiff + " points for crash");
  500. Olympiad.logResult(_playerOneName,_playerTwoName,0D,0D,0,0,_playerOneName+" crash",pointDiff,classed);
  501. playerTwoStat.set(POINTS, playerTwoPoints + pointDiff);
  502. playerTwoStat.set(COMP_WON, playerTwoWon + 1);
  503. if (Config.DEBUG)
  504. _log.info("Olympia Result: " + _playerOneName + " vs " + _playerTwoName + " ... "
  505. + _playerTwoName + " Win " + pointDiff + " points");
  506. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  507. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  508. _sm.addString(_playerTwoName);
  509. broadcastMessage(_sm, true);
  510. _sm2.addString(_playerTwoName);
  511. _sm2.addNumber(pointDiff);
  512. broadcastMessage(_sm2, false);
  513. }
  514. catch (Exception e)
  515. {
  516. e.printStackTrace();
  517. }
  518. }
  519. else if (_pTwoCrash && !_pOneCrash)
  520. {
  521. try
  522. {
  523. playerTwoStat.set(POINTS, playerTwoPoints - pointDiff);
  524. playerTwoStat.set(COMP_LOST, playerTwoLost + 1);
  525. if (Config.DEBUG)
  526. _log.info("Olympia Result: " + _playerTwoName + " vs " + _playerOneName + " ... "
  527. + _playerTwoName + " lost " + pointDiff + " points for crash");
  528. Olympiad.logResult(_playerOneName,_playerTwoName,0D,0D,0,0,_playerTwoName+" crash",pointDiff,classed);
  529. playerOneStat.set(POINTS, playerOnePoints + pointDiff);
  530. playerOneStat.set(COMP_WON, playerOneWon + 1);
  531. if (Config.DEBUG)
  532. _log.info("Olympia Result: " + _playerTwoName + " vs " + _playerOneName + " ... "
  533. + _playerOneName + " Win " + pointDiff + " points");
  534. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  535. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  536. _sm.addString(_playerOneName);
  537. broadcastMessage(_sm, true);
  538. _sm2.addString(_playerOneName);
  539. _sm2.addNumber(pointDiff);
  540. broadcastMessage(_sm2, false);
  541. }
  542. catch (Exception e)
  543. {
  544. e.printStackTrace();
  545. }
  546. }
  547. else if (_pOneCrash && _pTwoCrash)
  548. {
  549. try
  550. {
  551. playerOneStat.set(POINTS, playerOnePoints - pointDiff);
  552. playerOneStat.set(COMP_LOST, playerOneLost + 1);
  553. playerTwoStat.set(POINTS, playerTwoPoints - pointDiff);
  554. playerTwoStat.set(COMP_LOST, playerTwoLost + 1);
  555. if (Config.DEBUG)
  556. _log.info("Olympia Result: " + _playerOneName + " vs " + _playerTwoName + " ... "
  557. + " both lost " + pointDiff + " points for crash");
  558. Olympiad.logResult(_playerOneName,_playerTwoName,0D,0D,0,0,"both crash",pointDiff,classed);
  559. }
  560. catch (Exception e)
  561. {
  562. e.printStackTrace();
  563. }
  564. }
  565. playerOneStat.set(COMP_DONE, playerOnePlayed + 1);
  566. playerTwoStat.set(COMP_DONE, playerTwoPlayed + 1);
  567. Olympiad.updateNobleStats(_playerOneID, playerOneStat);
  568. Olympiad.updateNobleStats(_playerTwoID, playerTwoStat);
  569. return;
  570. }
  571. double playerOneHp = 0;
  572. if (!_playerOne.isDead())
  573. {
  574. playerOneHp = _playerOne.getCurrentHp() + _playerOne.getCurrentCp();
  575. }
  576. double playerTwoHp = 0;
  577. if (!_playerTwo.isDead())
  578. {
  579. playerTwoHp = _playerTwo.getCurrentHp() + _playerTwo.getCurrentCp();
  580. }
  581. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  582. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  583. _sm3 = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  584. String result = "";
  585. // if players crashed, search if they've relogged
  586. _playerOne = L2World.getInstance().getPlayer(_playerOneName);
  587. _players.set(0, _playerOne);
  588. _playerTwo = L2World.getInstance().getPlayer(_playerTwoName);
  589. _players.set(1, _playerTwo);
  590. String winner = "draw";
  591. if (_playerOne == null && _playerTwo == null)
  592. {
  593. playerOneStat.set(COMP_DRAWN, playerOneDrawn + 1);
  594. playerTwoStat.set(COMP_DRAWN, playerTwoDrawn + 1);
  595. result = " tie";
  596. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  597. broadcastMessage(_sm, true);
  598. }
  599. else if (_playerTwo == null
  600. || _playerTwo.isOnline() == 0
  601. || (playerTwoHp == 0 && playerOneHp != 0)
  602. || (_damageP1 > _damageP2 && playerTwoHp != 0 && playerOneHp != 0))
  603. {
  604. playerOneStat.set(POINTS, playerOnePoints + pointDiff);
  605. playerTwoStat.set(POINTS, playerTwoPoints - pointDiff);
  606. playerOneStat.set(COMP_WON, playerOneWon + 1);
  607. playerTwoStat.set(COMP_LOST, playerTwoLost + 1);
  608. _sm.addString(_playerOneName);
  609. broadcastMessage(_sm, true);
  610. _sm2.addString(_playerOneName);
  611. _sm2.addNumber(pointDiff);
  612. broadcastMessage(_sm2, false);
  613. _sm3.addString(_playerTwoName);
  614. _sm3.addNumber(pointDiff);
  615. broadcastMessage(_sm3, false);
  616. winner = _playerOneName + " won";
  617. try
  618. {
  619. result = " (" + playerOneHp + "hp vs " + playerTwoHp + "hp - "
  620. + _damageP1 + "dmg vs " + _damageP2 + "dmg) "
  621. + _playerOneName + " win " + pointDiff + " points";
  622. L2ItemInstance item = _playerOne.getInventory().addItem("Olympiad", Config.ALT_OLY_BATTLE_REWARD_ITEM, _gpreward, _playerOne, null);
  623. InventoryUpdate iu = new InventoryUpdate();
  624. iu.addModifiedItem(item);
  625. _playerOne.sendPacket(iu);
  626. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  627. sm.addItemName(item);
  628. sm.addNumber(_gpreward);
  629. _playerOne.sendPacket(sm);
  630. }
  631. catch (Exception e)
  632. {
  633. }
  634. }
  635. else if (_playerOne == null
  636. || _playerOne.isOnline() == 0
  637. || (playerOneHp == 0 && playerTwoHp != 0)
  638. || (_damageP2 > _damageP1 && playerOneHp != 0 && playerTwoHp != 0))
  639. {
  640. playerTwoStat.set(POINTS, playerTwoPoints + pointDiff);
  641. playerOneStat.set(POINTS, playerOnePoints - pointDiff);
  642. playerTwoStat.set(COMP_WON, playerTwoWon + 1);
  643. playerOneStat.set(COMP_LOST, playerOneLost + 1);
  644. _sm.addString(_playerTwoName);
  645. broadcastMessage(_sm, true);
  646. _sm2.addString(_playerTwoName);
  647. _sm2.addNumber(pointDiff);
  648. broadcastMessage(_sm2, false);
  649. _sm3.addString(_playerOneName);
  650. _sm3.addNumber(pointDiff);
  651. broadcastMessage(_sm3, false);
  652. winner = _playerTwoName + " won";
  653. try
  654. {
  655. result = " (" + playerOneHp + "hp vs " + playerTwoHp + "hp - "
  656. + _damageP1 + "dmg vs " + _damageP2 + "dmg) "
  657. + _playerTwoName + " win " + pointDiff + " points";
  658. L2ItemInstance item = _playerTwo.getInventory().addItem("Olympiad", Config.ALT_OLY_BATTLE_REWARD_ITEM, _gpreward, _playerTwo, null);
  659. InventoryUpdate iu = new InventoryUpdate();
  660. iu.addModifiedItem(item);
  661. _playerTwo.sendPacket(iu);
  662. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  663. sm.addItemName(item);
  664. sm.addNumber(_gpreward);
  665. _playerTwo.sendPacket(sm);
  666. }
  667. catch (Exception e)
  668. {
  669. }
  670. }
  671. else
  672. {
  673. result = " tie";
  674. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  675. broadcastMessage(_sm, true);
  676. int pointOneDiff = playerOnePoints / 5;
  677. int pointTwoDiff = playerTwoPoints / 5;
  678. playerOneStat.set(POINTS, playerOnePoints - pointOneDiff);
  679. playerTwoStat.set(POINTS, playerTwoPoints - pointTwoDiff);
  680. playerOneStat.set(COMP_DRAWN, playerOneDrawn + 1);
  681. playerTwoStat.set(COMP_DRAWN, playerTwoDrawn + 1);
  682. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  683. _sm2.addString(_playerOneName);
  684. _sm2.addNumber(pointOneDiff);
  685. broadcastMessage(_sm2, false);
  686. _sm3 = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  687. _sm3.addString(_playerTwoName);
  688. _sm3.addNumber(pointTwoDiff);
  689. broadcastMessage(_sm3, false);
  690. }
  691. if (Config.DEBUG)
  692. _log.info("Olympia Result: " + _playerOneName + " vs " + _playerTwoName + " ... " + result);
  693. playerOneStat.set(COMP_DONE, playerOnePlayed + 1);
  694. playerTwoStat.set(COMP_DONE, playerTwoPlayed + 1);
  695. Olympiad.updateNobleStats(_playerOneID, playerOneStat);
  696. Olympiad.updateNobleStats(_playerTwoID, playerTwoStat);
  697. Olympiad.logResult(_playerOneName, _playerTwoName, playerOneHp, playerTwoHp, _damageP1, _damageP2, winner, pointDiff, classed);
  698. for (int i = 40; i > 10; i -= 10)
  699. {
  700. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  701. _sm.addNumber(i);
  702. broadcastMessage(_sm, false);
  703. try
  704. {
  705. Thread.sleep(10000);
  706. }
  707. catch (InterruptedException e)
  708. {
  709. }
  710. if (i == 20)
  711. {
  712. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  713. _sm.addNumber(10);
  714. broadcastMessage(_sm, false);
  715. try
  716. {
  717. Thread.sleep(5000);
  718. }
  719. catch (InterruptedException e)
  720. {
  721. }
  722. }
  723. }
  724. for (int i = 5; i > 0; i--)
  725. {
  726. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  727. _sm.addNumber(i);
  728. broadcastMessage(_sm, false);
  729. try
  730. {
  731. Thread.sleep(1000);
  732. }
  733. catch (InterruptedException e)
  734. {
  735. }
  736. }
  737. }
  738. protected boolean makeCompetitionStart()
  739. {
  740. if (_aborted)
  741. return false;
  742. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  743. broadcastMessage(_sm, true);
  744. try
  745. {
  746. for (L2PcInstance player : _players)
  747. {
  748. player.setIsOlympiadStart(true);
  749. }
  750. }
  751. catch (Exception e)
  752. {
  753. _aborted = true;
  754. return false;
  755. }
  756. return true;
  757. }
  758. protected void addDamage(L2PcInstance player, int damage)
  759. {
  760. if (_playerOne == null || _playerTwo == null)
  761. return;
  762. if (player == _playerOne)
  763. _damageP1 += damage;
  764. else if (player == _playerTwo)
  765. _damageP2 += damage;
  766. }
  767. protected String getTitle()
  768. {
  769. String msg = "";
  770. msg += _playerOneName + " / " + _playerTwoName;
  771. return msg;
  772. }
  773. protected L2PcInstance[] getPlayers()
  774. {
  775. L2PcInstance[] players = new L2PcInstance[2];
  776. if (_playerOne == null || _playerTwo == null)
  777. return null;
  778. players[0] = _playerOne;
  779. players[1] = _playerTwo;
  780. return players;
  781. }
  782. private void broadcastMessage(SystemMessage sm, boolean toAll)
  783. {
  784. try
  785. {
  786. _playerOne.sendPacket(sm);
  787. _playerTwo.sendPacket(sm);
  788. }
  789. catch (Exception e)
  790. {
  791. }
  792. if (toAll && OlympiadManager.STADIUMS[_stadiumID].getSpectators() != null)
  793. {
  794. for (L2PcInstance spec : OlympiadManager.STADIUMS[_stadiumID].getSpectators())
  795. {
  796. try
  797. {
  798. spec.sendPacket(sm);
  799. }
  800. catch (NullPointerException e)
  801. {
  802. }
  803. }
  804. }
  805. }
  806. protected void announceGame()
  807. {
  808. for (L2Spawn manager : SpawnTable.getInstance().getSpawnTable().values())
  809. {
  810. if (manager != null && manager.getNpcid() == OLY_MANAGER)
  811. {
  812. int objId = manager.getLastSpawn().getObjectId();
  813. String npcName = manager.getLastSpawn().getName();
  814. manager.getLastSpawn().broadcastPacket(new CreatureSay(objId, 1, npcName, "Olympiad is going to begin in Arena " + (_stadiumID + 1) + " in a moment."));
  815. }
  816. }
  817. }
  818. }
  819. /**
  820. *
  821. * @author ascharot
  822. *
  823. */
  824. class OlympiadGameTask implements Runnable
  825. {
  826. protected static final Logger _log = Logger.getLogger(OlympiadGameTask.class.getName());
  827. public OlympiadGame _game = null;
  828. protected static final long BATTLE_PERIOD = Config.ALT_OLY_BATTLE; // 6 mins
  829. private boolean _terminated = false;
  830. private boolean _started = false;
  831. public boolean isTerminated()
  832. {
  833. return _terminated || _game._aborted;
  834. }
  835. public boolean isStarted()
  836. {
  837. return _started;
  838. }
  839. public OlympiadGameTask(OlympiadGame game)
  840. {
  841. _game = game;
  842. }
  843. protected boolean checkBattleStatus()
  844. {
  845. boolean _pOneCrash = (_game._playerOne == null || _game._playerOneDisconnected);
  846. boolean _pTwoCrash = (_game._playerTwo == null || _game._playerTwoDisconnected);
  847. if (_pOneCrash || _pTwoCrash || _game._aborted)
  848. {
  849. return false;
  850. }
  851. return true;
  852. }
  853. protected boolean checkDefaulted()
  854. {
  855. _game._playerOne = L2World.getInstance().getPlayer(_game._playerOneName);
  856. _game._players.set(0, _game._playerOne);
  857. _game._playerTwo = L2World.getInstance().getPlayer(_game._playerTwoName);
  858. _game._players.set(1, _game._playerTwo);
  859. for (int i = 0; i < 2; i++)
  860. {
  861. boolean defaulted = false;
  862. L2PcInstance player = _game._players.get(i);
  863. if (player != null)
  864. player.setOlympiadGameId(_game._stadiumID);
  865. L2PcInstance otherPlayer = _game._players.get(i^1);
  866. SystemMessage sm = null;
  867. if (player == null)
  868. {
  869. defaulted = true;
  870. }
  871. else if (player.isDead())
  872. {
  873. sm = new SystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_OLYMPIAD_WHILE_DEAD);
  874. sm.addPcName(player);
  875. defaulted = true;
  876. }
  877. else if (player.isSubClassActive())
  878. {
  879. sm = new SystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_IN_OLYMPIAD_WHILE_CHANGED_TO_SUB_CLASS);
  880. sm.addPcName(player);
  881. defaulted = true;
  882. }
  883. else if (player.isCursedWeaponEquipped())
  884. {
  885. sm = new SystemMessage(SystemMessageId.CANNOT_JOIN_OLYMPIAD_POSSESSING_S1);
  886. sm.addItemName(player.getCursedWeaponEquippedId());
  887. defaulted = true;
  888. }
  889. else if (player.getInventoryLimit()*0.8 <= player.getInventory().getSize())
  890. {
  891. sm = new SystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_IN_OLYMPIAD_INVENTORY_SLOT_EXCEEDS_80_PERCENT);
  892. sm.addPcName(player);
  893. defaulted = true;
  894. }
  895. if (defaulted)
  896. {
  897. if (player != null)
  898. player.sendPacket(sm);
  899. if (otherPlayer != null)
  900. otherPlayer.sendPacket(new SystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_DOES_NOT_MEET_THE_REQUIREMENTS_FOR_JOINING_THE_GAME));
  901. if (i == 0)
  902. _game._playerOneDefaulted = true;
  903. else
  904. _game._playerTwoDefaulted = true;
  905. }
  906. }
  907. return _game._playerOneDefaulted || _game._playerTwoDefaulted;
  908. }
  909. public void run()
  910. {
  911. _started = true;
  912. if (_game != null)
  913. {
  914. if (_game._playerOne == null || _game._playerTwo == null)
  915. {
  916. return;
  917. }
  918. if (teleportCountdown())
  919. runGame();
  920. _terminated = true;
  921. _game.validateWinner();
  922. _game.PlayersStatusBack();
  923. if (_game._gamestarted)
  924. {
  925. _game._gamestarted = false;
  926. OlympiadManager.STADIUMS[_game._stadiumID].closeDoors();
  927. try
  928. {
  929. _game.portPlayersBack();
  930. }
  931. catch (Exception e)
  932. {
  933. e.printStackTrace();
  934. }
  935. }
  936. if (OlympiadManager.STADIUMS[_game._stadiumID].getSpectators() != null)
  937. {
  938. for (L2PcInstance spec : OlympiadManager.STADIUMS[_game._stadiumID].getSpectators())
  939. {
  940. spec.sendPacket(new ExOlympiadMatchEnd());
  941. }
  942. }
  943. if (_game._spawnOne != null)
  944. {
  945. _game._spawnOne.getLastSpawn().deleteMe();
  946. _game._spawnOne = null;
  947. }
  948. if (_game._spawnTwo != null)
  949. {
  950. _game._spawnTwo.getLastSpawn().deleteMe();
  951. _game._spawnTwo = null;
  952. }
  953. _game.clearPlayers();
  954. OlympiadManager.getInstance().removeGame(_game);
  955. _game = null;
  956. }
  957. }
  958. private boolean runGame()
  959. {
  960. // Checking for opponents and teleporting to arena
  961. if (checkDefaulted())
  962. {
  963. return false;
  964. }
  965. OlympiadManager.STADIUMS[_game._stadiumID].closeDoors();
  966. _game.removals();
  967. _game.portPlayersToArena();
  968. if (Config.ALT_OLY_ANNOUNCE_GAMES)
  969. _game.announceGame();
  970. try
  971. {
  972. Thread.sleep(5000);
  973. }
  974. catch (InterruptedException e)
  975. {
  976. }
  977. synchronized (this)
  978. {
  979. if (!OlympiadGame._battleStarted)
  980. OlympiadGame._battleStarted = true;
  981. }
  982. for (int i = 60; i > 10; i -= 10)
  983. {
  984. _game.sendMessageToPlayers(true, i);
  985. try
  986. {
  987. Thread.sleep(10000);
  988. }
  989. catch (InterruptedException e)
  990. {
  991. }
  992. if (i == 20)
  993. {
  994. _game._damageP1 = 0;
  995. _game._damageP2 = 0;
  996. OlympiadManager.STADIUMS[_game._stadiumID].openDoors();
  997. _game.sendMessageToPlayers(true, 10);
  998. try
  999. {
  1000. Thread.sleep(5000);
  1001. }
  1002. catch (InterruptedException e)
  1003. {
  1004. }
  1005. }
  1006. }
  1007. for (int i = 5; i > 0; i--)
  1008. {
  1009. _game.sendMessageToPlayers(true, i);
  1010. try
  1011. {
  1012. Thread.sleep(1000);
  1013. }
  1014. catch (InterruptedException e)
  1015. {
  1016. }
  1017. }
  1018. if (!checkBattleStatus())
  1019. {
  1020. return false;
  1021. }
  1022. _game._playerOne.sendPacket(new ExOlympiadUserInfo(_game._playerTwo, 1));
  1023. _game._playerTwo.sendPacket(new ExOlympiadUserInfo(_game._playerOne, 1));
  1024. if (OlympiadManager.STADIUMS[_game._stadiumID].getSpectators() != null)
  1025. {
  1026. for (L2PcInstance spec : OlympiadManager.STADIUMS[_game._stadiumID].getSpectators())
  1027. {
  1028. spec.sendPacket(new ExOlympiadUserInfo(_game._playerOne, 1));
  1029. spec.sendPacket(new ExOlympiadUserInfo(_game._playerTwo, 2));
  1030. spec.sendPacket(new ExOlympiadSpelledInfo(_game._playerOne));
  1031. spec.sendPacket(new ExOlympiadSpelledInfo(_game._playerTwo));
  1032. }
  1033. }
  1034. _game._spawnOne.getLastSpawn().deleteMe();
  1035. _game._spawnTwo.getLastSpawn().deleteMe();
  1036. _game._spawnOne = null;
  1037. _game._spawnTwo = null;
  1038. if (!_game.makeCompetitionStart())
  1039. {
  1040. return false;
  1041. }
  1042. // Wait 3 mins (Battle)
  1043. for (int i = 0; i < BATTLE_PERIOD; i += 10000)
  1044. {
  1045. try
  1046. {
  1047. Thread.sleep(10000);
  1048. // If game haveWinner then stop waiting battle_period
  1049. // and validate winner
  1050. if (_game.haveWinner())
  1051. break;
  1052. }
  1053. catch (InterruptedException e)
  1054. {
  1055. }
  1056. }
  1057. return checkBattleStatus();
  1058. }
  1059. private boolean teleportCountdown()
  1060. {
  1061. // Waiting for teleport to arena
  1062. for (int i = 120; i > 10; i -= 5)
  1063. {
  1064. switch (i)
  1065. {
  1066. case 120:
  1067. case 60:
  1068. case 30:
  1069. case 15:
  1070. _game.sendMessageToPlayers(false, i);
  1071. break;
  1072. }
  1073. try
  1074. {
  1075. Thread.sleep(5000);
  1076. }
  1077. catch (InterruptedException e)
  1078. {
  1079. return false;
  1080. }
  1081. }
  1082. for (int i = 5; i > 0; i--)
  1083. {
  1084. _game.sendMessageToPlayers(false, i);
  1085. try
  1086. {
  1087. Thread.sleep(1000);
  1088. }
  1089. catch (InterruptedException e)
  1090. {
  1091. return false;
  1092. }
  1093. }
  1094. return true;
  1095. }
  1096. }