2014年2月19日 星期三

The Lua-based AI System

目前規劃了一個以LUA Script為架構的AI系統,基礎框架為如下:
function Stroll_Enter(Brain, FromStateName)
end
function Stroll_Update(Brain, fDeltaTime)
end
function Stroll_Exit(Brain)
end
C++程式的部份預設會Call Stroll這個State的Enter為進入點,之後可以透過ChangeState這個C++綁定的成員函式來達到切換行為的目的,比方Brain:ChangeState("Fight")…
下列是目前有關這個Demo Game的相關AI Script的內容:

--------------------------------------------------------------------------
NormalFightAI.lua:
math.randomseed(os.time())
function Stroll_Enter(Brain, FromStateName)
Brain:ResetReponseIntervalTime(3.0)
end
function Stroll_Update(Brain, fDeltaTime)
if Brain:IsReponse() and (Brain:IsProcessPath() == false) then
local EnemyInfo = CEnemyInformation()
if Brain:FindEnemy(10.0, CGameLogic.ENEMY_GROUP_TYPE_PLAYER, EnemyInfo) then
if Brain:IsFollowTransform() then
local TargetPosition = Brain:GetTransformByID(EnemyInfo.m_iObjectID):GetPosition()
Brain:GetSelfTransform():ReplaceYawAngle(Brain:GetTargetAngle(TargetPosition),
true)
Brain:UsetSkill(0)
else
Brain:FollowTransform(EnemyInfo.m_iObjectID, 5.0)
end
else
local iRandomValue = math.random(7)
if iRandomValue <= 2 then
local pSelfTransform = Brain:GetSelfTransform()
local DestPosition = pSelfTransform:GetPosition()
local Movement = CVector3()
Movement:ReadFormAngle(math.random()*360.0, 0.0)
Movement = Movement*10.0
DestPosition = DestPosition+Movement
Brain:MoveToTargetPosition(DestPosition, true)
end
end
end
end
function Stroll_Exit(Brain)
end
--------------------------------------------------------------------------
FollowAI.lua:
math.randomseed(os.time())

function Stroll_Enter(Brain, FromStateName)
Brain:ResetReponseIntervalTime(3.0)
end
function Stroll_Update(Brain, fDeltaTime)
if Brain:IsFollowTransform() == false then
local EnemyInfo = CEnemyInformation()
if Brain:FindEnemy(10.0, CGameLogic.ENEMY_GROUP_TYPE_PLAYER, EnemyInfo) then
Brain:FollowTransform(EnemyInfo.m_iObjectID, 5.0)
elseif Brain:IsReponse() and (Brain:IsProcessPath() == false) then
local pSelfTransform = Brain:GetSelfTransform()
local DestPosition = pSelfTransform:GetPosition()
local Movement = CVector3()
Movement:ReadFormAngle(math.random()*360.0, 0.0)
Movement = Movement*20.0
DestPosition = DestPosition+Movement
Brain:MoveToTargetPosition(DestPosition, true)
end
end
end
function Stroll_Exit(Brain)
end
--------------------------------------------------------------------------
AfraidAI.lua:

math.randomseed(os.time())

function Stroll_Enter(Brain, FromStateName)

Brain:ResetReponseIntervalTime(3.0)
end
function Stroll_Update(Brain, fDeltaTime)
if Brain:IsReponse() and (Brain:IsProcessPath() == false) then
local iRandomValue = 2
local EnemyInfo = CEnemyInformation()
local Movement = CVector3()
local fMoveDistance = 10.0
local SelfPosition = Brain:GetSelfTransform():GetPosition()
if Brain:FindEnemy(25.0, CGameLogic.ENEMY_GROUP_TYPE_PLAYER, EnemyInfo) then
Movement = SelfPosition-Brain:GetTransformByID(EnemyInfo.m_iObjectID):GetPosition()
Movement:Normalize()
Brain:ResetReponseIntervalTime(0.5)
fMoveDistance = 15.0
else
iRandomValue = math.random(7)
if iRandomValue <= 2 then
Movement:ReadFormAngle(math.random()*360.0, 0.0)
Brain:ResetReponseIntervalTime(2.0)
end
end
if iRandomValue <= 2 then
Movement = Movement*fMoveDistance
Brain:MoveToTargetPosition(SelfPosition+Movement, true)
end
end
end
function Stroll_Exit(Brain)
end
--------------------------------------------------------------------------
上述的範例都用了太多的if else,應該要切成不同的State來整理才對…
Demo影片如下:



Dream continues in...

沒有留言:

張貼留言