import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class NpcAttackHandler {
// Maps a 'new NPC' ID to a map of Faction ID -> Tied Faction NPC ID
private final Map<Integer, Map<Integer, Integer>> newNpcTies = new HashMap<>();
// Tracks which NPCs are currently alive by their ID
private final Set<Integer> aliveNpcs = new HashSet<>();
// Maps Player ID to their Faction ID
private final Map<Integer, Integer> playerFactions = new HashMap<>();
// --- Example Initialization (replace with your actual loading logic) ---
public NpcAttackHandler() {
// Define ties for new NPCs
newNpcTies.put(401, new HashMap<>());
newNpcTies.get(401).put(1, 101); // New NPC 401 tied to Faction 1's NPC 101
newNpcTies.get(401).put(2, 201); // New NPC 401 tied to Faction 2's NPC 201
newNpcTies.get(401).put(3, 301); // New NPC 401 tied to Faction 3's NPC 301
newNpcTies.put(402, new HashMap<>());
newNpcTies.get(402).put(1, 102); // New NPC 402 tied to Faction 1's NPC 102
newNpcTies.get(402).put(2, 202); // New NPC 402 tied to Faction 2's NPC 202
newNpcTies.get(402).put(3, 302); // New NPC 402 tied to Faction 3's NPC 302
// ... add more new NPC ties here
// Simulate some NPCs being alive
aliveNpcs.add(101); // Faction 1 NPC 101 is alive
// aliveNpcs.add(201); // Faction 2 NPC 201 is NOT alive in this example
aliveNpcs.add(301); // Faction 3 NPC 301 is alive
aliveNpcs.add(102); // Faction 1 NPC 102 is alive
aliveNpcs.add(202); // Faction 2 NPC 202 is alive
// aliveNpcs.add(302); // Faction 3 NPC 302 is NOT alive in this example
// Simulate the new NPCs being alive
aliveNpcs.add(401);
aliveNpcs.add(402);
// ... add other faction/normal NPCs that are alive
// Simulate player factions
playerFactions.put(1001, 1); // Player 1001 belongs to Faction 1
playerFactions.put(1002, 2); // Player 1002 belongs to Faction 2
playerFactions.put(1003, 3); // Player 1003 belongs to Faction 3
playerFactions.put(1004, 1); // Another player in Faction 1
}
// -----------------------------------------------------------------------
/**
* Handles a player's attempt to attack an NPC.
*
* @param playerId The ID of the attacking player.
* @param targetNpcId The ID of the target NPC.
* @return true if the attack is allowed to proceed, false if it is stopped.
*/
public boolean canPlayerAttackNpc(int playerId, int targetNpcId) {
// 1. Check if the target is one of the 'new' tied NPCs
if (newNpcTies.containsKey(targetNpcId)) {
// 2. Get the player's faction
Integer playerFactionId = playerFactions.get(playerId);
// Ensure the player has a faction assigned
if (playerFactionId == null) {
// Player has no faction, maybe they can attack anything?
// Or maybe they shouldn't exist? Decide based on game rules.
// For this logic, we assume a faction is required for the restriction.
// If no faction, the restriction doesn't apply based on its definition.
return true; // Allow attack if player has no registered faction
}
// 3. Get the map of tied NPCs for this specific 'new NPC'
Map<Integer, Integer> tiedNpcsForTarget = newNpcTies.get(targetNpcId);
// 4. Find the specific NPC tied to the player's faction for this 'new NPC' group
if (tiedNpcsForTarget.containsKey(playerFactionId)) {
Integer correspondingFactionNpcId = tiedNpcsForTarget.get(playerFactionId);
// 5. Check if this corresponding faction NPC is currently alive
if (aliveNpcs.contains(correspondingFactionNpcId)) {
// YES! The specific NPC tied to *this player's faction* is alive.
// Stop the attack.
return false; // Attack is blocked
} else {
// NO, the specific NPC tied to this player's faction is NOT alive.
// The restriction is lifted for this player. Allow the attack.
return true; // Attack is allowed
}
} else {
// The target New NPC is defined, but this player's faction is NOT
// listed as having a tied NPC for this group.
// Based on your example (401 tied to 101, 201, 301 covering factions 1,2,3),
// this case might mean this restriction doesn't apply to this faction
// for this specific 'new NPC' group. Allow the attack.
return true; // Attack is allowed (restriction doesn't apply to this faction/npc pairing)
}
} else {
// The target NPC is NOT one of the special 'new' tied NPCs.
// This specific restriction logic doesn't apply. Allow the attack to proceed
// to other normal attack checks (e.g., is target alive? is it friendly?).
return true; // Attack is allowed (not a restricted NPC)
}
}
// --- Helper methods to update NPC status ---
public void setNpcAlive(int npcId) {
aliveNpcs.add(npcId);
}
public void setNpcDead(int npcId) {
aliveNpcs.remove(npcId);
}
// --- Example Usage ---
public static void main(String[] args) {
NpcAttackHandler handler = new NpcAttackHandler();
int player1001 = 1001; // Faction 1
int player1002 = 1002; // Faction 2
int player1003 = 1003; // Faction 3
int npc401 = 401;
int npc402 = 402;
int npc101 = 101; // Faction 1 tied to 401
int npc201 = 201; // Faction 2 tied to 401
int npc301 = 301; // Faction 3 tied to 401
System.out.println("--- Initial State ---");
System.out.println("NPC 101 alive: " + handler.aliveNpcs.contains(101)); // Should be true
System.out.println("NPC 201 alive: " + handler.aliveNpcs.contains(201)); // Should be false
System.out.println("NPC 301 alive: " + handler.aliveNpcs.contains(301)); // Should be true
System.out.println();
System.out.println("--- Player 1001 (Faction 1) attacks NPC 401 ---");
// Player 1001 (F1) attacks 401. Tied NPC for F1 is 101. Is 101 alive? Yes. -> Block
boolean allowed1 = handler.canPlayerAttackNpc(player1001, npc401);
System.out.println("Attack allowed: " + allowed1); // Expected: false
System.out.println("\n--- Player 1002 (Faction 2) attacks NPC 401 ---");
// Player 1002 (F2) attacks 401. Tied NPC for F2 is 201. Is 201 alive? No. -> Allow
boolean allowed2 = handler.canPlayerAttackNpc(player1002, npc401);
System.out.println("Attack allowed: " + allowed2); // Expected: true
System.out.println("\n--- Player 1003 (Faction 3) attacks NPC 401 ---");
// Player 1003 (F3) attacks 401. Tied NPC for F3 is 301. Is 301 alive? Yes. -> Block
boolean allowed3 = handler.canPlayerAttackNpc(player1003, npc401);
System.out.println("Attack allowed: " + allowed3); // Expected: false
System.out.println("\n--- Player 1001 (Faction 1) attacks NPC 101 (Normal NPC) ---");
// Player 1001 attacks 101. 101 is NOT a 'new' tied NPC. -> Allow (subject to other game rules)
boolean allowed4 = handler.canPlayerAttackNpc(player1001, npc101);
System.out.println("Attack allowed: " + allowed4); // Expected: true
System.out.println("\n--- Scenario Change: Faction 1 NPC 101 Dies ---");
handler.setNpcDead(npc101);
System.out.println("NPC 101 alive: " + handler.aliveNpcs.contains(101)); // Should be false
System.out.println("\n--- Player 1001 (Faction 1) attacks NPC 401 (After 101 died) ---");
// Player 1001 (F1) attacks 401. Tied NPC for F1 is 101. Is 101 alive? No. -> Allow
boolean allowed5 = handler.canPlayerAttackNpc(player1001, npc401);
System.out.println("Attack allowed: " + allowed5); // Expected: true
}
}