OlympiadGame.java 33 KB

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