Selaa lähdekoodia

BETA: Fortress Ambassador rework.

Patch by: jurchiks
Reviewed by: UnAfraid, Zoey76
Zoey76 12 vuotta sitten
vanhempi
sitoutus
0ce9d20139

+ 1 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/FortUpdater.java

@@ -22,7 +22,6 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.l2jserver.Config;
-import com.l2jserver.gameserver.instancemanager.CastleManager;
 import com.l2jserver.gameserver.model.L2Clan;
 import com.l2jserver.gameserver.model.entity.Fort;
 import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
@@ -74,7 +73,7 @@ public class FortUpdater implements Runnable
 						if (_clan.getWarehouse().getAdena() >= Config.FS_FEE_FOR_CASTLE)
 						{
 							_clan.getWarehouse().destroyItemByItemId("FS_fee_for_Castle", PcInventory.ADENA_ID, Config.FS_FEE_FOR_CASTLE, null, null);
-							CastleManager.getInstance().getCastleById(_fort.getCastleId()).addToTreasuryNoTax(Config.FS_FEE_FOR_CASTLE);
+							_fort.getContractedCastle().addToTreasuryNoTax(Config.FS_FEE_FOR_CASTLE);
 							_fort.raiseSupplyLvL();
 						}
 						else

+ 37 - 45
L2J_Server_BETA/java/com/l2jserver/gameserver/model/actor/instance/L2FortEnvoyInstance.java

@@ -18,13 +18,13 @@
  */
 package com.l2jserver.gameserver.model.actor.instance;
 
-import java.util.StringTokenizer;
-
-import com.l2jserver.gameserver.instancemanager.CastleManager;
 import com.l2jserver.gameserver.model.actor.L2Npc;
 import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
+import com.l2jserver.gameserver.model.entity.Castle;
+import com.l2jserver.gameserver.model.entity.Fort;
 import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
 import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
