Skip to content

Zombie AI Setup

Roll4d4 edited this page Feb 24, 2025 · 5 revisions

🧠 Zombie AI Example: Autonomous Decision-Making with KLEP


Overview:

This example demonstrates how KLEP enables autonomous decision-making by an AI agent, highlighting the use of sensors, routers, goals, and dynamic actions. The setup builds upon the character controller example, transitioning from manual input control to fully autonomous AI behavior.

The Zombie AI Will:

  • Detect nearby mobs using a sensor.
  • Process detected data through a router to generate mock input.
  • Move toward the target using the same movement action as the character controller.
  • Initiate an "attack" when within range, changing the target's color to visualize the attack state.

⚙️ Setup Steps:

📷

Starting setup

1. Create the AI Agent:

  • Duplicate the character controller setup from the previous example.
  • Disable user input:
    • Remove or disable the Input Sensor.
    • The zombie will no longer respond to keyboard and mouse events.
    • In KLEP, disabled behaviors are treated as removed from the system.

📷

Starting setup

2. Set Up Team Keys and Key Loader:

  • Navigate to the Key Loader (or create a new one) and add a new property:
    • Property Name: TeamID
    • Property Type: Integer (int)

Create Team Keys:

  1. Right-click in the Project panel and select KLEP/Key.
  2. Rename the new key to TeamA_KEY.
  3. Set the key name to Team_KEY, and TeamID to 1.
  4. Duplicate the key, rename it to TeamB_KEY, and set TeamID to 2.
  5. Assign TeamA_KEY to the player mob and TeamB_KEY to the zombie mob.

3. Add a Mob Detection Sensor:

  • Attach a sensor to the zombie:
    • This sensor will generate a Mob_KEY for any detected target within a specified radius.
    • The Mob_KEY will carry data about the target's transform, which is necessary for both movement and attack actions.
    • The sensor will check the target's neuron for a TeamID_KEY. If the target's team is not the same as the zombie's team, it will generate a Mob_KEY as a valid target.

4. Set Up the Movement Router:

  1. Create an empty GameObject under the goal object, rename it to Routers, and add a child GameObject.
  2. Rename this object to MoveToEnemy.
  3. Assign the Zombie_MoveToEnemy router script to this object.

Router Behavior:

  • The router will emit mock input keys, such as:
    • W_Hold, A_Hold, S_Hold, D_Hold for movement controls.
    • MouseData_KEY to simulate the camera focusing on the target.
  • The movement action will interpret these keys as if they were real player input, causing the zombie to move autonomously.

5. Add the Attack Action:

  1. Create a new empty GameObject under the goal object, rename it to Actions, and add a child GameObject.
  2. Rename this object to AttackPlayer.
  3. Assign the AttackPlayerAction script to this object.

Attack Behavior:

  • When the target is within attack range, the attack action will fire.
  • The target's renderer will change color to red, indicating an attack state.
  • If the target moves out of range, the renderer resets to its default color.

📷

Making Goal

6. Set Executables to Be Managed by the Goal:

  • Toggle the "Managed By Goal" boolean to true for both AttackPlayerAction and Zombie_MoveToEnemy.
  • This ensures that only the goal will control when these executables fire, preventing the agent from independently choosing them.
  • Goals can manage other goals, so nested goals will also be treated as managed.

📷

Making Goal


7. Create the AttackPlayerGoal:

  1. Create a new script if you have not already done so, call it AttackPlayerGoal.
  2. Inherit from KLEPGoal, leaving the script body empty (all necessary functionality is handled by the base class).
  3. Assign this script to the parent object that holds the router and attack executables.

8. Set Up Goal Layers:

  • Navigate to the Executable Layers list in the goal inspector.
  • Add a new layer and assign the router (Zombie_MoveToEnemy) to Layer 1.
  • Set the execution requirement to "None Need to Fire":
    • This allows the goal to progress to the next layer without waiting for the router to complete.
  • Add another layer and assign the AttackPlayerAction to it.

📷

GoalLayers

🧠 Understanding Goal Layer Execution:

  • Layer 1: The router provides mock input to move towards the target.
  • Layer 2: The attack action will activate if the target is within range.
  • The goal can be set to complete when the attack action fires, or remain active until specific conditions are met (e.g., goal completion criteria).

🎬 Visual Demonstration:

zombie Klep


Key Takeaways:

  • Routers translate data into actionable input for executables.
  • Goals manage complex AI behavior, enabling layered decision-making.
  • The AI agent can fluidly switch between actions, utilizing the KLEP framework for smart behavior orchestration.

💡 Next Steps:

  • Experiment with additional routers to introduce behaviors like fleeing, seeking cover, or patrolling.
  • Create advanced goals that allow the zombie to prioritize actions based on dynamic environmental cues.

By utilizing KLEP's modular architecture, you can easily extend AI behaviors, adding new reactions, adaptive responses, and complex interactions with minimal additional coding. This example showcases how symbolic reasoning can create powerful, adaptive AI systems for games and simulations.

KLEP Wiki Navigation

🚀 Getting Started

🧠 Core Concepts

🎮 Examples and Tutorials

🛠️ Scripts and Components

💡 Experimental/Project Projections

🧬 Contributing

Clone this wiki locally