通用C#goap(以目标为导向的动作计划)库,其中包含Unity3D示例和助手课。
该库非常通用,如果您不包含Unity文件夹,则可以在任何游戏引擎中使用它。
首先在此处检查Unity FSM示例。
此示例使用与简单的FSM(有限状态机)团结起来的重新匹配库来处理宏观行为(在大多数游戏中,三个FSM状态应足够:空闲,goto,andimate)。
要使用它创建一个新的Unity项目,请打开外壳,转到资产目录,并在其中克隆整个存储库:
git clone https://github.com/luxkun/ReGoap.git(在Windows中,您可以使用命令行或任何GIT客户端进行操作,也可以单击“克隆或下载”,然后单击“下载zip”)
另外,您只需下载上一个版本的Unity软件包,这可能不会在此处或Unity Asset Store上进行最新更改。
(如果您只想使用图书馆,并希望有一个解释的示例跳过如何使用重新加工)
在解释如何在游戏中使用此库之前,让我解释一下goap系统如何工作,从杰夫·奥金(Jeff Orkin)的报价开始
Goal-Oriented Action Planning (aka GOAP, rhymes with soap) refers to a simplfied STRIPS-like planning architecture specifically designed for real-time control of autonomous character behavior in games.
基本上,它要做的就是找到一个计划(行动列表),该计划将实现所选择的目标。
您需要了解的主要概念是:状态,行动,目标,记忆和传感器
是世界的定义,在此库中,它们被用作字符串字典到对象的字典(字典<string,object>)。
在此文件中查看RegoApstate类:https://github.com/luxkun/regoap/blob/master/master/regoap/core/regoapstate.cs
示例: 'isat':'敌人','iswarned':true,'hasweapon':true
可以定义为前提条件和效果的列表,这些是代理(从现在开始开始称为代理商)可以做到的动作。
前提条件是要求采取行动必须被描述为一种状态,顾名思义,效果是行动的效果,也是一个状态。
示例:
重要的是:错误的先决条件不支持重要:操作完成后未写入记忆的动作效果,这是通缉行为,因为在大多数游戏中,您都需要从内存或传感器设置这些变量。如果您愿意,可以覆盖重新划分中的退出,并将效果设置为内存,示例如下。
可以定义为所谓的状态列表,这基本上是代理应该做的。
示例:
是对代理的记忆,代理商知道和感觉都应该在此处插入。在此库中,内存也可以具有许多传感器,这些传感器是内存辅助器。基本上,内存的工作是创建和保持更新的“世界”状态。
是内存辅助器,应处理特定范围。
例子:
现在,您应该了解什么是goap库的目的以及应该使用的内容,如果仍然有疑问或想了解更多有关此字段的信息,我建议您在此处阅读Jeff Orkin的论文:http://alumni.media.mit。 edu/〜Jorkin/
git clone https://github.com/luxkun/ReGoap.git还有什么?实际上,图书馆将处理所有计划,选择完成目标并运行第一个计划,直到完成目标,然后第二个目标,依此类推,您需要做的就是实施自己的行动和目标。
在下一段中,我将解释如何创建自己的类(但是对于大多数行为,您需要实现的只是Goapaction和Goapgoal)。
在此示例中查看操作:https://github.com/luxkun/regoap/ree/master/master/regoap/regoap/unity/fsmexample/actions/actions
查看重新竞争实现,以查看可以覆盖哪些功能:https://github.com/luxkun/regoap/regaap/blob/master/regoap/regoap/regoap/unity/regoapaction.cs
您必须通过实现IREGOAPACTION接口或继承重新配置来实现自己的重新启动。明智地选择通用类型,它们必须在代理的所有类别中相同。通常,弦,对象是最通用的,也是int/enum,对象同样通用但更轻。
对于简单的实施,您要做的就是这样做:
public class MyGoapAction : ReGoapAction < string , object >
{
protected override void Awake ( )
{
base . Awake ( ) ;
preconditions . Set ( " myPrecondition " , myValue ) ;
effects . Set ( " myEffects " , myValue ) ;
}
public override void Run ( IReGoapAction < string , object > previous , IReGoapAction < string , object > next , ReGoapState < string , object > settings , ReGoapState < string , object > goalState , Action < IReGoapAction < string , object > > done , Action < IReGoapAction < string , object > > fail )
{
base . Run ( previous , next , goalState , done , fail ) ;
// do your own game logic here
// when done, in this function or outside this function, call the done or fail callback, automatically saved to doneCallback and failCallback by ReGoapAction
doneCallback ( this ) ; // this will tell the ReGoapAgent that the action is succerfully done and go ahead in the action plan
// if the action has failed then run failCallback(this), the ReGoapAgent will automatically invalidate the whole plan and ask the ReGoapPlannerManager to create a new plan
}
}如默认情况下,重新启动之前所编写的,请在内存上写下效果,但是内存应该检查是否有效地完成了效果,如果您出于任何原因要在操作结束时设置效果,则可以添加此代码是您的重新启动实现:
public override void Exit ( IReGoapAction < string , object > next )
{
base . Exit ( next ) ;
var worldState = agent . GetMemory ( ) . GetWorldState ( ) ;
foreach ( var pair in effects ) {
worldState . Set ( pair . Key , pair . Value ) ;
}
}您还可以根据下一个动作的先决条件/效果动态更改前提条件和效果,例如,您如何处理代理中的goto操作。
请查看FSMEXAMPEl以查看如何执行此操作:https://github.com/luxkun/regoap/blob/master/master/regoap/regoap/regoap/fsmexample/campample/actions/genericgotoaction.cs
这并不那么棘手,大多数目标只会覆盖清醒功能以添加您自己的目标状态(目标)。
无论如此
还要在此示例中查看目标:https://github.com/luxkun/regoap/ree/master/master/regoap/regoap/unity/fsmexample/goals
public class MyGoapGoal : ReGoapGoal < string , object >
{
protected override void Awake ( )
{
base . Awake ( ) ;
goal . Set ( " myRequirement " , myValue ) ;
}
}注意:如果您想自动警告代理商,请确保使用RecoApgoalAdvanced。
在此处查看Goapsensor Basic类:https://github.com/luxkun/regoap/blob/master/regoap/regoap/unity/goapsensor.cs
在此处查看示例:https://github.com/luxkun/regoap/ree/master/master/regoap/regoap/unity/fsmexample/sensors
与往常一样,您必须通过继承RegoApsenSor或实现IREGOAPSENSOR接口来实现自己的类。
public class MySensor : ReGoapSensor < string , object >
{
void FixedUpdate ( )
{
var worldState = memory . GetWorldState ( ) ;
worldState . Set ( " mySensorValue " , myValue ) ; // like always myValue can be anything... it's a GoapState :)
}
}注意:由于基本类不检查和更新传感器,因此请确保在使用传感器时使用RegoApMemoryAdvanced。
要调试自己的代理商,您当然可以与自己喜欢的编辑一起自行调试。
但是Regoap在Unity的代理商中有一个非常用户的调试器(https://github.com/luxkun/regoap/regaap/reglob/master/regaap/regoap/regoap/regaapnoditor/regoapnodeeditor.cs and https:/ https://github.com/luxkun/regoap/ blob/master/regoap/unity/editor/regoapnodebaseeditor.cs)。
要使用它,只需单击Unity的菜单窗口,然后再重新加入调试器,将打开一个Unity窗口,这是Agent Debugger。
现在,如果您单击场景中的任何代理商(在播放时,仅在运行代理上工作),窗口将自动更新,让您知道代理人的“思想”(当前的世界状态,选择的目标和当前计划和当前计划,可能性目标,可能的目标,可能的动作,可以做什么,什么是什么,试试看!)。
对任何拉的请求都表示赞赏,只需确保在进行提交之前检查Unity测试(菜单窗口- >编辑器测试跑步者- >运行全部)并保留相同的代码样式。