-
Notifications
You must be signed in to change notification settings - Fork 0
KlepAgent
Roll4d4 edited this page Feb 23, 2025
·
1 revision
The KLEPAgent is the core decision-maker within the KLEP system. It is responsible for evaluating available executables, selecting the most appropriate action, and managing the execution cycle of KLEP behaviors.
The KLEPAgent serves as the AI controller, performing the following key functions:
-
Initialization:
- Binds to a parent neuron and registers necessary events.
- Prepares the agent to react to key changes and trigger behavior evaluations.
-
Update Management:
- Handles standard Unity update cycles (
UpdateandFixedUpdate). - Manages in-tandem and solo executables, ensuring appropriate execution order.
- Handles standard Unity update cycles (
-
Behavior Selection:
- Evaluates all available executables and selects the best option based on validation and execution checks.
-
Execution Control:
- Executes in-tandem behaviors as background tasks.
- Prioritizes and executes a single solo action if available and valid.
public void Initialize(KLEPNeuron neuron)
{
parentNeuron = neuron;
parentNeuron.bridge.RegisterEvent("KeyAdded", OnKeyAddedEvent);
}- Registers events with the SLASHBridge, enabling the agent to react to key changes.
- Prepares for dynamic behavior changes by rechecking in-tandem executables when new keys are added.
public void AgentUpdate()
{
if (parentNeuron == null) return;
var currentKeys = parentNeuron.heldKeys;
var allExecutables = parentNeuron.heldExecutables;
var inTandemExecutables = allExecutables.Where(exe => !exe.IsManagedByGoal && exe.InTandem).ToList();
var soloExecutables = allExecutables.Where(exe => !exe.InTandem && !exe.IsManagedByGoal).ToList();
foreach (var exe in inTandemExecutables)
{
if (exe.parentNeuron == null || !exe.gameObject.activeInHierarchy) continue;
exe.ExecutableUpdates();
}
ExecuteInTandemExecutables(inTandemExecutables);
DecideAndExecuteSoloAction(soloExecutables, currentKeys);
}- Executes in-tandem behaviors immediately if conditions are met.
- Selects and executes a single solo action based on validation and execution checks.
public void AgentFixedUpdate(HashSet<KLEPKey> fixedKeys)
{
if (parentNeuron == null) return;
var allExecutables = parentNeuron.heldExecutables;
var inTandemExecutables = allExecutables.Where(exe => !exe.IsManagedByGoal && exe.InTandem).ToList();
var soloExecutables = allExecutables.Where(exe => !exe.InTandem && !exe.IsManagedByGoal).ToList();
foreach (var exe in inTandemExecutables)
{
if (exe.parentNeuron == null || !exe.gameObject.activeSelf) continue;
exe.ExecutableFixedUpdate();
}
ExecuteInTandemExecutablesFixed(inTandemExecutables, fixedKeys);
DecideAndExecuteSoloActionFixed(soloExecutables, fixedKeys);
}- Handles physics-related execution during FixedUpdate.
- Ensures timing-sensitive behaviors are synchronized with Unity's physics engine.
private void ExecuteInTandemExecutables(List<KLEPExecutableBase> inTandemExecutables)
{
recheckInTandemExecutables = true;
int val = 0;
while (recheckInTandemExecutables)
{
recheckInTandemExecutables = false;
if (val > inTandemRecheckLimit) break;
foreach (var executable in inTandemExecutables.ToList())
{
if (executable.parentNeuron == null || !executable.gameObject.activeInHierarchy)
continue;
if (executable.CanValidate(parentNeuron.heldKeys) && executable.CanExecute(parentNeuron.heldKeys))
{
executable.Execute();
recheckInTandemExecutables = true;
}
}
if (inTandemExecutables.Count == 0) recheckInTandemExecutables = false;
val++;
}
}- In-tandem executables are executed immediately when conditions are met.
- The system rechecks in-tandem actions if new keys are generated, allowing for dynamic reactions within a single frame.
private KLEPExecutableBase SelectBestAction(HashSet<KLEPKey> currentElements, List<KLEPExecutableBase> soloActions)
{
float highestValue = float.MinValue;
KLEPExecutableBase selectedAction = null;
foreach (var action in soloActions)
{
if (!action.gameObject.activeInHierarchy) continue;
float attractionValue = action.CalculateAttraction(currentElements);
float combinedValue = attractionValue;
if (combinedValue > highestValue)
{
highestValue = combinedValue;
selectedAction = action;
}
}
return selectedAction;
}- Evaluates all solo actions and selects the most attractive option.
- Attraction values are calculated based on keys present and lock conditions, enabling smart prioritization.
-
In-Tandem vs. Solo Execution:
- In-tandem executables: Always fire if valid and executable.
- Solo executables: Undergo a selection process, with only one action firing per frame.
-
Reactivity to Key Changes:
- Listens for key additions and rechecks behavior options dynamically.
-
Execution Layers:
- Update cycle: For real-time behaviors.
- FixedUpdate cycle: For physics-based actions.
-
Smart Decision-Making:
- Uses validation and execution checks to prioritize actions.
- Supports adaptive AI behavior by balancing desirability and feasibility.
-
In-Tandem Executables:
- Always checks for the player's presence using a sensor.
- Updates patrol behavior if no target is found.
-
Solo Executables:
- Evaluates whether to attack, hide, or search for resources.
- Only the most attractive behavior is executed per frame, preventing erratic behavior.
This explanation covers the KLEPAgent's role, its execution flow, and key mechanisms for behavior selection. If you need further focus on specific features, like goal handling or event-driven actions, let me know! ๐