通用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測試(菜單窗口- >編輯器測試跑步者- >運行全部)並保留相同的代碼樣式。