OlympiadGame.java 35 KB

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