123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- /*
- * This program 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.
- *
- * This program 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.instancemanager;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.List;
- import java.util.Properties;
- import java.util.StringTokenizer;
- 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.L2DatabaseFactory;
- import com.l2jserver.gameserver.datatables.SkillTable;
- import com.l2jserver.gameserver.model.CombatFlag;
- import com.l2jserver.gameserver.model.L2Clan;
- import com.l2jserver.gameserver.model.L2Object;
- import com.l2jserver.gameserver.model.Location;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.entity.Fort;
- import com.l2jserver.gameserver.model.entity.FortSiege;
- import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- public class FortSiegeManager
- {
- private static final Logger _log = Logger.getLogger(FortSiegeManager.class.getName());
-
- public static final FortSiegeManager getInstance()
- {
- return SingletonHolder._instance;
- }
-
- // =========================================================
- // Data Field
- private int _attackerMaxClans = 500; // Max number of clans
-
- // Fort Siege settings
- private FastMap<Integer, FastList<SiegeSpawn>> _commanderSpawnList;
- private FastMap<Integer, FastList<CombatFlag>> _flagList;
- private int _flagMaxCount = 1; // Changeable in fortsiege.properties
- private int _siegeClanMinLevel = 4; // Changeable in fortsiege.properties
- private int _siegeLength = 60; // Time in minute. Changeable in fortsiege.properties
- private int _countDownLength = 10; // Time in minute. Changeable in fortsiege.properties
- private int _suspiciousMerchantRespawnDelay = 180; // Time in minute. Changeable in fortsiege.properties
- private List<FortSiege> _sieges;
-
- // =========================================================
- // Constructor
- private FortSiegeManager()
- {
- _log.info("Initializing FortSiegeManager");
- load();
- }
-
- // =========================================================
- // Method - Public
- public final void addSiegeSkills(L2PcInstance character)
- {
- character.addSkill(SkillTable.FrequentSkill.SEAL_OF_RULER.getSkill(), false);
- character.addSkill(SkillTable.FrequentSkill.BUILD_HEADQUARTERS.getSkill(), false);
- }
-
- /**
- * @param activeChar The L2Character of the character can summon
- * @param isCheckOnly
- * @return true if character summon
- */
- public final boolean checkIfOkToSummon(L2Character activeChar, boolean isCheckOnly)
- {
- if (!(activeChar instanceof L2PcInstance))
- return false;
-
- String text = "";
- L2PcInstance player = (L2PcInstance) activeChar;
- Fort fort = FortManager.getInstance().getFort(player);
-
- if (fort == null || fort.getFortId() <= 0)
- text = "You must be on fort ground to summon this";
- else if (!fort.getSiege().getIsInProgress())
- text = "You can only summon this during a siege.";
- else if (player.getClanId() != 0 && fort.getSiege().getAttackerClan(player.getClanId()) == null)
- text = "You can only summon this as a registered attacker.";
- else
- return true;
-
- if (!isCheckOnly)
- player.sendMessage(text);
- return false;
- }
-
- /**
- * @param clan The L2Clan of the player
- * @param fortid
- * @return true if the clan is registered or owner of a fort
- */
- public final boolean checkIsRegistered(L2Clan clan, int fortid)
- {
- if (clan == null)
- return false;
-
- Connection con = null;
- boolean register = false;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT clan_id FROM fortsiege_clans where clan_id=? and fort_id=?");
- statement.setInt(1, clan.getClanId());
- statement.setInt(2, fortid);
- ResultSet rs = statement.executeQuery();
-
- while (rs.next())
- {
- register = true;
- break;
- }
-
- rs.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: checkIsRegistered(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- return register;
- }
-
- public final void removeSiegeSkills(L2PcInstance character)
- {
- character.removeSkill(SkillTable.FrequentSkill.SEAL_OF_RULER.getSkill());
- character.removeSkill(SkillTable.FrequentSkill.BUILD_HEADQUARTERS.getSkill());
- }
-
- // =========================================================
- // Method - Private
- private final void load()
- {
- InputStream is = null;
- try
- {
- is = new FileInputStream(new File(Config.FORTSIEGE_CONFIGURATION_FILE));
- Properties siegeSettings = new Properties();
- siegeSettings.load(is);
-
- // Siege setting
- _attackerMaxClans = Integer.decode(siegeSettings.getProperty("AttackerMaxClans", "500"));
- _flagMaxCount = Integer.decode(siegeSettings.getProperty("MaxFlags", "1"));
- _siegeClanMinLevel = Integer.decode(siegeSettings.getProperty("SiegeClanMinLevel", "4"));
- _siegeLength = Integer.decode(siegeSettings.getProperty("SiegeLength", "60"));
- _countDownLength = Integer.decode(siegeSettings.getProperty("CountDownLength", "10"));
- _suspiciousMerchantRespawnDelay = Integer.decode(siegeSettings.getProperty("SuspiciousMerchantRespawnDelay", "180"));
-
- // Siege spawns settings
- _commanderSpawnList = new FastMap<Integer, FastList<SiegeSpawn>>();
- _flagList = new FastMap<Integer, FastList<CombatFlag>>();
-
- for (Fort fort : FortManager.getInstance().getForts())
- {
- FastList<SiegeSpawn> _commanderSpawns = new FastList<SiegeSpawn>();
- FastList<CombatFlag> _flagSpawns = new FastList<CombatFlag>();
- for (int i = 1; i < 5; i++)
- {
- String _spawnParams = siegeSettings.getProperty(fort.getName().replace(" ", "") + "Commander" + i, "");
- if (_spawnParams.length() == 0)
- break;
- StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
-
- try
- {
- int x = Integer.parseInt(st.nextToken());
- int y = Integer.parseInt(st.nextToken());
- int z = Integer.parseInt(st.nextToken());
- int heading = Integer.parseInt(st.nextToken());
- int npc_id = Integer.parseInt(st.nextToken());
-
- _commanderSpawns.add(new SiegeSpawn(fort.getFortId(), x, y, z, heading, npc_id, i));
- }
- catch (Exception e)
- {
- _log.warning("Error while loading commander(s) for " + fort.getName() + " fort.");
- }
- }
-
- _commanderSpawnList.put(fort.getFortId(), _commanderSpawns);
-
- for (int i = 1; i < 4; i++)
- {
- String _spawnParams = siegeSettings.getProperty(fort.getName().replace(" ", "") + "Flag" + i, "");
- if (_spawnParams.length() == 0)
- break;
- StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
-
- try
- {
- int x = Integer.parseInt(st.nextToken());
- int y = Integer.parseInt(st.nextToken());
- int z = Integer.parseInt(st.nextToken());
- int flag_id = Integer.parseInt(st.nextToken());
-
- _flagSpawns.add(new CombatFlag(fort.getFortId(), x, y, z, 0, flag_id));
- }
- catch (Exception e)
- {
- _log.warning("Error while loading flag(s) for " + fort.getName() + " fort.");
- }
- }
- _flagList.put(fort.getFortId(), _flagSpawns);
- }
-
- }
- catch (Exception e)
- {
- //_initialized = false;
- _log.log(Level.WARNING, "Error while loading fortsiege data." + e.getMessage(), e);
- }
- finally
- {
- try
- {
- is.close();
- }
- catch (Exception e)
- {
- }
- }
- }
-
- // =========================================================
- // Property - Public
- public final FastList<SiegeSpawn> getCommanderSpawnList(int _fortId)
- {
- if (_commanderSpawnList.containsKey(_fortId))
- {
- return _commanderSpawnList.get(_fortId);
- }
- return null;
- }
-
- public final FastList<CombatFlag> getFlagList(int _fortId)
- {
- if (_flagList.containsKey(_fortId))
- return _flagList.get(_fortId);
- return null;
- }
-
- public final int getAttackerMaxClans()
- {
- return _attackerMaxClans;
- }
-
- public final int getFlagMaxCount()
- {
- return _flagMaxCount;
- }
-
- public final int getSuspiciousMerchantRespawnDelay()
- {
- return _suspiciousMerchantRespawnDelay;
- }
-
- public final FortSiege getSiege(L2Object activeObject)
- {
- return getSiege(activeObject.getX(), activeObject.getY(), activeObject.getZ());
- }
-
- public final FortSiege getSiege(int x, int y, int z)
- {
- for (Fort fort : FortManager.getInstance().getForts())
- if (fort.getSiege().checkIfInZone(x, y, z))
- return fort.getSiege();
- return null;
- }
-
- public final int getSiegeClanMinLevel()
- {
- return _siegeClanMinLevel;
- }
-
- public final int getSiegeLength()
- {
- return _siegeLength;
- }
-
- public final int getCountDownLength()
- {
- return _countDownLength;
- }
-
- public final List<FortSiege> getSieges()
- {
- if (_sieges == null)
- _sieges = new FastList<FortSiege>();
- return _sieges;
- }
-
- public final void addSiege(FortSiege fortSiege)
- {
- if (_sieges == null)
- _sieges = new FastList<FortSiege>();
- _sieges.add(fortSiege);
- }
-
- public boolean isCombat(int itemId)
- {
- return (itemId == 9819);
- }
-
- public boolean activateCombatFlag(L2PcInstance player, L2ItemInstance item)
- {
- if (!checkIfCanPickup(player))
- return false;
-
- Fort fort = FortManager.getInstance().getFort(player);
-
- FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
- for (CombatFlag cf : fcf)
- {
- if (cf.getCombatFlagInstance() == item)
- {
- cf.activate(player, item);
- }
- }
- return true;
- }
-
- public boolean checkIfCanPickup(L2PcInstance player)
- {
- SystemMessage sm;
- sm = SystemMessage.getSystemMessage(SystemMessageId.THE_FORTRESS_BATTLE_OF_S1_HAS_FINISHED);
- sm.addItemName(9819);
- // Cannot own 2 combat flag
- if (player.isCombatFlagEquipped())
- {
- player.sendPacket(sm);
- return false;
- }
-
- // here check if is siege is in progress
- // here check if is siege is attacker
- Fort fort = FortManager.getInstance().getFort(player);
-
- if (fort == null || fort.getFortId() <= 0)
- {
- player.sendPacket(sm);
- return false;
- }
- else if (!fort.getSiege().getIsInProgress())
- {
- player.sendPacket(sm);
- return false;
- }
- else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
- {
- player.sendPacket(sm);
- return false;
- }
- return true;
- }
-
- public void dropCombatFlag(L2PcInstance player, int fortId)
- {
- Fort fort = FortManager.getInstance().getFortById(fortId);
-
- FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
-
- for (CombatFlag cf : fcf)
- {
- if (cf.getPlayerOnjectId() == player.getObjectId())
- {
- cf.dropIt();
- if (fort.getSiege().getIsInProgress())
- cf.spawnMe();
- }
- }
- }
-
- public static class SiegeSpawn
- {
- Location _location;
- private int _npcId;
- private int _heading;
- private int _fortId;
- private int _id;
-
- public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id, int id)
- {
- _fortId = fort_id;
- _location = new Location(x, y, z, heading);
- _heading = heading;
- _npcId = npc_id;
- _id = id;
- }
-
- public int getFortId()
- {
- return _fortId;
- }
-
- public int getNpcId()
- {
- return _npcId;
- }
-
- public int getHeading()
- {
- return _heading;
- }
-
- public int getId()
- {
- return _id;
- }
-
- public Location getLocation()
- {
- return _location;
- }
- }
-
- @SuppressWarnings("synthetic-access")
- private static class SingletonHolder
- {
- protected static final FortSiegeManager _instance = new FortSiegeManager();
- }
- }
|