Create a complete, modular, and advanced AI system in Unreal Engine. The system should integrate dynamic behaviors, perception systems, navigation, state machines, combat logic, and multiplayer compatibility. Follow these detailed instructions:


---

Phase 1: Planning & Design

1. AI System Scope & Architecture:

Design AI for multiple use cases: patrolling NPCs, combat enemies, and interactive companions.

Implement a hybrid behavior system combining Behavior Trees and State Machines.

Include a Blackboard for shared memory between behaviors and controllers.





---

Phase 2: Core Systems Implementation

2. AI Controller & Blackboard Setup:

Create a custom AI Controller class using Blueprints and C++.

Attach a Blackboard Asset to store variables like player location, health, or objectives.

Link the AI Controller with the corresponding AI Behavior Tree.



3. Behavior Tree Development:

Build the primary Behavior Tree Asset for the AI’s logic flow.

Add tasks for Patrolling, Chasing, Attacking, and Idle States.

Include Decorators to check conditions (e.g., “Is Player in Range?”).

Use Services to update key variables (e.g., target distance or health) dynamically.





---

Phase 3: Navigation System Configuration

4. NavMesh Setup for Pathfinding:

Place a NavMesh Bounds Volume covering the level.

Add NavLink Proxies for custom actions like jumping over gaps or climbing.

Configure the AI to recognize dynamic obstacles and replan its path as needed.



5. AI Movement Logic:

Use Simple Move To Actor and Move To Location functions for navigation tasks.

Set up fallback logic for when paths become blocked (e.g., use alternative routes).





---

Phase 4: Perception and Sensory Systems

6. AI Perception System Setup:

Add the AI Perception Component to handle multiple sensory inputs.

Configure AISense_Sight for vision-based detection with adjustable range and angle.

Add AISense_Hearing to detect player sounds (e.g., footsteps or gunfire).


Use Perception Events to trigger behavior transitions (e.g., start chasing when player is detected).



7. Environment Query System (EQS):

Create EQS queries for finding cover spots or detecting enemies dynamically.

Integrate EQS into the Behavior Tree for decision-making.

Example: Query the nearest safe cover when health is low.






---

Phase 5: State Machines and Combat Logic

8. State Machine Integration:

Build a custom state machine for combat (e.g., Idle, Attack, Retreat, Reload).

Define clear transitions between states (e.g., from Attack to Retreat based on health).

Sync with Behavior Tree actions to keep logic modular.



9. Combat AI Implementation:

Design attack patterns with cooldowns and skill usage.

Add threat level management to prioritize targets based on player actions.

Use prediction algorithms to anticipate player movements during combat (e.g., aiming ahead of moving players).

Integrate animations via Montages for attacks and special moves.





---

Phase 6: Communication & Game Integration

10. Interfaces and Event Dispatchers:



Use Blueprint Interfaces to enable AI communication with other game systems (e.g., detect power-ups, interact with doors).

Implement Event Dispatchers to trigger actions (e.g., notify when a companion AI spots the player).

Add custom events for multiplayer synchronization (e.g., AI actions replicated across clients).


11. Multiplayer Network Synchronization:



Ensure all AI actions are properly replicated for multiplayer (e.g., combat actions, state changes).

Use RPCs (Remote Procedure Calls) to sync important behaviors across server and clients.



---

Phase 7: Debugging, Optimization & Playtesting

12. Debugging AI Logic:



Use Behavior Tree Debugger to visualize the AI’s decision-making process in real-time.

Activate the EQS Debugger to inspect query results during gameplay.

Monitor Blackboard variables to detect logic inconsistencies.


13. Performance Optimization:



Set Tick Intervals appropriately to reduce AI load (e.g., update every 0.5s instead of every frame).

Optimize NavMesh and EQS queries for larger maps.

Use LOD (Level of Detail) for distant AI to reduce processing needs.

Batch AI updates for groups to avoid overloading the CPU.


14. Playtesting and Difficulty Balancing:



Test AI in different gameplay scenarios (combat, stealth, navigation).

Adjust sensory ranges, aggression levels, and response times based on playtest feedback.

Add dynamic difficulty scaling (e.g., AI adapts to player skill by increasing aggression or movement speed).



---

Phase 8: Final Polishing and Animation Sync

15. Animation and Blendspaces Integration:



Use Animation Blueprints to sync AI movements and actions smoothly.

Create Blendspaces for walking, running, and turning animations.

Add Attack Montages for combat sequences.


16. Edge Case Handling:



Add recovery logic for AI getting stuck (e.g., reset pathfinding if idle for too long).

Implement fail-safes for invalid states (e.g., default to idle if logic fails).



---

Phase 9: Deployment and Finalization

17. Polish and Deploy:



Make final tweaks based on profiling results and playtest feedback.

Package the AI system with the game and ensure it works seamlessly in all scenarios.



---

Deliverables:

1. A fully functional AI Controller with Behavior Trees and Blackboards.


2. Configured Perception System with sensory detection for sight and sound.


3. State Machine Logic integrated with combat and patrol behaviors.


4. Optimized Navigation System with fallback logic for pathfinding.


5. Multiplayer-compatible AI actions, properly synchronized across clients and servers.


6. Debug tools and optimization mechanisms implemented for stable performance.


7. Animation Blueprint integrated with smooth blendspaces for seamless behavior.




---

This detailed prompt ensures every component of the AI system is correctly implemented, tested, and optimized for both single-player and multiplayer environments.