+import com.l2jserver.gameserver.util.Util;
 
 public class L2FortEnvoyInstance extends L2Npc
 {
@@ -37,71 +37,63 @@ public class L2FortEnvoyInstance extends L2Npc
 	@Override
 	public void showChatWindow(L2PcInstance player)
 	{
-		player.sendPacket(ActionFailed.STATIC_PACKET);
-		
-		String filename;
-		
-		if (!player.isClanLeader() || (getFort().getFortId() != player.getClan().getFortId()))
+		String filePath;
+		final Fort fortress = getFort();
+		if (!player.isClanLeader() || (fortress.getFortId() != player.getClan().getFortId()))
 		{
-			filename = "data/html/fortress/envoy-noclan.htm";
+			filePath = "data/html/fortress/ambassador-not-leader.htm";
 		}
-		else if (getFort().getFortState() == 0)
+		else if (fortress.getFortState() == 1)
 		{
-			filename = "data/html/fortress/envoy.htm";
+			filePath = "data/html/fortress/ambassador-rejected.htm";
+		}
+		else if (fortress.getFortState() == 2)
+		{
+			filePath = "data/html/fortress/ambassador-signed.htm";
+		}
+		else if (fortress.isBorderFortress())
+		{
+			// border fortresses may only declare independence
+			filePath = "data/html/fortress/ambassador-border.htm";
 		}
 		else
 		{
-			filename = "data/html/fortress/envoy-no.htm";
+			// normal fortresses can swear fealty or declare independence
+			filePath = "data/html/fortress/ambassador.htm";
 		}
 		
+		player.sendPacket(ActionFailed.STATIC_PACKET);
 		NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
-		html.setFile(player.getHtmlPrefix(), filename);
+		html.setFile(player.getHtmlPrefix(), filePath);
 		html.replace("%objectId%", String.valueOf(getObjectId()));
-		html.replace("%castleName%", String.valueOf(CastleManager.getInstance().getCastleById(getFort().getCastleIdFromEnvoy(getNpcId())).getName()));
+		html.replace("%castleName%", String.valueOf(fortress.getCastleByAmbassador(getNpcId()).getName()));
 		player.sendPacket(html);
 	}
 	
 	@Override
 	public void onBypassFeedback(L2PcInstance player, String command)
 	{
-		StringTokenizer st = new StringTokenizer(command, " ");
-		String actualCommand = st.nextToken(); // Get actual command
-		
-		String par = "";
-		if (st.countTokens() >= 1)
+		if (command.startsWith("select "))
 		{
-			par = st.nextToken();
-		}
-		
-		if (actualCommand.equalsIgnoreCase("select"))
-		{
-			int val = 0;
-			try
-			{
-				val = Integer.parseInt(par);
-			}
-			catch (IndexOutOfBoundsException ioobe)
+			String param = command.substring(7);
+			Fort fortress = getFort();
+			Castle castle = fortress.getCastleByAmbassador(getNpcId());
+			String filePath;
+			
+			if (castle.getOwnerId() == 0)
 			{
+				filePath = "data/html/fortress/ambassador-not-owned.htm";
 			}
-			catch (NumberFormatException nfe)
+			else
 			{
+				int choice = Util.isDigit(param) ? Integer.parseInt(param) : 0;
+				fortress.setFortState(choice, castle.getCastleId());
+				filePath = (choice == 1) ? "data/html/fortress/ambassador-independent.htm" : "data/html/fortress/ambassador-signed.htm";
 			}
 			
-			int castleId = 0;
 			NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
-			if (val == 2)
-			{
-				castleId = getFort().getCastleIdFromEnvoy(getNpcId());
-				if (CastleManager.getInstance().getCastleById(castleId).getOwnerId() < 1)
-				{
-					html.setHtml("<html><body>Contact is currently not possible, " + CastleManager.getInstance().getCastleById(castleId).getName() + " Castle isn't currently owned by clan.</body></html>");
-					player.sendPacket(html);
-					return;
-				}
-			}
-			getFort().setFortState(val, castleId);
-			html.setFile(player.getHtmlPrefix(), "data/html/fortress/envoy-ok.htm");
-			html.replace("%castleName%", String.valueOf(CastleManager.getInstance().getCastleById(getFort().getCastleIdFromEnvoy(getNpcId())).getName()));
+			html.setFile(player.getHtmlPrefix(), filePath);
+			html.replace("%castleName%", castle.getName());
 			player.sendPacket(html);
 		}
 		else

+ 1 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/model/actor/instance/L2FortManagerInstance.java

@@ -25,7 +25,6 @@ import com.l2jserver.Config;
 import com.l2jserver.gameserver.cache.HtmCache;
 import com.l2jserver.gameserver.datatables.SkillTable;
 import com.l2jserver.gameserver.datatables.TeleportLocationTable;
-import com.l2jserver.gameserver.instancemanager.CastleManager;
 import com.l2jserver.gameserver.model.L2Clan;
 import com.l2jserver.gameserver.model.L2TeleportLocation;
 import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
@@ -177,7 +176,7 @@ public class L2FortManagerInstance extends L2MerchantInstance
 					}
 					hour = (int) Math.floor(getFort().getTimeTillNextFortUpdate() / 3600);
 					minutes = (int) (Math.floor(getFort().getTimeTillNextFortUpdate() - (hour * 3600)) / 60);
-					html.replace("%castle%", CastleManager.getInstance().getCastleById(getFort().getCastleId()).getName());
+					html.replace("%castle%", getFort().getContractedCastle().getName());
 					html.replace("%hr2%", String.valueOf(hour));
 					html.replace("%min2%", String.valueOf(minutes));
 					player.sendPacket(html);

+ 47 - 14
L2J_Server_BETA/java/com/l2jserver/gameserver/model/entity/Fort.java

@@ -23,7 +23,9 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
@@ -44,6 +46,7 @@ import com.l2jserver.gameserver.datatables.SkillTable;
 import com.l2jserver.gameserver.datatables.SkillTreesData;
 import com.l2jserver.gameserver.datatables.SpawnTable;
 import com.l2jserver.gameserver.datatables.StaticObjects;
+import com.l2jserver.gameserver.instancemanager.CastleManager;
 import com.l2jserver.gameserver.instancemanager.FortManager;
 import com.l2jserver.gameserver.instancemanager.ZoneManager;
 import com.l2jserver.gameserver.model.L2Clan;
@@ -96,6 +99,7 @@ public class Fort
 	private final FastList<L2Spawn> _specialEnvoys = new FastList<>();
 	
 	private final TIntIntHashMap _envoyCastles = new TIntIntHashMap(2);
+	private final Set<Integer> _availableCastles = new HashSet<>(1);
 	
 	/** Fortress Functions */
 	public static final int FUNC_TELEPORT = 1;
@@ -1085,7 +1089,7 @@ public class Fort
 	 *            <li>1 - independent</li>
 	 *            <li>2 - contracted with castle</li>
 	 *            </ul>
-	 * @param castleId set Castle Id for contracted fort
+	 * @param castleId the Id of the contracted castle (0 if no contract with any castle)
 	 */
 	public final void setFortState(int state, int castleId)
 	{
@@ -1095,7 +1099,7 @@ public class Fort
 			PreparedStatement ps = con.prepareStatement("UPDATE fort SET state=?,castleId=? WHERE id = ?"))
 		{
 			ps.setInt(1, getFortState());
-			ps.setInt(2, getCastleId());
+			ps.setInt(2, getContractedCastleId());
 			ps.setInt(3, getFortId());
 			ps.execute();
 		}
@@ -1106,31 +1110,58 @@ public class Fort
 	}
 	
 	/**
-	 * @return Returns Castle Id of fortress contracted with castle.
+	 * @return the fortress type (0 - small (3 commanders), 1 - big (4 commanders + control room))
 	 */
