12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358 |
- /*
- * Copyright (C) 2004-2013 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model.entity;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastList;
- import javolution.util.FastMap;
- import com.l2jserver.Config;
- import com.l2jserver.gameserver.cache.HtmCache;
- import com.l2jserver.gameserver.datatables.DoorTable;
- import com.l2jserver.gameserver.datatables.ItemTable;
- import com.l2jserver.gameserver.datatables.NpcTable;
- import com.l2jserver.gameserver.datatables.SkillTable;
- import com.l2jserver.gameserver.datatables.SpawnTable;
- import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
- import com.l2jserver.gameserver.instancemanager.InstanceManager;
- import com.l2jserver.gameserver.model.L2Spawn;
- import com.l2jserver.gameserver.model.L2World;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.actor.L2Npc;
- import com.l2jserver.gameserver.model.actor.L2Summon;
- import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
- import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
- import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
- import com.l2jserver.gameserver.model.olympiad.OlympiadManager;
- import com.l2jserver.gameserver.model.skills.L2Skill;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.clientpackets.Say2;
- import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
- import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
- import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
- import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- import com.l2jserver.gameserver.scripting.scriptengine.events.TvtKillEvent;
- import com.l2jserver.gameserver.scripting.scriptengine.impl.L2Script.EventStage;
- import com.l2jserver.gameserver.scripting.scriptengine.listeners.events.TvTListener;
- import com.l2jserver.util.Rnd;
- import com.l2jserver.util.StringUtil;
- /**
- * @author FBIagent
- */
- public class TvTEvent
- {
- enum EventState
- {
- INACTIVE,
- INACTIVATING,
- PARTICIPATING,
- STARTING,
- STARTED,
- REWARDING
- }
-
- protected static final Logger _log = Logger.getLogger(TvTEvent.class.getName());
- /** html path **/
- private static final String htmlPath = "data/html/mods/TvTEvent/";
- /** The teams of the TvTEvent<br> */
- private static TvTEventTeam[] _teams = new TvTEventTeam[2];
- /** The state of the TvTEvent<br> */
- private static EventState _state = EventState.INACTIVE;
- /** The spawn of the participation npc<br> */
- private static L2Spawn _npcSpawn = null;
- /** the npc instance of the participation npc<br> */
- private static L2Npc _lastNpcSpawn = null;
- /** Instance id<br> */
- private static int _TvTEventInstance = 0;
-
- private static FastList<TvTListener> tvtListeners = new FastList<TvTListener>().shared();
-
- /**
- * No instance of this class!<br>
- */
- private TvTEvent()
- {
- }
-
- /**
- * Teams initializing<br>
- */
- public static void init()
- {
- AntiFeedManager.getInstance().registerEvent(AntiFeedManager.TVT_ID);
- _teams[0] = new TvTEventTeam(Config.TVT_EVENT_TEAM_1_NAME, Config.TVT_EVENT_TEAM_1_COORDINATES);
- _teams[1] = new TvTEventTeam(Config.TVT_EVENT_TEAM_2_NAME, Config.TVT_EVENT_TEAM_2_COORDINATES);
- }
-
- /**
- * Starts the participation of the TvTEvent<br>
- * 1. Get L2NpcTemplate by Config.TVT_EVENT_PARTICIPATION_NPC_ID<br>
- * 2. Try to spawn a new npc of it<br>
- * <br>
- * @return boolean: true if success, otherwise false<br>
- */
- public static boolean startParticipation()
- {
- L2NpcTemplate tmpl = NpcTable.getInstance().getTemplate(Config.TVT_EVENT_PARTICIPATION_NPC_ID);
-
- if (tmpl == null)
- {
- _log.warning("TvTEventEngine[TvTEvent.startParticipation()]: L2NpcTemplate is a NullPointer -> Invalid npc id in configs?");
- return false;
- }
-
- try
- {
- _npcSpawn = new L2Spawn(tmpl);
-
- _npcSpawn.setLocx(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0]);
- _npcSpawn.setLocy(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1]);
- _npcSpawn.setLocz(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
- _npcSpawn.setAmount(1);
- _npcSpawn.setHeading(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[3]);
- _npcSpawn.setRespawnDelay(1);
- // later no need to delete spawn from db, we don't store it (false)
- SpawnTable.getInstance().addNewSpawn(_npcSpawn, false);
- _npcSpawn.init();
- _lastNpcSpawn = _npcSpawn.getLastSpawn();
- _lastNpcSpawn.setCurrentHp(_lastNpcSpawn.getMaxHp());
- _lastNpcSpawn.setTitle("TvT Event Participation");
- _lastNpcSpawn.isAggressive();
- _lastNpcSpawn.decayMe();
- _lastNpcSpawn.spawnMe(_npcSpawn.getLastSpawn().getX(), _npcSpawn.getLastSpawn().getY(), _npcSpawn.getLastSpawn().getZ());
- _lastNpcSpawn.broadcastPacket(new MagicSkillUse(_lastNpcSpawn, _lastNpcSpawn, 1034, 1, 1, 1));
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "TvTEventEngine[TvTEvent.startParticipation()]: exception: " + e.getMessage(), e);
- return false;
- }
-
- setState(EventState.PARTICIPATING);
- fireTvtEventListeners(EventStage.REGISTRATION_BEGIN);
- return true;
- }
-
- private static int highestLevelPcInstanceOf(Map<Integer, L2PcInstance> players)
- {
- int maxLevel = Integer.MIN_VALUE, maxLevelId = -1;
- for (L2PcInstance player : players.values())
- {
- if (player.getLevel() >= maxLevel)
- {
- maxLevel = player.getLevel();
- maxLevelId = player.getObjectId();
- }
- }
- return maxLevelId;
- }
-
- /**
- * Starts the TvTEvent fight<br>
- * 1. Set state EventState.STARTING<br>
- * 2. Close doors specified in configs<br>
- * 3. Abort if not enought participants(return false)<br>
- * 4. Set state EventState.STARTED<br>
- * 5. Teleport all participants to team spot<br>
- * <br>
- * @return boolean: true if success, otherwise false<br>
- */
- public static boolean startFight()
- {
- // Set state to STARTING
- setState(EventState.STARTING);
-
- // Randomize and balance team distribution
- Map<Integer, L2PcInstance> allParticipants = new FastMap<>();
- allParticipants.putAll(_teams[0].getParticipatedPlayers());
- allParticipants.putAll(_teams[1].getParticipatedPlayers());
- _teams[0].cleanMe();
- _teams[1].cleanMe();
-
- L2PcInstance player;
- Iterator<L2PcInstance> iter;
- if (needParticipationFee())
- {
- iter = allParticipants.values().iterator();
- while (iter.hasNext())
- {
- player = iter.next();
- if (!hasParticipationFee(player))
- {
- iter.remove();
- }
- }
- }
-
- int balance[] =
- {
- 0,
- 0
- }, priority = 0, highestLevelPlayerId;
- L2PcInstance highestLevelPlayer;
- // XXX: allParticipants should be sorted by level instead of using highestLevelPcInstanceOf for every fetch
- while (!allParticipants.isEmpty())
- {
- // Priority team gets one player
- highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
- highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
- allParticipants.remove(highestLevelPlayerId);
- _teams[priority].addPlayer(highestLevelPlayer);
- balance[priority] += highestLevelPlayer.getLevel();
- // Exiting if no more players
- if (allParticipants.isEmpty())
- {
- break;
- }
- // The other team gets one player
- // XXX: Code not dry
- priority = 1 - priority;
- highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
- highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
- allParticipants.remove(highestLevelPlayerId);
- _teams[priority].addPlayer(highestLevelPlayer);
- balance[priority] += highestLevelPlayer.getLevel();
- // Recalculating priority
- priority = balance[0] > balance[1] ? 1 : 0;
- }
-
- // Check for enought participants
- if ((_teams[0].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS) || (_teams[1].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS))
- {
- // Set state INACTIVE
- setState(EventState.INACTIVE);
- // Cleanup of teams
- _teams[0].cleanMe();
- _teams[1].cleanMe();
- // Unspawn the event NPC
- unSpawnNpc();
- AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
- return false;
- }
-
- if (needParticipationFee())
- {
- iter = _teams[0].getParticipatedPlayers().values().iterator();
- while (iter.hasNext())
- {
- player = iter.next();
- if (!payParticipationFee(player))
- {
- iter.remove();
- }
- }
- iter = _teams[1].getParticipatedPlayers().values().iterator();
- while (iter.hasNext())
- {
- player = iter.next();
- if (!payParticipationFee(player))
- {
- iter.remove();
- }
- }
- }
-
- if (Config.TVT_EVENT_IN_INSTANCE)
- {
- try
- {
- _TvTEventInstance = InstanceManager.getInstance().createDynamicInstance(Config.TVT_EVENT_INSTANCE_FILE);
- InstanceManager.getInstance().getInstance(_TvTEventInstance).setAllowSummon(false);
- InstanceManager.getInstance().getInstance(_TvTEventInstance).setPvPInstance(true);
- InstanceManager.getInstance().getInstance(_TvTEventInstance).setEmptyDestroyTime((Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY * 1000) + 60000L);
- }
- catch (Exception e)
- {
- _TvTEventInstance = 0;
- _log.log(Level.WARNING, "TvTEventEngine[TvTEvent.createDynamicInstance]: exception: " + e.getMessage(), e);
- }
- }
-
- // Opens all doors specified in configs for tvt
- openDoors(Config.TVT_DOORS_IDS_TO_OPEN);
- // Closes all doors specified in configs for tvt
- closeDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
- // Set state STARTED
- setState(EventState.STARTED);
-
- // Iterate over all teams
- for (TvTEventTeam team : _teams)
- {
- // Iterate over all participated player instances in this team
- for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- // Disable player revival.
- playerInstance.setCanRevive(false);
- // Teleporter implements Runnable and starts itself
- new TvTEventTeleporter(playerInstance, team.getCoordinates(), false, false);
- }
- }
- }
- fireTvtEventListeners(EventStage.START);
-
- return true;
- }
-
- /**
- * Calculates the TvTEvent reward<br>
- * 1. If both teams are at a tie(points equals), send it as system message to all participants, if one of the teams have 0 participants left online abort rewarding<br>
- * 2. Wait till teams are not at a tie anymore<br>
- * 3. Set state EvcentState.REWARDING<br>
- * 4. Reward team with more points<br>
- * 5. Show win html to wining team participants<br>
- * <br>
- * @return String: winning team name<br>
- */
- public static String calculateRewards()
- {
- if (_teams[0].getPoints() == _teams[1].getPoints())
- {
- // Check if one of the teams have no more players left
- if ((_teams[0].getParticipatedPlayerCount() == 0) || (_teams[1].getParticipatedPlayerCount() == 0))
- {
- // set state to rewarding
- setState(EventState.REWARDING);
- // return here, the fight can't be completed
- return "TvT Event: Event has ended. No team won due to inactivity!";
- }
-
- // Both teams have equals points
- sysMsgToAllParticipants("TvT Event: Event has ended, both teams have tied.");
- if (Config.TVT_REWARD_TEAM_TIE)
- {
- rewardTeam(_teams[0]);
- rewardTeam(_teams[1]);
- return "TvT Event: Event has ended with both teams tying.";
- }
- return "TvT Event: Event has ended with both teams tying.";
- }
-
- // Set state REWARDING so nobody can point anymore
- setState(EventState.REWARDING);
-
- // Get team which has more points
- TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
- rewardTeam(team);
- fireTvtEventListeners(EventStage.END);
- return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
- }
-
- private static void rewardTeam(TvTEventTeam team)
- {
- // Iterate over all participated player instances of the winning team
- for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
- {
- // Check for nullpointer
- if (playerInstance == null)
- {
- continue;
- }
-
- SystemMessage systemMessage = null;
-
- // Iterate over all tvt event rewards
- for (int[] reward : Config.TVT_EVENT_REWARDS)
- {
- PcInventory inv = playerInstance.getInventory();
-
- // Check for stackable item, non stackabe items need to be added one by one
- if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
- {
- inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
-
- if (reward[1] > 1)
- {
- systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
- systemMessage.addItemName(reward[0]);
- systemMessage.addItemNumber(reward[1]);
- }
- else
- {
- systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
- systemMessage.addItemName(reward[0]);
- }
-
- playerInstance.sendPacket(systemMessage);
- }
- else
- {
- for (int i = 0; i < reward[1]; ++i)
- {
- inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
- systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
- systemMessage.addItemName(reward[0]);
- playerInstance.sendPacket(systemMessage);
- }
- }
- }
-
- StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
-
- statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Reward.htm"));
- playerInstance.sendPacket(statusUpdate);
- playerInstance.sendPacket(npcHtmlMessage);
- }
- }
-
- /**
- * Stops the TvTEvent fight<br>
- * 1. Set state EventState.INACTIVATING<br>
- * 2. Remove tvt npc from world<br>
- * 3. Open doors specified in configs<br>
- * 4. Teleport all participants back to participation npc location<br>
- * 5. Teams cleaning<br>
- * 6. Set state EventState.INACTIVE<br>
- */
- public static void stopFight()
- {
- // Set state INACTIVATING
- setState(EventState.INACTIVATING);
- // Unspawn event npc
- unSpawnNpc();
- // Opens all doors specified in configs for tvt
- openDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
- // Closes all doors specified in Configs for tvt
- closeDoors(Config.TVT_DOORS_IDS_TO_OPEN);
-
- // Iterate over all teams
- for (TvTEventTeam team : _teams)
- {
- for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
- {
- // Check for nullpointer
- if (playerInstance != null)
- {
- // Enable player revival.
- playerInstance.setCanRevive(true);
- // Teleport back.
- new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false, false);
- }
- }
- }
-
- // Cleanup of teams
- _teams[0].cleanMe();
- _teams[1].cleanMe();
- // Set state INACTIVE
- setState(EventState.INACTIVE);
- AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
- }
-
- /**
- * Adds a player to a TvTEvent team<br>
- * 1. Calculate the id of the team in which the player should be added<br>
- * 2. Add the player to the calculated team<br>
- * <br>
- * @param playerInstance as L2PcInstance<br>
- * @return boolean: true if success, otherwise false<br>
- */
- public static synchronized boolean addParticipant(L2PcInstance playerInstance)
- {
- // Check for nullpoitner
- if (playerInstance == null)
- {
- return false;
- }
-
- byte teamId = 0;
-
- // Check to which team the player should be added
- if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
- {
- teamId = (byte) (Rnd.get(2));
- }
- else
- {
- teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
- }
- playerInstance.addEventListener(new TvTEventListener(playerInstance));
- return _teams[teamId].addPlayer(playerInstance);
- }
-
- /**
- * Removes a TvTEvent player from it's team<br>
- * 1. Get team id of the player<br>
- * 2. Remove player from it's team<br>
- * <br>
- * @param playerObjectId
- * @return boolean: true if success, otherwise false
- */
- public static boolean removeParticipant(int playerObjectId)
- {
- // Get the teamId of the player
- byte teamId = getParticipantTeamId(playerObjectId);
-
- // Check if the player is participant
- if (teamId != -1)
- {
- // Remove the player from team
- _teams[teamId].removePlayer(playerObjectId);
-
- final L2PcInstance player = L2World.getInstance().getPlayer(playerObjectId);
- if (player != null)
- {
- player.removeEventListener(TvTEventListener.class);
- }
-
- return true;
- }
-
- return false;
- }
-
- public static boolean needParticipationFee()
- {
- return (Config.TVT_EVENT_PARTICIPATION_FEE[0] != 0) && (Config.TVT_EVENT_PARTICIPATION_FEE[1] != 0);
- }
-
- public static boolean hasParticipationFee(L2PcInstance playerInstance)
- {
- return playerInstance.getInventory().getInventoryItemCount(Config.TVT_EVENT_PARTICIPATION_FEE[0], -1) >= Config.TVT_EVENT_PARTICIPATION_FEE[1];
- }
-
- public static boolean payParticipationFee(L2PcInstance playerInstance)
- {
- return playerInstance.destroyItemByItemId("TvT Participation Fee", Config.TVT_EVENT_PARTICIPATION_FEE[0], Config.TVT_EVENT_PARTICIPATION_FEE[1], _lastNpcSpawn, true);
- }
-
- public static String getParticipationFee()
- {
- int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
- int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
-
- if ((itemId == 0) || (itemNum == 0))
- {
- return "-";
- }
-
- return StringUtil.concat(String.valueOf(itemNum), " ", ItemTable.getInstance().getTemplate(itemId).getName());
- }
-
- /**
- * Send a SystemMessage to all participated players<br>
- * 1. Send the message to all players of team number one<br>
- * 2. Send the message to all players of team number two<br>
- * <br>
- * @param message as String<br>
- */
- public static void sysMsgToAllParticipants(String message)
- {
- for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- playerInstance.sendMessage(message);
- }
- }
-
- for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- playerInstance.sendMessage(message);
- }
- }
- }
-
- /**
- * Close doors specified in configs
- * @param doors
- */
- private static void closeDoors(List<Integer> doors)
- {
- for (int doorId : doors)
- {
- L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
-
- if (doorInstance != null)
- {
- doorInstance.closeMe();
- }
- }
- }
-
- /**
- * Open doors specified in configs
- * @param doors
- */
- private static void openDoors(List<Integer> doors)
- {
- for (int doorId : doors)
- {
- L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
-
- if (doorInstance != null)
- {
- doorInstance.openMe();
- }
- }
- }
-
- /**
- * UnSpawns the TvTEvent npc
- */
- private static void unSpawnNpc()
- {
- // Delete the npc
- _lastNpcSpawn.deleteMe();
- SpawnTable.getInstance().deleteSpawn(_lastNpcSpawn.getSpawn(), false);
- // Stop respawning of the npc
- _npcSpawn.stopRespawn();
- _npcSpawn = null;
- _lastNpcSpawn = null;
- }
-
- /**
- * Called when a player logs in<br>
- * <br>
- * @param playerInstance as L2PcInstance<br>
- */
- public static void onLogin(L2PcInstance playerInstance)
- {
- if ((playerInstance == null) || (!isStarting() && !isStarted()))
- {
- return;
- }
-
- byte teamId = getParticipantTeamId(playerInstance.getObjectId());
-
- if (teamId == -1)
- {
- return;
- }
-
- _teams[teamId].addPlayer(playerInstance);
- new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
- }
-
- /**
- * Called when a player logs out<br>
- * <br>
- * @param playerInstance as L2PcInstance<br>
- */
- public static void onLogout(L2PcInstance playerInstance)
- {
- if ((playerInstance != null) && (isStarting() || isStarted() || isParticipating()))
- {
- if (removeParticipant(playerInstance.getObjectId()))
- {
- playerInstance.setXYZInvisible((Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)) - 50, (Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)) - 50, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
- }
- }
- }
-
- /**
- * Called on every bypass by npc of type L2TvTEventNpc<br>
- * Needs synchronization cause of the max player check<br>
- * <br>
- * @param command as String<br>
- * @param playerInstance as L2PcInstance<br>
- */
- public static synchronized void onBypass(String command, L2PcInstance playerInstance)
- {
- if ((playerInstance == null) || !isParticipating())
- {
- return;
- }
-
- final String htmContent;
-
- if (command.equals("tvt_event_participation"))
- {
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
- int playerLevel = playerInstance.getLevel();
-
- if (playerInstance.isCursedWeaponEquipped())
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "CursedWeaponEquipped.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- }
- }
- else if (OlympiadManager.getInstance().isRegistered(playerInstance))
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Olympiad.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- }
- }
- else if (playerInstance.getKarma() > 0)
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Karma.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- }
- }
- else if ((playerLevel < Config.TVT_EVENT_MIN_LVL) || (playerLevel > Config.TVT_EVENT_MAX_LVL))
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Level.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
- npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
- }
- }
- else if ((_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS) && (_teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS))
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "TeamsFull.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
- }
- }
- else if ((Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0) && !AntiFeedManager.getInstance().tryAddPlayer(AntiFeedManager.TVT_ID, playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP))
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "IPRestriction.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%max%", String.valueOf(AntiFeedManager.getInstance().getLimit(playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP)));
- }
- }
- else if (needParticipationFee() && !hasParticipationFee(playerInstance))
- {
- htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "ParticipationFee.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%fee%", getParticipationFee());
- }
- }
- else if (addParticipant(playerInstance))
- {
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Registered.htm"));
- }
- else
- {
- return;
- }
-
- playerInstance.sendPacket(npcHtmlMessage);
- }
- else if (command.equals("tvt_event_remove_participation"))
- {
- removeParticipant(playerInstance.getObjectId());
- if (Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0)
- {
- AntiFeedManager.getInstance().removePlayer(AntiFeedManager.TVT_ID, playerInstance);
- }
-
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
-
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Unregistered.htm"));
- playerInstance.sendPacket(npcHtmlMessage);
- }
- }
-
- /**
- * Called on every onAction in L2PcIstance<br>
- * <br>
- * @param playerInstance
- * @param targetedPlayerObjectId
- * @return boolean: true if player is allowed to target, otherwise false
- */
- public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
- {
- if ((playerInstance == null) || !isStarted())
- {
- return true;
- }
-
- if (playerInstance.isGM())
- {
- return true;
- }
-
- byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
- byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
-
- if (((playerTeamId != -1) && (targetedPlayerTeamId == -1)) || ((playerTeamId == -1) && (targetedPlayerTeamId != -1)))
- {
- return false;
- }
-
- if ((playerTeamId != -1) && (targetedPlayerTeamId != -1) && (playerTeamId == targetedPlayerTeamId) && (playerInstance.getObjectId() != targetedPlayerObjectId) && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
- {
- return false;
- }
-
- return true;
- }
-
- /**
- * Called on every scroll use<br>
- * <br>
- * @param playerObjectId
- * @return boolean: true if player is allowed to use scroll, otherwise false
- */
- public static boolean onScrollUse(int playerObjectId)
- {
- if (!isStarted())
- {
- return true;
- }
-
- if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
- {
- return false;
- }
-
- return true;
- }
-
- /**
- * Called on every potion use
- * @param playerObjectId
- * @return boolean: true if player is allowed to use potions, otherwise false
- */
- public static boolean onPotionUse(int playerObjectId)
- {
- if (!isStarted())
- {
- return true;
- }
-
- if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
- {
- return false;
- }
-
- return true;
- }
-
- /**
- * Called on every escape use(thanks to nbd)
- * @param playerObjectId
- * @return boolean: true if player is not in tvt event, otherwise false
- */
- public static boolean onEscapeUse(int playerObjectId)
- {
- if (!isStarted())
- {
- return true;
- }
-
- if (isPlayerParticipant(playerObjectId))
- {
- return false;
- }
-
- return true;
- }
-
- /**
- * Called on every summon item use
- * @param playerObjectId
- * @return boolean: true if player is allowed to summon by item, otherwise false
- */
- public static boolean onItemSummon(int playerObjectId)
- {
- if (!isStarted())
- {
- return true;
- }
-
- if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
- {
- return false;
- }
-
- return true;
- }
-
- /**
- * Is called when a player is killed<br>
- * <br>
- * @param killerCharacter as L2Character<br>
- * @param killedPlayerInstance as L2PcInstance<br>
- */
- public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
- {
- if ((killedPlayerInstance == null) || !isStarted())
- {
- return;
- }
-
- byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
-
- if (killedTeamId == -1)
- {
- return;
- }
-
- new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
-
- if (killerCharacter == null)
- {
- return;
- }
-
- L2PcInstance killerPlayerInstance = null;
-
- if ((killerCharacter instanceof L2PetInstance) || (killerCharacter instanceof L2ServitorInstance))
- {
- killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
-
- if (killerPlayerInstance == null)
- {
- return;
- }
- }
- else if (killerCharacter instanceof L2PcInstance)
- {
- killerPlayerInstance = (L2PcInstance) killerCharacter;
- }
- else
- {
- return;
- }
-
- byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
-
- if ((killerTeamId != -1) && (killedTeamId != -1) && (killerTeamId != killedTeamId))
- {
- TvTEventTeam killerTeam = _teams[killerTeamId];
-
- killerTeam.increasePoints();
-
- CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
-
- for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- playerInstance.sendPacket(cs);
- }
- }
- fireTvtKillListeners(killerPlayerInstance, killedPlayerInstance, killerTeam);
- }
- }
-
- /**
- * Called on Appearing packet received (player finished teleporting)
- * @param playerInstance
- */
- public static void onTeleported(L2PcInstance playerInstance)
- {
- if (!isStarted() || (playerInstance == null) || !isPlayerParticipant(playerInstance.getObjectId()))
- {
- return;
- }
-
- if (playerInstance.isMageClass())
- {
- if ((Config.TVT_EVENT_MAGE_BUFFS != null) && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
- {
- for (Entry<Integer, Integer> e : Config.TVT_EVENT_MAGE_BUFFS.entrySet())
- {
- L2Skill skill = SkillTable.getInstance().getInfo(e.getKey(), e.getValue());
- if (skill != null)
- {
- skill.getEffects(playerInstance, playerInstance);
- }
- }
- }
- }
- else
- {
- if ((Config.TVT_EVENT_FIGHTER_BUFFS != null) && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
- {
- for (Entry<Integer, Integer> e : Config.TVT_EVENT_FIGHTER_BUFFS.entrySet())
- {
- L2Skill skill = SkillTable.getInstance().getInfo(e.getKey(), e.getValue());
- if (skill != null)
- {
- skill.getEffects(playerInstance, playerInstance);
- }
- }
- }
- }
- }
-
- /**
- * @param source
- * @param target
- * @param skill
- * @return true if player valid for skill
- */
- public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, L2Skill skill)
- {
- if (!isStarted())
- {
- return true;
- }
- // TvT is started
- final int sourcePlayerId = source.getObjectId();
- final int targetPlayerId = target.getObjectId();
- final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
- final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
-
- // both players not participating
- if (!isSourceParticipant && !isTargetParticipant)
- {
- return true;
- }
- // one player not participating
- if (!(isSourceParticipant && isTargetParticipant))
- {
- return false;
- }
- // players in the different teams ?
- if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
- {
- if (!skill.isBad())
- {
- return false;
- }
- }
- return true;
- }
-
- /**
- * Sets the TvTEvent state<br>
- * <br>
- * @param state as EventState<br>
- */
- private static void setState(EventState state)
- {
- synchronized (_state)
- {
- _state = state;
- }
- }
-
- /**
- * Is TvTEvent inactive?<br>
- * <br>
- * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
- */
- public static boolean isInactive()
- {
- boolean isInactive;
-
- synchronized (_state)
- {
- isInactive = _state == EventState.INACTIVE;
- }
-
- return isInactive;
- }
-
- /**
- * Is TvTEvent in inactivating?<br>
- * <br>
- * @return boolean: true if event is in inactivating progress, otherwise false<br>
- */
- public static boolean isInactivating()
- {
- boolean isInactivating;
-
- synchronized (_state)
- {
- isInactivating = _state == EventState.INACTIVATING;
- }
-
- return isInactivating;
- }
-
- /**
- * Is TvTEvent in participation?<br>
- * <br>
- * @return boolean: true if event is in participation progress, otherwise false<br>
- */
- public static boolean isParticipating()
- {
- boolean isParticipating;
-
- synchronized (_state)
- {
- isParticipating = _state == EventState.PARTICIPATING;
- }
-
- return isParticipating;
- }
-
- /**
- * Is TvTEvent starting?<br>
- * <br>
- * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
- */
- public static boolean isStarting()
- {
- boolean isStarting;
-
- synchronized (_state)
- {
- isStarting = _state == EventState.STARTING;
- }
-
- return isStarting;
- }
-
- /**
- * Is TvTEvent started?<br>
- * <br>
- * @return boolean: true if event is started, otherwise false<br>
- */
- public static boolean isStarted()
- {
- boolean isStarted;
-
- synchronized (_state)
- {
- isStarted = _state == EventState.STARTED;
- }
-
- return isStarted;
- }
-
- /**
- * Is TvTEvent rewarding?<br>
- * <br>
- * @return boolean: true if event is currently rewarding, otherwise false<br>
- */
- public static boolean isRewarding()
- {
- boolean isRewarding;
-
- synchronized (_state)
- {
- isRewarding = _state == EventState.REWARDING;
- }
-
- return isRewarding;
- }
-
- /**
- * Returns the team id of a player, if player is not participant it returns -1
- * @param playerObjectId
- * @return byte: team name of the given playerName, if not in event -1
- */
- public static byte getParticipantTeamId(int playerObjectId)
- {
- return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
- }
-
- /**
- * Returns the team of a player, if player is not participant it returns null
- * @param playerObjectId
- * @return TvTEventTeam: team of the given playerObjectId, if not in event null
- */
- public static TvTEventTeam getParticipantTeam(int playerObjectId)
- {
- return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
- }
-
- /**
- * Returns the enemy team of a player, if player is not participant it returns null
- * @param playerObjectId
- * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null
- */
- public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
- {
- return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
- }
-
- /**
- * Returns the team coordinates in which the player is in, if player is not in a team return null
- * @param playerObjectId
- * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2
- */
- public static int[] getParticipantTeamCoordinates(int playerObjectId)
- {
- return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
- }
-
- /**
- * Is given player participant of the event?
- * @param playerObjectId
- * @return boolean: true if player is participant, ohterwise false
- */
- public static boolean isPlayerParticipant(int playerObjectId)
- {
- if (!isParticipating() && !isStarting() && !isStarted())
- {
- return false;
- }
-
- return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
- }
-
- /**
- * Returns participated player count<br>
- * <br>
- * @return int: amount of players registered in the event<br>
- */
- public static int getParticipatedPlayersCount()
- {
- if (!isParticipating() && !isStarting() && !isStarted())
- {
- return 0;
- }
-
- return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
- }
-
- /**
- * Returns teams names<br>
- * <br>
- * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static String[] getTeamNames()
- {
- return new String[]
- {
- _teams[0].getName(),
- _teams[1].getName()
- };
- }
-
- /**
- * Returns player count of both teams<br>
- * <br>
- * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static int[] getTeamsPlayerCounts()
- {
- return new int[]
- {
- _teams[0].getParticipatedPlayerCount(),
- _teams[1].getParticipatedPlayerCount()
- };
- }
-
- /**
- * Returns points count of both teams
- * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static int[] getTeamsPoints()
- {
- return new int[]
- {
- _teams[0].getPoints(),
- _teams[1].getPoints()
- };
- }
-
- public static int getTvTEventInstance()
- {
- return _TvTEventInstance;
- }
-
- // Listeners
- /**
- * Fires all the TvTListener.onKill() methods, if any
- * @param killer
- * @param victim
- * @param killerTeam
- */
- private static void fireTvtKillListeners(L2PcInstance killer, L2PcInstance victim, TvTEventTeam killerTeam)
- {
- if (!tvtListeners.isEmpty() && (killer != null) && (victim != null) && (killerTeam != null))
- {
- TvtKillEvent event = new TvtKillEvent();
- event.setKiller(killer);
- event.setVictim(victim);
- event.setKillerTeam(killerTeam);
- for (TvTListener listener : tvtListeners)
- {
- listener.onKill(event);
- }
- }
- }
-
- /**
- * Fires the appropriate TvtEventListeners, if any
- * @param stage
- */
- private static void fireTvtEventListeners(EventStage stage)
- {
- if (!tvtListeners.isEmpty())
- {
- switch (stage)
- {
- case REGISTRATION_BEGIN:
- {
- for (TvTListener listener : tvtListeners)
- {
- listener.onRegistrationStart();
- }
- break;
- }
- case START:
- {
- for (TvTListener listener : tvtListeners)
- {
- listener.onBegin();
- }
- break;
- }
- case END:
- {
- for (TvTListener listener : tvtListeners)
- {
- listener.onEnd();
- }
- break;
- }
- }
- }
- }
-
- /**
- * Adds a TvT listener
- * @param listener
- */
- public static void addTvTListener(TvTListener listener)
- {
- if (!tvtListeners.contains(listener))
- {
- tvtListeners.add(listener);
- }
- }
-
- /**
- * Removes a TvT listener
- * @param listener
- */
- public static void removeTvtListener(TvTListener listener)
- {
- tvtListeners.remove(listener);
- }
- }
|