12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397 |
- /*
- * 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.model.entity;
- import gnu.trove.TIntIntHashMap;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Calendar;
- import java.util.Collection;
- import java.util.List;
- import java.util.concurrent.ScheduledFuture;
- 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.FortUpdater;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.datatables.ClanTable;
- import com.l2jserver.gameserver.datatables.DoorTable;
- import com.l2jserver.gameserver.datatables.NpcTable;
- import com.l2jserver.gameserver.datatables.ResidentialSkillTable;
- import com.l2jserver.gameserver.datatables.SpawnTable;
- import com.l2jserver.gameserver.datatables.StaticObjects;
- import com.l2jserver.gameserver.instancemanager.FortManager;
- import com.l2jserver.gameserver.instancemanager.ZoneManager;
- import com.l2jserver.gameserver.model.L2Clan;
- import com.l2jserver.gameserver.model.L2Object;
- import com.l2jserver.gameserver.model.L2Skill;
- import com.l2jserver.gameserver.model.L2Spawn;
- import com.l2jserver.gameserver.model.L2World;
- import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
- import com.l2jserver.gameserver.model.zone.L2ZoneType;
- import com.l2jserver.gameserver.model.zone.type.L2FortZone;
- import com.l2jserver.gameserver.model.zone.type.L2SiegeZone;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.serverpackets.PlaySound;
- import com.l2jserver.gameserver.network.serverpackets.PledgeShowInfoUpdate;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
- public class Fort
- {
- protected static final Logger _log = Logger.getLogger(Fort.class.getName());
-
- // =========================================================
- // Data Field
- private int _fortId = 0;
- private List<L2DoorInstance> _doors = new FastList<L2DoorInstance>();
- private L2StaticObjectInstance _flagPole = null;
- private List<String> _doorDefault = new FastList<String>();
- private List<String> _flagPoleStats = new FastList<String>();
- private String _name = "";
- private FortSiege _siege = null;
- private Calendar _siegeDate;
- private Calendar _lastOwnedTime;
- private L2FortZone _fortZone;
- private L2SiegeZone _zone;
- private L2Clan _fortOwner = null;
- private int _fortType = 0;
- private int _state = 0;
- private int _castleId = 0;
- private int _blood = 0;
- private FastMap<Integer, FortFunction> _function;
- private FastList<L2Skill> _residentialSkills = new FastList<L2Skill>();
- private ScheduledFuture<?> _FortUpdater;
-
- // Spawn Data
- private boolean _isSuspiciousMerchantSpawned = false;
- private FastList<L2Spawn> _siegeNpcs = new FastList<L2Spawn>();
- private FastList<L2Spawn> _npcCommanders = new FastList<L2Spawn>();
- private FastList<L2Spawn> _specialEnvoys = new FastList<L2Spawn>();
-
- private TIntIntHashMap _envoyCastles = new TIntIntHashMap(2);
-
- /** Fortress Functions */
- public static final int FUNC_TELEPORT = 1;
- public static final int FUNC_RESTORE_HP = 2;
- public static final int FUNC_RESTORE_MP = 3;
- public static final int FUNC_RESTORE_EXP = 4;
- public static final int FUNC_SUPPORT = 5;
-
- public class FortFunction
- {
- private int _type;
- private int _lvl;
- protected int _fee;
- protected int _tempFee;
- private long _rate;
- private long _endDate;
- protected boolean _inDebt;
- public boolean _cwh;
-
- public FortFunction(int type, int lvl, int lease, int tempLease, long rate, long time, boolean cwh)
- {
- _type = type;
- _lvl = lvl;
- _fee = lease;
- _tempFee = tempLease;
- _rate = rate;
- _endDate = time;
- initializeTask(cwh);
- }
-
- public int getType()
- {
- return _type;
- }
-
- public int getLvl()
- {
- return _lvl;
- }
-
- public int getLease()
- {
- return _fee;
- }
-
- public long getRate()
- {
- return _rate;
- }
-
- public long getEndTime()
- {
- return _endDate;
- }
-
- public void setLvl(int lvl)
- {
- _lvl = lvl;
- }
-
- public void setLease(int lease)
- {
- _fee = lease;
- }
-
- public void setEndTime(long time)
- {
- _endDate = time;
- }
-
- private void initializeTask(boolean cwh)
- {
- if (getOwnerClan() == null)
- return;
- long currentTime = System.currentTimeMillis();
- if (_endDate > currentTime)
- ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), _endDate - currentTime);
- else
- ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), 0);
- }
-
- private class FunctionTask implements Runnable
- {
- public FunctionTask(boolean cwh)
- {
- _cwh = cwh;
- }
-
- public void run()
- {
- try
- {
- if (getOwnerClan() == null)
- return;
- if (getOwnerClan().getWarehouse().getAdena() >= _fee || !_cwh)
- {
- int fee = _fee;
- if (getEndTime() == -1)
- fee = _tempFee;
-
- setEndTime(System.currentTimeMillis() + getRate());
- dbSave();
- if (_cwh)
- {
- getOwnerClan().getWarehouse().destroyItemByItemId("CS_function_fee", 57, fee, null, null);
- if (Config.DEBUG)
- _log.warning("Deducted " + fee + " adena from " + getName() + " owner's cwh for function id : " + getType());
- }
- ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(true), getRate());
- }
- else
- removeFunction(getType());
- }
- catch (Throwable t)
- {
- }
- }
- }
-
- public void dbSave()
- {
- Connection con = null;
- try
- {
- PreparedStatement statement;
-
- con = L2DatabaseFactory.getInstance().getConnection();
- statement = con.prepareStatement("REPLACE INTO fort_functions (fort_id, type, lvl, lease, rate, endTime) VALUES (?,?,?,?,?,?)");
- statement.setInt(1, getFortId());
- statement.setInt(2, getType());
- statement.setInt(3, getLvl());
- statement.setInt(4, getLease());
- statement.setLong(5, getRate());
- statement.setLong(6, getEndTime());
- statement.execute();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.SEVERE, "Exception: Fort.updateFunctions(int type, int lvl, int lease, long rate, long time, boolean addNew): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
- }
-
- // =========================================================
- // Constructor
- public Fort(int fortId)
- {
- _fortId = fortId;
- load();
- loadDoor();
- loadFlagPoles();
- _function = new FastMap<Integer, FortFunction>();
- _residentialSkills = ResidentialSkillTable.getInstance().getSkills(fortId);
- if (getOwnerClan() != null)
- {
- setVisibleFlag(true);
- loadFunctions();
- }
- initNpcs(); // load and spawn npcs (Always spawned)
- initSiegeNpcs(); // load suspicious merchants (Despawned 10mins before siege)
- //spawnSuspiciousMerchant();// spawn suspicious merchants
- initNpcCommanders(); // npc Commanders (not monsters) (Spawned during siege)
- spawnNpcCommanders(); // spawn npc Commanders
- initSpecialEnvoys(); // envoys from castles (Spawned after fort taken)
- if (getOwnerClan() != null && getFortState() == 0)
- {
- spawnSpecialEnvoys();
- ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleSpecialEnvoysDeSpawn(this), 1 * 60 * 60 * 1000); // Prepare 1hr task for special envoys despawn
- }
- }
-
- /** Return function with id */
- public FortFunction getFunction(int type)
- {
- if (_function.get(type) != null)
- return _function.get(type);
- return null;
- }
-
- public class ScheduleSpecialEnvoysDeSpawn implements Runnable
- {
- private Fort _fortInst;
-
- public ScheduleSpecialEnvoysDeSpawn(Fort pFort)
- {
- _fortInst = pFort;
- }
-
- public void run()
- {
- try
- {
- // if state not decided, change state to indenpendent
- if (_fortInst.getFortState() == 0)
- _fortInst.setFortState(1, 0);
- _fortInst.despawnSpecialEnvoys();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: ScheduleSpecialEnvoysSpawn() for Fort " + _fortInst.getName() + ": " + e.getMessage(), e);
- }
- }
- }
-
- // =========================================================
- // Method - Public
-
- public void endOfSiege(L2Clan clan)
- {
- ThreadPoolManager.getInstance().scheduleGeneral(new endFortressSiege(this, clan), 1000);
- }
-
- public void engrave(L2Clan clan)
- {
- setOwner(clan, true);
- }
-
- /**
- * Move non clan members off fort area and to nearest town.<BR><BR>
- */
- public void banishForeigners()
- {
- getFortZone().banishForeigners(getOwnerClan());
- }
-
- /**
- * Return true if object is inside the zone
- */
- public boolean checkIfInZone(int x, int y, int z)
- {
- return getZone().isInsideZone(x, y, z);
- }
-
- public L2SiegeZone getZone()
- {
- if (_zone == null)
- {
- for (L2ZoneType zone : ZoneManager.getInstance().getAllZones())
- {
- if (zone instanceof L2SiegeZone && ((L2SiegeZone) zone).getSiegeObjectId() == getFortId())
- {
- _zone = (L2SiegeZone) zone;
- break;
- }
- }
- }
- return _zone;
- }
-
- public L2FortZone getFortZone()
- {
- if (_fortZone == null)
- {
- for (L2ZoneType zone : ZoneManager.getInstance().getAllZones())
- {
- if (zone instanceof L2FortZone && ((L2FortZone) zone).getFortId() == getFortId())
- {
- _fortZone = (L2FortZone) zone;
- break;
- }
- }
- }
- return _fortZone;
- }
-
- /**
- * Get the objects distance to this fort
- * @param object
- * @return
- */
- public double getDistance(L2Object obj)
- {
- return getZone().getDistanceToZone(obj);
- }
-
- public void closeDoor(L2PcInstance activeChar, int doorId)
- {
- openCloseDoor(activeChar, doorId, false);
- }
-
- public void openDoor(L2PcInstance activeChar, int doorId)
- {
- openCloseDoor(activeChar, doorId, true);
- }
-
- public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
- {
- if (activeChar.getClan() != getOwnerClan())
- return;
-
- L2DoorInstance door = getDoor(doorId);
- if (door != null)
- {
- if (open)
- door.openMe();
- else
- door.closeMe();
- }
- }
-
- // This method is used to begin removing all fort upgrades
- public void removeUpgrade()
- {
- removeDoorUpgrade();
- }
-
- /**
- * This method will set owner for Fort
- * @param clan
- * @param updateClanPoints
- */
- public boolean setOwner(L2Clan clan, boolean updateClansReputation)
- {
- L2Clan oldowner = getOwnerClan();
-
- // Remove old owner
- if (oldowner != null && clan != null && clan != oldowner)
- {
- // Remove points from old owner
- updateClansReputation(oldowner, true);
- try
- {
- L2PcInstance oldleader = oldowner.getLeader().getPlayerInstance();
- if (oldleader != null)
- {
- if (oldleader.getMountType() == 2)
- oldleader.dismount();
- }
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception in setOwner: " + e.getMessage(), e);
- }
- removeOwner(true);
- }
- setFortState(0, 0); // initialize fort state
-
- // if clan already have castle, don't store him in fortress
- if (clan.getHasCastle() > 0)
- {
- getSiege().announceToPlayer(new SystemMessage(SystemMessageId.NPCS_RECAPTURED_FORTRESS));
- return false;
- }
- else
- {
- // Give points to new owner
- if (updateClansReputation)
- updateClansReputation(clan, false);
- spawnSpecialEnvoys();
- ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleSpecialEnvoysDeSpawn(this), 1 * 60 * 60 * 1000); // Prepare 1hr task for special envoys despawn
- // if clan have already fortress, remove it
- if (clan.getHasFort() > 0)
- FortManager.getInstance().getFortByOwner(clan).removeOwner(true);
-
- setBloodOathReward(0);
- setOwnerClan(clan);
- updateOwnerInDB(); // Update in database
-
- if (getSiege().getIsInProgress()) // If siege in progress
- getSiege().endSiege();
-
- for (L2PcInstance member : clan.getOnlineMembers(0))
- {
- giveResidentialSkills(member);
- member.sendSkillList();
- }
- return true;
- }
- }
-
- public void removeOwner(boolean updateDB)
- {
- L2Clan clan = getOwnerClan();
- if (clan != null)
- {
- for (L2PcInstance member : clan.getOnlineMembers(0))
- {
- removeResidentialSkills(member);
- member.sendSkillList();
- }
- clan.setHasFort(0);
- clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
- setOwnerClan(null);
- setBloodOathReward(0);
- if (updateDB)
- updateOwnerInDB();
- }
- }
-
- public void setBloodOathReward(int val)
- {
- _blood = val;
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
-
- statement = con.prepareStatement("UPDATE fort SET blood=? WHERE id = ?");
- statement.setInt(1, _blood);
- statement.setInt(2, getFortId());
- statement.execute();
- statement.close();
-
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: setBloodOathReward(int val): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- public int getBloodOathReward()
- {
- return _blood;
- }
-
- /**
- * Show or hide flag inside flagpole<BR><BR>
- */
- public void setVisibleFlag(boolean val)
- {
- L2StaticObjectInstance flagPole = getFlagPole();
- if (flagPole != null)
- flagPole.setMeshIndex(val ? 1 : 0);
- }
-
- /**
- * Respawn all doors on fort grounds<BR><BR>
- */
- public void resetDoors()
- {
- for (int i = 0; i < getDoors().size(); i++)
- {
- L2DoorInstance door = getDoors().get(i);
- if (door.getOpen())
- door.closeMe();
- if (door.getCurrentHp() <= 0)
- door.doRevive();
- if (door.getCurrentHp() < door.getMaxHp())
- door.setCurrentHp(door.getMaxHp());
- }
- loadDoorUpgrade(); // Check for any upgrade the doors may have
- }
-
- // This method upgrade door
- public void upgradeDoor(int doorId, int hp, int pDef, int mDef)
- {
- L2DoorInstance door = getDoor(doorId);
- if (door != null && door.getDoorId() == doorId)
- {
- door.setCurrentHp(door.getMaxHp() + hp);
-
- saveDoorUpgrade(doorId, hp, pDef, mDef);
- return;
- }
- }
-
- // =========================================================
- // Method - Private
- // This method loads fort
- private void load()
- {
- Connection con = null;
- try
- {
- PreparedStatement statement;
- ResultSet rs;
-
- con = L2DatabaseFactory.getInstance().getConnection();
-
- statement = con.prepareStatement("SELECT * FROM fort WHERE id = ?");
- statement.setInt(1, getFortId());
- rs = statement.executeQuery();
- int ownerId = 0;
-
- while (rs.next())
- {
- _name = rs.getString("name");
-
- _siegeDate = Calendar.getInstance();
- _lastOwnedTime = Calendar.getInstance();
- _siegeDate.setTimeInMillis(rs.getLong("siegeDate"));
- _lastOwnedTime.setTimeInMillis(rs.getLong("lastOwnedTime"));
- ownerId = rs.getInt("owner");
- _fortType = rs.getInt("fortType");
- _state = rs.getInt("state");
- _castleId = rs.getInt("castleId");
- _blood = rs.getInt("blood");
- }
-
- rs.close();
- statement.close();
-
- if (ownerId > 0)
- {
- L2Clan clan = ClanTable.getInstance().getClan(ownerId); // Try to find clan instance
- clan.setHasFort(getFortId());
- setOwnerClan(clan);
- int runCount = getOwnedTime() / (Config.FS_BLOOD_OATH_FRQ * 60);
- long initial = System.currentTimeMillis() - _lastOwnedTime.getTimeInMillis();
- while (initial > (Config.FS_BLOOD_OATH_FRQ * 60000))
- initial -= (Config.FS_BLOOD_OATH_FRQ * 60000);
- initial = (Config.FS_BLOOD_OATH_FRQ * 60000) - initial;
- _FortUpdater = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new FortUpdater(this, clan, runCount), initial, Config.FS_BLOOD_OATH_FRQ * 60000); // Schedule owner tasks to start running
- }
- else
- setOwnerClan(null);
-
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: loadFortData(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /** Load All Functions */
- private void loadFunctions()
- {
- Connection con = null;
- try
- {
- PreparedStatement statement;
- ResultSet rs;
- con = L2DatabaseFactory.getInstance().getConnection();
- statement = con.prepareStatement("SELECT * FROM fort_functions WHERE fort_id = ?");
- statement.setInt(1, getFortId());
- rs = statement.executeQuery();
- while (rs.next())
- {
- _function.put(rs.getInt("type"), new FortFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
- }
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.SEVERE, "Exception: Fort.loadFunctions(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /** Remove function In List and in DB */
- public void removeFunction(int functionType)
- {
- _function.remove(functionType);
- Connection con = null;
- try
- {
- PreparedStatement statement;
- con = L2DatabaseFactory.getInstance().getConnection();
- statement = con.prepareStatement("DELETE FROM fort_functions WHERE fort_id=? AND type=?");
- statement.setInt(1, getFortId());
- statement.setInt(2, functionType);
- statement.execute();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.SEVERE, "Exception: Fort.removeFunctions(int functionType): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
- {
- if (player == null)
- return false;
- if (Config.DEBUG)
- _log.warning("Called Fort.updateFunctions(int type, int lvl, int lease, long rate, boolean addNew) Owner : " + getOwnerClan());
- if (lease > 0)
- if (!player.destroyItemByItemId("Consume", 57, lease, null, true))
- return false;
- if (addNew)
- {
- _function.put(type, new FortFunction(type, lvl, lease, 0, rate, 0, false));
- }
- else
- {
- if (lvl == 0 && lease == 0)
- removeFunction(type);
- else
- {
- int diffLease = lease - _function.get(type).getLease();
- if (Config.DEBUG)
- _log.warning("Called Fort.updateFunctions diffLease : " + diffLease);
- if (diffLease > 0)
- {
- _function.remove(type);
- _function.put(type, new FortFunction(type, lvl, lease, 0, rate, -1, false));
- }
- else
- {
- _function.get(type).setLease(lease);
- _function.get(type).setLvl(lvl);
- _function.get(type).dbSave();
- }
- }
- }
- return true;
- }
-
- public void activateInstance()
- {
- for (final L2DoorInstance door : _doors)
- {
- door.spawnMe(door.getX(), door.getY(), door.getZ());
- }
- }
-
- // This method loads fort door data from database
- private void loadDoor()
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_staticobjects WHERE fortId = ? AND objectType = ?");
- statement.setInt(1, getFortId());
- statement.setInt(2, 0);
- ResultSet rs = statement.executeQuery();
-
- while (rs.next())
- {
- // Create list of the door default for use when respawning dead doors
- _doorDefault.add(rs.getString("name") + ";" + rs.getInt("id") + ";" + rs.getInt("x") + ";" + rs.getInt("y") + ";"
- + rs.getInt("z") + ";" + rs.getInt("range_xmin") + ";" + rs.getInt("range_ymin") + ";" + rs.getInt("range_zmin")
- + ";" + rs.getInt("range_xmax") + ";" + rs.getInt("range_ymax") + ";" + rs.getInt("range_zmax") + ";"
- + rs.getInt("hp") + ";" + rs.getInt("pDef") + ";" + rs.getInt("mDef") + ";" + rs.getBoolean("openType") + ";"
- + rs.getBoolean("commanderDoor"));
- L2DoorInstance door;
- _doors.add(door = DoorTable.parseList(_doorDefault.get(_doorDefault.size() - 1), true));
- DoorTable.getInstance().putDoor(door);
- }
-
- rs.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: loadFortDoor(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- // This method loads fort flagpoles data from database
- private void loadFlagPoles()
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_staticobjects WHERE fortId = ? AND objectType = ?");
- statement.setInt(1, getFortId());
- statement.setInt(2, 1);
- ResultSet rs = statement.executeQuery();
- while (rs.next())
- {
- //Create list of the door default for use when respawning dead doors
- _flagPoleStats.add(rs.getString("name") + ";" + rs.getInt("id") + ";" + rs.getInt("x") + ";" + rs.getInt("y") + ";"
- + rs.getInt("z") + ";3;none;0;0");
- _flagPole = StaticObjects.parse(_flagPoleStats.get(_flagPoleStats.size() - 1));
- StaticObjects.getInstance().putObject(_flagPole);
- }
- rs.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: loadFlagPoles(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- // This method loads fort door upgrade data from database
- private void loadDoorUpgrade()
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_doorupgrade WHERE doorId IN (SELECT Id FROM fort_staticobjects WHERE fortId = ? AND objectType = ?)");
- statement.setInt(1, getFortId());
- statement.setInt(2, 0);
- ResultSet rs = statement.executeQuery();
-
- while (rs.next())
- {
- upgradeDoor(rs.getInt("id"), rs.getInt("hp"), rs.getInt("pDef"), rs.getInt("mDef"));
- }
- rs.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: loadFortDoorUpgrade(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- private void removeDoorUpgrade()
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("DELETE FROM fort_doorupgrade WHERE doorId IN (SELECT id FROM fort_staticobjects WHERE fortId=? AND objectType = ?)");
- statement.setInt(1, getFortId());
- statement.setInt(2, 0);
- statement.execute();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: removeDoorUpgrade(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- private void saveDoorUpgrade(int doorId, int hp, int pDef, int mDef)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("INSERT INTO fort_doorupgrade (doorId, hp, pDef, mDef) VALUES (?,?,?,?)");
- statement.setInt(1, doorId);
- statement.setInt(2, hp);
- statement.setInt(3, pDef);
- statement.setInt(4, mDef);
- statement.execute();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: saveDoorUpgrade(int doorId, int hp, int pDef, int mDef): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- private void updateOwnerInDB()
- {
- L2Clan clan = getOwnerClan();
- int clanId = 0;
- if (clan != null)
- {
- clanId = clan.getClanId();
- _lastOwnedTime.setTimeInMillis(System.currentTimeMillis());
- }
- else
- _lastOwnedTime.setTimeInMillis(0);
-
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
-
- statement = con.prepareStatement("UPDATE fort SET owner=?,lastOwnedTime=?,state=?,castleId=?,blood=? WHERE id = ?");
- statement.setInt(1, clanId);
- statement.setLong(2, _lastOwnedTime.getTimeInMillis());
- statement.setInt(3, 0);
- statement.setInt(4, 0);
- statement.setInt(5, getBloodOathReward());
- statement.setInt(6, getFortId());
- statement.execute();
- statement.close();
-
- // ============================================================================
- // Announce to clan memebers
- if (clan != null)
- {
- clan.setHasFort(getFortId()); // Set has fort flag for new owner
- SystemMessage sm;
- sm = new SystemMessage(SystemMessageId.S1_CLAN_IS_VICTORIOUS_IN_THE_FORTRESS_BATTLE_OF_S2);
- sm.addString(clan.getName());
- sm.addFortId(getFortId());
- Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
- for (L2PcInstance player : pls)
- {
- player.sendPacket(sm);
- }
- clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
- clan.broadcastToOnlineMembers(new PlaySound(1, "Siege_Victory", 0, 0, 0, 0, 0));
- if (_FortUpdater != null)
- _FortUpdater.cancel(false);
- _FortUpdater = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new FortUpdater(this, clan, 0), Config.FS_BLOOD_OATH_FRQ * 60000, Config.FS_BLOOD_OATH_FRQ * 60000); // Schedule owner tasks to start running
- }
- else
- {
- _FortUpdater.cancel(false);
- _FortUpdater = null;
- }
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- public final int getFortId()
- {
- return _fortId;
- }
-
- public final L2Clan getOwnerClan()
- {
- return _fortOwner;
- }
-
- public final void setOwnerClan(L2Clan clan)
- {
- setVisibleFlag(clan != null ? true : false);
- _fortOwner = clan;
- }
-
- public final L2DoorInstance getDoor(int doorId)
- {
- if (doorId <= 0)
- return null;
-
- for (L2DoorInstance door : getDoors())
- {
- if (door.getDoorId() == doorId)
- return door;
- }
- return null;
- }
-
- public final List<L2DoorInstance> getDoors()
- {
- return _doors;
- }
-
- public final L2StaticObjectInstance getFlagPole()
- {
- return _flagPole;
- }
-
- public final FortSiege getSiege()
- {
- if (_siege == null)
- _siege = new FortSiege(new Fort[] { this });
- return _siege;
- }
-
- public final Calendar getSiegeDate()
- {
- return _siegeDate;
- }
-
- public final void setSiegeDate(Calendar siegeDate)
- {
- _siegeDate = siegeDate;
- }
-
- public final int getOwnedTime()
- {
- if (_lastOwnedTime.getTimeInMillis() == 0)
- return 0;
-
- return (int) ((System.currentTimeMillis() - _lastOwnedTime.getTimeInMillis()) / 1000);
- }
-
- public final String getName()
- {
- return _name;
- }
-
- public void updateClansReputation(L2Clan owner, boolean removePoints)
- {
- if (owner != null)
- {
- if (removePoints)
- owner.takeReputationScore(Config.LOOSE_FORT_POINTS, true);
- else
- owner.addReputationScore(Config.TAKE_FORT_POINTS, true);
- }
- }
-
- private class endFortressSiege implements Runnable
- {
- private Fort _f;
- private L2Clan _clan;
-
- public endFortressSiege(Fort f, L2Clan clan)
- {
- _f = f;
- _clan = clan;
- }
-
- public void run()
- {
- try
- {
- _f.engrave(_clan);
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception in endFortressSiege " + e.getMessage(), e);
- }
- }
-
- }
-
- /**
- * @return Returns state of fortress.<BR><BR>
- * 0 - not decided yet<BR>
- * 1 - independent<BR>
- * 2 - contracted with castle<BR>
- */
- public final int getFortState()
- {
- return _state;
- }
-
- /**
- * @param State.<BR>
- * 0 - not decided yet<BR>
- * 1 - independent<BR>
- * 2 - contracted with castle<BR><BR>
- * @param CastleId.<BR>
- * set Castle Id for contracted fort
- */
- public final void setFortState(int state, int castleId)
- {
- _state = state;
- _castleId = castleId;
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
-
- statement = con.prepareStatement("UPDATE fort SET state=?,castleId=? WHERE id = ?");
- statement.setInt(1, getFortState());
- statement.setInt(2, getCastleId());
- statement.setInt(3, getFortId());
- statement.execute();
- statement.close();
-
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: setFortState(int state, int castleId): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * @return Returns Castle Id of fortress contracted with castle.
- */
- public final int getCastleId()
- {
- return _castleId;
- }
-
- /**
- * @return Returns fortress type.<BR><BR>
- * 0 - small (3 commanders) <BR>
- * 1 - big (4 commanders + control room)
- */
- public final int getFortType()
- {
- return _fortType;
- }
-
- public final int getCastleIdFromEnvoy(int npcId)
- {
- return _envoyCastles.get(npcId);
- }
-
- /**
- * @return Returns amount of barracks.
- */
- public final int getFortSize()
- {
- return getFortType() == 0 ? 3 : 5;
- }
-
- public void spawnSuspiciousMerchant()
- {
- if (_isSuspiciousMerchantSpawned)
- return;
- _isSuspiciousMerchantSpawned = true;
- for (L2Spawn spawnDat : _siegeNpcs)
- {
- spawnDat.doSpawn();
- spawnDat.startRespawn();
- }
- }
- public void despawnSuspiciousMerchant()
- {
- if (!_isSuspiciousMerchantSpawned)
- return;
- _isSuspiciousMerchantSpawned = false;
- for (L2Spawn spawnDat : _siegeNpcs)
- {
- spawnDat.stopRespawn();
- spawnDat.getLastSpawn().deleteMe();
- }
- }
- public void spawnNpcCommanders()
- {
- for (L2Spawn spawnDat : _npcCommanders)
- {
- spawnDat.doSpawn();
- spawnDat.startRespawn();
- }
- }
-
- public void despawnNpcCommanders()
- {
- for (L2Spawn spawnDat : _npcCommanders)
- {
- spawnDat.stopRespawn();
- spawnDat.getLastSpawn().deleteMe();
- }
- }
- public void spawnSpecialEnvoys()
- {
- for (L2Spawn spawnDat : _specialEnvoys)
- {
- spawnDat.doSpawn();
- spawnDat.startRespawn();
- }
- }
-
- public void despawnSpecialEnvoys()
- {
- for (L2Spawn spawnDat : _specialEnvoys)
- {
- spawnDat.stopRespawn();
- spawnDat.getLastSpawn().deleteMe();
- }
- }
-
- private void initNpcs()
- {
- Connection con = null;
-
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ");
- statement.setInt(1, getFortId());
- statement.setInt(2, 0);
- ResultSet rset = statement.executeQuery();
-
- L2Spawn spawnDat;
- L2NpcTemplate template;
-
- while (rset.next())
- {
- template = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
- if (template != null)
- {
- spawnDat = new L2Spawn(template);
- spawnDat.setAmount(1);
- spawnDat.setLocx(rset.getInt("x"));
- spawnDat.setLocy(rset.getInt("y"));
- spawnDat.setLocz(rset.getInt("z"));
- spawnDat.setHeading(rset.getInt("heading"));
- spawnDat.setRespawnDelay(60);
- SpawnTable.getInstance().addNewSpawn(spawnDat, false);
- spawnDat.doSpawn();
- spawnDat.startRespawn();
- }
- else
- {
- _log.warning("Fort " + getFortId() + " initNpcs: Data missing in NPC table for ID: " + rset.getInt("npcId") + ".");
- }
- }
-
- rset.close();
- statement.close();
- }
- catch (Exception e)
- {
- // problem with initializing spawn, go to next one
- _log.log(Level.WARNING, "Fort " + getFortId() + " initNpcs: Spawn could not be initialized: " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- private void initSiegeNpcs()
- {
- Connection con = null;
- _siegeNpcs.clear();
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT id, npcId, x, y, z, heading FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id");
- statement.setInt(1, getFortId());
- statement.setInt(2, 2);
- ResultSet rset = statement.executeQuery();
-
- L2Spawn spawnDat;
- L2NpcTemplate template;
- while (rset.next())
- {
- template = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
- if (template != null)
- {
- spawnDat = new L2Spawn(template);
- spawnDat.setAmount(1);
- spawnDat.setLocx(rset.getInt("x"));
- spawnDat.setLocy(rset.getInt("y"));
- spawnDat.setLocz(rset.getInt("z"));
- spawnDat.setHeading(rset.getInt("heading"));
- spawnDat.setRespawnDelay(60);
- _siegeNpcs.add(spawnDat);
- }
- else
- {
- _log.warning("Fort " + getFortId() + " initSiegeNpcs: Data missing in NPC table for ID: " + rset.getInt("npcId") + ".");
- }
- }
- rset.close();
- statement.close();
- }
- catch (Exception e)
- {
- // problem with initializing spawn, go to next one
- _log.log(Level.WARNING, "Fort " + getFortId() + " initSiegeNpcs: Spawn could not be initialized: " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- private void initNpcCommanders()
- {
- Connection con = null;
- _npcCommanders.clear();
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT id, npcId, x, y, z, heading FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id");
- statement.setInt(1, getFortId());
- statement.setInt(2, 1);
- ResultSet rset = statement.executeQuery();
-
- L2Spawn spawnDat;
- L2NpcTemplate template;
- while (rset.next())
- {
- template = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
- if (template != null)
- {
- spawnDat = new L2Spawn(template);
- spawnDat.setAmount(1);
- spawnDat.setLocx(rset.getInt("x"));
- spawnDat.setLocy(rset.getInt("y"));
- spawnDat.setLocz(rset.getInt("z"));
- spawnDat.setHeading(rset.getInt("heading"));
- spawnDat.setRespawnDelay(60);
- _npcCommanders.add(spawnDat);
- }
- else
- {
- _log.warning("Fort " + getFortId() + " initNpcCommanders: Data missing in NPC table for ID: " + rset.getInt("npcId")
- + ".");
- }
- }
- rset.close();
- statement.close();
- }
- catch (Exception e)
- {
- // problem with initializing spawn, go to next one
- _log.log(Level.WARNING, "Fort " + getFortId() + " initNpcCommanders: Spawn could not be initialized: " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- private void initSpecialEnvoys()
- {
- Connection con = null;
- _specialEnvoys.clear();
- _envoyCastles.clear();
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT id, npcId, x, y, z, heading, castleId FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id");
- statement.setInt(1, getFortId());
- statement.setInt(2, 3);
- ResultSet rset = statement.executeQuery();
-
- L2Spawn spawnDat;
- L2NpcTemplate template;
- while (rset.next())
- {
- int castleId = rset.getInt("castleId");
- int npcId = rset.getInt("npcId");
- template = NpcTable.getInstance().getTemplate(npcId);
- if (template != null)
- {
- spawnDat = new L2Spawn(template);
- spawnDat.setAmount(1);
- spawnDat.setLocx(rset.getInt("x"));
- spawnDat.setLocy(rset.getInt("y"));
- spawnDat.setLocz(rset.getInt("z"));
- spawnDat.setHeading(rset.getInt("heading"));
- spawnDat.setRespawnDelay(60);
- _specialEnvoys.add(spawnDat);
- _envoyCastles.put(npcId, castleId);
- }
- else
- {
- _log.warning("Fort " + getFortId() + " initSpecialEnvoys: Data missing in NPC table for ID: " + rset.getInt("npcId")
- + ".");
- }
- }
- rset.close();
- statement.close();
- }
- catch (Exception e)
- {
- // problem with initializing spawn, go to next one
- _log.log(Level.WARNING, "Fort " + getFortId() + " initSpecialEnvoys: Spawn could not be initialized: " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- public FastList<L2Skill> getResidentialSkills()
- {
- return _residentialSkills;
- }
-
- public void giveResidentialSkills(L2PcInstance player)
- {
- if (_residentialSkills != null && !_residentialSkills.isEmpty())
- {
- for (L2Skill sk : _residentialSkills)
- player.addSkill(sk, false);
- }
- }
-
- public void removeResidentialSkills(L2PcInstance player)
- {
- if (_residentialSkills != null && !_residentialSkills.isEmpty())
- {
- for (L2Skill sk : _residentialSkills)
- player.removeSkill(sk, false, true);
- }
- }
- }
|