-	public final int getCastleId()
+	public final int getFortType()
+	{
+		return _fortType;
+	}
+	
+	/**
+	 * @param npcId the Id of the ambassador NPC
+	 * @return the Id of the castle this ambassador represents
+	 */
+	public final int getCastleIdByAmbassador(int npcId)
+	{
+		return _envoyCastles.get(npcId);
+	}
+	
+	/**
+	 * @param npcId the Id of the ambassador NPC
+	 * @return the castle this ambassador represents
+	 */
+	public final Castle getCastleByAmbassador(int npcId)
+	{
+		return CastleManager.getInstance().getCastleById(getCastleIdByAmbassador(npcId));
+	}
+	
+	/**
+	 * @return the Id of the castle contracted with this fortress
+	 */
+	public final int getContractedCastleId()
 	{
 		return _castleId;
 	}
 	
 	/**
-	 * @return Returns fortress type.<BR>
-	 * <BR>
-	 *         0 - small (3 commanders) <BR>
-	 *         1 - big (4 commanders + control room)
+	 * @return the castle contracted with this fortress ({@code null} if no contract with any castle)
 	 */
-	public final int getFortType()
+	public final Castle getContractedCastle()
 	{
-		return _fortType;
+		return CastleManager.getInstance().getCastleById(getContractedCastleId());
 	}
 	
-	public final int getCastleIdFromEnvoy(int npcId)
+	/**
+	 * Check if this is a border fortress (associated with multiple castles).
+	 * @return {@code true} if this is a border fortress (associated with more than one castle), {@code false} otherwise
+	 */
+	public final boolean isBorderFortress()
 	{
-		return _envoyCastles.get(npcId);
+		return _availableCastles.size() > 1;
 	}
 	
 	/**
-	 * @return Returns amount of barracks.
+	 * @return the amount of barracks in this fortress
 	 */
 	public final int getFortSize()
 	{
@@ -1206,7 +1237,7 @@ public class Fort
 	private void initNpcs()
 	{
 		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
-			PreparedStatement ps = con.prepareStatement("SELECT * FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? "))
+			PreparedStatement ps = con.prepareStatement("SELECT * FROM fort_spawnlist WHERE fortId = ? AND spawnType = ?"))
 		{
 			ps.setInt(1, getFortId());
 			ps.setInt(2, 0);
@@ -1326,6 +1357,7 @@ public class Fort
 	{
 		_specialEnvoys.clear();
 		_envoyCastles.clear();
+		_availableCastles.clear();
 		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
 			PreparedStatement ps = con.prepareStatement("SELECT id, npcId, x, y, z, heading, castleId FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id"))
 		{
@@ -1351,6 +1383,7 @@ public class Fort
 						spawnDat.setRespawnDelay(60);
 						_specialEnvoys.add(spawnDat);
 						_envoyCastles.put(npcId, castleId);
+						_availableCastles.add(castleId);
 					}
 					else
 					{

+ 1 - 1
L2J_Server_BETA/java/com/l2jserver/gameserver/model/entity/FortSiege.java

@@ -966,7 +966,7 @@ public class FortSiege implements Siegable
 			b = false;
 			player.sendPacket(SystemMessageId.CLAN_THAT_OWNS_CASTLE_IS_AUTOMATICALLY_REGISTERED_DEFENDING);
 		}
-		else if ((getFort().getOwnerClan() != null) && (player.getClan().getCastleId() > 0) && (player.getClan().getCastleId() == getFort().getCastleId()))
+		else if ((getFort().getOwnerClan() != null) && (player.getClan().getCastleId() > 0) && (player.getClan().getCastleId() == getFort().getContractedCastleId()))
 		{
 			b = false;
 			player.sendPacket(SystemMessageId.CANT_REGISTER_TO_SIEGE_DUE_TO_CONTRACT);