import java.util.HashMap;
import java.util.Map;
// Assume this class exists in your original code
// Provides a way to check if an NPC is alive
class NpcStatusService {
// This is a placeholder. Replace with your actual implementation.
// For demonstration, let's simulate some states.
private static Map<Integer, Boolean> npcAliveStates = new HashMap<>();
// Method to set the alive status for testing
public static void setAliveStatus(int npcId, boolean isAlive) {
npcAliveStates.put(npcId, isAlive);
}
// Your actual method to check if an NPC is alive
public static boolean isAlive(int npcId) {
// Replace this with your actual logic to check NPC status
// For simulation:
return npcAliveStates.getOrDefault(npcId, true); // Assume alive by default if not set
}
}
// Represents an NPC
class Npc {
private int id;
private int factionId; // Only relevant for existing NPCs tied to a faction
public Npc(int id, int factionId) {
this.id = id;
this.factionId = factionId;
}
public int getId() {
return id;
}
public int getFactionId() {
return factionId;
}
}
// Manages NPCs and their relationships
class NpcManager {
// Map of existing NPC IDs to their Npc objects
private Map<Integer, Npc> existingNpcsById = new HashMap<>();
/*
* Map of New NPC IDs to their ties.
* The inner map links Faction ID to the corresponding existing NPC ID
* for that faction.
* Example: newNpcTies.get(401) -> { 1 -> 101, 2 -> 201, 3 -> 301 }
*/
private Map<Integer, Map<Integer, Integer>> newNpcTies = new HashMap<>();
public NpcManager() {
// Constructor, initializes maps
}
// Method to add an existing NPC
public void addExistingNpc(int id, int factionId) {
if (existingNpcsById.containsKey(id)) {
// Handle duplicate ID if necessary
return;
}
existingNpcsById.put(id, new Npc(id, factionId));
}
/*
* Method to add a new NPC and define its ties to existing NPCs
* across different factions.
* tiedExistingNpcsByFaction: map where key is Faction ID, value is the existing NPC ID
* from that faction this new NPC is tied to.
*/
public void addNewNpcTie(int newNpcId, Map<Integer, Integer> tiedExistingNpcsByFaction) {
if (newNpcTies.containsKey(newNpcId)) {
// Handle duplicate ID if necessary
return;
}
newNpcTies.put(newNpcId, new HashMap<>(tiedExistingNpcsByFaction));
}
/*
* Determines if a player's attack on a new NPC should be stopped.
* Attack is stopped if the corresponding existing NPC
* for the player's faction is currently alive.
*/
public boolean shouldStopAttack(int playerFactionId, int attackedNewNpcId) {
// 1. Check if the attacked NPC is a 'new NPC' we track ties for
Map<Integer, Integer> tiedNpcs = newNpcTies.get(attackedNewNpcId);
if (tiedNpcs == null) {
// Attacked NPC is not one of the 'new NPCs' with special tie logic
// Do not stop the attack based on this logic
return false;
}
// 2. Find the existing NPC tied to the attacked new NPC for the player's faction
Integer correspondingExistingNpcId = tiedNpcs.get(playerFactionId);
if (correspondingExistingNpcId == null) {
// This 'new NPC' is not tied to an existing NPC in the player's faction
// Do not stop the attack based on this logic
return false;
}
// 3. Check if the corresponding existing NPC is alive
// We assume NpcStatusService.isAlive(npcId) is available
boolean isCorrespondingNpcAlive = NpcStatusService.isAlive(correspondingExistingNpcId);
// Stop the attack if the corresponding NPC is alive
return isCorrespondingNpcAlive;
}
}
// Example Usage
class Main {
public static void main(String[] args) {
NpcManager npcManager = new NpcManager();
// --- Setup Existing NPCs ---
// Add existing NPCs for faction 1
npcManager.addExistingNpc(101, 1);
npcManager.addExistingNpc(102, 1);
npcManager.addExistingNpc(103, 1);
// Add existing NPCs for faction 2
npcManager.addExistingNpc(201, 2);
npcManager.addExistingNpc(202, 2);
npcManager.addExistingNpc(203, 2);
// Add existing NPCs for faction 3
npcManager.addExistingNpc(301, 3);
npcManager.addExistingNpc(302, 3);
npcManager.addExistingNpc(303, 3);
// --- Setup New NPC Ties ---
// Define ties for new NPC 401, 402, 403
Map<Integer, Integer> ties401_403 = new HashMap<>();
ties401_403.put(1, 101); // Faction 1 maps to existing NPC 101
ties401_403.put(2, 201); // Faction 2 maps to existing NPC 201
ties401_403.put(3, 301); // Faction 3 maps to existing NPC 301
npcManager.addNewNpcTie(401, ties401_403);
npcManager.addNewNpcTie(402, ties401_403); // 402 and 403 share the same ties as 401
npcManager.addNewNpcTie(403, ties401_403);
// Define ties for new NPC 404, 405, 406
Map<Integer, Integer> ties404_406 = new HashMap<>();
ties404_406.put(1, 102); // Faction 1 maps to existing NPC 102
ties404_406.put(2, 202); // Faction 2 maps to existing NPC 202
ties404_406.put(3, 302); // Faction 3 maps to existing NPC 302
npcManager.addNewNpcTie(404, ties404_406);
npcManager.addNewNpcTie(405, ties404_406); // 405 and 406 share the same ties as 404
npcManager.addNewNpcTie(406, ties404_406);
// --- Simulate NPC Alive Statuses (using the placeholder service) ---
NpcStatusService.setAliveStatus(101, true); // 101 is alive
NpcStatusService.setAliveStatus(201, false); // 201 is dead
NpcStatusService.setAliveStatus(301, false); // 301 is dead
NpcStatusService.setAliveStatus(102, false); // 102 is dead
NpcStatusService.setAliveStatus(202, true); // 202 is alive
NpcStatusService.setAliveStatus(302, false); // 302 is dead
// --- Test Cases ---
System.out.println("--- Testing Attacks on New NPCs tied to (101, 201, 301) like 401 ---");
// Player Faction 1 attacks New NPC 401 (tied to 101, 201, 301)
// Corresponding existing NPC for Faction 1 is 101. 101 is ALIVE.
boolean stopAttack1 = npcManager.shouldStopAttack(1, 401);
System.out.println("Player Faction 1 attacking 401: Stop attack? " + stopAttack1 + " (Expected: true, because 101 is alive)"); // Expected: true
// Player Faction 2 attacks New NPC 401 (tied to 101, 201, 301)
// Corresponding existing NPC for Faction 2 is 201. 201 is DEAD.
boolean stopAttack2 = npcManager.shouldStopAttack(2, 401);
System.out.println("Player Faction 2 attacking 401: Stop attack? " + stopAttack2 + " (Expected: false, because 201 is dead)"); // Expected: false
// Player Faction 3 attacks New NPC 401 (tied to 101, 201, 301)
// Corresponding existing NPC for Faction 3 is 301. 301 is DEAD.
boolean stopAttack3 = npcManager.shouldStopAttack(3, 401);
System.out.println("Player Faction 3 attacking 401: Stop attack? " + stopAttack3 + " (Expected: false, because 301 is dead)"); // Expected: false
System.out.println("\n--- Testing Attacks on New NPCs tied to (102, 202, 302) like 404 ---");
// Player Faction 1 attacks New NPC 404 (tied to 102, 202, 302)
// Corresponding existing NPC for Faction 1 is 102. 102 is DEAD.
boolean stopAttack4 = npcManager.shouldStopAttack(1, 404);
System.out.println("Player Faction 1 attacking 404: Stop attack? " + stopAttack4 + " (Expected: false, because 102 is dead)"); // Expected: false
// Player Faction 2 attacks New NPC 404 (tied to 102, 202, 302)
// Corresponding existing NPC for Faction 2 is 202. 202 is ALIVE.
boolean stopAttack5 = npcManager.shouldStopAttack(2, 404);
System.out.println("Player Faction 2 attacking 404: Stop attack? " + stopAttack5 + " (Expected: true, because 202 is alive)"); // Expected: true
// Player Faction 3 attacks New NPC 404 (tied to 102, 202, 302)
// Corresponding existing NPC for Faction 3 is 302. 302 is DEAD.
boolean stopAttack6 = npcManager.shouldStopAttack(3, 404);
System.out.println("Player Faction 3 attacking 404: Stop attack? " + stopAttack6 + " (Expected: false, because 302 is dead)"); // Expected: false
System.out.println("\n--- Testing Attack on NPC without specific tie logic (e.g., Existing NPC) ---");
// Attacking an existing NPC like 101 would not trigger the 'new NPC' tie logic
// The shouldStopAttack method for 101 would return false based on its current implementation
boolean stopAttack7 = npcManager.shouldStopAttack(1, 101); // 101 is an existing NPC, not in newNpcTies map
System.out.println("Player Faction 1 attacking 101 (Existing NPC): Stop attack? " + stopAttack7 + " (Expected: false, as 101 is not a 'new NPC' with tie logic)"); // Expected: false
}
}