GOAP
Using King Commands
Overview
A King Command is a command given by the player that the controller will resolve and send to appropriate aliens (priority + JobType + distance). Then the GOAP resolver in workers will process those commands.
How to add a king command
- Add a new Goap Action in /Code/AI/Actions/Command by copying from _ExampeCommandAction.cs
// /Code/AI/Actions/Command/NewCommandAction.cs
using CrashKonijn.Agent.Core;
using CrashKonijn.Agent.Runtime;
using CrashKonijn.Goap.Runtime;
using Object;
using UnityEngine;
namespace AI
{
public class NewCommandAction : GoapActionBase<NewCommandAction.Data>, IKingCommandAction
{
// This method is called every frame before the action is performed
// If this method returns false, the action will be stopped
// This method is optional and can be removed
public override bool IsValid(IActionReceiver agent, Data data)
{
return true;
}
// This method is called when the action is started
// This method is optional and can be removed
public override void Start(IMonoAgent agent, Data data)
{
}
// This method is called once before the action is performed
// This method is optional and can be removed
public override void BeforePerform(IMonoAgent agent, Data data)
{
}
// This method is called every frame while the action is running
// This method is required
public override IActionRunState Perform(IMonoAgent agent, Data data, IActionContext context)
{
return ActionRunState.Completed;
}
// This method is called when the action is completed
// This method is optional and can be removed
public override void Complete(IMonoAgent agent, Data data)
{
if (data.Target is InstanceTarget<MonoBehaviour> instanceTarget)
{
var target = instanceTarget.Instance;
// Your logics go here
};
// Make sure to call this to complete the command, otherwise it will run forever
data.Job.CompleteCommand();
}
// This method is called when the action is stopped
// This method is optional and can be removed
public override void Stop(IMonoAgent agent, Data data)
{
data.Job.CancelCommand();
}
// This method is called when the action is completed or stopped
// This method is optional and can be removed
public override void End(IMonoAgent agent, Data data)
{
}
// The action class itself must be stateless!
// All data should be stored in the data class
public class Data : IActionData
{
[GetComponent]
public AlienJob Job { get; set; }
public ITarget Target { get; set; }
}
}
}
- In Code/~SharedREF/Alien/AlienJobs.cs, add to enum JobAction with the exact same name as the Goap Action and mapped JobType
public enum JobAction
{
...
[MappedJob(JobType.Builder)]
NewCommandAction,
}
- Use KingCommandPreset to add a Goap Action to Base Capability
// Code/AI/Capability/BaseCapability.cs
using AI;
using CrashKonijn.Goap.Core;
using CrashKonijn.Goap.Runtime;
namespace AI
{
public class BaseCapability : CapabilityFactoryBase
{
public override ICapabilityConfig Create()
{
var builder = new CapabilityBuilder("BaseCapability");
// Must Add Command Sensors and Goal
builder.AddMultiSensor<CommandSensors>();
KingCommandPreset.AddKingCommandGoal(ref builder);
// Add King Command Action
// You can add more command action as you wish
KingCommandPreset.AddCommandAction<NewCommandAction>(ref builder);
return builder.Build();
}
}
}
- Add command to the JobController
InitializeObject();
// Add King Command
var itemEntity = obj.GetComponent<ItemEntity>();
if (dragDetectionRing.pointerEntity)
{
// Give command to the selected villager
JobController.AddCommand(JobAction.ToolPickUpCommandAction, itemEntity, dragDetectionRing.pointerEntity.Job);
}
else
{
// Give command to the commands pool
JobController.AddCommand(JobAction.ToolPickUpCommandAction, itemEntity, 1);
}