Okay, here are two ways to achieve the desired attack restriction logic in Java.
Assumptions:
- You have a way to get the attacking player's faction ID (e.g.,
int playerFactionId).
- You have a way to get the ID of the NPC being attacked (e.g.,
int targetNpcId).
- You have a function
boolean isAlive(int npcId) that returns true if the NPC with the given ID is alive, false otherwise.
Setup (Common for both solutions)
We need data structures to represent the links and NPC factions.
import java.util.*;
// --- Common Setup Data ---
// Represents the link: New NPC ID -> List of linked Existing NPC IDs (one per faction)
Map<Integer, List<Integer>> newNpcLinks = new HashMap<>();
// Represents which faction an *existing* NPC belongs to: Existing NPC ID -> Faction ID
Map<Integer, Integer> existingNpcFactions = new HashMap<>();
// --- Dummy isAlive function (replace with your actual implementation) ---
// For demonstration, let's assume NPC 101 is alive, and thus 201 and 301 are not.
// And NPC 202 is alive, and thus 102 and 302 are not.
Set<Integer> aliveNpcs = new HashSet<>(Arrays.asList(101, 202)); // Example alive NPCs
boolean isAlive(int npcId) {
// Replace this with your actual check
return aliveNpcs.contains(npcId);
}
// --- Populate the data structures with example data ---
void setupData() {
// Faction IDs
int Faction1 = 1;
int Faction2 = 2;
int Faction3 = 3;
// Existing NPCs and their factions
existingNpcFactions.put(101, Faction1);
existingNpcFactions.put(102, Faction1);
existingNpcFactions.put(103, Faction1);
existingNpcFactions.put(201, Faction2);
existingNpcFactions.put(202, Faction2);
existingNpcFactions.put(203, Faction2);
existingNpcFactions.put(301, Faction3);
existingNpcFactions.put(302, Faction3);
existingNpcFactions.put(303, Faction3);
// New NPCs linked to groups of existing NPCs
newNpcLinks.put(401, Arrays.asList(101, 201, 301));
newNpcLinks.put(402, Arrays.asList(101, 201, 301)); // Linked to the same group
newNpcLinks.put(403, Arrays.asList(101, 201, 301)); // Linked to the same group
newNpcLinks.put(404, Arrays.asList(102, 202, 302));
newNpcLinks.put(405, Arrays.asList(102, 202, 302));
newNpcLinks.put(406, Arrays.asList(102, 202, 302));
// Add more links as needed
}
Solution #1: Best Approach (Using a dedicated class/manager)
This approach encapsulates the logic and data, making it more organized and maintainable.
import java.util.*;
// Assumes common setup code from above is available (Maps, isAlive, setupData)
class AttackRuleManager {
private final Map<Integer, List<Integer>> links;
private final Map<Integer, Integer> npcFactions;
private final Set<Integer> currentAliveNpcs; // Or access to a live isAlive source
// Constructor takes the necessary data structures
public AttackRuleManager(Map<Integer, List<Integer>> newNpcLinks,
Map<Integer, Integer> existingNpcFactions,
Set<Integer> aliveNpcs) { // Pass the alive set or isAlive mechanism
this.links = newNpcLinks;
this.npcFactions = existingNpcFactions;
this.currentAliveNpcs = aliveNpcs; // Store the reference
}
// Dummy isAlive for this class context
private boolean isNpcAlive(int npcId) {
// In a real scenario, this might query a game state manager
return this.currentAliveNpcs.contains(npcId);
}
/**
* Checks if a player is allowed to attack a target NPC based on the rules.
*
* @param playerFactionId The faction ID of the attacking player.
* @param targetNpcId The ID of the NPC being attacked.
* @return true if the attack is allowed, false if it should be stopped.
*/
public boolean canPlayerAttack(int playerFactionId, int targetNpcId) {
// 1. Check if the target NPC is one of the "new" NPCs with special rules
if (!links.containsKey(targetNpcId)) {
return true; // Not a specially linked NPC, allow attack
}
// 2. Get the group of existing NPCs linked to this new NPC
List<Integer> linkedGroup = links.get(targetNpcId);
// 3. Find which (if any) of the linked NPCs is alive
for (int linkedNpcId : linkedGroup) {
if (isNpcAlive(linkedNpcId)) {
// 4. Get the faction of the ALIVE linked NPC
Integer aliveNpcFactionId = npcFactions.get(linkedNpcId);
// This check prevents NullPointerException if data is inconsistent
if (aliveNpcFactionId == null) {
System.err.println("Warning: Alive NPC " + linkedNpcId + " has no faction mapping.");
continue; // Or decide how to handle inconsistent data
}
// 5. Compare player's faction with the ALIVE NPC's faction
if (playerFactionId == aliveNpcFactionId) {
// Player's faction matches the faction of the ALIVE linked NPC
System.out.println("Attack Stopped: Player Faction " + playerFactionId
+ " cannot attack NPC " + targetNpcId
+ " because linked NPC " + linkedNpcId + " (Faction " + aliveNpcFactionId + ") is alive.");
return false; // Stop the attack
} else {
// An NPC from a *different* faction is alive, player can attack
// Since only one from the group should be alive, we can stop checking the group.
System.out.println("Attack Allowed: Player Faction " + playerFactionId
+ " can attack NPC " + targetNpcId
+ ". Although linked NPC " + linkedNpcId + " (Faction " + aliveNpcFactionId + ") is alive, it's not the player's faction.");
return true; // Allow attack
}
}
}
// 6. If loop finishes, none of the linked NPCs were found alive (or data was inconsistent)
System.out.println("Attack Allowed: Player Faction " + playerFactionId
+ " can attack NPC " + targetNpcId + ". No relevant linked NPC found alive.");
return true; // Allow attack
}
}
// --- Example Usage for Solution #1 ---
public class Solution1_Main {
// Assume setupData(), common maps, and isAlive() are defined globally or accessible here
static Map<Integer, List<Integer>> newNpcLinks = new HashMap<>();
static Map<Integer, Integer> existingNpcFactions = new HashMap<>();
static Set<Integer> aliveNpcs = new HashSet<>(Arrays.asList(101, 202)); // Example alive NPCs
static boolean isAlive(int npcId) { return aliveNpcs.contains(npcId); } // Dummy needed if manager doesn't implement it internally
static void setupData() { /* ... copy setupData() from above ... */
int Faction1 = 1, Faction2 = 2, Faction3 = 3;
existingNpcFactions.put(101, Faction1); existingNpcFactions.put(102, Faction1);
existingNpcFactions.put(201, Faction2); existingNpcFactions.put(202, Faction2);
existingNpcFactions.put(301, Faction3); existingNpcFactions.put(302, Faction3);
newNpcLinks.put(401, Arrays.asList(101, 201, 301));
newNpcLinks.put(404, Arrays.asList(102, 202, 302));
}
public static void main(String[] args) {
setupData(); // Initialize the maps
// Create the manager instance
AttackRuleManager attackManager = new AttackRuleManager(newNpcLinks, existingNpcFactions, aliveNpcs);
// --- Test Cases ---
System.out.println("--- Testing NPC 401 (Linked to 101[A], 201, 301) ---");
// Player Faction 1 attacks 401. NPC 101 is alive. Player faction == Alive NPC faction. -> Block
attackManager.canPlayerAttack(1, 401); // Expected: false (Blocked)
// Player Faction 2 attacks 401. NPC 101 is alive. Player faction != Alive NPC faction. -> Allow
attackManager.canPlayerAttack(2, 401); // Expected: true (Allowed)
// Player Faction 3 attacks 401. NPC 101 is alive. Player faction != Alive NPC faction. -> Allow
attackManager.canPlayerAttack(3, 401); // Expected: true (Allowed)
System.out.println("\n--- Testing NPC 404 (Linked to 102, 202[A], 302) ---");
// Player Faction 1 attacks 404. NPC 202 is alive. Player faction != Alive NPC faction. -> Allow
attackManager.canPlayerAttack(1, 404); // Expected: true (Allowed)
// Player Faction 2 attacks 404. NPC 202 is alive. Player faction == Alive NPC faction. -> Block
attackManager.canPlayerAttack(2, 404); // Expected: false (Blocked)
// Player Faction 3 attacks 404. NPC 202 is alive. Player faction != Alive NPC faction. -> Allow
attackManager.canPlayerAttack(3, 404); // Expected: true (Allowed)
System.out.println("\n--- Testing Regular NPC 500 (Not Linked) ---");
// Player Faction 1 attacks 500. Not a linked NPC -> Allow
attackManager.canPlayerAttack(1, 500); // Expected: true (Allowed)
}
}